2 * $FreeBSD: src/sys/cam/scsi/scsi_sa.c,v 1.45.2.13 2002/12/17 17:08:50 trhodes Exp $
3 * $DragonFly: src/sys/bus/cam/scsi/scsi_sa.c,v 1.35 2008/05/18 20:30:20 pavalos Exp $
5 * Implementation of SCSI Sequential Access Peripheral driver for CAM.
7 * Copyright (c) 1999, 2000 Matthew Jacob
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/queue.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/types.h>
42 #include <sys/malloc.h>
49 #include <sys/thread2.h>
51 #include <sys/fcntl.h>
52 #include <sys/devicestat.h>
53 #include <machine/limits.h>
61 #include "../cam_ccb.h"
62 #include "../cam_extend.h"
63 #include "../cam_periph.h"
64 #include "../cam_xpt_periph.h"
65 #include "../cam_debug.h"
68 #include "scsi_message.h"
76 #define SA_IO_TIMEOUT 4
78 #ifndef SA_SPACE_TIMEOUT
79 #define SA_SPACE_TIMEOUT 1 * 60
81 #ifndef SA_REWIND_TIMEOUT
82 #define SA_REWIND_TIMEOUT 2 * 60
84 #ifndef SA_ERASE_TIMEOUT
85 #define SA_ERASE_TIMEOUT 4 * 60
88 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
90 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
91 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
92 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
93 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
96 * Additional options that can be set for config: SA_1FM_AT_EOT
99 #ifndef UNUSED_PARAMETER
100 #define UNUSED_PARAMETER(x) x = x
104 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
105 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
111 MALLOC_DEFINE(M_SCSISA, "SCSI sa", "SCSI sequential access buffers");
114 SA_STATE_NORMAL, SA_STATE_ABNORMAL
117 #define ccb_pflags ppriv_field0
118 #define ccb_bio ppriv_ptr1
120 #define SA_CCB_BUFFER_IO 0x0
121 #define SA_CCB_WAITING 0x1
122 #define SA_CCB_TYPEMASK 0x1
123 #define SA_POSITION_UPDATED 0x2
125 #define Set_CCB_Type(x, type) \
126 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
127 x->ccb_h.ccb_pflags |= type
129 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
134 SA_FLAG_OPEN = 0x0001,
135 SA_FLAG_FIXED = 0x0002,
136 SA_FLAG_TAPE_LOCKED = 0x0004,
137 SA_FLAG_TAPE_MOUNTED = 0x0008,
138 SA_FLAG_TAPE_WP = 0x0010,
139 SA_FLAG_TAPE_WRITTEN = 0x0020,
140 SA_FLAG_EOM_PENDING = 0x0040,
141 SA_FLAG_EIO_PENDING = 0x0080,
142 SA_FLAG_EOF_PENDING = 0x0100,
143 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
144 SA_FLAG_EOF_PENDING),
145 SA_FLAG_INVALID = 0x0200,
146 SA_FLAG_COMP_ENABLED = 0x0400,
147 SA_FLAG_COMP_SUPP = 0x0800,
148 SA_FLAG_COMP_UNSUPP = 0x1000,
149 SA_FLAG_TAPE_FROZEN = 0x2000
153 SA_MODE_REWIND = 0x00,
154 SA_MODE_NOREWIND = 0x01,
155 SA_MODE_OFFLINE = 0x02
159 SA_PARAM_NONE = 0x00,
160 SA_PARAM_BLOCKSIZE = 0x01,
161 SA_PARAM_DENSITY = 0x02,
162 SA_PARAM_COMPRESSION = 0x04,
163 SA_PARAM_BUFF_MODE = 0x08,
164 SA_PARAM_NUMBLOCKS = 0x10,
166 SA_PARAM_SPEED = 0x40,
171 SA_QUIRK_NONE = 0x00,
172 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
173 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
174 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
175 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
176 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
177 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
178 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
179 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
182 /* units are bits 4-7, 16-21 (1024 units) */
183 #define SAUNIT(DEV) \
184 (((minor(DEV) & 0xF0) >> 4) | ((minor(DEV) & 0x3f0000) >> 16))
186 #define SAMODE(z) ((minor(z) & 0x3))
187 #define SADENSITY(z) (((minor(z) >> 2) & 0x3))
188 #define SA_IS_CTRL(z) (minor(z) & (1 << 29))
190 #define SA_NOT_CTLDEV 0
194 #define SA_ATYPE_NR 1
195 #define SA_ATYPE_ER 2
197 #define SAMINOR(ctl, unit, mode, access) \
198 ((ctl << 29) | ((unit & 0x3f0) << 16) | ((unit & 0xf) << 4) | \
199 (mode << 0x2) | (access & 0x3))
201 #define SA_UNITMASK SAMINOR(0, -1, 0, 0)
202 #define SA_UNIT(unit) SAMINOR(0, unit, 0, 0)
204 #define SA_NUM_MODES 4
210 struct bio_queue_head bio_queue;
212 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, cdev_t);
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 */
430 #define SA_CDEV_MAJOR 14
432 static struct dev_ops sa_ops = {
433 { "sa", SA_CDEV_MAJOR, D_TAPE },
437 .d_write = physwrite,
439 .d_strategy = sastrategy,
442 static struct extend_array *saperiphs;
445 saopen(struct dev_open_args *ap)
447 cdev_t dev = ap->a_head.a_dev;
448 struct cam_periph *periph;
449 struct sa_softc *softc;
455 periph = cam_extend_get(saperiphs, unit);
456 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
460 cam_periph_lock(periph);
462 softc = (struct sa_softc *)periph->softc;
464 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
465 ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
467 if (SA_IS_CTRL(dev)) {
468 softc->ctrl_mode = 1;
469 cam_periph_unlock(periph);
473 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
474 cam_periph_unlock(periph);
475 cam_periph_release(periph);
479 if (softc->flags & SA_FLAG_OPEN) {
481 } else if (softc->flags & SA_FLAG_INVALID) {
485 * Preserve whether this is a read_only open.
487 softc->open_rdonly = (ap->a_oflags & O_RDWR) == O_RDONLY;
490 * The function samount ensures media is loaded and ready.
491 * It also does a device RESERVE if the tape isn't yet mounted.
493 * If the mount fails and this was a non-blocking open,
494 * make this a 'open_pending_mount' action.
496 error = samount(periph, ap->a_oflags, dev);
497 if (error && (ap->a_oflags & O_NONBLOCK)) {
498 softc->flags |= SA_FLAG_OPEN;
499 softc->open_pending_mount = 1;
500 cam_periph_unhold(periph);
501 cam_periph_unlock(periph);
507 cam_periph_unhold(periph);
508 cam_periph_unlock(periph);
509 cam_periph_release(periph);
513 saprevent(periph, PR_PREVENT);
514 softc->flags |= SA_FLAG_OPEN;
516 cam_periph_unhold(periph);
517 cam_periph_unlock(periph);
522 saclose(struct dev_close_args *ap)
524 cdev_t dev = ap->a_head.a_dev;
525 struct cam_periph *periph;
526 struct sa_softc *softc;
527 int unit, mode, error, writing, tmp;
528 int closedbits = SA_FLAG_OPEN;
532 periph = cam_extend_get(saperiphs, unit);
536 cam_periph_lock(periph);
538 softc = (struct sa_softc *)periph->softc;
540 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
541 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
544 softc->open_rdonly = 0;
545 if (SA_IS_CTRL(dev)) {
546 softc->ctrl_mode = 0;
547 cam_periph_unlock(periph);
548 cam_periph_release(periph);
552 if (softc->open_pending_mount) {
553 softc->flags &= ~SA_FLAG_OPEN;
554 softc->open_pending_mount = 0;
555 cam_periph_unlock(periph);
556 cam_periph_release(periph);
560 if ((error = cam_periph_hold(periph, 0)) != 0) {
561 cam_periph_unlock(periph);
566 * Were we writing the tape?
568 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
571 * See whether or not we need to write filemarks. If this
572 * fails, we probably have to assume we've lost tape
575 error = sacheckeod(periph);
577 xpt_print(periph->path,
578 "failed to write terminating filemark(s)\n");
579 softc->flags |= SA_FLAG_TAPE_FROZEN;
583 * Whatever we end up doing, allow users to eject tapes from here on.
585 saprevent(periph, PR_ALLOW);
588 * Decide how to end...
590 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
591 closedbits |= SA_FLAG_TAPE_FROZEN;
592 } else switch (mode) {
593 case SA_MODE_OFFLINE:
595 * An 'offline' close is an unconditional release of
596 * frozen && mount conditions, irrespective of whether
597 * these operations succeeded. The reason for this is
598 * to allow at least some kind of programmatic way
599 * around our state getting all fouled up. If somebody
600 * issues an 'offline' command, that will be allowed
604 saloadunload(periph, FALSE);
605 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
609 * If the rewind fails, return an error- if anyone cares,
610 * but not overwriting any previous error.
612 * We don't clear the notion of mounted here, but we do
613 * clear the notion of frozen if we successfully rewound.
615 tmp = sarewind(periph);
620 closedbits |= SA_FLAG_TAPE_FROZEN;
623 case SA_MODE_NOREWIND:
625 * If we're not rewinding/unloading the tape, find out
626 * whether we need to back up over one of two filemarks
627 * we wrote (if we wrote two filemarks) so that appends
628 * from this point on will be sane.
630 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
631 tmp = saspace(periph, -1, SS_FILEMARKS);
633 xpt_print(periph->path, "unable to backspace "
634 "over one of double filemarks at end of "
636 xpt_print(periph->path, "it is possible that "
637 "this device needs a SA_QUIRK_1FM quirk set"
639 softc->flags |= SA_FLAG_TAPE_FROZEN;
644 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode);
650 * We wish to note here that there are no more filemarks to be written.
652 softc->filemarks = 0;
653 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
656 * And we are no longer open for business.
658 softc->flags &= ~closedbits;
661 * Inform users if tape state if frozen....
663 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
664 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, "
665 "REWIND or MTEOM command to clear this state.\n");
668 /* release the device if it is no longer mounted */
669 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
670 sareservereleaseunit(periph, FALSE);
672 cam_periph_unhold(periph);
673 cam_periph_unlock(periph);
674 cam_periph_release(periph);
680 * Actually translate the requested transfer into one the physical driver
681 * can understand. The transfer is described by a buf and will include
682 * only one physical transfer.
685 sastrategy(struct dev_strategy_args *ap)
687 cdev_t dev = ap->a_head.a_dev;
688 struct bio *bio = ap->a_bio;
689 struct buf *bp = bio->bio_buf;
690 struct cam_periph *periph;
691 struct sa_softc *softc;
694 if (SA_IS_CTRL(dev)) {
695 bp->b_error = EINVAL;
699 periph = cam_extend_get(saperiphs, unit);
700 if (periph == NULL) {
704 cam_periph_lock(periph);
706 softc = (struct sa_softc *)periph->softc;
708 if (softc->flags & SA_FLAG_INVALID) {
709 cam_periph_unlock(periph);
714 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
715 cam_periph_unlock(periph);
721 * This should actually never occur as the write(2)
722 * system call traps attempts to write to a read-only
725 if (bp->b_cmd == BUF_CMD_WRITE && softc->open_rdonly) {
726 cam_periph_unlock(periph);
731 if (softc->open_pending_mount) {
732 int error = samount(periph, 0, dev);
734 cam_periph_unlock(periph);
738 saprevent(periph, PR_PREVENT);
739 softc->open_pending_mount = 0;
743 * If it's a null transfer, return immediately
745 if (bp->b_bcount == 0) {
746 cam_periph_unlock(periph);
751 if (softc->flags & SA_FLAG_FIXED) {
753 * Fixed block device. The byte count must
754 * be a multiple of our block size.
756 if (((softc->blk_mask != ~0) &&
757 ((bp->b_bcount & softc->blk_mask) != 0)) ||
758 ((softc->blk_mask == ~0) &&
759 ((bp->b_bcount % softc->min_blk) != 0))) {
760 xpt_print(periph->path, "Invalid request. Fixed block "
761 "device requests must be a multiple of %d bytes\n",
763 cam_periph_unlock(periph);
764 bp->b_error = EINVAL;
767 } else if ((bp->b_bcount > softc->max_blk) ||
768 (bp->b_bcount < softc->min_blk) ||
769 (bp->b_bcount & softc->blk_mask) != 0) {
771 xpt_print_path(periph->path);
772 kprintf("Invalid request. Variable block "
773 "device requests must be ");
774 if (softc->blk_mask != 0) {
775 kprintf("a multiple of %d ", (0x1 << softc->blk_gran));
777 kprintf("between %d and %d bytes\n", softc->min_blk,
779 cam_periph_unlock(periph);
780 bp->b_error = EINVAL;
785 * Place it at the end of the queue.
787 bioq_insert_tail(&softc->bio_queue, bio);
788 softc->queue_count++;
790 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
791 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
792 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
793 (bp->bio_cmd == BIO_READ)? "read" : "write"));
795 if (softc->queue_count > 1) {
796 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
797 ("sastrategy: queue count now %d\n", softc->queue_count));
801 * Schedule ourselves for performing the work.
803 xpt_schedule(periph, 1);
804 cam_periph_unlock(periph);
808 bp->b_flags |= B_ERROR;
812 * Correctly set the buf to indicate a completed xfer
814 bp->b_resid = bp->b_bcount;
820 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
821 if (softc->open_pending_mount) { \
822 error = samount(periph, 0, dev); \
826 saprevent(periph, PR_PREVENT); \
827 softc->open_pending_mount = 0; \
831 saioctl(struct dev_ioctl_args *ap)
833 cdev_t dev = ap->a_head.a_dev;
834 caddr_t addr = ap->a_data;
835 struct cam_periph *periph;
836 struct sa_softc *softc;
837 scsi_space_code spaceop;
838 int didlockperiph = 0;
845 error = 0; /* shut up gcc */
846 spaceop = 0; /* shut up gcc */
848 periph = cam_extend_get(saperiphs, unit);
852 cam_periph_lock(periph);
853 softc = (struct sa_softc *)periph->softc;
856 * Check for control mode accesses. We allow MTIOCGET and
857 * MTIOCERRSTAT (but need to be the only one open in order
858 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
859 * and MTCOMP (but need to be the only one accessing this
860 * device to run those).
863 if (SA_IS_CTRL(dev)) {
865 case MTIOCGETEOTMODEL:
870 * If the periph isn't already locked, lock it
871 * so our MTIOCERRSTAT can reset latched error stats.
873 * If the periph is already locked, skip it because
874 * we're just getting status and it'll be up to the
875 * other thread that has this device open to do
876 * an MTIOCERRSTAT that would clear latched status.
878 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
879 error = cam_periph_hold(periph, PCATCH);
889 struct mtop *mt = (struct mtop *) addr;
892 * Check to make sure it's an OP we can perform
893 * with no media inserted.
909 case MTIOCSETEOTMODEL:
911 * We need to acquire the peripheral here rather
912 * than at open time because we are sharing writable
913 * access to data structures.
915 error = cam_periph_hold(periph, PCATCH);
928 * Find the device that the user is talking about
933 struct mtget *g = (struct mtget *)addr;
936 * If this isn't the control mode device, actually go out
937 * and ask the drive again what it's set to.
939 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) {
940 u_int8_t write_protect;
941 int comp_enabled, comp_supported;
942 error = sagetparams(periph, SA_PARAM_ALL,
943 &softc->media_blksize, &softc->media_density,
944 &softc->media_numblks, &softc->buffer_mode,
945 &write_protect, &softc->speed, &comp_supported,
946 &comp_enabled, &softc->comp_algorithm, NULL);
950 softc->flags |= SA_FLAG_TAPE_WP;
952 softc->flags &= ~SA_FLAG_TAPE_WP;
953 softc->flags &= ~(SA_FLAG_COMP_SUPP|
954 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
955 if (comp_supported) {
956 if (softc->saved_comp_algorithm == 0)
957 softc->saved_comp_algorithm =
958 softc->comp_algorithm;
959 softc->flags |= SA_FLAG_COMP_SUPP;
961 softc->flags |= SA_FLAG_COMP_ENABLED;
963 softc->flags |= SA_FLAG_COMP_UNSUPP;
965 bzero(g, sizeof(struct mtget));
966 g->mt_type = MT_ISAR;
967 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
968 g->mt_comp = MT_COMP_UNSUPP;
969 g->mt_comp0 = MT_COMP_UNSUPP;
970 g->mt_comp1 = MT_COMP_UNSUPP;
971 g->mt_comp2 = MT_COMP_UNSUPP;
972 g->mt_comp3 = MT_COMP_UNSUPP;
974 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
975 g->mt_comp = MT_COMP_DISABLED;
977 g->mt_comp = softc->comp_algorithm;
979 g->mt_comp0 = softc->comp_algorithm;
980 g->mt_comp1 = softc->comp_algorithm;
981 g->mt_comp2 = softc->comp_algorithm;
982 g->mt_comp3 = softc->comp_algorithm;
984 g->mt_density = softc->media_density;
985 g->mt_density0 = softc->media_density;
986 g->mt_density1 = softc->media_density;
987 g->mt_density2 = softc->media_density;
988 g->mt_density3 = softc->media_density;
989 g->mt_blksiz = softc->media_blksize;
990 g->mt_blksiz0 = softc->media_blksize;
991 g->mt_blksiz1 = softc->media_blksize;
992 g->mt_blksiz2 = softc->media_blksize;
993 g->mt_blksiz3 = softc->media_blksize;
994 g->mt_fileno = softc->fileno;
995 g->mt_blkno = softc->blkno;
996 g->mt_dsreg = (short) softc->dsreg;
998 * Yes, we know that this is likely to overflow
1000 if (softc->last_resid_was_io) {
1001 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
1002 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1003 softc->last_io_resid = 0;
1007 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
1008 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1009 softc->last_ctl_resid = 0;
1018 struct scsi_tape_errors *sep =
1019 &((union mterrstat *)addr)->scsi_errstat;
1021 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1022 ("saioctl: MTIOCERRSTAT\n"));
1024 bzero(sep, sizeof(*sep));
1025 sep->io_resid = softc->last_io_resid;
1026 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1027 sizeof (sep->io_sense));
1028 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1029 sizeof (sep->io_cdb));
1030 sep->ctl_resid = softc->last_ctl_resid;
1031 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1032 sizeof (sep->ctl_sense));
1033 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1034 sizeof (sep->ctl_cdb));
1036 if ((SA_IS_CTRL(dev) == 0 && softc->open_pending_mount) ||
1038 bzero((caddr_t) &softc->errinfo,
1039 sizeof (softc->errinfo));
1048 PENDING_MOUNT_CHECK(softc, periph, dev);
1050 mt = (struct mtop *)addr;
1052 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1053 ("saioctl: op=0x%x count=0x%x\n",
1054 mt->mt_op, mt->mt_count));
1056 count = mt->mt_count;
1057 switch (mt->mt_op) {
1058 case MTWEOF: /* write an end-of-file marker */
1060 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1061 * flag because by keeping track of filemarks
1062 * we have last written we know ehether or not
1063 * we need to write more when we close the device.
1065 error = sawritefilemarks(periph, count, FALSE);
1067 case MTWSS: /* write a setmark */
1068 error = sawritefilemarks(periph, count, TRUE);
1070 case MTBSR: /* backward space record */
1071 case MTFSR: /* forward space record */
1072 case MTBSF: /* backward space file */
1073 case MTFSF: /* forward space file */
1074 case MTBSS: /* backward space setmark */
1075 case MTFSS: /* forward space setmark */
1076 case MTEOD: /* space to end of recorded medium */
1080 spaceop = SS_FILEMARKS;
1081 nmarks = softc->filemarks;
1082 error = sacheckeod(periph);
1084 xpt_print(periph->path,
1085 "EOD check prior to spacing failed\n");
1086 softc->flags |= SA_FLAG_EIO_PENDING;
1089 nmarks -= softc->filemarks;
1095 spaceop = SS_BLOCKS;
1106 spaceop = SS_SETMARKS;
1120 nmarks = softc->filemarks;
1122 * XXX: Why are we checking again?
1124 error = sacheckeod(periph);
1127 nmarks -= softc->filemarks;
1128 error = saspace(periph, count - nmarks, spaceop);
1130 * At this point, clear that we've written the tape
1131 * and that we've written any filemarks. We really
1132 * don't know what the applications wishes to do next-
1133 * the sacheckeod's will make sure we terminated the
1134 * tape correctly if we'd been writing, but the next
1135 * action the user application takes will set again
1136 * whether we need to write filemarks.
1139 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1140 softc->filemarks = 0;
1143 case MTREW: /* rewind */
1144 PENDING_MOUNT_CHECK(softc, periph, dev);
1146 error = sarewind(periph);
1149 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1150 softc->flags &= ~SA_FLAG_ERR_PENDING;
1151 softc->filemarks = 0;
1153 case MTERASE: /* erase */
1154 PENDING_MOUNT_CHECK(softc, periph, dev);
1155 error = saerase(periph, count);
1157 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1158 softc->flags &= ~SA_FLAG_ERR_PENDING;
1160 case MTRETENS: /* re-tension tape */
1161 PENDING_MOUNT_CHECK(softc, periph, dev);
1162 error = saretension(periph);
1164 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1165 softc->flags &= ~SA_FLAG_ERR_PENDING;
1167 case MTOFFL: /* rewind and put the drive offline */
1169 PENDING_MOUNT_CHECK(softc, periph, dev);
1173 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1174 softc->filemarks = 0;
1176 error = sarewind(periph);
1177 /* clear the frozen flag anyway */
1178 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1181 * Be sure to allow media removal before ejecting.
1184 saprevent(periph, PR_ALLOW);
1186 error = saloadunload(periph, FALSE);
1188 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1193 case MTNOP: /* no operation, sets status only */
1194 case MTCACHE: /* enable controller cache */
1195 case MTNOCACHE: /* disable controller cache */
1199 case MTSETBSIZ: /* Set block size for device */
1201 PENDING_MOUNT_CHECK(softc, periph, dev);
1203 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1206 softc->last_media_blksize =
1207 softc->media_blksize;
1208 softc->media_blksize = count;
1210 softc->flags |= SA_FLAG_FIXED;
1211 if (powerof2(count)) {
1214 softc->blk_mask = count - 1;
1216 softc->blk_mask = ~0;
1217 softc->blk_shift = 0;
1220 * Make the user's desire 'persistent'.
1222 softc->quirks &= ~SA_QUIRK_VARIABLE;
1223 softc->quirks |= SA_QUIRK_FIXED;
1225 softc->flags &= ~SA_FLAG_FIXED;
1226 if (softc->max_blk == 0) {
1227 softc->max_blk = ~0;
1229 softc->blk_shift = 0;
1230 if (softc->blk_gran != 0) {
1232 softc->blk_gran - 1;
1234 softc->blk_mask = 0;
1237 * Make the user's desire 'persistent'.
1239 softc->quirks |= SA_QUIRK_VARIABLE;
1240 softc->quirks &= ~SA_QUIRK_FIXED;
1244 case MTSETDNSTY: /* Set density for device and mode */
1245 PENDING_MOUNT_CHECK(softc, periph, dev);
1247 if (count > UCHAR_MAX) {
1251 error = sasetparams(periph, SA_PARAM_DENSITY,
1255 case MTCOMP: /* enable compression */
1256 PENDING_MOUNT_CHECK(softc, periph, dev);
1258 * Some devices don't support compression, and
1259 * don't like it if you ask them for the
1262 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1263 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1267 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1268 0, 0, count, SF_NO_PRINT);
1280 PENDING_MOUNT_CHECK(softc, periph, dev);
1281 error = sardpos(periph, 0, (u_int32_t *) addr);
1284 PENDING_MOUNT_CHECK(softc, periph, dev);
1285 error = sardpos(periph, 1, (u_int32_t *) addr);
1288 PENDING_MOUNT_CHECK(softc, periph, dev);
1289 error = sasetpos(periph, 0, (u_int32_t *) addr);
1292 PENDING_MOUNT_CHECK(softc, periph, dev);
1293 error = sasetpos(periph, 1, (u_int32_t *) addr);
1295 case MTIOCGETEOTMODEL:
1297 if (softc->quirks & SA_QUIRK_1FM)
1301 *((u_int32_t *) addr) = mode;
1303 case MTIOCSETEOTMODEL:
1305 switch (*((u_int32_t *) addr)) {
1307 softc->quirks &= ~SA_QUIRK_2FM;
1308 softc->quirks |= SA_QUIRK_1FM;
1311 softc->quirks &= ~SA_QUIRK_1FM;
1312 softc->quirks |= SA_QUIRK_2FM;
1320 error = cam_periph_ioctl(periph, ap->a_cmd, addr, saerror);
1325 * Check to see if we cleared a frozen state
1327 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1333 softc->fileno = (daddr_t) -1;
1334 softc->blkno = (daddr_t) -1;
1335 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1336 xpt_print(periph->path,
1337 "tape state now unfrozen.\n");
1343 if (didlockperiph) {
1344 cam_periph_unhold(periph);
1346 cam_periph_unlock(periph);
1356 * Create our extend array for storing the devices we attach to.
1358 saperiphs = cam_extend_new();
1359 if (saperiphs == NULL) {
1360 kprintf("sa: Failed to alloc extend array!\n");
1365 * Install a global async callback.
1367 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL);
1369 if (status != CAM_REQ_CMP) {
1370 kprintf("sa: Failed to attach master async callback "
1371 "due to status 0x%x!\n", status);
1376 saoninvalidate(struct cam_periph *periph)
1378 struct sa_softc *softc;
1382 softc = (struct sa_softc *)periph->softc;
1385 * De-register any async callbacks.
1387 xpt_register_async(0, saasync, periph, periph->path);
1389 softc->flags |= SA_FLAG_INVALID;
1392 * Return all queued I/O with ENXIO.
1393 * XXX Handle any transactions queued to the card
1394 * with XPT_ABORT_CCB.
1396 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
1397 bioq_remove(&softc->bio_queue, q_bio);
1398 q_bp = q_bio->bio_buf;
1399 q_bp->b_resid = q_bp->b_bcount;
1400 q_bp->b_error = ENXIO;
1401 q_bp->b_flags |= B_ERROR;
1404 softc->queue_count = 0;
1406 xpt_print(periph->path, "lost device\n");
1411 sacleanup(struct cam_periph *periph)
1413 struct sa_softc *softc;
1415 softc = (struct sa_softc *)periph->softc;
1417 devstat_remove_entry(&softc->device_stats);
1419 cam_extend_release(saperiphs, periph->unit_number);
1420 xpt_print(periph->path, "removing device entry\n");
1421 dev_ops_remove(&sa_ops, SA_UNITMASK, SA_UNIT(periph->unit_number));
1422 kfree(softc, M_SCSISA);
1426 saasync(void *callback_arg, u_int32_t code,
1427 struct cam_path *path, void *arg)
1429 struct cam_periph *periph;
1431 periph = (struct cam_periph *)callback_arg;
1433 case AC_FOUND_DEVICE:
1435 struct ccb_getdev *cgd;
1438 cgd = (struct ccb_getdev *)arg;
1442 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1446 * Allocate a peripheral instance for
1447 * this device and start the probe
1450 status = cam_periph_alloc(saregister, saoninvalidate,
1452 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1453 saasync, AC_FOUND_DEVICE, cgd);
1455 if (status != CAM_REQ_CMP
1456 && status != CAM_REQ_INPROG)
1457 kprintf("saasync: Unable to probe new device "
1458 "due to status 0x%x\n", status);
1462 cam_periph_async(periph, code, path, arg);
1468 saregister(struct cam_periph *periph, void *arg)
1470 struct sa_softc *softc;
1471 struct ccb_getdev *cgd;
1475 cgd = (struct ccb_getdev *)arg;
1476 if (periph == NULL) {
1477 kprintf("saregister: periph was NULL!!\n");
1478 return (CAM_REQ_CMP_ERR);
1482 kprintf("saregister: no getdev CCB, can't register device\n");
1483 return (CAM_REQ_CMP_ERR);
1486 softc = kmalloc(sizeof (*softc), M_SCSISA, M_INTWAIT | M_ZERO);
1487 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1488 softc->state = SA_STATE_NORMAL;
1489 softc->fileno = (daddr_t) -1;
1490 softc->blkno = (daddr_t) -1;
1492 bioq_init(&softc->bio_queue);
1493 periph->softc = softc;
1494 cam_extend_set(saperiphs, periph->unit_number, periph);
1497 * See if this device has any quirks.
1499 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1500 (caddr_t)sa_quirk_table,
1501 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1502 sizeof(*sa_quirk_table), scsi_inquiry_match);
1504 if (match != NULL) {
1505 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1506 softc->last_media_blksize =
1507 ((struct sa_quirk_entry *)match)->prefblk;
1509 xpt_print(periph->path, "found quirk entry %d\n",
1510 (int) (((struct sa_quirk_entry *) match) - sa_quirk_table));
1513 softc->quirks = SA_QUIRK_NONE;
1516 * The SA driver supports a blocksize, but we don't know the
1517 * blocksize until we media is inserted. So, set a flag to
1518 * indicate that the blocksize is unavailable right now.
1520 cam_periph_unlock(periph);
1521 devstat_add_entry(&softc->device_stats, "sa", periph->unit_number, 0,
1522 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1523 DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE);
1525 dev_ops_add(&sa_ops, SA_UNITMASK, SA_UNIT(periph->unit_number));
1526 make_dev(&sa_ops, SAMINOR(SA_CTLDEV,
1527 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1528 0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1530 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1531 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1532 0660, "%s%d", periph->periph_name, periph->unit_number);
1534 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1535 periph->unit_number, 0, SA_ATYPE_NR), UID_ROOT, GID_OPERATOR,
1536 0660, "n%s%d", periph->periph_name, periph->unit_number);
1538 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1539 periph->unit_number, 0, SA_ATYPE_ER), UID_ROOT, GID_OPERATOR,
1540 0660, "e%s%d", periph->periph_name, periph->unit_number);
1542 for (i = 0; i < SA_NUM_MODES; i++) {
1545 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R),
1546 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1547 periph->periph_name, periph->unit_number, i);
1550 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR),
1551 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1552 periph->periph_name, periph->unit_number, i);
1556 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER),
1557 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1558 periph->periph_name, periph->unit_number, i);
1560 cam_periph_lock(periph);
1563 * Add an async callback so that we get
1564 * notified if this device goes away.
1566 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path);
1568 xpt_announce_periph(periph, NULL);
1570 return (CAM_REQ_CMP);
1574 sastart(struct cam_periph *periph, union ccb *start_ccb)
1576 struct sa_softc *softc;
1578 softc = (struct sa_softc *)periph->softc;
1580 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
1583 switch (softc->state) {
1584 case SA_STATE_NORMAL:
1586 /* Pull a buffer from the queue and get going on it */
1591 * See if there is a buf with work for us to do..
1593 bio = bioq_first(&softc->bio_queue);
1594 if (periph->immediate_priority <= periph->pinfo.priority) {
1595 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1596 ("queuing for immediate ccb\n"));
1597 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1598 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1600 periph->immediate_priority = CAM_PRIORITY_NONE;
1601 wakeup(&periph->ccb_list);
1602 } else if (bio == NULL) {
1603 xpt_release_ccb(start_ccb);
1604 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1605 struct bio *done_bio;
1607 softc->queue_count--;
1608 bioq_remove(&softc->bio_queue, bio);
1610 bp->b_resid = bp->b_bcount;
1612 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1614 * We now just clear errors in this case
1615 * and let the residual be the notifier.
1618 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1620 * This can only happen if we're reading
1621 * in fixed length mode. In this case,
1622 * we dump the rest of the list the
1626 if (bioq_first(&softc->bio_queue) != NULL) {
1630 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1632 bp->b_flags |= B_ERROR;
1634 bio = bioq_first(&softc->bio_queue);
1636 * Only if we have no other buffers queued up
1637 * do we clear the pending error flag.
1640 softc->flags &= ~SA_FLAG_ERR_PENDING;
1641 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1642 ("sastart- ERR_PENDING now 0x%x, bio is %sNULL, "
1643 "%d more buffers queued up\n",
1644 (softc->flags & SA_FLAG_ERR_PENDING),
1645 (bio != NULL)? "not " : " ", softc->queue_count));
1646 xpt_release_ccb(start_ccb);
1651 bioq_remove(&softc->bio_queue, bio);
1653 softc->queue_count--;
1655 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1656 if (softc->blk_shift != 0) {
1658 bp->b_bcount >> softc->blk_shift;
1659 } else if (softc->media_blksize != 0) {
1661 bp->b_bcount / softc->media_blksize;
1664 xpt_print(periph->path, "zero blocksize"
1665 " for FIXED length writes?\n");
1670 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1671 ("issuing a %d fixed record %s\n",
1672 length, (bp->bio_cmd == BIO_READ)? "read" :
1676 length = bp->b_bcount;
1678 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1679 ("issuing a %d variable byte %s\n",
1680 length, (bp->bio_cmd == BIO_READ)? "read" :
1684 devstat_start_transaction(&softc->device_stats);
1686 * Some people have theorized that we should
1687 * suppress illegal length indication if we are
1688 * running in variable block mode so that we don't
1689 * have to request sense every time our requested
1690 * block size is larger than the written block.
1691 * The residual information from the ccb allows
1692 * us to identify this situation anyway. The only
1693 * problem with this is that we will not get
1694 * information about blocks that are larger than
1695 * our read buffer unless we set the block size
1696 * in the mode page to something other than 0.
1698 * I believe that this is a non-issue. If user apps
1699 * don't adjust their read size to match our record
1700 * size, that's just life. Anyway, the typical usage
1701 * would be to issue, e.g., 64KB reads and occasionally
1702 * have to do deal with 512 byte or 1KB intermediate
1705 softc->dsreg = (bp->b_cmd == BUF_CMD_READ) ?
1706 MTIO_DSREG_RD : MTIO_DSREG_WR;
1707 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1708 MSG_SIMPLE_Q_TAG, (bp->b_cmd == BUF_CMD_READ) != 0,
1709 FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1710 length, bp->b_data, bp->b_bcount, SSD_FULL_SIZE,
1712 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1713 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1714 start_ccb->ccb_h.ccb_bio = bio;
1715 bio = bioq_first(&softc->bio_queue);
1716 xpt_action(start_ccb);
1720 /* Have more work to do, so ensure we stay scheduled */
1721 xpt_schedule(periph, 1);
1725 case SA_STATE_ABNORMAL:
1727 panic("state 0x%x in sastart", softc->state);
1734 sadone(struct cam_periph *periph, union ccb *done_ccb)
1736 struct sa_softc *softc;
1737 struct ccb_scsiio *csio;
1739 softc = (struct sa_softc *)periph->softc;
1740 csio = &done_ccb->csio;
1741 switch (CCB_Type(csio)) {
1742 case SA_CCB_BUFFER_IO:
1748 softc->dsreg = MTIO_DSREG_REST;
1749 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1752 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1753 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1755 * A retry was scheduled, so just return.
1766 * Catastrophic error. Mark the tape as frozen
1767 * (we no longer know tape position).
1769 * Return all queued I/O with EIO, and unfreeze
1770 * our queue so that future transactions that
1771 * attempt to fix this problem can get to the
1776 softc->flags |= SA_FLAG_TAPE_FROZEN;
1777 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) {
1778 bioq_remove(&softc->bio_queue, q_bio);
1779 q_bp = q_bio->bio_buf;
1780 q_bp->b_resid = q_bp->b_bcount;
1781 q_bp->b_error = EIO;
1782 q_bp->b_flags |= B_ERROR;
1787 bp->b_resid = bp->b_bcount;
1788 bp->b_error = error;
1789 bp->b_flags |= B_ERROR;
1791 * In the error case, position is updated in saerror.
1794 bp->b_resid = csio->resid;
1796 if (csio->resid != 0) {
1797 bp->b_flags |= B_ERROR;
1799 if (bp->b_cmd != BUF_CMD_READ) {
1800 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1801 softc->filemarks = 0;
1803 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1804 (softc->blkno != (daddr_t) -1)) {
1805 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1807 if (softc->blk_shift != 0) {
1812 softc->media_blksize;
1814 softc->blkno += (daddr_t) l;
1821 * If we had an error (immediate or pending),
1822 * release the device queue now.
1824 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1825 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1827 if (error || bp->b_resid) {
1828 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1829 ("error %d resid %d count %d\n", error,
1830 bp->b_resid, bp->b_bcount));
1833 devstat_end_transaction_buf(&softc->device_stats, bp);
1837 case SA_CCB_WAITING:
1839 /* Caller will release the CCB */
1840 wakeup(&done_ccb->ccb_h.cbfcnp);
1844 xpt_release_ccb(done_ccb);
1848 * Mount the tape (make sure it's ready for I/O).
1851 samount(struct cam_periph *periph, int oflags, cdev_t dev)
1853 struct sa_softc *softc;
1858 * oflags can be checked for 'kind' of open (read-only check) - later
1859 * dev can be checked for a control-mode or compression open - later
1861 UNUSED_PARAMETER(oflags);
1862 UNUSED_PARAMETER(dev);
1865 softc = (struct sa_softc *)periph->softc;
1868 * This should determine if something has happend since the last
1869 * open/mount that would invalidate the mount. We do *not* want
1870 * to retry this command- we just want the status. But we only
1871 * do this if we're mounted already- if we're not mounted,
1872 * we don't care about the unit read state and can instead use
1873 * this opportunity to attempt to reserve the tape unit.
1876 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1877 ccb = cam_periph_getccb(periph, 1);
1878 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1879 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1880 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1881 &softc->device_stats);
1883 if (error == ENXIO) {
1884 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1885 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1886 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1887 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1888 &softc->device_stats);
1892 * We don't need to freeze the tape because we
1893 * will now attempt to rewind/load it.
1895 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1896 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
1897 xpt_print(periph->path,
1898 "error %d on TUR in samount\n", error);
1902 error = sareservereleaseunit(periph, TRUE);
1906 ccb = cam_periph_getccb(periph, 1);
1907 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1908 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1909 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1910 &softc->device_stats);
1914 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1915 struct scsi_read_block_limits_data *rblim = NULL;
1916 int comp_enabled, comp_supported;
1917 u_int8_t write_protect, guessing = 0;
1920 * Clear out old state.
1922 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1923 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1924 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1925 softc->filemarks = 0;
1928 * *Very* first off, make sure we're loaded to BOT.
1930 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1931 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1932 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1933 &softc->device_stats);
1937 * In case this doesn't work, do a REWIND instead
1940 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1941 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1942 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1943 &softc->device_stats);
1947 xpt_release_ccb(ccb);
1952 * Do a dummy test read to force access to the
1953 * media so that the drive will really know what's
1954 * there. We actually don't really care what the
1955 * blocksize on tape is and don't expect to really
1956 * read a full record.
1958 rblim = kmalloc(8192, M_SCSISA, M_INTWAIT);
1960 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1961 scsi_sa_read_write(&ccb->csio, 0, sadone,
1962 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1963 (void *) rblim, 8192, SSD_FULL_SIZE,
1965 cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1966 &softc->device_stats);
1968 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1969 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1970 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1971 SF_NO_PRINT | SF_RETRY_UA,
1972 &softc->device_stats);
1975 xpt_print(periph->path,
1976 "unable to rewind after test read\n");
1977 xpt_release_ccb(ccb);
1983 * Next off, determine block limits.
1985 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1986 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1988 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1989 SF_NO_PRINT | SF_RETRY_UA, &softc->device_stats);
1992 xpt_release_ccb(ccb);
1996 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1997 * a MANDATORY command. Anyway- it doesn't matter-
1998 * we can proceed anyway.
2000 softc->blk_gran = 0;
2001 softc->max_blk = ~0;
2004 if (softc->scsi_rev >= SCSI_REV_SPC) {
2005 softc->blk_gran = RBL_GRAN(rblim);
2007 softc->blk_gran = 0;
2010 * We take max_blk == min_blk to mean a default to
2011 * fixed mode- but note that whatever we get out of
2012 * sagetparams below will actually determine whether
2013 * we are actually *in* fixed mode.
2015 softc->max_blk = scsi_3btoul(rblim->maximum);
2016 softc->min_blk = scsi_2btoul(rblim->minimum);
2021 * Next, perform a mode sense to determine
2022 * current density, blocksize, compression etc.
2024 error = sagetparams(periph, SA_PARAM_ALL,
2025 &softc->media_blksize,
2026 &softc->media_density,
2027 &softc->media_numblks,
2028 &softc->buffer_mode, &write_protect,
2029 &softc->speed, &comp_supported,
2030 &comp_enabled, &softc->comp_algorithm,
2035 * We could work a little harder here. We could
2036 * adjust our attempts to get information. It
2037 * might be an ancient tape drive. If someone
2038 * nudges us, we'll do that.
2044 * If no quirk has determined that this is a device that is
2045 * preferred to be in fixed or variable mode, now is the time
2048 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2051 * This could be expensive to find out. Luckily we
2052 * only need to do this once. If we start out in
2053 * 'default' mode, try and set ourselves to one
2054 * of the densities that would determine a wad
2055 * of other stuff. Go from highest to lowest.
2057 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2059 static u_int8_t ctry[] = {
2060 SCSI_DENSITY_HALFINCH_PE,
2061 SCSI_DENSITY_HALFINCH_6250C,
2062 SCSI_DENSITY_HALFINCH_6250,
2063 SCSI_DENSITY_HALFINCH_1600,
2064 SCSI_DENSITY_HALFINCH_800,
2065 SCSI_DENSITY_QIC_4GB,
2066 SCSI_DENSITY_QIC_2GB,
2067 SCSI_DENSITY_QIC_525_320,
2068 SCSI_DENSITY_QIC_150,
2069 SCSI_DENSITY_QIC_120,
2070 SCSI_DENSITY_QIC_24,
2071 SCSI_DENSITY_QIC_11_9TRK,
2072 SCSI_DENSITY_QIC_11_4TRK,
2073 SCSI_DENSITY_QIC_1320,
2074 SCSI_DENSITY_QIC_3080,
2077 for (i = 0; ctry[i]; i++) {
2078 error = sasetparams(periph,
2079 SA_PARAM_DENSITY, 0, ctry[i],
2082 softc->media_density = ctry[i];
2087 switch (softc->media_density) {
2088 case SCSI_DENSITY_QIC_11_4TRK:
2089 case SCSI_DENSITY_QIC_11_9TRK:
2090 case SCSI_DENSITY_QIC_24:
2091 case SCSI_DENSITY_QIC_120:
2092 case SCSI_DENSITY_QIC_150:
2093 case SCSI_DENSITY_QIC_525_320:
2094 case SCSI_DENSITY_QIC_1320:
2095 case SCSI_DENSITY_QIC_3080:
2096 softc->quirks &= ~SA_QUIRK_2FM;
2097 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2098 softc->last_media_blksize = 512;
2100 case SCSI_DENSITY_QIC_4GB:
2101 case SCSI_DENSITY_QIC_2GB:
2102 softc->quirks &= ~SA_QUIRK_2FM;
2103 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2104 softc->last_media_blksize = 1024;
2107 softc->last_media_blksize =
2108 softc->media_blksize;
2109 softc->quirks |= SA_QUIRK_VARIABLE;
2115 * If no quirk has determined that this is a device that needs
2116 * to have 2 Filemarks at EOD, now is the time to find out.
2119 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2120 switch (softc->media_density) {
2121 case SCSI_DENSITY_HALFINCH_800:
2122 case SCSI_DENSITY_HALFINCH_1600:
2123 case SCSI_DENSITY_HALFINCH_6250:
2124 case SCSI_DENSITY_HALFINCH_6250C:
2125 case SCSI_DENSITY_HALFINCH_PE:
2126 softc->quirks &= ~SA_QUIRK_1FM;
2127 softc->quirks |= SA_QUIRK_2FM;
2135 * Now validate that some info we got makes sense.
2137 if ((softc->max_blk < softc->media_blksize) ||
2138 (softc->min_blk > softc->media_blksize &&
2139 softc->media_blksize)) {
2140 xpt_print(periph->path,
2141 "BLOCK LIMITS (%d..%d) could not match current "
2142 "block settings (%d)- adjusting\n", softc->min_blk,
2143 softc->max_blk, softc->media_blksize);
2144 softc->max_blk = softc->min_blk =
2145 softc->media_blksize;
2149 * Now put ourselves into the right frame of mind based
2154 * If we want to be in FIXED mode and our current blocksize
2155 * is not equal to our last blocksize (if nonzero), try and
2156 * set ourselves to this last blocksize (as the 'preferred'
2157 * block size). The initial quirkmatch at registry sets the
2158 * initial 'last' blocksize. If, for whatever reason, this
2159 * 'last' blocksize is zero, set the blocksize to 512,
2160 * or min_blk if that's larger.
2162 if ((softc->quirks & SA_QUIRK_FIXED) &&
2163 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2164 (softc->media_blksize != softc->last_media_blksize)) {
2165 softc->media_blksize = softc->last_media_blksize;
2166 if (softc->media_blksize == 0) {
2167 softc->media_blksize = 512;
2168 if (softc->media_blksize < softc->min_blk) {
2169 softc->media_blksize = softc->min_blk;
2172 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2173 softc->media_blksize, 0, 0, SF_NO_PRINT);
2175 xpt_print(periph->path,
2176 "unable to set fixed blocksize to %d\n",
2177 softc->media_blksize);
2182 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2183 (softc->media_blksize != 0)) {
2184 softc->last_media_blksize = softc->media_blksize;
2185 softc->media_blksize = 0;
2186 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2187 0, 0, 0, SF_NO_PRINT);
2190 * If this fails and we were guessing, just
2191 * assume that we got it wrong and go try
2192 * fixed block mode. Don't even check against
2193 * density code at this point.
2196 softc->quirks &= ~SA_QUIRK_VARIABLE;
2197 softc->quirks |= SA_QUIRK_FIXED;
2198 if (softc->last_media_blksize == 0)
2199 softc->last_media_blksize = 512;
2202 xpt_print(periph->path,
2203 "unable to set variable blocksize\n");
2209 * Now that we have the current block size,
2210 * set up some parameters for sastart's usage.
2212 if (softc->media_blksize) {
2213 softc->flags |= SA_FLAG_FIXED;
2214 if (powerof2(softc->media_blksize)) {
2216 ffs(softc->media_blksize) - 1;
2217 softc->blk_mask = softc->media_blksize - 1;
2219 softc->blk_mask = ~0;
2220 softc->blk_shift = 0;
2224 * The SCSI-3 spec allows 0 to mean "unspecified".
2225 * The SCSI-1 spec allows 0 to mean 'infinite'.
2227 * Either works here.
2229 if (softc->max_blk == 0) {
2230 softc->max_blk = ~0;
2232 softc->blk_shift = 0;
2233 if (softc->blk_gran != 0) {
2234 softc->blk_mask = softc->blk_gran - 1;
2236 softc->blk_mask = 0;
2241 softc->flags |= SA_FLAG_TAPE_WP;
2243 if (comp_supported) {
2244 if (softc->saved_comp_algorithm == 0)
2245 softc->saved_comp_algorithm =
2246 softc->comp_algorithm;
2247 softc->flags |= SA_FLAG_COMP_SUPP;
2249 softc->flags |= SA_FLAG_COMP_ENABLED;
2251 softc->flags |= SA_FLAG_COMP_UNSUPP;
2253 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2254 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2255 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2258 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2260 xpt_print(periph->path,
2261 "unable to set buffered mode\n");
2263 error = 0; /* not an error */
2268 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2272 kfree(rblim, M_SCSISA);
2275 softc->dsreg = MTIO_DSREG_NIL;
2277 softc->fileno = softc->blkno = 0;
2278 softc->dsreg = MTIO_DSREG_REST;
2280 #ifdef SA_1FM_AT_EOD
2281 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2282 softc->quirks |= SA_QUIRK_1FM;
2284 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2285 softc->quirks |= SA_QUIRK_2FM;
2288 xpt_release_ccb(ccb);
2291 * If we return an error, we're not mounted any more,
2292 * so release any device reservation.
2295 sareservereleaseunit(periph, FALSE);
2298 * Clear I/O residual.
2300 softc->last_io_resid = 0;
2301 softc->last_ctl_resid = 0;
2307 * How many filemarks do we need to write if we were to terminate the
2308 * tape session right now? Note that this can be a negative number
2312 samarkswanted(struct cam_periph *periph)
2315 struct sa_softc *softc;
2317 softc = (struct sa_softc *)periph->softc;
2319 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2321 if (softc->quirks & SA_QUIRK_2FM)
2324 markswanted -= softc->filemarks;
2325 return (markswanted);
2329 sacheckeod(struct cam_periph *periph)
2334 markswanted = samarkswanted(periph);
2336 if (markswanted > 0) {
2337 error = sawritefilemarks(periph, markswanted, FALSE);
2345 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2347 static const char *toobig =
2348 "%d-byte tape record bigger than supplied buffer\n";
2349 struct cam_periph *periph;
2350 struct sa_softc *softc;
2351 struct ccb_scsiio *csio;
2352 struct scsi_sense_data *sense;
2353 u_int32_t resid = 0;
2356 int error_code, sense_key, asc, ascq, error, aqvalid;
2358 periph = xpt_path_periph(ccb->ccb_h.path);
2359 softc = (struct sa_softc *)periph->softc;
2361 sense = &csio->sense_data;
2362 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2363 aqvalid = sense->extra_len >= 6;
2366 status = csio->ccb_h.status & CAM_STATUS_MASK;
2369 * Calculate/latch up, any residuals... We do this in a funny 2-step
2370 * so we can print stuff here if we have CAM_DEBUG enabled for this
2373 if (status == CAM_SCSI_STATUS_ERROR) {
2374 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2375 info = (int32_t) scsi_4btoul(sense->info);
2377 if ((softc->flags & SA_FLAG_FIXED) != 0)
2378 resid *= softc->media_blksize;
2380 resid = csio->dxfer_len;
2382 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2383 if (softc->media_blksize)
2384 info /= softc->media_blksize;
2387 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2388 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2389 sizeof (struct scsi_sense_data));
2390 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2391 (int) csio->cdb_len);
2392 softc->last_io_resid = resid;
2393 softc->last_resid_was_io = 1;
2395 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2396 sizeof (struct scsi_sense_data));
2397 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2398 (int) csio->cdb_len);
2399 softc->last_ctl_resid = resid;
2400 softc->last_resid_was_io = 0;
2402 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2403 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %d "
2404 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2405 sense_key, asc, ascq, status,
2406 sense->flags & ~SSD_KEY_RESERVED, resid, csio->dxfer_len));
2408 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2409 ("Cam Status 0x%x\n", status));
2415 case CAM_SCSI_STATUS_ERROR:
2417 * If a read/write command, we handle it here.
2419 if (CCB_Type(csio) != SA_CCB_WAITING) {
2423 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2424 * on a non read/write command, we assume it's not an error
2425 * and propagate the residule and return.
2427 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2428 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2429 csio->resid = resid;
2434 * Otherwise, we let the common code handle this.
2436 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2440 * We cannot depend upon CAM honoring retry counts for these.
2442 case CAM_SCSI_BUS_RESET:
2444 if (ccb->ccb_h.retry_count <= 0) {
2449 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2453 * Handle filemark, end of tape, mismatched record sizes....
2454 * From this point out, we're only handling read/write cases.
2455 * Handle writes && reads differently.
2458 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2459 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2460 csio->resid = resid;
2462 } else if (sense->flags & SSD_EOM) {
2463 softc->flags |= SA_FLAG_EOM_PENDING;
2465 * Grotesque as it seems, the few times
2466 * I've actually seen a non-zero resid,
2467 * the tape drive actually lied and had
2468 * written all the data!.
2473 csio->resid = resid;
2474 if (sense_key == SSD_KEY_BLANK_CHECK) {
2475 if (softc->quirks & SA_QUIRK_1FM) {
2477 softc->flags |= SA_FLAG_EOM_PENDING;
2481 } else if (sense->flags & SSD_FILEMARK) {
2482 if (softc->flags & SA_FLAG_FIXED) {
2484 softc->flags |= SA_FLAG_EOF_PENDING;
2487 * Unconditionally, if we detected a filemark on a read,
2488 * mark that we've run moved a file ahead.
2490 if (softc->fileno != (daddr_t) -1) {
2493 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2499 * Incorrect Length usually applies to read, but can apply to writes.
2501 if (error == 0 && (sense->flags & SSD_ILI)) {
2503 xpt_print(csio->ccb_h.path, toobig,
2504 csio->dxfer_len - info);
2505 csio->resid = csio->dxfer_len;
2508 csio->resid = resid;
2509 if (softc->flags & SA_FLAG_FIXED) {
2510 softc->flags |= SA_FLAG_EIO_PENDING;
2513 * Bump the block number if we hadn't seen a filemark.
2514 * Do this independent of errors (we've moved anyway).
2516 if ((sense->flags & SSD_FILEMARK) == 0) {
2517 if (softc->blkno != (daddr_t) -1) {
2519 csio->ccb_h.ccb_pflags |=
2520 SA_POSITION_UPDATED;
2528 * Unfreeze the queue if frozen as we're not returning anything
2529 * to our waiters that would indicate an I/O error has occurred
2539 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2540 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2541 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2542 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2547 struct scsi_mode_header_6 *mode_hdr;
2548 struct scsi_mode_blk_desc *mode_blk;
2549 int mode_buffer_len;
2550 struct sa_softc *softc;
2555 softc = (struct sa_softc *)periph->softc;
2556 ccb = cam_periph_getccb(periph, 1);
2557 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2558 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2560 cpage = SA_DATA_COMPRESSION_PAGE;
2563 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2565 if (params_to_get & SA_PARAM_COMPRESSION) {
2566 if (softc->quirks & SA_QUIRK_NOCOMP) {
2567 *comp_supported = FALSE;
2568 params_to_get &= ~SA_PARAM_COMPRESSION;
2570 mode_buffer_len += sizeof (sa_comp_t);
2573 mode_buffer = kmalloc(mode_buffer_len, M_SCSISA, M_INTWAIT | M_ZERO);
2574 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2575 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2577 /* it is safe to retry this */
2578 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2579 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2580 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2581 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2583 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2584 &softc->device_stats);
2587 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2589 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2591 * Hmm. Let's see if we can try another page...
2592 * If we've already done that, give up on compression
2593 * for this device and remember this for the future
2594 * and attempt the request without asking for compression
2597 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2598 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2601 softc->quirks |= SA_QUIRK_NOCOMP;
2602 kfree(mode_buffer, M_SCSISA);
2604 } else if (status == CAM_SCSI_STATUS_ERROR) {
2605 /* Tell the user about the fatal error. */
2606 scsi_sense_print(&ccb->csio);
2607 goto sagetparamsexit;
2611 * If the user only wants the compression information, and
2612 * the device doesn't send back the block descriptor, it's
2613 * no big deal. If the user wants more than just
2614 * compression, though, and the device doesn't pass back the
2615 * block descriptor, we need to send another mode sense to
2616 * get the block descriptor.
2618 if ((mode_hdr->blk_desc_len == 0) &&
2619 (params_to_get & SA_PARAM_COMPRESSION) &&
2620 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2623 * Decrease the mode buffer length by the size of
2624 * the compression page, to make sure the data
2625 * there doesn't get overwritten.
2627 mode_buffer_len -= sizeof (sa_comp_t);
2630 * Now move the compression page that we presumably
2631 * got back down the memory chunk a little bit so
2632 * it doesn't get spammed.
2634 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2635 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2638 * Now, we issue another mode sense and just ask
2639 * for the block descriptor, etc.
2642 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2643 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2644 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2647 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2648 &softc->device_stats);
2652 goto sagetparamsexit;
2655 if (params_to_get & SA_PARAM_BLOCKSIZE)
2656 *blocksize = scsi_3btoul(mode_blk->blklen);
2658 if (params_to_get & SA_PARAM_NUMBLOCKS)
2659 *numblocks = scsi_3btoul(mode_blk->nblocks);
2661 if (params_to_get & SA_PARAM_BUFF_MODE)
2662 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2664 if (params_to_get & SA_PARAM_DENSITY)
2665 *density = mode_blk->density;
2667 if (params_to_get & SA_PARAM_WP)
2668 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2670 if (params_to_get & SA_PARAM_SPEED)
2671 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2673 if (params_to_get & SA_PARAM_COMPRESSION) {
2674 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2675 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2676 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2678 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2680 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2681 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2683 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2685 * We don't really know whether this device supports
2686 * Data Compression if the the algorithm field is
2687 * zero. Just say we do.
2689 *comp_supported = TRUE;
2691 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2692 *comp_algorithm = cp->sel_comp_alg;
2695 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2698 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2700 char *xyz = mode_buffer;
2701 xpt_print_path(periph->path);
2702 kprintf("Mode Sense Data=");
2703 for (idx = 0; idx < mode_buffer_len; idx++)
2704 kprintf(" 0x%02x", xyz[idx] & 0xff);
2710 xpt_release_ccb(ccb);
2711 kfree(mode_buffer, M_SCSISA);
2716 * The purpose of this function is to set one of four different parameters
2720 * - compression / compression algorithm
2723 * The assumption is that this will be called from saioctl(), and therefore
2724 * from a process context. Thus the waiting malloc calls below. If that
2725 * assumption ever changes, the malloc calls should be changed to be
2728 * Any or all of the four parameters may be set when this function is
2729 * called. It should handle setting more than one parameter at once.
2732 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2733 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2734 u_int32_t sense_flags)
2736 struct sa_softc *softc;
2737 u_int32_t current_blocksize;
2738 u_int32_t current_calg;
2739 u_int8_t current_density;
2740 u_int8_t current_speed;
2741 int comp_enabled, comp_supported;
2743 int mode_buffer_len;
2744 struct scsi_mode_header_6 *mode_hdr;
2745 struct scsi_mode_blk_desc *mode_blk;
2746 sa_comp_t *ccomp, *cpage;
2748 union ccb *ccb = NULL;
2751 softc = (struct sa_softc *)periph->softc;
2753 ccomp = kmalloc(sizeof (sa_comp_t), M_SCSISA, M_INTWAIT);
2756 * Since it doesn't make sense to set the number of blocks, or
2757 * write protection, we won't try to get the current value. We
2758 * always want to get the blocksize, so we can set it back to the
2761 error = sagetparams(periph,
2762 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2763 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2764 ¤t_speed, &comp_supported, &comp_enabled,
2765 ¤t_calg, ccomp);
2768 kfree(ccomp, M_SCSISA);
2772 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2773 if (params_to_set & SA_PARAM_COMPRESSION)
2774 mode_buffer_len += sizeof (sa_comp_t);
2776 mode_buffer = kmalloc(mode_buffer_len, M_SCSISA, M_INTWAIT | M_ZERO);
2778 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2779 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2781 ccb = cam_periph_getccb(periph, 1);
2785 if (params_to_set & SA_PARAM_COMPRESSION) {
2787 cpage = (sa_comp_t *)&mode_blk[1];
2789 cpage = (sa_comp_t *)&mode_hdr[1];
2791 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2792 cpage->hdr.pagecode &= ~0x80;
2797 * If the caller wants us to set the blocksize, use the one they
2798 * pass in. Otherwise, use the blocksize we got back from the
2799 * mode select above.
2802 if (params_to_set & SA_PARAM_BLOCKSIZE)
2803 scsi_ulto3b(blocksize, mode_blk->blklen);
2805 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2808 * Set density if requested, else preserve old density.
2809 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2810 * devices, else density we've latched up in our softc.
2812 if (params_to_set & SA_PARAM_DENSITY) {
2813 mode_blk->density = density;
2814 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2815 mode_blk->density = SCSI_SAME_DENSITY;
2817 mode_blk->density = softc->media_density;
2822 * For mode selects, these two fields must be zero.
2824 mode_hdr->data_length = 0;
2825 mode_hdr->medium_type = 0;
2827 /* set the speed to the current value */
2828 mode_hdr->dev_spec = current_speed;
2830 /* if set, set single-initiator buffering mode */
2831 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
2832 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2836 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2838 mode_hdr->blk_desc_len = 0;
2841 * First, if the user wants us to set the compression algorithm or
2842 * just turn compression on, check to make sure that this drive
2843 * supports compression.
2845 if (params_to_set & SA_PARAM_COMPRESSION) {
2847 * If the compression algorithm is 0, disable compression.
2848 * If the compression algorithm is non-zero, enable
2849 * compression and set the compression type to the
2850 * specified compression algorithm, unless the algorithm is
2851 * MT_COMP_ENABLE. In that case, we look at the
2852 * compression algorithm that is currently set and if it is
2853 * non-zero, we leave it as-is. If it is zero, and we have
2854 * saved a compression algorithm from a time when
2855 * compression was enabled before, set the compression to
2858 switch (ccomp->hdr.pagecode & ~0x80) {
2859 case SA_DEVICE_CONFIGURATION_PAGE:
2861 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2863 dcp->sel_comp_alg = SA_COMP_NONE;
2866 if (calg != MT_COMP_ENABLE) {
2867 dcp->sel_comp_alg = calg;
2868 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2869 softc->saved_comp_algorithm != 0) {
2870 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2874 case SA_DATA_COMPRESSION_PAGE:
2875 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2876 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2879 * Disable compression, but leave the
2880 * decompression and the capability bit
2883 dcp->dce_and_dcc = SA_DCP_DCC;
2884 dcp->dde_and_red |= SA_DCP_DDE;
2887 /* enable compression && decompression */
2888 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2889 dcp->dde_and_red |= SA_DCP_DDE;
2891 * If there, use compression algorithm from caller.
2892 * Otherwise, if there's a saved compression algorithm
2893 * and there is no current algorithm, use the saved
2894 * algorithm. Else parrot back what we got and hope
2897 if (calg != MT_COMP_ENABLE) {
2898 scsi_ulto4b(calg, dcp->comp_algorithm);
2899 scsi_ulto4b(calg, dcp->decomp_algorithm);
2900 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2901 softc->saved_comp_algorithm != 0) {
2902 scsi_ulto4b(softc->saved_comp_algorithm,
2903 dcp->comp_algorithm);
2904 scsi_ulto4b(softc->saved_comp_algorithm,
2905 dcp->decomp_algorithm);
2910 * Compression does not appear to be supported-
2911 * at least via the DATA COMPRESSION page. It
2912 * would be too much to ask us to believe that
2913 * the page itself is supported, but incorrectly
2914 * reports an ability to manipulate data compression,
2915 * so we'll assume that this device doesn't support
2916 * compression. We can just fall through for that.
2921 * The drive doesn't seem to support compression,
2922 * so turn off the set compression bit.
2924 params_to_set &= ~SA_PARAM_COMPRESSION;
2925 xpt_print(periph->path,
2926 "device does not seem to support compression\n");
2929 * If that was the only thing the user wanted us to set,
2930 * clean up allocated resources and return with
2931 * 'operation not supported'.
2933 if (params_to_set == SA_PARAM_NONE) {
2934 kfree(mode_buffer, M_SCSISA);
2935 xpt_release_ccb(ccb);
2940 * That wasn't the only thing the user wanted us to set.
2941 * So, decrease the stated mode buffer length by the
2942 * size of the compression mode page.
2944 mode_buffer_len -= sizeof(sa_comp_t);
2948 /* It is safe to retry this operation */
2949 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2950 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2951 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2953 error = cam_periph_runccb(ccb, saerror, 0,
2954 sense_flags, &softc->device_stats);
2957 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2959 char *xyz = mode_buffer;
2960 xpt_print_path(periph->path);
2961 kprintf("Err%d, Mode Select Data=", error);
2962 for (idx = 0; idx < mode_buffer_len; idx++)
2963 kprintf(" 0x%02x", xyz[idx] & 0xff);
2970 * If we can, try without setting density/blocksize.
2973 if ((params_to_set &
2974 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2979 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2980 cpage = (sa_comp_t *)&mode_blk[1];
2984 * If we were setting the blocksize, and that failed, we
2985 * want to set it to its original value. If we weren't
2986 * setting the blocksize, we don't want to change it.
2988 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2991 * Set density if requested, else preserve old density.
2992 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2993 * devices, else density we've latched up in our softc.
2995 if (params_to_set & SA_PARAM_DENSITY) {
2996 mode_blk->density = current_density;
2997 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2998 mode_blk->density = SCSI_SAME_DENSITY;
3000 mode_blk->density = softc->media_density;
3003 if (params_to_set & SA_PARAM_COMPRESSION)
3004 bcopy(ccomp, cpage, sizeof (sa_comp_t));
3007 * The retry count is the only CCB field that might have been
3008 * changed that we care about, so reset it back to 1.
3010 ccb->ccb_h.retry_count = 1;
3011 cam_periph_runccb(ccb, saerror, 0, sense_flags,
3012 &softc->device_stats);
3016 xpt_release_ccb(ccb);
3019 kfree(ccomp, M_SCSISA);
3021 if (params_to_set & SA_PARAM_COMPRESSION) {
3023 softc->flags &= ~SA_FLAG_COMP_ENABLED;
3025 * Even if we get an error setting compression,
3026 * do not say that we don't support it. We could
3027 * have been wrong, or it may be media specific.
3028 * softc->flags &= ~SA_FLAG_COMP_SUPP;
3030 softc->saved_comp_algorithm = softc->comp_algorithm;
3031 softc->comp_algorithm = 0;
3033 softc->flags |= SA_FLAG_COMP_ENABLED;
3034 softc->comp_algorithm = calg;
3038 kfree(mode_buffer, M_SCSISA);
3043 saprevent(struct cam_periph *periph, int action)
3045 struct sa_softc *softc;
3049 softc = (struct sa_softc *)periph->softc;
3051 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3053 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3057 * We can be quiet about illegal requests.
3059 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3064 ccb = cam_periph_getccb(periph, 1);
3066 /* It is safe to retry this operation */
3067 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3068 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3070 error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats);
3073 if (action == PR_ALLOW)
3074 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3076 softc->flags |= SA_FLAG_TAPE_LOCKED;
3079 xpt_release_ccb(ccb);
3083 sarewind(struct cam_periph *periph)
3086 struct sa_softc *softc;
3089 softc = (struct sa_softc *)periph->softc;
3091 ccb = cam_periph_getccb(periph, 1);
3093 /* It is safe to retry this operation */
3094 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3095 SSD_FULL_SIZE, REWIND_TIMEOUT);
3097 softc->dsreg = MTIO_DSREG_REW;
3098 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3099 softc->dsreg = MTIO_DSREG_REST;
3101 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3102 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3104 xpt_release_ccb(ccb);
3106 softc->fileno = softc->blkno = (daddr_t) 0;
3108 softc->fileno = softc->blkno = (daddr_t) -1;
3113 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3116 struct sa_softc *softc;
3119 softc = (struct sa_softc *)periph->softc;
3121 ccb = cam_periph_getccb(periph, 1);
3123 /* This cannot be retried */
3125 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3126 SSD_FULL_SIZE, SPACE_TIMEOUT);
3129 * Clear residual because we will be using it.
3131 softc->last_ctl_resid = 0;
3133 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3134 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3135 softc->dsreg = MTIO_DSREG_REST;
3137 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3138 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3140 xpt_release_ccb(ccb);
3143 * If a spacing operation has failed, we need to invalidate
3146 * If the spacing operation was setmarks or to end of recorded data,
3147 * we no longer know our relative position.
3149 * If the spacing operations was spacing files in reverse, we
3150 * take account of the residual, but still check against less
3151 * than zero- if we've gone negative, we must have hit BOT.
3153 * If the spacing operations was spacing records in reverse and
3154 * we have a residual, we've either hit BOT or hit a filemark.
3155 * In the former case, we know our new record number (0). In
3156 * the latter case, we have absolutely no idea what the real
3157 * record number is- we've stopped between the end of the last
3158 * record in the previous file and the filemark that stopped
3159 * our spacing backwards.
3162 softc->fileno = softc->blkno = (daddr_t) -1;
3163 } else if (code == SS_SETMARKS || code == SS_EOD) {
3164 softc->fileno = softc->blkno = (daddr_t) -1;
3165 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3166 softc->fileno += (count - softc->last_ctl_resid);
3167 if (softc->fileno < 0) /* we must of hit BOT */
3170 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3171 softc->blkno += (count - softc->last_ctl_resid);
3173 if (softc->last_ctl_resid || softc->blkno < 0) {
3174 if (softc->fileno == 0) {
3177 softc->blkno = (daddr_t) -1;
3186 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3189 struct sa_softc *softc;
3192 softc = (struct sa_softc *)periph->softc;
3193 if (softc->open_rdonly)
3196 ccb = cam_periph_getccb(periph, 1);
3198 * Clear residual because we will be using it.
3200 softc->last_ctl_resid = 0;
3202 softc->dsreg = MTIO_DSREG_FMK;
3203 /* this *must* not be retried */
3204 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3205 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3206 softc->dsreg = MTIO_DSREG_REST;
3209 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3211 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3212 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3214 if (error == 0 && nmarks) {
3215 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3216 nwm = nmarks - softc->last_ctl_resid;
3217 softc->filemarks += nwm;
3220 xpt_release_ccb(ccb);
3223 * Update relative positions (if we're doing that).
3226 softc->fileno = softc->blkno = (daddr_t) -1;
3227 } else if (softc->fileno != (daddr_t) -1) {
3228 softc->fileno += nwm;
3235 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3237 struct scsi_tape_position_data loc;
3239 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3243 * We try and flush any buffered writes here if we were writing
3244 * and we're trying to get hardware block position. It eats
3245 * up performance substantially, but I'm wary of drive firmware.
3247 * I think that *logical* block position is probably okay-
3248 * but hardware block position might have to wait for data
3249 * to hit media to be valid. Caveat Emptor.
3252 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3253 error = sawritefilemarks(periph, 0, 0);
3254 if (error && error != EACCES)
3258 ccb = cam_periph_getccb(periph, 1);
3259 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3260 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3261 softc->dsreg = MTIO_DSREG_RBSY;
3262 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3263 softc->dsreg = MTIO_DSREG_REST;
3264 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3265 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3268 if (loc.flags & SA_RPOS_UNCERTAIN) {
3269 error = EINVAL; /* nothing is certain */
3271 *blkptr = scsi_4btoul(loc.firstblk);
3275 xpt_release_ccb(ccb);
3280 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3283 struct sa_softc *softc;
3287 * We used to try and flush any buffered writes here.
3288 * Now we push this onto user applications to either
3289 * flush the pending writes themselves (via a zero count
3290 * WRITE FILEMARKS command) or they can trust their tape
3291 * drive to do this correctly for them.
3294 softc = (struct sa_softc *)periph->softc;
3295 ccb = cam_periph_getccb(periph, 1);
3298 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3299 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3302 softc->dsreg = MTIO_DSREG_POS;
3303 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3304 softc->dsreg = MTIO_DSREG_REST;
3305 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3306 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3307 xpt_release_ccb(ccb);
3309 * Note relative file && block number position as now unknown.
3311 softc->fileno = softc->blkno = (daddr_t) -1;
3316 saretension(struct cam_periph *periph)
3319 struct sa_softc *softc;
3322 softc = (struct sa_softc *)periph->softc;
3324 ccb = cam_periph_getccb(periph, 1);
3326 /* It is safe to retry this operation */
3327 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3328 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3330 softc->dsreg = MTIO_DSREG_TEN;
3331 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3332 softc->dsreg = MTIO_DSREG_REST;
3334 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3335 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3336 xpt_release_ccb(ccb);
3338 softc->fileno = softc->blkno = (daddr_t) 0;
3340 softc->fileno = softc->blkno = (daddr_t) -1;
3345 sareservereleaseunit(struct cam_periph *periph, int reserve)
3348 struct sa_softc *softc;
3351 softc = (struct sa_softc *)periph->softc;
3352 ccb = cam_periph_getccb(periph, 1);
3354 /* It is safe to retry this operation */
3355 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3356 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3357 softc->dsreg = MTIO_DSREG_RBSY;
3358 error = cam_periph_runccb(ccb, saerror, 0,
3359 SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats);
3360 softc->dsreg = MTIO_DSREG_REST;
3362 xpt_release_ccb(ccb);
3365 * If the error was Illegal Request, then the device doesn't support
3366 * RESERVE/RELEASE. This is not an error.
3368 if (error == EINVAL) {
3376 saloadunload(struct cam_periph *periph, int load)
3379 struct sa_softc *softc;
3382 softc = (struct sa_softc *)periph->softc;
3384 ccb = cam_periph_getccb(periph, 1);
3386 /* It is safe to retry this operation */
3387 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3388 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3390 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3391 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3392 softc->dsreg = MTIO_DSREG_REST;
3394 xpt_release_ccb(ccb);
3396 if (error || load == 0)
3397 softc->fileno = softc->blkno = (daddr_t) -1;
3398 else if (error == 0)
3399 softc->fileno = softc->blkno = (daddr_t) 0;
3404 saerase(struct cam_periph *periph, int longerase)
3408 struct sa_softc *softc;
3411 softc = (struct sa_softc *)periph->softc;
3412 if (softc->open_rdonly)
3415 ccb = cam_periph_getccb(periph, 1);
3417 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3418 SSD_FULL_SIZE, ERASE_TIMEOUT);
3420 softc->dsreg = MTIO_DSREG_ZER;
3421 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3422 softc->dsreg = MTIO_DSREG_REST;
3424 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3425 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3426 xpt_release_ccb(ccb);
3430 #endif /* _KERNEL */
3433 * Read tape block limits command.
3436 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3437 void (*cbfcnp)(struct cam_periph *, union ccb *),
3438 u_int8_t tag_action,
3439 struct scsi_read_block_limits_data *rlimit_buf,
3440 u_int8_t sense_len, u_int32_t timeout)
3442 struct scsi_read_block_limits *scsi_cmd;
3444 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3445 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3446 sizeof(*scsi_cmd), timeout);
3448 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3449 bzero(scsi_cmd, sizeof(*scsi_cmd));
3450 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3454 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3455 void (*cbfcnp)(struct cam_periph *, union ccb *),
3456 u_int8_t tag_action, int readop, int sli,
3457 int fixed, u_int32_t length, u_int8_t *data_ptr,
3458 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3460 struct scsi_sa_rw *scsi_cmd;
3462 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3463 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3464 scsi_cmd->sli_fixed = 0;
3466 scsi_cmd->sli_fixed |= SAR_SLI;
3468 scsi_cmd->sli_fixed |= SARW_FIXED;
3469 scsi_ulto3b(length, scsi_cmd->length);
3470 scsi_cmd->control = 0;
3472 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3473 tag_action, data_ptr, dxfer_len, sense_len,
3474 sizeof(*scsi_cmd), timeout);
3478 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3479 void (*cbfcnp)(struct cam_periph *, union ccb *),
3480 u_int8_t tag_action, int immediate, int eot,
3481 int reten, int load, u_int8_t sense_len,
3484 struct scsi_load_unload *scsi_cmd;
3486 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3487 bzero(scsi_cmd, sizeof(*scsi_cmd));
3488 scsi_cmd->opcode = LOAD_UNLOAD;
3490 scsi_cmd->immediate = SLU_IMMED;
3492 scsi_cmd->eot_reten_load |= SLU_EOT;
3494 scsi_cmd->eot_reten_load |= SLU_RETEN;
3496 scsi_cmd->eot_reten_load |= SLU_LOAD;
3498 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3499 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3503 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3504 void (*cbfcnp)(struct cam_periph *, union ccb *),
3505 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3508 struct scsi_rewind *scsi_cmd;
3510 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3511 bzero(scsi_cmd, sizeof(*scsi_cmd));
3512 scsi_cmd->opcode = REWIND;
3514 scsi_cmd->immediate = SREW_IMMED;
3516 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3517 0, sense_len, sizeof(*scsi_cmd), timeout);
3521 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3522 void (*cbfcnp)(struct cam_periph *, union ccb *),
3523 u_int8_t tag_action, scsi_space_code code,
3524 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3526 struct scsi_space *scsi_cmd;
3528 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3529 scsi_cmd->opcode = SPACE;
3530 scsi_cmd->code = code;
3531 scsi_ulto3b(count, scsi_cmd->count);
3532 scsi_cmd->control = 0;
3534 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3535 0, sense_len, sizeof(*scsi_cmd), timeout);
3539 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3540 void (*cbfcnp)(struct cam_periph *, union ccb *),
3541 u_int8_t tag_action, int immediate, int setmark,
3542 u_int32_t num_marks, u_int8_t sense_len,
3545 struct scsi_write_filemarks *scsi_cmd;
3547 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3548 bzero(scsi_cmd, sizeof(*scsi_cmd));
3549 scsi_cmd->opcode = WRITE_FILEMARKS;
3551 scsi_cmd->byte2 |= SWFMRK_IMMED;
3553 scsi_cmd->byte2 |= SWFMRK_WSMK;
3555 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3557 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3558 0, sense_len, sizeof(*scsi_cmd), timeout);
3562 * The reserve and release unit commands differ only by their opcodes.
3565 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3566 void (*cbfcnp)(struct cam_periph *, union ccb *),
3567 u_int8_t tag_action, int third_party,
3568 int third_party_id, u_int8_t sense_len,
3569 u_int32_t timeout, int reserve)
3571 struct scsi_reserve_release_unit *scsi_cmd;
3573 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3574 bzero(scsi_cmd, sizeof(*scsi_cmd));
3577 scsi_cmd->opcode = RESERVE_UNIT;
3579 scsi_cmd->opcode = RELEASE_UNIT;
3582 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3583 scsi_cmd->lun_thirdparty |=
3584 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3587 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3588 0, sense_len, sizeof(*scsi_cmd), timeout);
3592 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3593 void (*cbfcnp)(struct cam_periph *, union ccb *),
3594 u_int8_t tag_action, int immediate, int long_erase,
3595 u_int8_t sense_len, u_int32_t timeout)
3597 struct scsi_erase *scsi_cmd;
3599 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3600 bzero(scsi_cmd, sizeof(*scsi_cmd));
3602 scsi_cmd->opcode = ERASE;
3605 scsi_cmd->lun_imm_long |= SE_IMMED;
3608 scsi_cmd->lun_imm_long |= SE_LONG;
3610 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3611 0, sense_len, sizeof(*scsi_cmd), timeout);
3615 * Read Tape Position command.
3618 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3619 void (*cbfcnp)(struct cam_periph *, union ccb *),
3620 u_int8_t tag_action, int hardsoft,
3621 struct scsi_tape_position_data *sbp,
3622 u_int8_t sense_len, u_int32_t timeout)
3624 struct scsi_tape_read_position *scmd;
3626 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3627 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3628 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3629 bzero(scmd, sizeof(*scmd));
3630 scmd->opcode = READ_POSITION;
3631 scmd->byte1 = hardsoft;
3635 * Set Tape Position command.
3638 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3639 void (*cbfcnp)(struct cam_periph *, union ccb *),
3640 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3641 u_int8_t sense_len, u_int32_t timeout)
3643 struct scsi_tape_locate *scmd;
3645 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3646 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3647 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3648 bzero(scmd, sizeof(*scmd));
3649 scmd->opcode = LOCATE;
3651 scmd->byte1 |= SA_SPOS_BT;
3652 scsi_ulto4b(blkno, scmd->blkaddr);