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>
47 #include <sys/fcntl.h>
48 #include <sys/devicestat.h>
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_periph.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_debug.h>
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/scsi/scsi_sa.h>
70 #define SA_IO_TIMEOUT 4
72 #ifndef SA_SPACE_TIMEOUT
73 #define SA_SPACE_TIMEOUT 1 * 60
75 #ifndef SA_REWIND_TIMEOUT
76 #define SA_REWIND_TIMEOUT 2 * 60
78 #ifndef SA_ERASE_TIMEOUT
79 #define SA_ERASE_TIMEOUT 4 * 60
82 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
84 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
85 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
86 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
87 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
90 * Additional options that can be set for config: SA_1FM_AT_EOT
93 #ifndef UNUSED_PARAMETER
94 #define UNUSED_PARAMETER(x) x = x
98 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
99 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
105 static MALLOC_DEFINE(M_SCSISA, "SCSI sa", "SCSI sequential access buffers");
108 SA_STATE_NORMAL, SA_STATE_ABNORMAL
111 #define ccb_pflags ppriv_field0
112 #define ccb_bp ppriv_ptr1
114 #define SA_CCB_BUFFER_IO 0x0
115 #define SA_CCB_WAITING 0x1
116 #define SA_CCB_TYPEMASK 0x1
117 #define SA_POSITION_UPDATED 0x2
119 #define Set_CCB_Type(x, type) \
120 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
121 x->ccb_h.ccb_pflags |= type
123 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
128 SA_FLAG_OPEN = 0x0001,
129 SA_FLAG_FIXED = 0x0002,
130 SA_FLAG_TAPE_LOCKED = 0x0004,
131 SA_FLAG_TAPE_MOUNTED = 0x0008,
132 SA_FLAG_TAPE_WP = 0x0010,
133 SA_FLAG_TAPE_WRITTEN = 0x0020,
134 SA_FLAG_EOM_PENDING = 0x0040,
135 SA_FLAG_EIO_PENDING = 0x0080,
136 SA_FLAG_EOF_PENDING = 0x0100,
137 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
138 SA_FLAG_EOF_PENDING),
139 SA_FLAG_INVALID = 0x0200,
140 SA_FLAG_COMP_ENABLED = 0x0400,
141 SA_FLAG_COMP_SUPP = 0x0800,
142 SA_FLAG_COMP_UNSUPP = 0x1000,
143 SA_FLAG_TAPE_FROZEN = 0x2000
147 SA_MODE_REWIND = 0x00,
148 SA_MODE_NOREWIND = 0x01,
149 SA_MODE_OFFLINE = 0x02
153 SA_PARAM_NONE = 0x00,
154 SA_PARAM_BLOCKSIZE = 0x01,
155 SA_PARAM_DENSITY = 0x02,
156 SA_PARAM_COMPRESSION = 0x04,
157 SA_PARAM_BUFF_MODE = 0x08,
158 SA_PARAM_NUMBLOCKS = 0x10,
160 SA_PARAM_SPEED = 0x40,
165 SA_QUIRK_NONE = 0x00,
166 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
167 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
168 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
169 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
170 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
171 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
172 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
173 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
176 #define SA_QUIRK_BIT_STRING \
187 #define SAMODE(z) (dev2unit(z) & 0x3)
188 #define SADENSITY(z) ((dev2unit(z) >> 2) & 0x3)
189 #define SA_IS_CTRL(z) (dev2unit(z) & (1 << 4))
191 #define SA_NOT_CTLDEV 0
195 #define SA_ATYPE_NR 1
196 #define SA_ATYPE_ER 2
198 #define SAMINOR(ctl, mode, access) \
199 ((ctl << 4) | (mode << 2) | (access & 0x3))
201 #define SA_NUM_MODES 4
203 struct cdev *ctl_dev;
204 struct sa_mode_devs {
208 } mode_devs[SA_NUM_MODES];
216 struct bio_queue_head bio_queue;
218 struct devstat *device_stats;
226 u_int32_t comp_algorithm;
227 u_int32_t saved_comp_algorithm;
228 u_int32_t media_blksize;
229 u_int32_t last_media_blksize;
230 u_int32_t media_numblks;
231 u_int8_t media_density;
234 u_int8_t dsreg; /* mtio mt_dsreg, redux */
238 int last_resid_was_io;
241 * Relative to BOT Location.
250 struct scsi_sense_data _last_io_sense;
251 u_int64_t _last_io_resid;
252 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
253 struct scsi_sense_data _last_ctl_sense;
254 u_int64_t _last_ctl_resid;
255 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
256 #define last_io_sense errinfo._last_io_sense
257 #define last_io_resid errinfo._last_io_resid
258 #define last_io_cdb errinfo._last_io_cdb
259 #define last_ctl_sense errinfo._last_ctl_sense
260 #define last_ctl_resid errinfo._last_ctl_resid
261 #define last_ctl_cdb errinfo._last_ctl_cdb
264 * Misc other flags/state
268 open_rdonly : 1, /* open read-only */
269 open_pending_mount : 1, /* open pending mount */
270 ctrl_mode : 1; /* control device open */
273 struct sa_quirk_entry {
274 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
275 sa_quirks quirks; /* specific quirk type */
276 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
279 static struct sa_quirk_entry sa_quirk_table[] =
282 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
283 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
284 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
287 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
288 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
291 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
292 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
295 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
296 "Python*", "*"}, SA_QUIRK_NODREAD, 0
299 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
300 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
303 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
304 "VIPER 2525 25462", "-011"},
305 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
308 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
309 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
313 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
314 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
318 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
319 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
322 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
323 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
326 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
327 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
330 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
331 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
334 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
335 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
338 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
339 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
341 { /* jreynold@primenet.com */
342 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
343 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
345 { /* mike@sentex.net */
346 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
347 "STT20000*", "*"}, SA_QUIRK_1FM, 0
350 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "SEAGATE",
351 "DAT 06241-XXX", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
354 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
355 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
358 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
359 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
362 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
363 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
366 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
367 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
370 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
371 " SLR*", "*"}, SA_QUIRK_1FM, 0
374 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
375 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
378 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
379 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
383 static d_open_t saopen;
384 static d_close_t saclose;
385 static d_strategy_t sastrategy;
386 static d_ioctl_t saioctl;
387 static periph_init_t sainit;
388 static periph_ctor_t saregister;
389 static periph_oninv_t saoninvalidate;
390 static periph_dtor_t sacleanup;
391 static periph_start_t sastart;
392 static void saasync(void *callback_arg, u_int32_t code,
393 struct cam_path *path, void *arg);
394 static void sadone(struct cam_periph *periph,
395 union ccb *start_ccb);
396 static int saerror(union ccb *ccb, u_int32_t cam_flags,
397 u_int32_t sense_flags);
398 static int samarkswanted(struct cam_periph *);
399 static int sacheckeod(struct cam_periph *periph);
400 static int sagetparams(struct cam_periph *periph,
401 sa_params params_to_get,
402 u_int32_t *blocksize, u_int8_t *density,
403 u_int32_t *numblocks, int *buff_mode,
404 u_int8_t *write_protect, u_int8_t *speed,
405 int *comp_supported, int *comp_enabled,
406 u_int32_t *comp_algorithm,
407 sa_comp_t *comp_page);
408 static int sasetparams(struct cam_periph *periph,
409 sa_params params_to_set,
410 u_int32_t blocksize, u_int8_t density,
411 u_int32_t comp_algorithm,
412 u_int32_t sense_flags);
413 static void saprevent(struct cam_periph *periph, int action);
414 static int sarewind(struct cam_periph *periph);
415 static int saspace(struct cam_periph *periph, int count,
416 scsi_space_code code);
417 static int samount(struct cam_periph *, int, struct cdev *);
418 static int saretension(struct cam_periph *periph);
419 static int sareservereleaseunit(struct cam_periph *periph,
421 static int saloadunload(struct cam_periph *periph, int load);
422 static int saerase(struct cam_periph *periph, int longerase);
423 static int sawritefilemarks(struct cam_periph *periph,
424 int nmarks, int setmarks);
425 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
426 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
429 static struct periph_driver sadriver =
432 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
435 PERIPHDRIVER_DECLARE(sa, sadriver);
437 /* For 2.2-stable support */
443 static struct cdevsw sa_cdevsw = {
444 .d_version = D_VERSION,
448 .d_write = physwrite,
450 .d_strategy = sastrategy,
456 saopen(struct cdev *dev, int flags, int fmt, struct thread *td)
458 struct cam_periph *periph;
459 struct sa_softc *softc;
462 periph = (struct cam_periph *)dev->si_drv1;
463 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
467 cam_periph_lock(periph);
469 softc = (struct sa_softc *)periph->softc;
471 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
472 ("saopen(%s): softc=0x%x\n", devtoname(dev), softc->flags));
474 if (SA_IS_CTRL(dev)) {
475 softc->ctrl_mode = 1;
476 cam_periph_unlock(periph);
480 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
481 cam_periph_unlock(periph);
482 cam_periph_release(periph);
486 if (softc->flags & SA_FLAG_OPEN) {
488 } else if (softc->flags & SA_FLAG_INVALID) {
492 * Preserve whether this is a read_only open.
494 softc->open_rdonly = (flags & O_RDWR) == O_RDONLY;
497 * The function samount ensures media is loaded and ready.
498 * It also does a device RESERVE if the tape isn't yet mounted.
500 * If the mount fails and this was a non-blocking open,
501 * make this a 'open_pending_mount' action.
503 error = samount(periph, flags, dev);
504 if (error && (flags & O_NONBLOCK)) {
505 softc->flags |= SA_FLAG_OPEN;
506 softc->open_pending_mount = 1;
507 cam_periph_unhold(periph);
508 cam_periph_unlock(periph);
514 cam_periph_unhold(periph);
515 cam_periph_unlock(periph);
516 cam_periph_release(periph);
520 saprevent(periph, PR_PREVENT);
521 softc->flags |= SA_FLAG_OPEN;
523 cam_periph_unhold(periph);
524 cam_periph_unlock(periph);
529 saclose(struct cdev *dev, int flag, int fmt, struct thread *td)
531 struct cam_periph *periph;
532 struct sa_softc *softc;
533 int mode, error, writing, tmp;
534 int closedbits = SA_FLAG_OPEN;
537 periph = (struct cam_periph *)dev->si_drv1;
541 cam_periph_lock(periph);
543 softc = (struct sa_softc *)periph->softc;
545 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
546 ("saclose(%s): softc=0x%x\n", devtoname(dev), softc->flags));
549 softc->open_rdonly = 0;
550 if (SA_IS_CTRL(dev)) {
551 softc->ctrl_mode = 0;
552 cam_periph_unlock(periph);
553 cam_periph_release(periph);
557 if (softc->open_pending_mount) {
558 softc->flags &= ~SA_FLAG_OPEN;
559 softc->open_pending_mount = 0;
560 cam_periph_unlock(periph);
561 cam_periph_release(periph);
565 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
566 cam_periph_unlock(periph);
571 * Were we writing the tape?
573 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
576 * See whether or not we need to write filemarks. If this
577 * fails, we probably have to assume we've lost tape
580 error = sacheckeod(periph);
582 xpt_print(periph->path,
583 "failed to write terminating filemark(s)\n");
584 softc->flags |= SA_FLAG_TAPE_FROZEN;
588 * Whatever we end up doing, allow users to eject tapes from here on.
590 saprevent(periph, PR_ALLOW);
593 * Decide how to end...
595 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
596 closedbits |= SA_FLAG_TAPE_FROZEN;
597 } else switch (mode) {
598 case SA_MODE_OFFLINE:
600 * An 'offline' close is an unconditional release of
601 * frozen && mount conditions, irrespective of whether
602 * these operations succeeded. The reason for this is
603 * to allow at least some kind of programmatic way
604 * around our state getting all fouled up. If somebody
605 * issues an 'offline' command, that will be allowed
608 (void) sarewind(periph);
609 (void) saloadunload(periph, FALSE);
610 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
614 * If the rewind fails, return an error- if anyone cares,
615 * but not overwriting any previous error.
617 * We don't clear the notion of mounted here, but we do
618 * clear the notion of frozen if we successfully rewound.
620 tmp = sarewind(periph);
625 closedbits |= SA_FLAG_TAPE_FROZEN;
628 case SA_MODE_NOREWIND:
630 * If we're not rewinding/unloading the tape, find out
631 * whether we need to back up over one of two filemarks
632 * we wrote (if we wrote two filemarks) so that appends
633 * from this point on will be sane.
635 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
636 tmp = saspace(periph, -1, SS_FILEMARKS);
638 xpt_print(periph->path, "unable to backspace "
639 "over one of double filemarks at end of "
641 xpt_print(periph->path, "it is possible that "
642 "this device needs a SA_QUIRK_1FM quirk set"
644 softc->flags |= SA_FLAG_TAPE_FROZEN;
649 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode);
655 * We wish to note here that there are no more filemarks to be written.
657 softc->filemarks = 0;
658 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
661 * And we are no longer open for business.
663 softc->flags &= ~closedbits;
666 * Inform users if tape state if frozen....
668 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
669 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, "
670 "REWIND or MTEOM command to clear this state.\n");
673 /* release the device if it is no longer mounted */
674 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
675 sareservereleaseunit(periph, FALSE);
677 cam_periph_unhold(periph);
678 cam_periph_unlock(periph);
679 cam_periph_release(periph);
685 * Actually translate the requested transfer into one the physical driver
686 * can understand. The transfer is described by a buf and will include
687 * only one physical transfer.
690 sastrategy(struct bio *bp)
692 struct cam_periph *periph;
693 struct sa_softc *softc;
695 bp->bio_resid = bp->bio_bcount;
696 if (SA_IS_CTRL(bp->bio_dev)) {
697 biofinish(bp, NULL, EINVAL);
700 periph = (struct cam_periph *)bp->bio_dev->si_drv1;
701 if (periph == NULL) {
702 biofinish(bp, NULL, ENXIO);
705 cam_periph_lock(periph);
707 softc = (struct sa_softc *)periph->softc;
709 if (softc->flags & SA_FLAG_INVALID) {
710 cam_periph_unlock(periph);
711 biofinish(bp, NULL, ENXIO);
715 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
716 cam_periph_unlock(periph);
717 biofinish(bp, NULL, EPERM);
722 * This should actually never occur as the write(2)
723 * system call traps attempts to write to a read-only
726 if (bp->bio_cmd == BIO_WRITE && softc->open_rdonly) {
727 cam_periph_unlock(periph);
728 biofinish(bp, NULL, EBADF);
732 if (softc->open_pending_mount) {
733 int error = samount(periph, 0, bp->bio_dev);
735 cam_periph_unlock(periph);
736 biofinish(bp, NULL, ENXIO);
739 saprevent(periph, PR_PREVENT);
740 softc->open_pending_mount = 0;
745 * If it's a null transfer, return immediately
747 if (bp->bio_bcount == 0) {
748 cam_periph_unlock(periph);
754 if (softc->flags & SA_FLAG_FIXED) {
756 * Fixed block device. The byte count must
757 * be a multiple of our block size.
759 if (((softc->blk_mask != ~0) &&
760 ((bp->bio_bcount & softc->blk_mask) != 0)) ||
761 ((softc->blk_mask == ~0) &&
762 ((bp->bio_bcount % softc->min_blk) != 0))) {
763 xpt_print(periph->path, "Invalid request. Fixed block "
764 "device requests must be a multiple of %d bytes\n",
766 cam_periph_unlock(periph);
767 biofinish(bp, NULL, EINVAL);
770 } else if ((bp->bio_bcount > softc->max_blk) ||
771 (bp->bio_bcount < softc->min_blk) ||
772 (bp->bio_bcount & softc->blk_mask) != 0) {
774 xpt_print_path(periph->path);
775 printf("Invalid request. Variable block "
776 "device requests must be ");
777 if (softc->blk_mask != 0) {
778 printf("a multiple of %d ", (0x1 << softc->blk_gran));
780 printf("between %d and %d bytes\n", softc->min_blk,
782 cam_periph_unlock(periph);
783 biofinish(bp, NULL, EINVAL);
788 * Place it at the end of the queue.
790 bioq_insert_tail(&softc->bio_queue, bp);
791 softc->queue_count++;
793 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
794 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
795 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
796 (bp->bio_cmd == BIO_READ)? "read" : "write"));
798 if (softc->queue_count > 1) {
799 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
800 ("sastrategy: queue count now %d\n", softc->queue_count));
804 * Schedule ourselves for performing the work.
806 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
807 cam_periph_unlock(periph);
813 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
814 if (softc->open_pending_mount) { \
815 error = samount(periph, 0, dev); \
819 saprevent(periph, PR_PREVENT); \
820 softc->open_pending_mount = 0; \
824 saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
826 struct cam_periph *periph;
827 struct sa_softc *softc;
828 scsi_space_code spaceop;
829 int didlockperiph = 0;
834 error = 0; /* shut up gcc */
835 spaceop = 0; /* shut up gcc */
837 periph = (struct cam_periph *)dev->si_drv1;
841 cam_periph_lock(periph);
842 softc = (struct sa_softc *)periph->softc;
845 * Check for control mode accesses. We allow MTIOCGET and
846 * MTIOCERRSTAT (but need to be the only one open in order
847 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
848 * and MTCOMP (but need to be the only one accessing this
849 * device to run those).
852 if (SA_IS_CTRL(dev)) {
854 case MTIOCGETEOTMODEL:
859 * If the periph isn't already locked, lock it
860 * so our MTIOCERRSTAT can reset latched error stats.
862 * If the periph is already locked, skip it because
863 * we're just getting status and it'll be up to the
864 * other thread that has this device open to do
865 * an MTIOCERRSTAT that would clear latched status.
867 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
868 error = cam_periph_hold(periph, PRIBIO|PCATCH);
870 cam_periph_unlock(periph);
879 struct mtop *mt = (struct mtop *) arg;
882 * Check to make sure it's an OP we can perform
883 * with no media inserted.
899 case MTIOCSETEOTMODEL:
901 * We need to acquire the peripheral here rather
902 * than at open time because we are sharing writable
903 * access to data structures.
905 error = cam_periph_hold(periph, PRIBIO|PCATCH);
907 cam_periph_unlock(periph);
914 cam_periph_unlock(periph);
920 * Find the device that the user is talking about
925 struct mtget *g = (struct mtget *)arg;
928 * If this isn't the control mode device, actually go out
929 * and ask the drive again what it's set to.
931 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) {
932 u_int8_t write_protect;
933 int comp_enabled, comp_supported;
934 error = sagetparams(periph, SA_PARAM_ALL,
935 &softc->media_blksize, &softc->media_density,
936 &softc->media_numblks, &softc->buffer_mode,
937 &write_protect, &softc->speed, &comp_supported,
938 &comp_enabled, &softc->comp_algorithm, NULL);
942 softc->flags |= SA_FLAG_TAPE_WP;
944 softc->flags &= ~SA_FLAG_TAPE_WP;
945 softc->flags &= ~(SA_FLAG_COMP_SUPP|
946 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
947 if (comp_supported) {
948 if (softc->saved_comp_algorithm == 0)
949 softc->saved_comp_algorithm =
950 softc->comp_algorithm;
951 softc->flags |= SA_FLAG_COMP_SUPP;
953 softc->flags |= SA_FLAG_COMP_ENABLED;
955 softc->flags |= SA_FLAG_COMP_UNSUPP;
957 bzero(g, sizeof(struct mtget));
958 g->mt_type = MT_ISAR;
959 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
960 g->mt_comp = MT_COMP_UNSUPP;
961 g->mt_comp0 = MT_COMP_UNSUPP;
962 g->mt_comp1 = MT_COMP_UNSUPP;
963 g->mt_comp2 = MT_COMP_UNSUPP;
964 g->mt_comp3 = MT_COMP_UNSUPP;
966 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
967 g->mt_comp = MT_COMP_DISABLED;
969 g->mt_comp = softc->comp_algorithm;
971 g->mt_comp0 = softc->comp_algorithm;
972 g->mt_comp1 = softc->comp_algorithm;
973 g->mt_comp2 = softc->comp_algorithm;
974 g->mt_comp3 = softc->comp_algorithm;
976 g->mt_density = softc->media_density;
977 g->mt_density0 = softc->media_density;
978 g->mt_density1 = softc->media_density;
979 g->mt_density2 = softc->media_density;
980 g->mt_density3 = softc->media_density;
981 g->mt_blksiz = softc->media_blksize;
982 g->mt_blksiz0 = softc->media_blksize;
983 g->mt_blksiz1 = softc->media_blksize;
984 g->mt_blksiz2 = softc->media_blksize;
985 g->mt_blksiz3 = softc->media_blksize;
986 g->mt_fileno = softc->fileno;
987 g->mt_blkno = softc->blkno;
988 g->mt_dsreg = (short) softc->dsreg;
990 * Yes, we know that this is likely to overflow
992 if (softc->last_resid_was_io) {
993 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
994 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
995 softc->last_io_resid = 0;
999 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
1000 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1001 softc->last_ctl_resid = 0;
1010 struct scsi_tape_errors *sep =
1011 &((union mterrstat *)arg)->scsi_errstat;
1013 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1014 ("saioctl: MTIOCERRSTAT\n"));
1016 bzero(sep, sizeof(*sep));
1017 sep->io_resid = softc->last_io_resid;
1018 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1019 sizeof (sep->io_sense));
1020 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1021 sizeof (sep->io_cdb));
1022 sep->ctl_resid = softc->last_ctl_resid;
1023 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1024 sizeof (sep->ctl_sense));
1025 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1026 sizeof (sep->ctl_cdb));
1028 if ((SA_IS_CTRL(dev) == 0 && softc->open_pending_mount) ||
1030 bzero((caddr_t) &softc->errinfo,
1031 sizeof (softc->errinfo));
1040 PENDING_MOUNT_CHECK(softc, periph, dev);
1042 mt = (struct mtop *)arg;
1045 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1046 ("saioctl: op=0x%x count=0x%x\n",
1047 mt->mt_op, mt->mt_count));
1049 count = mt->mt_count;
1050 switch (mt->mt_op) {
1051 case MTWEOF: /* write an end-of-file marker */
1053 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1054 * flag because by keeping track of filemarks
1055 * we have last written we know ehether or not
1056 * we need to write more when we close the device.
1058 error = sawritefilemarks(periph, count, FALSE);
1060 case MTWSS: /* write a setmark */
1061 error = sawritefilemarks(periph, count, TRUE);
1063 case MTBSR: /* backward space record */
1064 case MTFSR: /* forward space record */
1065 case MTBSF: /* backward space file */
1066 case MTFSF: /* forward space file */
1067 case MTBSS: /* backward space setmark */
1068 case MTFSS: /* forward space setmark */
1069 case MTEOD: /* space to end of recorded medium */
1073 spaceop = SS_FILEMARKS;
1074 nmarks = softc->filemarks;
1075 error = sacheckeod(periph);
1077 xpt_print(periph->path,
1078 "EOD check prior to spacing failed\n");
1079 softc->flags |= SA_FLAG_EIO_PENDING;
1082 nmarks -= softc->filemarks;
1088 spaceop = SS_BLOCKS;
1099 spaceop = SS_SETMARKS;
1113 nmarks = softc->filemarks;
1115 * XXX: Why are we checking again?
1117 error = sacheckeod(periph);
1120 nmarks -= softc->filemarks;
1121 error = saspace(periph, count - nmarks, spaceop);
1123 * At this point, clear that we've written the tape
1124 * and that we've written any filemarks. We really
1125 * don't know what the applications wishes to do next-
1126 * the sacheckeod's will make sure we terminated the
1127 * tape correctly if we'd been writing, but the next
1128 * action the user application takes will set again
1129 * whether we need to write filemarks.
1132 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1133 softc->filemarks = 0;
1136 case MTREW: /* rewind */
1137 PENDING_MOUNT_CHECK(softc, periph, dev);
1138 (void) sacheckeod(periph);
1139 error = sarewind(periph);
1142 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1143 softc->flags &= ~SA_FLAG_ERR_PENDING;
1144 softc->filemarks = 0;
1146 case MTERASE: /* erase */
1147 PENDING_MOUNT_CHECK(softc, periph, dev);
1148 error = saerase(periph, count);
1150 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1151 softc->flags &= ~SA_FLAG_ERR_PENDING;
1153 case MTRETENS: /* re-tension tape */
1154 PENDING_MOUNT_CHECK(softc, periph, dev);
1155 error = saretension(periph);
1157 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1158 softc->flags &= ~SA_FLAG_ERR_PENDING;
1160 case MTOFFL: /* rewind and put the drive offline */
1162 PENDING_MOUNT_CHECK(softc, periph, dev);
1164 (void) sacheckeod(periph);
1166 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1167 softc->filemarks = 0;
1169 error = sarewind(periph);
1170 /* clear the frozen flag anyway */
1171 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1174 * Be sure to allow media removal before ejecting.
1177 saprevent(periph, PR_ALLOW);
1179 error = saloadunload(periph, FALSE);
1181 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1186 case MTNOP: /* no operation, sets status only */
1187 case MTCACHE: /* enable controller cache */
1188 case MTNOCACHE: /* disable controller cache */
1192 case MTSETBSIZ: /* Set block size for device */
1194 PENDING_MOUNT_CHECK(softc, periph, dev);
1196 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1199 softc->last_media_blksize =
1200 softc->media_blksize;
1201 softc->media_blksize = count;
1203 softc->flags |= SA_FLAG_FIXED;
1204 if (powerof2(count)) {
1207 softc->blk_mask = count - 1;
1209 softc->blk_mask = ~0;
1210 softc->blk_shift = 0;
1213 * Make the user's desire 'persistent'.
1215 softc->quirks &= ~SA_QUIRK_VARIABLE;
1216 softc->quirks |= SA_QUIRK_FIXED;
1218 softc->flags &= ~SA_FLAG_FIXED;
1219 if (softc->max_blk == 0) {
1220 softc->max_blk = ~0;
1222 softc->blk_shift = 0;
1223 if (softc->blk_gran != 0) {
1225 softc->blk_gran - 1;
1227 softc->blk_mask = 0;
1230 * Make the user's desire 'persistent'.
1232 softc->quirks |= SA_QUIRK_VARIABLE;
1233 softc->quirks &= ~SA_QUIRK_FIXED;
1237 case MTSETDNSTY: /* Set density for device and mode */
1238 PENDING_MOUNT_CHECK(softc, periph, dev);
1240 if (count > UCHAR_MAX) {
1244 error = sasetparams(periph, SA_PARAM_DENSITY,
1248 case MTCOMP: /* enable compression */
1249 PENDING_MOUNT_CHECK(softc, periph, dev);
1251 * Some devices don't support compression, and
1252 * don't like it if you ask them for the
1255 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1256 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1260 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1261 0, 0, count, SF_NO_PRINT);
1273 PENDING_MOUNT_CHECK(softc, periph, dev);
1274 error = sardpos(periph, 0, (u_int32_t *) arg);
1277 PENDING_MOUNT_CHECK(softc, periph, dev);
1278 error = sardpos(periph, 1, (u_int32_t *) arg);
1281 PENDING_MOUNT_CHECK(softc, periph, dev);
1282 error = sasetpos(periph, 0, (u_int32_t *) arg);
1285 PENDING_MOUNT_CHECK(softc, periph, dev);
1286 error = sasetpos(periph, 1, (u_int32_t *) arg);
1288 case MTIOCGETEOTMODEL:
1290 if (softc->quirks & SA_QUIRK_1FM)
1294 *((u_int32_t *) arg) = mode;
1296 case MTIOCSETEOTMODEL:
1298 switch (*((u_int32_t *) arg)) {
1300 softc->quirks &= ~SA_QUIRK_2FM;
1301 softc->quirks |= SA_QUIRK_1FM;
1304 softc->quirks &= ~SA_QUIRK_1FM;
1305 softc->quirks |= SA_QUIRK_2FM;
1313 error = cam_periph_ioctl(periph, cmd, arg, saerror);
1318 * Check to see if we cleared a frozen state
1320 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1326 softc->fileno = (daddr_t) -1;
1327 softc->blkno = (daddr_t) -1;
1328 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1329 xpt_print(periph->path,
1330 "tape state now unfrozen.\n");
1336 if (didlockperiph) {
1337 cam_periph_unhold(periph);
1339 cam_periph_unlock(periph);
1349 * Install a global async callback.
1351 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL);
1353 if (status != CAM_REQ_CMP) {
1354 printf("sa: Failed to attach master async callback "
1355 "due to status 0x%x!\n", status);
1360 saoninvalidate(struct cam_periph *periph)
1362 struct sa_softc *softc;
1364 softc = (struct sa_softc *)periph->softc;
1367 * De-register any async callbacks.
1369 xpt_register_async(0, saasync, periph, periph->path);
1371 softc->flags |= SA_FLAG_INVALID;
1374 * Return all queued I/O with ENXIO.
1375 * XXX Handle any transactions queued to the card
1376 * with XPT_ABORT_CCB.
1378 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1379 softc->queue_count = 0;
1383 sacleanup(struct cam_periph *periph)
1385 struct sa_softc *softc;
1388 softc = (struct sa_softc *)periph->softc;
1390 devstat_remove_entry(softc->device_stats);
1391 cam_periph_unlock(periph);
1392 destroy_dev(softc->devs.ctl_dev);
1393 for (i = 0; i < SA_NUM_MODES; i++) {
1394 destroy_dev(softc->devs.mode_devs[i].r_dev);
1395 destroy_dev(softc->devs.mode_devs[i].nr_dev);
1396 destroy_dev(softc->devs.mode_devs[i].er_dev);
1398 cam_periph_lock(periph);
1399 free(softc, M_SCSISA);
1403 saasync(void *callback_arg, u_int32_t code,
1404 struct cam_path *path, void *arg)
1406 struct cam_periph *periph;
1408 periph = (struct cam_periph *)callback_arg;
1410 case AC_FOUND_DEVICE:
1412 struct ccb_getdev *cgd;
1415 cgd = (struct ccb_getdev *)arg;
1419 if (cgd->protocol != PROTO_SCSI)
1422 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1426 * Allocate a peripheral instance for
1427 * this device and start the probe
1430 status = cam_periph_alloc(saregister, saoninvalidate,
1432 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1433 saasync, AC_FOUND_DEVICE, cgd);
1435 if (status != CAM_REQ_CMP
1436 && status != CAM_REQ_INPROG)
1437 printf("saasync: Unable to probe new device "
1438 "due to status 0x%x\n", status);
1442 cam_periph_async(periph, code, path, arg);
1448 saregister(struct cam_periph *periph, void *arg)
1450 struct sa_softc *softc;
1451 struct ccb_getdev *cgd;
1452 struct ccb_pathinq cpi;
1456 cgd = (struct ccb_getdev *)arg;
1458 printf("saregister: no getdev CCB, can't register device\n");
1459 return (CAM_REQ_CMP_ERR);
1462 softc = (struct sa_softc *)
1463 malloc(sizeof (*softc), M_SCSISA, M_NOWAIT | M_ZERO);
1464 if (softc == NULL) {
1465 printf("saregister: Unable to probe new device. "
1466 "Unable to allocate softc\n");
1467 return (CAM_REQ_CMP_ERR);
1469 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1470 softc->state = SA_STATE_NORMAL;
1471 softc->fileno = (daddr_t) -1;
1472 softc->blkno = (daddr_t) -1;
1474 bioq_init(&softc->bio_queue);
1475 periph->softc = softc;
1478 * See if this device has any quirks.
1480 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1481 (caddr_t)sa_quirk_table,
1482 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1483 sizeof(*sa_quirk_table), scsi_inquiry_match);
1485 if (match != NULL) {
1486 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1487 softc->last_media_blksize =
1488 ((struct sa_quirk_entry *)match)->prefblk;
1490 softc->quirks = SA_QUIRK_NONE;
1492 bzero(&cpi, sizeof(cpi));
1493 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1494 cpi.ccb_h.func_code = XPT_PATH_INQ;
1495 xpt_action((union ccb *)&cpi);
1498 * The SA driver supports a blocksize, but we don't know the
1499 * blocksize until we media is inserted. So, set a flag to
1500 * indicate that the blocksize is unavailable right now.
1502 cam_periph_unlock(periph);
1503 softc->device_stats = devstat_new_entry("sa", periph->unit_number, 0,
1504 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1505 XPORT_DEVSTAT_TYPE(cpi.transport), DEVSTAT_PRIORITY_TAPE);
1508 * If maxio isn't set, we fall back to DFLTPHYS. If it is set, we
1509 * take it whether or not it's larger than MAXPHYS. physio will
1510 * break it down into pieces small enough to fit in a buffer.
1513 softc->maxio = DFLTPHYS;
1515 softc->maxio = cpi.maxio;
1518 * If the SIM supports unmapped I/O, let physio know that we can
1519 * handle unmapped buffers.
1521 if (cpi.hba_misc & PIM_UNMAPPED)
1522 softc->si_flags = SI_UNMAPPED;
1524 softc->devs.ctl_dev = make_dev(&sa_cdevsw, SAMINOR(SA_CTLDEV,
1525 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1526 0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1527 softc->devs.ctl_dev->si_drv1 = periph;
1528 softc->devs.ctl_dev->si_iosize_max = softc->maxio;
1529 softc->devs.ctl_dev->si_flags |= softc->si_flags;
1531 for (i = 0; i < SA_NUM_MODES; i++) {
1533 softc->devs.mode_devs[i].r_dev = make_dev(&sa_cdevsw,
1534 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_R),
1535 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1536 periph->periph_name, periph->unit_number, i);
1537 softc->devs.mode_devs[i].r_dev->si_drv1 = periph;
1538 softc->devs.mode_devs[i].r_dev->si_iosize_max = softc->maxio;
1539 softc->devs.mode_devs[i].r_dev->si_flags |= softc->si_flags;
1541 softc->devs.mode_devs[i].nr_dev = make_dev(&sa_cdevsw,
1542 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_NR),
1543 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1544 periph->periph_name, periph->unit_number, i);
1545 softc->devs.mode_devs[i].nr_dev->si_drv1 = periph;
1546 softc->devs.mode_devs[i].nr_dev->si_iosize_max = softc->maxio;
1547 softc->devs.mode_devs[i].nr_dev->si_flags |= softc->si_flags;
1549 softc->devs.mode_devs[i].er_dev = make_dev(&sa_cdevsw,
1550 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_ER),
1551 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1552 periph->periph_name, periph->unit_number, i);
1553 softc->devs.mode_devs[i].er_dev->si_drv1 = periph;
1554 softc->devs.mode_devs[i].er_dev->si_iosize_max = softc->maxio;
1555 softc->devs.mode_devs[i].er_dev->si_flags |= softc->si_flags;
1558 * Make the (well known) aliases for the first mode.
1563 alias = make_dev_alias(softc->devs.mode_devs[i].r_dev,
1564 "%s%d", periph->periph_name, periph->unit_number);
1565 alias->si_drv1 = periph;
1566 alias->si_iosize_max = softc->maxio;
1567 alias->si_flags |= softc->si_flags;
1569 alias = make_dev_alias(softc->devs.mode_devs[i].nr_dev,
1570 "n%s%d", periph->periph_name, periph->unit_number);
1571 alias->si_drv1 = periph;
1572 alias->si_iosize_max = softc->maxio;
1573 alias->si_flags |= softc->si_flags;
1575 alias = make_dev_alias(softc->devs.mode_devs[i].er_dev,
1576 "e%s%d", periph->periph_name, periph->unit_number);
1577 alias->si_drv1 = periph;
1578 alias->si_iosize_max = softc->maxio;
1579 alias->si_flags |= softc->si_flags;
1582 cam_periph_lock(periph);
1585 * Add an async callback so that we get
1586 * notified if this device goes away.
1588 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path);
1590 xpt_announce_periph(periph, NULL);
1591 xpt_announce_quirks(periph, softc->quirks, SA_QUIRK_BIT_STRING);
1593 return (CAM_REQ_CMP);
1597 sastart(struct cam_periph *periph, union ccb *start_ccb)
1599 struct sa_softc *softc;
1601 softc = (struct sa_softc *)periph->softc;
1603 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
1606 switch (softc->state) {
1607 case SA_STATE_NORMAL:
1609 /* Pull a buffer from the queue and get going on it */
1613 * See if there is a buf with work for us to do..
1615 bp = bioq_first(&softc->bio_queue);
1616 if (periph->immediate_priority <= periph->pinfo.priority) {
1617 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1618 ("queuing for immediate ccb\n"));
1619 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1620 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1622 periph->immediate_priority = CAM_PRIORITY_NONE;
1623 wakeup(&periph->ccb_list);
1624 } else if (bp == NULL) {
1625 xpt_release_ccb(start_ccb);
1626 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1627 struct bio *done_bp;
1629 softc->queue_count--;
1630 bioq_remove(&softc->bio_queue, bp);
1631 bp->bio_resid = bp->bio_bcount;
1633 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1635 * We now just clear errors in this case
1636 * and let the residual be the notifier.
1639 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1641 * This can only happen if we're reading
1642 * in fixed length mode. In this case,
1643 * we dump the rest of the list the
1647 if (bioq_first(&softc->bio_queue) != NULL) {
1651 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1652 bp->bio_error = EIO;
1653 bp->bio_flags |= BIO_ERROR;
1655 bp = bioq_first(&softc->bio_queue);
1657 * Only if we have no other buffers queued up
1658 * do we clear the pending error flag.
1661 softc->flags &= ~SA_FLAG_ERR_PENDING;
1662 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1663 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
1664 "%d more buffers queued up\n",
1665 (softc->flags & SA_FLAG_ERR_PENDING),
1666 (bp != NULL)? "not " : " ", softc->queue_count));
1667 xpt_release_ccb(start_ccb);
1672 bioq_remove(&softc->bio_queue, bp);
1673 softc->queue_count--;
1675 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1676 if (softc->blk_shift != 0) {
1678 bp->bio_bcount >> softc->blk_shift;
1679 } else if (softc->media_blksize != 0) {
1680 length = bp->bio_bcount /
1681 softc->media_blksize;
1683 bp->bio_error = EIO;
1684 xpt_print(periph->path, "zero blocksize"
1685 " for FIXED length writes?\n");
1690 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1691 ("issuing a %d fixed record %s\n",
1692 length, (bp->bio_cmd == BIO_READ)? "read" :
1696 length = bp->bio_bcount;
1698 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1699 ("issuing a %d variable byte %s\n",
1700 length, (bp->bio_cmd == BIO_READ)? "read" :
1704 devstat_start_transaction_bio(softc->device_stats, bp);
1706 * Some people have theorized that we should
1707 * suppress illegal length indication if we are
1708 * running in variable block mode so that we don't
1709 * have to request sense every time our requested
1710 * block size is larger than the written block.
1711 * The residual information from the ccb allows
1712 * us to identify this situation anyway. The only
1713 * problem with this is that we will not get
1714 * information about blocks that are larger than
1715 * our read buffer unless we set the block size
1716 * in the mode page to something other than 0.
1718 * I believe that this is a non-issue. If user apps
1719 * don't adjust their read size to match our record
1720 * size, that's just life. Anyway, the typical usage
1721 * would be to issue, e.g., 64KB reads and occasionally
1722 * have to do deal with 512 byte or 1KB intermediate
1725 softc->dsreg = (bp->bio_cmd == BIO_READ)?
1726 MTIO_DSREG_RD : MTIO_DSREG_WR;
1727 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1728 MSG_SIMPLE_Q_TAG, (bp->bio_cmd == BIO_READ ?
1729 SCSI_RW_READ : SCSI_RW_WRITE) |
1730 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
1731 SCSI_RW_BIO : 0), FALSE,
1732 (softc->flags & SA_FLAG_FIXED) != 0, length,
1733 (bp->bio_flags & BIO_UNMAPPED) != 0 ? (void *)bp :
1734 bp->bio_data, bp->bio_bcount, SSD_FULL_SIZE,
1736 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1737 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1738 start_ccb->ccb_h.ccb_bp = bp;
1739 bp = bioq_first(&softc->bio_queue);
1740 xpt_action(start_ccb);
1744 /* Have more work to do, so ensure we stay scheduled */
1745 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1749 case SA_STATE_ABNORMAL:
1751 panic("state 0x%x in sastart", softc->state);
1758 sadone(struct cam_periph *periph, union ccb *done_ccb)
1760 struct sa_softc *softc;
1761 struct ccb_scsiio *csio;
1763 softc = (struct sa_softc *)periph->softc;
1764 csio = &done_ccb->csio;
1765 switch (CCB_Type(csio)) {
1766 case SA_CCB_BUFFER_IO:
1771 softc->dsreg = MTIO_DSREG_REST;
1772 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1774 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1775 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1777 * A retry was scheduled, so just return.
1786 * Catastrophic error. Mark the tape as frozen
1787 * (we no longer know tape position).
1789 * Return all queued I/O with EIO, and unfreeze
1790 * our queue so that future transactions that
1791 * attempt to fix this problem can get to the
1796 softc->flags |= SA_FLAG_TAPE_FROZEN;
1797 bioq_flush(&softc->bio_queue, NULL, EIO);
1800 bp->bio_resid = bp->bio_bcount;
1801 bp->bio_error = error;
1802 bp->bio_flags |= BIO_ERROR;
1804 * In the error case, position is updated in saerror.
1807 bp->bio_resid = csio->resid;
1809 if (csio->resid != 0) {
1810 bp->bio_flags |= BIO_ERROR;
1812 if (bp->bio_cmd == BIO_WRITE) {
1813 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1814 softc->filemarks = 0;
1816 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1817 (softc->blkno != (daddr_t) -1)) {
1818 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1820 if (softc->blk_shift != 0) {
1821 l = bp->bio_bcount >>
1824 l = bp->bio_bcount /
1825 softc->media_blksize;
1827 softc->blkno += (daddr_t) l;
1834 * If we had an error (immediate or pending),
1835 * release the device queue now.
1837 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1838 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1839 if (error || bp->bio_resid) {
1840 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1841 ("error %d resid %ld count %ld\n", error,
1842 bp->bio_resid, bp->bio_bcount));
1844 biofinish(bp, softc->device_stats, 0);
1847 case SA_CCB_WAITING:
1849 /* Caller will release the CCB */
1850 wakeup(&done_ccb->ccb_h.cbfcnp);
1854 xpt_release_ccb(done_ccb);
1858 * Mount the tape (make sure it's ready for I/O).
1861 samount(struct cam_periph *periph, int oflags, struct cdev *dev)
1863 struct sa_softc *softc;
1868 * oflags can be checked for 'kind' of open (read-only check) - later
1869 * dev can be checked for a control-mode or compression open - later
1871 UNUSED_PARAMETER(oflags);
1872 UNUSED_PARAMETER(dev);
1875 softc = (struct sa_softc *)periph->softc;
1878 * This should determine if something has happend since the last
1879 * open/mount that would invalidate the mount. We do *not* want
1880 * to retry this command- we just want the status. But we only
1881 * do this if we're mounted already- if we're not mounted,
1882 * we don't care about the unit read state and can instead use
1883 * this opportunity to attempt to reserve the tape unit.
1886 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1887 ccb = cam_periph_getccb(periph, 1);
1888 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1889 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1890 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1891 softc->device_stats);
1892 if (error == ENXIO) {
1893 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1894 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1895 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1896 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1897 softc->device_stats);
1900 * We don't need to freeze the tape because we
1901 * will now attempt to rewind/load it.
1903 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1904 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
1905 xpt_print(periph->path,
1906 "error %d on TUR in samount\n", error);
1910 error = sareservereleaseunit(periph, TRUE);
1914 ccb = cam_periph_getccb(periph, 1);
1915 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1916 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1917 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1918 softc->device_stats);
1921 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1922 struct scsi_read_block_limits_data *rblim = NULL;
1923 int comp_enabled, comp_supported;
1924 u_int8_t write_protect, guessing = 0;
1927 * Clear out old state.
1929 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1930 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1931 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1932 softc->filemarks = 0;
1935 * *Very* first off, make sure we're loaded to BOT.
1937 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1938 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1939 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1940 softc->device_stats);
1943 * In case this doesn't work, do a REWIND instead
1946 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1947 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1948 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1949 softc->device_stats);
1952 xpt_release_ccb(ccb);
1957 * Do a dummy test read to force access to the
1958 * media so that the drive will really know what's
1959 * there. We actually don't really care what the
1960 * blocksize on tape is and don't expect to really
1961 * read a full record.
1963 rblim = (struct scsi_read_block_limits_data *)
1964 malloc(8192, M_SCSISA, M_NOWAIT);
1965 if (rblim == NULL) {
1966 xpt_print(periph->path, "no memory for test read\n");
1967 xpt_release_ccb(ccb);
1972 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1973 scsi_sa_read_write(&ccb->csio, 0, sadone,
1974 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1975 (void *) rblim, 8192, SSD_FULL_SIZE,
1977 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1978 softc->device_stats);
1979 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1980 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1981 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1982 SF_NO_PRINT | SF_RETRY_UA,
1983 softc->device_stats);
1985 xpt_print(periph->path,
1986 "unable to rewind after test read\n");
1987 xpt_release_ccb(ccb);
1993 * Next off, determine block limits.
1995 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1996 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1998 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1999 SF_NO_PRINT | SF_RETRY_UA, softc->device_stats);
2001 xpt_release_ccb(ccb);
2005 * If it's less than SCSI-2, READ BLOCK LIMITS is not
2006 * a MANDATORY command. Anyway- it doesn't matter-
2007 * we can proceed anyway.
2009 softc->blk_gran = 0;
2010 softc->max_blk = ~0;
2013 if (softc->scsi_rev >= SCSI_REV_SPC) {
2014 softc->blk_gran = RBL_GRAN(rblim);
2016 softc->blk_gran = 0;
2019 * We take max_blk == min_blk to mean a default to
2020 * fixed mode- but note that whatever we get out of
2021 * sagetparams below will actually determine whether
2022 * we are actually *in* fixed mode.
2024 softc->max_blk = scsi_3btoul(rblim->maximum);
2025 softc->min_blk = scsi_2btoul(rblim->minimum);
2030 * Next, perform a mode sense to determine
2031 * current density, blocksize, compression etc.
2033 error = sagetparams(periph, SA_PARAM_ALL,
2034 &softc->media_blksize,
2035 &softc->media_density,
2036 &softc->media_numblks,
2037 &softc->buffer_mode, &write_protect,
2038 &softc->speed, &comp_supported,
2039 &comp_enabled, &softc->comp_algorithm,
2044 * We could work a little harder here. We could
2045 * adjust our attempts to get information. It
2046 * might be an ancient tape drive. If someone
2047 * nudges us, we'll do that.
2053 * If no quirk has determined that this is a device that is
2054 * preferred to be in fixed or variable mode, now is the time
2057 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2060 * This could be expensive to find out. Luckily we
2061 * only need to do this once. If we start out in
2062 * 'default' mode, try and set ourselves to one
2063 * of the densities that would determine a wad
2064 * of other stuff. Go from highest to lowest.
2066 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2068 static u_int8_t ctry[] = {
2069 SCSI_DENSITY_HALFINCH_PE,
2070 SCSI_DENSITY_HALFINCH_6250C,
2071 SCSI_DENSITY_HALFINCH_6250,
2072 SCSI_DENSITY_HALFINCH_1600,
2073 SCSI_DENSITY_HALFINCH_800,
2074 SCSI_DENSITY_QIC_4GB,
2075 SCSI_DENSITY_QIC_2GB,
2076 SCSI_DENSITY_QIC_525_320,
2077 SCSI_DENSITY_QIC_150,
2078 SCSI_DENSITY_QIC_120,
2079 SCSI_DENSITY_QIC_24,
2080 SCSI_DENSITY_QIC_11_9TRK,
2081 SCSI_DENSITY_QIC_11_4TRK,
2082 SCSI_DENSITY_QIC_1320,
2083 SCSI_DENSITY_QIC_3080,
2086 for (i = 0; ctry[i]; i++) {
2087 error = sasetparams(periph,
2088 SA_PARAM_DENSITY, 0, ctry[i],
2091 softc->media_density = ctry[i];
2096 switch (softc->media_density) {
2097 case SCSI_DENSITY_QIC_11_4TRK:
2098 case SCSI_DENSITY_QIC_11_9TRK:
2099 case SCSI_DENSITY_QIC_24:
2100 case SCSI_DENSITY_QIC_120:
2101 case SCSI_DENSITY_QIC_150:
2102 case SCSI_DENSITY_QIC_525_320:
2103 case SCSI_DENSITY_QIC_1320:
2104 case SCSI_DENSITY_QIC_3080:
2105 softc->quirks &= ~SA_QUIRK_2FM;
2106 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2107 softc->last_media_blksize = 512;
2109 case SCSI_DENSITY_QIC_4GB:
2110 case SCSI_DENSITY_QIC_2GB:
2111 softc->quirks &= ~SA_QUIRK_2FM;
2112 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2113 softc->last_media_blksize = 1024;
2116 softc->last_media_blksize =
2117 softc->media_blksize;
2118 softc->quirks |= SA_QUIRK_VARIABLE;
2124 * If no quirk has determined that this is a device that needs
2125 * to have 2 Filemarks at EOD, now is the time to find out.
2128 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2129 switch (softc->media_density) {
2130 case SCSI_DENSITY_HALFINCH_800:
2131 case SCSI_DENSITY_HALFINCH_1600:
2132 case SCSI_DENSITY_HALFINCH_6250:
2133 case SCSI_DENSITY_HALFINCH_6250C:
2134 case SCSI_DENSITY_HALFINCH_PE:
2135 softc->quirks &= ~SA_QUIRK_1FM;
2136 softc->quirks |= SA_QUIRK_2FM;
2144 * Now validate that some info we got makes sense.
2146 if ((softc->max_blk < softc->media_blksize) ||
2147 (softc->min_blk > softc->media_blksize &&
2148 softc->media_blksize)) {
2149 xpt_print(periph->path,
2150 "BLOCK LIMITS (%d..%d) could not match current "
2151 "block settings (%d)- adjusting\n", softc->min_blk,
2152 softc->max_blk, softc->media_blksize);
2153 softc->max_blk = softc->min_blk =
2154 softc->media_blksize;
2158 * Now put ourselves into the right frame of mind based
2163 * If we want to be in FIXED mode and our current blocksize
2164 * is not equal to our last blocksize (if nonzero), try and
2165 * set ourselves to this last blocksize (as the 'preferred'
2166 * block size). The initial quirkmatch at registry sets the
2167 * initial 'last' blocksize. If, for whatever reason, this
2168 * 'last' blocksize is zero, set the blocksize to 512,
2169 * or min_blk if that's larger.
2171 if ((softc->quirks & SA_QUIRK_FIXED) &&
2172 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2173 (softc->media_blksize != softc->last_media_blksize)) {
2174 softc->media_blksize = softc->last_media_blksize;
2175 if (softc->media_blksize == 0) {
2176 softc->media_blksize = 512;
2177 if (softc->media_blksize < softc->min_blk) {
2178 softc->media_blksize = softc->min_blk;
2181 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2182 softc->media_blksize, 0, 0, SF_NO_PRINT);
2184 xpt_print(periph->path,
2185 "unable to set fixed blocksize to %d\n",
2186 softc->media_blksize);
2191 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2192 (softc->media_blksize != 0)) {
2193 softc->last_media_blksize = softc->media_blksize;
2194 softc->media_blksize = 0;
2195 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2196 0, 0, 0, SF_NO_PRINT);
2199 * If this fails and we were guessing, just
2200 * assume that we got it wrong and go try
2201 * fixed block mode. Don't even check against
2202 * density code at this point.
2205 softc->quirks &= ~SA_QUIRK_VARIABLE;
2206 softc->quirks |= SA_QUIRK_FIXED;
2207 if (softc->last_media_blksize == 0)
2208 softc->last_media_blksize = 512;
2211 xpt_print(periph->path,
2212 "unable to set variable blocksize\n");
2218 * Now that we have the current block size,
2219 * set up some parameters for sastart's usage.
2221 if (softc->media_blksize) {
2222 softc->flags |= SA_FLAG_FIXED;
2223 if (powerof2(softc->media_blksize)) {
2225 ffs(softc->media_blksize) - 1;
2226 softc->blk_mask = softc->media_blksize - 1;
2228 softc->blk_mask = ~0;
2229 softc->blk_shift = 0;
2233 * The SCSI-3 spec allows 0 to mean "unspecified".
2234 * The SCSI-1 spec allows 0 to mean 'infinite'.
2236 * Either works here.
2238 if (softc->max_blk == 0) {
2239 softc->max_blk = ~0;
2241 softc->blk_shift = 0;
2242 if (softc->blk_gran != 0) {
2243 softc->blk_mask = softc->blk_gran - 1;
2245 softc->blk_mask = 0;
2250 softc->flags |= SA_FLAG_TAPE_WP;
2252 if (comp_supported) {
2253 if (softc->saved_comp_algorithm == 0)
2254 softc->saved_comp_algorithm =
2255 softc->comp_algorithm;
2256 softc->flags |= SA_FLAG_COMP_SUPP;
2258 softc->flags |= SA_FLAG_COMP_ENABLED;
2260 softc->flags |= SA_FLAG_COMP_UNSUPP;
2262 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2263 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2264 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2267 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2269 xpt_print(periph->path,
2270 "unable to set buffered mode\n");
2272 error = 0; /* not an error */
2277 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2281 free(rblim, M_SCSISA);
2284 softc->dsreg = MTIO_DSREG_NIL;
2286 softc->fileno = softc->blkno = 0;
2287 softc->dsreg = MTIO_DSREG_REST;
2289 #ifdef SA_1FM_AT_EOD
2290 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2291 softc->quirks |= SA_QUIRK_1FM;
2293 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2294 softc->quirks |= SA_QUIRK_2FM;
2297 xpt_release_ccb(ccb);
2300 * If we return an error, we're not mounted any more,
2301 * so release any device reservation.
2304 (void) sareservereleaseunit(periph, FALSE);
2307 * Clear I/O residual.
2309 softc->last_io_resid = 0;
2310 softc->last_ctl_resid = 0;
2316 * How many filemarks do we need to write if we were to terminate the
2317 * tape session right now? Note that this can be a negative number
2321 samarkswanted(struct cam_periph *periph)
2324 struct sa_softc *softc;
2326 softc = (struct sa_softc *)periph->softc;
2328 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2330 if (softc->quirks & SA_QUIRK_2FM)
2333 markswanted -= softc->filemarks;
2334 return (markswanted);
2338 sacheckeod(struct cam_periph *periph)
2343 markswanted = samarkswanted(periph);
2345 if (markswanted > 0) {
2346 error = sawritefilemarks(periph, markswanted, FALSE);
2354 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2356 static const char *toobig =
2357 "%d-byte tape record bigger than supplied buffer\n";
2358 struct cam_periph *periph;
2359 struct sa_softc *softc;
2360 struct ccb_scsiio *csio;
2361 struct scsi_sense_data *sense;
2365 int error_code, sense_key, asc, ascq, error, aqvalid, stream_valid;
2367 uint8_t stream_bits;
2369 periph = xpt_path_periph(ccb->ccb_h.path);
2370 softc = (struct sa_softc *)periph->softc;
2372 sense = &csio->sense_data;
2373 sense_len = csio->sense_len - csio->sense_resid;
2374 scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
2375 &asc, &ascq, /*show_errors*/ 1);
2376 if (asc != -1 && ascq != -1)
2380 if (scsi_get_stream_info(sense, sense_len, NULL, &stream_bits) == 0)
2386 status = csio->ccb_h.status & CAM_STATUS_MASK;
2389 * Calculate/latch up, any residuals... We do this in a funny 2-step
2390 * so we can print stuff here if we have CAM_DEBUG enabled for this
2393 if (status == CAM_SCSI_STATUS_ERROR) {
2394 if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &resid,
2396 if ((softc->flags & SA_FLAG_FIXED) != 0)
2397 resid *= softc->media_blksize;
2399 resid = csio->dxfer_len;
2401 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2402 if (softc->media_blksize)
2403 info /= softc->media_blksize;
2406 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2407 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2408 sizeof (struct scsi_sense_data));
2409 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2410 (int) csio->cdb_len);
2411 softc->last_io_resid = resid;
2412 softc->last_resid_was_io = 1;
2414 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2415 sizeof (struct scsi_sense_data));
2416 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2417 (int) csio->cdb_len);
2418 softc->last_ctl_resid = resid;
2419 softc->last_resid_was_io = 0;
2421 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2422 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %jd "
2423 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2424 sense_key, asc, ascq, status,
2425 (stream_valid) ? stream_bits : 0, (intmax_t)resid,
2428 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2429 ("Cam Status 0x%x\n", status));
2435 case CAM_SCSI_STATUS_ERROR:
2437 * If a read/write command, we handle it here.
2439 if (CCB_Type(csio) != SA_CCB_WAITING) {
2443 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2444 * on a non read/write command, we assume it's not an error
2445 * and propagate the residule and return.
2447 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2448 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2449 csio->resid = resid;
2454 * Otherwise, we let the common code handle this.
2456 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2460 * We cannot depend upon CAM honoring retry counts for these.
2462 case CAM_SCSI_BUS_RESET:
2464 if (ccb->ccb_h.retry_count <= 0) {
2469 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2473 * Handle filemark, end of tape, mismatched record sizes....
2474 * From this point out, we're only handling read/write cases.
2475 * Handle writes && reads differently.
2478 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2479 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2480 csio->resid = resid;
2482 } else if ((stream_valid != 0) && (stream_bits & SSD_EOM)) {
2483 softc->flags |= SA_FLAG_EOM_PENDING;
2485 * Grotesque as it seems, the few times
2486 * I've actually seen a non-zero resid,
2487 * the tape drive actually lied and had
2488 * written all the data!.
2493 csio->resid = resid;
2494 if (sense_key == SSD_KEY_BLANK_CHECK) {
2495 if (softc->quirks & SA_QUIRK_1FM) {
2497 softc->flags |= SA_FLAG_EOM_PENDING;
2501 } else if ((stream_valid != 0) && (stream_bits & SSD_FILEMARK)){
2502 if (softc->flags & SA_FLAG_FIXED) {
2504 softc->flags |= SA_FLAG_EOF_PENDING;
2507 * Unconditionally, if we detected a filemark on a read,
2508 * mark that we've run moved a file ahead.
2510 if (softc->fileno != (daddr_t) -1) {
2513 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2519 * Incorrect Length usually applies to read, but can apply to writes.
2521 if (error == 0 && (stream_valid != 0) && (stream_bits & SSD_ILI)) {
2523 xpt_print(csio->ccb_h.path, toobig,
2524 csio->dxfer_len - info);
2525 csio->resid = csio->dxfer_len;
2528 csio->resid = resid;
2529 if (softc->flags & SA_FLAG_FIXED) {
2530 softc->flags |= SA_FLAG_EIO_PENDING;
2533 * Bump the block number if we hadn't seen a filemark.
2534 * Do this independent of errors (we've moved anyway).
2536 if ((stream_valid == 0) ||
2537 (stream_bits & SSD_FILEMARK) == 0) {
2538 if (softc->blkno != (daddr_t) -1) {
2540 csio->ccb_h.ccb_pflags |=
2541 SA_POSITION_UPDATED;
2549 * Unfreeze the queue if frozen as we're not returning anything
2550 * to our waiters that would indicate an I/O error has occurred
2560 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2561 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2562 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2563 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2568 struct scsi_mode_header_6 *mode_hdr;
2569 struct scsi_mode_blk_desc *mode_blk;
2570 int mode_buffer_len;
2571 struct sa_softc *softc;
2576 softc = (struct sa_softc *)periph->softc;
2577 ccb = cam_periph_getccb(periph, 1);
2578 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2579 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2581 cpage = SA_DATA_COMPRESSION_PAGE;
2584 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2586 if (params_to_get & SA_PARAM_COMPRESSION) {
2587 if (softc->quirks & SA_QUIRK_NOCOMP) {
2588 *comp_supported = FALSE;
2589 params_to_get &= ~SA_PARAM_COMPRESSION;
2591 mode_buffer_len += sizeof (sa_comp_t);
2594 /* XXX Fix M_NOWAIT */
2595 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
2596 if (mode_buffer == NULL) {
2597 xpt_release_ccb(ccb);
2600 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2601 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2603 /* it is safe to retry this */
2604 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2605 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2606 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2607 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2609 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2610 softc->device_stats);
2612 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2614 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2616 * Hmm. Let's see if we can try another page...
2617 * If we've already done that, give up on compression
2618 * for this device and remember this for the future
2619 * and attempt the request without asking for compression
2622 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2623 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2626 softc->quirks |= SA_QUIRK_NOCOMP;
2627 free(mode_buffer, M_SCSISA);
2629 } else if (status == CAM_SCSI_STATUS_ERROR) {
2630 /* Tell the user about the fatal error. */
2631 scsi_sense_print(&ccb->csio);
2632 goto sagetparamsexit;
2636 * If the user only wants the compression information, and
2637 * the device doesn't send back the block descriptor, it's
2638 * no big deal. If the user wants more than just
2639 * compression, though, and the device doesn't pass back the
2640 * block descriptor, we need to send another mode sense to
2641 * get the block descriptor.
2643 if ((mode_hdr->blk_desc_len == 0) &&
2644 (params_to_get & SA_PARAM_COMPRESSION) &&
2645 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2648 * Decrease the mode buffer length by the size of
2649 * the compression page, to make sure the data
2650 * there doesn't get overwritten.
2652 mode_buffer_len -= sizeof (sa_comp_t);
2655 * Now move the compression page that we presumably
2656 * got back down the memory chunk a little bit so
2657 * it doesn't get spammed.
2659 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2660 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2663 * Now, we issue another mode sense and just ask
2664 * for the block descriptor, etc.
2667 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2668 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2669 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2672 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2673 softc->device_stats);
2676 goto sagetparamsexit;
2679 if (params_to_get & SA_PARAM_BLOCKSIZE)
2680 *blocksize = scsi_3btoul(mode_blk->blklen);
2682 if (params_to_get & SA_PARAM_NUMBLOCKS)
2683 *numblocks = scsi_3btoul(mode_blk->nblocks);
2685 if (params_to_get & SA_PARAM_BUFF_MODE)
2686 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2688 if (params_to_get & SA_PARAM_DENSITY)
2689 *density = mode_blk->density;
2691 if (params_to_get & SA_PARAM_WP)
2692 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2694 if (params_to_get & SA_PARAM_SPEED)
2695 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2697 if (params_to_get & SA_PARAM_COMPRESSION) {
2698 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2699 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2700 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2702 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2704 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2705 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2707 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2709 * We don't really know whether this device supports
2710 * Data Compression if the algorithm field is
2711 * zero. Just say we do.
2713 *comp_supported = TRUE;
2715 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2716 *comp_algorithm = cp->sel_comp_alg;
2719 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2722 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2724 char *xyz = mode_buffer;
2725 xpt_print_path(periph->path);
2726 printf("Mode Sense Data=");
2727 for (idx = 0; idx < mode_buffer_len; idx++)
2728 printf(" 0x%02x", xyz[idx] & 0xff);
2734 xpt_release_ccb(ccb);
2735 free(mode_buffer, M_SCSISA);
2740 * The purpose of this function is to set one of four different parameters
2744 * - compression / compression algorithm
2747 * The assumption is that this will be called from saioctl(), and therefore
2748 * from a process context. Thus the waiting malloc calls below. If that
2749 * assumption ever changes, the malloc calls should be changed to be
2752 * Any or all of the four parameters may be set when this function is
2753 * called. It should handle setting more than one parameter at once.
2756 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2757 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2758 u_int32_t sense_flags)
2760 struct sa_softc *softc;
2761 u_int32_t current_blocksize;
2762 u_int32_t current_calg;
2763 u_int8_t current_density;
2764 u_int8_t current_speed;
2765 int comp_enabled, comp_supported;
2767 int mode_buffer_len;
2768 struct scsi_mode_header_6 *mode_hdr;
2769 struct scsi_mode_blk_desc *mode_blk;
2770 sa_comp_t *ccomp, *cpage;
2772 union ccb *ccb = NULL;
2775 softc = (struct sa_softc *)periph->softc;
2777 ccomp = malloc(sizeof (sa_comp_t), M_SCSISA, M_NOWAIT);
2782 * Since it doesn't make sense to set the number of blocks, or
2783 * write protection, we won't try to get the current value. We
2784 * always want to get the blocksize, so we can set it back to the
2787 error = sagetparams(periph,
2788 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2789 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2790 ¤t_speed, &comp_supported, &comp_enabled,
2791 ¤t_calg, ccomp);
2794 free(ccomp, M_SCSISA);
2798 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2799 if (params_to_set & SA_PARAM_COMPRESSION)
2800 mode_buffer_len += sizeof (sa_comp_t);
2802 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
2803 if (mode_buffer == NULL) {
2804 free(ccomp, M_SCSISA);
2808 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2809 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2811 ccb = cam_periph_getccb(periph, 1);
2815 if (params_to_set & SA_PARAM_COMPRESSION) {
2817 cpage = (sa_comp_t *)&mode_blk[1];
2819 cpage = (sa_comp_t *)&mode_hdr[1];
2821 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2822 cpage->hdr.pagecode &= ~0x80;
2827 * If the caller wants us to set the blocksize, use the one they
2828 * pass in. Otherwise, use the blocksize we got back from the
2829 * mode select above.
2832 if (params_to_set & SA_PARAM_BLOCKSIZE)
2833 scsi_ulto3b(blocksize, mode_blk->blklen);
2835 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2838 * Set density if requested, else preserve old density.
2839 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2840 * devices, else density we've latched up in our softc.
2842 if (params_to_set & SA_PARAM_DENSITY) {
2843 mode_blk->density = density;
2844 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2845 mode_blk->density = SCSI_SAME_DENSITY;
2847 mode_blk->density = softc->media_density;
2852 * For mode selects, these two fields must be zero.
2854 mode_hdr->data_length = 0;
2855 mode_hdr->medium_type = 0;
2857 /* set the speed to the current value */
2858 mode_hdr->dev_spec = current_speed;
2860 /* if set, set single-initiator buffering mode */
2861 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
2862 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2866 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2868 mode_hdr->blk_desc_len = 0;
2871 * First, if the user wants us to set the compression algorithm or
2872 * just turn compression on, check to make sure that this drive
2873 * supports compression.
2875 if (params_to_set & SA_PARAM_COMPRESSION) {
2877 * If the compression algorithm is 0, disable compression.
2878 * If the compression algorithm is non-zero, enable
2879 * compression and set the compression type to the
2880 * specified compression algorithm, unless the algorithm is
2881 * MT_COMP_ENABLE. In that case, we look at the
2882 * compression algorithm that is currently set and if it is
2883 * non-zero, we leave it as-is. If it is zero, and we have
2884 * saved a compression algorithm from a time when
2885 * compression was enabled before, set the compression to
2888 switch (ccomp->hdr.pagecode & ~0x80) {
2889 case SA_DEVICE_CONFIGURATION_PAGE:
2891 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2893 dcp->sel_comp_alg = SA_COMP_NONE;
2896 if (calg != MT_COMP_ENABLE) {
2897 dcp->sel_comp_alg = calg;
2898 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2899 softc->saved_comp_algorithm != 0) {
2900 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2904 case SA_DATA_COMPRESSION_PAGE:
2905 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2906 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2909 * Disable compression, but leave the
2910 * decompression and the capability bit
2913 dcp->dce_and_dcc = SA_DCP_DCC;
2914 dcp->dde_and_red |= SA_DCP_DDE;
2917 /* enable compression && decompression */
2918 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2919 dcp->dde_and_red |= SA_DCP_DDE;
2921 * If there, use compression algorithm from caller.
2922 * Otherwise, if there's a saved compression algorithm
2923 * and there is no current algorithm, use the saved
2924 * algorithm. Else parrot back what we got and hope
2927 if (calg != MT_COMP_ENABLE) {
2928 scsi_ulto4b(calg, dcp->comp_algorithm);
2929 scsi_ulto4b(calg, dcp->decomp_algorithm);
2930 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2931 softc->saved_comp_algorithm != 0) {
2932 scsi_ulto4b(softc->saved_comp_algorithm,
2933 dcp->comp_algorithm);
2934 scsi_ulto4b(softc->saved_comp_algorithm,
2935 dcp->decomp_algorithm);
2940 * Compression does not appear to be supported-
2941 * at least via the DATA COMPRESSION page. It
2942 * would be too much to ask us to believe that
2943 * the page itself is supported, but incorrectly
2944 * reports an ability to manipulate data compression,
2945 * so we'll assume that this device doesn't support
2946 * compression. We can just fall through for that.
2951 * The drive doesn't seem to support compression,
2952 * so turn off the set compression bit.
2954 params_to_set &= ~SA_PARAM_COMPRESSION;
2955 xpt_print(periph->path,
2956 "device does not seem to support compression\n");
2959 * If that was the only thing the user wanted us to set,
2960 * clean up allocated resources and return with
2961 * 'operation not supported'.
2963 if (params_to_set == SA_PARAM_NONE) {
2964 free(mode_buffer, M_SCSISA);
2965 xpt_release_ccb(ccb);
2970 * That wasn't the only thing the user wanted us to set.
2971 * So, decrease the stated mode buffer length by the
2972 * size of the compression mode page.
2974 mode_buffer_len -= sizeof(sa_comp_t);
2978 /* It is safe to retry this operation */
2979 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2980 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2981 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2983 error = cam_periph_runccb(ccb, saerror, 0,
2984 sense_flags, softc->device_stats);
2986 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2988 char *xyz = mode_buffer;
2989 xpt_print_path(periph->path);
2990 printf("Err%d, Mode Select Data=", error);
2991 for (idx = 0; idx < mode_buffer_len; idx++)
2992 printf(" 0x%02x", xyz[idx] & 0xff);
2999 * If we can, try without setting density/blocksize.
3002 if ((params_to_set &
3003 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
3008 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
3009 cpage = (sa_comp_t *)&mode_blk[1];
3013 * If we were setting the blocksize, and that failed, we
3014 * want to set it to its original value. If we weren't
3015 * setting the blocksize, we don't want to change it.
3017 scsi_ulto3b(current_blocksize, mode_blk->blklen);
3020 * Set density if requested, else preserve old density.
3021 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
3022 * devices, else density we've latched up in our softc.
3024 if (params_to_set & SA_PARAM_DENSITY) {
3025 mode_blk->density = current_density;
3026 } else if (softc->scsi_rev > SCSI_REV_CCS) {
3027 mode_blk->density = SCSI_SAME_DENSITY;
3029 mode_blk->density = softc->media_density;
3032 if (params_to_set & SA_PARAM_COMPRESSION)
3033 bcopy(ccomp, cpage, sizeof (sa_comp_t));
3036 * The retry count is the only CCB field that might have been
3037 * changed that we care about, so reset it back to 1.
3039 ccb->ccb_h.retry_count = 1;
3040 cam_periph_runccb(ccb, saerror, 0, sense_flags,
3041 softc->device_stats);
3044 xpt_release_ccb(ccb);
3047 free(ccomp, M_SCSISA);
3049 if (params_to_set & SA_PARAM_COMPRESSION) {
3051 softc->flags &= ~SA_FLAG_COMP_ENABLED;
3053 * Even if we get an error setting compression,
3054 * do not say that we don't support it. We could
3055 * have been wrong, or it may be media specific.
3056 * softc->flags &= ~SA_FLAG_COMP_SUPP;
3058 softc->saved_comp_algorithm = softc->comp_algorithm;
3059 softc->comp_algorithm = 0;
3061 softc->flags |= SA_FLAG_COMP_ENABLED;
3062 softc->comp_algorithm = calg;
3066 free(mode_buffer, M_SCSISA);
3071 saprevent(struct cam_periph *periph, int action)
3073 struct sa_softc *softc;
3077 softc = (struct sa_softc *)periph->softc;
3079 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3081 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3085 * We can be quiet about illegal requests.
3087 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3092 ccb = cam_periph_getccb(periph, 1);
3094 /* It is safe to retry this operation */
3095 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3096 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3098 error = cam_periph_runccb(ccb, saerror, 0, sf, softc->device_stats);
3100 if (action == PR_ALLOW)
3101 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3103 softc->flags |= SA_FLAG_TAPE_LOCKED;
3106 xpt_release_ccb(ccb);
3110 sarewind(struct cam_periph *periph)
3113 struct sa_softc *softc;
3116 softc = (struct sa_softc *)periph->softc;
3118 ccb = cam_periph_getccb(periph, 1);
3120 /* It is safe to retry this operation */
3121 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3122 SSD_FULL_SIZE, REWIND_TIMEOUT);
3124 softc->dsreg = MTIO_DSREG_REW;
3125 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3126 softc->dsreg = MTIO_DSREG_REST;
3128 xpt_release_ccb(ccb);
3130 softc->fileno = softc->blkno = (daddr_t) 0;
3132 softc->fileno = softc->blkno = (daddr_t) -1;
3137 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3140 struct sa_softc *softc;
3143 softc = (struct sa_softc *)periph->softc;
3145 ccb = cam_periph_getccb(periph, 1);
3147 /* This cannot be retried */
3149 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3150 SSD_FULL_SIZE, SPACE_TIMEOUT);
3153 * Clear residual because we will be using it.
3155 softc->last_ctl_resid = 0;
3157 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3158 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3159 softc->dsreg = MTIO_DSREG_REST;
3161 xpt_release_ccb(ccb);
3164 * If a spacing operation has failed, we need to invalidate
3167 * If the spacing operation was setmarks or to end of recorded data,
3168 * we no longer know our relative position.
3170 * If the spacing operations was spacing files in reverse, we
3171 * take account of the residual, but still check against less
3172 * than zero- if we've gone negative, we must have hit BOT.
3174 * If the spacing operations was spacing records in reverse and
3175 * we have a residual, we've either hit BOT or hit a filemark.
3176 * In the former case, we know our new record number (0). In
3177 * the latter case, we have absolutely no idea what the real
3178 * record number is- we've stopped between the end of the last
3179 * record in the previous file and the filemark that stopped
3180 * our spacing backwards.
3183 softc->fileno = softc->blkno = (daddr_t) -1;
3184 } else if (code == SS_SETMARKS || code == SS_EOD) {
3185 softc->fileno = softc->blkno = (daddr_t) -1;
3186 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3187 softc->fileno += (count - softc->last_ctl_resid);
3188 if (softc->fileno < 0) /* we must of hit BOT */
3191 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3192 softc->blkno += (count - softc->last_ctl_resid);
3194 if (softc->last_ctl_resid || softc->blkno < 0) {
3195 if (softc->fileno == 0) {
3198 softc->blkno = (daddr_t) -1;
3207 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3210 struct sa_softc *softc;
3213 softc = (struct sa_softc *)periph->softc;
3214 if (softc->open_rdonly)
3217 ccb = cam_periph_getccb(periph, 1);
3219 * Clear residual because we will be using it.
3221 softc->last_ctl_resid = 0;
3223 softc->dsreg = MTIO_DSREG_FMK;
3224 /* this *must* not be retried */
3225 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3226 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3227 softc->dsreg = MTIO_DSREG_REST;
3230 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3232 if (error == 0 && nmarks) {
3233 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3234 nwm = nmarks - softc->last_ctl_resid;
3235 softc->filemarks += nwm;
3238 xpt_release_ccb(ccb);
3241 * Update relative positions (if we're doing that).
3244 softc->fileno = softc->blkno = (daddr_t) -1;
3245 } else if (softc->fileno != (daddr_t) -1) {
3246 softc->fileno += nwm;
3253 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3255 struct scsi_tape_position_data loc;
3257 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3261 * We try and flush any buffered writes here if we were writing
3262 * and we're trying to get hardware block position. It eats
3263 * up performance substantially, but I'm wary of drive firmware.
3265 * I think that *logical* block position is probably okay-
3266 * but hardware block position might have to wait for data
3267 * to hit media to be valid. Caveat Emptor.
3270 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3271 error = sawritefilemarks(periph, 0, 0);
3272 if (error && error != EACCES)
3276 ccb = cam_periph_getccb(periph, 1);
3277 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3278 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3279 softc->dsreg = MTIO_DSREG_RBSY;
3280 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3281 softc->dsreg = MTIO_DSREG_REST;
3284 if (loc.flags & SA_RPOS_UNCERTAIN) {
3285 error = EINVAL; /* nothing is certain */
3287 *blkptr = scsi_4btoul(loc.firstblk);
3291 xpt_release_ccb(ccb);
3296 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3299 struct sa_softc *softc;
3303 * We used to try and flush any buffered writes here.
3304 * Now we push this onto user applications to either
3305 * flush the pending writes themselves (via a zero count
3306 * WRITE FILEMARKS command) or they can trust their tape
3307 * drive to do this correctly for them.
3310 softc = (struct sa_softc *)periph->softc;
3311 ccb = cam_periph_getccb(periph, 1);
3314 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3315 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3318 softc->dsreg = MTIO_DSREG_POS;
3319 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3320 softc->dsreg = MTIO_DSREG_REST;
3321 xpt_release_ccb(ccb);
3323 * Note relative file && block number position as now unknown.
3325 softc->fileno = softc->blkno = (daddr_t) -1;
3330 saretension(struct cam_periph *periph)
3333 struct sa_softc *softc;
3336 softc = (struct sa_softc *)periph->softc;
3338 ccb = cam_periph_getccb(periph, 1);
3340 /* It is safe to retry this operation */
3341 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3342 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3344 softc->dsreg = MTIO_DSREG_TEN;
3345 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3346 softc->dsreg = MTIO_DSREG_REST;
3348 xpt_release_ccb(ccb);
3350 softc->fileno = softc->blkno = (daddr_t) 0;
3352 softc->fileno = softc->blkno = (daddr_t) -1;
3357 sareservereleaseunit(struct cam_periph *periph, int reserve)
3360 struct sa_softc *softc;
3363 softc = (struct sa_softc *)periph->softc;
3364 ccb = cam_periph_getccb(periph, 1);
3366 /* It is safe to retry this operation */
3367 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3368 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3369 softc->dsreg = MTIO_DSREG_RBSY;
3370 error = cam_periph_runccb(ccb, saerror, 0,
3371 SF_RETRY_UA | SF_NO_PRINT, softc->device_stats);
3372 softc->dsreg = MTIO_DSREG_REST;
3373 xpt_release_ccb(ccb);
3376 * If the error was Illegal Request, then the device doesn't support
3377 * RESERVE/RELEASE. This is not an error.
3379 if (error == EINVAL) {
3387 saloadunload(struct cam_periph *periph, int load)
3390 struct sa_softc *softc;
3393 softc = (struct sa_softc *)periph->softc;
3395 ccb = cam_periph_getccb(periph, 1);
3397 /* It is safe to retry this operation */
3398 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3399 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3401 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3402 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3403 softc->dsreg = MTIO_DSREG_REST;
3404 xpt_release_ccb(ccb);
3406 if (error || load == 0)
3407 softc->fileno = softc->blkno = (daddr_t) -1;
3408 else if (error == 0)
3409 softc->fileno = softc->blkno = (daddr_t) 0;
3414 saerase(struct cam_periph *periph, int longerase)
3418 struct sa_softc *softc;
3421 softc = (struct sa_softc *)periph->softc;
3422 if (softc->open_rdonly)
3425 ccb = cam_periph_getccb(periph, 1);
3427 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3428 SSD_FULL_SIZE, ERASE_TIMEOUT);
3430 softc->dsreg = MTIO_DSREG_ZER;
3431 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3432 softc->dsreg = MTIO_DSREG_REST;
3434 xpt_release_ccb(ccb);
3438 #endif /* _KERNEL */
3441 * Read tape block limits command.
3444 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3445 void (*cbfcnp)(struct cam_periph *, union ccb *),
3446 u_int8_t tag_action,
3447 struct scsi_read_block_limits_data *rlimit_buf,
3448 u_int8_t sense_len, u_int32_t timeout)
3450 struct scsi_read_block_limits *scsi_cmd;
3452 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3453 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3454 sizeof(*scsi_cmd), timeout);
3456 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3457 bzero(scsi_cmd, sizeof(*scsi_cmd));
3458 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3462 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3463 void (*cbfcnp)(struct cam_periph *, union ccb *),
3464 u_int8_t tag_action, int readop, int sli,
3465 int fixed, u_int32_t length, u_int8_t *data_ptr,
3466 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3468 struct scsi_sa_rw *scsi_cmd;
3471 read = (readop & SCSI_RW_DIRMASK) == SCSI_RW_READ;
3473 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3474 scsi_cmd->opcode = read ? SA_READ : SA_WRITE;
3475 scsi_cmd->sli_fixed = 0;
3477 scsi_cmd->sli_fixed |= SAR_SLI;
3479 scsi_cmd->sli_fixed |= SARW_FIXED;
3480 scsi_ulto3b(length, scsi_cmd->length);
3481 scsi_cmd->control = 0;
3483 cam_fill_csio(csio, retries, cbfcnp, (read ? CAM_DIR_IN : CAM_DIR_OUT) |
3484 ((readop & SCSI_RW_BIO) != 0 ? CAM_DATA_BIO : 0),
3485 tag_action, data_ptr, dxfer_len, sense_len,
3486 sizeof(*scsi_cmd), timeout);
3490 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3491 void (*cbfcnp)(struct cam_periph *, union ccb *),
3492 u_int8_t tag_action, int immediate, int eot,
3493 int reten, int load, u_int8_t sense_len,
3496 struct scsi_load_unload *scsi_cmd;
3498 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3499 bzero(scsi_cmd, sizeof(*scsi_cmd));
3500 scsi_cmd->opcode = LOAD_UNLOAD;
3502 scsi_cmd->immediate = SLU_IMMED;
3504 scsi_cmd->eot_reten_load |= SLU_EOT;
3506 scsi_cmd->eot_reten_load |= SLU_RETEN;
3508 scsi_cmd->eot_reten_load |= SLU_LOAD;
3510 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3511 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3515 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3516 void (*cbfcnp)(struct cam_periph *, union ccb *),
3517 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3520 struct scsi_rewind *scsi_cmd;
3522 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3523 bzero(scsi_cmd, sizeof(*scsi_cmd));
3524 scsi_cmd->opcode = REWIND;
3526 scsi_cmd->immediate = SREW_IMMED;
3528 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3529 0, sense_len, sizeof(*scsi_cmd), timeout);
3533 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3534 void (*cbfcnp)(struct cam_periph *, union ccb *),
3535 u_int8_t tag_action, scsi_space_code code,
3536 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3538 struct scsi_space *scsi_cmd;
3540 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3541 scsi_cmd->opcode = SPACE;
3542 scsi_cmd->code = code;
3543 scsi_ulto3b(count, scsi_cmd->count);
3544 scsi_cmd->control = 0;
3546 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3547 0, sense_len, sizeof(*scsi_cmd), timeout);
3551 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3552 void (*cbfcnp)(struct cam_periph *, union ccb *),
3553 u_int8_t tag_action, int immediate, int setmark,
3554 u_int32_t num_marks, u_int8_t sense_len,
3557 struct scsi_write_filemarks *scsi_cmd;
3559 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3560 bzero(scsi_cmd, sizeof(*scsi_cmd));
3561 scsi_cmd->opcode = WRITE_FILEMARKS;
3563 scsi_cmd->byte2 |= SWFMRK_IMMED;
3565 scsi_cmd->byte2 |= SWFMRK_WSMK;
3567 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3569 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3570 0, sense_len, sizeof(*scsi_cmd), timeout);
3574 * The reserve and release unit commands differ only by their opcodes.
3577 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3578 void (*cbfcnp)(struct cam_periph *, union ccb *),
3579 u_int8_t tag_action, int third_party,
3580 int third_party_id, u_int8_t sense_len,
3581 u_int32_t timeout, int reserve)
3583 struct scsi_reserve_release_unit *scsi_cmd;
3585 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3586 bzero(scsi_cmd, sizeof(*scsi_cmd));
3589 scsi_cmd->opcode = RESERVE_UNIT;
3591 scsi_cmd->opcode = RELEASE_UNIT;
3594 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3595 scsi_cmd->lun_thirdparty |=
3596 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3599 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3600 0, sense_len, sizeof(*scsi_cmd), timeout);
3604 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3605 void (*cbfcnp)(struct cam_periph *, union ccb *),
3606 u_int8_t tag_action, int immediate, int long_erase,
3607 u_int8_t sense_len, u_int32_t timeout)
3609 struct scsi_erase *scsi_cmd;
3611 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3612 bzero(scsi_cmd, sizeof(*scsi_cmd));
3614 scsi_cmd->opcode = ERASE;
3617 scsi_cmd->lun_imm_long |= SE_IMMED;
3620 scsi_cmd->lun_imm_long |= SE_LONG;
3622 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3623 0, sense_len, sizeof(*scsi_cmd), timeout);
3627 * Read Tape Position command.
3630 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3631 void (*cbfcnp)(struct cam_periph *, union ccb *),
3632 u_int8_t tag_action, int hardsoft,
3633 struct scsi_tape_position_data *sbp,
3634 u_int8_t sense_len, u_int32_t timeout)
3636 struct scsi_tape_read_position *scmd;
3638 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3639 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3640 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3641 bzero(scmd, sizeof(*scmd));
3642 scmd->opcode = READ_POSITION;
3643 scmd->byte1 = hardsoft;
3647 * Set Tape Position command.
3650 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3651 void (*cbfcnp)(struct cam_periph *, union ccb *),
3652 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3653 u_int8_t sense_len, u_int32_t timeout)
3655 struct scsi_tape_locate *scmd;
3657 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3658 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3659 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3660 bzero(scmd, sizeof(*scmd));
3661 scmd->opcode = LOCATE;
3663 scmd->byte1 |= SA_SPOS_BT;
3664 scsi_ulto4b(blkno, scmd->blkaddr);