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.36 2008/07/18 00:07:23 dillon 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, 1);
506 cam_periph_unhold(periph, 1);
507 cam_periph_release(periph);
511 saprevent(periph, PR_PREVENT);
512 softc->flags |= SA_FLAG_OPEN;
514 cam_periph_unhold(periph, 1);
519 saclose(struct dev_close_args *ap)
521 cdev_t dev = ap->a_head.a_dev;
522 struct cam_periph *periph;
523 struct sa_softc *softc;
524 int unit, mode, error, writing, tmp;
525 int closedbits = SA_FLAG_OPEN;
529 periph = cam_extend_get(saperiphs, unit);
533 cam_periph_lock(periph);
535 softc = (struct sa_softc *)periph->softc;
537 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
538 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
541 softc->open_rdonly = 0;
542 if (SA_IS_CTRL(dev)) {
543 softc->ctrl_mode = 0;
544 cam_periph_unlock(periph);
545 cam_periph_release(periph);
549 if (softc->open_pending_mount) {
550 softc->flags &= ~SA_FLAG_OPEN;
551 softc->open_pending_mount = 0;
552 cam_periph_unlock(periph);
553 cam_periph_release(periph);
557 if ((error = cam_periph_hold(periph, 0)) != 0) {
558 cam_periph_unlock(periph);
563 * Were we writing the tape?
565 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
568 * See whether or not we need to write filemarks. If this
569 * fails, we probably have to assume we've lost tape
572 error = sacheckeod(periph);
574 xpt_print(periph->path,
575 "failed to write terminating filemark(s)\n");
576 softc->flags |= SA_FLAG_TAPE_FROZEN;
580 * Whatever we end up doing, allow users to eject tapes from here on.
582 saprevent(periph, PR_ALLOW);
585 * Decide how to end...
587 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
588 closedbits |= SA_FLAG_TAPE_FROZEN;
589 } else switch (mode) {
590 case SA_MODE_OFFLINE:
592 * An 'offline' close is an unconditional release of
593 * frozen && mount conditions, irrespective of whether
594 * these operations succeeded. The reason for this is
595 * to allow at least some kind of programmatic way
596 * around our state getting all fouled up. If somebody
597 * issues an 'offline' command, that will be allowed
601 saloadunload(periph, FALSE);
602 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
606 * If the rewind fails, return an error- if anyone cares,
607 * but not overwriting any previous error.
609 * We don't clear the notion of mounted here, but we do
610 * clear the notion of frozen if we successfully rewound.
612 tmp = sarewind(periph);
617 closedbits |= SA_FLAG_TAPE_FROZEN;
620 case SA_MODE_NOREWIND:
622 * If we're not rewinding/unloading the tape, find out
623 * whether we need to back up over one of two filemarks
624 * we wrote (if we wrote two filemarks) so that appends
625 * from this point on will be sane.
627 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
628 tmp = saspace(periph, -1, SS_FILEMARKS);
630 xpt_print(periph->path, "unable to backspace "
631 "over one of double filemarks at end of "
633 xpt_print(periph->path, "it is possible that "
634 "this device needs a SA_QUIRK_1FM quirk set"
636 softc->flags |= SA_FLAG_TAPE_FROZEN;
641 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode);
647 * We wish to note here that there are no more filemarks to be written.
649 softc->filemarks = 0;
650 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
653 * And we are no longer open for business.
655 softc->flags &= ~closedbits;
658 * Inform users if tape state if frozen....
660 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
661 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, "
662 "REWIND or MTEOM command to clear this state.\n");
665 /* release the device if it is no longer mounted */
666 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
667 sareservereleaseunit(periph, FALSE);
669 cam_periph_unhold(periph, 1);
670 cam_periph_release(periph);
676 * Actually translate the requested transfer into one the physical driver
677 * can understand. The transfer is described by a buf and will include
678 * only one physical transfer.
681 sastrategy(struct dev_strategy_args *ap)
683 cdev_t dev = ap->a_head.a_dev;
684 struct bio *bio = ap->a_bio;
685 struct buf *bp = bio->bio_buf;
686 struct cam_periph *periph;
687 struct sa_softc *softc;
690 if (SA_IS_CTRL(dev)) {
691 bp->b_error = EINVAL;
695 periph = cam_extend_get(saperiphs, unit);
696 if (periph == NULL) {
700 cam_periph_lock(periph);
702 softc = (struct sa_softc *)periph->softc;
704 if (softc->flags & SA_FLAG_INVALID) {
705 cam_periph_unlock(periph);
710 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
711 cam_periph_unlock(periph);
717 * This should actually never occur as the write(2)
718 * system call traps attempts to write to a read-only
721 if (bp->b_cmd == BUF_CMD_WRITE && softc->open_rdonly) {
722 cam_periph_unlock(periph);
727 if (softc->open_pending_mount) {
728 int error = samount(periph, 0, dev);
730 cam_periph_unlock(periph);
734 saprevent(periph, PR_PREVENT);
735 softc->open_pending_mount = 0;
739 * If it's a null transfer, return immediately
741 if (bp->b_bcount == 0) {
742 cam_periph_unlock(periph);
747 if (softc->flags & SA_FLAG_FIXED) {
749 * Fixed block device. The byte count must
750 * be a multiple of our block size.
752 if (((softc->blk_mask != ~0) &&
753 ((bp->b_bcount & softc->blk_mask) != 0)) ||
754 ((softc->blk_mask == ~0) &&
755 ((bp->b_bcount % softc->min_blk) != 0))) {
756 xpt_print(periph->path, "Invalid request. Fixed block "
757 "device requests must be a multiple of %d bytes\n",
759 cam_periph_unlock(periph);
760 bp->b_error = EINVAL;
763 } else if ((bp->b_bcount > softc->max_blk) ||
764 (bp->b_bcount < softc->min_blk) ||
765 (bp->b_bcount & softc->blk_mask) != 0) {
767 xpt_print_path(periph->path);
768 kprintf("Invalid request. Variable block "
769 "device requests must be ");
770 if (softc->blk_mask != 0) {
771 kprintf("a multiple of %d ", (0x1 << softc->blk_gran));
773 kprintf("between %d and %d bytes\n", softc->min_blk,
775 cam_periph_unlock(periph);
776 bp->b_error = EINVAL;
781 * Place it at the end of the queue.
783 bioq_insert_tail(&softc->bio_queue, bio);
784 softc->queue_count++;
786 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
787 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
788 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
789 (bp->bio_cmd == BIO_READ)? "read" : "write"));
791 if (softc->queue_count > 1) {
792 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
793 ("sastrategy: queue count now %d\n", softc->queue_count));
797 * Schedule ourselves for performing the work.
799 xpt_schedule(periph, 1);
800 cam_periph_unlock(periph);
804 bp->b_flags |= B_ERROR;
808 * Correctly set the buf to indicate a completed xfer
810 bp->b_resid = bp->b_bcount;
816 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
817 if (softc->open_pending_mount) { \
818 error = samount(periph, 0, dev); \
822 saprevent(periph, PR_PREVENT); \
823 softc->open_pending_mount = 0; \
827 saioctl(struct dev_ioctl_args *ap)
829 cdev_t dev = ap->a_head.a_dev;
830 caddr_t addr = ap->a_data;
831 struct cam_periph *periph;
832 struct sa_softc *softc;
833 scsi_space_code spaceop;
834 int didholdperiph = 0;
841 error = 0; /* shut up gcc */
842 spaceop = 0; /* shut up gcc */
844 periph = cam_extend_get(saperiphs, unit);
848 cam_periph_lock(periph);
849 softc = (struct sa_softc *)periph->softc;
852 * Check for control mode accesses. We allow MTIOCGET and
853 * MTIOCERRSTAT (but need to be the only one open in order
854 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
855 * and MTCOMP (but need to be the only one accessing this
856 * device to run those).
859 if (SA_IS_CTRL(dev)) {
861 case MTIOCGETEOTMODEL:
866 * If the periph isn't already locked, lock it
867 * so our MTIOCERRSTAT can reset latched error stats.
869 * If the periph is already locked, skip it because
870 * we're just getting status and it'll be up to the
871 * other thread that has this device open to do
872 * an MTIOCERRSTAT that would clear latched status.
874 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
875 error = cam_periph_hold(periph, PCATCH);
885 struct mtop *mt = (struct mtop *) addr;
888 * Check to make sure it's an OP we can perform
889 * with no media inserted.
905 case MTIOCSETEOTMODEL:
907 * We need to acquire the peripheral here rather
908 * than at open time because we are sharing writable
909 * access to data structures.
911 error = cam_periph_hold(periph, PCATCH);
924 * Find the device that the user is talking about
929 struct mtget *g = (struct mtget *)addr;
932 * If this isn't the control mode device, actually go out
933 * and ask the drive again what it's set to.
935 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) {
936 u_int8_t write_protect;
937 int comp_enabled, comp_supported;
938 error = sagetparams(periph, SA_PARAM_ALL,
939 &softc->media_blksize, &softc->media_density,
940 &softc->media_numblks, &softc->buffer_mode,
941 &write_protect, &softc->speed, &comp_supported,
942 &comp_enabled, &softc->comp_algorithm, NULL);
946 softc->flags |= SA_FLAG_TAPE_WP;
948 softc->flags &= ~SA_FLAG_TAPE_WP;
949 softc->flags &= ~(SA_FLAG_COMP_SUPP|
950 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
951 if (comp_supported) {
952 if (softc->saved_comp_algorithm == 0)
953 softc->saved_comp_algorithm =
954 softc->comp_algorithm;
955 softc->flags |= SA_FLAG_COMP_SUPP;
957 softc->flags |= SA_FLAG_COMP_ENABLED;
959 softc->flags |= SA_FLAG_COMP_UNSUPP;
961 bzero(g, sizeof(struct mtget));
962 g->mt_type = MT_ISAR;
963 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
964 g->mt_comp = MT_COMP_UNSUPP;
965 g->mt_comp0 = MT_COMP_UNSUPP;
966 g->mt_comp1 = MT_COMP_UNSUPP;
967 g->mt_comp2 = MT_COMP_UNSUPP;
968 g->mt_comp3 = MT_COMP_UNSUPP;
970 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
971 g->mt_comp = MT_COMP_DISABLED;
973 g->mt_comp = softc->comp_algorithm;
975 g->mt_comp0 = softc->comp_algorithm;
976 g->mt_comp1 = softc->comp_algorithm;
977 g->mt_comp2 = softc->comp_algorithm;
978 g->mt_comp3 = softc->comp_algorithm;
980 g->mt_density = softc->media_density;
981 g->mt_density0 = softc->media_density;
982 g->mt_density1 = softc->media_density;
983 g->mt_density2 = softc->media_density;
984 g->mt_density3 = softc->media_density;
985 g->mt_blksiz = softc->media_blksize;
986 g->mt_blksiz0 = softc->media_blksize;
987 g->mt_blksiz1 = softc->media_blksize;
988 g->mt_blksiz2 = softc->media_blksize;
989 g->mt_blksiz3 = softc->media_blksize;
990 g->mt_fileno = softc->fileno;
991 g->mt_blkno = softc->blkno;
992 g->mt_dsreg = (short) softc->dsreg;
994 * Yes, we know that this is likely to overflow
996 if (softc->last_resid_was_io) {
997 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
998 if (SA_IS_CTRL(dev) == 0 || didholdperiph) {
999 softc->last_io_resid = 0;
1003 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
1004 if (SA_IS_CTRL(dev) == 0 || didholdperiph) {
1005 softc->last_ctl_resid = 0;
1014 struct scsi_tape_errors *sep =
1015 &((union mterrstat *)addr)->scsi_errstat;
1017 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1018 ("saioctl: MTIOCERRSTAT\n"));
1020 bzero(sep, sizeof(*sep));
1021 sep->io_resid = softc->last_io_resid;
1022 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1023 sizeof (sep->io_sense));
1024 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1025 sizeof (sep->io_cdb));
1026 sep->ctl_resid = softc->last_ctl_resid;
1027 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1028 sizeof (sep->ctl_sense));
1029 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1030 sizeof (sep->ctl_cdb));
1032 if ((SA_IS_CTRL(dev) == 0 && softc->open_pending_mount) ||
1034 bzero((caddr_t) &softc->errinfo,
1035 sizeof (softc->errinfo));
1044 PENDING_MOUNT_CHECK(softc, periph, dev);
1046 mt = (struct mtop *)addr;
1048 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1049 ("saioctl: op=0x%x count=0x%x\n",
1050 mt->mt_op, mt->mt_count));
1052 count = mt->mt_count;
1053 switch (mt->mt_op) {
1054 case MTWEOF: /* write an end-of-file marker */
1056 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1057 * flag because by keeping track of filemarks
1058 * we have last written we know ehether or not
1059 * we need to write more when we close the device.
1061 error = sawritefilemarks(periph, count, FALSE);
1063 case MTWSS: /* write a setmark */
1064 error = sawritefilemarks(periph, count, TRUE);
1066 case MTBSR: /* backward space record */
1067 case MTFSR: /* forward space record */
1068 case MTBSF: /* backward space file */
1069 case MTFSF: /* forward space file */
1070 case MTBSS: /* backward space setmark */
1071 case MTFSS: /* forward space setmark */
1072 case MTEOD: /* space to end of recorded medium */
1076 spaceop = SS_FILEMARKS;
1077 nmarks = softc->filemarks;
1078 error = sacheckeod(periph);
1080 xpt_print(periph->path,
1081 "EOD check prior to spacing failed\n");
1082 softc->flags |= SA_FLAG_EIO_PENDING;
1085 nmarks -= softc->filemarks;
1091 spaceop = SS_BLOCKS;
1102 spaceop = SS_SETMARKS;
1116 nmarks = softc->filemarks;
1118 * XXX: Why are we checking again?
1120 error = sacheckeod(periph);
1123 nmarks -= softc->filemarks;
1124 error = saspace(periph, count - nmarks, spaceop);
1126 * At this point, clear that we've written the tape
1127 * and that we've written any filemarks. We really
1128 * don't know what the applications wishes to do next-
1129 * the sacheckeod's will make sure we terminated the
1130 * tape correctly if we'd been writing, but the next
1131 * action the user application takes will set again
1132 * whether we need to write filemarks.
1135 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1136 softc->filemarks = 0;
1139 case MTREW: /* rewind */
1140 PENDING_MOUNT_CHECK(softc, periph, dev);
1142 error = sarewind(periph);
1145 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1146 softc->flags &= ~SA_FLAG_ERR_PENDING;
1147 softc->filemarks = 0;
1149 case MTERASE: /* erase */
1150 PENDING_MOUNT_CHECK(softc, periph, dev);
1151 error = saerase(periph, count);
1153 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1154 softc->flags &= ~SA_FLAG_ERR_PENDING;
1156 case MTRETENS: /* re-tension tape */
1157 PENDING_MOUNT_CHECK(softc, periph, dev);
1158 error = saretension(periph);
1160 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1161 softc->flags &= ~SA_FLAG_ERR_PENDING;
1163 case MTOFFL: /* rewind and put the drive offline */
1165 PENDING_MOUNT_CHECK(softc, periph, dev);
1169 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1170 softc->filemarks = 0;
1172 error = sarewind(periph);
1173 /* clear the frozen flag anyway */
1174 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1177 * Be sure to allow media removal before ejecting.
1180 saprevent(periph, PR_ALLOW);
1182 error = saloadunload(periph, FALSE);
1184 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1189 case MTNOP: /* no operation, sets status only */
1190 case MTCACHE: /* enable controller cache */
1191 case MTNOCACHE: /* disable controller cache */
1195 case MTSETBSIZ: /* Set block size for device */
1197 PENDING_MOUNT_CHECK(softc, periph, dev);
1199 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1202 softc->last_media_blksize =
1203 softc->media_blksize;
1204 softc->media_blksize = count;
1206 softc->flags |= SA_FLAG_FIXED;
1207 if (powerof2(count)) {
1210 softc->blk_mask = count - 1;
1212 softc->blk_mask = ~0;
1213 softc->blk_shift = 0;
1216 * Make the user's desire 'persistent'.
1218 softc->quirks &= ~SA_QUIRK_VARIABLE;
1219 softc->quirks |= SA_QUIRK_FIXED;
1221 softc->flags &= ~SA_FLAG_FIXED;
1222 if (softc->max_blk == 0) {
1223 softc->max_blk = ~0;
1225 softc->blk_shift = 0;
1226 if (softc->blk_gran != 0) {
1228 softc->blk_gran - 1;
1230 softc->blk_mask = 0;
1233 * Make the user's desire 'persistent'.
1235 softc->quirks |= SA_QUIRK_VARIABLE;
1236 softc->quirks &= ~SA_QUIRK_FIXED;
1240 case MTSETDNSTY: /* Set density for device and mode */
1241 PENDING_MOUNT_CHECK(softc, periph, dev);
1243 if (count > UCHAR_MAX) {
1247 error = sasetparams(periph, SA_PARAM_DENSITY,
1251 case MTCOMP: /* enable compression */
1252 PENDING_MOUNT_CHECK(softc, periph, dev);
1254 * Some devices don't support compression, and
1255 * don't like it if you ask them for the
1258 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1259 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1263 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1264 0, 0, count, SF_NO_PRINT);
1276 PENDING_MOUNT_CHECK(softc, periph, dev);
1277 error = sardpos(periph, 0, (u_int32_t *) addr);
1280 PENDING_MOUNT_CHECK(softc, periph, dev);
1281 error = sardpos(periph, 1, (u_int32_t *) addr);
1284 PENDING_MOUNT_CHECK(softc, periph, dev);
1285 error = sasetpos(periph, 0, (u_int32_t *) addr);
1288 PENDING_MOUNT_CHECK(softc, periph, dev);
1289 error = sasetpos(periph, 1, (u_int32_t *) addr);
1291 case MTIOCGETEOTMODEL:
1293 if (softc->quirks & SA_QUIRK_1FM)
1297 *((u_int32_t *) addr) = mode;
1299 case MTIOCSETEOTMODEL:
1301 switch (*((u_int32_t *) addr)) {
1303 softc->quirks &= ~SA_QUIRK_2FM;
1304 softc->quirks |= SA_QUIRK_1FM;
1307 softc->quirks &= ~SA_QUIRK_1FM;
1308 softc->quirks |= SA_QUIRK_2FM;
1316 error = cam_periph_ioctl(periph, ap->a_cmd, addr, saerror);
1321 * Check to see if we cleared a frozen state
1323 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1329 softc->fileno = (daddr_t) -1;
1330 softc->blkno = (daddr_t) -1;
1331 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1332 xpt_print(periph->path,
1333 "tape state now unfrozen.\n");
1340 cam_periph_unhold(periph, 1);
1342 cam_periph_unlock(periph);
1352 * Create our extend array for storing the devices we attach to.
1354 saperiphs = cam_extend_new();
1355 if (saperiphs == NULL) {
1356 kprintf("sa: Failed to alloc extend array!\n");
1361 * Install a global async callback.
1363 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL);
1365 if (status != CAM_REQ_CMP) {
1366 kprintf("sa: Failed to attach master async callback "
1367 "due to status 0x%x!\n", status);
1372 saoninvalidate(struct cam_periph *periph)
1374 struct sa_softc *softc;
1378 softc = (struct sa_softc *)periph->softc;
1381 * De-register any async callbacks.
1383 xpt_register_async(0, saasync, periph, periph->path);
1385 softc->flags |= SA_FLAG_INVALID;
1388 * Return all queued I/O with ENXIO.
1389 * XXX Handle any transactions queued to the card
1390 * with XPT_ABORT_CCB.
1392 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
1393 bioq_remove(&softc->bio_queue, q_bio);
1394 q_bp = q_bio->bio_buf;
1395 q_bp->b_resid = q_bp->b_bcount;
1396 q_bp->b_error = ENXIO;
1397 q_bp->b_flags |= B_ERROR;
1400 softc->queue_count = 0;
1402 xpt_print(periph->path, "lost device\n");
1407 sacleanup(struct cam_periph *periph)
1409 struct sa_softc *softc;
1411 softc = (struct sa_softc *)periph->softc;
1413 devstat_remove_entry(&softc->device_stats);
1415 cam_extend_release(saperiphs, periph->unit_number);
1416 xpt_print(periph->path, "removing device entry\n");
1417 dev_ops_remove_minor(&sa_ops, /*SA_UNITMASK,*/ SA_UNIT(periph->unit_number));
1418 kfree(softc, M_SCSISA);
1422 saasync(void *callback_arg, u_int32_t code,
1423 struct cam_path *path, void *arg)
1425 struct cam_periph *periph;
1427 periph = (struct cam_periph *)callback_arg;
1429 case AC_FOUND_DEVICE:
1431 struct ccb_getdev *cgd;
1434 cgd = (struct ccb_getdev *)arg;
1438 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1442 * Allocate a peripheral instance for
1443 * this device and start the probe
1446 status = cam_periph_alloc(saregister, saoninvalidate,
1448 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1449 saasync, AC_FOUND_DEVICE, cgd);
1451 if (status != CAM_REQ_CMP
1452 && status != CAM_REQ_INPROG)
1453 kprintf("saasync: Unable to probe new device "
1454 "due to status 0x%x\n", status);
1458 cam_periph_async(periph, code, path, arg);
1464 saregister(struct cam_periph *periph, void *arg)
1466 struct sa_softc *softc;
1467 struct ccb_getdev *cgd;
1471 cgd = (struct ccb_getdev *)arg;
1472 if (periph == NULL) {
1473 kprintf("saregister: periph was NULL!!\n");
1474 return (CAM_REQ_CMP_ERR);
1478 kprintf("saregister: no getdev CCB, can't register device\n");
1479 return (CAM_REQ_CMP_ERR);
1482 softc = kmalloc(sizeof (*softc), M_SCSISA, M_INTWAIT | M_ZERO);
1483 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1484 softc->state = SA_STATE_NORMAL;
1485 softc->fileno = (daddr_t) -1;
1486 softc->blkno = (daddr_t) -1;
1488 bioq_init(&softc->bio_queue);
1489 periph->softc = softc;
1490 cam_extend_set(saperiphs, periph->unit_number, periph);
1493 * See if this device has any quirks.
1495 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1496 (caddr_t)sa_quirk_table,
1497 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1498 sizeof(*sa_quirk_table), scsi_inquiry_match);
1500 if (match != NULL) {
1501 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1502 softc->last_media_blksize =
1503 ((struct sa_quirk_entry *)match)->prefblk;
1505 xpt_print(periph->path, "found quirk entry %d\n",
1506 (int) (((struct sa_quirk_entry *) match) - sa_quirk_table));
1509 softc->quirks = SA_QUIRK_NONE;
1512 * The SA driver supports a blocksize, but we don't know the
1513 * blocksize until we media is inserted. So, set a flag to
1514 * indicate that the blocksize is unavailable right now.
1516 cam_periph_unlock(periph);
1517 devstat_add_entry(&softc->device_stats, "sa", periph->unit_number, 0,
1518 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1519 DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE);
1521 make_dev(&sa_ops, SAMINOR(SA_CTLDEV,
1522 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1523 0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1525 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1526 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1527 0660, "%s%d", periph->periph_name, periph->unit_number);
1529 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1530 periph->unit_number, 0, SA_ATYPE_NR), UID_ROOT, GID_OPERATOR,
1531 0660, "n%s%d", periph->periph_name, periph->unit_number);
1533 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1534 periph->unit_number, 0, SA_ATYPE_ER), UID_ROOT, GID_OPERATOR,
1535 0660, "e%s%d", periph->periph_name, periph->unit_number);
1537 for (i = 0; i < SA_NUM_MODES; i++) {
1540 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R),
1541 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1542 periph->periph_name, periph->unit_number, i);
1545 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR),
1546 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1547 periph->periph_name, periph->unit_number, i);
1551 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER),
1552 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1553 periph->periph_name, periph->unit_number, i);
1555 cam_periph_lock(periph);
1558 * Add an async callback so that we get
1559 * notified if this device goes away.
1561 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path);
1563 xpt_announce_periph(periph, NULL);
1565 return (CAM_REQ_CMP);
1569 sastart(struct cam_periph *periph, union ccb *start_ccb)
1571 struct sa_softc *softc;
1573 softc = (struct sa_softc *)periph->softc;
1575 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
1578 switch (softc->state) {
1579 case SA_STATE_NORMAL:
1581 /* Pull a buffer from the queue and get going on it */
1586 * See if there is a buf with work for us to do..
1588 bio = bioq_first(&softc->bio_queue);
1589 if (periph->immediate_priority <= periph->pinfo.priority) {
1590 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1591 ("queuing for immediate ccb\n"));
1592 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1593 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1595 periph->immediate_priority = CAM_PRIORITY_NONE;
1596 wakeup(&periph->ccb_list);
1597 } else if (bio == NULL) {
1598 xpt_release_ccb(start_ccb);
1599 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1600 struct bio *done_bio;
1602 softc->queue_count--;
1603 bioq_remove(&softc->bio_queue, bio);
1605 bp->b_resid = bp->b_bcount;
1607 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1609 * We now just clear errors in this case
1610 * and let the residual be the notifier.
1613 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1615 * This can only happen if we're reading
1616 * in fixed length mode. In this case,
1617 * we dump the rest of the list the
1621 if (bioq_first(&softc->bio_queue) != NULL) {
1625 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1627 bp->b_flags |= B_ERROR;
1629 bio = bioq_first(&softc->bio_queue);
1631 * Only if we have no other buffers queued up
1632 * do we clear the pending error flag.
1635 softc->flags &= ~SA_FLAG_ERR_PENDING;
1636 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1637 ("sastart- ERR_PENDING now 0x%x, bio is %sNULL, "
1638 "%d more buffers queued up\n",
1639 (softc->flags & SA_FLAG_ERR_PENDING),
1640 (bio != NULL)? "not " : " ", softc->queue_count));
1641 xpt_release_ccb(start_ccb);
1646 bioq_remove(&softc->bio_queue, bio);
1648 softc->queue_count--;
1650 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1651 if (softc->blk_shift != 0) {
1653 bp->b_bcount >> softc->blk_shift;
1654 } else if (softc->media_blksize != 0) {
1656 bp->b_bcount / softc->media_blksize;
1659 xpt_print(periph->path, "zero blocksize"
1660 " for FIXED length writes?\n");
1665 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1666 ("issuing a %d fixed record %s\n",
1667 length, (bp->bio_cmd == BIO_READ)? "read" :
1671 length = bp->b_bcount;
1673 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1674 ("issuing a %d variable byte %s\n",
1675 length, (bp->bio_cmd == BIO_READ)? "read" :
1679 devstat_start_transaction(&softc->device_stats);
1681 * Some people have theorized that we should
1682 * suppress illegal length indication if we are
1683 * running in variable block mode so that we don't
1684 * have to request sense every time our requested
1685 * block size is larger than the written block.
1686 * The residual information from the ccb allows
1687 * us to identify this situation anyway. The only
1688 * problem with this is that we will not get
1689 * information about blocks that are larger than
1690 * our read buffer unless we set the block size
1691 * in the mode page to something other than 0.
1693 * I believe that this is a non-issue. If user apps
1694 * don't adjust their read size to match our record
1695 * size, that's just life. Anyway, the typical usage
1696 * would be to issue, e.g., 64KB reads and occasionally
1697 * have to do deal with 512 byte or 1KB intermediate
1700 softc->dsreg = (bp->b_cmd == BUF_CMD_READ) ?
1701 MTIO_DSREG_RD : MTIO_DSREG_WR;
1702 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1703 MSG_SIMPLE_Q_TAG, (bp->b_cmd == BUF_CMD_READ) != 0,
1704 FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1705 length, bp->b_data, bp->b_bcount, SSD_FULL_SIZE,
1707 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1708 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1709 start_ccb->ccb_h.ccb_bio = bio;
1710 bio = bioq_first(&softc->bio_queue);
1711 xpt_action(start_ccb);
1715 /* Have more work to do, so ensure we stay scheduled */
1716 xpt_schedule(periph, 1);
1720 case SA_STATE_ABNORMAL:
1722 panic("state 0x%x in sastart", softc->state);
1729 sadone(struct cam_periph *periph, union ccb *done_ccb)
1731 struct sa_softc *softc;
1732 struct ccb_scsiio *csio;
1734 softc = (struct sa_softc *)periph->softc;
1735 csio = &done_ccb->csio;
1736 switch (CCB_Type(csio)) {
1737 case SA_CCB_BUFFER_IO:
1743 softc->dsreg = MTIO_DSREG_REST;
1744 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1747 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1748 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1750 * A retry was scheduled, so just return.
1761 * Catastrophic error. Mark the tape as frozen
1762 * (we no longer know tape position).
1764 * Return all queued I/O with EIO, and unfreeze
1765 * our queue so that future transactions that
1766 * attempt to fix this problem can get to the
1771 softc->flags |= SA_FLAG_TAPE_FROZEN;
1772 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) {
1773 bioq_remove(&softc->bio_queue, q_bio);
1774 q_bp = q_bio->bio_buf;
1775 q_bp->b_resid = q_bp->b_bcount;
1776 q_bp->b_error = EIO;
1777 q_bp->b_flags |= B_ERROR;
1782 bp->b_resid = bp->b_bcount;
1783 bp->b_error = error;
1784 bp->b_flags |= B_ERROR;
1786 * In the error case, position is updated in saerror.
1789 bp->b_resid = csio->resid;
1791 if (csio->resid != 0) {
1792 bp->b_flags |= B_ERROR;
1794 if (bp->b_cmd != BUF_CMD_READ) {
1795 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1796 softc->filemarks = 0;
1798 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1799 (softc->blkno != (daddr_t) -1)) {
1800 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1802 if (softc->blk_shift != 0) {
1807 softc->media_blksize;
1809 softc->blkno += (daddr_t) l;
1816 * If we had an error (immediate or pending),
1817 * release the device queue now.
1819 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1820 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1822 if (error || bp->b_resid) {
1823 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1824 ("error %d resid %d count %d\n", error,
1825 bp->b_resid, bp->b_bcount));
1828 devstat_end_transaction_buf(&softc->device_stats, bp);
1832 case SA_CCB_WAITING:
1834 /* Caller will release the CCB */
1835 wakeup(&done_ccb->ccb_h.cbfcnp);
1839 xpt_release_ccb(done_ccb);
1843 * Mount the tape (make sure it's ready for I/O).
1846 samount(struct cam_periph *periph, int oflags, cdev_t dev)
1848 struct sa_softc *softc;
1853 * oflags can be checked for 'kind' of open (read-only check) - later
1854 * dev can be checked for a control-mode or compression open - later
1856 UNUSED_PARAMETER(oflags);
1857 UNUSED_PARAMETER(dev);
1860 softc = (struct sa_softc *)periph->softc;
1863 * This should determine if something has happend since the last
1864 * open/mount that would invalidate the mount. We do *not* want
1865 * to retry this command- we just want the status. But we only
1866 * do this if we're mounted already- if we're not mounted,
1867 * we don't care about the unit read state and can instead use
1868 * this opportunity to attempt to reserve the tape unit.
1871 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1872 ccb = cam_periph_getccb(periph, 1);
1873 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1874 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1875 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1876 &softc->device_stats);
1878 if (error == ENXIO) {
1879 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1880 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1881 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1882 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1883 &softc->device_stats);
1887 * We don't need to freeze the tape because we
1888 * will now attempt to rewind/load it.
1890 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1891 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
1892 xpt_print(periph->path,
1893 "error %d on TUR in samount\n", error);
1897 error = sareservereleaseunit(periph, TRUE);
1901 ccb = cam_periph_getccb(periph, 1);
1902 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1903 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1904 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1905 &softc->device_stats);
1909 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1910 struct scsi_read_block_limits_data *rblim = NULL;
1911 int comp_enabled, comp_supported;
1912 u_int8_t write_protect, guessing = 0;
1915 * Clear out old state.
1917 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1918 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1919 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1920 softc->filemarks = 0;
1923 * *Very* first off, make sure we're loaded to BOT.
1925 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1926 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1927 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1928 &softc->device_stats);
1932 * In case this doesn't work, do a REWIND instead
1935 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1936 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1937 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1938 &softc->device_stats);
1942 xpt_release_ccb(ccb);
1947 * Do a dummy test read to force access to the
1948 * media so that the drive will really know what's
1949 * there. We actually don't really care what the
1950 * blocksize on tape is and don't expect to really
1951 * read a full record.
1953 rblim = kmalloc(8192, M_SCSISA, M_INTWAIT);
1955 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1956 scsi_sa_read_write(&ccb->csio, 0, sadone,
1957 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1958 (void *) rblim, 8192, SSD_FULL_SIZE,
1960 cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1961 &softc->device_stats);
1963 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1964 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1965 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1966 SF_NO_PRINT | SF_RETRY_UA,
1967 &softc->device_stats);
1970 xpt_print(periph->path,
1971 "unable to rewind after test read\n");
1972 xpt_release_ccb(ccb);
1978 * Next off, determine block limits.
1980 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1981 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1983 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1984 SF_NO_PRINT | SF_RETRY_UA, &softc->device_stats);
1987 xpt_release_ccb(ccb);
1991 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1992 * a MANDATORY command. Anyway- it doesn't matter-
1993 * we can proceed anyway.
1995 softc->blk_gran = 0;
1996 softc->max_blk = ~0;
1999 if (softc->scsi_rev >= SCSI_REV_SPC) {
2000 softc->blk_gran = RBL_GRAN(rblim);
2002 softc->blk_gran = 0;
2005 * We take max_blk == min_blk to mean a default to
2006 * fixed mode- but note that whatever we get out of
2007 * sagetparams below will actually determine whether
2008 * we are actually *in* fixed mode.
2010 softc->max_blk = scsi_3btoul(rblim->maximum);
2011 softc->min_blk = scsi_2btoul(rblim->minimum);
2016 * Next, perform a mode sense to determine
2017 * current density, blocksize, compression etc.
2019 error = sagetparams(periph, SA_PARAM_ALL,
2020 &softc->media_blksize,
2021 &softc->media_density,
2022 &softc->media_numblks,
2023 &softc->buffer_mode, &write_protect,
2024 &softc->speed, &comp_supported,
2025 &comp_enabled, &softc->comp_algorithm,
2030 * We could work a little harder here. We could
2031 * adjust our attempts to get information. It
2032 * might be an ancient tape drive. If someone
2033 * nudges us, we'll do that.
2039 * If no quirk has determined that this is a device that is
2040 * preferred to be in fixed or variable mode, now is the time
2043 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2046 * This could be expensive to find out. Luckily we
2047 * only need to do this once. If we start out in
2048 * 'default' mode, try and set ourselves to one
2049 * of the densities that would determine a wad
2050 * of other stuff. Go from highest to lowest.
2052 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2054 static u_int8_t ctry[] = {
2055 SCSI_DENSITY_HALFINCH_PE,
2056 SCSI_DENSITY_HALFINCH_6250C,
2057 SCSI_DENSITY_HALFINCH_6250,
2058 SCSI_DENSITY_HALFINCH_1600,
2059 SCSI_DENSITY_HALFINCH_800,
2060 SCSI_DENSITY_QIC_4GB,
2061 SCSI_DENSITY_QIC_2GB,
2062 SCSI_DENSITY_QIC_525_320,
2063 SCSI_DENSITY_QIC_150,
2064 SCSI_DENSITY_QIC_120,
2065 SCSI_DENSITY_QIC_24,
2066 SCSI_DENSITY_QIC_11_9TRK,
2067 SCSI_DENSITY_QIC_11_4TRK,
2068 SCSI_DENSITY_QIC_1320,
2069 SCSI_DENSITY_QIC_3080,
2072 for (i = 0; ctry[i]; i++) {
2073 error = sasetparams(periph,
2074 SA_PARAM_DENSITY, 0, ctry[i],
2077 softc->media_density = ctry[i];
2082 switch (softc->media_density) {
2083 case SCSI_DENSITY_QIC_11_4TRK:
2084 case SCSI_DENSITY_QIC_11_9TRK:
2085 case SCSI_DENSITY_QIC_24:
2086 case SCSI_DENSITY_QIC_120:
2087 case SCSI_DENSITY_QIC_150:
2088 case SCSI_DENSITY_QIC_525_320:
2089 case SCSI_DENSITY_QIC_1320:
2090 case SCSI_DENSITY_QIC_3080:
2091 softc->quirks &= ~SA_QUIRK_2FM;
2092 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2093 softc->last_media_blksize = 512;
2095 case SCSI_DENSITY_QIC_4GB:
2096 case SCSI_DENSITY_QIC_2GB:
2097 softc->quirks &= ~SA_QUIRK_2FM;
2098 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2099 softc->last_media_blksize = 1024;
2102 softc->last_media_blksize =
2103 softc->media_blksize;
2104 softc->quirks |= SA_QUIRK_VARIABLE;
2110 * If no quirk has determined that this is a device that needs
2111 * to have 2 Filemarks at EOD, now is the time to find out.
2114 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2115 switch (softc->media_density) {
2116 case SCSI_DENSITY_HALFINCH_800:
2117 case SCSI_DENSITY_HALFINCH_1600:
2118 case SCSI_DENSITY_HALFINCH_6250:
2119 case SCSI_DENSITY_HALFINCH_6250C:
2120 case SCSI_DENSITY_HALFINCH_PE:
2121 softc->quirks &= ~SA_QUIRK_1FM;
2122 softc->quirks |= SA_QUIRK_2FM;
2130 * Now validate that some info we got makes sense.
2132 if ((softc->max_blk < softc->media_blksize) ||
2133 (softc->min_blk > softc->media_blksize &&
2134 softc->media_blksize)) {
2135 xpt_print(periph->path,
2136 "BLOCK LIMITS (%d..%d) could not match current "
2137 "block settings (%d)- adjusting\n", softc->min_blk,
2138 softc->max_blk, softc->media_blksize);
2139 softc->max_blk = softc->min_blk =
2140 softc->media_blksize;
2144 * Now put ourselves into the right frame of mind based
2149 * If we want to be in FIXED mode and our current blocksize
2150 * is not equal to our last blocksize (if nonzero), try and
2151 * set ourselves to this last blocksize (as the 'preferred'
2152 * block size). The initial quirkmatch at registry sets the
2153 * initial 'last' blocksize. If, for whatever reason, this
2154 * 'last' blocksize is zero, set the blocksize to 512,
2155 * or min_blk if that's larger.
2157 if ((softc->quirks & SA_QUIRK_FIXED) &&
2158 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2159 (softc->media_blksize != softc->last_media_blksize)) {
2160 softc->media_blksize = softc->last_media_blksize;
2161 if (softc->media_blksize == 0) {
2162 softc->media_blksize = 512;
2163 if (softc->media_blksize < softc->min_blk) {
2164 softc->media_blksize = softc->min_blk;
2167 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2168 softc->media_blksize, 0, 0, SF_NO_PRINT);
2170 xpt_print(periph->path,
2171 "unable to set fixed blocksize to %d\n",
2172 softc->media_blksize);
2177 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2178 (softc->media_blksize != 0)) {
2179 softc->last_media_blksize = softc->media_blksize;
2180 softc->media_blksize = 0;
2181 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2182 0, 0, 0, SF_NO_PRINT);
2185 * If this fails and we were guessing, just
2186 * assume that we got it wrong and go try
2187 * fixed block mode. Don't even check against
2188 * density code at this point.
2191 softc->quirks &= ~SA_QUIRK_VARIABLE;
2192 softc->quirks |= SA_QUIRK_FIXED;
2193 if (softc->last_media_blksize == 0)
2194 softc->last_media_blksize = 512;
2197 xpt_print(periph->path,
2198 "unable to set variable blocksize\n");
2204 * Now that we have the current block size,
2205 * set up some parameters for sastart's usage.
2207 if (softc->media_blksize) {
2208 softc->flags |= SA_FLAG_FIXED;
2209 if (powerof2(softc->media_blksize)) {
2211 ffs(softc->media_blksize) - 1;
2212 softc->blk_mask = softc->media_blksize - 1;
2214 softc->blk_mask = ~0;
2215 softc->blk_shift = 0;
2219 * The SCSI-3 spec allows 0 to mean "unspecified".
2220 * The SCSI-1 spec allows 0 to mean 'infinite'.
2222 * Either works here.
2224 if (softc->max_blk == 0) {
2225 softc->max_blk = ~0;
2227 softc->blk_shift = 0;
2228 if (softc->blk_gran != 0) {
2229 softc->blk_mask = softc->blk_gran - 1;
2231 softc->blk_mask = 0;
2236 softc->flags |= SA_FLAG_TAPE_WP;
2238 if (comp_supported) {
2239 if (softc->saved_comp_algorithm == 0)
2240 softc->saved_comp_algorithm =
2241 softc->comp_algorithm;
2242 softc->flags |= SA_FLAG_COMP_SUPP;
2244 softc->flags |= SA_FLAG_COMP_ENABLED;
2246 softc->flags |= SA_FLAG_COMP_UNSUPP;
2248 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2249 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2250 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2253 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2255 xpt_print(periph->path,
2256 "unable to set buffered mode\n");
2258 error = 0; /* not an error */
2263 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2267 kfree(rblim, M_SCSISA);
2270 softc->dsreg = MTIO_DSREG_NIL;
2272 softc->fileno = softc->blkno = 0;
2273 softc->dsreg = MTIO_DSREG_REST;
2275 #ifdef SA_1FM_AT_EOD
2276 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2277 softc->quirks |= SA_QUIRK_1FM;
2279 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2280 softc->quirks |= SA_QUIRK_2FM;
2283 xpt_release_ccb(ccb);
2286 * If we return an error, we're not mounted any more,
2287 * so release any device reservation.
2290 sareservereleaseunit(periph, FALSE);
2293 * Clear I/O residual.
2295 softc->last_io_resid = 0;
2296 softc->last_ctl_resid = 0;
2302 * How many filemarks do we need to write if we were to terminate the
2303 * tape session right now? Note that this can be a negative number
2307 samarkswanted(struct cam_periph *periph)
2310 struct sa_softc *softc;
2312 softc = (struct sa_softc *)periph->softc;
2314 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2316 if (softc->quirks & SA_QUIRK_2FM)
2319 markswanted -= softc->filemarks;
2320 return (markswanted);
2324 sacheckeod(struct cam_periph *periph)
2329 markswanted = samarkswanted(periph);
2331 if (markswanted > 0) {
2332 error = sawritefilemarks(periph, markswanted, FALSE);
2340 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2342 static const char *toobig =
2343 "%d-byte tape record bigger than supplied buffer\n";
2344 struct cam_periph *periph;
2345 struct sa_softc *softc;
2346 struct ccb_scsiio *csio;
2347 struct scsi_sense_data *sense;
2348 u_int32_t resid = 0;
2351 int error_code, sense_key, asc, ascq, error, aqvalid;
2353 periph = xpt_path_periph(ccb->ccb_h.path);
2354 softc = (struct sa_softc *)periph->softc;
2356 sense = &csio->sense_data;
2357 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2358 aqvalid = sense->extra_len >= 6;
2361 status = csio->ccb_h.status & CAM_STATUS_MASK;
2364 * Calculate/latch up, any residuals... We do this in a funny 2-step
2365 * so we can print stuff here if we have CAM_DEBUG enabled for this
2368 if (status == CAM_SCSI_STATUS_ERROR) {
2369 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2370 info = (int32_t) scsi_4btoul(sense->info);
2372 if ((softc->flags & SA_FLAG_FIXED) != 0)
2373 resid *= softc->media_blksize;
2375 resid = csio->dxfer_len;
2377 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2378 if (softc->media_blksize)
2379 info /= softc->media_blksize;
2382 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2383 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2384 sizeof (struct scsi_sense_data));
2385 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2386 (int) csio->cdb_len);
2387 softc->last_io_resid = resid;
2388 softc->last_resid_was_io = 1;
2390 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2391 sizeof (struct scsi_sense_data));
2392 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2393 (int) csio->cdb_len);
2394 softc->last_ctl_resid = resid;
2395 softc->last_resid_was_io = 0;
2397 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2398 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %d "
2399 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2400 sense_key, asc, ascq, status,
2401 sense->flags & ~SSD_KEY_RESERVED, resid, csio->dxfer_len));
2403 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2404 ("Cam Status 0x%x\n", status));
2410 case CAM_SCSI_STATUS_ERROR:
2412 * If a read/write command, we handle it here.
2414 if (CCB_Type(csio) != SA_CCB_WAITING) {
2418 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2419 * on a non read/write command, we assume it's not an error
2420 * and propagate the residule and return.
2422 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2423 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2424 csio->resid = resid;
2429 * Otherwise, we let the common code handle this.
2431 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2435 * We cannot depend upon CAM honoring retry counts for these.
2437 case CAM_SCSI_BUS_RESET:
2439 if (ccb->ccb_h.retry_count <= 0) {
2444 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2448 * Handle filemark, end of tape, mismatched record sizes....
2449 * From this point out, we're only handling read/write cases.
2450 * Handle writes && reads differently.
2453 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2454 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2455 csio->resid = resid;
2457 } else if (sense->flags & SSD_EOM) {
2458 softc->flags |= SA_FLAG_EOM_PENDING;
2460 * Grotesque as it seems, the few times
2461 * I've actually seen a non-zero resid,
2462 * the tape drive actually lied and had
2463 * written all the data!.
2468 csio->resid = resid;
2469 if (sense_key == SSD_KEY_BLANK_CHECK) {
2470 if (softc->quirks & SA_QUIRK_1FM) {
2472 softc->flags |= SA_FLAG_EOM_PENDING;
2476 } else if (sense->flags & SSD_FILEMARK) {
2477 if (softc->flags & SA_FLAG_FIXED) {
2479 softc->flags |= SA_FLAG_EOF_PENDING;
2482 * Unconditionally, if we detected a filemark on a read,
2483 * mark that we've run moved a file ahead.
2485 if (softc->fileno != (daddr_t) -1) {
2488 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2494 * Incorrect Length usually applies to read, but can apply to writes.
2496 if (error == 0 && (sense->flags & SSD_ILI)) {
2498 xpt_print(csio->ccb_h.path, toobig,
2499 csio->dxfer_len - info);
2500 csio->resid = csio->dxfer_len;
2503 csio->resid = resid;
2504 if (softc->flags & SA_FLAG_FIXED) {
2505 softc->flags |= SA_FLAG_EIO_PENDING;
2508 * Bump the block number if we hadn't seen a filemark.
2509 * Do this independent of errors (we've moved anyway).
2511 if ((sense->flags & SSD_FILEMARK) == 0) {
2512 if (softc->blkno != (daddr_t) -1) {
2514 csio->ccb_h.ccb_pflags |=
2515 SA_POSITION_UPDATED;
2523 * Unfreeze the queue if frozen as we're not returning anything
2524 * to our waiters that would indicate an I/O error has occurred
2534 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2535 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2536 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2537 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2542 struct scsi_mode_header_6 *mode_hdr;
2543 struct scsi_mode_blk_desc *mode_blk;
2544 int mode_buffer_len;
2545 struct sa_softc *softc;
2550 softc = (struct sa_softc *)periph->softc;
2551 ccb = cam_periph_getccb(periph, 1);
2552 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2553 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2555 cpage = SA_DATA_COMPRESSION_PAGE;
2558 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2560 if (params_to_get & SA_PARAM_COMPRESSION) {
2561 if (softc->quirks & SA_QUIRK_NOCOMP) {
2562 *comp_supported = FALSE;
2563 params_to_get &= ~SA_PARAM_COMPRESSION;
2565 mode_buffer_len += sizeof (sa_comp_t);
2568 mode_buffer = kmalloc(mode_buffer_len, M_SCSISA, M_INTWAIT | M_ZERO);
2569 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2570 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2572 /* it is safe to retry this */
2573 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2574 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2575 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2576 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2578 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2579 &softc->device_stats);
2582 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2584 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2586 * Hmm. Let's see if we can try another page...
2587 * If we've already done that, give up on compression
2588 * for this device and remember this for the future
2589 * and attempt the request without asking for compression
2592 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2593 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2596 softc->quirks |= SA_QUIRK_NOCOMP;
2597 kfree(mode_buffer, M_SCSISA);
2599 } else if (status == CAM_SCSI_STATUS_ERROR) {
2600 /* Tell the user about the fatal error. */
2601 scsi_sense_print(&ccb->csio);
2602 goto sagetparamsexit;
2606 * If the user only wants the compression information, and
2607 * the device doesn't send back the block descriptor, it's
2608 * no big deal. If the user wants more than just
2609 * compression, though, and the device doesn't pass back the
2610 * block descriptor, we need to send another mode sense to
2611 * get the block descriptor.
2613 if ((mode_hdr->blk_desc_len == 0) &&
2614 (params_to_get & SA_PARAM_COMPRESSION) &&
2615 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2618 * Decrease the mode buffer length by the size of
2619 * the compression page, to make sure the data
2620 * there doesn't get overwritten.
2622 mode_buffer_len -= sizeof (sa_comp_t);
2625 * Now move the compression page that we presumably
2626 * got back down the memory chunk a little bit so
2627 * it doesn't get spammed.
2629 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2630 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2633 * Now, we issue another mode sense and just ask
2634 * for the block descriptor, etc.
2637 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2638 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2639 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2642 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2643 &softc->device_stats);
2647 goto sagetparamsexit;
2650 if (params_to_get & SA_PARAM_BLOCKSIZE)
2651 *blocksize = scsi_3btoul(mode_blk->blklen);
2653 if (params_to_get & SA_PARAM_NUMBLOCKS)
2654 *numblocks = scsi_3btoul(mode_blk->nblocks);
2656 if (params_to_get & SA_PARAM_BUFF_MODE)
2657 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2659 if (params_to_get & SA_PARAM_DENSITY)
2660 *density = mode_blk->density;
2662 if (params_to_get & SA_PARAM_WP)
2663 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2665 if (params_to_get & SA_PARAM_SPEED)
2666 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2668 if (params_to_get & SA_PARAM_COMPRESSION) {
2669 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2670 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2671 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2673 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2675 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2676 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2678 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2680 * We don't really know whether this device supports
2681 * Data Compression if the the algorithm field is
2682 * zero. Just say we do.
2684 *comp_supported = TRUE;
2686 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2687 *comp_algorithm = cp->sel_comp_alg;
2690 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2693 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2695 char *xyz = mode_buffer;
2696 xpt_print_path(periph->path);
2697 kprintf("Mode Sense Data=");
2698 for (idx = 0; idx < mode_buffer_len; idx++)
2699 kprintf(" 0x%02x", xyz[idx] & 0xff);
2705 xpt_release_ccb(ccb);
2706 kfree(mode_buffer, M_SCSISA);
2711 * The purpose of this function is to set one of four different parameters
2715 * - compression / compression algorithm
2718 * The assumption is that this will be called from saioctl(), and therefore
2719 * from a process context. Thus the waiting malloc calls below. If that
2720 * assumption ever changes, the malloc calls should be changed to be
2723 * Any or all of the four parameters may be set when this function is
2724 * called. It should handle setting more than one parameter at once.
2727 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2728 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2729 u_int32_t sense_flags)
2731 struct sa_softc *softc;
2732 u_int32_t current_blocksize;
2733 u_int32_t current_calg;
2734 u_int8_t current_density;
2735 u_int8_t current_speed;
2736 int comp_enabled, comp_supported;
2738 int mode_buffer_len;
2739 struct scsi_mode_header_6 *mode_hdr;
2740 struct scsi_mode_blk_desc *mode_blk;
2741 sa_comp_t *ccomp, *cpage;
2743 union ccb *ccb = NULL;
2746 softc = (struct sa_softc *)periph->softc;
2748 ccomp = kmalloc(sizeof (sa_comp_t), M_SCSISA, M_INTWAIT);
2751 * Since it doesn't make sense to set the number of blocks, or
2752 * write protection, we won't try to get the current value. We
2753 * always want to get the blocksize, so we can set it back to the
2756 error = sagetparams(periph,
2757 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2758 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2759 ¤t_speed, &comp_supported, &comp_enabled,
2760 ¤t_calg, ccomp);
2763 kfree(ccomp, M_SCSISA);
2767 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2768 if (params_to_set & SA_PARAM_COMPRESSION)
2769 mode_buffer_len += sizeof (sa_comp_t);
2771 mode_buffer = kmalloc(mode_buffer_len, M_SCSISA, M_INTWAIT | M_ZERO);
2773 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2774 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2776 ccb = cam_periph_getccb(periph, 1);
2780 if (params_to_set & SA_PARAM_COMPRESSION) {
2782 cpage = (sa_comp_t *)&mode_blk[1];
2784 cpage = (sa_comp_t *)&mode_hdr[1];
2786 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2787 cpage->hdr.pagecode &= ~0x80;
2792 * If the caller wants us to set the blocksize, use the one they
2793 * pass in. Otherwise, use the blocksize we got back from the
2794 * mode select above.
2797 if (params_to_set & SA_PARAM_BLOCKSIZE)
2798 scsi_ulto3b(blocksize, mode_blk->blklen);
2800 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2803 * Set density if requested, else preserve old density.
2804 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2805 * devices, else density we've latched up in our softc.
2807 if (params_to_set & SA_PARAM_DENSITY) {
2808 mode_blk->density = density;
2809 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2810 mode_blk->density = SCSI_SAME_DENSITY;
2812 mode_blk->density = softc->media_density;
2817 * For mode selects, these two fields must be zero.
2819 mode_hdr->data_length = 0;
2820 mode_hdr->medium_type = 0;
2822 /* set the speed to the current value */
2823 mode_hdr->dev_spec = current_speed;
2825 /* if set, set single-initiator buffering mode */
2826 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
2827 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2831 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2833 mode_hdr->blk_desc_len = 0;
2836 * First, if the user wants us to set the compression algorithm or
2837 * just turn compression on, check to make sure that this drive
2838 * supports compression.
2840 if (params_to_set & SA_PARAM_COMPRESSION) {
2842 * If the compression algorithm is 0, disable compression.
2843 * If the compression algorithm is non-zero, enable
2844 * compression and set the compression type to the
2845 * specified compression algorithm, unless the algorithm is
2846 * MT_COMP_ENABLE. In that case, we look at the
2847 * compression algorithm that is currently set and if it is
2848 * non-zero, we leave it as-is. If it is zero, and we have
2849 * saved a compression algorithm from a time when
2850 * compression was enabled before, set the compression to
2853 switch (ccomp->hdr.pagecode & ~0x80) {
2854 case SA_DEVICE_CONFIGURATION_PAGE:
2856 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2858 dcp->sel_comp_alg = SA_COMP_NONE;
2861 if (calg != MT_COMP_ENABLE) {
2862 dcp->sel_comp_alg = calg;
2863 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2864 softc->saved_comp_algorithm != 0) {
2865 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2869 case SA_DATA_COMPRESSION_PAGE:
2870 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2871 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2874 * Disable compression, but leave the
2875 * decompression and the capability bit
2878 dcp->dce_and_dcc = SA_DCP_DCC;
2879 dcp->dde_and_red |= SA_DCP_DDE;
2882 /* enable compression && decompression */
2883 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2884 dcp->dde_and_red |= SA_DCP_DDE;
2886 * If there, use compression algorithm from caller.
2887 * Otherwise, if there's a saved compression algorithm
2888 * and there is no current algorithm, use the saved
2889 * algorithm. Else parrot back what we got and hope
2892 if (calg != MT_COMP_ENABLE) {
2893 scsi_ulto4b(calg, dcp->comp_algorithm);
2894 scsi_ulto4b(calg, dcp->decomp_algorithm);
2895 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2896 softc->saved_comp_algorithm != 0) {
2897 scsi_ulto4b(softc->saved_comp_algorithm,
2898 dcp->comp_algorithm);
2899 scsi_ulto4b(softc->saved_comp_algorithm,
2900 dcp->decomp_algorithm);
2905 * Compression does not appear to be supported-
2906 * at least via the DATA COMPRESSION page. It
2907 * would be too much to ask us to believe that
2908 * the page itself is supported, but incorrectly
2909 * reports an ability to manipulate data compression,
2910 * so we'll assume that this device doesn't support
2911 * compression. We can just fall through for that.
2916 * The drive doesn't seem to support compression,
2917 * so turn off the set compression bit.
2919 params_to_set &= ~SA_PARAM_COMPRESSION;
2920 xpt_print(periph->path,
2921 "device does not seem to support compression\n");
2924 * If that was the only thing the user wanted us to set,
2925 * clean up allocated resources and return with
2926 * 'operation not supported'.
2928 if (params_to_set == SA_PARAM_NONE) {
2929 kfree(mode_buffer, M_SCSISA);
2930 xpt_release_ccb(ccb);
2935 * That wasn't the only thing the user wanted us to set.
2936 * So, decrease the stated mode buffer length by the
2937 * size of the compression mode page.
2939 mode_buffer_len -= sizeof(sa_comp_t);
2943 /* It is safe to retry this operation */
2944 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2945 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2946 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2948 error = cam_periph_runccb(ccb, saerror, 0,
2949 sense_flags, &softc->device_stats);
2952 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2954 char *xyz = mode_buffer;
2955 xpt_print_path(periph->path);
2956 kprintf("Err%d, Mode Select Data=", error);
2957 for (idx = 0; idx < mode_buffer_len; idx++)
2958 kprintf(" 0x%02x", xyz[idx] & 0xff);
2965 * If we can, try without setting density/blocksize.
2968 if ((params_to_set &
2969 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2974 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2975 cpage = (sa_comp_t *)&mode_blk[1];
2979 * If we were setting the blocksize, and that failed, we
2980 * want to set it to its original value. If we weren't
2981 * setting the blocksize, we don't want to change it.
2983 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2986 * Set density if requested, else preserve old density.
2987 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2988 * devices, else density we've latched up in our softc.
2990 if (params_to_set & SA_PARAM_DENSITY) {
2991 mode_blk->density = current_density;
2992 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2993 mode_blk->density = SCSI_SAME_DENSITY;
2995 mode_blk->density = softc->media_density;
2998 if (params_to_set & SA_PARAM_COMPRESSION)
2999 bcopy(ccomp, cpage, sizeof (sa_comp_t));
3002 * The retry count is the only CCB field that might have been
3003 * changed that we care about, so reset it back to 1.
3005 ccb->ccb_h.retry_count = 1;
3006 cam_periph_runccb(ccb, saerror, 0, sense_flags,
3007 &softc->device_stats);
3011 xpt_release_ccb(ccb);
3014 kfree(ccomp, M_SCSISA);
3016 if (params_to_set & SA_PARAM_COMPRESSION) {
3018 softc->flags &= ~SA_FLAG_COMP_ENABLED;
3020 * Even if we get an error setting compression,
3021 * do not say that we don't support it. We could
3022 * have been wrong, or it may be media specific.
3023 * softc->flags &= ~SA_FLAG_COMP_SUPP;
3025 softc->saved_comp_algorithm = softc->comp_algorithm;
3026 softc->comp_algorithm = 0;
3028 softc->flags |= SA_FLAG_COMP_ENABLED;
3029 softc->comp_algorithm = calg;
3033 kfree(mode_buffer, M_SCSISA);
3038 saprevent(struct cam_periph *periph, int action)
3040 struct sa_softc *softc;
3044 softc = (struct sa_softc *)periph->softc;
3046 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3048 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3052 * We can be quiet about illegal requests.
3054 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3059 ccb = cam_periph_getccb(periph, 1);
3061 /* It is safe to retry this operation */
3062 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3063 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3065 error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats);
3068 if (action == PR_ALLOW)
3069 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3071 softc->flags |= SA_FLAG_TAPE_LOCKED;
3074 xpt_release_ccb(ccb);
3078 sarewind(struct cam_periph *periph)
3081 struct sa_softc *softc;
3084 softc = (struct sa_softc *)periph->softc;
3086 ccb = cam_periph_getccb(periph, 1);
3088 /* It is safe to retry this operation */
3089 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3090 SSD_FULL_SIZE, REWIND_TIMEOUT);
3092 softc->dsreg = MTIO_DSREG_REW;
3093 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3094 softc->dsreg = MTIO_DSREG_REST;
3096 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3097 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3099 xpt_release_ccb(ccb);
3101 softc->fileno = softc->blkno = (daddr_t) 0;
3103 softc->fileno = softc->blkno = (daddr_t) -1;
3108 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3111 struct sa_softc *softc;
3114 softc = (struct sa_softc *)periph->softc;
3116 ccb = cam_periph_getccb(periph, 1);
3118 /* This cannot be retried */
3120 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3121 SSD_FULL_SIZE, SPACE_TIMEOUT);
3124 * Clear residual because we will be using it.
3126 softc->last_ctl_resid = 0;
3128 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3129 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3130 softc->dsreg = MTIO_DSREG_REST;
3132 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3133 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3135 xpt_release_ccb(ccb);
3138 * If a spacing operation has failed, we need to invalidate
3141 * If the spacing operation was setmarks or to end of recorded data,
3142 * we no longer know our relative position.
3144 * If the spacing operations was spacing files in reverse, we
3145 * take account of the residual, but still check against less
3146 * than zero- if we've gone negative, we must have hit BOT.
3148 * If the spacing operations was spacing records in reverse and
3149 * we have a residual, we've either hit BOT or hit a filemark.
3150 * In the former case, we know our new record number (0). In
3151 * the latter case, we have absolutely no idea what the real
3152 * record number is- we've stopped between the end of the last
3153 * record in the previous file and the filemark that stopped
3154 * our spacing backwards.
3157 softc->fileno = softc->blkno = (daddr_t) -1;
3158 } else if (code == SS_SETMARKS || code == SS_EOD) {
3159 softc->fileno = softc->blkno = (daddr_t) -1;
3160 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3161 softc->fileno += (count - softc->last_ctl_resid);
3162 if (softc->fileno < 0) /* we must of hit BOT */
3165 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3166 softc->blkno += (count - softc->last_ctl_resid);
3168 if (softc->last_ctl_resid || softc->blkno < 0) {
3169 if (softc->fileno == 0) {
3172 softc->blkno = (daddr_t) -1;
3181 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3184 struct sa_softc *softc;
3187 softc = (struct sa_softc *)periph->softc;
3188 if (softc->open_rdonly)
3191 ccb = cam_periph_getccb(periph, 1);
3193 * Clear residual because we will be using it.
3195 softc->last_ctl_resid = 0;
3197 softc->dsreg = MTIO_DSREG_FMK;
3198 /* this *must* not be retried */
3199 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3200 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3201 softc->dsreg = MTIO_DSREG_REST;
3204 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3206 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3207 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3209 if (error == 0 && nmarks) {
3210 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3211 nwm = nmarks - softc->last_ctl_resid;
3212 softc->filemarks += nwm;
3215 xpt_release_ccb(ccb);
3218 * Update relative positions (if we're doing that).
3221 softc->fileno = softc->blkno = (daddr_t) -1;
3222 } else if (softc->fileno != (daddr_t) -1) {
3223 softc->fileno += nwm;
3230 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3232 struct scsi_tape_position_data loc;
3234 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3238 * We try and flush any buffered writes here if we were writing
3239 * and we're trying to get hardware block position. It eats
3240 * up performance substantially, but I'm wary of drive firmware.
3242 * I think that *logical* block position is probably okay-
3243 * but hardware block position might have to wait for data
3244 * to hit media to be valid. Caveat Emptor.
3247 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3248 error = sawritefilemarks(periph, 0, 0);
3249 if (error && error != EACCES)
3253 ccb = cam_periph_getccb(periph, 1);
3254 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3255 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3256 softc->dsreg = MTIO_DSREG_RBSY;
3257 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3258 softc->dsreg = MTIO_DSREG_REST;
3259 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3260 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3263 if (loc.flags & SA_RPOS_UNCERTAIN) {
3264 error = EINVAL; /* nothing is certain */
3266 *blkptr = scsi_4btoul(loc.firstblk);
3270 xpt_release_ccb(ccb);
3275 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3278 struct sa_softc *softc;
3282 * We used to try and flush any buffered writes here.
3283 * Now we push this onto user applications to either
3284 * flush the pending writes themselves (via a zero count
3285 * WRITE FILEMARKS command) or they can trust their tape
3286 * drive to do this correctly for them.
3289 softc = (struct sa_softc *)periph->softc;
3290 ccb = cam_periph_getccb(periph, 1);
3293 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3294 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3297 softc->dsreg = MTIO_DSREG_POS;
3298 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3299 softc->dsreg = MTIO_DSREG_REST;
3300 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3301 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3302 xpt_release_ccb(ccb);
3304 * Note relative file && block number position as now unknown.
3306 softc->fileno = softc->blkno = (daddr_t) -1;
3311 saretension(struct cam_periph *periph)
3314 struct sa_softc *softc;
3317 softc = (struct sa_softc *)periph->softc;
3319 ccb = cam_periph_getccb(periph, 1);
3321 /* It is safe to retry this operation */
3322 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3323 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3325 softc->dsreg = MTIO_DSREG_TEN;
3326 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3327 softc->dsreg = MTIO_DSREG_REST;
3329 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3330 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3331 xpt_release_ccb(ccb);
3333 softc->fileno = softc->blkno = (daddr_t) 0;
3335 softc->fileno = softc->blkno = (daddr_t) -1;
3340 sareservereleaseunit(struct cam_periph *periph, int reserve)
3343 struct sa_softc *softc;
3346 softc = (struct sa_softc *)periph->softc;
3347 ccb = cam_periph_getccb(periph, 1);
3349 /* It is safe to retry this operation */
3350 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3351 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3352 softc->dsreg = MTIO_DSREG_RBSY;
3353 error = cam_periph_runccb(ccb, saerror, 0,
3354 SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats);
3355 softc->dsreg = MTIO_DSREG_REST;
3357 xpt_release_ccb(ccb);
3360 * If the error was Illegal Request, then the device doesn't support
3361 * RESERVE/RELEASE. This is not an error.
3363 if (error == EINVAL) {
3371 saloadunload(struct cam_periph *periph, int load)
3374 struct sa_softc *softc;
3377 softc = (struct sa_softc *)periph->softc;
3379 ccb = cam_periph_getccb(periph, 1);
3381 /* It is safe to retry this operation */
3382 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3383 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3385 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3386 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3387 softc->dsreg = MTIO_DSREG_REST;
3389 xpt_release_ccb(ccb);
3391 if (error || load == 0)
3392 softc->fileno = softc->blkno = (daddr_t) -1;
3393 else if (error == 0)
3394 softc->fileno = softc->blkno = (daddr_t) 0;
3399 saerase(struct cam_periph *periph, int longerase)
3403 struct sa_softc *softc;
3406 softc = (struct sa_softc *)periph->softc;
3407 if (softc->open_rdonly)
3410 ccb = cam_periph_getccb(periph, 1);
3412 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3413 SSD_FULL_SIZE, ERASE_TIMEOUT);
3415 softc->dsreg = MTIO_DSREG_ZER;
3416 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3417 softc->dsreg = MTIO_DSREG_REST;
3419 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3420 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3421 xpt_release_ccb(ccb);
3425 #endif /* _KERNEL */
3428 * Read tape block limits command.
3431 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3432 void (*cbfcnp)(struct cam_periph *, union ccb *),
3433 u_int8_t tag_action,
3434 struct scsi_read_block_limits_data *rlimit_buf,
3435 u_int8_t sense_len, u_int32_t timeout)
3437 struct scsi_read_block_limits *scsi_cmd;
3439 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3440 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3441 sizeof(*scsi_cmd), timeout);
3443 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3444 bzero(scsi_cmd, sizeof(*scsi_cmd));
3445 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3449 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3450 void (*cbfcnp)(struct cam_periph *, union ccb *),
3451 u_int8_t tag_action, int readop, int sli,
3452 int fixed, u_int32_t length, u_int8_t *data_ptr,
3453 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3455 struct scsi_sa_rw *scsi_cmd;
3457 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3458 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3459 scsi_cmd->sli_fixed = 0;
3461 scsi_cmd->sli_fixed |= SAR_SLI;
3463 scsi_cmd->sli_fixed |= SARW_FIXED;
3464 scsi_ulto3b(length, scsi_cmd->length);
3465 scsi_cmd->control = 0;
3467 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3468 tag_action, data_ptr, dxfer_len, sense_len,
3469 sizeof(*scsi_cmd), timeout);
3473 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3474 void (*cbfcnp)(struct cam_periph *, union ccb *),
3475 u_int8_t tag_action, int immediate, int eot,
3476 int reten, int load, u_int8_t sense_len,
3479 struct scsi_load_unload *scsi_cmd;
3481 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3482 bzero(scsi_cmd, sizeof(*scsi_cmd));
3483 scsi_cmd->opcode = LOAD_UNLOAD;
3485 scsi_cmd->immediate = SLU_IMMED;
3487 scsi_cmd->eot_reten_load |= SLU_EOT;
3489 scsi_cmd->eot_reten_load |= SLU_RETEN;
3491 scsi_cmd->eot_reten_load |= SLU_LOAD;
3493 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3494 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3498 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3499 void (*cbfcnp)(struct cam_periph *, union ccb *),
3500 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3503 struct scsi_rewind *scsi_cmd;
3505 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3506 bzero(scsi_cmd, sizeof(*scsi_cmd));
3507 scsi_cmd->opcode = REWIND;
3509 scsi_cmd->immediate = SREW_IMMED;
3511 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3512 0, sense_len, sizeof(*scsi_cmd), timeout);
3516 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3517 void (*cbfcnp)(struct cam_periph *, union ccb *),
3518 u_int8_t tag_action, scsi_space_code code,
3519 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3521 struct scsi_space *scsi_cmd;
3523 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3524 scsi_cmd->opcode = SPACE;
3525 scsi_cmd->code = code;
3526 scsi_ulto3b(count, scsi_cmd->count);
3527 scsi_cmd->control = 0;
3529 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3530 0, sense_len, sizeof(*scsi_cmd), timeout);
3534 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3535 void (*cbfcnp)(struct cam_periph *, union ccb *),
3536 u_int8_t tag_action, int immediate, int setmark,
3537 u_int32_t num_marks, u_int8_t sense_len,
3540 struct scsi_write_filemarks *scsi_cmd;
3542 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3543 bzero(scsi_cmd, sizeof(*scsi_cmd));
3544 scsi_cmd->opcode = WRITE_FILEMARKS;
3546 scsi_cmd->byte2 |= SWFMRK_IMMED;
3548 scsi_cmd->byte2 |= SWFMRK_WSMK;
3550 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3552 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3553 0, sense_len, sizeof(*scsi_cmd), timeout);
3557 * The reserve and release unit commands differ only by their opcodes.
3560 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3561 void (*cbfcnp)(struct cam_periph *, union ccb *),
3562 u_int8_t tag_action, int third_party,
3563 int third_party_id, u_int8_t sense_len,
3564 u_int32_t timeout, int reserve)
3566 struct scsi_reserve_release_unit *scsi_cmd;
3568 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3569 bzero(scsi_cmd, sizeof(*scsi_cmd));
3572 scsi_cmd->opcode = RESERVE_UNIT;
3574 scsi_cmd->opcode = RELEASE_UNIT;
3577 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3578 scsi_cmd->lun_thirdparty |=
3579 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3582 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3583 0, sense_len, sizeof(*scsi_cmd), timeout);
3587 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3588 void (*cbfcnp)(struct cam_periph *, union ccb *),
3589 u_int8_t tag_action, int immediate, int long_erase,
3590 u_int8_t sense_len, u_int32_t timeout)
3592 struct scsi_erase *scsi_cmd;
3594 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3595 bzero(scsi_cmd, sizeof(*scsi_cmd));
3597 scsi_cmd->opcode = ERASE;
3600 scsi_cmd->lun_imm_long |= SE_IMMED;
3603 scsi_cmd->lun_imm_long |= SE_LONG;
3605 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3606 0, sense_len, sizeof(*scsi_cmd), timeout);
3610 * Read Tape Position command.
3613 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3614 void (*cbfcnp)(struct cam_periph *, union ccb *),
3615 u_int8_t tag_action, int hardsoft,
3616 struct scsi_tape_position_data *sbp,
3617 u_int8_t sense_len, u_int32_t timeout)
3619 struct scsi_tape_read_position *scmd;
3621 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3622 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3623 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3624 bzero(scmd, sizeof(*scmd));
3625 scmd->opcode = READ_POSITION;
3626 scmd->byte1 = hardsoft;
3630 * Set Tape Position command.
3633 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3634 void (*cbfcnp)(struct cam_periph *, union ccb *),
3635 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3636 u_int8_t sense_len, u_int32_t timeout)
3638 struct scsi_tape_locate *scmd;
3640 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3641 NULL, 0, sense_len, sizeof(*scmd), timeout);
3642 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3643 bzero(scmd, sizeof(*scmd));
3644 scmd->opcode = LOCATE;
3646 scmd->byte1 |= SA_SPOS_BT;
3647 scsi_ulto4b(blkno, scmd->blkaddr);