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 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 /* units are bits 4-7, 16-21 (1024 units) */
177 #define SAUNIT(DEV) \
178 (((minor(DEV) & 0xF0) >> 4) | ((minor(DEV) & 0x3f0000) >> 16))
180 #define SAMODE(z) ((minor(z) & 0x3))
181 #define SADENSITY(z) (((minor(z) >> 2) & 0x3))
182 #define SA_IS_CTRL(z) (minor(z) & (1 << 29))
184 #define SA_NOT_CTLDEV 0
188 #define SA_ATYPE_NR 1
189 #define SA_ATYPE_ER 2
191 #define SAMINOR(ctl, unit, mode, access) \
192 ((ctl << 29) | ((unit & 0x3f0) << 16) | ((unit & 0xf) << 4) | \
193 (mode << 0x2) | (access & 0x3))
195 #define SA_NUM_MODES 4
197 struct cdev *ctl_dev;
198 struct sa_mode_devs {
202 } mode_devs[SA_NUM_MODES];
209 struct bio_queue_head bio_queue;
211 struct devstat *device_stats;
218 u_int32_t comp_algorithm;
219 u_int32_t saved_comp_algorithm;
220 u_int32_t media_blksize;
221 u_int32_t last_media_blksize;
222 u_int32_t media_numblks;
223 u_int8_t media_density;
226 u_int8_t dsreg; /* mtio mt_dsreg, redux */
230 int last_resid_was_io;
233 * Relative to BOT Location.
242 struct scsi_sense_data _last_io_sense;
243 u_int32_t _last_io_resid;
244 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
245 struct scsi_sense_data _last_ctl_sense;
246 u_int32_t _last_ctl_resid;
247 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
248 #define last_io_sense errinfo._last_io_sense
249 #define last_io_resid errinfo._last_io_resid
250 #define last_io_cdb errinfo._last_io_cdb
251 #define last_ctl_sense errinfo._last_ctl_sense
252 #define last_ctl_resid errinfo._last_ctl_resid
253 #define last_ctl_cdb errinfo._last_ctl_cdb
256 * Misc other flags/state
260 open_rdonly : 1, /* open read-only */
261 open_pending_mount : 1, /* open pending mount */
262 ctrl_mode : 1; /* control device open */
265 struct sa_quirk_entry {
266 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
267 sa_quirks quirks; /* specific quirk type */
268 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
271 static struct sa_quirk_entry sa_quirk_table[] =
274 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
275 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
276 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
279 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
280 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
283 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
284 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
287 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
288 "Python*", "*"}, SA_QUIRK_NODREAD, 0
291 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
292 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
295 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
296 "VIPER 2525 25462", "-011"},
297 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
300 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
301 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
305 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
306 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
310 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
311 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
314 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
315 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
318 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
319 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
322 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
323 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
326 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
327 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
330 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
331 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
333 { /* jreynold@primenet.com */
334 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
335 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
337 { /* mike@sentex.net */
338 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
339 "STT20000*", "*"}, SA_QUIRK_1FM, 0
342 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
343 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
346 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
347 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
350 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
351 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
354 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
355 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
358 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
359 " SLR*", "*"}, SA_QUIRK_1FM, 0
362 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
363 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
366 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
367 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
371 static d_open_t saopen;
372 static d_close_t saclose;
373 static d_strategy_t sastrategy;
374 static d_ioctl_t saioctl;
375 static periph_init_t sainit;
376 static periph_ctor_t saregister;
377 static periph_oninv_t saoninvalidate;
378 static periph_dtor_t sacleanup;
379 static periph_start_t sastart;
380 static void saasync(void *callback_arg, u_int32_t code,
381 struct cam_path *path, void *arg);
382 static void sadone(struct cam_periph *periph,
383 union ccb *start_ccb);
384 static int saerror(union ccb *ccb, u_int32_t cam_flags,
385 u_int32_t sense_flags);
386 static int samarkswanted(struct cam_periph *);
387 static int sacheckeod(struct cam_periph *periph);
388 static int sagetparams(struct cam_periph *periph,
389 sa_params params_to_get,
390 u_int32_t *blocksize, u_int8_t *density,
391 u_int32_t *numblocks, int *buff_mode,
392 u_int8_t *write_protect, u_int8_t *speed,
393 int *comp_supported, int *comp_enabled,
394 u_int32_t *comp_algorithm,
395 sa_comp_t *comp_page);
396 static int sasetparams(struct cam_periph *periph,
397 sa_params params_to_set,
398 u_int32_t blocksize, u_int8_t density,
399 u_int32_t comp_algorithm,
400 u_int32_t sense_flags);
401 static void saprevent(struct cam_periph *periph, int action);
402 static int sarewind(struct cam_periph *periph);
403 static int saspace(struct cam_periph *periph, int count,
404 scsi_space_code code);
405 static int samount(struct cam_periph *, int, struct cdev *);
406 static int saretension(struct cam_periph *periph);
407 static int sareservereleaseunit(struct cam_periph *periph,
409 static int saloadunload(struct cam_periph *periph, int load);
410 static int saerase(struct cam_periph *periph, int longerase);
411 static int sawritefilemarks(struct cam_periph *periph,
412 int nmarks, int setmarks);
413 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
414 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
417 static struct periph_driver sadriver =
420 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
423 PERIPHDRIVER_DECLARE(sa, sadriver);
425 /* For 2.2-stable support */
431 static struct cdevsw sa_cdevsw = {
432 .d_version = D_VERSION,
436 .d_write = physwrite,
438 .d_strategy = sastrategy,
440 .d_flags = D_TAPE | D_NEEDGIANT,
444 saopen(struct cdev *dev, int flags, int fmt, struct thread *td)
446 struct cam_periph *periph;
447 struct sa_softc *softc;
455 periph = (struct cam_periph *)dev->si_drv1;
456 if (periph == NULL) {
460 softc = (struct sa_softc *)periph->softc;
461 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
467 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
468 ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
470 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
471 cam_periph_unlock(periph);
475 if (SA_IS_CTRL(dev)) {
476 softc->ctrl_mode = 1;
477 cam_periph_unlock(periph);
481 if (softc->flags & SA_FLAG_OPEN) {
483 } else if (softc->flags & SA_FLAG_INVALID) {
487 * Preserve whether this is a read_only open.
489 softc->open_rdonly = (flags & O_RDWR) == O_RDONLY;
492 * The function samount ensures media is loaded and ready.
493 * It also does a device RESERVE if the tape isn't yet mounted.
495 * If the mount fails and this was a non-blocking open,
496 * make this a 'open_pending_mount' action.
498 error = samount(periph, flags, dev);
499 if (error && (flags & O_NONBLOCK)) {
500 softc->flags |= SA_FLAG_OPEN;
501 softc->open_pending_mount = 1;
502 cam_periph_unlock(periph);
508 cam_periph_release(periph);
510 saprevent(periph, PR_PREVENT);
511 softc->flags |= SA_FLAG_OPEN;
513 cam_periph_unlock(periph);
518 saclose(struct cdev *dev, int flag, int fmt, struct thread *td)
520 struct cam_periph *periph;
521 struct sa_softc *softc;
522 int unit, mode, error, writing, tmp;
523 int closedbits = SA_FLAG_OPEN;
527 periph = (struct cam_periph *)dev->si_drv1;
531 softc = (struct sa_softc *)periph->softc;
533 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
534 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
537 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
541 softc->open_rdonly = 0;
542 if (SA_IS_CTRL(dev)) {
543 softc->ctrl_mode = 0;
544 cam_periph_release(periph);
545 cam_periph_unlock(periph);
549 if (softc->open_pending_mount) {
550 softc->flags &= ~SA_FLAG_OPEN;
551 softc->open_pending_mount = 0;
552 cam_periph_release(periph);
553 cam_periph_unlock(periph);
558 * Were we writing the tape?
560 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
563 * See whether or not we need to write filemarks. If this
564 * fails, we probably have to assume we've lost tape
567 error = sacheckeod(periph);
569 xpt_print_path(periph->path);
570 printf("failed to write terminating filemark(s)\n");
571 softc->flags |= SA_FLAG_TAPE_FROZEN;
575 * Whatever we end up doing, allow users to eject tapes from here on.
577 saprevent(periph, PR_ALLOW);
580 * Decide how to end...
582 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
583 closedbits |= SA_FLAG_TAPE_FROZEN;
584 } else switch (mode) {
585 case SA_MODE_OFFLINE:
587 * An 'offline' close is an unconditional release of
588 * frozen && mount conditions, irrespective of whether
589 * these operations succeeded. The reason for this is
590 * to allow at least some kind of programmatic way
591 * around our state getting all fouled up. If somebody
592 * issues an 'offline' command, that will be allowed
595 (void) sarewind(periph);
596 (void) saloadunload(periph, FALSE);
597 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
601 * If the rewind fails, return an error- if anyone cares,
602 * but not overwriting any previous error.
604 * We don't clear the notion of mounted here, but we do
605 * clear the notion of frozen if we successfully rewound.
607 tmp = sarewind(periph);
612 closedbits |= SA_FLAG_TAPE_FROZEN;
615 case SA_MODE_NOREWIND:
617 * If we're not rewinding/unloading the tape, find out
618 * whether we need to back up over one of two filemarks
619 * we wrote (if we wrote two filemarks) so that appends
620 * from this point on will be sane.
622 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
623 tmp = saspace(periph, -1, SS_FILEMARKS);
625 xpt_print_path(periph->path);
626 printf("unable to backspace over one of double"
627 " filemarks at end of tape\n");
628 xpt_print_path(periph->path);
629 printf("it is possible that this device"
630 " needs a SA_QUIRK_1FM quirk set for it\n");
631 softc->flags |= SA_FLAG_TAPE_FROZEN;
636 xpt_print_path(periph->path);
637 panic("unknown mode 0x%x in saclose", mode);
643 * We wish to note here that there are no more filemarks to be written.
645 softc->filemarks = 0;
646 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
649 * And we are no longer open for business.
651 softc->flags &= ~closedbits;
654 * Inform users if tape state if frozen....
656 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
657 xpt_print_path(periph->path);
658 printf("tape is now frozen- use an OFFLINE, REWIND or MTEOM "
659 "command to clear this state.\n");
662 /* release the device if it is no longer mounted */
663 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
664 sareservereleaseunit(periph, FALSE);
666 cam_periph_unlock(periph);
667 cam_periph_release(periph);
673 * Actually translate the requested transfer into one the physical driver
674 * can understand. The transfer is described by a buf and will include
675 * only one physical transfer.
678 sastrategy(struct bio *bp)
680 struct cam_periph *periph;
681 struct sa_softc *softc;
684 bp->bio_resid = bp->bio_bcount;
685 if (SA_IS_CTRL(bp->bio_dev)) {
686 biofinish(bp, NULL, EINVAL);
689 periph = (struct cam_periph *)bp->bio_dev->si_drv1;
690 if (periph == NULL) {
691 biofinish(bp, NULL, ENXIO);
694 softc = (struct sa_softc *)periph->softc;
698 if (softc->flags & SA_FLAG_INVALID) {
700 biofinish(bp, NULL, ENXIO);
704 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
706 biofinish(bp, NULL, EPERM);
711 * This should actually never occur as the write(2)
712 * system call traps attempts to write to a read-only
715 if (bp->bio_cmd == BIO_WRITE && softc->open_rdonly) {
717 biofinish(bp, NULL, EBADF);
723 if (softc->open_pending_mount) {
724 int error = samount(periph, 0, bp->bio_dev);
726 biofinish(bp, NULL, ENXIO);
729 saprevent(periph, PR_PREVENT);
730 softc->open_pending_mount = 0;
735 * If it's a null transfer, return immediately
737 if (bp->bio_bcount == 0) {
743 if (softc->flags & SA_FLAG_FIXED) {
745 * Fixed block device. The byte count must
746 * be a multiple of our block size.
748 if (((softc->blk_mask != ~0) &&
749 ((bp->bio_bcount & softc->blk_mask) != 0)) ||
750 ((softc->blk_mask == ~0) &&
751 ((bp->bio_bcount % softc->min_blk) != 0))) {
752 xpt_print_path(periph->path);
753 printf("Invalid request. Fixed block device "
754 "requests must be a multiple "
755 "of %d bytes\n", softc->min_blk);
756 biofinish(bp, NULL, EINVAL);
759 } else if ((bp->bio_bcount > softc->max_blk) ||
760 (bp->bio_bcount < softc->min_blk) ||
761 (bp->bio_bcount & softc->blk_mask) != 0) {
763 xpt_print_path(periph->path);
764 printf("Invalid request. Variable block device "
765 "requests must be ");
766 if (softc->blk_mask != 0) {
767 printf("a multiple of %d ", (0x1 << softc->blk_gran));
769 printf("between %d and %d bytes\n", softc->min_blk,
771 biofinish(bp, NULL, EINVAL);
776 * Mask interrupts so that the device cannot be invalidated until
777 * after we are in the queue. Otherwise, we might not properly
778 * clean up one of the buffers.
783 * Place it at the end of the queue.
785 bioq_insert_tail(&softc->bio_queue, bp);
786 softc->queue_count++;
788 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
789 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
790 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
791 (bp->bio_cmd == BIO_READ)? "read" : "write"));
793 if (softc->queue_count > 1) {
794 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
795 ("sastrategy: queue count now %d\n", softc->queue_count));
800 * Schedule ourselves for performing the work.
802 xpt_schedule(periph, 1);
808 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
809 if (softc->open_pending_mount) { \
810 error = samount(periph, 0, dev); \
814 saprevent(periph, PR_PREVENT); \
815 softc->open_pending_mount = 0; \
819 saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
821 struct cam_periph *periph;
822 struct sa_softc *softc;
823 scsi_space_code spaceop;
824 int didlockperiph = 0;
830 error = 0; /* shut up gcc */
831 spaceop = 0; /* shut up gcc */
833 periph = (struct cam_periph *)dev->si_drv1;
837 softc = (struct sa_softc *)periph->softc;
840 * Check for control mode accesses. We allow MTIOCGET and
841 * MTIOCERRSTAT (but need to be the only one open in order
842 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
843 * and MTCOMP (but need to be the only one accessing this
844 * device to run those).
847 if (SA_IS_CTRL(dev)) {
849 case MTIOCGETEOTMODEL:
854 * If the periph isn't already locked, lock it
855 * so our MTIOCERRSTAT can reset latched error stats.
857 * If the periph is already locked, skip it because
858 * we're just getting status and it'll be up to the
859 * other thread that has this device open to do
860 * an MTIOCERRSTAT that would clear latched status.
863 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
864 error = cam_periph_lock(periph, PRIBIO|PCATCH);
875 struct mtop *mt = (struct mtop *) arg;
878 * Check to make sure it's an OP we can perform
879 * with no media inserted.
895 case MTIOCSETEOTMODEL:
897 * We need to acquire the peripheral here rather
898 * than at open time because we are sharing writable
899 * access to data structures.
902 error = cam_periph_lock(periph, PRIBIO|PCATCH);
916 * Find the device that the user is talking about
921 struct mtget *g = (struct mtget *)arg;
924 * If this isn't the control mode device, actually go out
925 * and ask the drive again what it's set to.
927 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) {
928 u_int8_t write_protect;
929 int comp_enabled, comp_supported;
930 error = sagetparams(periph, SA_PARAM_ALL,
931 &softc->media_blksize, &softc->media_density,
932 &softc->media_numblks, &softc->buffer_mode,
933 &write_protect, &softc->speed, &comp_supported,
934 &comp_enabled, &softc->comp_algorithm, NULL);
938 softc->flags |= SA_FLAG_TAPE_WP;
940 softc->flags &= ~SA_FLAG_TAPE_WP;
941 softc->flags &= ~(SA_FLAG_COMP_SUPP|
942 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
943 if (comp_supported) {
944 if (softc->saved_comp_algorithm == 0)
945 softc->saved_comp_algorithm =
946 softc->comp_algorithm;
947 softc->flags |= SA_FLAG_COMP_SUPP;
949 softc->flags |= SA_FLAG_COMP_ENABLED;
951 softc->flags |= SA_FLAG_COMP_UNSUPP;
953 bzero(g, sizeof(struct mtget));
954 g->mt_type = MT_ISAR;
955 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
956 g->mt_comp = MT_COMP_UNSUPP;
957 g->mt_comp0 = MT_COMP_UNSUPP;
958 g->mt_comp1 = MT_COMP_UNSUPP;
959 g->mt_comp2 = MT_COMP_UNSUPP;
960 g->mt_comp3 = MT_COMP_UNSUPP;
962 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
963 g->mt_comp = MT_COMP_DISABLED;
965 g->mt_comp = softc->comp_algorithm;
967 g->mt_comp0 = softc->comp_algorithm;
968 g->mt_comp1 = softc->comp_algorithm;
969 g->mt_comp2 = softc->comp_algorithm;
970 g->mt_comp3 = softc->comp_algorithm;
972 g->mt_density = softc->media_density;
973 g->mt_density0 = softc->media_density;
974 g->mt_density1 = softc->media_density;
975 g->mt_density2 = softc->media_density;
976 g->mt_density3 = softc->media_density;
977 g->mt_blksiz = softc->media_blksize;
978 g->mt_blksiz0 = softc->media_blksize;
979 g->mt_blksiz1 = softc->media_blksize;
980 g->mt_blksiz2 = softc->media_blksize;
981 g->mt_blksiz3 = softc->media_blksize;
982 g->mt_fileno = softc->fileno;
983 g->mt_blkno = softc->blkno;
984 g->mt_dsreg = (short) softc->dsreg;
986 * Yes, we know that this is likely to overflow
988 if (softc->last_resid_was_io) {
989 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
990 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
991 softc->last_io_resid = 0;
995 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
996 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
997 softc->last_ctl_resid = 0;
1006 struct scsi_tape_errors *sep =
1007 &((union mterrstat *)arg)->scsi_errstat;
1009 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1010 ("saioctl: MTIOCERRSTAT\n"));
1012 bzero(sep, sizeof(*sep));
1013 sep->io_resid = softc->last_io_resid;
1014 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1015 sizeof (sep->io_sense));
1016 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1017 sizeof (sep->io_cdb));
1018 sep->ctl_resid = softc->last_ctl_resid;
1019 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1020 sizeof (sep->ctl_sense));
1021 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1022 sizeof (sep->ctl_cdb));
1024 if ((SA_IS_CTRL(dev) == 0 && softc->open_pending_mount) ||
1026 bzero((caddr_t) &softc->errinfo,
1027 sizeof (softc->errinfo));
1036 PENDING_MOUNT_CHECK(softc, periph, dev);
1038 mt = (struct mtop *)arg;
1041 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1042 ("saioctl: op=0x%x count=0x%x\n",
1043 mt->mt_op, mt->mt_count));
1045 count = mt->mt_count;
1046 switch (mt->mt_op) {
1047 case MTWEOF: /* write an end-of-file marker */
1049 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1050 * flag because by keeping track of filemarks
1051 * we have last written we know ehether or not
1052 * we need to write more when we close the device.
1054 error = sawritefilemarks(periph, count, FALSE);
1056 case MTWSS: /* write a setmark */
1057 error = sawritefilemarks(periph, count, TRUE);
1059 case MTBSR: /* backward space record */
1060 case MTFSR: /* forward space record */
1061 case MTBSF: /* backward space file */
1062 case MTFSF: /* forward space file */
1063 case MTBSS: /* backward space setmark */
1064 case MTFSS: /* forward space setmark */
1065 case MTEOD: /* space to end of recorded medium */
1069 spaceop = SS_FILEMARKS;
1070 nmarks = softc->filemarks;
1071 error = sacheckeod(periph);
1073 xpt_print_path(periph->path);
1074 printf("EOD check prior to spacing failed\n");
1075 softc->flags |= SA_FLAG_EIO_PENDING;
1078 nmarks -= softc->filemarks;
1084 spaceop = SS_BLOCKS;
1095 spaceop = SS_SETMARKS;
1109 nmarks = softc->filemarks;
1111 * XXX: Why are we checking again?
1113 error = sacheckeod(periph);
1116 nmarks -= softc->filemarks;
1117 error = saspace(periph, count - nmarks, spaceop);
1119 * At this point, clear that we've written the tape
1120 * and that we've written any filemarks. We really
1121 * don't know what the applications wishes to do next-
1122 * the sacheckeod's will make sure we terminated the
1123 * tape correctly if we'd been writing, but the next
1124 * action the user application takes will set again
1125 * whether we need to write filemarks.
1128 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1129 softc->filemarks = 0;
1132 case MTREW: /* rewind */
1133 PENDING_MOUNT_CHECK(softc, periph, dev);
1134 (void) sacheckeod(periph);
1135 error = sarewind(periph);
1138 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1139 softc->flags &= ~SA_FLAG_ERR_PENDING;
1140 softc->filemarks = 0;
1142 case MTERASE: /* erase */
1143 PENDING_MOUNT_CHECK(softc, periph, dev);
1144 error = saerase(periph, count);
1146 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1147 softc->flags &= ~SA_FLAG_ERR_PENDING;
1149 case MTRETENS: /* re-tension tape */
1150 PENDING_MOUNT_CHECK(softc, periph, dev);
1151 error = saretension(periph);
1153 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1154 softc->flags &= ~SA_FLAG_ERR_PENDING;
1156 case MTOFFL: /* rewind and put the drive offline */
1158 PENDING_MOUNT_CHECK(softc, periph, dev);
1160 (void) sacheckeod(periph);
1162 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1163 softc->filemarks = 0;
1165 error = sarewind(periph);
1166 /* clear the frozen flag anyway */
1167 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1170 * Be sure to allow media removal before ejecting.
1173 saprevent(periph, PR_ALLOW);
1175 error = saloadunload(periph, FALSE);
1177 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1182 case MTNOP: /* no operation, sets status only */
1183 case MTCACHE: /* enable controller cache */
1184 case MTNOCACHE: /* disable controller cache */
1188 case MTSETBSIZ: /* Set block size for device */
1190 PENDING_MOUNT_CHECK(softc, periph, dev);
1192 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1195 softc->last_media_blksize =
1196 softc->media_blksize;
1197 softc->media_blksize = count;
1199 softc->flags |= SA_FLAG_FIXED;
1200 if (powerof2(count)) {
1203 softc->blk_mask = count - 1;
1205 softc->blk_mask = ~0;
1206 softc->blk_shift = 0;
1209 * Make the user's desire 'persistent'.
1211 softc->quirks &= ~SA_QUIRK_VARIABLE;
1212 softc->quirks |= SA_QUIRK_FIXED;
1214 softc->flags &= ~SA_FLAG_FIXED;
1215 if (softc->max_blk == 0) {
1216 softc->max_blk = ~0;
1218 softc->blk_shift = 0;
1219 if (softc->blk_gran != 0) {
1221 softc->blk_gran - 1;
1223 softc->blk_mask = 0;
1226 * Make the user's desire 'persistent'.
1228 softc->quirks |= SA_QUIRK_VARIABLE;
1229 softc->quirks &= ~SA_QUIRK_FIXED;
1233 case MTSETDNSTY: /* Set density for device and mode */
1234 PENDING_MOUNT_CHECK(softc, periph, dev);
1236 if (count > UCHAR_MAX) {
1240 error = sasetparams(periph, SA_PARAM_DENSITY,
1244 case MTCOMP: /* enable compression */
1245 PENDING_MOUNT_CHECK(softc, periph, dev);
1247 * Some devices don't support compression, and
1248 * don't like it if you ask them for the
1251 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1252 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1256 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1257 0, 0, count, SF_NO_PRINT);
1269 PENDING_MOUNT_CHECK(softc, periph, dev);
1270 error = sardpos(periph, 0, (u_int32_t *) arg);
1273 PENDING_MOUNT_CHECK(softc, periph, dev);
1274 error = sardpos(periph, 1, (u_int32_t *) arg);
1277 PENDING_MOUNT_CHECK(softc, periph, dev);
1278 error = sasetpos(periph, 0, (u_int32_t *) arg);
1281 PENDING_MOUNT_CHECK(softc, periph, dev);
1282 error = sasetpos(periph, 1, (u_int32_t *) arg);
1284 case MTIOCGETEOTMODEL:
1286 if (softc->quirks & SA_QUIRK_1FM)
1290 *((u_int32_t *) arg) = mode;
1292 case MTIOCSETEOTMODEL:
1294 switch (*((u_int32_t *) arg)) {
1296 softc->quirks &= ~SA_QUIRK_2FM;
1297 softc->quirks |= SA_QUIRK_1FM;
1300 softc->quirks &= ~SA_QUIRK_1FM;
1301 softc->quirks |= SA_QUIRK_2FM;
1309 error = cam_periph_ioctl(periph, cmd, arg, saerror);
1314 * Check to see if we cleared a frozen state
1316 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1322 softc->fileno = (daddr_t) -1;
1323 softc->blkno = (daddr_t) -1;
1324 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1325 xpt_print_path(periph->path);
1326 printf("tape state now unfrozen.\n");
1332 if (didlockperiph) {
1333 cam_periph_unlock(periph);
1342 struct cam_path *path;
1345 * Install a global async callback.
1347 status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1348 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1350 if (status == CAM_REQ_CMP) {
1351 /* Register the async callbacks of interrest */
1352 struct ccb_setasync csa; /*
1353 * This is an immediate CCB,
1354 * so using the stack is OK
1356 xpt_setup_ccb(&csa.ccb_h, path, 5);
1357 csa.ccb_h.func_code = XPT_SASYNC_CB;
1358 csa.event_enable = AC_FOUND_DEVICE;
1359 csa.callback = saasync;
1360 csa.callback_arg = NULL;
1361 xpt_action((union ccb *)&csa);
1362 status = csa.ccb_h.status;
1363 xpt_free_path(path);
1366 if (status != CAM_REQ_CMP) {
1367 printf("sa: Failed to attach master async callback "
1368 "due to status 0x%x!\n", status);
1373 saoninvalidate(struct cam_periph *periph)
1375 struct sa_softc *softc;
1376 struct ccb_setasync csa;
1379 softc = (struct sa_softc *)periph->softc;
1382 * De-register any async callbacks.
1384 xpt_setup_ccb(&csa.ccb_h, periph->path,
1386 csa.ccb_h.func_code = XPT_SASYNC_CB;
1387 csa.event_enable = 0;
1388 csa.callback = saasync;
1389 csa.callback_arg = periph;
1390 xpt_action((union ccb *)&csa);
1392 softc->flags |= SA_FLAG_INVALID;
1395 * Although the oninvalidate() routines are always called at
1396 * splsoftcam, we need to be at splbio() here to keep the buffer
1397 * queue from being modified while we traverse it.
1402 * Return all queued I/O with ENXIO.
1403 * XXX Handle any transactions queued to the card
1404 * with XPT_ABORT_CCB.
1406 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1407 softc->queue_count = 0;
1410 xpt_print_path(periph->path);
1411 printf("lost device\n");
1416 sacleanup(struct cam_periph *periph)
1418 struct sa_softc *softc;
1421 softc = (struct sa_softc *)periph->softc;
1423 devstat_remove_entry(softc->device_stats);
1425 destroy_dev(softc->devs.ctl_dev);
1427 for (i = 0; i < SA_NUM_MODES; i++) {
1428 destroy_dev(softc->devs.mode_devs[i].r_dev);
1429 destroy_dev(softc->devs.mode_devs[i].nr_dev);
1430 destroy_dev(softc->devs.mode_devs[i].er_dev);
1433 xpt_print_path(periph->path);
1434 printf("removing device entry\n");
1435 free(softc, M_SCSISA);
1439 saasync(void *callback_arg, u_int32_t code,
1440 struct cam_path *path, void *arg)
1442 struct cam_periph *periph;
1444 periph = (struct cam_periph *)callback_arg;
1446 case AC_FOUND_DEVICE:
1448 struct ccb_getdev *cgd;
1451 cgd = (struct ccb_getdev *)arg;
1455 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1459 * Allocate a peripheral instance for
1460 * this device and start the probe
1463 status = cam_periph_alloc(saregister, saoninvalidate,
1465 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1466 saasync, AC_FOUND_DEVICE, cgd);
1468 if (status != CAM_REQ_CMP
1469 && status != CAM_REQ_INPROG)
1470 printf("saasync: Unable to probe new device "
1471 "due to status 0x%x\n", status);
1475 cam_periph_async(periph, code, path, arg);
1481 saregister(struct cam_periph *periph, void *arg)
1483 struct sa_softc *softc;
1484 struct ccb_setasync csa;
1485 struct ccb_getdev *cgd;
1489 cgd = (struct ccb_getdev *)arg;
1490 if (periph == NULL) {
1491 printf("saregister: periph was NULL!!\n");
1492 return (CAM_REQ_CMP_ERR);
1496 printf("saregister: no getdev CCB, can't register device\n");
1497 return (CAM_REQ_CMP_ERR);
1500 softc = (struct sa_softc *)
1501 malloc(sizeof (*softc), M_SCSISA, M_NOWAIT | M_ZERO);
1502 if (softc == NULL) {
1503 printf("saregister: Unable to probe new device. "
1504 "Unable to allocate softc\n");
1505 return (CAM_REQ_CMP_ERR);
1507 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1508 softc->state = SA_STATE_NORMAL;
1509 softc->fileno = (daddr_t) -1;
1510 softc->blkno = (daddr_t) -1;
1512 bioq_init(&softc->bio_queue);
1513 periph->softc = softc;
1516 * See if this device has any quirks.
1518 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1519 (caddr_t)sa_quirk_table,
1520 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1521 sizeof(*sa_quirk_table), scsi_inquiry_match);
1523 if (match != NULL) {
1524 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1525 softc->last_media_blksize =
1526 ((struct sa_quirk_entry *)match)->prefblk;
1528 xpt_print_path(periph->path);
1529 printf("found quirk entry %d\n", (int)
1530 (((struct sa_quirk_entry *) match) - sa_quirk_table));
1533 softc->quirks = SA_QUIRK_NONE;
1536 * The SA driver supports a blocksize, but we don't know the
1537 * blocksize until we media is inserted. So, set a flag to
1538 * indicate that the blocksize is unavailable right now.
1540 softc->device_stats = devstat_new_entry("sa", periph->unit_number, 0,
1541 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1542 DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE);
1544 softc->devs.ctl_dev = make_dev(&sa_cdevsw, SAMINOR(SA_CTLDEV,
1545 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1546 0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1547 softc->devs.ctl_dev->si_drv1 = periph;
1549 for (i = 0; i < SA_NUM_MODES; i++) {
1551 softc->devs.mode_devs[i].r_dev = make_dev(&sa_cdevsw,
1552 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R),
1553 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1554 periph->periph_name, periph->unit_number, i);
1555 softc->devs.mode_devs[i].r_dev->si_drv1 = periph;
1557 softc->devs.mode_devs[i].nr_dev = make_dev(&sa_cdevsw,
1558 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR),
1559 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1560 periph->periph_name, periph->unit_number, i);
1561 softc->devs.mode_devs[i].nr_dev->si_drv1 = periph;
1563 softc->devs.mode_devs[i].er_dev = make_dev(&sa_cdevsw,
1564 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER),
1565 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1566 periph->periph_name, periph->unit_number, i);
1567 softc->devs.mode_devs[i].er_dev->si_drv1 = periph;
1570 * Make the (well known) aliases for the first mode.
1575 alias = make_dev_alias(softc->devs.mode_devs[i].r_dev,
1576 "%s%d", periph->periph_name, periph->unit_number);
1577 alias->si_drv1 = periph;
1578 alias = make_dev_alias(softc->devs.mode_devs[i].nr_dev,
1579 "n%s%d", periph->periph_name, periph->unit_number);
1580 alias->si_drv1 = periph;
1581 alias = make_dev_alias(softc->devs.mode_devs[i].er_dev,
1582 "e%s%d", periph->periph_name, periph->unit_number);
1583 alias->si_drv1 = periph;
1588 * Add an async callback so that we get
1589 * notified if this device goes away.
1591 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
1592 csa.ccb_h.func_code = XPT_SASYNC_CB;
1593 csa.event_enable = AC_LOST_DEVICE;
1594 csa.callback = saasync;
1595 csa.callback_arg = periph;
1596 xpt_action((union ccb *)&csa);
1598 xpt_announce_periph(periph, NULL);
1600 return (CAM_REQ_CMP);
1604 sastart(struct cam_periph *periph, union ccb *start_ccb)
1606 struct sa_softc *softc;
1608 softc = (struct sa_softc *)periph->softc;
1610 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
1613 switch (softc->state) {
1614 case SA_STATE_NORMAL:
1616 /* Pull a buffer from the queue and get going on it */
1621 * See if there is a buf with work for us to do..
1624 bp = bioq_first(&softc->bio_queue);
1625 if (periph->immediate_priority <= periph->pinfo.priority) {
1626 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1627 ("queuing for immediate ccb\n"));
1628 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1629 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1631 periph->immediate_priority = CAM_PRIORITY_NONE;
1633 wakeup(&periph->ccb_list);
1634 } else if (bp == NULL) {
1636 xpt_release_ccb(start_ccb);
1637 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1638 struct bio *done_bp;
1640 softc->queue_count--;
1641 bioq_remove(&softc->bio_queue, bp);
1642 bp->bio_resid = bp->bio_bcount;
1644 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1646 * We now just clear errors in this case
1647 * and let the residual be the notifier.
1650 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1652 * This can only happen if we're reading
1653 * in fixed length mode. In this case,
1654 * we dump the rest of the list the
1658 if (bioq_first(&softc->bio_queue) != NULL) {
1662 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1663 bp->bio_error = EIO;
1664 bp->bio_flags |= BIO_ERROR;
1666 bp = bioq_first(&softc->bio_queue);
1668 * Only if we have no other buffers queued up
1669 * do we clear the pending error flag.
1672 softc->flags &= ~SA_FLAG_ERR_PENDING;
1673 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1674 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
1675 "%d more buffers queued up\n",
1676 (softc->flags & SA_FLAG_ERR_PENDING),
1677 (bp != NULL)? "not " : " ", softc->queue_count));
1679 xpt_release_ccb(start_ccb);
1684 bioq_remove(&softc->bio_queue, bp);
1685 softc->queue_count--;
1687 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1688 if (softc->blk_shift != 0) {
1690 bp->bio_bcount >> softc->blk_shift;
1691 } else if (softc->media_blksize != 0) {
1692 length = bp->bio_bcount /
1693 softc->media_blksize;
1695 bp->bio_error = EIO;
1696 xpt_print_path(periph->path);
1697 printf("zero blocksize for "
1698 "FIXED length writes?\n");
1704 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1705 ("issuing a %d fixed record %s\n",
1706 length, (bp->bio_cmd == BIO_READ)? "read" :
1710 length = bp->bio_bcount;
1712 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1713 ("issuing a %d variable byte %s\n",
1714 length, (bp->bio_cmd == BIO_READ)? "read" :
1718 devstat_start_transaction_bio(softc->device_stats, bp);
1720 * Some people have theorized that we should
1721 * suppress illegal length indication if we are
1722 * running in variable block mode so that we don't
1723 * have to request sense every time our requested
1724 * block size is larger than the written block.
1725 * The residual information from the ccb allows
1726 * us to identify this situation anyway. The only
1727 * problem with this is that we will not get
1728 * information about blocks that are larger than
1729 * our read buffer unless we set the block size
1730 * in the mode page to something other than 0.
1732 * I believe that this is a non-issue. If user apps
1733 * don't adjust their read size to match our record
1734 * size, that's just life. Anyway, the typical usage
1735 * would be to issue, e.g., 64KB reads and occasionally
1736 * have to do deal with 512 byte or 1KB intermediate
1739 softc->dsreg = (bp->bio_cmd == BIO_READ)?
1740 MTIO_DSREG_RD : MTIO_DSREG_WR;
1741 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1742 MSG_SIMPLE_Q_TAG, (bp->bio_cmd == BIO_READ),
1743 FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1744 length, bp->bio_data, bp->bio_bcount, SSD_FULL_SIZE,
1746 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1747 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1748 start_ccb->ccb_h.ccb_bp = bp;
1749 bp = bioq_first(&softc->bio_queue);
1751 xpt_action(start_ccb);
1755 /* Have more work to do, so ensure we stay scheduled */
1756 xpt_schedule(periph, 1);
1760 case SA_STATE_ABNORMAL:
1762 panic("state 0x%x in sastart", softc->state);
1769 sadone(struct cam_periph *periph, union ccb *done_ccb)
1771 struct sa_softc *softc;
1772 struct ccb_scsiio *csio;
1774 softc = (struct sa_softc *)periph->softc;
1775 csio = &done_ccb->csio;
1776 switch (CCB_Type(csio)) {
1777 case SA_CCB_BUFFER_IO:
1782 softc->dsreg = MTIO_DSREG_REST;
1783 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1785 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1786 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1788 * A retry was scheduled, so just return.
1798 * Catastrophic error. Mark the tape as frozen
1799 * (we no longer know tape position).
1801 * Return all queued I/O with EIO, and unfreeze
1802 * our queue so that future transactions that
1803 * attempt to fix this problem can get to the
1809 softc->flags |= SA_FLAG_TAPE_FROZEN;
1810 bioq_flush(&softc->bio_queue, NULL, EIO);
1814 bp->bio_resid = bp->bio_bcount;
1815 bp->bio_error = error;
1816 bp->bio_flags |= BIO_ERROR;
1818 * In the error case, position is updated in saerror.
1821 bp->bio_resid = csio->resid;
1823 if (csio->resid != 0) {
1824 bp->bio_flags |= BIO_ERROR;
1826 if (bp->bio_cmd == BIO_WRITE) {
1827 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1828 softc->filemarks = 0;
1830 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1831 (softc->blkno != (daddr_t) -1)) {
1832 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1834 if (softc->blk_shift != 0) {
1835 l = bp->bio_bcount >>
1838 l = bp->bio_bcount /
1839 softc->media_blksize;
1841 softc->blkno += (daddr_t) l;
1848 * If we had an error (immediate or pending),
1849 * release the device queue now.
1851 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1852 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1854 if (error || bp->bio_resid) {
1855 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1856 ("error %d resid %ld count %ld\n", error,
1857 bp->bio_resid, bp->bio_bcount));
1860 biofinish(bp, softc->device_stats, 0);
1863 case SA_CCB_WAITING:
1865 /* Caller will release the CCB */
1866 wakeup(&done_ccb->ccb_h.cbfcnp);
1870 xpt_release_ccb(done_ccb);
1874 * Mount the tape (make sure it's ready for I/O).
1877 samount(struct cam_periph *periph, int oflags, struct cdev *dev)
1879 struct sa_softc *softc;
1884 * oflags can be checked for 'kind' of open (read-only check) - later
1885 * dev can be checked for a control-mode or compression open - later
1887 UNUSED_PARAMETER(oflags);
1888 UNUSED_PARAMETER(dev);
1891 softc = (struct sa_softc *)periph->softc;
1894 * This should determine if something has happend since the last
1895 * open/mount that would invalidate the mount. We do *not* want
1896 * to retry this command- we just want the status. But we only
1897 * do this if we're mounted already- if we're not mounted,
1898 * we don't care about the unit read state and can instead use
1899 * this opportunity to attempt to reserve the tape unit.
1902 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1903 ccb = cam_periph_getccb(periph, 1);
1904 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1905 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1906 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1907 softc->device_stats);
1909 if (error == ENXIO) {
1910 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1911 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1912 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1913 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1914 softc->device_stats);
1918 * We don't need to freeze the tape because we
1919 * will now attempt to rewind/load it.
1921 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1922 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
1923 xpt_print_path(periph->path);
1924 printf("error %d on TUR in samount\n", error);
1928 error = sareservereleaseunit(periph, TRUE);
1932 ccb = cam_periph_getccb(periph, 1);
1933 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1934 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1935 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1936 softc->device_stats);
1940 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1941 struct scsi_read_block_limits_data *rblim = NULL;
1942 int comp_enabled, comp_supported;
1943 u_int8_t write_protect, guessing = 0;
1946 * Clear out old state.
1948 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1949 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1950 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1951 softc->filemarks = 0;
1954 * *Very* first off, make sure we're loaded to BOT.
1956 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1957 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1958 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1959 softc->device_stats);
1963 * In case this doesn't work, do a REWIND instead
1966 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1967 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1968 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1969 softc->device_stats);
1973 xpt_release_ccb(ccb);
1978 * Do a dummy test read to force access to the
1979 * media so that the drive will really know what's
1980 * there. We actually don't really care what the
1981 * blocksize on tape is and don't expect to really
1982 * read a full record.
1984 rblim = (struct scsi_read_block_limits_data *)
1985 malloc(8192, M_SCSISA, M_WAITOK);
1986 if (rblim == NULL) {
1987 xpt_print_path(periph->path);
1988 printf("no memory for test read\n");
1989 xpt_release_ccb(ccb);
1994 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1995 scsi_sa_read_write(&ccb->csio, 0, sadone,
1996 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1997 (void *) rblim, 8192, SSD_FULL_SIZE,
1999 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2000 softc->device_stats);
2002 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
2003 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
2004 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
2005 SF_NO_PRINT | SF_RETRY_UA,
2006 softc->device_stats);
2009 xpt_print_path(periph->path);
2010 printf("unable to rewind after test read\n");
2011 xpt_release_ccb(ccb);
2017 * Next off, determine block limits.
2019 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2020 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2022 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
2023 SF_NO_PRINT | SF_RETRY_UA, softc->device_stats);
2026 xpt_release_ccb(ccb);
2030 * If it's less than SCSI-2, READ BLOCK LIMITS is not
2031 * a MANDATORY command. Anyway- it doesn't matter-
2032 * we can proceed anyway.
2034 softc->blk_gran = 0;
2035 softc->max_blk = ~0;
2038 if (softc->scsi_rev >= SCSI_REV_SPC) {
2039 softc->blk_gran = RBL_GRAN(rblim);
2041 softc->blk_gran = 0;
2044 * We take max_blk == min_blk to mean a default to
2045 * fixed mode- but note that whatever we get out of
2046 * sagetparams below will actually determine whether
2047 * we are actually *in* fixed mode.
2049 softc->max_blk = scsi_3btoul(rblim->maximum);
2050 softc->min_blk = scsi_2btoul(rblim->minimum);
2055 * Next, perform a mode sense to determine
2056 * current density, blocksize, compression etc.
2058 error = sagetparams(periph, SA_PARAM_ALL,
2059 &softc->media_blksize,
2060 &softc->media_density,
2061 &softc->media_numblks,
2062 &softc->buffer_mode, &write_protect,
2063 &softc->speed, &comp_supported,
2064 &comp_enabled, &softc->comp_algorithm,
2069 * We could work a little harder here. We could
2070 * adjust our attempts to get information. It
2071 * might be an ancient tape drive. If someone
2072 * nudges us, we'll do that.
2078 * If no quirk has determined that this is a device that is
2079 * preferred to be in fixed or variable mode, now is the time
2082 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2085 * This could be expensive to find out. Luckily we
2086 * only need to do this once. If we start out in
2087 * 'default' mode, try and set ourselves to one
2088 * of the densities that would determine a wad
2089 * of other stuff. Go from highest to lowest.
2091 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2093 static u_int8_t ctry[] = {
2094 SCSI_DENSITY_HALFINCH_PE,
2095 SCSI_DENSITY_HALFINCH_6250C,
2096 SCSI_DENSITY_HALFINCH_6250,
2097 SCSI_DENSITY_HALFINCH_1600,
2098 SCSI_DENSITY_HALFINCH_800,
2099 SCSI_DENSITY_QIC_4GB,
2100 SCSI_DENSITY_QIC_2GB,
2101 SCSI_DENSITY_QIC_525_320,
2102 SCSI_DENSITY_QIC_150,
2103 SCSI_DENSITY_QIC_120,
2104 SCSI_DENSITY_QIC_24,
2105 SCSI_DENSITY_QIC_11_9TRK,
2106 SCSI_DENSITY_QIC_11_4TRK,
2107 SCSI_DENSITY_QIC_1320,
2108 SCSI_DENSITY_QIC_3080,
2111 for (i = 0; ctry[i]; i++) {
2112 error = sasetparams(periph,
2113 SA_PARAM_DENSITY, 0, ctry[i],
2116 softc->media_density = ctry[i];
2121 switch (softc->media_density) {
2122 case SCSI_DENSITY_QIC_11_4TRK:
2123 case SCSI_DENSITY_QIC_11_9TRK:
2124 case SCSI_DENSITY_QIC_24:
2125 case SCSI_DENSITY_QIC_120:
2126 case SCSI_DENSITY_QIC_150:
2127 case SCSI_DENSITY_QIC_525_320:
2128 case SCSI_DENSITY_QIC_1320:
2129 case SCSI_DENSITY_QIC_3080:
2130 softc->quirks &= ~SA_QUIRK_2FM;
2131 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2132 softc->last_media_blksize = 512;
2134 case SCSI_DENSITY_QIC_4GB:
2135 case SCSI_DENSITY_QIC_2GB:
2136 softc->quirks &= ~SA_QUIRK_2FM;
2137 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2138 softc->last_media_blksize = 1024;
2141 softc->last_media_blksize =
2142 softc->media_blksize;
2143 softc->quirks |= SA_QUIRK_VARIABLE;
2149 * If no quirk has determined that this is a device that needs
2150 * to have 2 Filemarks at EOD, now is the time to find out.
2153 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2154 switch (softc->media_density) {
2155 case SCSI_DENSITY_HALFINCH_800:
2156 case SCSI_DENSITY_HALFINCH_1600:
2157 case SCSI_DENSITY_HALFINCH_6250:
2158 case SCSI_DENSITY_HALFINCH_6250C:
2159 case SCSI_DENSITY_HALFINCH_PE:
2160 softc->quirks &= ~SA_QUIRK_1FM;
2161 softc->quirks |= SA_QUIRK_2FM;
2169 * Now validate that some info we got makes sense.
2171 if ((softc->max_blk < softc->media_blksize) ||
2172 (softc->min_blk > softc->media_blksize &&
2173 softc->media_blksize)) {
2174 xpt_print_path(periph->path);
2175 printf("BLOCK LIMITS (%d..%d) could not match current "
2176 "block settings (%d)- adjusting\n", softc->min_blk,
2177 softc->max_blk, softc->media_blksize);
2178 softc->max_blk = softc->min_blk =
2179 softc->media_blksize;
2183 * Now put ourselves into the right frame of mind based
2188 * If we want to be in FIXED mode and our current blocksize
2189 * is not equal to our last blocksize (if nonzero), try and
2190 * set ourselves to this last blocksize (as the 'preferred'
2191 * block size). The initial quirkmatch at registry sets the
2192 * initial 'last' blocksize. If, for whatever reason, this
2193 * 'last' blocksize is zero, set the blocksize to 512,
2194 * or min_blk if that's larger.
2196 if ((softc->quirks & SA_QUIRK_FIXED) &&
2197 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2198 (softc->media_blksize != softc->last_media_blksize)) {
2199 softc->media_blksize = softc->last_media_blksize;
2200 if (softc->media_blksize == 0) {
2201 softc->media_blksize = 512;
2202 if (softc->media_blksize < softc->min_blk) {
2203 softc->media_blksize = softc->min_blk;
2206 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2207 softc->media_blksize, 0, 0, SF_NO_PRINT);
2209 xpt_print_path(periph->path);
2210 printf("unable to set fixed blocksize to %d\n",
2211 softc->media_blksize);
2216 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2217 (softc->media_blksize != 0)) {
2218 softc->last_media_blksize = softc->media_blksize;
2219 softc->media_blksize = 0;
2220 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2221 0, 0, 0, SF_NO_PRINT);
2224 * If this fails and we were guessing, just
2225 * assume that we got it wrong and go try
2226 * fixed block mode. Don't even check against
2227 * density code at this point.
2230 softc->quirks &= ~SA_QUIRK_VARIABLE;
2231 softc->quirks |= SA_QUIRK_FIXED;
2232 if (softc->last_media_blksize == 0)
2233 softc->last_media_blksize = 512;
2236 xpt_print_path(periph->path);
2237 printf("unable to set variable blocksize\n");
2243 * Now that we have the current block size,
2244 * set up some parameters for sastart's usage.
2246 if (softc->media_blksize) {
2247 softc->flags |= SA_FLAG_FIXED;
2248 if (powerof2(softc->media_blksize)) {
2250 ffs(softc->media_blksize) - 1;
2251 softc->blk_mask = softc->media_blksize - 1;
2253 softc->blk_mask = ~0;
2254 softc->blk_shift = 0;
2258 * The SCSI-3 spec allows 0 to mean "unspecified".
2259 * The SCSI-1 spec allows 0 to mean 'infinite'.
2261 * Either works here.
2263 if (softc->max_blk == 0) {
2264 softc->max_blk = ~0;
2266 softc->blk_shift = 0;
2267 if (softc->blk_gran != 0) {
2268 softc->blk_mask = softc->blk_gran - 1;
2270 softc->blk_mask = 0;
2275 softc->flags |= SA_FLAG_TAPE_WP;
2277 if (comp_supported) {
2278 if (softc->saved_comp_algorithm == 0)
2279 softc->saved_comp_algorithm =
2280 softc->comp_algorithm;
2281 softc->flags |= SA_FLAG_COMP_SUPP;
2283 softc->flags |= SA_FLAG_COMP_ENABLED;
2285 softc->flags |= SA_FLAG_COMP_UNSUPP;
2287 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2288 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2289 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2292 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2294 xpt_print_path(periph->path);
2295 printf("unable to set buffered mode\n");
2297 error = 0; /* not an error */
2302 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2306 free(rblim, M_SCSISA);
2309 softc->dsreg = MTIO_DSREG_NIL;
2311 softc->fileno = softc->blkno = 0;
2312 softc->dsreg = MTIO_DSREG_REST;
2314 #ifdef SA_1FM_AT_EOD
2315 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2316 softc->quirks |= SA_QUIRK_1FM;
2318 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2319 softc->quirks |= SA_QUIRK_2FM;
2322 xpt_release_ccb(ccb);
2325 * If we return an error, we're not mounted any more,
2326 * so release any device reservation.
2329 (void) sareservereleaseunit(periph, FALSE);
2332 * Clear I/O residual.
2334 softc->last_io_resid = 0;
2335 softc->last_ctl_resid = 0;
2341 * How many filemarks do we need to write if we were to terminate the
2342 * tape session right now? Note that this can be a negative number
2346 samarkswanted(struct cam_periph *periph)
2349 struct sa_softc *softc;
2351 softc = (struct sa_softc *)periph->softc;
2353 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2355 if (softc->quirks & SA_QUIRK_2FM)
2358 markswanted -= softc->filemarks;
2359 return (markswanted);
2363 sacheckeod(struct cam_periph *periph)
2368 markswanted = samarkswanted(periph);
2370 if (markswanted > 0) {
2371 error = sawritefilemarks(periph, markswanted, FALSE);
2379 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2381 static const char *toobig =
2382 "%d-byte tape record bigger than supplied buffer\n";
2383 struct cam_periph *periph;
2384 struct sa_softc *softc;
2385 struct ccb_scsiio *csio;
2386 struct scsi_sense_data *sense;
2387 u_int32_t resid = 0;
2390 int error_code, sense_key, asc, ascq, error, aqvalid;
2392 periph = xpt_path_periph(ccb->ccb_h.path);
2393 softc = (struct sa_softc *)periph->softc;
2395 sense = &csio->sense_data;
2396 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2397 aqvalid = sense->extra_len >= 6;
2400 status = csio->ccb_h.status & CAM_STATUS_MASK;
2403 * Calculate/latch up, any residuals... We do this in a funny 2-step
2404 * so we can print stuff here if we have CAM_DEBUG enabled for this
2407 if (status == CAM_SCSI_STATUS_ERROR) {
2408 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2409 info = (int32_t) scsi_4btoul(sense->info);
2411 if ((softc->flags & SA_FLAG_FIXED) != 0)
2412 resid *= softc->media_blksize;
2414 resid = csio->dxfer_len;
2416 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2417 if (softc->media_blksize)
2418 info /= softc->media_blksize;
2421 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2422 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2423 sizeof (struct scsi_sense_data));
2424 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2425 (int) csio->cdb_len);
2426 softc->last_io_resid = resid;
2427 softc->last_resid_was_io = 1;
2429 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2430 sizeof (struct scsi_sense_data));
2431 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2432 (int) csio->cdb_len);
2433 softc->last_ctl_resid = resid;
2434 softc->last_resid_was_io = 0;
2436 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2437 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %d "
2438 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2439 sense_key, asc, ascq, status,
2440 sense->flags & ~SSD_KEY_RESERVED, resid, csio->dxfer_len));
2442 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2443 ("Cam Status 0x%x\n", status));
2449 case CAM_SCSI_STATUS_ERROR:
2451 * If a read/write command, we handle it here.
2453 if (CCB_Type(csio) != SA_CCB_WAITING) {
2457 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2458 * on a non read/write command, we assume it's not an error
2459 * and propagate the residule and return.
2461 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2462 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2463 csio->resid = resid;
2468 * Otherwise, we let the common code handle this.
2470 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2474 * We cannot depend upon CAM honoring retry counts for these.
2476 case CAM_SCSI_BUS_RESET:
2478 if (ccb->ccb_h.retry_count <= 0) {
2483 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2487 * Handle filemark, end of tape, mismatched record sizes....
2488 * From this point out, we're only handling read/write cases.
2489 * Handle writes && reads differently.
2492 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2493 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2494 csio->resid = resid;
2496 } else if (sense->flags & SSD_EOM) {
2497 softc->flags |= SA_FLAG_EOM_PENDING;
2499 * Grotesque as it seems, the few times
2500 * I've actually seen a non-zero resid,
2501 * the tape drive actually lied and had
2502 * written all the data!.
2507 csio->resid = resid;
2508 if (sense_key == SSD_KEY_BLANK_CHECK) {
2509 if (softc->quirks & SA_QUIRK_1FM) {
2511 softc->flags |= SA_FLAG_EOM_PENDING;
2515 } else if (sense->flags & SSD_FILEMARK) {
2516 if (softc->flags & SA_FLAG_FIXED) {
2518 softc->flags |= SA_FLAG_EOF_PENDING;
2521 * Unconditionally, if we detected a filemark on a read,
2522 * mark that we've run moved a file ahead.
2524 if (softc->fileno != (daddr_t) -1) {
2527 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2533 * Incorrect Length usually applies to read, but can apply to writes.
2535 if (error == 0 && (sense->flags & SSD_ILI)) {
2537 xpt_print_path(csio->ccb_h.path);
2538 printf(toobig, csio->dxfer_len - info);
2539 csio->resid = csio->dxfer_len;
2542 csio->resid = resid;
2543 if (softc->flags & SA_FLAG_FIXED) {
2544 softc->flags |= SA_FLAG_EIO_PENDING;
2547 * Bump the block number if we hadn't seen a filemark.
2548 * Do this independent of errors (we've moved anyway).
2550 if ((sense->flags & SSD_FILEMARK) == 0) {
2551 if (softc->blkno != (daddr_t) -1) {
2553 csio->ccb_h.ccb_pflags |=
2554 SA_POSITION_UPDATED;
2562 * Unfreeze the queue if frozen as we're not returning anything
2563 * to our waiters that would indicate an I/O error has occurred
2573 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2574 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2575 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2576 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2581 struct scsi_mode_header_6 *mode_hdr;
2582 struct scsi_mode_blk_desc *mode_blk;
2583 int mode_buffer_len;
2584 struct sa_softc *softc;
2589 softc = (struct sa_softc *)periph->softc;
2590 ccb = cam_periph_getccb(periph, 1);
2591 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2592 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2594 cpage = SA_DATA_COMPRESSION_PAGE;
2597 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2599 if (params_to_get & SA_PARAM_COMPRESSION) {
2600 if (softc->quirks & SA_QUIRK_NOCOMP) {
2601 *comp_supported = FALSE;
2602 params_to_get &= ~SA_PARAM_COMPRESSION;
2604 mode_buffer_len += sizeof (sa_comp_t);
2607 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_WAITOK | M_ZERO);
2608 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2609 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2611 /* it is safe to retry this */
2612 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2613 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2614 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2615 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2617 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2618 softc->device_stats);
2621 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2623 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2625 * Hmm. Let's see if we can try another page...
2626 * If we've already done that, give up on compression
2627 * for this device and remember this for the future
2628 * and attempt the request without asking for compression
2631 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2632 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2635 softc->quirks |= SA_QUIRK_NOCOMP;
2636 free(mode_buffer, M_SCSISA);
2638 } else if (status == CAM_SCSI_STATUS_ERROR) {
2639 /* Tell the user about the fatal error. */
2640 scsi_sense_print(&ccb->csio);
2641 goto sagetparamsexit;
2645 * If the user only wants the compression information, and
2646 * the device doesn't send back the block descriptor, it's
2647 * no big deal. If the user wants more than just
2648 * compression, though, and the device doesn't pass back the
2649 * block descriptor, we need to send another mode sense to
2650 * get the block descriptor.
2652 if ((mode_hdr->blk_desc_len == 0) &&
2653 (params_to_get & SA_PARAM_COMPRESSION) &&
2654 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2657 * Decrease the mode buffer length by the size of
2658 * the compression page, to make sure the data
2659 * there doesn't get overwritten.
2661 mode_buffer_len -= sizeof (sa_comp_t);
2664 * Now move the compression page that we presumably
2665 * got back down the memory chunk a little bit so
2666 * it doesn't get spammed.
2668 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2669 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2672 * Now, we issue another mode sense and just ask
2673 * for the block descriptor, etc.
2676 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2677 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2678 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2681 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2682 softc->device_stats);
2686 goto sagetparamsexit;
2689 if (params_to_get & SA_PARAM_BLOCKSIZE)
2690 *blocksize = scsi_3btoul(mode_blk->blklen);
2692 if (params_to_get & SA_PARAM_NUMBLOCKS)
2693 *numblocks = scsi_3btoul(mode_blk->nblocks);
2695 if (params_to_get & SA_PARAM_BUFF_MODE)
2696 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2698 if (params_to_get & SA_PARAM_DENSITY)
2699 *density = mode_blk->density;
2701 if (params_to_get & SA_PARAM_WP)
2702 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2704 if (params_to_get & SA_PARAM_SPEED)
2705 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2707 if (params_to_get & SA_PARAM_COMPRESSION) {
2708 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2709 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2710 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2712 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2714 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2715 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2717 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2719 * We don't really know whether this device supports
2720 * Data Compression if the the algorithm field is
2721 * zero. Just say we do.
2723 *comp_supported = TRUE;
2725 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2726 *comp_algorithm = cp->sel_comp_alg;
2729 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2732 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2734 char *xyz = mode_buffer;
2735 xpt_print_path(periph->path);
2736 printf("Mode Sense Data=");
2737 for (idx = 0; idx < mode_buffer_len; idx++)
2738 printf(" 0x%02x", xyz[idx] & 0xff);
2744 xpt_release_ccb(ccb);
2745 free(mode_buffer, M_SCSISA);
2750 * The purpose of this function is to set one of four different parameters
2754 * - compression / compression algorithm
2757 * The assumption is that this will be called from saioctl(), and therefore
2758 * from a process context. Thus the waiting malloc calls below. If that
2759 * assumption ever changes, the malloc calls should be changed to be
2762 * Any or all of the four parameters may be set when this function is
2763 * called. It should handle setting more than one parameter at once.
2766 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2767 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2768 u_int32_t sense_flags)
2770 struct sa_softc *softc;
2771 u_int32_t current_blocksize;
2772 u_int32_t current_calg;
2773 u_int8_t current_density;
2774 u_int8_t current_speed;
2775 int comp_enabled, comp_supported;
2777 int mode_buffer_len;
2778 struct scsi_mode_header_6 *mode_hdr;
2779 struct scsi_mode_blk_desc *mode_blk;
2780 sa_comp_t *ccomp, *cpage;
2782 union ccb *ccb = NULL;
2785 softc = (struct sa_softc *)periph->softc;
2787 ccomp = malloc(sizeof (sa_comp_t), M_SCSISA, M_WAITOK);
2790 * Since it doesn't make sense to set the number of blocks, or
2791 * write protection, we won't try to get the current value. We
2792 * always want to get the blocksize, so we can set it back to the
2795 error = sagetparams(periph,
2796 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2797 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2798 ¤t_speed, &comp_supported, &comp_enabled,
2799 ¤t_calg, ccomp);
2802 free(ccomp, M_SCSISA);
2806 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2807 if (params_to_set & SA_PARAM_COMPRESSION)
2808 mode_buffer_len += sizeof (sa_comp_t);
2810 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_WAITOK | M_ZERO);
2812 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2813 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2815 ccb = cam_periph_getccb(periph, 1);
2819 if (params_to_set & SA_PARAM_COMPRESSION) {
2821 cpage = (sa_comp_t *)&mode_blk[1];
2823 cpage = (sa_comp_t *)&mode_hdr[1];
2825 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2826 cpage->hdr.pagecode &= ~0x80;
2831 * If the caller wants us to set the blocksize, use the one they
2832 * pass in. Otherwise, use the blocksize we got back from the
2833 * mode select above.
2836 if (params_to_set & SA_PARAM_BLOCKSIZE)
2837 scsi_ulto3b(blocksize, mode_blk->blklen);
2839 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2842 * Set density if requested, else preserve old density.
2843 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2844 * devices, else density we've latched up in our softc.
2846 if (params_to_set & SA_PARAM_DENSITY) {
2847 mode_blk->density = density;
2848 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2849 mode_blk->density = SCSI_SAME_DENSITY;
2851 mode_blk->density = softc->media_density;
2856 * For mode selects, these two fields must be zero.
2858 mode_hdr->data_length = 0;
2859 mode_hdr->medium_type = 0;
2861 /* set the speed to the current value */
2862 mode_hdr->dev_spec = current_speed;
2864 /* if set, set single-initiator buffering mode */
2865 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
2866 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2870 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2872 mode_hdr->blk_desc_len = 0;
2875 * First, if the user wants us to set the compression algorithm or
2876 * just turn compression on, check to make sure that this drive
2877 * supports compression.
2879 if (params_to_set & SA_PARAM_COMPRESSION) {
2881 * If the compression algorithm is 0, disable compression.
2882 * If the compression algorithm is non-zero, enable
2883 * compression and set the compression type to the
2884 * specified compression algorithm, unless the algorithm is
2885 * MT_COMP_ENABLE. In that case, we look at the
2886 * compression algorithm that is currently set and if it is
2887 * non-zero, we leave it as-is. If it is zero, and we have
2888 * saved a compression algorithm from a time when
2889 * compression was enabled before, set the compression to
2892 switch (ccomp->hdr.pagecode & ~0x80) {
2893 case SA_DEVICE_CONFIGURATION_PAGE:
2895 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2897 dcp->sel_comp_alg = SA_COMP_NONE;
2900 if (calg != MT_COMP_ENABLE) {
2901 dcp->sel_comp_alg = calg;
2902 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2903 softc->saved_comp_algorithm != 0) {
2904 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2908 case SA_DATA_COMPRESSION_PAGE:
2909 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2910 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2913 * Disable compression, but leave the
2914 * decompression and the capability bit
2917 dcp->dce_and_dcc = SA_DCP_DCC;
2918 dcp->dde_and_red |= SA_DCP_DDE;
2921 /* enable compression && decompression */
2922 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2923 dcp->dde_and_red |= SA_DCP_DDE;
2925 * If there, use compression algorithm from caller.
2926 * Otherwise, if there's a saved compression algorithm
2927 * and there is no current algorithm, use the saved
2928 * algorithm. Else parrot back what we got and hope
2931 if (calg != MT_COMP_ENABLE) {
2932 scsi_ulto4b(calg, dcp->comp_algorithm);
2933 scsi_ulto4b(calg, dcp->decomp_algorithm);
2934 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2935 softc->saved_comp_algorithm != 0) {
2936 scsi_ulto4b(softc->saved_comp_algorithm,
2937 dcp->comp_algorithm);
2938 scsi_ulto4b(softc->saved_comp_algorithm,
2939 dcp->decomp_algorithm);
2944 * Compression does not appear to be supported-
2945 * at least via the DATA COMPRESSION page. It
2946 * would be too much to ask us to believe that
2947 * the page itself is supported, but incorrectly
2948 * reports an ability to manipulate data compression,
2949 * so we'll assume that this device doesn't support
2950 * compression. We can just fall through for that.
2955 * The drive doesn't seem to support compression,
2956 * so turn off the set compression bit.
2958 params_to_set &= ~SA_PARAM_COMPRESSION;
2959 xpt_print_path(periph->path);
2960 printf("device does not seem to support compression\n");
2963 * If that was the only thing the user wanted us to set,
2964 * clean up allocated resources and return with
2965 * 'operation not supported'.
2967 if (params_to_set == SA_PARAM_NONE) {
2968 free(mode_buffer, M_SCSISA);
2969 xpt_release_ccb(ccb);
2974 * That wasn't the only thing the user wanted us to set.
2975 * So, decrease the stated mode buffer length by the
2976 * size of the compression mode page.
2978 mode_buffer_len -= sizeof(sa_comp_t);
2982 /* It is safe to retry this operation */
2983 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2984 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2985 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2987 error = cam_periph_runccb(ccb, saerror, 0,
2988 sense_flags, softc->device_stats);
2991 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2993 char *xyz = mode_buffer;
2994 xpt_print_path(periph->path);
2995 printf("Err%d, Mode Select Data=", error);
2996 for (idx = 0; idx < mode_buffer_len; idx++)
2997 printf(" 0x%02x", xyz[idx] & 0xff);
3004 * If we can, try without setting density/blocksize.
3007 if ((params_to_set &
3008 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
3013 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
3014 cpage = (sa_comp_t *)&mode_blk[1];
3018 * If we were setting the blocksize, and that failed, we
3019 * want to set it to its original value. If we weren't
3020 * setting the blocksize, we don't want to change it.
3022 scsi_ulto3b(current_blocksize, mode_blk->blklen);
3025 * Set density if requested, else preserve old density.
3026 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
3027 * devices, else density we've latched up in our softc.
3029 if (params_to_set & SA_PARAM_DENSITY) {
3030 mode_blk->density = current_density;
3031 } else if (softc->scsi_rev > SCSI_REV_CCS) {
3032 mode_blk->density = SCSI_SAME_DENSITY;
3034 mode_blk->density = softc->media_density;
3037 if (params_to_set & SA_PARAM_COMPRESSION)
3038 bcopy(ccomp, cpage, sizeof (sa_comp_t));
3041 * The retry count is the only CCB field that might have been
3042 * changed that we care about, so reset it back to 1.
3044 ccb->ccb_h.retry_count = 1;
3045 cam_periph_runccb(ccb, saerror, 0, sense_flags,
3046 softc->device_stats);
3050 xpt_release_ccb(ccb);
3053 free(ccomp, M_SCSISA);
3055 if (params_to_set & SA_PARAM_COMPRESSION) {
3057 softc->flags &= ~SA_FLAG_COMP_ENABLED;
3059 * Even if we get an error setting compression,
3060 * do not say that we don't support it. We could
3061 * have been wrong, or it may be media specific.
3062 * softc->flags &= ~SA_FLAG_COMP_SUPP;
3064 softc->saved_comp_algorithm = softc->comp_algorithm;
3065 softc->comp_algorithm = 0;
3067 softc->flags |= SA_FLAG_COMP_ENABLED;
3068 softc->comp_algorithm = calg;
3072 free(mode_buffer, M_SCSISA);
3077 saprevent(struct cam_periph *periph, int action)
3079 struct sa_softc *softc;
3083 softc = (struct sa_softc *)periph->softc;
3085 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3087 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3091 * We can be quiet about illegal requests.
3093 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3098 ccb = cam_periph_getccb(periph, 1);
3100 /* It is safe to retry this operation */
3101 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3102 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3104 error = cam_periph_runccb(ccb, saerror, 0, sf, softc->device_stats);
3107 if (action == PR_ALLOW)
3108 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3110 softc->flags |= SA_FLAG_TAPE_LOCKED;
3113 xpt_release_ccb(ccb);
3117 sarewind(struct cam_periph *periph)
3120 struct sa_softc *softc;
3123 softc = (struct sa_softc *)periph->softc;
3125 ccb = cam_periph_getccb(periph, 1);
3127 /* It is safe to retry this operation */
3128 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3129 SSD_FULL_SIZE, REWIND_TIMEOUT);
3131 softc->dsreg = MTIO_DSREG_REW;
3132 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3133 softc->dsreg = MTIO_DSREG_REST;
3135 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3136 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3138 xpt_release_ccb(ccb);
3140 softc->fileno = softc->blkno = (daddr_t) 0;
3142 softc->fileno = softc->blkno = (daddr_t) -1;
3147 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3150 struct sa_softc *softc;
3153 softc = (struct sa_softc *)periph->softc;
3155 ccb = cam_periph_getccb(periph, 1);
3157 /* This cannot be retried */
3159 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3160 SSD_FULL_SIZE, SPACE_TIMEOUT);
3163 * Clear residual because we will be using it.
3165 softc->last_ctl_resid = 0;
3167 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3168 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3169 softc->dsreg = MTIO_DSREG_REST;
3171 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3172 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3174 xpt_release_ccb(ccb);
3177 * If a spacing operation has failed, we need to invalidate
3180 * If the spacing operation was setmarks or to end of recorded data,
3181 * we no longer know our relative position.
3183 * If the spacing operations was spacing files in reverse, we
3184 * take account of the residual, but still check against less
3185 * than zero- if we've gone negative, we must have hit BOT.
3187 * If the spacing operations was spacing records in reverse and
3188 * we have a residual, we've either hit BOT or hit a filemark.
3189 * In the former case, we know our new record number (0). In
3190 * the latter case, we have absolutely no idea what the real
3191 * record number is- we've stopped between the end of the last
3192 * record in the previous file and the filemark that stopped
3193 * our spacing backwards.
3196 softc->fileno = softc->blkno = (daddr_t) -1;
3197 } else if (code == SS_SETMARKS || code == SS_EOD) {
3198 softc->fileno = softc->blkno = (daddr_t) -1;
3199 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3200 softc->fileno += (count - softc->last_ctl_resid);
3201 if (softc->fileno < 0) /* we must of hit BOT */
3204 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3205 softc->blkno += (count - softc->last_ctl_resid);
3207 if (softc->last_ctl_resid || softc->blkno < 0) {
3208 if (softc->fileno == 0) {
3211 softc->blkno = (daddr_t) -1;
3220 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3223 struct sa_softc *softc;
3226 softc = (struct sa_softc *)periph->softc;
3227 if (softc->open_rdonly)
3230 ccb = cam_periph_getccb(periph, 1);
3232 * Clear residual because we will be using it.
3234 softc->last_ctl_resid = 0;
3236 softc->dsreg = MTIO_DSREG_FMK;
3237 /* this *must* not be retried */
3238 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3239 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3240 softc->dsreg = MTIO_DSREG_REST;
3243 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3245 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3246 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3248 if (error == 0 && nmarks) {
3249 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3250 nwm = nmarks - softc->last_ctl_resid;
3251 softc->filemarks += nwm;
3254 xpt_release_ccb(ccb);
3257 * Update relative positions (if we're doing that).
3260 softc->fileno = softc->blkno = (daddr_t) -1;
3261 } else if (softc->fileno != (daddr_t) -1) {
3262 softc->fileno += nwm;
3269 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3271 struct scsi_tape_position_data loc;
3273 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3277 * We try and flush any buffered writes here if we were writing
3278 * and we're trying to get hardware block position. It eats
3279 * up performance substantially, but I'm wary of drive firmware.
3281 * I think that *logical* block position is probably okay-
3282 * but hardware block position might have to wait for data
3283 * to hit media to be valid. Caveat Emptor.
3286 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3287 error = sawritefilemarks(periph, 0, 0);
3288 if (error && error != EACCES)
3292 ccb = cam_periph_getccb(periph, 1);
3293 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3294 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3295 softc->dsreg = MTIO_DSREG_RBSY;
3296 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3297 softc->dsreg = MTIO_DSREG_REST;
3298 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3299 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3302 if (loc.flags & SA_RPOS_UNCERTAIN) {
3303 error = EINVAL; /* nothing is certain */
3305 *blkptr = scsi_4btoul(loc.firstblk);
3309 xpt_release_ccb(ccb);
3314 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3317 struct sa_softc *softc;
3321 * We used to try and flush any buffered writes here.
3322 * Now we push this onto user applications to either
3323 * flush the pending writes themselves (via a zero count
3324 * WRITE FILEMARKS command) or they can trust their tape
3325 * drive to do this correctly for them.
3328 softc = (struct sa_softc *)periph->softc;
3329 ccb = cam_periph_getccb(periph, 1);
3332 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3333 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3336 softc->dsreg = MTIO_DSREG_POS;
3337 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3338 softc->dsreg = MTIO_DSREG_REST;
3339 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3340 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3341 xpt_release_ccb(ccb);
3343 * Note relative file && block number position as now unknown.
3345 softc->fileno = softc->blkno = (daddr_t) -1;
3350 saretension(struct cam_periph *periph)
3353 struct sa_softc *softc;
3356 softc = (struct sa_softc *)periph->softc;
3358 ccb = cam_periph_getccb(periph, 1);
3360 /* It is safe to retry this operation */
3361 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3362 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3364 softc->dsreg = MTIO_DSREG_TEN;
3365 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3366 softc->dsreg = MTIO_DSREG_REST;
3368 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3369 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3370 xpt_release_ccb(ccb);
3372 softc->fileno = softc->blkno = (daddr_t) 0;
3374 softc->fileno = softc->blkno = (daddr_t) -1;
3379 sareservereleaseunit(struct cam_periph *periph, int reserve)
3382 struct sa_softc *softc;
3385 softc = (struct sa_softc *)periph->softc;
3386 ccb = cam_periph_getccb(periph, 1);
3388 /* It is safe to retry this operation */
3389 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3390 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3391 softc->dsreg = MTIO_DSREG_RBSY;
3392 error = cam_periph_runccb(ccb, saerror, 0,
3393 SF_RETRY_UA | SF_NO_PRINT, softc->device_stats);
3394 softc->dsreg = MTIO_DSREG_REST;
3396 xpt_release_ccb(ccb);
3399 * If the error was Illegal Request, then the device doesn't support
3400 * RESERVE/RELEASE. This is not an error.
3402 if (error == EINVAL) {
3410 saloadunload(struct cam_periph *periph, int load)
3413 struct sa_softc *softc;
3416 softc = (struct sa_softc *)periph->softc;
3418 ccb = cam_periph_getccb(periph, 1);
3420 /* It is safe to retry this operation */
3421 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3422 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3424 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3425 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3426 softc->dsreg = MTIO_DSREG_REST;
3428 xpt_release_ccb(ccb);
3430 if (error || load == 0)
3431 softc->fileno = softc->blkno = (daddr_t) -1;
3432 else if (error == 0)
3433 softc->fileno = softc->blkno = (daddr_t) 0;
3438 saerase(struct cam_periph *periph, int longerase)
3442 struct sa_softc *softc;
3445 softc = (struct sa_softc *)periph->softc;
3446 if (softc->open_rdonly)
3449 ccb = cam_periph_getccb(periph, 1);
3451 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3452 SSD_FULL_SIZE, ERASE_TIMEOUT);
3454 softc->dsreg = MTIO_DSREG_ZER;
3455 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3456 softc->dsreg = MTIO_DSREG_REST;
3458 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3459 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3460 xpt_release_ccb(ccb);
3464 #endif /* _KERNEL */
3467 * Read tape block limits command.
3470 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3471 void (*cbfcnp)(struct cam_periph *, union ccb *),
3472 u_int8_t tag_action,
3473 struct scsi_read_block_limits_data *rlimit_buf,
3474 u_int8_t sense_len, u_int32_t timeout)
3476 struct scsi_read_block_limits *scsi_cmd;
3478 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3479 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3480 sizeof(*scsi_cmd), timeout);
3482 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3483 bzero(scsi_cmd, sizeof(*scsi_cmd));
3484 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3488 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3489 void (*cbfcnp)(struct cam_periph *, union ccb *),
3490 u_int8_t tag_action, int readop, int sli,
3491 int fixed, u_int32_t length, u_int8_t *data_ptr,
3492 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3494 struct scsi_sa_rw *scsi_cmd;
3496 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3497 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3498 scsi_cmd->sli_fixed = 0;
3500 scsi_cmd->sli_fixed |= SAR_SLI;
3502 scsi_cmd->sli_fixed |= SARW_FIXED;
3503 scsi_ulto3b(length, scsi_cmd->length);
3504 scsi_cmd->control = 0;
3506 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3507 tag_action, data_ptr, dxfer_len, sense_len,
3508 sizeof(*scsi_cmd), timeout);
3512 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3513 void (*cbfcnp)(struct cam_periph *, union ccb *),
3514 u_int8_t tag_action, int immediate, int eot,
3515 int reten, int load, u_int8_t sense_len,
3518 struct scsi_load_unload *scsi_cmd;
3520 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3521 bzero(scsi_cmd, sizeof(*scsi_cmd));
3522 scsi_cmd->opcode = LOAD_UNLOAD;
3524 scsi_cmd->immediate = SLU_IMMED;
3526 scsi_cmd->eot_reten_load |= SLU_EOT;
3528 scsi_cmd->eot_reten_load |= SLU_RETEN;
3530 scsi_cmd->eot_reten_load |= SLU_LOAD;
3532 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3533 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3537 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3538 void (*cbfcnp)(struct cam_periph *, union ccb *),
3539 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3542 struct scsi_rewind *scsi_cmd;
3544 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3545 bzero(scsi_cmd, sizeof(*scsi_cmd));
3546 scsi_cmd->opcode = REWIND;
3548 scsi_cmd->immediate = SREW_IMMED;
3550 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3551 0, sense_len, sizeof(*scsi_cmd), timeout);
3555 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3556 void (*cbfcnp)(struct cam_periph *, union ccb *),
3557 u_int8_t tag_action, scsi_space_code code,
3558 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3560 struct scsi_space *scsi_cmd;
3562 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3563 scsi_cmd->opcode = SPACE;
3564 scsi_cmd->code = code;
3565 scsi_ulto3b(count, scsi_cmd->count);
3566 scsi_cmd->control = 0;
3568 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3569 0, sense_len, sizeof(*scsi_cmd), timeout);
3573 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3574 void (*cbfcnp)(struct cam_periph *, union ccb *),
3575 u_int8_t tag_action, int immediate, int setmark,
3576 u_int32_t num_marks, u_int8_t sense_len,
3579 struct scsi_write_filemarks *scsi_cmd;
3581 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3582 bzero(scsi_cmd, sizeof(*scsi_cmd));
3583 scsi_cmd->opcode = WRITE_FILEMARKS;
3585 scsi_cmd->byte2 |= SWFMRK_IMMED;
3587 scsi_cmd->byte2 |= SWFMRK_WSMK;
3589 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3591 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3592 0, sense_len, sizeof(*scsi_cmd), timeout);
3596 * The reserve and release unit commands differ only by their opcodes.
3599 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3600 void (*cbfcnp)(struct cam_periph *, union ccb *),
3601 u_int8_t tag_action, int third_party,
3602 int third_party_id, u_int8_t sense_len,
3603 u_int32_t timeout, int reserve)
3605 struct scsi_reserve_release_unit *scsi_cmd;
3607 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3608 bzero(scsi_cmd, sizeof(*scsi_cmd));
3611 scsi_cmd->opcode = RESERVE_UNIT;
3613 scsi_cmd->opcode = RELEASE_UNIT;
3616 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3617 scsi_cmd->lun_thirdparty |=
3618 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3621 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3622 0, sense_len, sizeof(*scsi_cmd), timeout);
3626 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3627 void (*cbfcnp)(struct cam_periph *, union ccb *),
3628 u_int8_t tag_action, int immediate, int long_erase,
3629 u_int8_t sense_len, u_int32_t timeout)
3631 struct scsi_erase *scsi_cmd;
3633 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3634 bzero(scsi_cmd, sizeof(*scsi_cmd));
3636 scsi_cmd->opcode = ERASE;
3639 scsi_cmd->lun_imm_long |= SE_IMMED;
3642 scsi_cmd->lun_imm_long |= SE_LONG;
3644 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3645 0, sense_len, sizeof(*scsi_cmd), timeout);
3649 * Read Tape Position command.
3652 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3653 void (*cbfcnp)(struct cam_periph *, union ccb *),
3654 u_int8_t tag_action, int hardsoft,
3655 struct scsi_tape_position_data *sbp,
3656 u_int8_t sense_len, u_int32_t timeout)
3658 struct scsi_tape_read_position *scmd;
3660 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3661 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3662 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3663 bzero(scmd, sizeof(*scmd));
3664 scmd->opcode = READ_POSITION;
3665 scmd->byte1 = hardsoft;
3669 * Set Tape Position command.
3672 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3673 void (*cbfcnp)(struct cam_periph *, union ccb *),
3674 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3675 u_int8_t sense_len, u_int32_t timeout)
3677 struct scsi_tape_locate *scmd;
3679 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3680 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3681 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3682 bzero(scmd, sizeof(*scmd));
3683 scmd->opcode = LOCATE;
3685 scmd->byte1 |= SA_SPOS_BT;
3686 scsi_ulto4b(blkno, scmd->blkaddr);