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_WAITING 0x1
118 #define SA_CCB_TYPEMASK 0x1
119 #define SA_POSITION_UPDATED 0x2
121 #define Set_CCB_Type(x, type) \
122 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
123 x->ccb_h.ccb_pflags |= type
125 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
130 SA_FLAG_OPEN = 0x0001,
131 SA_FLAG_FIXED = 0x0002,
132 SA_FLAG_TAPE_LOCKED = 0x0004,
133 SA_FLAG_TAPE_MOUNTED = 0x0008,
134 SA_FLAG_TAPE_WP = 0x0010,
135 SA_FLAG_TAPE_WRITTEN = 0x0020,
136 SA_FLAG_EOM_PENDING = 0x0040,
137 SA_FLAG_EIO_PENDING = 0x0080,
138 SA_FLAG_EOF_PENDING = 0x0100,
139 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
140 SA_FLAG_EOF_PENDING),
141 SA_FLAG_INVALID = 0x0200,
142 SA_FLAG_COMP_ENABLED = 0x0400,
143 SA_FLAG_COMP_SUPP = 0x0800,
144 SA_FLAG_COMP_UNSUPP = 0x1000,
145 SA_FLAG_TAPE_FROZEN = 0x2000
149 SA_MODE_REWIND = 0x00,
150 SA_MODE_NOREWIND = 0x01,
151 SA_MODE_OFFLINE = 0x02
155 SA_PARAM_NONE = 0x00,
156 SA_PARAM_BLOCKSIZE = 0x01,
157 SA_PARAM_DENSITY = 0x02,
158 SA_PARAM_COMPRESSION = 0x04,
159 SA_PARAM_BUFF_MODE = 0x08,
160 SA_PARAM_NUMBLOCKS = 0x10,
162 SA_PARAM_SPEED = 0x40,
167 SA_QUIRK_NONE = 0x00,
168 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
169 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
170 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
171 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
172 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
173 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
174 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
175 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
178 #define SA_QUIRK_BIT_STRING \
189 #define SAMODE(z) (dev2unit(z) & 0x3)
190 #define SADENSITY(z) ((dev2unit(z) >> 2) & 0x3)
191 #define SA_IS_CTRL(z) (dev2unit(z) & (1 << 4))
193 #define SA_NOT_CTLDEV 0
197 #define SA_ATYPE_NR 1
198 #define SA_ATYPE_ER 2
200 #define SAMINOR(ctl, mode, access) \
201 ((ctl << 4) | (mode << 2) | (access & 0x3))
203 #define SA_NUM_MODES 4
205 struct cdev *ctl_dev;
206 struct sa_mode_devs {
210 } mode_devs[SA_NUM_MODES];
218 struct bio_queue_head bio_queue;
220 struct devstat *device_stats;
230 u_int32_t comp_algorithm;
231 u_int32_t saved_comp_algorithm;
232 u_int32_t media_blksize;
233 u_int32_t last_media_blksize;
234 u_int32_t media_numblks;
235 u_int8_t media_density;
238 u_int8_t dsreg; /* mtio mt_dsreg, redux */
242 int last_resid_was_io;
245 * Relative to BOT Location.
254 struct scsi_sense_data _last_io_sense;
255 u_int64_t _last_io_resid;
256 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
257 struct scsi_sense_data _last_ctl_sense;
258 u_int64_t _last_ctl_resid;
259 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
260 #define last_io_sense errinfo._last_io_sense
261 #define last_io_resid errinfo._last_io_resid
262 #define last_io_cdb errinfo._last_io_cdb
263 #define last_ctl_sense errinfo._last_ctl_sense
264 #define last_ctl_resid errinfo._last_ctl_resid
265 #define last_ctl_cdb errinfo._last_ctl_cdb
268 * Misc other flags/state
272 open_rdonly : 1, /* open read-only */
273 open_pending_mount : 1, /* open pending mount */
274 ctrl_mode : 1; /* control device open */
276 struct task sysctl_task;
277 struct sysctl_ctx_list sysctl_ctx;
278 struct sysctl_oid *sysctl_tree;
281 struct sa_quirk_entry {
282 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
283 sa_quirks quirks; /* specific quirk type */
284 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
287 static struct sa_quirk_entry sa_quirk_table[] =
290 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
291 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
292 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
295 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
296 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
299 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
300 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
303 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
304 "Python*", "*"}, SA_QUIRK_NODREAD, 0
307 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
308 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
311 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
312 "VIPER 2525 25462", "-011"},
313 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
316 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
317 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
321 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
322 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
326 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
327 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
330 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
331 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
334 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
335 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
338 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
339 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
342 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
343 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
346 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
347 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
349 { /* jreynold@primenet.com */
350 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
351 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
353 { /* mike@sentex.net */
354 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
355 "STT20000*", "*"}, SA_QUIRK_1FM, 0
358 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "SEAGATE",
359 "DAT 06241-XXX", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
362 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
363 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
366 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
367 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
370 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
371 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
374 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
375 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
378 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
379 " SLR*", "*"}, SA_QUIRK_1FM, 0
382 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
383 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
386 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
387 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
391 static d_open_t saopen;
392 static d_close_t saclose;
393 static d_strategy_t sastrategy;
394 static d_ioctl_t saioctl;
395 static periph_init_t sainit;
396 static periph_ctor_t saregister;
397 static periph_oninv_t saoninvalidate;
398 static periph_dtor_t sacleanup;
399 static periph_start_t sastart;
400 static void saasync(void *callback_arg, u_int32_t code,
401 struct cam_path *path, void *arg);
402 static void sadone(struct cam_periph *periph,
403 union ccb *start_ccb);
404 static int saerror(union ccb *ccb, u_int32_t cam_flags,
405 u_int32_t sense_flags);
406 static int samarkswanted(struct cam_periph *);
407 static int sacheckeod(struct cam_periph *periph);
408 static int sagetparams(struct cam_periph *periph,
409 sa_params params_to_get,
410 u_int32_t *blocksize, u_int8_t *density,
411 u_int32_t *numblocks, int *buff_mode,
412 u_int8_t *write_protect, u_int8_t *speed,
413 int *comp_supported, int *comp_enabled,
414 u_int32_t *comp_algorithm,
415 sa_comp_t *comp_page);
416 static int sasetparams(struct cam_periph *periph,
417 sa_params params_to_set,
418 u_int32_t blocksize, u_int8_t density,
419 u_int32_t comp_algorithm,
420 u_int32_t sense_flags);
421 static void saprevent(struct cam_periph *periph, int action);
422 static int sarewind(struct cam_periph *periph);
423 static int saspace(struct cam_periph *periph, int count,
424 scsi_space_code code);
425 static int samount(struct cam_periph *, int, struct cdev *);
426 static int saretension(struct cam_periph *periph);
427 static int sareservereleaseunit(struct cam_periph *periph,
429 static int saloadunload(struct cam_periph *periph, int load);
430 static int saerase(struct cam_periph *periph, int longerase);
431 static int sawritefilemarks(struct cam_periph *periph,
432 int nmarks, int setmarks);
433 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
434 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
437 #ifndef SA_DEFAULT_IO_SPLIT
438 #define SA_DEFAULT_IO_SPLIT 0
441 static int sa_allow_io_split = SA_DEFAULT_IO_SPLIT;
444 * Tunable to allow the user to set a global allow_io_split value. Note
445 * that this WILL GO AWAY in FreeBSD 11.0. Silently splitting the I/O up
446 * is bad behavior, because it hides the true tape block size from the
449 TUNABLE_INT("kern.cam.sa.allow_io_split", &sa_allow_io_split);
450 static SYSCTL_NODE(_kern_cam, OID_AUTO, sa, CTLFLAG_RD, 0,
451 "CAM Sequential Access Tape Driver");
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, cgd->ccb_h.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,
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);
1725 if (periph->immediate_priority <= periph->pinfo.priority) {
1726 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1727 ("queuing for immediate ccb\n"));
1728 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1729 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1731 periph->immediate_priority = CAM_PRIORITY_NONE;
1732 wakeup(&periph->ccb_list);
1733 } else if (bp == NULL) {
1734 xpt_release_ccb(start_ccb);
1735 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1736 struct bio *done_bp;
1738 softc->queue_count--;
1739 bioq_remove(&softc->bio_queue, bp);
1740 bp->bio_resid = bp->bio_bcount;
1742 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1744 * We now just clear errors in this case
1745 * and let the residual be the notifier.
1748 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1750 * This can only happen if we're reading
1751 * in fixed length mode. In this case,
1752 * we dump the rest of the list the
1756 if (bioq_first(&softc->bio_queue) != NULL) {
1760 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1761 bp->bio_error = EIO;
1762 bp->bio_flags |= BIO_ERROR;
1764 bp = bioq_first(&softc->bio_queue);
1766 * Only if we have no other buffers queued up
1767 * do we clear the pending error flag.
1770 softc->flags &= ~SA_FLAG_ERR_PENDING;
1771 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1772 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
1773 "%d more buffers queued up\n",
1774 (softc->flags & SA_FLAG_ERR_PENDING),
1775 (bp != NULL)? "not " : " ", softc->queue_count));
1776 xpt_release_ccb(start_ccb);
1781 bioq_remove(&softc->bio_queue, bp);
1782 softc->queue_count--;
1784 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1785 if (softc->blk_shift != 0) {
1787 bp->bio_bcount >> softc->blk_shift;
1788 } else if (softc->media_blksize != 0) {
1789 length = bp->bio_bcount /
1790 softc->media_blksize;
1792 bp->bio_error = EIO;
1793 xpt_print(periph->path, "zero blocksize"
1794 " for FIXED length writes?\n");
1799 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1800 ("issuing a %d fixed record %s\n",
1801 length, (bp->bio_cmd == BIO_READ)? "read" :
1805 length = bp->bio_bcount;
1807 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1808 ("issuing a %d variable byte %s\n",
1809 length, (bp->bio_cmd == BIO_READ)? "read" :
1813 devstat_start_transaction_bio(softc->device_stats, bp);
1815 * Some people have theorized that we should
1816 * suppress illegal length indication if we are
1817 * running in variable block mode so that we don't
1818 * have to request sense every time our requested
1819 * block size is larger than the written block.
1820 * The residual information from the ccb allows
1821 * us to identify this situation anyway. The only
1822 * problem with this is that we will not get
1823 * information about blocks that are larger than
1824 * our read buffer unless we set the block size
1825 * in the mode page to something other than 0.
1827 * I believe that this is a non-issue. If user apps
1828 * don't adjust their read size to match our record
1829 * size, that's just life. Anyway, the typical usage
1830 * would be to issue, e.g., 64KB reads and occasionally
1831 * have to do deal with 512 byte or 1KB intermediate
1834 softc->dsreg = (bp->bio_cmd == BIO_READ)?
1835 MTIO_DSREG_RD : MTIO_DSREG_WR;
1836 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1837 MSG_SIMPLE_Q_TAG, (bp->bio_cmd == BIO_READ ?
1838 SCSI_RW_READ : SCSI_RW_WRITE) |
1839 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
1840 SCSI_RW_BIO : 0), FALSE,
1841 (softc->flags & SA_FLAG_FIXED) != 0, length,
1842 (bp->bio_flags & BIO_UNMAPPED) != 0 ? (void *)bp :
1843 bp->bio_data, bp->bio_bcount, SSD_FULL_SIZE,
1845 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1846 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1847 start_ccb->ccb_h.ccb_bp = bp;
1848 bp = bioq_first(&softc->bio_queue);
1849 xpt_action(start_ccb);
1853 /* Have more work to do, so ensure we stay scheduled */
1854 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1858 case SA_STATE_ABNORMAL:
1860 panic("state 0x%x in sastart", softc->state);
1867 sadone(struct cam_periph *periph, union ccb *done_ccb)
1869 struct sa_softc *softc;
1870 struct ccb_scsiio *csio;
1872 softc = (struct sa_softc *)periph->softc;
1873 csio = &done_ccb->csio;
1874 switch (CCB_Type(csio)) {
1875 case SA_CCB_BUFFER_IO:
1880 softc->dsreg = MTIO_DSREG_REST;
1881 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1883 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1884 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1886 * A retry was scheduled, so just return.
1895 * Catastrophic error. Mark the tape as frozen
1896 * (we no longer know tape position).
1898 * Return all queued I/O with EIO, and unfreeze
1899 * our queue so that future transactions that
1900 * attempt to fix this problem can get to the
1905 softc->flags |= SA_FLAG_TAPE_FROZEN;
1906 bioq_flush(&softc->bio_queue, NULL, EIO);
1909 bp->bio_resid = bp->bio_bcount;
1910 bp->bio_error = error;
1911 bp->bio_flags |= BIO_ERROR;
1913 * In the error case, position is updated in saerror.
1916 bp->bio_resid = csio->resid;
1918 if (csio->resid != 0) {
1919 bp->bio_flags |= BIO_ERROR;
1921 if (bp->bio_cmd == BIO_WRITE) {
1922 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1923 softc->filemarks = 0;
1925 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1926 (softc->blkno != (daddr_t) -1)) {
1927 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1929 if (softc->blk_shift != 0) {
1930 l = bp->bio_bcount >>
1933 l = bp->bio_bcount /
1934 softc->media_blksize;
1936 softc->blkno += (daddr_t) l;
1943 * If we had an error (immediate or pending),
1944 * release the device queue now.
1946 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1947 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1948 if (error || bp->bio_resid) {
1949 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1950 ("error %d resid %ld count %ld\n", error,
1951 bp->bio_resid, bp->bio_bcount));
1953 biofinish(bp, softc->device_stats, 0);
1956 case SA_CCB_WAITING:
1958 /* Caller will release the CCB */
1959 wakeup(&done_ccb->ccb_h.cbfcnp);
1963 xpt_release_ccb(done_ccb);
1967 * Mount the tape (make sure it's ready for I/O).
1970 samount(struct cam_periph *periph, int oflags, struct cdev *dev)
1972 struct sa_softc *softc;
1977 * oflags can be checked for 'kind' of open (read-only check) - later
1978 * dev can be checked for a control-mode or compression open - later
1980 UNUSED_PARAMETER(oflags);
1981 UNUSED_PARAMETER(dev);
1984 softc = (struct sa_softc *)periph->softc;
1987 * This should determine if something has happend since the last
1988 * open/mount that would invalidate the mount. We do *not* want
1989 * to retry this command- we just want the status. But we only
1990 * do this if we're mounted already- if we're not mounted,
1991 * we don't care about the unit read state and can instead use
1992 * this opportunity to attempt to reserve the tape unit.
1995 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1996 ccb = cam_periph_getccb(periph, 1);
1997 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1998 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1999 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2000 softc->device_stats);
2001 if (error == ENXIO) {
2002 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
2003 scsi_test_unit_ready(&ccb->csio, 0, sadone,
2004 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
2005 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2006 softc->device_stats);
2009 * We don't need to freeze the tape because we
2010 * will now attempt to rewind/load it.
2012 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
2013 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2014 xpt_print(periph->path,
2015 "error %d on TUR in samount\n", error);
2019 error = sareservereleaseunit(periph, TRUE);
2023 ccb = cam_periph_getccb(periph, 1);
2024 scsi_test_unit_ready(&ccb->csio, 0, sadone,
2025 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
2026 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2027 softc->device_stats);
2030 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
2031 struct scsi_read_block_limits_data *rblim = NULL;
2032 int comp_enabled, comp_supported;
2033 u_int8_t write_protect, guessing = 0;
2036 * Clear out old state.
2038 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
2039 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
2040 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
2041 softc->filemarks = 0;
2044 * *Very* first off, make sure we're loaded to BOT.
2046 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2047 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
2048 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2049 softc->device_stats);
2052 * In case this doesn't work, do a REWIND instead
2055 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
2056 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
2057 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2058 softc->device_stats);
2061 xpt_release_ccb(ccb);
2066 * Do a dummy test read to force access to the
2067 * media so that the drive will really know what's
2068 * there. We actually don't really care what the
2069 * blocksize on tape is and don't expect to really
2070 * read a full record.
2072 rblim = (struct scsi_read_block_limits_data *)
2073 malloc(8192, M_SCSISA, M_NOWAIT);
2074 if (rblim == NULL) {
2075 xpt_print(periph->path, "no memory for test read\n");
2076 xpt_release_ccb(ccb);
2081 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
2082 scsi_sa_read_write(&ccb->csio, 0, sadone,
2083 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
2084 (void *) rblim, 8192, SSD_FULL_SIZE,
2086 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2087 softc->device_stats);
2088 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
2089 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
2090 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
2091 SF_NO_PRINT | SF_RETRY_UA,
2092 softc->device_stats);
2094 xpt_print(periph->path,
2095 "unable to rewind after test read\n");
2096 xpt_release_ccb(ccb);
2102 * Next off, determine block limits.
2104 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2105 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2107 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
2108 SF_NO_PRINT | SF_RETRY_UA, softc->device_stats);
2110 xpt_release_ccb(ccb);
2114 * If it's less than SCSI-2, READ BLOCK LIMITS is not
2115 * a MANDATORY command. Anyway- it doesn't matter-
2116 * we can proceed anyway.
2118 softc->blk_gran = 0;
2119 softc->max_blk = ~0;
2122 if (softc->scsi_rev >= SCSI_REV_SPC) {
2123 softc->blk_gran = RBL_GRAN(rblim);
2125 softc->blk_gran = 0;
2128 * We take max_blk == min_blk to mean a default to
2129 * fixed mode- but note that whatever we get out of
2130 * sagetparams below will actually determine whether
2131 * we are actually *in* fixed mode.
2133 softc->max_blk = scsi_3btoul(rblim->maximum);
2134 softc->min_blk = scsi_2btoul(rblim->minimum);
2139 * Next, perform a mode sense to determine
2140 * current density, blocksize, compression etc.
2142 error = sagetparams(periph, SA_PARAM_ALL,
2143 &softc->media_blksize,
2144 &softc->media_density,
2145 &softc->media_numblks,
2146 &softc->buffer_mode, &write_protect,
2147 &softc->speed, &comp_supported,
2148 &comp_enabled, &softc->comp_algorithm,
2153 * We could work a little harder here. We could
2154 * adjust our attempts to get information. It
2155 * might be an ancient tape drive. If someone
2156 * nudges us, we'll do that.
2162 * If no quirk has determined that this is a device that is
2163 * preferred to be in fixed or variable mode, now is the time
2166 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2169 * This could be expensive to find out. Luckily we
2170 * only need to do this once. If we start out in
2171 * 'default' mode, try and set ourselves to one
2172 * of the densities that would determine a wad
2173 * of other stuff. Go from highest to lowest.
2175 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2177 static u_int8_t ctry[] = {
2178 SCSI_DENSITY_HALFINCH_PE,
2179 SCSI_DENSITY_HALFINCH_6250C,
2180 SCSI_DENSITY_HALFINCH_6250,
2181 SCSI_DENSITY_HALFINCH_1600,
2182 SCSI_DENSITY_HALFINCH_800,
2183 SCSI_DENSITY_QIC_4GB,
2184 SCSI_DENSITY_QIC_2GB,
2185 SCSI_DENSITY_QIC_525_320,
2186 SCSI_DENSITY_QIC_150,
2187 SCSI_DENSITY_QIC_120,
2188 SCSI_DENSITY_QIC_24,
2189 SCSI_DENSITY_QIC_11_9TRK,
2190 SCSI_DENSITY_QIC_11_4TRK,
2191 SCSI_DENSITY_QIC_1320,
2192 SCSI_DENSITY_QIC_3080,
2195 for (i = 0; ctry[i]; i++) {
2196 error = sasetparams(periph,
2197 SA_PARAM_DENSITY, 0, ctry[i],
2200 softc->media_density = ctry[i];
2205 switch (softc->media_density) {
2206 case SCSI_DENSITY_QIC_11_4TRK:
2207 case SCSI_DENSITY_QIC_11_9TRK:
2208 case SCSI_DENSITY_QIC_24:
2209 case SCSI_DENSITY_QIC_120:
2210 case SCSI_DENSITY_QIC_150:
2211 case SCSI_DENSITY_QIC_525_320:
2212 case SCSI_DENSITY_QIC_1320:
2213 case SCSI_DENSITY_QIC_3080:
2214 softc->quirks &= ~SA_QUIRK_2FM;
2215 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2216 softc->last_media_blksize = 512;
2218 case SCSI_DENSITY_QIC_4GB:
2219 case SCSI_DENSITY_QIC_2GB:
2220 softc->quirks &= ~SA_QUIRK_2FM;
2221 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2222 softc->last_media_blksize = 1024;
2225 softc->last_media_blksize =
2226 softc->media_blksize;
2227 softc->quirks |= SA_QUIRK_VARIABLE;
2233 * If no quirk has determined that this is a device that needs
2234 * to have 2 Filemarks at EOD, now is the time to find out.
2237 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2238 switch (softc->media_density) {
2239 case SCSI_DENSITY_HALFINCH_800:
2240 case SCSI_DENSITY_HALFINCH_1600:
2241 case SCSI_DENSITY_HALFINCH_6250:
2242 case SCSI_DENSITY_HALFINCH_6250C:
2243 case SCSI_DENSITY_HALFINCH_PE:
2244 softc->quirks &= ~SA_QUIRK_1FM;
2245 softc->quirks |= SA_QUIRK_2FM;
2253 * Now validate that some info we got makes sense.
2255 if ((softc->max_blk < softc->media_blksize) ||
2256 (softc->min_blk > softc->media_blksize &&
2257 softc->media_blksize)) {
2258 xpt_print(periph->path,
2259 "BLOCK LIMITS (%d..%d) could not match current "
2260 "block settings (%d)- adjusting\n", softc->min_blk,
2261 softc->max_blk, softc->media_blksize);
2262 softc->max_blk = softc->min_blk =
2263 softc->media_blksize;
2267 * Now put ourselves into the right frame of mind based
2272 * If we want to be in FIXED mode and our current blocksize
2273 * is not equal to our last blocksize (if nonzero), try and
2274 * set ourselves to this last blocksize (as the 'preferred'
2275 * block size). The initial quirkmatch at registry sets the
2276 * initial 'last' blocksize. If, for whatever reason, this
2277 * 'last' blocksize is zero, set the blocksize to 512,
2278 * or min_blk if that's larger.
2280 if ((softc->quirks & SA_QUIRK_FIXED) &&
2281 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2282 (softc->media_blksize != softc->last_media_blksize)) {
2283 softc->media_blksize = softc->last_media_blksize;
2284 if (softc->media_blksize == 0) {
2285 softc->media_blksize = 512;
2286 if (softc->media_blksize < softc->min_blk) {
2287 softc->media_blksize = softc->min_blk;
2290 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2291 softc->media_blksize, 0, 0, SF_NO_PRINT);
2293 xpt_print(periph->path,
2294 "unable to set fixed blocksize to %d\n",
2295 softc->media_blksize);
2300 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2301 (softc->media_blksize != 0)) {
2302 softc->last_media_blksize = softc->media_blksize;
2303 softc->media_blksize = 0;
2304 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2305 0, 0, 0, SF_NO_PRINT);
2308 * If this fails and we were guessing, just
2309 * assume that we got it wrong and go try
2310 * fixed block mode. Don't even check against
2311 * density code at this point.
2314 softc->quirks &= ~SA_QUIRK_VARIABLE;
2315 softc->quirks |= SA_QUIRK_FIXED;
2316 if (softc->last_media_blksize == 0)
2317 softc->last_media_blksize = 512;
2320 xpt_print(periph->path,
2321 "unable to set variable blocksize\n");
2327 * Now that we have the current block size,
2328 * set up some parameters for sastart's usage.
2330 if (softc->media_blksize) {
2331 softc->flags |= SA_FLAG_FIXED;
2332 if (powerof2(softc->media_blksize)) {
2334 ffs(softc->media_blksize) - 1;
2335 softc->blk_mask = softc->media_blksize - 1;
2337 softc->blk_mask = ~0;
2338 softc->blk_shift = 0;
2342 * The SCSI-3 spec allows 0 to mean "unspecified".
2343 * The SCSI-1 spec allows 0 to mean 'infinite'.
2345 * Either works here.
2347 if (softc->max_blk == 0) {
2348 softc->max_blk = ~0;
2350 softc->blk_shift = 0;
2351 if (softc->blk_gran != 0) {
2352 softc->blk_mask = softc->blk_gran - 1;
2354 softc->blk_mask = 0;
2359 softc->flags |= SA_FLAG_TAPE_WP;
2361 if (comp_supported) {
2362 if (softc->saved_comp_algorithm == 0)
2363 softc->saved_comp_algorithm =
2364 softc->comp_algorithm;
2365 softc->flags |= SA_FLAG_COMP_SUPP;
2367 softc->flags |= SA_FLAG_COMP_ENABLED;
2369 softc->flags |= SA_FLAG_COMP_UNSUPP;
2371 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2372 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2373 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2376 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2378 xpt_print(periph->path,
2379 "unable to set buffered mode\n");
2381 error = 0; /* not an error */
2386 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2390 free(rblim, M_SCSISA);
2393 softc->dsreg = MTIO_DSREG_NIL;
2395 softc->fileno = softc->blkno = 0;
2396 softc->dsreg = MTIO_DSREG_REST;
2398 #ifdef SA_1FM_AT_EOD
2399 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2400 softc->quirks |= SA_QUIRK_1FM;
2402 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2403 softc->quirks |= SA_QUIRK_2FM;
2406 xpt_release_ccb(ccb);
2409 * If we return an error, we're not mounted any more,
2410 * so release any device reservation.
2413 (void) sareservereleaseunit(periph, FALSE);
2416 * Clear I/O residual.
2418 softc->last_io_resid = 0;
2419 softc->last_ctl_resid = 0;
2425 * How many filemarks do we need to write if we were to terminate the
2426 * tape session right now? Note that this can be a negative number
2430 samarkswanted(struct cam_periph *periph)
2433 struct sa_softc *softc;
2435 softc = (struct sa_softc *)periph->softc;
2437 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2439 if (softc->quirks & SA_QUIRK_2FM)
2442 markswanted -= softc->filemarks;
2443 return (markswanted);
2447 sacheckeod(struct cam_periph *periph)
2452 markswanted = samarkswanted(periph);
2454 if (markswanted > 0) {
2455 error = sawritefilemarks(periph, markswanted, FALSE);
2463 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2465 static const char *toobig =
2466 "%d-byte tape record bigger than supplied buffer\n";
2467 struct cam_periph *periph;
2468 struct sa_softc *softc;
2469 struct ccb_scsiio *csio;
2470 struct scsi_sense_data *sense;
2474 int error_code, sense_key, asc, ascq, error, aqvalid, stream_valid;
2476 uint8_t stream_bits;
2478 periph = xpt_path_periph(ccb->ccb_h.path);
2479 softc = (struct sa_softc *)periph->softc;
2481 sense = &csio->sense_data;
2482 sense_len = csio->sense_len - csio->sense_resid;
2483 scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
2484 &asc, &ascq, /*show_errors*/ 1);
2485 if (asc != -1 && ascq != -1)
2489 if (scsi_get_stream_info(sense, sense_len, NULL, &stream_bits) == 0)
2495 status = csio->ccb_h.status & CAM_STATUS_MASK;
2498 * Calculate/latch up, any residuals... We do this in a funny 2-step
2499 * so we can print stuff here if we have CAM_DEBUG enabled for this
2502 if (status == CAM_SCSI_STATUS_ERROR) {
2503 if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &resid,
2505 if ((softc->flags & SA_FLAG_FIXED) != 0)
2506 resid *= softc->media_blksize;
2508 resid = csio->dxfer_len;
2510 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2511 if (softc->media_blksize)
2512 info /= softc->media_blksize;
2515 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2516 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2517 sizeof (struct scsi_sense_data));
2518 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2519 (int) csio->cdb_len);
2520 softc->last_io_resid = resid;
2521 softc->last_resid_was_io = 1;
2523 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2524 sizeof (struct scsi_sense_data));
2525 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2526 (int) csio->cdb_len);
2527 softc->last_ctl_resid = resid;
2528 softc->last_resid_was_io = 0;
2530 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2531 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %jd "
2532 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2533 sense_key, asc, ascq, status,
2534 (stream_valid) ? stream_bits : 0, (intmax_t)resid,
2537 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2538 ("Cam Status 0x%x\n", status));
2544 case CAM_SCSI_STATUS_ERROR:
2546 * If a read/write command, we handle it here.
2548 if (CCB_Type(csio) != SA_CCB_WAITING) {
2552 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2553 * on a non read/write command, we assume it's not an error
2554 * and propagate the residule and return.
2556 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2557 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2558 csio->resid = resid;
2563 * Otherwise, we let the common code handle this.
2565 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2569 * We cannot depend upon CAM honoring retry counts for these.
2571 case CAM_SCSI_BUS_RESET:
2573 if (ccb->ccb_h.retry_count <= 0) {
2578 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2582 * Handle filemark, end of tape, mismatched record sizes....
2583 * From this point out, we're only handling read/write cases.
2584 * Handle writes && reads differently.
2587 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2588 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2589 csio->resid = resid;
2591 } else if ((stream_valid != 0) && (stream_bits & SSD_EOM)) {
2592 softc->flags |= SA_FLAG_EOM_PENDING;
2594 * Grotesque as it seems, the few times
2595 * I've actually seen a non-zero resid,
2596 * the tape drive actually lied and had
2597 * written all the data!.
2602 csio->resid = resid;
2603 if (sense_key == SSD_KEY_BLANK_CHECK) {
2604 if (softc->quirks & SA_QUIRK_1FM) {
2606 softc->flags |= SA_FLAG_EOM_PENDING;
2610 } else if ((stream_valid != 0) && (stream_bits & SSD_FILEMARK)){
2611 if (softc->flags & SA_FLAG_FIXED) {
2613 softc->flags |= SA_FLAG_EOF_PENDING;
2616 * Unconditionally, if we detected a filemark on a read,
2617 * mark that we've run moved a file ahead.
2619 if (softc->fileno != (daddr_t) -1) {
2622 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2628 * Incorrect Length usually applies to read, but can apply to writes.
2630 if (error == 0 && (stream_valid != 0) && (stream_bits & SSD_ILI)) {
2632 xpt_print(csio->ccb_h.path, toobig,
2633 csio->dxfer_len - info);
2634 csio->resid = csio->dxfer_len;
2637 csio->resid = resid;
2638 if (softc->flags & SA_FLAG_FIXED) {
2639 softc->flags |= SA_FLAG_EIO_PENDING;
2642 * Bump the block number if we hadn't seen a filemark.
2643 * Do this independent of errors (we've moved anyway).
2645 if ((stream_valid == 0) ||
2646 (stream_bits & SSD_FILEMARK) == 0) {
2647 if (softc->blkno != (daddr_t) -1) {
2649 csio->ccb_h.ccb_pflags |=
2650 SA_POSITION_UPDATED;
2658 * Unfreeze the queue if frozen as we're not returning anything
2659 * to our waiters that would indicate an I/O error has occurred
2669 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2670 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2671 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2672 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2677 struct scsi_mode_header_6 *mode_hdr;
2678 struct scsi_mode_blk_desc *mode_blk;
2679 int mode_buffer_len;
2680 struct sa_softc *softc;
2685 softc = (struct sa_softc *)periph->softc;
2686 ccb = cam_periph_getccb(periph, 1);
2687 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2688 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2690 cpage = SA_DATA_COMPRESSION_PAGE;
2693 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2695 if (params_to_get & SA_PARAM_COMPRESSION) {
2696 if (softc->quirks & SA_QUIRK_NOCOMP) {
2697 *comp_supported = FALSE;
2698 params_to_get &= ~SA_PARAM_COMPRESSION;
2700 mode_buffer_len += sizeof (sa_comp_t);
2703 /* XXX Fix M_NOWAIT */
2704 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
2705 if (mode_buffer == NULL) {
2706 xpt_release_ccb(ccb);
2709 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2710 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2712 /* it is safe to retry this */
2713 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2714 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2715 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2716 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2718 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2719 softc->device_stats);
2721 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2723 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2725 * Hmm. Let's see if we can try another page...
2726 * If we've already done that, give up on compression
2727 * for this device and remember this for the future
2728 * and attempt the request without asking for compression
2731 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2732 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2735 softc->quirks |= SA_QUIRK_NOCOMP;
2736 free(mode_buffer, M_SCSISA);
2738 } else if (status == CAM_SCSI_STATUS_ERROR) {
2739 /* Tell the user about the fatal error. */
2740 scsi_sense_print(&ccb->csio);
2741 goto sagetparamsexit;
2745 * If the user only wants the compression information, and
2746 * the device doesn't send back the block descriptor, it's
2747 * no big deal. If the user wants more than just
2748 * compression, though, and the device doesn't pass back the
2749 * block descriptor, we need to send another mode sense to
2750 * get the block descriptor.
2752 if ((mode_hdr->blk_desc_len == 0) &&
2753 (params_to_get & SA_PARAM_COMPRESSION) &&
2754 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2757 * Decrease the mode buffer length by the size of
2758 * the compression page, to make sure the data
2759 * there doesn't get overwritten.
2761 mode_buffer_len -= sizeof (sa_comp_t);
2764 * Now move the compression page that we presumably
2765 * got back down the memory chunk a little bit so
2766 * it doesn't get spammed.
2768 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2769 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2772 * Now, we issue another mode sense and just ask
2773 * for the block descriptor, etc.
2776 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2777 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2778 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2781 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2782 softc->device_stats);
2785 goto sagetparamsexit;
2788 if (params_to_get & SA_PARAM_BLOCKSIZE)
2789 *blocksize = scsi_3btoul(mode_blk->blklen);
2791 if (params_to_get & SA_PARAM_NUMBLOCKS)
2792 *numblocks = scsi_3btoul(mode_blk->nblocks);
2794 if (params_to_get & SA_PARAM_BUFF_MODE)
2795 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2797 if (params_to_get & SA_PARAM_DENSITY)
2798 *density = mode_blk->density;
2800 if (params_to_get & SA_PARAM_WP)
2801 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2803 if (params_to_get & SA_PARAM_SPEED)
2804 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2806 if (params_to_get & SA_PARAM_COMPRESSION) {
2807 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2808 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2809 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2811 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2813 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2814 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2816 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2818 * We don't really know whether this device supports
2819 * Data Compression if the algorithm field is
2820 * zero. Just say we do.
2822 *comp_supported = TRUE;
2824 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2825 *comp_algorithm = cp->sel_comp_alg;
2828 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2831 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2833 char *xyz = mode_buffer;
2834 xpt_print_path(periph->path);
2835 printf("Mode Sense Data=");
2836 for (idx = 0; idx < mode_buffer_len; idx++)
2837 printf(" 0x%02x", xyz[idx] & 0xff);
2843 xpt_release_ccb(ccb);
2844 free(mode_buffer, M_SCSISA);
2849 * The purpose of this function is to set one of four different parameters
2853 * - compression / compression algorithm
2856 * The assumption is that this will be called from saioctl(), and therefore
2857 * from a process context. Thus the waiting malloc calls below. If that
2858 * assumption ever changes, the malloc calls should be changed to be
2861 * Any or all of the four parameters may be set when this function is
2862 * called. It should handle setting more than one parameter at once.
2865 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2866 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2867 u_int32_t sense_flags)
2869 struct sa_softc *softc;
2870 u_int32_t current_blocksize;
2871 u_int32_t current_calg;
2872 u_int8_t current_density;
2873 u_int8_t current_speed;
2874 int comp_enabled, comp_supported;
2876 int mode_buffer_len;
2877 struct scsi_mode_header_6 *mode_hdr;
2878 struct scsi_mode_blk_desc *mode_blk;
2879 sa_comp_t *ccomp, *cpage;
2881 union ccb *ccb = NULL;
2884 softc = (struct sa_softc *)periph->softc;
2886 ccomp = malloc(sizeof (sa_comp_t), M_SCSISA, M_NOWAIT);
2891 * Since it doesn't make sense to set the number of blocks, or
2892 * write protection, we won't try to get the current value. We
2893 * always want to get the blocksize, so we can set it back to the
2896 error = sagetparams(periph,
2897 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2898 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2899 ¤t_speed, &comp_supported, &comp_enabled,
2900 ¤t_calg, ccomp);
2903 free(ccomp, M_SCSISA);
2907 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2908 if (params_to_set & SA_PARAM_COMPRESSION)
2909 mode_buffer_len += sizeof (sa_comp_t);
2911 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
2912 if (mode_buffer == NULL) {
2913 free(ccomp, M_SCSISA);
2917 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2918 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2920 ccb = cam_periph_getccb(periph, 1);
2924 if (params_to_set & SA_PARAM_COMPRESSION) {
2926 cpage = (sa_comp_t *)&mode_blk[1];
2928 cpage = (sa_comp_t *)&mode_hdr[1];
2930 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2931 cpage->hdr.pagecode &= ~0x80;
2936 * If the caller wants us to set the blocksize, use the one they
2937 * pass in. Otherwise, use the blocksize we got back from the
2938 * mode select above.
2941 if (params_to_set & SA_PARAM_BLOCKSIZE)
2942 scsi_ulto3b(blocksize, mode_blk->blklen);
2944 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2947 * Set density if requested, else preserve old density.
2948 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2949 * devices, else density we've latched up in our softc.
2951 if (params_to_set & SA_PARAM_DENSITY) {
2952 mode_blk->density = density;
2953 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2954 mode_blk->density = SCSI_SAME_DENSITY;
2956 mode_blk->density = softc->media_density;
2961 * For mode selects, these two fields must be zero.
2963 mode_hdr->data_length = 0;
2964 mode_hdr->medium_type = 0;
2966 /* set the speed to the current value */
2967 mode_hdr->dev_spec = current_speed;
2969 /* if set, set single-initiator buffering mode */
2970 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
2971 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2975 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2977 mode_hdr->blk_desc_len = 0;
2980 * First, if the user wants us to set the compression algorithm or
2981 * just turn compression on, check to make sure that this drive
2982 * supports compression.
2984 if (params_to_set & SA_PARAM_COMPRESSION) {
2986 * If the compression algorithm is 0, disable compression.
2987 * If the compression algorithm is non-zero, enable
2988 * compression and set the compression type to the
2989 * specified compression algorithm, unless the algorithm is
2990 * MT_COMP_ENABLE. In that case, we look at the
2991 * compression algorithm that is currently set and if it is
2992 * non-zero, we leave it as-is. If it is zero, and we have
2993 * saved a compression algorithm from a time when
2994 * compression was enabled before, set the compression to
2997 switch (ccomp->hdr.pagecode & ~0x80) {
2998 case SA_DEVICE_CONFIGURATION_PAGE:
3000 struct scsi_dev_conf_page *dcp = &cpage->dconf;
3002 dcp->sel_comp_alg = SA_COMP_NONE;
3005 if (calg != MT_COMP_ENABLE) {
3006 dcp->sel_comp_alg = calg;
3007 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
3008 softc->saved_comp_algorithm != 0) {
3009 dcp->sel_comp_alg = softc->saved_comp_algorithm;
3013 case SA_DATA_COMPRESSION_PAGE:
3014 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
3015 struct scsi_data_compression_page *dcp = &cpage->dcomp;
3018 * Disable compression, but leave the
3019 * decompression and the capability bit
3022 dcp->dce_and_dcc = SA_DCP_DCC;
3023 dcp->dde_and_red |= SA_DCP_DDE;
3026 /* enable compression && decompression */
3027 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
3028 dcp->dde_and_red |= SA_DCP_DDE;
3030 * If there, use compression algorithm from caller.
3031 * Otherwise, if there's a saved compression algorithm
3032 * and there is no current algorithm, use the saved
3033 * algorithm. Else parrot back what we got and hope
3036 if (calg != MT_COMP_ENABLE) {
3037 scsi_ulto4b(calg, dcp->comp_algorithm);
3038 scsi_ulto4b(calg, dcp->decomp_algorithm);
3039 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
3040 softc->saved_comp_algorithm != 0) {
3041 scsi_ulto4b(softc->saved_comp_algorithm,
3042 dcp->comp_algorithm);
3043 scsi_ulto4b(softc->saved_comp_algorithm,
3044 dcp->decomp_algorithm);
3049 * Compression does not appear to be supported-
3050 * at least via the DATA COMPRESSION page. It
3051 * would be too much to ask us to believe that
3052 * the page itself is supported, but incorrectly
3053 * reports an ability to manipulate data compression,
3054 * so we'll assume that this device doesn't support
3055 * compression. We can just fall through for that.
3060 * The drive doesn't seem to support compression,
3061 * so turn off the set compression bit.
3063 params_to_set &= ~SA_PARAM_COMPRESSION;
3064 xpt_print(periph->path,
3065 "device does not seem to support compression\n");
3068 * If that was the only thing the user wanted us to set,
3069 * clean up allocated resources and return with
3070 * 'operation not supported'.
3072 if (params_to_set == SA_PARAM_NONE) {
3073 free(mode_buffer, M_SCSISA);
3074 xpt_release_ccb(ccb);
3079 * That wasn't the only thing the user wanted us to set.
3080 * So, decrease the stated mode buffer length by the
3081 * size of the compression mode page.
3083 mode_buffer_len -= sizeof(sa_comp_t);
3087 /* It is safe to retry this operation */
3088 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
3089 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
3090 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3092 error = cam_periph_runccb(ccb, saerror, 0,
3093 sense_flags, softc->device_stats);
3095 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3097 char *xyz = mode_buffer;
3098 xpt_print_path(periph->path);
3099 printf("Err%d, Mode Select Data=", error);
3100 for (idx = 0; idx < mode_buffer_len; idx++)
3101 printf(" 0x%02x", xyz[idx] & 0xff);
3108 * If we can, try without setting density/blocksize.
3111 if ((params_to_set &
3112 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
3117 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
3118 cpage = (sa_comp_t *)&mode_blk[1];
3122 * If we were setting the blocksize, and that failed, we
3123 * want to set it to its original value. If we weren't
3124 * setting the blocksize, we don't want to change it.
3126 scsi_ulto3b(current_blocksize, mode_blk->blklen);
3129 * Set density if requested, else preserve old density.
3130 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
3131 * devices, else density we've latched up in our softc.
3133 if (params_to_set & SA_PARAM_DENSITY) {
3134 mode_blk->density = current_density;
3135 } else if (softc->scsi_rev > SCSI_REV_CCS) {
3136 mode_blk->density = SCSI_SAME_DENSITY;
3138 mode_blk->density = softc->media_density;
3141 if (params_to_set & SA_PARAM_COMPRESSION)
3142 bcopy(ccomp, cpage, sizeof (sa_comp_t));
3145 * The retry count is the only CCB field that might have been
3146 * changed that we care about, so reset it back to 1.
3148 ccb->ccb_h.retry_count = 1;
3149 cam_periph_runccb(ccb, saerror, 0, sense_flags,
3150 softc->device_stats);
3153 xpt_release_ccb(ccb);
3156 free(ccomp, M_SCSISA);
3158 if (params_to_set & SA_PARAM_COMPRESSION) {
3160 softc->flags &= ~SA_FLAG_COMP_ENABLED;
3162 * Even if we get an error setting compression,
3163 * do not say that we don't support it. We could
3164 * have been wrong, or it may be media specific.
3165 * softc->flags &= ~SA_FLAG_COMP_SUPP;
3167 softc->saved_comp_algorithm = softc->comp_algorithm;
3168 softc->comp_algorithm = 0;
3170 softc->flags |= SA_FLAG_COMP_ENABLED;
3171 softc->comp_algorithm = calg;
3175 free(mode_buffer, M_SCSISA);
3180 saprevent(struct cam_periph *periph, int action)
3182 struct sa_softc *softc;
3186 softc = (struct sa_softc *)periph->softc;
3188 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3190 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3194 * We can be quiet about illegal requests.
3196 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3201 ccb = cam_periph_getccb(periph, 1);
3203 /* It is safe to retry this operation */
3204 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3205 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3207 error = cam_periph_runccb(ccb, saerror, 0, sf, softc->device_stats);
3209 if (action == PR_ALLOW)
3210 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3212 softc->flags |= SA_FLAG_TAPE_LOCKED;
3215 xpt_release_ccb(ccb);
3219 sarewind(struct cam_periph *periph)
3222 struct sa_softc *softc;
3225 softc = (struct sa_softc *)periph->softc;
3227 ccb = cam_periph_getccb(periph, 1);
3229 /* It is safe to retry this operation */
3230 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3231 SSD_FULL_SIZE, REWIND_TIMEOUT);
3233 softc->dsreg = MTIO_DSREG_REW;
3234 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3235 softc->dsreg = MTIO_DSREG_REST;
3237 xpt_release_ccb(ccb);
3239 softc->fileno = softc->blkno = (daddr_t) 0;
3241 softc->fileno = softc->blkno = (daddr_t) -1;
3246 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3249 struct sa_softc *softc;
3252 softc = (struct sa_softc *)periph->softc;
3254 ccb = cam_periph_getccb(periph, 1);
3256 /* This cannot be retried */
3258 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3259 SSD_FULL_SIZE, SPACE_TIMEOUT);
3262 * Clear residual because we will be using it.
3264 softc->last_ctl_resid = 0;
3266 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3267 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3268 softc->dsreg = MTIO_DSREG_REST;
3270 xpt_release_ccb(ccb);
3273 * If a spacing operation has failed, we need to invalidate
3276 * If the spacing operation was setmarks or to end of recorded data,
3277 * we no longer know our relative position.
3279 * If the spacing operations was spacing files in reverse, we
3280 * take account of the residual, but still check against less
3281 * than zero- if we've gone negative, we must have hit BOT.
3283 * If the spacing operations was spacing records in reverse and
3284 * we have a residual, we've either hit BOT or hit a filemark.
3285 * In the former case, we know our new record number (0). In
3286 * the latter case, we have absolutely no idea what the real
3287 * record number is- we've stopped between the end of the last
3288 * record in the previous file and the filemark that stopped
3289 * our spacing backwards.
3292 softc->fileno = softc->blkno = (daddr_t) -1;
3293 } else if (code == SS_SETMARKS || code == SS_EOD) {
3294 softc->fileno = softc->blkno = (daddr_t) -1;
3295 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3296 softc->fileno += (count - softc->last_ctl_resid);
3297 if (softc->fileno < 0) /* we must of hit BOT */
3300 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3301 softc->blkno += (count - softc->last_ctl_resid);
3303 if (softc->last_ctl_resid || softc->blkno < 0) {
3304 if (softc->fileno == 0) {
3307 softc->blkno = (daddr_t) -1;
3316 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3319 struct sa_softc *softc;
3322 softc = (struct sa_softc *)periph->softc;
3323 if (softc->open_rdonly)
3326 ccb = cam_periph_getccb(periph, 1);
3328 * Clear residual because we will be using it.
3330 softc->last_ctl_resid = 0;
3332 softc->dsreg = MTIO_DSREG_FMK;
3333 /* this *must* not be retried */
3334 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3335 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3336 softc->dsreg = MTIO_DSREG_REST;
3339 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3341 if (error == 0 && nmarks) {
3342 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3343 nwm = nmarks - softc->last_ctl_resid;
3344 softc->filemarks += nwm;
3347 xpt_release_ccb(ccb);
3350 * Update relative positions (if we're doing that).
3353 softc->fileno = softc->blkno = (daddr_t) -1;
3354 } else if (softc->fileno != (daddr_t) -1) {
3355 softc->fileno += nwm;
3362 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3364 struct scsi_tape_position_data loc;
3366 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3370 * We try and flush any buffered writes here if we were writing
3371 * and we're trying to get hardware block position. It eats
3372 * up performance substantially, but I'm wary of drive firmware.
3374 * I think that *logical* block position is probably okay-
3375 * but hardware block position might have to wait for data
3376 * to hit media to be valid. Caveat Emptor.
3379 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3380 error = sawritefilemarks(periph, 0, 0);
3381 if (error && error != EACCES)
3385 ccb = cam_periph_getccb(periph, 1);
3386 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3387 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3388 softc->dsreg = MTIO_DSREG_RBSY;
3389 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3390 softc->dsreg = MTIO_DSREG_REST;
3393 if (loc.flags & SA_RPOS_UNCERTAIN) {
3394 error = EINVAL; /* nothing is certain */
3396 *blkptr = scsi_4btoul(loc.firstblk);
3400 xpt_release_ccb(ccb);
3405 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3408 struct sa_softc *softc;
3412 * We used to try and flush any buffered writes here.
3413 * Now we push this onto user applications to either
3414 * flush the pending writes themselves (via a zero count
3415 * WRITE FILEMARKS command) or they can trust their tape
3416 * drive to do this correctly for them.
3419 softc = (struct sa_softc *)periph->softc;
3420 ccb = cam_periph_getccb(periph, 1);
3423 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3424 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3427 softc->dsreg = MTIO_DSREG_POS;
3428 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3429 softc->dsreg = MTIO_DSREG_REST;
3430 xpt_release_ccb(ccb);
3432 * Note relative file && block number position as now unknown.
3434 softc->fileno = softc->blkno = (daddr_t) -1;
3439 saretension(struct cam_periph *periph)
3442 struct sa_softc *softc;
3445 softc = (struct sa_softc *)periph->softc;
3447 ccb = cam_periph_getccb(periph, 1);
3449 /* It is safe to retry this operation */
3450 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3451 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3453 softc->dsreg = MTIO_DSREG_TEN;
3454 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3455 softc->dsreg = MTIO_DSREG_REST;
3457 xpt_release_ccb(ccb);
3459 softc->fileno = softc->blkno = (daddr_t) 0;
3461 softc->fileno = softc->blkno = (daddr_t) -1;
3466 sareservereleaseunit(struct cam_periph *periph, int reserve)
3469 struct sa_softc *softc;
3472 softc = (struct sa_softc *)periph->softc;
3473 ccb = cam_periph_getccb(periph, 1);
3475 /* It is safe to retry this operation */
3476 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3477 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3478 softc->dsreg = MTIO_DSREG_RBSY;
3479 error = cam_periph_runccb(ccb, saerror, 0,
3480 SF_RETRY_UA | SF_NO_PRINT, softc->device_stats);
3481 softc->dsreg = MTIO_DSREG_REST;
3482 xpt_release_ccb(ccb);
3485 * If the error was Illegal Request, then the device doesn't support
3486 * RESERVE/RELEASE. This is not an error.
3488 if (error == EINVAL) {
3496 saloadunload(struct cam_periph *periph, int load)
3499 struct sa_softc *softc;
3502 softc = (struct sa_softc *)periph->softc;
3504 ccb = cam_periph_getccb(periph, 1);
3506 /* It is safe to retry this operation */
3507 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3508 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3510 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3511 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3512 softc->dsreg = MTIO_DSREG_REST;
3513 xpt_release_ccb(ccb);
3515 if (error || load == 0)
3516 softc->fileno = softc->blkno = (daddr_t) -1;
3517 else if (error == 0)
3518 softc->fileno = softc->blkno = (daddr_t) 0;
3523 saerase(struct cam_periph *periph, int longerase)
3527 struct sa_softc *softc;
3530 softc = (struct sa_softc *)periph->softc;
3531 if (softc->open_rdonly)
3534 ccb = cam_periph_getccb(periph, 1);
3536 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3537 SSD_FULL_SIZE, ERASE_TIMEOUT);
3539 softc->dsreg = MTIO_DSREG_ZER;
3540 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3541 softc->dsreg = MTIO_DSREG_REST;
3543 xpt_release_ccb(ccb);
3547 #endif /* _KERNEL */
3550 * Read tape block limits command.
3553 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3554 void (*cbfcnp)(struct cam_periph *, union ccb *),
3555 u_int8_t tag_action,
3556 struct scsi_read_block_limits_data *rlimit_buf,
3557 u_int8_t sense_len, u_int32_t timeout)
3559 struct scsi_read_block_limits *scsi_cmd;
3561 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3562 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3563 sizeof(*scsi_cmd), timeout);
3565 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3566 bzero(scsi_cmd, sizeof(*scsi_cmd));
3567 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3571 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3572 void (*cbfcnp)(struct cam_periph *, union ccb *),
3573 u_int8_t tag_action, int readop, int sli,
3574 int fixed, u_int32_t length, u_int8_t *data_ptr,
3575 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3577 struct scsi_sa_rw *scsi_cmd;
3580 read = (readop & SCSI_RW_DIRMASK) == SCSI_RW_READ;
3582 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3583 scsi_cmd->opcode = read ? SA_READ : SA_WRITE;
3584 scsi_cmd->sli_fixed = 0;
3586 scsi_cmd->sli_fixed |= SAR_SLI;
3588 scsi_cmd->sli_fixed |= SARW_FIXED;
3589 scsi_ulto3b(length, scsi_cmd->length);
3590 scsi_cmd->control = 0;
3592 cam_fill_csio(csio, retries, cbfcnp, (read ? CAM_DIR_IN : CAM_DIR_OUT) |
3593 ((readop & SCSI_RW_BIO) != 0 ? CAM_DATA_BIO : 0),
3594 tag_action, data_ptr, dxfer_len, sense_len,
3595 sizeof(*scsi_cmd), timeout);
3599 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3600 void (*cbfcnp)(struct cam_periph *, union ccb *),
3601 u_int8_t tag_action, int immediate, int eot,
3602 int reten, int load, u_int8_t sense_len,
3605 struct scsi_load_unload *scsi_cmd;
3607 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3608 bzero(scsi_cmd, sizeof(*scsi_cmd));
3609 scsi_cmd->opcode = LOAD_UNLOAD;
3611 scsi_cmd->immediate = SLU_IMMED;
3613 scsi_cmd->eot_reten_load |= SLU_EOT;
3615 scsi_cmd->eot_reten_load |= SLU_RETEN;
3617 scsi_cmd->eot_reten_load |= SLU_LOAD;
3619 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3620 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3624 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3625 void (*cbfcnp)(struct cam_periph *, union ccb *),
3626 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3629 struct scsi_rewind *scsi_cmd;
3631 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3632 bzero(scsi_cmd, sizeof(*scsi_cmd));
3633 scsi_cmd->opcode = REWIND;
3635 scsi_cmd->immediate = SREW_IMMED;
3637 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3638 0, sense_len, sizeof(*scsi_cmd), timeout);
3642 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3643 void (*cbfcnp)(struct cam_periph *, union ccb *),
3644 u_int8_t tag_action, scsi_space_code code,
3645 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3647 struct scsi_space *scsi_cmd;
3649 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3650 scsi_cmd->opcode = SPACE;
3651 scsi_cmd->code = code;
3652 scsi_ulto3b(count, scsi_cmd->count);
3653 scsi_cmd->control = 0;
3655 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3656 0, sense_len, sizeof(*scsi_cmd), timeout);
3660 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3661 void (*cbfcnp)(struct cam_periph *, union ccb *),
3662 u_int8_t tag_action, int immediate, int setmark,
3663 u_int32_t num_marks, u_int8_t sense_len,
3666 struct scsi_write_filemarks *scsi_cmd;
3668 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3669 bzero(scsi_cmd, sizeof(*scsi_cmd));
3670 scsi_cmd->opcode = WRITE_FILEMARKS;
3672 scsi_cmd->byte2 |= SWFMRK_IMMED;
3674 scsi_cmd->byte2 |= SWFMRK_WSMK;
3676 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3678 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3679 0, sense_len, sizeof(*scsi_cmd), timeout);
3683 * The reserve and release unit commands differ only by their opcodes.
3686 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3687 void (*cbfcnp)(struct cam_periph *, union ccb *),
3688 u_int8_t tag_action, int third_party,
3689 int third_party_id, u_int8_t sense_len,
3690 u_int32_t timeout, int reserve)
3692 struct scsi_reserve_release_unit *scsi_cmd;
3694 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3695 bzero(scsi_cmd, sizeof(*scsi_cmd));
3698 scsi_cmd->opcode = RESERVE_UNIT;
3700 scsi_cmd->opcode = RELEASE_UNIT;
3703 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3704 scsi_cmd->lun_thirdparty |=
3705 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3708 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3709 0, sense_len, sizeof(*scsi_cmd), timeout);
3713 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3714 void (*cbfcnp)(struct cam_periph *, union ccb *),
3715 u_int8_t tag_action, int immediate, int long_erase,
3716 u_int8_t sense_len, u_int32_t timeout)
3718 struct scsi_erase *scsi_cmd;
3720 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3721 bzero(scsi_cmd, sizeof(*scsi_cmd));
3723 scsi_cmd->opcode = ERASE;
3726 scsi_cmd->lun_imm_long |= SE_IMMED;
3729 scsi_cmd->lun_imm_long |= SE_LONG;
3731 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3732 0, sense_len, sizeof(*scsi_cmd), timeout);
3736 * Read Tape Position command.
3739 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3740 void (*cbfcnp)(struct cam_periph *, union ccb *),
3741 u_int8_t tag_action, int hardsoft,
3742 struct scsi_tape_position_data *sbp,
3743 u_int8_t sense_len, u_int32_t timeout)
3745 struct scsi_tape_read_position *scmd;
3747 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3748 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3749 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3750 bzero(scmd, sizeof(*scmd));
3751 scmd->opcode = READ_POSITION;
3752 scmd->byte1 = hardsoft;
3756 * Set Tape Position command.
3759 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3760 void (*cbfcnp)(struct cam_periph *, union ccb *),
3761 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3762 u_int8_t sense_len, u_int32_t timeout)
3764 struct scsi_tape_locate *scmd;
3766 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3767 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3768 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3769 bzero(scmd, sizeof(*scmd));
3770 scmd->opcode = LOCATE;
3772 scmd->byte1 |= SA_SPOS_BT;
3773 scsi_ulto4b(blkno, scmd->blkaddr);