2 * Implementation of SCSI Sequential Access Peripheral driver for CAM.
4 * Copyright (c) 1999, 2000 Matthew Jacob
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/queue.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/types.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
46 #include <sys/sysctl.h>
47 #include <sys/taskqueue.h>
49 #include <sys/fcntl.h>
50 #include <sys/devicestat.h>
58 #include <cam/cam_ccb.h>
59 #include <cam/cam_periph.h>
60 #include <cam/cam_xpt_periph.h>
61 #include <cam/cam_debug.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_message.h>
65 #include <cam/scsi/scsi_sa.h>
72 #define SA_IO_TIMEOUT 4
74 #ifndef SA_SPACE_TIMEOUT
75 #define SA_SPACE_TIMEOUT 1 * 60
77 #ifndef SA_REWIND_TIMEOUT
78 #define SA_REWIND_TIMEOUT 2 * 60
80 #ifndef SA_ERASE_TIMEOUT
81 #define SA_ERASE_TIMEOUT 4 * 60
84 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
86 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
87 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
88 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
89 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
92 * Additional options that can be set for config: SA_1FM_AT_EOT
95 #ifndef UNUSED_PARAMETER
96 #define UNUSED_PARAMETER(x) x = x
100 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
101 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
107 static MALLOC_DEFINE(M_SCSISA, "SCSI sa", "SCSI sequential access buffers");
110 SA_STATE_NORMAL, SA_STATE_ABNORMAL
113 #define ccb_pflags ppriv_field0
114 #define ccb_bp ppriv_ptr1
116 #define SA_CCB_BUFFER_IO 0x0
117 #define SA_CCB_TYPEMASK 0x1
118 #define SA_POSITION_UPDATED 0x2
120 #define Set_CCB_Type(x, type) \
121 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
122 x->ccb_h.ccb_pflags |= type
124 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
129 SA_FLAG_OPEN = 0x0001,
130 SA_FLAG_FIXED = 0x0002,
131 SA_FLAG_TAPE_LOCKED = 0x0004,
132 SA_FLAG_TAPE_MOUNTED = 0x0008,
133 SA_FLAG_TAPE_WP = 0x0010,
134 SA_FLAG_TAPE_WRITTEN = 0x0020,
135 SA_FLAG_EOM_PENDING = 0x0040,
136 SA_FLAG_EIO_PENDING = 0x0080,
137 SA_FLAG_EOF_PENDING = 0x0100,
138 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
139 SA_FLAG_EOF_PENDING),
140 SA_FLAG_INVALID = 0x0200,
141 SA_FLAG_COMP_ENABLED = 0x0400,
142 SA_FLAG_COMP_SUPP = 0x0800,
143 SA_FLAG_COMP_UNSUPP = 0x1000,
144 SA_FLAG_TAPE_FROZEN = 0x2000
148 SA_MODE_REWIND = 0x00,
149 SA_MODE_NOREWIND = 0x01,
150 SA_MODE_OFFLINE = 0x02
154 SA_PARAM_NONE = 0x00,
155 SA_PARAM_BLOCKSIZE = 0x01,
156 SA_PARAM_DENSITY = 0x02,
157 SA_PARAM_COMPRESSION = 0x04,
158 SA_PARAM_BUFF_MODE = 0x08,
159 SA_PARAM_NUMBLOCKS = 0x10,
161 SA_PARAM_SPEED = 0x40,
166 SA_QUIRK_NONE = 0x00,
167 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
168 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
169 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
170 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
171 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
172 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
173 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
174 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
177 #define SA_QUIRK_BIT_STRING \
188 #define SAMODE(z) (dev2unit(z) & 0x3)
189 #define SADENSITY(z) ((dev2unit(z) >> 2) & 0x3)
190 #define SA_IS_CTRL(z) (dev2unit(z) & (1 << 4))
192 #define SA_NOT_CTLDEV 0
196 #define SA_ATYPE_NR 1
197 #define SA_ATYPE_ER 2
199 #define SAMINOR(ctl, mode, access) \
200 ((ctl << 4) | (mode << 2) | (access & 0x3))
202 #define SA_NUM_MODES 4
204 struct cdev *ctl_dev;
205 struct sa_mode_devs {
209 } mode_devs[SA_NUM_MODES];
217 struct bio_queue_head bio_queue;
219 struct devstat *device_stats;
229 u_int32_t comp_algorithm;
230 u_int32_t saved_comp_algorithm;
231 u_int32_t media_blksize;
232 u_int32_t last_media_blksize;
233 u_int32_t media_numblks;
234 u_int8_t media_density;
237 u_int8_t dsreg; /* mtio mt_dsreg, redux */
241 int last_resid_was_io;
244 * Relative to BOT Location.
253 struct scsi_sense_data _last_io_sense;
254 u_int64_t _last_io_resid;
255 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
256 struct scsi_sense_data _last_ctl_sense;
257 u_int64_t _last_ctl_resid;
258 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
259 #define last_io_sense errinfo._last_io_sense
260 #define last_io_resid errinfo._last_io_resid
261 #define last_io_cdb errinfo._last_io_cdb
262 #define last_ctl_sense errinfo._last_ctl_sense
263 #define last_ctl_resid errinfo._last_ctl_resid
264 #define last_ctl_cdb errinfo._last_ctl_cdb
267 * Misc other flags/state
271 open_rdonly : 1, /* open read-only */
272 open_pending_mount : 1, /* open pending mount */
273 ctrl_mode : 1; /* control device open */
275 struct task sysctl_task;
276 struct sysctl_ctx_list sysctl_ctx;
277 struct sysctl_oid *sysctl_tree;
280 struct sa_quirk_entry {
281 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
282 sa_quirks quirks; /* specific quirk type */
283 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
286 static struct sa_quirk_entry sa_quirk_table[] =
289 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
290 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
291 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
294 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
295 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
298 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
299 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
302 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
303 "Python*", "*"}, SA_QUIRK_NODREAD, 0
306 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
307 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
310 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
311 "VIPER 2525 25462", "-011"},
312 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
315 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
316 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
320 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
321 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
325 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
326 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
329 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
330 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
333 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
334 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
337 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
338 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
341 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
342 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
345 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
346 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
348 { /* jreynold@primenet.com */
349 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
350 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
352 { /* mike@sentex.net */
353 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
354 "STT20000*", "*"}, SA_QUIRK_1FM, 0
357 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "SEAGATE",
358 "DAT 06241-XXX", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
361 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
362 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
365 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
366 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
369 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
370 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
373 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
374 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
377 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
378 " SLR*", "*"}, SA_QUIRK_1FM, 0
381 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
382 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
385 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
386 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
390 static d_open_t saopen;
391 static d_close_t saclose;
392 static d_strategy_t sastrategy;
393 static d_ioctl_t saioctl;
394 static periph_init_t sainit;
395 static periph_ctor_t saregister;
396 static periph_oninv_t saoninvalidate;
397 static periph_dtor_t sacleanup;
398 static periph_start_t sastart;
399 static void saasync(void *callback_arg, u_int32_t code,
400 struct cam_path *path, void *arg);
401 static void sadone(struct cam_periph *periph,
402 union ccb *start_ccb);
403 static int saerror(union ccb *ccb, u_int32_t cam_flags,
404 u_int32_t sense_flags);
405 static int samarkswanted(struct cam_periph *);
406 static int sacheckeod(struct cam_periph *periph);
407 static int sagetparams(struct cam_periph *periph,
408 sa_params params_to_get,
409 u_int32_t *blocksize, u_int8_t *density,
410 u_int32_t *numblocks, int *buff_mode,
411 u_int8_t *write_protect, u_int8_t *speed,
412 int *comp_supported, int *comp_enabled,
413 u_int32_t *comp_algorithm,
414 sa_comp_t *comp_page);
415 static int sasetparams(struct cam_periph *periph,
416 sa_params params_to_set,
417 u_int32_t blocksize, u_int8_t density,
418 u_int32_t comp_algorithm,
419 u_int32_t sense_flags);
420 static void saprevent(struct cam_periph *periph, int action);
421 static int sarewind(struct cam_periph *periph);
422 static int saspace(struct cam_periph *periph, int count,
423 scsi_space_code code);
424 static int samount(struct cam_periph *, int, struct cdev *);
425 static int saretension(struct cam_periph *periph);
426 static int sareservereleaseunit(struct cam_periph *periph,
428 static int saloadunload(struct cam_periph *periph, int load);
429 static int saerase(struct cam_periph *periph, int longerase);
430 static int sawritefilemarks(struct cam_periph *periph,
431 int nmarks, int setmarks);
432 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
433 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
436 #ifndef SA_DEFAULT_IO_SPLIT
437 #define SA_DEFAULT_IO_SPLIT 0
440 static int sa_allow_io_split = SA_DEFAULT_IO_SPLIT;
443 * Tunable to allow the user to set a global allow_io_split value. Note
444 * that this WILL GO AWAY in FreeBSD 11.0. Silently splitting the I/O up
445 * is bad behavior, because it hides the true tape block size from the
448 static SYSCTL_NODE(_kern_cam, OID_AUTO, sa, CTLFLAG_RD, 0,
449 "CAM Sequential Access Tape Driver");
450 SYSCTL_INT(_kern_cam_sa, OID_AUTO, allow_io_split, CTLFLAG_RDTUN,
451 &sa_allow_io_split, 0, "Default I/O split value");
453 static struct periph_driver sadriver =
456 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
459 PERIPHDRIVER_DECLARE(sa, sadriver);
461 /* For 2.2-stable support */
467 static struct cdevsw sa_cdevsw = {
468 .d_version = D_VERSION,
472 .d_write = physwrite,
474 .d_strategy = sastrategy,
480 saopen(struct cdev *dev, int flags, int fmt, struct thread *td)
482 struct cam_periph *periph;
483 struct sa_softc *softc;
486 periph = (struct cam_periph *)dev->si_drv1;
487 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
491 cam_periph_lock(periph);
493 softc = (struct sa_softc *)periph->softc;
495 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
496 ("saopen(%s): softc=0x%x\n", devtoname(dev), softc->flags));
498 if (SA_IS_CTRL(dev)) {
499 softc->ctrl_mode = 1;
500 cam_periph_unlock(periph);
504 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
505 cam_periph_unlock(periph);
506 cam_periph_release(periph);
510 if (softc->flags & SA_FLAG_OPEN) {
512 } else if (softc->flags & SA_FLAG_INVALID) {
516 * Preserve whether this is a read_only open.
518 softc->open_rdonly = (flags & O_RDWR) == O_RDONLY;
521 * The function samount ensures media is loaded and ready.
522 * It also does a device RESERVE if the tape isn't yet mounted.
524 * If the mount fails and this was a non-blocking open,
525 * make this a 'open_pending_mount' action.
527 error = samount(periph, flags, dev);
528 if (error && (flags & O_NONBLOCK)) {
529 softc->flags |= SA_FLAG_OPEN;
530 softc->open_pending_mount = 1;
531 cam_periph_unhold(periph);
532 cam_periph_unlock(periph);
538 cam_periph_unhold(periph);
539 cam_periph_unlock(periph);
540 cam_periph_release(periph);
544 saprevent(periph, PR_PREVENT);
545 softc->flags |= SA_FLAG_OPEN;
547 cam_periph_unhold(periph);
548 cam_periph_unlock(periph);
553 saclose(struct cdev *dev, int flag, int fmt, struct thread *td)
555 struct cam_periph *periph;
556 struct sa_softc *softc;
557 int mode, error, writing, tmp;
558 int closedbits = SA_FLAG_OPEN;
561 periph = (struct cam_periph *)dev->si_drv1;
565 cam_periph_lock(periph);
567 softc = (struct sa_softc *)periph->softc;
569 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
570 ("saclose(%s): softc=0x%x\n", devtoname(dev), softc->flags));
573 softc->open_rdonly = 0;
574 if (SA_IS_CTRL(dev)) {
575 softc->ctrl_mode = 0;
576 cam_periph_unlock(periph);
577 cam_periph_release(periph);
581 if (softc->open_pending_mount) {
582 softc->flags &= ~SA_FLAG_OPEN;
583 softc->open_pending_mount = 0;
584 cam_periph_unlock(periph);
585 cam_periph_release(periph);
589 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
590 cam_periph_unlock(periph);
595 * Were we writing the tape?
597 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
600 * See whether or not we need to write filemarks. If this
601 * fails, we probably have to assume we've lost tape
604 error = sacheckeod(periph);
606 xpt_print(periph->path,
607 "failed to write terminating filemark(s)\n");
608 softc->flags |= SA_FLAG_TAPE_FROZEN;
612 * Whatever we end up doing, allow users to eject tapes from here on.
614 saprevent(periph, PR_ALLOW);
617 * Decide how to end...
619 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
620 closedbits |= SA_FLAG_TAPE_FROZEN;
621 } else switch (mode) {
622 case SA_MODE_OFFLINE:
624 * An 'offline' close is an unconditional release of
625 * frozen && mount conditions, irrespective of whether
626 * these operations succeeded. The reason for this is
627 * to allow at least some kind of programmatic way
628 * around our state getting all fouled up. If somebody
629 * issues an 'offline' command, that will be allowed
632 (void) sarewind(periph);
633 (void) saloadunload(periph, FALSE);
634 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
638 * If the rewind fails, return an error- if anyone cares,
639 * but not overwriting any previous error.
641 * We don't clear the notion of mounted here, but we do
642 * clear the notion of frozen if we successfully rewound.
644 tmp = sarewind(periph);
649 closedbits |= SA_FLAG_TAPE_FROZEN;
652 case SA_MODE_NOREWIND:
654 * If we're not rewinding/unloading the tape, find out
655 * whether we need to back up over one of two filemarks
656 * we wrote (if we wrote two filemarks) so that appends
657 * from this point on will be sane.
659 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
660 tmp = saspace(periph, -1, SS_FILEMARKS);
662 xpt_print(periph->path, "unable to backspace "
663 "over one of double filemarks at end of "
665 xpt_print(periph->path, "it is possible that "
666 "this device needs a SA_QUIRK_1FM quirk set"
668 softc->flags |= SA_FLAG_TAPE_FROZEN;
673 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode);
679 * We wish to note here that there are no more filemarks to be written.
681 softc->filemarks = 0;
682 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
685 * And we are no longer open for business.
687 softc->flags &= ~closedbits;
690 * Inform users if tape state if frozen....
692 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
693 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, "
694 "REWIND or MTEOM command to clear this state.\n");
697 /* release the device if it is no longer mounted */
698 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
699 sareservereleaseunit(periph, FALSE);
701 cam_periph_unhold(periph);
702 cam_periph_unlock(periph);
703 cam_periph_release(periph);
709 * Actually translate the requested transfer into one the physical driver
710 * can understand. The transfer is described by a buf and will include
711 * only one physical transfer.
714 sastrategy(struct bio *bp)
716 struct cam_periph *periph;
717 struct sa_softc *softc;
719 bp->bio_resid = bp->bio_bcount;
720 if (SA_IS_CTRL(bp->bio_dev)) {
721 biofinish(bp, NULL, EINVAL);
724 periph = (struct cam_periph *)bp->bio_dev->si_drv1;
725 if (periph == NULL) {
726 biofinish(bp, NULL, ENXIO);
729 cam_periph_lock(periph);
731 softc = (struct sa_softc *)periph->softc;
733 if (softc->flags & SA_FLAG_INVALID) {
734 cam_periph_unlock(periph);
735 biofinish(bp, NULL, ENXIO);
739 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
740 cam_periph_unlock(periph);
741 biofinish(bp, NULL, EPERM);
746 * This should actually never occur as the write(2)
747 * system call traps attempts to write to a read-only
750 if (bp->bio_cmd == BIO_WRITE && softc->open_rdonly) {
751 cam_periph_unlock(periph);
752 biofinish(bp, NULL, EBADF);
756 if (softc->open_pending_mount) {
757 int error = samount(periph, 0, bp->bio_dev);
759 cam_periph_unlock(periph);
760 biofinish(bp, NULL, ENXIO);
763 saprevent(periph, PR_PREVENT);
764 softc->open_pending_mount = 0;
769 * If it's a null transfer, return immediately
771 if (bp->bio_bcount == 0) {
772 cam_periph_unlock(periph);
778 if (softc->flags & SA_FLAG_FIXED) {
780 * Fixed block device. The byte count must
781 * be a multiple of our block size.
783 if (((softc->blk_mask != ~0) &&
784 ((bp->bio_bcount & softc->blk_mask) != 0)) ||
785 ((softc->blk_mask == ~0) &&
786 ((bp->bio_bcount % softc->min_blk) != 0))) {
787 xpt_print(periph->path, "Invalid request. Fixed block "
788 "device requests must be a multiple of %d bytes\n",
790 cam_periph_unlock(periph);
791 biofinish(bp, NULL, EINVAL);
794 } else if ((bp->bio_bcount > softc->max_blk) ||
795 (bp->bio_bcount < softc->min_blk) ||
796 (bp->bio_bcount & softc->blk_mask) != 0) {
798 xpt_print_path(periph->path);
799 printf("Invalid request. Variable block "
800 "device requests must be ");
801 if (softc->blk_mask != 0) {
802 printf("a multiple of %d ", (0x1 << softc->blk_gran));
804 printf("between %d and %d bytes\n", softc->min_blk,
806 cam_periph_unlock(periph);
807 biofinish(bp, NULL, EINVAL);
812 * Place it at the end of the queue.
814 bioq_insert_tail(&softc->bio_queue, bp);
815 softc->queue_count++;
817 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
818 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
819 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
820 (bp->bio_cmd == BIO_READ)? "read" : "write"));
822 if (softc->queue_count > 1) {
823 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
824 ("sastrategy: queue count now %d\n", softc->queue_count));
828 * Schedule ourselves for performing the work.
830 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
831 cam_periph_unlock(periph);
837 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
838 if (softc->open_pending_mount) { \
839 error = samount(periph, 0, dev); \
843 saprevent(periph, PR_PREVENT); \
844 softc->open_pending_mount = 0; \
848 saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
850 struct cam_periph *periph;
851 struct sa_softc *softc;
852 scsi_space_code spaceop;
853 int didlockperiph = 0;
858 error = 0; /* shut up gcc */
859 spaceop = 0; /* shut up gcc */
861 periph = (struct cam_periph *)dev->si_drv1;
865 cam_periph_lock(periph);
866 softc = (struct sa_softc *)periph->softc;
869 * Check for control mode accesses. We allow MTIOCGET and
870 * MTIOCERRSTAT (but need to be the only one open in order
871 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
872 * and MTCOMP (but need to be the only one accessing this
873 * device to run those).
876 if (SA_IS_CTRL(dev)) {
878 case MTIOCGETEOTMODEL:
883 * If the periph isn't already locked, lock it
884 * so our MTIOCERRSTAT can reset latched error stats.
886 * If the periph is already locked, skip it because
887 * we're just getting status and it'll be up to the
888 * other thread that has this device open to do
889 * an MTIOCERRSTAT that would clear latched status.
891 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
892 error = cam_periph_hold(periph, PRIBIO|PCATCH);
894 cam_periph_unlock(periph);
903 struct mtop *mt = (struct mtop *) arg;
906 * Check to make sure it's an OP we can perform
907 * with no media inserted.
923 case MTIOCSETEOTMODEL:
925 * We need to acquire the peripheral here rather
926 * than at open time because we are sharing writable
927 * access to data structures.
929 error = cam_periph_hold(periph, PRIBIO|PCATCH);
931 cam_periph_unlock(periph);
938 cam_periph_unlock(periph);
944 * Find the device that the user is talking about
949 struct mtget *g = (struct mtget *)arg;
952 * If this isn't the control mode device, actually go out
953 * and ask the drive again what it's set to.
955 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) {
956 u_int8_t write_protect;
957 int comp_enabled, comp_supported;
958 error = sagetparams(periph, SA_PARAM_ALL,
959 &softc->media_blksize, &softc->media_density,
960 &softc->media_numblks, &softc->buffer_mode,
961 &write_protect, &softc->speed, &comp_supported,
962 &comp_enabled, &softc->comp_algorithm, NULL);
966 softc->flags |= SA_FLAG_TAPE_WP;
968 softc->flags &= ~SA_FLAG_TAPE_WP;
969 softc->flags &= ~(SA_FLAG_COMP_SUPP|
970 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
971 if (comp_supported) {
972 if (softc->saved_comp_algorithm == 0)
973 softc->saved_comp_algorithm =
974 softc->comp_algorithm;
975 softc->flags |= SA_FLAG_COMP_SUPP;
977 softc->flags |= SA_FLAG_COMP_ENABLED;
979 softc->flags |= SA_FLAG_COMP_UNSUPP;
981 bzero(g, sizeof(struct mtget));
982 g->mt_type = MT_ISAR;
983 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
984 g->mt_comp = MT_COMP_UNSUPP;
985 g->mt_comp0 = MT_COMP_UNSUPP;
986 g->mt_comp1 = MT_COMP_UNSUPP;
987 g->mt_comp2 = MT_COMP_UNSUPP;
988 g->mt_comp3 = MT_COMP_UNSUPP;
990 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
991 g->mt_comp = MT_COMP_DISABLED;
993 g->mt_comp = softc->comp_algorithm;
995 g->mt_comp0 = softc->comp_algorithm;
996 g->mt_comp1 = softc->comp_algorithm;
997 g->mt_comp2 = softc->comp_algorithm;
998 g->mt_comp3 = softc->comp_algorithm;
1000 g->mt_density = softc->media_density;
1001 g->mt_density0 = softc->media_density;
1002 g->mt_density1 = softc->media_density;
1003 g->mt_density2 = softc->media_density;
1004 g->mt_density3 = softc->media_density;
1005 g->mt_blksiz = softc->media_blksize;
1006 g->mt_blksiz0 = softc->media_blksize;
1007 g->mt_blksiz1 = softc->media_blksize;
1008 g->mt_blksiz2 = softc->media_blksize;
1009 g->mt_blksiz3 = softc->media_blksize;
1010 g->mt_fileno = softc->fileno;
1011 g->mt_blkno = softc->blkno;
1012 g->mt_dsreg = (short) softc->dsreg;
1014 * Yes, we know that this is likely to overflow
1016 if (softc->last_resid_was_io) {
1017 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
1018 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1019 softc->last_io_resid = 0;
1023 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
1024 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1025 softc->last_ctl_resid = 0;
1034 struct scsi_tape_errors *sep =
1035 &((union mterrstat *)arg)->scsi_errstat;
1037 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1038 ("saioctl: MTIOCERRSTAT\n"));
1040 bzero(sep, sizeof(*sep));
1041 sep->io_resid = softc->last_io_resid;
1042 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1043 sizeof (sep->io_sense));
1044 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1045 sizeof (sep->io_cdb));
1046 sep->ctl_resid = softc->last_ctl_resid;
1047 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1048 sizeof (sep->ctl_sense));
1049 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1050 sizeof (sep->ctl_cdb));
1052 if ((SA_IS_CTRL(dev) == 0 && softc->open_pending_mount) ||
1054 bzero((caddr_t) &softc->errinfo,
1055 sizeof (softc->errinfo));
1064 PENDING_MOUNT_CHECK(softc, periph, dev);
1066 mt = (struct mtop *)arg;
1069 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1070 ("saioctl: op=0x%x count=0x%x\n",
1071 mt->mt_op, mt->mt_count));
1073 count = mt->mt_count;
1074 switch (mt->mt_op) {
1075 case MTWEOF: /* write an end-of-file marker */
1077 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1078 * flag because by keeping track of filemarks
1079 * we have last written we know ehether or not
1080 * we need to write more when we close the device.
1082 error = sawritefilemarks(periph, count, FALSE);
1084 case MTWSS: /* write a setmark */
1085 error = sawritefilemarks(periph, count, TRUE);
1087 case MTBSR: /* backward space record */
1088 case MTFSR: /* forward space record */
1089 case MTBSF: /* backward space file */
1090 case MTFSF: /* forward space file */
1091 case MTBSS: /* backward space setmark */
1092 case MTFSS: /* forward space setmark */
1093 case MTEOD: /* space to end of recorded medium */
1097 spaceop = SS_FILEMARKS;
1098 nmarks = softc->filemarks;
1099 error = sacheckeod(periph);
1101 xpt_print(periph->path,
1102 "EOD check prior to spacing failed\n");
1103 softc->flags |= SA_FLAG_EIO_PENDING;
1106 nmarks -= softc->filemarks;
1112 spaceop = SS_BLOCKS;
1123 spaceop = SS_SETMARKS;
1137 nmarks = softc->filemarks;
1139 * XXX: Why are we checking again?
1141 error = sacheckeod(periph);
1144 nmarks -= softc->filemarks;
1145 error = saspace(periph, count - nmarks, spaceop);
1147 * At this point, clear that we've written the tape
1148 * and that we've written any filemarks. We really
1149 * don't know what the applications wishes to do next-
1150 * the sacheckeod's will make sure we terminated the
1151 * tape correctly if we'd been writing, but the next
1152 * action the user application takes will set again
1153 * whether we need to write filemarks.
1156 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1157 softc->filemarks = 0;
1160 case MTREW: /* rewind */
1161 PENDING_MOUNT_CHECK(softc, periph, dev);
1162 (void) sacheckeod(periph);
1163 error = sarewind(periph);
1166 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1167 softc->flags &= ~SA_FLAG_ERR_PENDING;
1168 softc->filemarks = 0;
1170 case MTERASE: /* erase */
1171 PENDING_MOUNT_CHECK(softc, periph, dev);
1172 error = saerase(periph, count);
1174 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1175 softc->flags &= ~SA_FLAG_ERR_PENDING;
1177 case MTRETENS: /* re-tension tape */
1178 PENDING_MOUNT_CHECK(softc, periph, dev);
1179 error = saretension(periph);
1181 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1182 softc->flags &= ~SA_FLAG_ERR_PENDING;
1184 case MTOFFL: /* rewind and put the drive offline */
1186 PENDING_MOUNT_CHECK(softc, periph, dev);
1188 (void) sacheckeod(periph);
1190 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1191 softc->filemarks = 0;
1193 error = sarewind(periph);
1194 /* clear the frozen flag anyway */
1195 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1198 * Be sure to allow media removal before ejecting.
1201 saprevent(periph, PR_ALLOW);
1203 error = saloadunload(periph, FALSE);
1205 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1210 case MTNOP: /* no operation, sets status only */
1211 case MTCACHE: /* enable controller cache */
1212 case MTNOCACHE: /* disable controller cache */
1216 case MTSETBSIZ: /* Set block size for device */
1218 PENDING_MOUNT_CHECK(softc, periph, dev);
1220 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1223 softc->last_media_blksize =
1224 softc->media_blksize;
1225 softc->media_blksize = count;
1227 softc->flags |= SA_FLAG_FIXED;
1228 if (powerof2(count)) {
1231 softc->blk_mask = count - 1;
1233 softc->blk_mask = ~0;
1234 softc->blk_shift = 0;
1237 * Make the user's desire 'persistent'.
1239 softc->quirks &= ~SA_QUIRK_VARIABLE;
1240 softc->quirks |= SA_QUIRK_FIXED;
1242 softc->flags &= ~SA_FLAG_FIXED;
1243 if (softc->max_blk == 0) {
1244 softc->max_blk = ~0;
1246 softc->blk_shift = 0;
1247 if (softc->blk_gran != 0) {
1249 softc->blk_gran - 1;
1251 softc->blk_mask = 0;
1254 * Make the user's desire 'persistent'.
1256 softc->quirks |= SA_QUIRK_VARIABLE;
1257 softc->quirks &= ~SA_QUIRK_FIXED;
1261 case MTSETDNSTY: /* Set density for device and mode */
1262 PENDING_MOUNT_CHECK(softc, periph, dev);
1264 if (count > UCHAR_MAX) {
1268 error = sasetparams(periph, SA_PARAM_DENSITY,
1272 case MTCOMP: /* enable compression */
1273 PENDING_MOUNT_CHECK(softc, periph, dev);
1275 * Some devices don't support compression, and
1276 * don't like it if you ask them for the
1279 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1280 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1284 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1285 0, 0, count, SF_NO_PRINT);
1297 PENDING_MOUNT_CHECK(softc, periph, dev);
1298 error = sardpos(periph, 0, (u_int32_t *) arg);
1301 PENDING_MOUNT_CHECK(softc, periph, dev);
1302 error = sardpos(periph, 1, (u_int32_t *) arg);
1305 PENDING_MOUNT_CHECK(softc, periph, dev);
1306 error = sasetpos(periph, 0, (u_int32_t *) arg);
1309 PENDING_MOUNT_CHECK(softc, periph, dev);
1310 error = sasetpos(periph, 1, (u_int32_t *) arg);
1312 case MTIOCGETEOTMODEL:
1314 if (softc->quirks & SA_QUIRK_1FM)
1318 *((u_int32_t *) arg) = mode;
1320 case MTIOCSETEOTMODEL:
1322 switch (*((u_int32_t *) arg)) {
1324 softc->quirks &= ~SA_QUIRK_2FM;
1325 softc->quirks |= SA_QUIRK_1FM;
1328 softc->quirks &= ~SA_QUIRK_1FM;
1329 softc->quirks |= SA_QUIRK_2FM;
1337 error = cam_periph_ioctl(periph, cmd, arg, saerror);
1342 * Check to see if we cleared a frozen state
1344 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1350 softc->fileno = (daddr_t) -1;
1351 softc->blkno = (daddr_t) -1;
1352 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1353 xpt_print(periph->path,
1354 "tape state now unfrozen.\n");
1360 if (didlockperiph) {
1361 cam_periph_unhold(periph);
1363 cam_periph_unlock(periph);
1373 * Install a global async callback.
1375 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL);
1377 if (status != CAM_REQ_CMP) {
1378 printf("sa: Failed to attach master async callback "
1379 "due to status 0x%x!\n", status);
1384 saoninvalidate(struct cam_periph *periph)
1386 struct sa_softc *softc;
1388 softc = (struct sa_softc *)periph->softc;
1391 * De-register any async callbacks.
1393 xpt_register_async(0, saasync, periph, periph->path);
1395 softc->flags |= SA_FLAG_INVALID;
1398 * Return all queued I/O with ENXIO.
1399 * XXX Handle any transactions queued to the card
1400 * with XPT_ABORT_CCB.
1402 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1403 softc->queue_count = 0;
1407 sacleanup(struct cam_periph *periph)
1409 struct sa_softc *softc;
1412 softc = (struct sa_softc *)periph->softc;
1414 devstat_remove_entry(softc->device_stats);
1415 cam_periph_unlock(periph);
1416 destroy_dev(softc->devs.ctl_dev);
1417 for (i = 0; i < SA_NUM_MODES; i++) {
1418 destroy_dev(softc->devs.mode_devs[i].r_dev);
1419 destroy_dev(softc->devs.mode_devs[i].nr_dev);
1420 destroy_dev(softc->devs.mode_devs[i].er_dev);
1422 cam_periph_lock(periph);
1423 free(softc, M_SCSISA);
1427 saasync(void *callback_arg, u_int32_t code,
1428 struct cam_path *path, void *arg)
1430 struct cam_periph *periph;
1432 periph = (struct cam_periph *)callback_arg;
1434 case AC_FOUND_DEVICE:
1436 struct ccb_getdev *cgd;
1439 cgd = (struct ccb_getdev *)arg;
1443 if (cgd->protocol != PROTO_SCSI)
1446 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1450 * Allocate a peripheral instance for
1451 * this device and start the probe
1454 status = cam_periph_alloc(saregister, saoninvalidate,
1456 "sa", CAM_PERIPH_BIO, path,
1457 saasync, AC_FOUND_DEVICE, cgd);
1459 if (status != CAM_REQ_CMP
1460 && status != CAM_REQ_INPROG)
1461 printf("saasync: Unable to probe new device "
1462 "due to status 0x%x\n", status);
1466 cam_periph_async(periph, code, path, arg);
1472 sasysctlinit(void *context, int pending)
1474 struct cam_periph *periph;
1475 struct sa_softc *softc;
1476 char tmpstr[80], tmpstr2[80];
1478 periph = (struct cam_periph *)context;
1480 * If the periph is invalid, no need to setup the sysctls.
1482 if (periph->flags & CAM_PERIPH_INVALID)
1485 softc = (struct sa_softc *)periph->softc;
1487 snprintf(tmpstr, sizeof(tmpstr), "CAM SA unit %d", periph->unit_number);
1488 snprintf(tmpstr2, sizeof(tmpstr2), "%u", periph->unit_number);
1490 sysctl_ctx_init(&softc->sysctl_ctx);
1491 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1492 SYSCTL_STATIC_CHILDREN(_kern_cam_sa), OID_AUTO, tmpstr2,
1493 CTLFLAG_RD, 0, tmpstr);
1494 if (softc->sysctl_tree == NULL)
1497 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1498 OID_AUTO, "allow_io_split", CTLTYPE_INT | CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
1499 &softc->allow_io_split, 0, "Allow Splitting I/O");
1500 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1501 OID_AUTO, "maxio", CTLTYPE_INT | CTLFLAG_RD,
1502 &softc->maxio, 0, "Maximum I/O size");
1503 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1504 OID_AUTO, "cpi_maxio", CTLTYPE_INT | CTLFLAG_RD,
1505 &softc->cpi_maxio, 0, "Maximum Controller I/O size");
1509 * Release the reference that was held when this task was enqueued.
1511 cam_periph_release(periph);
1515 saregister(struct cam_periph *periph, void *arg)
1517 struct sa_softc *softc;
1518 struct ccb_getdev *cgd;
1519 struct ccb_pathinq cpi;
1524 cgd = (struct ccb_getdev *)arg;
1526 printf("saregister: no getdev CCB, can't register device\n");
1527 return (CAM_REQ_CMP_ERR);
1530 softc = (struct sa_softc *)
1531 malloc(sizeof (*softc), M_SCSISA, M_NOWAIT | M_ZERO);
1532 if (softc == NULL) {
1533 printf("saregister: Unable to probe new device. "
1534 "Unable to allocate softc\n");
1535 return (CAM_REQ_CMP_ERR);
1537 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1538 softc->state = SA_STATE_NORMAL;
1539 softc->fileno = (daddr_t) -1;
1540 softc->blkno = (daddr_t) -1;
1542 bioq_init(&softc->bio_queue);
1543 periph->softc = softc;
1546 * See if this device has any quirks.
1548 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1549 (caddr_t)sa_quirk_table,
1550 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1551 sizeof(*sa_quirk_table), scsi_inquiry_match);
1553 if (match != NULL) {
1554 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1555 softc->last_media_blksize =
1556 ((struct sa_quirk_entry *)match)->prefblk;
1558 softc->quirks = SA_QUIRK_NONE;
1560 bzero(&cpi, sizeof(cpi));
1561 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1562 cpi.ccb_h.func_code = XPT_PATH_INQ;
1563 xpt_action((union ccb *)&cpi);
1566 * The SA driver supports a blocksize, but we don't know the
1567 * blocksize until we media is inserted. So, set a flag to
1568 * indicate that the blocksize is unavailable right now.
1570 cam_periph_unlock(periph);
1571 softc->device_stats = devstat_new_entry("sa", periph->unit_number, 0,
1572 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1573 XPORT_DEVSTAT_TYPE(cpi.transport), DEVSTAT_PRIORITY_TAPE);
1576 * Load the default value that is either compiled in, or loaded
1577 * in the global kern.cam.sa.allow_io_split tunable.
1579 softc->allow_io_split = sa_allow_io_split;
1582 * Load a per-instance tunable, if it exists. NOTE that this
1583 * tunable WILL GO AWAY in FreeBSD 11.0.
1585 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.sa.%u.allow_io_split",
1586 periph->unit_number);
1587 TUNABLE_INT_FETCH(tmpstr, &softc->allow_io_split);
1590 * If maxio isn't set, we fall back to DFLTPHYS. Otherwise we take
1591 * the smaller of cpi.maxio or MAXPHYS.
1594 softc->maxio = DFLTPHYS;
1595 else if (cpi.maxio > MAXPHYS)
1596 softc->maxio = MAXPHYS;
1598 softc->maxio = cpi.maxio;
1601 * Record the controller's maximum I/O size so we can report it to
1604 softc->cpi_maxio = cpi.maxio;
1607 * By default we tell physio that we do not want our I/O split.
1608 * The user needs to have a 1:1 mapping between the size of his
1609 * write to a tape character device and the size of the write
1610 * that actually goes down to the drive.
1612 if (softc->allow_io_split == 0)
1613 softc->si_flags = SI_NOSPLIT;
1615 softc->si_flags = 0;
1617 TASK_INIT(&softc->sysctl_task, 0, sasysctlinit, periph);
1620 * If the SIM supports unmapped I/O, let physio know that we can
1621 * handle unmapped buffers.
1623 if (cpi.hba_misc & PIM_UNMAPPED)
1624 softc->si_flags |= SI_UNMAPPED;
1626 softc->devs.ctl_dev = make_dev(&sa_cdevsw, SAMINOR(SA_CTLDEV,
1627 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1628 0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1629 softc->devs.ctl_dev->si_drv1 = periph;
1630 softc->devs.ctl_dev->si_iosize_max = softc->maxio;
1631 softc->devs.ctl_dev->si_flags |= softc->si_flags;
1633 for (i = 0; i < SA_NUM_MODES; i++) {
1635 softc->devs.mode_devs[i].r_dev = make_dev(&sa_cdevsw,
1636 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_R),
1637 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1638 periph->periph_name, periph->unit_number, i);
1639 softc->devs.mode_devs[i].r_dev->si_drv1 = periph;
1640 softc->devs.mode_devs[i].r_dev->si_iosize_max = softc->maxio;
1641 softc->devs.mode_devs[i].r_dev->si_flags |= softc->si_flags;
1643 softc->devs.mode_devs[i].nr_dev = make_dev(&sa_cdevsw,
1644 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_NR),
1645 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1646 periph->periph_name, periph->unit_number, i);
1647 softc->devs.mode_devs[i].nr_dev->si_drv1 = periph;
1648 softc->devs.mode_devs[i].nr_dev->si_iosize_max = softc->maxio;
1649 softc->devs.mode_devs[i].nr_dev->si_flags |= softc->si_flags;
1651 softc->devs.mode_devs[i].er_dev = make_dev(&sa_cdevsw,
1652 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_ER),
1653 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1654 periph->periph_name, periph->unit_number, i);
1655 softc->devs.mode_devs[i].er_dev->si_drv1 = periph;
1656 softc->devs.mode_devs[i].er_dev->si_iosize_max = softc->maxio;
1657 softc->devs.mode_devs[i].er_dev->si_flags |= softc->si_flags;
1660 * Make the (well known) aliases for the first mode.
1665 alias = make_dev_alias(softc->devs.mode_devs[i].r_dev,
1666 "%s%d", periph->periph_name, periph->unit_number);
1667 alias->si_drv1 = periph;
1668 alias->si_iosize_max = softc->maxio;
1669 alias->si_flags |= softc->si_flags;
1671 alias = make_dev_alias(softc->devs.mode_devs[i].nr_dev,
1672 "n%s%d", periph->periph_name, periph->unit_number);
1673 alias->si_drv1 = periph;
1674 alias->si_iosize_max = softc->maxio;
1675 alias->si_flags |= softc->si_flags;
1677 alias = make_dev_alias(softc->devs.mode_devs[i].er_dev,
1678 "e%s%d", periph->periph_name, periph->unit_number);
1679 alias->si_drv1 = periph;
1680 alias->si_iosize_max = softc->maxio;
1681 alias->si_flags |= softc->si_flags;
1684 cam_periph_lock(periph);
1687 * Bump the peripheral refcount for the sysctl thread, in case we
1688 * get invalidated before the thread has a chance to run.
1690 cam_periph_acquire(periph);
1691 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
1694 * Add an async callback so that we get
1695 * notified if this device goes away.
1697 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path);
1699 xpt_announce_periph(periph, NULL);
1700 xpt_announce_quirks(periph, softc->quirks, SA_QUIRK_BIT_STRING);
1702 return (CAM_REQ_CMP);
1706 sastart(struct cam_periph *periph, union ccb *start_ccb)
1708 struct sa_softc *softc;
1710 softc = (struct sa_softc *)periph->softc;
1712 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
1715 switch (softc->state) {
1716 case SA_STATE_NORMAL:
1718 /* Pull a buffer from the queue and get going on it */
1722 * See if there is a buf with work for us to do..
1724 bp = bioq_first(&softc->bio_queue);
1726 xpt_release_ccb(start_ccb);
1727 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1728 struct bio *done_bp;
1730 softc->queue_count--;
1731 bioq_remove(&softc->bio_queue, bp);
1732 bp->bio_resid = bp->bio_bcount;
1734 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1736 * We now just clear errors in this case
1737 * and let the residual be the notifier.
1740 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1742 * This can only happen if we're reading
1743 * in fixed length mode. In this case,
1744 * we dump the rest of the list the
1748 if (bioq_first(&softc->bio_queue) != NULL) {
1752 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1753 bp->bio_error = EIO;
1754 bp->bio_flags |= BIO_ERROR;
1756 bp = bioq_first(&softc->bio_queue);
1758 * Only if we have no other buffers queued up
1759 * do we clear the pending error flag.
1762 softc->flags &= ~SA_FLAG_ERR_PENDING;
1763 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1764 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
1765 "%d more buffers queued up\n",
1766 (softc->flags & SA_FLAG_ERR_PENDING),
1767 (bp != NULL)? "not " : " ", softc->queue_count));
1768 xpt_release_ccb(start_ccb);
1773 bioq_remove(&softc->bio_queue, bp);
1774 softc->queue_count--;
1776 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1777 if (softc->blk_shift != 0) {
1779 bp->bio_bcount >> softc->blk_shift;
1780 } else if (softc->media_blksize != 0) {
1781 length = bp->bio_bcount /
1782 softc->media_blksize;
1784 bp->bio_error = EIO;
1785 xpt_print(periph->path, "zero blocksize"
1786 " for FIXED length writes?\n");
1791 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1792 ("issuing a %d fixed record %s\n",
1793 length, (bp->bio_cmd == BIO_READ)? "read" :
1797 length = bp->bio_bcount;
1799 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1800 ("issuing a %d variable byte %s\n",
1801 length, (bp->bio_cmd == BIO_READ)? "read" :
1805 devstat_start_transaction_bio(softc->device_stats, bp);
1807 * Some people have theorized that we should
1808 * suppress illegal length indication if we are
1809 * running in variable block mode so that we don't
1810 * have to request sense every time our requested
1811 * block size is larger than the written block.
1812 * The residual information from the ccb allows
1813 * us to identify this situation anyway. The only
1814 * problem with this is that we will not get
1815 * information about blocks that are larger than
1816 * our read buffer unless we set the block size
1817 * in the mode page to something other than 0.
1819 * I believe that this is a non-issue. If user apps
1820 * don't adjust their read size to match our record
1821 * size, that's just life. Anyway, the typical usage
1822 * would be to issue, e.g., 64KB reads and occasionally
1823 * have to do deal with 512 byte or 1KB intermediate
1826 softc->dsreg = (bp->bio_cmd == BIO_READ)?
1827 MTIO_DSREG_RD : MTIO_DSREG_WR;
1828 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1829 MSG_SIMPLE_Q_TAG, (bp->bio_cmd == BIO_READ ?
1830 SCSI_RW_READ : SCSI_RW_WRITE) |
1831 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
1832 SCSI_RW_BIO : 0), FALSE,
1833 (softc->flags & SA_FLAG_FIXED) != 0, length,
1834 (bp->bio_flags & BIO_UNMAPPED) != 0 ? (void *)bp :
1835 bp->bio_data, bp->bio_bcount, SSD_FULL_SIZE,
1837 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1838 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1839 start_ccb->ccb_h.ccb_bp = bp;
1840 bp = bioq_first(&softc->bio_queue);
1841 xpt_action(start_ccb);
1845 /* Have more work to do, so ensure we stay scheduled */
1846 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1850 case SA_STATE_ABNORMAL:
1852 panic("state 0x%x in sastart", softc->state);
1859 sadone(struct cam_periph *periph, union ccb *done_ccb)
1861 struct sa_softc *softc;
1862 struct ccb_scsiio *csio;
1864 softc = (struct sa_softc *)periph->softc;
1865 csio = &done_ccb->csio;
1866 switch (CCB_Type(csio)) {
1867 case SA_CCB_BUFFER_IO:
1872 softc->dsreg = MTIO_DSREG_REST;
1873 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1875 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1876 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1878 * A retry was scheduled, so just return.
1887 * Catastrophic error. Mark the tape as frozen
1888 * (we no longer know tape position).
1890 * Return all queued I/O with EIO, and unfreeze
1891 * our queue so that future transactions that
1892 * attempt to fix this problem can get to the
1897 softc->flags |= SA_FLAG_TAPE_FROZEN;
1898 bioq_flush(&softc->bio_queue, NULL, EIO);
1901 bp->bio_resid = bp->bio_bcount;
1902 bp->bio_error = error;
1903 bp->bio_flags |= BIO_ERROR;
1905 * In the error case, position is updated in saerror.
1908 bp->bio_resid = csio->resid;
1910 if (csio->resid != 0) {
1911 bp->bio_flags |= BIO_ERROR;
1913 if (bp->bio_cmd == BIO_WRITE) {
1914 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1915 softc->filemarks = 0;
1917 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1918 (softc->blkno != (daddr_t) -1)) {
1919 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1921 if (softc->blk_shift != 0) {
1922 l = bp->bio_bcount >>
1925 l = bp->bio_bcount /
1926 softc->media_blksize;
1928 softc->blkno += (daddr_t) l;
1935 * If we had an error (immediate or pending),
1936 * release the device queue now.
1938 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1939 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1940 if (error || bp->bio_resid) {
1941 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1942 ("error %d resid %ld count %ld\n", error,
1943 bp->bio_resid, bp->bio_bcount));
1945 biofinish(bp, softc->device_stats, 0);
1949 xpt_release_ccb(done_ccb);
1953 * Mount the tape (make sure it's ready for I/O).
1956 samount(struct cam_periph *periph, int oflags, struct cdev *dev)
1958 struct sa_softc *softc;
1963 * oflags can be checked for 'kind' of open (read-only check) - later
1964 * dev can be checked for a control-mode or compression open - later
1966 UNUSED_PARAMETER(oflags);
1967 UNUSED_PARAMETER(dev);
1970 softc = (struct sa_softc *)periph->softc;
1973 * This should determine if something has happend since the last
1974 * open/mount that would invalidate the mount. We do *not* want
1975 * to retry this command- we just want the status. But we only
1976 * do this if we're mounted already- if we're not mounted,
1977 * we don't care about the unit read state and can instead use
1978 * this opportunity to attempt to reserve the tape unit.
1981 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1982 ccb = cam_periph_getccb(periph, 1);
1983 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1984 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1985 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1986 softc->device_stats);
1987 if (error == ENXIO) {
1988 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1989 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1990 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1991 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1992 softc->device_stats);
1995 * We don't need to freeze the tape because we
1996 * will now attempt to rewind/load it.
1998 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1999 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2000 xpt_print(periph->path,
2001 "error %d on TUR in samount\n", error);
2005 error = sareservereleaseunit(periph, TRUE);
2009 ccb = cam_periph_getccb(periph, 1);
2010 scsi_test_unit_ready(&ccb->csio, 0, sadone,
2011 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
2012 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2013 softc->device_stats);
2016 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
2017 struct scsi_read_block_limits_data *rblim = NULL;
2018 int comp_enabled, comp_supported;
2019 u_int8_t write_protect, guessing = 0;
2022 * Clear out old state.
2024 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
2025 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
2026 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
2027 softc->filemarks = 0;
2030 * *Very* first off, make sure we're loaded to BOT.
2032 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2033 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
2034 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2035 softc->device_stats);
2038 * In case this doesn't work, do a REWIND instead
2041 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
2042 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
2043 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2044 softc->device_stats);
2047 xpt_release_ccb(ccb);
2052 * Do a dummy test read to force access to the
2053 * media so that the drive will really know what's
2054 * there. We actually don't really care what the
2055 * blocksize on tape is and don't expect to really
2056 * read a full record.
2058 rblim = (struct scsi_read_block_limits_data *)
2059 malloc(8192, M_SCSISA, M_NOWAIT);
2060 if (rblim == NULL) {
2061 xpt_print(periph->path, "no memory for test read\n");
2062 xpt_release_ccb(ccb);
2067 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
2068 scsi_sa_read_write(&ccb->csio, 0, sadone,
2069 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
2070 (void *) rblim, 8192, SSD_FULL_SIZE,
2072 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2073 softc->device_stats);
2074 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
2075 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
2076 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
2077 SF_NO_PRINT | SF_RETRY_UA,
2078 softc->device_stats);
2080 xpt_print(periph->path,
2081 "unable to rewind after test read\n");
2082 xpt_release_ccb(ccb);
2088 * Next off, determine block limits.
2090 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2091 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2093 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
2094 SF_NO_PRINT | SF_RETRY_UA, softc->device_stats);
2096 xpt_release_ccb(ccb);
2100 * If it's less than SCSI-2, READ BLOCK LIMITS is not
2101 * a MANDATORY command. Anyway- it doesn't matter-
2102 * we can proceed anyway.
2104 softc->blk_gran = 0;
2105 softc->max_blk = ~0;
2108 if (softc->scsi_rev >= SCSI_REV_SPC) {
2109 softc->blk_gran = RBL_GRAN(rblim);
2111 softc->blk_gran = 0;
2114 * We take max_blk == min_blk to mean a default to
2115 * fixed mode- but note that whatever we get out of
2116 * sagetparams below will actually determine whether
2117 * we are actually *in* fixed mode.
2119 softc->max_blk = scsi_3btoul(rblim->maximum);
2120 softc->min_blk = scsi_2btoul(rblim->minimum);
2125 * Next, perform a mode sense to determine
2126 * current density, blocksize, compression etc.
2128 error = sagetparams(periph, SA_PARAM_ALL,
2129 &softc->media_blksize,
2130 &softc->media_density,
2131 &softc->media_numblks,
2132 &softc->buffer_mode, &write_protect,
2133 &softc->speed, &comp_supported,
2134 &comp_enabled, &softc->comp_algorithm,
2139 * We could work a little harder here. We could
2140 * adjust our attempts to get information. It
2141 * might be an ancient tape drive. If someone
2142 * nudges us, we'll do that.
2148 * If no quirk has determined that this is a device that is
2149 * preferred to be in fixed or variable mode, now is the time
2152 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2155 * This could be expensive to find out. Luckily we
2156 * only need to do this once. If we start out in
2157 * 'default' mode, try and set ourselves to one
2158 * of the densities that would determine a wad
2159 * of other stuff. Go from highest to lowest.
2161 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2163 static u_int8_t ctry[] = {
2164 SCSI_DENSITY_HALFINCH_PE,
2165 SCSI_DENSITY_HALFINCH_6250C,
2166 SCSI_DENSITY_HALFINCH_6250,
2167 SCSI_DENSITY_HALFINCH_1600,
2168 SCSI_DENSITY_HALFINCH_800,
2169 SCSI_DENSITY_QIC_4GB,
2170 SCSI_DENSITY_QIC_2GB,
2171 SCSI_DENSITY_QIC_525_320,
2172 SCSI_DENSITY_QIC_150,
2173 SCSI_DENSITY_QIC_120,
2174 SCSI_DENSITY_QIC_24,
2175 SCSI_DENSITY_QIC_11_9TRK,
2176 SCSI_DENSITY_QIC_11_4TRK,
2177 SCSI_DENSITY_QIC_1320,
2178 SCSI_DENSITY_QIC_3080,
2181 for (i = 0; ctry[i]; i++) {
2182 error = sasetparams(periph,
2183 SA_PARAM_DENSITY, 0, ctry[i],
2186 softc->media_density = ctry[i];
2191 switch (softc->media_density) {
2192 case SCSI_DENSITY_QIC_11_4TRK:
2193 case SCSI_DENSITY_QIC_11_9TRK:
2194 case SCSI_DENSITY_QIC_24:
2195 case SCSI_DENSITY_QIC_120:
2196 case SCSI_DENSITY_QIC_150:
2197 case SCSI_DENSITY_QIC_525_320:
2198 case SCSI_DENSITY_QIC_1320:
2199 case SCSI_DENSITY_QIC_3080:
2200 softc->quirks &= ~SA_QUIRK_2FM;
2201 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2202 softc->last_media_blksize = 512;
2204 case SCSI_DENSITY_QIC_4GB:
2205 case SCSI_DENSITY_QIC_2GB:
2206 softc->quirks &= ~SA_QUIRK_2FM;
2207 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2208 softc->last_media_blksize = 1024;
2211 softc->last_media_blksize =
2212 softc->media_blksize;
2213 softc->quirks |= SA_QUIRK_VARIABLE;
2219 * If no quirk has determined that this is a device that needs
2220 * to have 2 Filemarks at EOD, now is the time to find out.
2223 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2224 switch (softc->media_density) {
2225 case SCSI_DENSITY_HALFINCH_800:
2226 case SCSI_DENSITY_HALFINCH_1600:
2227 case SCSI_DENSITY_HALFINCH_6250:
2228 case SCSI_DENSITY_HALFINCH_6250C:
2229 case SCSI_DENSITY_HALFINCH_PE:
2230 softc->quirks &= ~SA_QUIRK_1FM;
2231 softc->quirks |= SA_QUIRK_2FM;
2239 * Now validate that some info we got makes sense.
2241 if ((softc->max_blk < softc->media_blksize) ||
2242 (softc->min_blk > softc->media_blksize &&
2243 softc->media_blksize)) {
2244 xpt_print(periph->path,
2245 "BLOCK LIMITS (%d..%d) could not match current "
2246 "block settings (%d)- adjusting\n", softc->min_blk,
2247 softc->max_blk, softc->media_blksize);
2248 softc->max_blk = softc->min_blk =
2249 softc->media_blksize;
2253 * Now put ourselves into the right frame of mind based
2258 * If we want to be in FIXED mode and our current blocksize
2259 * is not equal to our last blocksize (if nonzero), try and
2260 * set ourselves to this last blocksize (as the 'preferred'
2261 * block size). The initial quirkmatch at registry sets the
2262 * initial 'last' blocksize. If, for whatever reason, this
2263 * 'last' blocksize is zero, set the blocksize to 512,
2264 * or min_blk if that's larger.
2266 if ((softc->quirks & SA_QUIRK_FIXED) &&
2267 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2268 (softc->media_blksize != softc->last_media_blksize)) {
2269 softc->media_blksize = softc->last_media_blksize;
2270 if (softc->media_blksize == 0) {
2271 softc->media_blksize = 512;
2272 if (softc->media_blksize < softc->min_blk) {
2273 softc->media_blksize = softc->min_blk;
2276 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2277 softc->media_blksize, 0, 0, SF_NO_PRINT);
2279 xpt_print(periph->path,
2280 "unable to set fixed blocksize to %d\n",
2281 softc->media_blksize);
2286 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2287 (softc->media_blksize != 0)) {
2288 softc->last_media_blksize = softc->media_blksize;
2289 softc->media_blksize = 0;
2290 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2291 0, 0, 0, SF_NO_PRINT);
2294 * If this fails and we were guessing, just
2295 * assume that we got it wrong and go try
2296 * fixed block mode. Don't even check against
2297 * density code at this point.
2300 softc->quirks &= ~SA_QUIRK_VARIABLE;
2301 softc->quirks |= SA_QUIRK_FIXED;
2302 if (softc->last_media_blksize == 0)
2303 softc->last_media_blksize = 512;
2306 xpt_print(periph->path,
2307 "unable to set variable blocksize\n");
2313 * Now that we have the current block size,
2314 * set up some parameters for sastart's usage.
2316 if (softc->media_blksize) {
2317 softc->flags |= SA_FLAG_FIXED;
2318 if (powerof2(softc->media_blksize)) {
2320 ffs(softc->media_blksize) - 1;
2321 softc->blk_mask = softc->media_blksize - 1;
2323 softc->blk_mask = ~0;
2324 softc->blk_shift = 0;
2328 * The SCSI-3 spec allows 0 to mean "unspecified".
2329 * The SCSI-1 spec allows 0 to mean 'infinite'.
2331 * Either works here.
2333 if (softc->max_blk == 0) {
2334 softc->max_blk = ~0;
2336 softc->blk_shift = 0;
2337 if (softc->blk_gran != 0) {
2338 softc->blk_mask = softc->blk_gran - 1;
2340 softc->blk_mask = 0;
2345 softc->flags |= SA_FLAG_TAPE_WP;
2347 if (comp_supported) {
2348 if (softc->saved_comp_algorithm == 0)
2349 softc->saved_comp_algorithm =
2350 softc->comp_algorithm;
2351 softc->flags |= SA_FLAG_COMP_SUPP;
2353 softc->flags |= SA_FLAG_COMP_ENABLED;
2355 softc->flags |= SA_FLAG_COMP_UNSUPP;
2357 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2358 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2359 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2362 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2364 xpt_print(periph->path,
2365 "unable to set buffered mode\n");
2367 error = 0; /* not an error */
2372 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2376 free(rblim, M_SCSISA);
2379 softc->dsreg = MTIO_DSREG_NIL;
2381 softc->fileno = softc->blkno = 0;
2382 softc->dsreg = MTIO_DSREG_REST;
2384 #ifdef SA_1FM_AT_EOD
2385 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2386 softc->quirks |= SA_QUIRK_1FM;
2388 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2389 softc->quirks |= SA_QUIRK_2FM;
2392 xpt_release_ccb(ccb);
2395 * If we return an error, we're not mounted any more,
2396 * so release any device reservation.
2399 (void) sareservereleaseunit(periph, FALSE);
2402 * Clear I/O residual.
2404 softc->last_io_resid = 0;
2405 softc->last_ctl_resid = 0;
2411 * How many filemarks do we need to write if we were to terminate the
2412 * tape session right now? Note that this can be a negative number
2416 samarkswanted(struct cam_periph *periph)
2419 struct sa_softc *softc;
2421 softc = (struct sa_softc *)periph->softc;
2423 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2425 if (softc->quirks & SA_QUIRK_2FM)
2428 markswanted -= softc->filemarks;
2429 return (markswanted);
2433 sacheckeod(struct cam_periph *periph)
2438 markswanted = samarkswanted(periph);
2440 if (markswanted > 0) {
2441 error = sawritefilemarks(periph, markswanted, FALSE);
2449 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2451 static const char *toobig =
2452 "%d-byte tape record bigger than supplied buffer\n";
2453 struct cam_periph *periph;
2454 struct sa_softc *softc;
2455 struct ccb_scsiio *csio;
2456 struct scsi_sense_data *sense;
2460 int error_code, sense_key, asc, ascq, error, aqvalid, stream_valid;
2462 uint8_t stream_bits;
2464 periph = xpt_path_periph(ccb->ccb_h.path);
2465 softc = (struct sa_softc *)periph->softc;
2467 sense = &csio->sense_data;
2468 sense_len = csio->sense_len - csio->sense_resid;
2469 scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
2470 &asc, &ascq, /*show_errors*/ 1);
2471 if (asc != -1 && ascq != -1)
2475 if (scsi_get_stream_info(sense, sense_len, NULL, &stream_bits) == 0)
2481 status = csio->ccb_h.status & CAM_STATUS_MASK;
2484 * Calculate/latch up, any residuals... We do this in a funny 2-step
2485 * so we can print stuff here if we have CAM_DEBUG enabled for this
2488 if (status == CAM_SCSI_STATUS_ERROR) {
2489 if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &resid,
2491 if ((softc->flags & SA_FLAG_FIXED) != 0)
2492 resid *= softc->media_blksize;
2494 resid = csio->dxfer_len;
2496 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2497 if (softc->media_blksize)
2498 info /= softc->media_blksize;
2501 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2502 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2503 sizeof (struct scsi_sense_data));
2504 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2505 (int) csio->cdb_len);
2506 softc->last_io_resid = resid;
2507 softc->last_resid_was_io = 1;
2509 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2510 sizeof (struct scsi_sense_data));
2511 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2512 (int) csio->cdb_len);
2513 softc->last_ctl_resid = resid;
2514 softc->last_resid_was_io = 0;
2516 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2517 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %jd "
2518 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2519 sense_key, asc, ascq, status,
2520 (stream_valid) ? stream_bits : 0, (intmax_t)resid,
2523 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2524 ("Cam Status 0x%x\n", status));
2530 case CAM_SCSI_STATUS_ERROR:
2532 * If a read/write command, we handle it here.
2534 if (csio->cdb_io.cdb_bytes[0] == SA_READ ||
2535 csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2539 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2540 * on a non read/write command, we assume it's not an error
2541 * and propagate the residule and return.
2543 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2544 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2545 csio->resid = resid;
2550 * Otherwise, we let the common code handle this.
2552 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2556 * We cannot depend upon CAM honoring retry counts for these.
2558 case CAM_SCSI_BUS_RESET:
2560 if (ccb->ccb_h.retry_count <= 0) {
2565 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2569 * Handle filemark, end of tape, mismatched record sizes....
2570 * From this point out, we're only handling read/write cases.
2571 * Handle writes && reads differently.
2574 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2575 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2576 csio->resid = resid;
2578 } else if ((stream_valid != 0) && (stream_bits & SSD_EOM)) {
2579 softc->flags |= SA_FLAG_EOM_PENDING;
2581 * Grotesque as it seems, the few times
2582 * I've actually seen a non-zero resid,
2583 * the tape drive actually lied and had
2584 * written all the data!.
2589 csio->resid = resid;
2590 if (sense_key == SSD_KEY_BLANK_CHECK) {
2591 if (softc->quirks & SA_QUIRK_1FM) {
2593 softc->flags |= SA_FLAG_EOM_PENDING;
2597 } else if ((stream_valid != 0) && (stream_bits & SSD_FILEMARK)){
2598 if (softc->flags & SA_FLAG_FIXED) {
2600 softc->flags |= SA_FLAG_EOF_PENDING;
2603 * Unconditionally, if we detected a filemark on a read,
2604 * mark that we've run moved a file ahead.
2606 if (softc->fileno != (daddr_t) -1) {
2609 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2615 * Incorrect Length usually applies to read, but can apply to writes.
2617 if (error == 0 && (stream_valid != 0) && (stream_bits & SSD_ILI)) {
2619 xpt_print(csio->ccb_h.path, toobig,
2620 csio->dxfer_len - info);
2621 csio->resid = csio->dxfer_len;
2624 csio->resid = resid;
2625 if (softc->flags & SA_FLAG_FIXED) {
2626 softc->flags |= SA_FLAG_EIO_PENDING;
2629 * Bump the block number if we hadn't seen a filemark.
2630 * Do this independent of errors (we've moved anyway).
2632 if ((stream_valid == 0) ||
2633 (stream_bits & SSD_FILEMARK) == 0) {
2634 if (softc->blkno != (daddr_t) -1) {
2636 csio->ccb_h.ccb_pflags |=
2637 SA_POSITION_UPDATED;
2645 * Unfreeze the queue if frozen as we're not returning anything
2646 * to our waiters that would indicate an I/O error has occurred
2656 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2657 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2658 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2659 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2664 struct scsi_mode_header_6 *mode_hdr;
2665 struct scsi_mode_blk_desc *mode_blk;
2666 int mode_buffer_len;
2667 struct sa_softc *softc;
2672 softc = (struct sa_softc *)periph->softc;
2673 ccb = cam_periph_getccb(periph, 1);
2674 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2675 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2677 cpage = SA_DATA_COMPRESSION_PAGE;
2680 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2682 if (params_to_get & SA_PARAM_COMPRESSION) {
2683 if (softc->quirks & SA_QUIRK_NOCOMP) {
2684 *comp_supported = FALSE;
2685 params_to_get &= ~SA_PARAM_COMPRESSION;
2687 mode_buffer_len += sizeof (sa_comp_t);
2690 /* XXX Fix M_NOWAIT */
2691 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
2692 if (mode_buffer == NULL) {
2693 xpt_release_ccb(ccb);
2696 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2697 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2699 /* it is safe to retry this */
2700 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2701 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2702 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2703 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2705 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2706 softc->device_stats);
2708 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2710 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2712 * Hmm. Let's see if we can try another page...
2713 * If we've already done that, give up on compression
2714 * for this device and remember this for the future
2715 * and attempt the request without asking for compression
2718 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2719 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2722 softc->quirks |= SA_QUIRK_NOCOMP;
2723 free(mode_buffer, M_SCSISA);
2725 } else if (status == CAM_SCSI_STATUS_ERROR) {
2726 /* Tell the user about the fatal error. */
2727 scsi_sense_print(&ccb->csio);
2728 goto sagetparamsexit;
2732 * If the user only wants the compression information, and
2733 * the device doesn't send back the block descriptor, it's
2734 * no big deal. If the user wants more than just
2735 * compression, though, and the device doesn't pass back the
2736 * block descriptor, we need to send another mode sense to
2737 * get the block descriptor.
2739 if ((mode_hdr->blk_desc_len == 0) &&
2740 (params_to_get & SA_PARAM_COMPRESSION) &&
2741 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2744 * Decrease the mode buffer length by the size of
2745 * the compression page, to make sure the data
2746 * there doesn't get overwritten.
2748 mode_buffer_len -= sizeof (sa_comp_t);
2751 * Now move the compression page that we presumably
2752 * got back down the memory chunk a little bit so
2753 * it doesn't get spammed.
2755 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2756 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2759 * Now, we issue another mode sense and just ask
2760 * for the block descriptor, etc.
2763 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2764 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2765 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2768 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2769 softc->device_stats);
2772 goto sagetparamsexit;
2775 if (params_to_get & SA_PARAM_BLOCKSIZE)
2776 *blocksize = scsi_3btoul(mode_blk->blklen);
2778 if (params_to_get & SA_PARAM_NUMBLOCKS)
2779 *numblocks = scsi_3btoul(mode_blk->nblocks);
2781 if (params_to_get & SA_PARAM_BUFF_MODE)
2782 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2784 if (params_to_get & SA_PARAM_DENSITY)
2785 *density = mode_blk->density;
2787 if (params_to_get & SA_PARAM_WP)
2788 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2790 if (params_to_get & SA_PARAM_SPEED)
2791 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2793 if (params_to_get & SA_PARAM_COMPRESSION) {
2794 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2795 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2796 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2798 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2800 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2801 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2803 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2805 * We don't really know whether this device supports
2806 * Data Compression if the algorithm field is
2807 * zero. Just say we do.
2809 *comp_supported = TRUE;
2811 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2812 *comp_algorithm = cp->sel_comp_alg;
2815 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2818 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2820 char *xyz = mode_buffer;
2821 xpt_print_path(periph->path);
2822 printf("Mode Sense Data=");
2823 for (idx = 0; idx < mode_buffer_len; idx++)
2824 printf(" 0x%02x", xyz[idx] & 0xff);
2830 xpt_release_ccb(ccb);
2831 free(mode_buffer, M_SCSISA);
2836 * The purpose of this function is to set one of four different parameters
2840 * - compression / compression algorithm
2843 * The assumption is that this will be called from saioctl(), and therefore
2844 * from a process context. Thus the waiting malloc calls below. If that
2845 * assumption ever changes, the malloc calls should be changed to be
2848 * Any or all of the four parameters may be set when this function is
2849 * called. It should handle setting more than one parameter at once.
2852 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2853 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2854 u_int32_t sense_flags)
2856 struct sa_softc *softc;
2857 u_int32_t current_blocksize;
2858 u_int32_t current_calg;
2859 u_int8_t current_density;
2860 u_int8_t current_speed;
2861 int comp_enabled, comp_supported;
2863 int mode_buffer_len;
2864 struct scsi_mode_header_6 *mode_hdr;
2865 struct scsi_mode_blk_desc *mode_blk;
2866 sa_comp_t *ccomp, *cpage;
2868 union ccb *ccb = NULL;
2871 softc = (struct sa_softc *)periph->softc;
2873 ccomp = malloc(sizeof (sa_comp_t), M_SCSISA, M_NOWAIT);
2878 * Since it doesn't make sense to set the number of blocks, or
2879 * write protection, we won't try to get the current value. We
2880 * always want to get the blocksize, so we can set it back to the
2883 error = sagetparams(periph,
2884 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2885 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2886 ¤t_speed, &comp_supported, &comp_enabled,
2887 ¤t_calg, ccomp);
2890 free(ccomp, M_SCSISA);
2894 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2895 if (params_to_set & SA_PARAM_COMPRESSION)
2896 mode_buffer_len += sizeof (sa_comp_t);
2898 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
2899 if (mode_buffer == NULL) {
2900 free(ccomp, M_SCSISA);
2904 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2905 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2907 ccb = cam_periph_getccb(periph, 1);
2911 if (params_to_set & SA_PARAM_COMPRESSION) {
2913 cpage = (sa_comp_t *)&mode_blk[1];
2915 cpage = (sa_comp_t *)&mode_hdr[1];
2917 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2918 cpage->hdr.pagecode &= ~0x80;
2923 * If the caller wants us to set the blocksize, use the one they
2924 * pass in. Otherwise, use the blocksize we got back from the
2925 * mode select above.
2928 if (params_to_set & SA_PARAM_BLOCKSIZE)
2929 scsi_ulto3b(blocksize, mode_blk->blklen);
2931 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2934 * Set density if requested, else preserve old density.
2935 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2936 * devices, else density we've latched up in our softc.
2938 if (params_to_set & SA_PARAM_DENSITY) {
2939 mode_blk->density = density;
2940 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2941 mode_blk->density = SCSI_SAME_DENSITY;
2943 mode_blk->density = softc->media_density;
2948 * For mode selects, these two fields must be zero.
2950 mode_hdr->data_length = 0;
2951 mode_hdr->medium_type = 0;
2953 /* set the speed to the current value */
2954 mode_hdr->dev_spec = current_speed;
2956 /* if set, set single-initiator buffering mode */
2957 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
2958 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2962 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2964 mode_hdr->blk_desc_len = 0;
2967 * First, if the user wants us to set the compression algorithm or
2968 * just turn compression on, check to make sure that this drive
2969 * supports compression.
2971 if (params_to_set & SA_PARAM_COMPRESSION) {
2973 * If the compression algorithm is 0, disable compression.
2974 * If the compression algorithm is non-zero, enable
2975 * compression and set the compression type to the
2976 * specified compression algorithm, unless the algorithm is
2977 * MT_COMP_ENABLE. In that case, we look at the
2978 * compression algorithm that is currently set and if it is
2979 * non-zero, we leave it as-is. If it is zero, and we have
2980 * saved a compression algorithm from a time when
2981 * compression was enabled before, set the compression to
2984 switch (ccomp->hdr.pagecode & ~0x80) {
2985 case SA_DEVICE_CONFIGURATION_PAGE:
2987 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2989 dcp->sel_comp_alg = SA_COMP_NONE;
2992 if (calg != MT_COMP_ENABLE) {
2993 dcp->sel_comp_alg = calg;
2994 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2995 softc->saved_comp_algorithm != 0) {
2996 dcp->sel_comp_alg = softc->saved_comp_algorithm;
3000 case SA_DATA_COMPRESSION_PAGE:
3001 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
3002 struct scsi_data_compression_page *dcp = &cpage->dcomp;
3005 * Disable compression, but leave the
3006 * decompression and the capability bit
3009 dcp->dce_and_dcc = SA_DCP_DCC;
3010 dcp->dde_and_red |= SA_DCP_DDE;
3013 /* enable compression && decompression */
3014 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
3015 dcp->dde_and_red |= SA_DCP_DDE;
3017 * If there, use compression algorithm from caller.
3018 * Otherwise, if there's a saved compression algorithm
3019 * and there is no current algorithm, use the saved
3020 * algorithm. Else parrot back what we got and hope
3023 if (calg != MT_COMP_ENABLE) {
3024 scsi_ulto4b(calg, dcp->comp_algorithm);
3025 scsi_ulto4b(calg, dcp->decomp_algorithm);
3026 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
3027 softc->saved_comp_algorithm != 0) {
3028 scsi_ulto4b(softc->saved_comp_algorithm,
3029 dcp->comp_algorithm);
3030 scsi_ulto4b(softc->saved_comp_algorithm,
3031 dcp->decomp_algorithm);
3036 * Compression does not appear to be supported-
3037 * at least via the DATA COMPRESSION page. It
3038 * would be too much to ask us to believe that
3039 * the page itself is supported, but incorrectly
3040 * reports an ability to manipulate data compression,
3041 * so we'll assume that this device doesn't support
3042 * compression. We can just fall through for that.
3047 * The drive doesn't seem to support compression,
3048 * so turn off the set compression bit.
3050 params_to_set &= ~SA_PARAM_COMPRESSION;
3051 xpt_print(periph->path,
3052 "device does not seem to support compression\n");
3055 * If that was the only thing the user wanted us to set,
3056 * clean up allocated resources and return with
3057 * 'operation not supported'.
3059 if (params_to_set == SA_PARAM_NONE) {
3060 free(mode_buffer, M_SCSISA);
3061 xpt_release_ccb(ccb);
3066 * That wasn't the only thing the user wanted us to set.
3067 * So, decrease the stated mode buffer length by the
3068 * size of the compression mode page.
3070 mode_buffer_len -= sizeof(sa_comp_t);
3074 /* It is safe to retry this operation */
3075 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
3076 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
3077 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3079 error = cam_periph_runccb(ccb, saerror, 0,
3080 sense_flags, softc->device_stats);
3082 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3084 char *xyz = mode_buffer;
3085 xpt_print_path(periph->path);
3086 printf("Err%d, Mode Select Data=", error);
3087 for (idx = 0; idx < mode_buffer_len; idx++)
3088 printf(" 0x%02x", xyz[idx] & 0xff);
3095 * If we can, try without setting density/blocksize.
3098 if ((params_to_set &
3099 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
3104 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
3105 cpage = (sa_comp_t *)&mode_blk[1];
3109 * If we were setting the blocksize, and that failed, we
3110 * want to set it to its original value. If we weren't
3111 * setting the blocksize, we don't want to change it.
3113 scsi_ulto3b(current_blocksize, mode_blk->blklen);
3116 * Set density if requested, else preserve old density.
3117 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
3118 * devices, else density we've latched up in our softc.
3120 if (params_to_set & SA_PARAM_DENSITY) {
3121 mode_blk->density = current_density;
3122 } else if (softc->scsi_rev > SCSI_REV_CCS) {
3123 mode_blk->density = SCSI_SAME_DENSITY;
3125 mode_blk->density = softc->media_density;
3128 if (params_to_set & SA_PARAM_COMPRESSION)
3129 bcopy(ccomp, cpage, sizeof (sa_comp_t));
3132 * The retry count is the only CCB field that might have been
3133 * changed that we care about, so reset it back to 1.
3135 ccb->ccb_h.retry_count = 1;
3136 cam_periph_runccb(ccb, saerror, 0, sense_flags,
3137 softc->device_stats);
3140 xpt_release_ccb(ccb);
3143 free(ccomp, M_SCSISA);
3145 if (params_to_set & SA_PARAM_COMPRESSION) {
3147 softc->flags &= ~SA_FLAG_COMP_ENABLED;
3149 * Even if we get an error setting compression,
3150 * do not say that we don't support it. We could
3151 * have been wrong, or it may be media specific.
3152 * softc->flags &= ~SA_FLAG_COMP_SUPP;
3154 softc->saved_comp_algorithm = softc->comp_algorithm;
3155 softc->comp_algorithm = 0;
3157 softc->flags |= SA_FLAG_COMP_ENABLED;
3158 softc->comp_algorithm = calg;
3162 free(mode_buffer, M_SCSISA);
3167 saprevent(struct cam_periph *periph, int action)
3169 struct sa_softc *softc;
3173 softc = (struct sa_softc *)periph->softc;
3175 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3177 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3181 * We can be quiet about illegal requests.
3183 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3188 ccb = cam_periph_getccb(periph, 1);
3190 /* It is safe to retry this operation */
3191 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3192 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3194 error = cam_periph_runccb(ccb, saerror, 0, sf, softc->device_stats);
3196 if (action == PR_ALLOW)
3197 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3199 softc->flags |= SA_FLAG_TAPE_LOCKED;
3202 xpt_release_ccb(ccb);
3206 sarewind(struct cam_periph *periph)
3209 struct sa_softc *softc;
3212 softc = (struct sa_softc *)periph->softc;
3214 ccb = cam_periph_getccb(periph, 1);
3216 /* It is safe to retry this operation */
3217 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3218 SSD_FULL_SIZE, REWIND_TIMEOUT);
3220 softc->dsreg = MTIO_DSREG_REW;
3221 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3222 softc->dsreg = MTIO_DSREG_REST;
3224 xpt_release_ccb(ccb);
3226 softc->fileno = softc->blkno = (daddr_t) 0;
3228 softc->fileno = softc->blkno = (daddr_t) -1;
3233 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3236 struct sa_softc *softc;
3239 softc = (struct sa_softc *)periph->softc;
3241 ccb = cam_periph_getccb(periph, 1);
3243 /* This cannot be retried */
3245 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3246 SSD_FULL_SIZE, SPACE_TIMEOUT);
3249 * Clear residual because we will be using it.
3251 softc->last_ctl_resid = 0;
3253 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3254 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3255 softc->dsreg = MTIO_DSREG_REST;
3257 xpt_release_ccb(ccb);
3260 * If a spacing operation has failed, we need to invalidate
3263 * If the spacing operation was setmarks or to end of recorded data,
3264 * we no longer know our relative position.
3266 * If the spacing operations was spacing files in reverse, we
3267 * take account of the residual, but still check against less
3268 * than zero- if we've gone negative, we must have hit BOT.
3270 * If the spacing operations was spacing records in reverse and
3271 * we have a residual, we've either hit BOT or hit a filemark.
3272 * In the former case, we know our new record number (0). In
3273 * the latter case, we have absolutely no idea what the real
3274 * record number is- we've stopped between the end of the last
3275 * record in the previous file and the filemark that stopped
3276 * our spacing backwards.
3279 softc->fileno = softc->blkno = (daddr_t) -1;
3280 } else if (code == SS_SETMARKS || code == SS_EOD) {
3281 softc->fileno = softc->blkno = (daddr_t) -1;
3282 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3283 softc->fileno += (count - softc->last_ctl_resid);
3284 if (softc->fileno < 0) /* we must of hit BOT */
3287 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3288 softc->blkno += (count - softc->last_ctl_resid);
3290 if (softc->last_ctl_resid || softc->blkno < 0) {
3291 if (softc->fileno == 0) {
3294 softc->blkno = (daddr_t) -1;
3303 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3306 struct sa_softc *softc;
3309 softc = (struct sa_softc *)periph->softc;
3310 if (softc->open_rdonly)
3313 ccb = cam_periph_getccb(periph, 1);
3315 * Clear residual because we will be using it.
3317 softc->last_ctl_resid = 0;
3319 softc->dsreg = MTIO_DSREG_FMK;
3320 /* this *must* not be retried */
3321 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3322 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3323 softc->dsreg = MTIO_DSREG_REST;
3326 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3328 if (error == 0 && nmarks) {
3329 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3330 nwm = nmarks - softc->last_ctl_resid;
3331 softc->filemarks += nwm;
3334 xpt_release_ccb(ccb);
3337 * Update relative positions (if we're doing that).
3340 softc->fileno = softc->blkno = (daddr_t) -1;
3341 } else if (softc->fileno != (daddr_t) -1) {
3342 softc->fileno += nwm;
3349 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3351 struct scsi_tape_position_data loc;
3353 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3357 * We try and flush any buffered writes here if we were writing
3358 * and we're trying to get hardware block position. It eats
3359 * up performance substantially, but I'm wary of drive firmware.
3361 * I think that *logical* block position is probably okay-
3362 * but hardware block position might have to wait for data
3363 * to hit media to be valid. Caveat Emptor.
3366 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3367 error = sawritefilemarks(periph, 0, 0);
3368 if (error && error != EACCES)
3372 ccb = cam_periph_getccb(periph, 1);
3373 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3374 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3375 softc->dsreg = MTIO_DSREG_RBSY;
3376 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3377 softc->dsreg = MTIO_DSREG_REST;
3380 if (loc.flags & SA_RPOS_UNCERTAIN) {
3381 error = EINVAL; /* nothing is certain */
3383 *blkptr = scsi_4btoul(loc.firstblk);
3387 xpt_release_ccb(ccb);
3392 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3395 struct sa_softc *softc;
3399 * We used to try and flush any buffered writes here.
3400 * Now we push this onto user applications to either
3401 * flush the pending writes themselves (via a zero count
3402 * WRITE FILEMARKS command) or they can trust their tape
3403 * drive to do this correctly for them.
3406 softc = (struct sa_softc *)periph->softc;
3407 ccb = cam_periph_getccb(periph, 1);
3410 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3411 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3414 softc->dsreg = MTIO_DSREG_POS;
3415 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3416 softc->dsreg = MTIO_DSREG_REST;
3417 xpt_release_ccb(ccb);
3419 * Note relative file && block number position as now unknown.
3421 softc->fileno = softc->blkno = (daddr_t) -1;
3426 saretension(struct cam_periph *periph)
3429 struct sa_softc *softc;
3432 softc = (struct sa_softc *)periph->softc;
3434 ccb = cam_periph_getccb(periph, 1);
3436 /* It is safe to retry this operation */
3437 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3438 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3440 softc->dsreg = MTIO_DSREG_TEN;
3441 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3442 softc->dsreg = MTIO_DSREG_REST;
3444 xpt_release_ccb(ccb);
3446 softc->fileno = softc->blkno = (daddr_t) 0;
3448 softc->fileno = softc->blkno = (daddr_t) -1;
3453 sareservereleaseunit(struct cam_periph *periph, int reserve)
3456 struct sa_softc *softc;
3459 softc = (struct sa_softc *)periph->softc;
3460 ccb = cam_periph_getccb(periph, 1);
3462 /* It is safe to retry this operation */
3463 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3464 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3465 softc->dsreg = MTIO_DSREG_RBSY;
3466 error = cam_periph_runccb(ccb, saerror, 0,
3467 SF_RETRY_UA | SF_NO_PRINT, softc->device_stats);
3468 softc->dsreg = MTIO_DSREG_REST;
3469 xpt_release_ccb(ccb);
3472 * If the error was Illegal Request, then the device doesn't support
3473 * RESERVE/RELEASE. This is not an error.
3475 if (error == EINVAL) {
3483 saloadunload(struct cam_periph *periph, int load)
3486 struct sa_softc *softc;
3489 softc = (struct sa_softc *)periph->softc;
3491 ccb = cam_periph_getccb(periph, 1);
3493 /* It is safe to retry this operation */
3494 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3495 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3497 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3498 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3499 softc->dsreg = MTIO_DSREG_REST;
3500 xpt_release_ccb(ccb);
3502 if (error || load == 0)
3503 softc->fileno = softc->blkno = (daddr_t) -1;
3504 else if (error == 0)
3505 softc->fileno = softc->blkno = (daddr_t) 0;
3510 saerase(struct cam_periph *periph, int longerase)
3514 struct sa_softc *softc;
3517 softc = (struct sa_softc *)periph->softc;
3518 if (softc->open_rdonly)
3521 ccb = cam_periph_getccb(periph, 1);
3523 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3524 SSD_FULL_SIZE, ERASE_TIMEOUT);
3526 softc->dsreg = MTIO_DSREG_ZER;
3527 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3528 softc->dsreg = MTIO_DSREG_REST;
3530 xpt_release_ccb(ccb);
3534 #endif /* _KERNEL */
3537 * Read tape block limits command.
3540 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3541 void (*cbfcnp)(struct cam_periph *, union ccb *),
3542 u_int8_t tag_action,
3543 struct scsi_read_block_limits_data *rlimit_buf,
3544 u_int8_t sense_len, u_int32_t timeout)
3546 struct scsi_read_block_limits *scsi_cmd;
3548 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3549 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3550 sizeof(*scsi_cmd), timeout);
3552 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3553 bzero(scsi_cmd, sizeof(*scsi_cmd));
3554 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3558 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3559 void (*cbfcnp)(struct cam_periph *, union ccb *),
3560 u_int8_t tag_action, int readop, int sli,
3561 int fixed, u_int32_t length, u_int8_t *data_ptr,
3562 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3564 struct scsi_sa_rw *scsi_cmd;
3567 read = (readop & SCSI_RW_DIRMASK) == SCSI_RW_READ;
3569 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3570 scsi_cmd->opcode = read ? SA_READ : SA_WRITE;
3571 scsi_cmd->sli_fixed = 0;
3573 scsi_cmd->sli_fixed |= SAR_SLI;
3575 scsi_cmd->sli_fixed |= SARW_FIXED;
3576 scsi_ulto3b(length, scsi_cmd->length);
3577 scsi_cmd->control = 0;
3579 cam_fill_csio(csio, retries, cbfcnp, (read ? CAM_DIR_IN : CAM_DIR_OUT) |
3580 ((readop & SCSI_RW_BIO) != 0 ? CAM_DATA_BIO : 0),
3581 tag_action, data_ptr, dxfer_len, sense_len,
3582 sizeof(*scsi_cmd), timeout);
3586 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3587 void (*cbfcnp)(struct cam_periph *, union ccb *),
3588 u_int8_t tag_action, int immediate, int eot,
3589 int reten, int load, u_int8_t sense_len,
3592 struct scsi_load_unload *scsi_cmd;
3594 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3595 bzero(scsi_cmd, sizeof(*scsi_cmd));
3596 scsi_cmd->opcode = LOAD_UNLOAD;
3598 scsi_cmd->immediate = SLU_IMMED;
3600 scsi_cmd->eot_reten_load |= SLU_EOT;
3602 scsi_cmd->eot_reten_load |= SLU_RETEN;
3604 scsi_cmd->eot_reten_load |= SLU_LOAD;
3606 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3607 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3611 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3612 void (*cbfcnp)(struct cam_periph *, union ccb *),
3613 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3616 struct scsi_rewind *scsi_cmd;
3618 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3619 bzero(scsi_cmd, sizeof(*scsi_cmd));
3620 scsi_cmd->opcode = REWIND;
3622 scsi_cmd->immediate = SREW_IMMED;
3624 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3625 0, sense_len, sizeof(*scsi_cmd), timeout);
3629 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3630 void (*cbfcnp)(struct cam_periph *, union ccb *),
3631 u_int8_t tag_action, scsi_space_code code,
3632 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3634 struct scsi_space *scsi_cmd;
3636 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3637 scsi_cmd->opcode = SPACE;
3638 scsi_cmd->code = code;
3639 scsi_ulto3b(count, scsi_cmd->count);
3640 scsi_cmd->control = 0;
3642 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3643 0, sense_len, sizeof(*scsi_cmd), timeout);
3647 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3648 void (*cbfcnp)(struct cam_periph *, union ccb *),
3649 u_int8_t tag_action, int immediate, int setmark,
3650 u_int32_t num_marks, u_int8_t sense_len,
3653 struct scsi_write_filemarks *scsi_cmd;
3655 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3656 bzero(scsi_cmd, sizeof(*scsi_cmd));
3657 scsi_cmd->opcode = WRITE_FILEMARKS;
3659 scsi_cmd->byte2 |= SWFMRK_IMMED;
3661 scsi_cmd->byte2 |= SWFMRK_WSMK;
3663 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3665 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3666 0, sense_len, sizeof(*scsi_cmd), timeout);
3670 * The reserve and release unit commands differ only by their opcodes.
3673 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3674 void (*cbfcnp)(struct cam_periph *, union ccb *),
3675 u_int8_t tag_action, int third_party,
3676 int third_party_id, u_int8_t sense_len,
3677 u_int32_t timeout, int reserve)
3679 struct scsi_reserve_release_unit *scsi_cmd;
3681 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3682 bzero(scsi_cmd, sizeof(*scsi_cmd));
3685 scsi_cmd->opcode = RESERVE_UNIT;
3687 scsi_cmd->opcode = RELEASE_UNIT;
3690 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3691 scsi_cmd->lun_thirdparty |=
3692 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3695 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3696 0, sense_len, sizeof(*scsi_cmd), timeout);
3700 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3701 void (*cbfcnp)(struct cam_periph *, union ccb *),
3702 u_int8_t tag_action, int immediate, int long_erase,
3703 u_int8_t sense_len, u_int32_t timeout)
3705 struct scsi_erase *scsi_cmd;
3707 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3708 bzero(scsi_cmd, sizeof(*scsi_cmd));
3710 scsi_cmd->opcode = ERASE;
3713 scsi_cmd->lun_imm_long |= SE_IMMED;
3716 scsi_cmd->lun_imm_long |= SE_LONG;
3718 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3719 0, sense_len, sizeof(*scsi_cmd), timeout);
3723 * Read Tape Position command.
3726 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3727 void (*cbfcnp)(struct cam_periph *, union ccb *),
3728 u_int8_t tag_action, int hardsoft,
3729 struct scsi_tape_position_data *sbp,
3730 u_int8_t sense_len, u_int32_t timeout)
3732 struct scsi_tape_read_position *scmd;
3734 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3735 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3736 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3737 bzero(scmd, sizeof(*scmd));
3738 scmd->opcode = READ_POSITION;
3739 scmd->byte1 = hardsoft;
3743 * Set Tape Position command.
3746 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3747 void (*cbfcnp)(struct cam_periph *, union ccb *),
3748 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3749 u_int8_t sense_len, u_int32_t timeout)
3751 struct scsi_tape_locate *scmd;
3753 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3754 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3755 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3756 bzero(scmd, sizeof(*scmd));
3757 scmd->opcode = LOCATE;
3759 scmd->byte1 |= SA_SPOS_BT;
3760 scsi_ulto4b(blkno, scmd->blkaddr);