2 * $FreeBSD: src/sys/cam/scsi/scsi_sa.c,v 1.45.2.13 2002/12/17 17:08:50 trhodes Exp $
4 * Implementation of SCSI Sequential Access Peripheral driver for CAM.
6 * Copyright (c) 1999, 2000 Matthew Jacob
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification, immediately at the beginning of the file.
15 * 2. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
33 #include <sys/queue.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/types.h>
40 #include <sys/malloc.h>
44 #include <sys/devicestat.h>
45 #include <machine/limits.h>
53 #include <cam/cam_ccb.h>
54 #include <cam/cam_extend.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_xpt_periph.h>
57 #include <cam/cam_debug.h>
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/scsi/scsi_sa.h>
68 #define SA_IO_TIMEOUT 4
70 #ifndef SA_SPACE_TIMEOUT
71 #define SA_SPACE_TIMEOUT 1 * 60
73 #ifndef SA_REWIND_TIMEOUT
74 #define SA_REWIND_TIMEOUT 2 * 60
76 #ifndef SA_ERASE_TIMEOUT
77 #define SA_ERASE_TIMEOUT 4 * 60
80 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
82 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
83 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
84 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
85 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
88 * Additional options that can be set for config: SA_1FM_AT_EOT
91 #ifndef UNUSED_PARAMETER
92 #define UNUSED_PARAMETER(x) x = x
96 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
97 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
105 SA_STATE_NORMAL, SA_STATE_ABNORMAL
108 #define ccb_pflags ppriv_field0
109 #define ccb_bp ppriv_ptr1
111 #define SA_CCB_BUFFER_IO 0x0
112 #define SA_CCB_WAITING 0x1
113 #define SA_CCB_TYPEMASK 0x1
114 #define SA_POSITION_UPDATED 0x2
116 #define Set_CCB_Type(x, type) \
117 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
118 x->ccb_h.ccb_pflags |= type
120 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
125 SA_FLAG_OPEN = 0x0001,
126 SA_FLAG_FIXED = 0x0002,
127 SA_FLAG_TAPE_LOCKED = 0x0004,
128 SA_FLAG_TAPE_MOUNTED = 0x0008,
129 SA_FLAG_TAPE_WP = 0x0010,
130 SA_FLAG_TAPE_WRITTEN = 0x0020,
131 SA_FLAG_EOM_PENDING = 0x0040,
132 SA_FLAG_EIO_PENDING = 0x0080,
133 SA_FLAG_EOF_PENDING = 0x0100,
134 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
135 SA_FLAG_EOF_PENDING),
136 SA_FLAG_INVALID = 0x0200,
137 SA_FLAG_COMP_ENABLED = 0x0400,
138 SA_FLAG_COMP_SUPP = 0x0800,
139 SA_FLAG_COMP_UNSUPP = 0x1000,
140 SA_FLAG_TAPE_FROZEN = 0x2000
144 SA_MODE_REWIND = 0x00,
145 SA_MODE_NOREWIND = 0x01,
146 SA_MODE_OFFLINE = 0x02
150 SA_PARAM_NONE = 0x00,
151 SA_PARAM_BLOCKSIZE = 0x01,
152 SA_PARAM_DENSITY = 0x02,
153 SA_PARAM_COMPRESSION = 0x04,
154 SA_PARAM_BUFF_MODE = 0x08,
155 SA_PARAM_NUMBLOCKS = 0x10,
157 SA_PARAM_SPEED = 0x40,
162 SA_QUIRK_NONE = 0x00,
163 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
164 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
165 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
166 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
167 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
168 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
169 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
170 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
173 /* units are bits 4-7, 16-21 (1024 units) */
174 #define SAUNIT(DEV) \
175 (((minor(DEV) & 0xF0) >> 4) | ((minor(DEV) & 0x3f0000) >> 16))
177 #define SAMODE(z) ((minor(z) & 0x3))
178 #define SADENSITY(z) (((minor(z) >> 2) & 0x3))
179 #define SA_IS_CTRL(z) (minor(z) & (1 << 29))
181 #define SA_NOT_CTLDEV 0
185 #define SA_ATYPE_NR 1
186 #define SA_ATYPE_ER 2
188 #define SAMINOR(ctl, unit, mode, access) \
189 ((ctl << 29) | ((unit & 0x3f0) << 16) | ((unit & 0xf) << 4) | \
190 (mode << 0x2) | (access & 0x3))
192 #define SA_NUM_MODES 4
195 struct sa_mode_devs {
199 } mode_devs[SA_NUM_MODES];
209 struct buf_queue_head buf_queue;
211 struct devstat device_stats;
218 u_int32_t comp_algorithm;
219 u_int32_t saved_comp_algorithm;
220 u_int32_t media_blksize;
221 u_int32_t last_media_blksize;
222 u_int32_t media_numblks;
223 u_int8_t media_density;
226 u_int8_t dsreg; /* mtio mt_dsreg, redux */
230 int last_resid_was_io;
233 * Relative to BOT Location.
242 struct scsi_sense_data _last_io_sense;
243 u_int32_t _last_io_resid;
244 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
245 struct scsi_sense_data _last_ctl_sense;
246 u_int32_t _last_ctl_resid;
247 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
248 #define last_io_sense errinfo._last_io_sense
249 #define last_io_resid errinfo._last_io_resid
250 #define last_io_cdb errinfo._last_io_cdb
251 #define last_ctl_sense errinfo._last_ctl_sense
252 #define last_ctl_resid errinfo._last_ctl_resid
253 #define last_ctl_cdb errinfo._last_ctl_cdb
256 * Misc other flags/state
260 ctrl_mode : 1; /* control device open */
263 struct sa_quirk_entry {
264 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
265 sa_quirks quirks; /* specific quirk type */
266 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
269 static struct sa_quirk_entry sa_quirk_table[] =
272 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
273 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
274 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
277 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
278 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
281 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
282 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
285 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
286 "Python*", "*"}, SA_QUIRK_NODREAD, 0
289 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
290 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
293 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
294 "VIPER 2525 25462", "-011"},
295 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
298 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
299 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
303 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
304 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
308 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
309 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
312 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
313 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
316 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
317 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
320 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
321 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
324 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
325 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
328 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
329 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
331 { /* jreynold@primenet.com */
332 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
333 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
335 { /* mike@sentex.net */
336 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
337 "STT20000*", "*"}, SA_QUIRK_1FM, 0
340 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
341 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
344 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
345 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
348 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
349 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
352 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
353 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
356 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
357 " SLR*", "*"}, SA_QUIRK_1FM, 0
360 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
361 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
364 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
365 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
369 static d_open_t saopen;
370 static d_close_t saclose;
371 static d_strategy_t sastrategy;
372 static d_ioctl_t saioctl;
373 static periph_init_t sainit;
374 static periph_ctor_t saregister;
375 static periph_oninv_t saoninvalidate;
376 static periph_dtor_t sacleanup;
377 static periph_start_t sastart;
378 static void saasync(void *callback_arg, u_int32_t code,
379 struct cam_path *path, void *arg);
380 static void sadone(struct cam_periph *periph,
381 union ccb *start_ccb);
382 static int saerror(union ccb *ccb, u_int32_t cam_flags,
383 u_int32_t sense_flags);
384 static int samarkswanted(struct cam_periph *);
385 static int sacheckeod(struct cam_periph *periph);
386 static int sagetparams(struct cam_periph *periph,
387 sa_params params_to_get,
388 u_int32_t *blocksize, u_int8_t *density,
389 u_int32_t *numblocks, int *buff_mode,
390 u_int8_t *write_protect, u_int8_t *speed,
391 int *comp_supported, int *comp_enabled,
392 u_int32_t *comp_algorithm,
393 sa_comp_t *comp_page);
394 static int sasetparams(struct cam_periph *periph,
395 sa_params params_to_set,
396 u_int32_t blocksize, u_int8_t density,
397 u_int32_t comp_algorithm,
398 u_int32_t sense_flags);
399 static void saprevent(struct cam_periph *periph, int action);
400 static int sarewind(struct cam_periph *periph);
401 static int saspace(struct cam_periph *periph, int count,
402 scsi_space_code code);
403 static int samount(struct cam_periph *, int, dev_t);
404 static int saretension(struct cam_periph *periph);
405 static int sareservereleaseunit(struct cam_periph *periph,
407 static int saloadunload(struct cam_periph *periph, int load);
408 static int saerase(struct cam_periph *periph, int longerase);
409 static int sawritefilemarks(struct cam_periph *periph,
410 int nmarks, int setmarks);
411 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
412 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
415 static struct periph_driver sadriver =
418 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
421 DATA_SET(periphdriver_set, sadriver);
423 /* For 2.2-stable support */
428 #define SA_CDEV_MAJOR 14
430 static struct cdevsw sa_cdevsw = {
434 /* write */ physwrite,
438 /* strategy */ sastrategy,
440 /* maj */ SA_CDEV_MAJOR,
447 static struct extend_array *saperiphs;
450 saopen(dev_t dev, int flags, int fmt, struct proc *p)
452 struct cam_periph *periph;
453 struct sa_softc *softc;
462 density = SADENSITY(dev);
465 periph = cam_extend_get(saperiphs, unit);
466 if (periph == NULL) {
470 softc = (struct sa_softc *)periph->softc;
471 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
477 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
478 ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
480 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
481 cam_periph_unlock(periph);
484 if (SA_IS_CTRL(dev)) {
485 softc->ctrl_mode = 1;
486 cam_periph_unlock(periph);
491 if (softc->flags & SA_FLAG_OPEN) {
493 } else if (softc->flags & SA_FLAG_INVALID) {
497 * The function samount ensures media is loaded and ready.
498 * It also does a device RESERVE if the tape isn't yet mounted.
500 error = samount(periph, flags, dev);
504 cam_periph_release(periph);
506 saprevent(periph, PR_PREVENT);
507 softc->flags |= SA_FLAG_OPEN;
509 cam_periph_unlock(periph);
514 saclose(dev_t dev, int flag, int fmt, struct proc *p)
516 struct cam_periph *periph;
517 struct sa_softc *softc;
518 int unit, mode, error, writing, tmp;
519 int closedbits = SA_FLAG_OPEN;
523 periph = cam_extend_get(saperiphs, unit);
527 softc = (struct sa_softc *)periph->softc;
529 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
530 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
533 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
537 if (SA_IS_CTRL(dev)) {
538 softc->ctrl_mode = 0;
539 cam_periph_release(periph);
540 cam_periph_unlock(periph);
545 * Were we writing the tape?
547 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
550 * See whether or not we need to write filemarks. If this
551 * fails, we probably have to assume we've lost tape
554 error = sacheckeod(periph);
556 xpt_print_path(periph->path);
557 printf("failed to write terminating filemark(s)\n");
558 softc->flags |= SA_FLAG_TAPE_FROZEN;
562 * Whatever we end up doing, allow users to eject tapes from here on.
564 saprevent(periph, PR_ALLOW);
567 * Decide how to end...
569 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
570 closedbits |= SA_FLAG_TAPE_FROZEN;
571 } else switch (mode) {
572 case SA_MODE_OFFLINE:
574 * An 'offline' close is an unconditional release of
575 * frozen && mount conditions, irrespective of whether
576 * these operations succeeded. The reason for this is
577 * to allow at least some kind of programmatic way
578 * around our state getting all fouled up. If somebody
579 * issues an 'offline' command, that will be allowed
582 (void) sarewind(periph);
583 (void) saloadunload(periph, FALSE);
584 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
588 * If the rewind fails, return an error- if anyone cares,
589 * but not overwriting any previous error.
591 * We don't clear the notion of mounted here, but we do
592 * clear the notion of frozen if we successfully rewound.
594 tmp = sarewind(periph);
599 closedbits |= SA_FLAG_TAPE_FROZEN;
602 case SA_MODE_NOREWIND:
604 * If we're not rewinding/unloading the tape, find out
605 * whether we need to back up over one of two filemarks
606 * we wrote (if we wrote two filemarks) so that appends
607 * from this point on will be sane.
609 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
610 tmp = saspace(periph, -1, SS_FILEMARKS);
612 xpt_print_path(periph->path);
613 printf("unable to backspace over one of double"
614 " filemarks at end of tape\n");
615 xpt_print_path(periph->path);
616 printf("it is possible that this device"
617 " needs a SA_QUIRK_1FM quirk set for it\n");
618 softc->flags |= SA_FLAG_TAPE_FROZEN;
623 xpt_print_path(periph->path);
624 panic("unknown mode 0x%x in saclose\n", mode);
630 * We wish to note here that there are no more filemarks to be written.
632 softc->filemarks = 0;
633 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
636 * And we are no longer open for business.
638 softc->flags &= ~closedbits;
641 * Inform users if tape state if frozen....
643 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
644 xpt_print_path(periph->path);
645 printf("tape is now frozen- use an OFFLINE, REWIND or MTEOM "
646 "command to clear this state.\n");
649 /* release the device if it is no longer mounted */
650 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
651 sareservereleaseunit(periph, FALSE);
653 cam_periph_unlock(periph);
654 cam_periph_release(periph);
660 * Actually translate the requested transfer into one the physical driver
661 * can understand. The transfer is described by a buf and will include
662 * only one physical transfer.
665 sastrategy(struct buf *bp)
667 struct cam_periph *periph;
668 struct sa_softc *softc;
672 if (SA_IS_CTRL(bp->b_dev)) {
673 bp->b_error = EINVAL;
676 unit = SAUNIT(bp->b_dev);
677 periph = cam_extend_get(saperiphs, unit);
678 if (periph == NULL) {
682 softc = (struct sa_softc *)periph->softc;
686 if (softc->flags & SA_FLAG_INVALID) {
692 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
701 * If it's a null transfer, return immediatly
703 if (bp->b_bcount == 0)
707 if (softc->flags & SA_FLAG_FIXED) {
709 * Fixed block device. The byte count must
710 * be a multiple of our block size.
712 if (((softc->blk_mask != ~0) &&
713 ((bp->b_bcount & softc->blk_mask) != 0)) ||
714 ((softc->blk_mask == ~0) &&
715 ((bp->b_bcount % softc->min_blk) != 0))) {
716 xpt_print_path(periph->path);
717 printf("Invalid request. Fixed block device "
718 "requests must be a multiple "
719 "of %d bytes\n", softc->media_blksize);
720 bp->b_error = EINVAL;
723 } else if ((bp->b_bcount > softc->max_blk) ||
724 (bp->b_bcount < softc->min_blk) ||
725 (bp->b_bcount & softc->blk_mask) != 0) {
727 xpt_print_path(periph->path);
728 printf("Invalid request. Variable block device "
729 "requests must be ");
730 if (softc->blk_mask != 0) {
731 printf("a multiple of %d ", (0x1 << softc->blk_gran));
733 printf("between %d and %d bytes\n", softc->min_blk,
735 bp->b_error = EINVAL;
740 * Mask interrupts so that the device cannot be invalidated until
741 * after we are in the queue. Otherwise, we might not properly
742 * clean up one of the buffers.
747 * Place it at the end of the queue.
749 bufq_insert_tail(&softc->buf_queue, bp);
751 softc->queue_count++;
752 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("sastrategy: enqueuing a %d "
753 "%s byte %s queue count now %d\n", (int) bp->b_bcount,
754 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
755 (bp->b_flags & B_READ)? "read" : "write", softc->queue_count));
760 * Schedule ourselves for performing the work.
762 xpt_schedule(periph, 1);
766 bp->b_flags |= B_ERROR;
770 * Correctly set the buf to indicate a completed xfer
772 bp->b_resid = bp->b_bcount;
777 saioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
779 struct cam_periph *periph;
780 struct sa_softc *softc;
781 scsi_space_code spaceop;
782 int didlockperiph = 0;
791 density = SADENSITY(dev);
792 error = 0; /* shut up gcc */
793 spaceop = 0; /* shut up gcc */
795 periph = cam_extend_get(saperiphs, unit);
799 softc = (struct sa_softc *)periph->softc;
802 * Check for control mode accesses. We allow MTIOCGET and
803 * MTIOCERRSTAT (but need to be the only one open in order
804 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
805 * and MTCOMP (but need to be the only one accessing this
806 * device to run those).
809 if (SA_IS_CTRL(dev)) {
811 case MTIOCGETEOTMODEL:
816 * If the periph isn't already locked, lock it
817 * so our MTIOCERRSTAT can reset latched error stats.
819 * If the periph is already locked, skip it because
820 * we're just getting status and it'll be up to the
821 * other thread that has this device open to do
822 * an MTIOCERRSTAT that would clear latched status.
825 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
826 error = cam_periph_lock(periph, PRIBIO|PCATCH);
835 case MTIOCSETEOTMODEL:
840 * We need to acquire the peripheral here rather
841 * than at open time because we are sharing writable
842 * access to data structures.
845 error = cam_periph_lock(periph, PRIBIO|PCATCH);
859 * Find the device that the user is talking about
864 struct mtget *g = (struct mtget *)arg;
867 * If this isn't the control mode device, actually go out
868 * and ask the drive again what it's set to.
870 if (!SA_IS_CTRL(dev)) {
871 u_int8_t write_protect;
872 int comp_enabled, comp_supported;
873 error = sagetparams(periph, SA_PARAM_ALL,
874 &softc->media_blksize, &softc->media_density,
875 &softc->media_numblks, &softc->buffer_mode,
876 &write_protect, &softc->speed, &comp_supported,
877 &comp_enabled, &softc->comp_algorithm, NULL);
881 softc->flags |= SA_FLAG_TAPE_WP;
883 softc->flags &= ~SA_FLAG_TAPE_WP;
884 softc->flags &= ~(SA_FLAG_COMP_SUPP|
885 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
886 if (comp_supported) {
887 if (softc->saved_comp_algorithm == 0)
888 softc->saved_comp_algorithm =
889 softc->comp_algorithm;
890 softc->flags |= SA_FLAG_COMP_SUPP;
892 softc->flags |= SA_FLAG_COMP_ENABLED;
894 softc->flags |= SA_FLAG_COMP_UNSUPP;
896 bzero(g, sizeof(struct mtget));
897 g->mt_type = MT_ISAR;
898 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
899 g->mt_comp = MT_COMP_UNSUPP;
900 g->mt_comp0 = MT_COMP_UNSUPP;
901 g->mt_comp1 = MT_COMP_UNSUPP;
902 g->mt_comp2 = MT_COMP_UNSUPP;
903 g->mt_comp3 = MT_COMP_UNSUPP;
905 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
906 g->mt_comp = MT_COMP_DISABLED;
908 g->mt_comp = softc->comp_algorithm;
910 g->mt_comp0 = softc->comp_algorithm;
911 g->mt_comp1 = softc->comp_algorithm;
912 g->mt_comp2 = softc->comp_algorithm;
913 g->mt_comp3 = softc->comp_algorithm;
915 g->mt_density = softc->media_density;
916 g->mt_density0 = softc->media_density;
917 g->mt_density1 = softc->media_density;
918 g->mt_density2 = softc->media_density;
919 g->mt_density3 = softc->media_density;
920 g->mt_blksiz = softc->media_blksize;
921 g->mt_blksiz0 = softc->media_blksize;
922 g->mt_blksiz1 = softc->media_blksize;
923 g->mt_blksiz2 = softc->media_blksize;
924 g->mt_blksiz3 = softc->media_blksize;
925 g->mt_fileno = softc->fileno;
926 g->mt_blkno = softc->blkno;
927 g->mt_dsreg = (short) softc->dsreg;
929 * Yes, we know that this is likely to overflow
931 if (softc->last_resid_was_io) {
932 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
933 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
934 softc->last_io_resid = 0;
938 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
939 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
940 softc->last_ctl_resid = 0;
949 struct scsi_tape_errors *sep =
950 &((union mterrstat *)arg)->scsi_errstat;
952 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
953 ("saioctl: MTIOCERRSTAT\n"));
955 bzero(sep, sizeof(*sep));
956 sep->io_resid = softc->last_io_resid;
957 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
958 sizeof (sep->io_sense));
959 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
960 sizeof (sep->io_cdb));
961 sep->ctl_resid = softc->last_ctl_resid;
962 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
963 sizeof (sep->ctl_sense));
964 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
965 sizeof (sep->ctl_cdb));
967 if (SA_IS_CTRL(dev) == 0 || didlockperiph)
968 bzero((caddr_t) &softc->errinfo,
969 sizeof (softc->errinfo));
978 mt = (struct mtop *)arg;
980 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
981 ("saioctl: op=0x%x count=0x%x\n",
982 mt->mt_op, mt->mt_count));
984 count = mt->mt_count;
986 case MTWEOF: /* write an end-of-file marker */
988 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
989 * flag because by keeping track of filemarks
990 * we have last written we know ehether or not
991 * we need to write more when we close the device.
993 error = sawritefilemarks(periph, count, FALSE);
995 case MTWSS: /* write a setmark */
996 error = sawritefilemarks(periph, count, TRUE);
998 case MTBSR: /* backward space record */
999 case MTFSR: /* forward space record */
1000 case MTBSF: /* backward space file */
1001 case MTFSF: /* forward space file */
1002 case MTBSS: /* backward space setmark */
1003 case MTFSS: /* forward space setmark */
1004 case MTEOD: /* space to end of recorded medium */
1008 spaceop = SS_FILEMARKS;
1009 nmarks = softc->filemarks;
1010 error = sacheckeod(periph);
1012 xpt_print_path(periph->path);
1013 printf("EOD check prior to spacing failed\n");
1014 softc->flags |= SA_FLAG_EIO_PENDING;
1017 nmarks -= softc->filemarks;
1023 spaceop = SS_BLOCKS;
1034 spaceop = SS_SETMARKS;
1048 nmarks = softc->filemarks;
1050 * XXX: Why are we checking again?
1052 error = sacheckeod(periph);
1055 nmarks -= softc->filemarks;
1056 error = saspace(periph, count - nmarks, spaceop);
1058 * At this point, clear that we've written the tape
1059 * and that we've written any filemarks. We really
1060 * don't know what the applications wishes to do next-
1061 * the sacheckeod's will make sure we terminated the
1062 * tape correctly if we'd been writing, but the next
1063 * action the user application takes will set again
1064 * whether we need to write filemarks.
1067 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1068 softc->filemarks = 0;
1071 case MTREW: /* rewind */
1072 (void) sacheckeod(periph);
1073 error = sarewind(periph);
1076 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1077 softc->flags &= ~SA_FLAG_ERR_PENDING;
1078 softc->filemarks = 0;
1080 case MTERASE: /* erase */
1081 error = saerase(periph, count);
1083 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1084 softc->flags &= ~SA_FLAG_ERR_PENDING;
1086 case MTRETENS: /* re-tension tape */
1087 error = saretension(periph);
1089 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1090 softc->flags &= ~SA_FLAG_ERR_PENDING;
1092 case MTOFFL: /* rewind and put the drive offline */
1094 (void) sacheckeod(periph);
1096 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1097 softc->filemarks = 0;
1099 error = sarewind(periph);
1100 /* clear the frozen flag anyway */
1101 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1104 * Be sure to allow media removal before ejecting.
1107 saprevent(periph, PR_ALLOW);
1109 error = saloadunload(periph, FALSE);
1111 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1116 case MTNOP: /* no operation, sets status only */
1117 case MTCACHE: /* enable controller cache */
1118 case MTNOCACHE: /* disable controller cache */
1122 case MTSETBSIZ: /* Set block size for device */
1124 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1127 softc->last_media_blksize =
1128 softc->media_blksize;
1129 softc->media_blksize = count;
1131 softc->flags |= SA_FLAG_FIXED;
1132 if (powerof2(count)) {
1135 softc->blk_mask = count - 1;
1137 softc->blk_mask = ~0;
1138 softc->blk_shift = 0;
1141 * Make the user's desire 'persistent'.
1143 softc->quirks &= ~SA_QUIRK_VARIABLE;
1144 softc->quirks |= SA_QUIRK_FIXED;
1146 softc->flags &= ~SA_FLAG_FIXED;
1147 if (softc->max_blk == 0) {
1148 softc->max_blk = ~0;
1150 softc->blk_shift = 0;
1151 if (softc->blk_gran != 0) {
1153 softc->blk_gran - 1;
1155 softc->blk_mask = 0;
1158 * Make the user's desire 'persistent'.
1160 softc->quirks |= SA_QUIRK_VARIABLE;
1161 softc->quirks &= ~SA_QUIRK_FIXED;
1165 case MTSETDNSTY: /* Set density for device and mode */
1166 if (count > UCHAR_MAX) {
1170 error = sasetparams(periph, SA_PARAM_DENSITY,
1174 case MTCOMP: /* enable compression */
1176 * Some devices don't support compression, and
1177 * don't like it if you ask them for the
1180 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1181 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1185 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1186 0, 0, count, SF_NO_PRINT);
1198 error = sardpos(periph, 0, (u_int32_t *) arg);
1201 error = sardpos(periph, 1, (u_int32_t *) arg);
1204 error = sasetpos(periph, 0, (u_int32_t *) arg);
1207 error = sasetpos(periph, 1, (u_int32_t *) arg);
1209 case MTIOCGETEOTMODEL:
1211 if (softc->quirks & SA_QUIRK_1FM)
1215 *((u_int32_t *) arg) = mode;
1217 case MTIOCSETEOTMODEL:
1219 switch (*((u_int32_t *) arg)) {
1221 softc->quirks &= ~SA_QUIRK_2FM;
1222 softc->quirks |= SA_QUIRK_1FM;
1225 softc->quirks &= ~SA_QUIRK_1FM;
1226 softc->quirks |= SA_QUIRK_2FM;
1234 error = cam_periph_ioctl(periph, cmd, arg, saerror);
1239 * Check to see if we cleared a frozen state
1241 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1247 softc->fileno = (daddr_t) -1;
1248 softc->blkno = (daddr_t) -1;
1249 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1250 xpt_print_path(periph->path);
1251 printf("tape state now unfrozen.\n");
1257 if (didlockperiph) {
1258 cam_periph_unlock(periph);
1267 struct cam_path *path;
1270 * Create our extend array for storing the devices we attach to.
1272 saperiphs = cam_extend_new();
1273 if (saperiphs == NULL) {
1274 printf("sa: Failed to alloc extend array!\n");
1279 * Install a global async callback.
1281 status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1282 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1284 if (status == CAM_REQ_CMP) {
1285 /* Register the async callbacks of interrest */
1286 struct ccb_setasync csa; /*
1287 * This is an immediate CCB,
1288 * so using the stack is OK
1290 xpt_setup_ccb(&csa.ccb_h, path, 5);
1291 csa.ccb_h.func_code = XPT_SASYNC_CB;
1292 csa.event_enable = AC_FOUND_DEVICE;
1293 csa.callback = saasync;
1294 csa.callback_arg = NULL;
1295 xpt_action((union ccb *)&csa);
1296 status = csa.ccb_h.status;
1297 xpt_free_path(path);
1300 if (status != CAM_REQ_CMP) {
1301 printf("sa: Failed to attach master async callback "
1302 "due to status 0x%x!\n", status);
1307 saoninvalidate(struct cam_periph *periph)
1309 struct sa_softc *softc;
1311 struct ccb_setasync csa;
1314 softc = (struct sa_softc *)periph->softc;
1317 * De-register any async callbacks.
1319 xpt_setup_ccb(&csa.ccb_h, periph->path,
1321 csa.ccb_h.func_code = XPT_SASYNC_CB;
1322 csa.event_enable = 0;
1323 csa.callback = saasync;
1324 csa.callback_arg = periph;
1325 xpt_action((union ccb *)&csa);
1327 softc->flags |= SA_FLAG_INVALID;
1330 * Although the oninvalidate() routines are always called at
1331 * splsoftcam, we need to be at splbio() here to keep the buffer
1332 * queue from being modified while we traverse it.
1337 * Return all queued I/O with ENXIO.
1338 * XXX Handle any transactions queued to the card
1339 * with XPT_ABORT_CCB.
1341 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
1342 bufq_remove(&softc->buf_queue, q_bp);
1343 q_bp->b_resid = q_bp->b_bcount;
1344 q_bp->b_error = ENXIO;
1345 q_bp->b_flags |= B_ERROR;
1348 softc->queue_count = 0;
1351 xpt_print_path(periph->path);
1352 printf("lost device\n");
1357 sacleanup(struct cam_periph *periph)
1359 struct sa_softc *softc;
1362 softc = (struct sa_softc *)periph->softc;
1364 devstat_remove_entry(&softc->device_stats);
1366 destroy_dev(softc->devs.ctl_dev);
1367 destroy_dev(softc->devs.r_dev);
1368 destroy_dev(softc->devs.nr_dev);
1369 destroy_dev(softc->devs.er_dev);
1371 for (i = 0; i < SA_NUM_MODES; i++) {
1372 destroy_dev(softc->devs.mode_devs[i].r_dev);
1373 destroy_dev(softc->devs.mode_devs[i].nr_dev);
1374 destroy_dev(softc->devs.mode_devs[i].er_dev);
1377 cam_extend_release(saperiphs, periph->unit_number);
1378 xpt_print_path(periph->path);
1379 printf("removing device entry\n");
1380 free(softc, M_DEVBUF);
1384 saasync(void *callback_arg, u_int32_t code,
1385 struct cam_path *path, void *arg)
1387 struct cam_periph *periph;
1389 periph = (struct cam_periph *)callback_arg;
1391 case AC_FOUND_DEVICE:
1393 struct ccb_getdev *cgd;
1396 cgd = (struct ccb_getdev *)arg;
1400 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1404 * Allocate a peripheral instance for
1405 * this device and start the probe
1408 status = cam_periph_alloc(saregister, saoninvalidate,
1410 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1411 saasync, AC_FOUND_DEVICE, cgd);
1413 if (status != CAM_REQ_CMP
1414 && status != CAM_REQ_INPROG)
1415 printf("saasync: Unable to probe new device "
1416 "due to status 0x%x\n", status);
1420 cam_periph_async(periph, code, path, arg);
1426 saregister(struct cam_periph *periph, void *arg)
1428 struct sa_softc *softc;
1429 struct ccb_setasync csa;
1430 struct ccb_getdev *cgd;
1434 cgd = (struct ccb_getdev *)arg;
1435 if (periph == NULL) {
1436 printf("saregister: periph was NULL!!\n");
1437 return (CAM_REQ_CMP_ERR);
1441 printf("saregister: no getdev CCB, can't register device\n");
1442 return (CAM_REQ_CMP_ERR);
1445 softc = (struct sa_softc *)malloc(sizeof (*softc), M_DEVBUF, M_NOWAIT);
1446 if (softc == NULL) {
1447 printf("saregister: Unable to probe new device. "
1448 "Unable to allocate softc\n");
1449 return (CAM_REQ_CMP_ERR);
1452 bzero(softc, sizeof(*softc));
1453 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1454 softc->state = SA_STATE_NORMAL;
1455 softc->fileno = (daddr_t) -1;
1456 softc->blkno = (daddr_t) -1;
1458 bufq_init(&softc->buf_queue);
1459 periph->softc = softc;
1460 cam_extend_set(saperiphs, periph->unit_number, periph);
1463 * See if this device has any quirks.
1465 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1466 (caddr_t)sa_quirk_table,
1467 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1468 sizeof(*sa_quirk_table), scsi_inquiry_match);
1470 if (match != NULL) {
1471 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1472 softc->last_media_blksize =
1473 ((struct sa_quirk_entry *)match)->prefblk;
1475 xpt_print_path(periph->path);
1476 printf("found quirk entry %d\n", (int)
1477 (((struct sa_quirk_entry *) match) - sa_quirk_table));
1480 softc->quirks = SA_QUIRK_NONE;
1483 * The SA driver supports a blocksize, but we don't know the
1484 * blocksize until we media is inserted. So, set a flag to
1485 * indicate that the blocksize is unavailable right now.
1487 devstat_add_entry(&softc->device_stats, "sa", periph->unit_number, 0,
1488 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1489 DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE);
1491 softc->devs.ctl_dev = make_dev(&sa_cdevsw, SAMINOR(SA_CTLDEV,
1492 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1493 0660, "r%s%d.ctl", periph->periph_name, periph->unit_number);
1495 softc->devs.r_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV,
1496 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1497 0660, "r%s%d", periph->periph_name, periph->unit_number);
1499 softc->devs.nr_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV,
1500 periph->unit_number, 0, SA_ATYPE_NR), UID_ROOT, GID_OPERATOR,
1501 0660, "nr%s%d", periph->periph_name, periph->unit_number);
1503 softc->devs.er_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV,
1504 periph->unit_number, 0, SA_ATYPE_ER), UID_ROOT, GID_OPERATOR,
1505 0660, "er%s%d", periph->periph_name, periph->unit_number);
1507 for (i = 0; i < SA_NUM_MODES; i++) {
1509 softc->devs.mode_devs[i].r_dev = make_dev(&sa_cdevsw,
1510 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R),
1511 UID_ROOT, GID_OPERATOR, 0660, "r%s%d.%d",
1512 periph->periph_name, periph->unit_number, i);
1514 softc->devs.mode_devs[i].nr_dev = make_dev(&sa_cdevsw,
1515 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR),
1516 UID_ROOT, GID_OPERATOR, 0660, "nr%s%d.%d",
1517 periph->periph_name, periph->unit_number, i);
1520 softc->devs.mode_devs[i].er_dev = make_dev(&sa_cdevsw,
1521 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER),
1522 UID_ROOT, GID_OPERATOR, 0660, "er%s%d.%d",
1523 periph->periph_name, periph->unit_number, i);
1527 * Add an async callback so that we get
1528 * notified if this device goes away.
1530 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
1531 csa.ccb_h.func_code = XPT_SASYNC_CB;
1532 csa.event_enable = AC_LOST_DEVICE;
1533 csa.callback = saasync;
1534 csa.callback_arg = periph;
1535 xpt_action((union ccb *)&csa);
1537 xpt_announce_periph(periph, NULL);
1539 return (CAM_REQ_CMP);
1543 sastart(struct cam_periph *periph, union ccb *start_ccb)
1545 struct sa_softc *softc;
1547 softc = (struct sa_softc *)periph->softc;
1549 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("sastart"));
1552 switch (softc->state) {
1553 case SA_STATE_NORMAL:
1555 /* Pull a buffer from the queue and get going on it */
1560 * See if there is a buf with work for us to do..
1563 bp = bufq_first(&softc->buf_queue);
1564 if (periph->immediate_priority <= periph->pinfo.priority) {
1565 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1566 ("queuing for immediate ccb\n"));
1567 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1568 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1570 periph->immediate_priority = CAM_PRIORITY_NONE;
1572 wakeup(&periph->ccb_list);
1573 } else if (bp == NULL) {
1575 xpt_release_ccb(start_ccb);
1576 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1577 struct buf *done_bp;
1579 softc->queue_count--;
1580 bufq_remove(&softc->buf_queue, bp);
1581 bp->b_resid = bp->b_bcount;
1583 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1585 * We now just clear errors in this case
1586 * and let the residual be the notifier.
1589 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1591 * This can only happen if we're reading
1592 * in fixed length mode. In this case,
1593 * we dump the rest of the list the
1597 if (bufq_first(&softc->buf_queue) != NULL) {
1601 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1603 bp->b_flags |= B_ERROR;
1605 bp = bufq_first(&softc->buf_queue);
1607 * Only if we have no other buffers queued up
1608 * do we clear the pending error flag.
1611 softc->flags &= ~SA_FLAG_ERR_PENDING;
1612 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1613 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
1614 "%d more buffers queued up\n",
1615 (softc->flags & SA_FLAG_ERR_PENDING),
1616 (bp != NULL)? "not " : " ", softc->queue_count));
1618 xpt_release_ccb(start_ccb);
1623 bufq_remove(&softc->buf_queue, bp);
1624 softc->queue_count--;
1626 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1627 if (softc->blk_shift != 0) {
1629 bp->b_bcount >> softc->blk_shift;
1630 } else if (softc->media_blksize != 0) {
1632 bp->b_bcount / softc->media_blksize;
1635 xpt_print_path(periph->path);
1636 printf("zero blocksize for "
1637 "FIXED length writes?\n");
1642 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1643 ("Fixed Record Count is %d\n", length));
1645 length = bp->b_bcount;
1646 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1647 ("Variable Record Count is %d\n", length));
1649 devstat_start_transaction(&softc->device_stats);
1651 * Some people have theorized that we should
1652 * suppress illegal length indication if we are
1653 * running in variable block mode so that we don't
1654 * have to request sense every time our requested
1655 * block size is larger than the written block.
1656 * The residual information from the ccb allows
1657 * us to identify this situation anyway. The only
1658 * problem with this is that we will not get
1659 * information about blocks that are larger than
1660 * our read buffer unless we set the block size
1661 * in the mode page to something other than 0.
1663 * I believe that this is a non-issue. If user apps
1664 * don't adjust their read size to match our record
1665 * size, that's just life. Anyway, the typical usage
1666 * would be to issue, e.g., 64KB reads and occasionally
1667 * have to do deal with 512 byte or 1KB intermediate
1670 softc->dsreg = (bp->b_flags & B_READ)?
1671 MTIO_DSREG_RD : MTIO_DSREG_WR;
1672 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1673 MSG_SIMPLE_Q_TAG, (bp->b_flags & B_READ) != 0,
1674 FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1675 length, bp->b_data, bp->b_bcount, SSD_FULL_SIZE,
1677 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1678 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1679 start_ccb->ccb_h.ccb_bp = bp;
1680 bp = bufq_first(&softc->buf_queue);
1682 xpt_action(start_ccb);
1686 /* Have more work to do, so ensure we stay scheduled */
1687 xpt_schedule(periph, 1);
1691 case SA_STATE_ABNORMAL:
1693 panic("state 0x%x in sastart", softc->state);
1700 sadone(struct cam_periph *periph, union ccb *done_ccb)
1702 struct sa_softc *softc;
1703 struct ccb_scsiio *csio;
1705 softc = (struct sa_softc *)periph->softc;
1706 csio = &done_ccb->csio;
1707 switch (CCB_Type(csio)) {
1708 case SA_CCB_BUFFER_IO:
1713 softc->dsreg = MTIO_DSREG_REST;
1714 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1716 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1717 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1719 * A retry was scheduled, so just return.
1730 * Catastrophic error. Mark the tape as frozen
1731 * (we no longer know tape position).
1733 * Return all queued I/O with EIO, and unfreeze
1734 * our queue so that future transactions that
1735 * attempt to fix this problem can get to the
1741 softc->flags |= SA_FLAG_TAPE_FROZEN;
1742 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL) {
1743 bufq_remove(&softc->buf_queue, q_bp);
1744 q_bp->b_resid = q_bp->b_bcount;
1745 q_bp->b_error = EIO;
1746 q_bp->b_flags |= B_ERROR;
1752 bp->b_resid = bp->b_bcount;
1753 bp->b_error = error;
1754 bp->b_flags |= B_ERROR;
1756 * In the error case, position is updated in saerror.
1759 bp->b_resid = csio->resid;
1761 if (csio->resid != 0) {
1762 bp->b_flags |= B_ERROR;
1764 if ((bp->b_flags & B_READ) == 0) {
1765 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1766 softc->filemarks = 0;
1768 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1769 (softc->blkno != (daddr_t) -1)) {
1770 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1772 if (softc->blk_shift != 0) {
1777 softc->media_blksize;
1779 softc->blkno += (daddr_t) l;
1786 * If we had an error (immediate or pending),
1787 * release the device queue now.
1789 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1790 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1792 if (error || bp->b_resid) {
1793 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1794 ("error %d resid %ld count %ld\n", error,
1795 bp->b_resid, bp->b_bcount));
1798 devstat_end_transaction_buf(&softc->device_stats, bp);
1802 case SA_CCB_WAITING:
1804 /* Caller will release the CCB */
1805 wakeup(&done_ccb->ccb_h.cbfcnp);
1809 xpt_release_ccb(done_ccb);
1813 * Mount the tape (make sure it's ready for I/O).
1816 samount(struct cam_periph *periph, int oflags, dev_t dev)
1818 struct sa_softc *softc;
1823 * oflags can be checked for 'kind' of open (read-only check) - later
1824 * dev can be checked for a control-mode or compression open - later
1826 UNUSED_PARAMETER(oflags);
1827 UNUSED_PARAMETER(dev);
1830 softc = (struct sa_softc *)periph->softc;
1833 * This should determine if something has happend since the last
1834 * open/mount that would invalidate the mount. We do *not* want
1835 * to retry this command- we just want the status. But we only
1836 * do this if we're mounted already- if we're not mounted,
1837 * we don't care about the unit read state and can instead use
1838 * this opportunity to attempt to reserve the tape unit.
1841 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1842 ccb = cam_periph_getccb(periph, 1);
1843 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1844 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1845 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1846 &softc->device_stats);
1848 if (error == ENXIO) {
1849 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1850 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1851 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1852 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1853 &softc->device_stats);
1857 * We don't need to freeze the tape because we
1858 * will now attempt to rewind/load it.
1860 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1861 if (CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO)) {
1862 xpt_print_path(ccb->ccb_h.path);
1863 printf("error %d on TUR in samount\n", error);
1867 error = sareservereleaseunit(periph, TRUE);
1871 ccb = cam_periph_getccb(periph, 1);
1872 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1873 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1874 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1875 &softc->device_stats);
1879 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1880 struct scsi_read_block_limits_data *rblim = NULL;
1881 int comp_enabled, comp_supported;
1882 u_int8_t write_protect, guessing = 0;
1885 * Clear out old state.
1887 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1888 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1889 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1890 softc->filemarks = 0;
1893 * *Very* first off, make sure we're loaded to BOT.
1895 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1896 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1897 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1898 &softc->device_stats);
1902 * In case this doesn't work, do a REWIND instead
1905 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1906 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1907 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1908 &softc->device_stats);
1912 xpt_release_ccb(ccb);
1917 * Do a dummy test read to force access to the
1918 * media so that the drive will really know what's
1919 * there. We actually don't really care what the
1920 * blocksize on tape is and don't expect to really
1921 * read a full record.
1923 rblim = (struct scsi_read_block_limits_data *)
1924 malloc(8192, M_TEMP, M_WAITOK);
1925 if (rblim == NULL) {
1926 xpt_print_path(ccb->ccb_h.path);
1927 printf("no memory for test read\n");
1928 xpt_release_ccb(ccb);
1933 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1934 scsi_sa_read_write(&ccb->csio, 0, sadone,
1935 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1936 (void *) rblim, 8192, SSD_FULL_SIZE,
1938 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1939 &softc->device_stats);
1941 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1942 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1943 error = cam_periph_runccb(ccb, saerror, 0,
1944 SF_NO_PRINT | SF_RETRY_SELTO | SF_RETRY_UA,
1945 &softc->device_stats);
1948 xpt_print_path(ccb->ccb_h.path);
1949 printf("unable to rewind after test read\n");
1950 xpt_release_ccb(ccb);
1956 * Next off, determine block limits.
1958 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1959 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1961 error = cam_periph_runccb(ccb, saerror, 0,
1962 SF_NO_PRINT | SF_RETRY_UA | SF_RETRY_SELTO,
1963 &softc->device_stats);
1965 xpt_release_ccb(ccb);
1969 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1970 * a MANDATORY command. Anyway- it doesn't matter-
1971 * we can proceed anyway.
1973 softc->blk_gran = 0;
1974 softc->max_blk = ~0;
1977 if (softc->scsi_rev >= SCSI_REV_3) {
1978 softc->blk_gran = RBL_GRAN(rblim);
1980 softc->blk_gran = 0;
1983 * We take max_blk == min_blk to mean a default to
1984 * fixed mode- but note that whatever we get out of
1985 * sagetparams below will actually determine whether
1986 * we are actually *in* fixed mode.
1988 softc->max_blk = scsi_3btoul(rblim->maximum);
1989 softc->min_blk = scsi_2btoul(rblim->minimum);
1994 * Next, perform a mode sense to determine
1995 * current density, blocksize, compression etc.
1997 error = sagetparams(periph, SA_PARAM_ALL,
1998 &softc->media_blksize,
1999 &softc->media_density,
2000 &softc->media_numblks,
2001 &softc->buffer_mode, &write_protect,
2002 &softc->speed, &comp_supported,
2003 &comp_enabled, &softc->comp_algorithm,
2008 * We could work a little harder here. We could
2009 * adjust our attempts to get information. It
2010 * might be an ancient tape drive. If someone
2011 * nudges us, we'll do that.
2017 * If no quirk has determined that this is a device that is
2018 * preferred to be in fixed or variable mode, now is the time
2021 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2024 * This could be expensive to find out. Luckily we
2025 * only need to do this once. If we start out in
2026 * 'default' mode, try and set ourselves to one
2027 * of the densities that would determine a wad
2028 * of other stuff. Go from highest to lowest.
2030 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2032 static u_int8_t ctry[] = {
2033 SCSI_DENSITY_HALFINCH_PE,
2034 SCSI_DENSITY_HALFINCH_6250C,
2035 SCSI_DENSITY_HALFINCH_6250,
2036 SCSI_DENSITY_HALFINCH_1600,
2037 SCSI_DENSITY_HALFINCH_800,
2038 SCSI_DENSITY_QIC_4GB,
2039 SCSI_DENSITY_QIC_2GB,
2040 SCSI_DENSITY_QIC_525_320,
2041 SCSI_DENSITY_QIC_150,
2042 SCSI_DENSITY_QIC_120,
2043 SCSI_DENSITY_QIC_24,
2044 SCSI_DENSITY_QIC_11_9TRK,
2045 SCSI_DENSITY_QIC_11_4TRK,
2046 SCSI_DENSITY_QIC_1320,
2047 SCSI_DENSITY_QIC_3080,
2050 for (i = 0; ctry[i]; i++) {
2051 error = sasetparams(periph,
2052 SA_PARAM_DENSITY, 0, ctry[i],
2055 softc->media_density = ctry[i];
2060 switch (softc->media_density) {
2061 case SCSI_DENSITY_QIC_11_4TRK:
2062 case SCSI_DENSITY_QIC_11_9TRK:
2063 case SCSI_DENSITY_QIC_24:
2064 case SCSI_DENSITY_QIC_120:
2065 case SCSI_DENSITY_QIC_150:
2066 case SCSI_DENSITY_QIC_525_320:
2067 case SCSI_DENSITY_QIC_1320:
2068 case SCSI_DENSITY_QIC_3080:
2069 softc->quirks &= ~SA_QUIRK_2FM;
2070 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2071 softc->last_media_blksize = 512;
2073 case SCSI_DENSITY_QIC_4GB:
2074 case SCSI_DENSITY_QIC_2GB:
2075 softc->quirks &= ~SA_QUIRK_2FM;
2076 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2077 softc->last_media_blksize = 1024;
2080 softc->last_media_blksize =
2081 softc->media_blksize;
2082 softc->quirks |= SA_QUIRK_VARIABLE;
2088 * If no quirk has determined that this is a device that needs
2089 * to have 2 Filemarks at EOD, now is the time to find out.
2092 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2093 switch (softc->media_density) {
2094 case SCSI_DENSITY_HALFINCH_800:
2095 case SCSI_DENSITY_HALFINCH_1600:
2096 case SCSI_DENSITY_HALFINCH_6250:
2097 case SCSI_DENSITY_HALFINCH_6250C:
2098 case SCSI_DENSITY_HALFINCH_PE:
2099 softc->quirks &= ~SA_QUIRK_1FM;
2100 softc->quirks |= SA_QUIRK_2FM;
2108 * Now validate that some info we got makes sense.
2110 if ((softc->max_blk < softc->media_blksize) ||
2111 (softc->min_blk > softc->media_blksize &&
2112 softc->media_blksize)) {
2113 xpt_print_path(ccb->ccb_h.path);
2114 printf("BLOCK LIMITS (%d..%d) could not match current "
2115 "block settings (%d)- adjusting\n", softc->min_blk,
2116 softc->max_blk, softc->media_blksize);
2117 softc->max_blk = softc->min_blk =
2118 softc->media_blksize;
2122 * Now put ourselves into the right frame of mind based
2127 * If we want to be in FIXED mode and our current blocksize
2128 * is not equal to our last blocksize (if nonzero), try and
2129 * set ourselves to this last blocksize (as the 'preferred'
2130 * block size). The initial quirkmatch at registry sets the
2131 * initial 'last' blocksize. If, for whatever reason, this
2132 * 'last' blocksize is zero, set the blocksize to 512,
2133 * or min_blk if that's larger.
2135 if ((softc->quirks & SA_QUIRK_FIXED) &&
2136 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2137 (softc->media_blksize != softc->last_media_blksize)) {
2138 softc->media_blksize = softc->last_media_blksize;
2139 if (softc->media_blksize == 0) {
2140 softc->media_blksize = 512;
2141 if (softc->media_blksize < softc->min_blk) {
2142 softc->media_blksize = softc->min_blk;
2145 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2146 softc->media_blksize, 0, 0, SF_NO_PRINT);
2148 xpt_print_path(ccb->ccb_h.path);
2149 printf("unable to set fixed blocksize to %d\n",
2150 softc->media_blksize);
2155 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2156 (softc->media_blksize != 0)) {
2157 softc->last_media_blksize = softc->media_blksize;
2158 softc->media_blksize = 0;
2159 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2160 0, 0, 0, SF_NO_PRINT);
2163 * If this fails and we were guessing, just
2164 * assume that we got it wrong and go try
2165 * fixed block mode. Don't even check against
2166 * density code at this point.
2169 softc->quirks &= ~SA_QUIRK_VARIABLE;
2170 softc->quirks |= SA_QUIRK_FIXED;
2171 if (softc->last_media_blksize == 0)
2172 softc->last_media_blksize = 512;
2175 xpt_print_path(ccb->ccb_h.path);
2176 printf("unable to set variable blocksize\n");
2182 * Now that we have the current block size,
2183 * set up some parameters for sastart's usage.
2185 if (softc->media_blksize) {
2186 softc->flags |= SA_FLAG_FIXED;
2187 if (powerof2(softc->media_blksize)) {
2189 ffs(softc->media_blksize) - 1;
2190 softc->blk_mask = softc->media_blksize - 1;
2192 softc->blk_mask = ~0;
2193 softc->blk_shift = 0;
2197 * The SCSI-3 spec allows 0 to mean "unspecified".
2198 * The SCSI-1 spec allows 0 to mean 'infinite'.
2200 * Either works here.
2202 if (softc->max_blk == 0) {
2203 softc->max_blk = ~0;
2205 softc->blk_shift = 0;
2206 if (softc->blk_gran != 0) {
2207 softc->blk_mask = softc->blk_gran - 1;
2209 softc->blk_mask = 0;
2214 softc->flags |= SA_FLAG_TAPE_WP;
2216 if (comp_supported) {
2217 if (softc->saved_comp_algorithm == 0)
2218 softc->saved_comp_algorithm =
2219 softc->comp_algorithm;
2220 softc->flags |= SA_FLAG_COMP_SUPP;
2222 softc->flags |= SA_FLAG_COMP_ENABLED;
2224 softc->flags |= SA_FLAG_COMP_UNSUPP;
2226 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2227 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2228 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2231 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2233 xpt_print_path(ccb->ccb_h.path);
2234 printf("unable to set buffered mode\n");
2236 error = 0; /* not an error */
2241 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2245 free(rblim, M_TEMP);
2248 softc->dsreg = MTIO_DSREG_NIL;
2250 softc->fileno = softc->blkno = 0;
2251 softc->dsreg = MTIO_DSREG_REST;
2253 #ifdef SA_1FM_AT_EOD
2254 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2255 softc->quirks |= SA_QUIRK_1FM;
2257 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2258 softc->quirks |= SA_QUIRK_2FM;
2261 xpt_release_ccb(ccb);
2264 * If we return an error, we're not mounted any more,
2265 * so release any device reservation.
2268 (void) sareservereleaseunit(periph, FALSE);
2271 * Clear I/O residual.
2273 softc->last_io_resid = 0;
2274 softc->last_ctl_resid = 0;
2280 * How many filemarks do we need to write if we were to terminate the
2281 * tape session right now? Note that this can be a negative number
2285 samarkswanted(struct cam_periph *periph)
2288 struct sa_softc *softc;
2290 softc = (struct sa_softc *)periph->softc;
2292 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2294 if (softc->quirks & SA_QUIRK_2FM)
2297 markswanted -= softc->filemarks;
2298 return (markswanted);
2302 sacheckeod(struct cam_periph *periph)
2306 struct sa_softc *softc;
2308 softc = (struct sa_softc *)periph->softc;
2309 markswanted = samarkswanted(periph);
2311 if (markswanted > 0) {
2312 error = sawritefilemarks(periph, markswanted, FALSE);
2320 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2322 static const char *toobig =
2323 "%d-byte tape record bigger than supplied buffer\n";
2324 struct cam_periph *periph;
2325 struct sa_softc *softc;
2326 struct ccb_scsiio *csio;
2327 struct scsi_sense_data *sense;
2328 u_int32_t resid = 0;
2331 int error_code, sense_key, asc, ascq, error, aqvalid;
2333 periph = xpt_path_periph(ccb->ccb_h.path);
2334 softc = (struct sa_softc *)periph->softc;
2336 sense = &csio->sense_data;
2337 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2338 aqvalid = sense->extra_len >= 6;
2341 status = csio->ccb_h.status & CAM_STATUS_MASK;
2344 * Calculate/latch up, any residuals... We do this in a funny 2-step
2345 * so we can print stuff here if we have CAM_DEBUG enabled for this
2348 if (status == CAM_SCSI_STATUS_ERROR) {
2349 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2350 info = (int32_t) scsi_4btoul(sense->info);
2352 if ((softc->flags & SA_FLAG_FIXED) != 0)
2353 resid *= softc->media_blksize;
2355 resid = csio->dxfer_len;
2357 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2358 if (softc->media_blksize)
2359 info /= softc->media_blksize;
2362 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2363 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2364 sizeof (struct scsi_sense_data));
2365 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2366 (int) csio->cdb_len);
2367 softc->last_io_resid = resid;
2368 softc->last_resid_was_io = 1;
2370 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2371 sizeof (struct scsi_sense_data));
2372 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2373 (int) csio->cdb_len);
2374 softc->last_ctl_resid = resid;
2375 softc->last_resid_was_io = 0;
2377 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2378 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %d "
2379 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2380 sense_key, asc, ascq, status,
2381 sense->flags & ~SSD_KEY_RESERVED, resid, csio->dxfer_len));
2383 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2384 ("Cam Status 0x%x\n", status));
2390 case CAM_SCSI_STATUS_ERROR:
2392 * If a read/write command, we handle it here.
2394 if (CCB_Type(csio) != SA_CCB_WAITING) {
2398 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2399 * on a non read/write command, we assume it's not an error
2400 * and propagate the residule and return.
2402 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2403 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2404 csio->resid = resid;
2409 * Otherwise, we let the common code handle this.
2411 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2415 * We cannot depend upon CAM honoring retry counts for these.
2417 case CAM_SCSI_BUS_RESET:
2419 if (ccb->ccb_h.retry_count <= 0) {
2425 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2429 * Handle filemark, end of tape, mismatched record sizes....
2430 * From this point out, we're only handling read/write cases.
2431 * Handle writes && reads differently.
2434 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2435 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2436 csio->resid = resid;
2438 } else if (sense->flags & SSD_EOM) {
2439 softc->flags |= SA_FLAG_EOM_PENDING;
2441 * Grotesque as it seems, the few times
2442 * I've actually seen a non-zero resid,
2443 * the tape drive actually lied and had
2444 * writtent all the data!.
2449 csio->resid = resid;
2450 if (sense_key == SSD_KEY_BLANK_CHECK) {
2451 if (softc->quirks & SA_QUIRK_1FM) {
2453 softc->flags |= SA_FLAG_EOM_PENDING;
2457 } else if (sense->flags & SSD_FILEMARK) {
2458 if (softc->flags & SA_FLAG_FIXED) {
2460 softc->flags |= SA_FLAG_EOF_PENDING;
2463 * Unconditionally, if we detected a filemark on a read,
2464 * mark that we've run moved a file ahead.
2466 if (softc->fileno != (daddr_t) -1) {
2469 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2475 * Incorrect Length usually applies to read, but can apply to writes.
2477 if (error == 0 && (sense->flags & SSD_ILI)) {
2479 xpt_print_path(csio->ccb_h.path);
2480 printf(toobig, csio->dxfer_len - info);
2481 csio->resid = csio->dxfer_len;
2484 csio->resid = resid;
2485 if (softc->flags & SA_FLAG_FIXED) {
2486 softc->flags |= SA_FLAG_EIO_PENDING;
2489 * Bump the block number if we hadn't seen a filemark.
2490 * Do this independent of errors (we've moved anyway).
2492 if ((sense->flags & SSD_FILEMARK) == 0) {
2493 if (softc->blkno != (daddr_t) -1) {
2495 csio->ccb_h.ccb_pflags |=
2496 SA_POSITION_UPDATED;
2504 * Unfreeze the queue if frozen as we're not returning anything
2505 * to our waiters that would indicate an I/O error has occurred
2515 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2516 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2517 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2518 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2523 struct scsi_mode_header_6 *mode_hdr;
2524 struct scsi_mode_blk_desc *mode_blk;
2525 int mode_buffer_len;
2526 struct sa_softc *softc;
2531 softc = (struct sa_softc *)periph->softc;
2532 ccb = cam_periph_getccb(periph, 1);
2533 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2534 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2536 cpage = SA_DATA_COMPRESSION_PAGE;
2539 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2541 if (params_to_get & SA_PARAM_COMPRESSION) {
2542 if (softc->quirks & SA_QUIRK_NOCOMP) {
2543 *comp_supported = FALSE;
2544 params_to_get &= ~SA_PARAM_COMPRESSION;
2546 mode_buffer_len += sizeof (sa_comp_t);
2549 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_WAITOK);
2550 bzero(mode_buffer, mode_buffer_len);
2551 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2552 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2554 /* it is safe to retry this */
2555 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2556 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2557 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2558 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2560 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2561 &softc->device_stats);
2564 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2566 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2568 * Hmm. Let's see if we can try another page...
2569 * If we've already done that, give up on compression
2570 * for this device and remember this for the future
2571 * and attempt the request without asking for compression
2574 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2575 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2578 softc->quirks |= SA_QUIRK_NOCOMP;
2579 free(mode_buffer, M_TEMP);
2581 } else if (status == CAM_SCSI_STATUS_ERROR) {
2582 /* Tell the user about the fatal error. */
2583 scsi_sense_print(&ccb->csio);
2584 goto sagetparamsexit;
2588 * If the user only wants the compression information, and
2589 * the device doesn't send back the block descriptor, it's
2590 * no big deal. If the user wants more than just
2591 * compression, though, and the device doesn't pass back the
2592 * block descriptor, we need to send another mode sense to
2593 * get the block descriptor.
2595 if ((mode_hdr->blk_desc_len == 0) &&
2596 (params_to_get & SA_PARAM_COMPRESSION) &&
2597 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2600 * Decrease the mode buffer length by the size of
2601 * the compression page, to make sure the data
2602 * there doesn't get overwritten.
2604 mode_buffer_len -= sizeof (sa_comp_t);
2607 * Now move the compression page that we presumably
2608 * got back down the memory chunk a little bit so
2609 * it doesn't get spammed.
2611 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2612 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2615 * Now, we issue another mode sense and just ask
2616 * for the block descriptor, etc.
2619 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2620 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2621 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2624 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2625 &softc->device_stats);
2629 goto sagetparamsexit;
2632 if (params_to_get & SA_PARAM_BLOCKSIZE)
2633 *blocksize = scsi_3btoul(mode_blk->blklen);
2635 if (params_to_get & SA_PARAM_NUMBLOCKS)
2636 *numblocks = scsi_3btoul(mode_blk->nblocks);
2638 if (params_to_get & SA_PARAM_BUFF_MODE)
2639 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2641 if (params_to_get & SA_PARAM_DENSITY)
2642 *density = mode_blk->density;
2644 if (params_to_get & SA_PARAM_WP)
2645 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2647 if (params_to_get & SA_PARAM_SPEED)
2648 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2650 if (params_to_get & SA_PARAM_COMPRESSION) {
2651 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2652 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2653 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2655 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2657 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2658 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2660 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2662 * We don't really know whether this device supports
2663 * Data Compression if the the algorithm field is
2664 * zero. Just say we do.
2666 *comp_supported = TRUE;
2668 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2669 *comp_algorithm = cp->sel_comp_alg;
2672 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2675 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2677 char *xyz = mode_buffer;
2678 xpt_print_path(periph->path);
2679 printf("Mode Sense Data=");
2680 for (idx = 0; idx < mode_buffer_len; idx++)
2681 printf(" 0x%02x", xyz[idx] & 0xff);
2687 xpt_release_ccb(ccb);
2688 free(mode_buffer, M_TEMP);
2693 * The purpose of this function is to set one of four different parameters
2697 * - compression / compression algorithm
2700 * The assumption is that this will be called from saioctl(), and therefore
2701 * from a process context. Thus the waiting malloc calls below. If that
2702 * assumption ever changes, the malloc calls should be changed to be
2705 * Any or all of the four parameters may be set when this function is
2706 * called. It should handle setting more than one parameter at once.
2709 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2710 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2711 u_int32_t sense_flags)
2713 struct sa_softc *softc;
2714 u_int32_t current_blocksize;
2715 u_int32_t current_calg;
2716 u_int8_t current_density;
2717 u_int8_t current_speed;
2718 int comp_enabled, comp_supported;
2720 int mode_buffer_len;
2721 struct scsi_mode_header_6 *mode_hdr;
2722 struct scsi_mode_blk_desc *mode_blk;
2723 sa_comp_t *ccomp, *cpage;
2725 union ccb *ccb = NULL;
2728 softc = (struct sa_softc *)periph->softc;
2730 ccomp = malloc(sizeof (sa_comp_t), M_TEMP, M_WAITOK);
2733 * Since it doesn't make sense to set the number of blocks, or
2734 * write protection, we won't try to get the current value. We
2735 * always want to get the blocksize, so we can set it back to the
2738 error = sagetparams(periph,
2739 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2740 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2741 ¤t_speed, &comp_supported, &comp_enabled,
2742 ¤t_calg, ccomp);
2745 free(ccomp, M_TEMP);
2749 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2750 if (params_to_set & SA_PARAM_COMPRESSION)
2751 mode_buffer_len += sizeof (sa_comp_t);
2753 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_WAITOK);
2754 bzero(mode_buffer, mode_buffer_len);
2756 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2757 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2759 ccb = cam_periph_getccb(periph, 1);
2763 if (params_to_set & SA_PARAM_COMPRESSION) {
2765 cpage = (sa_comp_t *)&mode_blk[1];
2767 cpage = (sa_comp_t *)&mode_hdr[1];
2769 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2770 cpage->hdr.pagecode &= ~0x80;
2775 * If the caller wants us to set the blocksize, use the one they
2776 * pass in. Otherwise, use the blocksize we got back from the
2777 * mode select above.
2780 if (params_to_set & SA_PARAM_BLOCKSIZE)
2781 scsi_ulto3b(blocksize, mode_blk->blklen);
2783 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2786 * Set density if requested, else preserve old density.
2787 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2788 * devices, else density we've latched up in our softc.
2790 if (params_to_set & SA_PARAM_DENSITY) {
2791 mode_blk->density = density;
2792 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2793 mode_blk->density = SCSI_SAME_DENSITY;
2795 mode_blk->density = softc->media_density;
2800 * For mode selects, these two fields must be zero.
2802 mode_hdr->data_length = 0;
2803 mode_hdr->medium_type = 0;
2805 /* set the speed to the current value */
2806 mode_hdr->dev_spec = current_speed;
2808 /* set single-initiator buffering mode */
2809 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2812 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2814 mode_hdr->blk_desc_len = 0;
2817 * First, if the user wants us to set the compression algorithm or
2818 * just turn compression on, check to make sure that this drive
2819 * supports compression.
2821 if (params_to_set & SA_PARAM_COMPRESSION) {
2823 * If the compression algorithm is 0, disable compression.
2824 * If the compression algorithm is non-zero, enable
2825 * compression and set the compression type to the
2826 * specified compression algorithm, unless the algorithm is
2827 * MT_COMP_ENABLE. In that case, we look at the
2828 * compression algorithm that is currently set and if it is
2829 * non-zero, we leave it as-is. If it is zero, and we have
2830 * saved a compression algorithm from a time when
2831 * compression was enabled before, set the compression to
2834 switch (ccomp->hdr.pagecode & ~0x80) {
2835 case SA_DATA_COMPRESSION_PAGE:
2836 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2837 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2840 * Disable compression, but leave the
2841 * decompression and the capability bit
2844 dcp->dce_and_dcc = SA_DCP_DCC;
2845 dcp->dde_and_red |= SA_DCP_DDE;
2848 /* enable compression && decompression */
2849 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2850 dcp->dde_and_red |= SA_DCP_DDE;
2852 * If there, use compression algorithm from caller.
2853 * Otherwise, if there's a saved compression algorithm
2854 * and there is no current algorithm, use the saved
2855 * algorithm. Else parrot back what we got and hope
2858 if (calg != MT_COMP_ENABLE) {
2859 scsi_ulto4b(calg, dcp->comp_algorithm);
2860 scsi_ulto4b(calg, dcp->decomp_algorithm);
2861 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2862 softc->saved_comp_algorithm != 0) {
2863 scsi_ulto4b(softc->saved_comp_algorithm,
2864 dcp->comp_algorithm);
2865 scsi_ulto4b(softc->saved_comp_algorithm,
2866 dcp->decomp_algorithm);
2870 case SA_DEVICE_CONFIGURATION_PAGE:
2872 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2874 dcp->sel_comp_alg = SA_COMP_NONE;
2877 if (calg != MT_COMP_ENABLE) {
2878 dcp->sel_comp_alg = calg;
2879 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2880 softc->saved_comp_algorithm != 0) {
2881 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2887 * The drive doesn't seem to support compression,
2888 * so turn off the set compression bit.
2890 params_to_set &= ~SA_PARAM_COMPRESSION;
2891 xpt_print_path(periph->path);
2892 printf("device does not seem to support compression\n");
2895 * If that was the only thing the user wanted us to set,
2896 * clean up allocated resources and return with
2897 * 'operation not supported'.
2899 if (params_to_set == SA_PARAM_NONE) {
2900 free(mode_buffer, M_TEMP);
2901 xpt_release_ccb(ccb);
2906 * That wasn't the only thing the user wanted us to set.
2907 * So, decrease the stated mode buffer length by the
2908 * size of the compression mode page.
2910 mode_buffer_len -= sizeof(sa_comp_t);
2914 /* It is safe to retry this operation */
2915 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2916 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2917 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2919 error = cam_periph_runccb(ccb, saerror, 0,
2920 sense_flags, &softc->device_stats);
2923 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2925 char *xyz = mode_buffer;
2926 xpt_print_path(periph->path);
2927 printf("Err%d, Mode Select Data=", error);
2928 for (idx = 0; idx < mode_buffer_len; idx++)
2929 printf(" 0x%02x", xyz[idx] & 0xff);
2936 * If we can, try without setting density/blocksize.
2939 if ((params_to_set &
2940 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2945 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2946 cpage = (sa_comp_t *)&mode_blk[1];
2950 * If we were setting the blocksize, and that failed, we
2951 * want to set it to its original value. If we weren't
2952 * setting the blocksize, we don't want to change it.
2954 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2957 * Set density if requested, else preserve old density.
2958 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2959 * devices, else density we've latched up in our softc.
2961 if (params_to_set & SA_PARAM_DENSITY) {
2962 mode_blk->density = current_density;
2963 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2964 mode_blk->density = SCSI_SAME_DENSITY;
2966 mode_blk->density = softc->media_density;
2969 if (params_to_set & SA_PARAM_COMPRESSION)
2970 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2973 * The retry count is the only CCB field that might have been
2974 * changed that we care about, so reset it back to 1.
2976 ccb->ccb_h.retry_count = 1;
2977 cam_periph_runccb(ccb, saerror, 0, sense_flags,
2978 &softc->device_stats);
2982 xpt_release_ccb(ccb);
2985 free(ccomp, M_TEMP);
2987 if (params_to_set & SA_PARAM_COMPRESSION) {
2989 softc->flags &= ~SA_FLAG_COMP_ENABLED;
2991 * Even if we get an error setting compression,
2992 * do not say that we don't support it. We could
2993 * have been wrong, or it may be media specific.
2994 * softc->flags &= ~SA_FLAG_COMP_SUPP;
2996 softc->saved_comp_algorithm = softc->comp_algorithm;
2997 softc->comp_algorithm = 0;
2999 softc->flags |= SA_FLAG_COMP_ENABLED;
3000 softc->comp_algorithm = calg;
3004 free(mode_buffer, M_TEMP);
3009 saprevent(struct cam_periph *periph, int action)
3011 struct sa_softc *softc;
3015 softc = (struct sa_softc *)periph->softc;
3017 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3019 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3023 * We can be quiet about illegal requests.
3025 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3030 ccb = cam_periph_getccb(periph, 1);
3032 /* It is safe to retry this operation */
3033 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3034 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3036 error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats);
3039 if (action == PR_ALLOW)
3040 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3042 softc->flags |= SA_FLAG_TAPE_LOCKED;
3045 xpt_release_ccb(ccb);
3049 sarewind(struct cam_periph *periph)
3052 struct sa_softc *softc;
3055 softc = (struct sa_softc *)periph->softc;
3057 ccb = cam_periph_getccb(periph, 1);
3059 /* It is safe to retry this operation */
3060 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3061 SSD_FULL_SIZE, REWIND_TIMEOUT);
3063 softc->dsreg = MTIO_DSREG_REW;
3064 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3065 softc->dsreg = MTIO_DSREG_REST;
3067 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3068 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3070 xpt_release_ccb(ccb);
3072 softc->fileno = softc->blkno = (daddr_t) 0;
3074 softc->fileno = softc->blkno = (daddr_t) -1;
3079 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3082 struct sa_softc *softc;
3085 softc = (struct sa_softc *)periph->softc;
3087 ccb = cam_periph_getccb(periph, 1);
3089 /* This cannot be retried */
3091 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3092 SSD_FULL_SIZE, SPACE_TIMEOUT);
3095 * Clear residual because we will be using it.
3097 softc->last_ctl_resid = 0;
3099 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3100 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3101 softc->dsreg = MTIO_DSREG_REST;
3103 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3104 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3106 xpt_release_ccb(ccb);
3109 * If a spacing operation has failed, we need to invalidate
3112 * If the spacing operation was setmarks or to end of recorded data,
3113 * we no longer know our relative position.
3115 * If the spacing operations was spacing files in reverse, we
3116 * take account of the residual, but still check against less
3117 * than zero- if we've gone negative, we must have hit BOT.
3119 * If the spacing operations was spacing records in reverse and
3120 * we have a residual, we've either hit BOT or hit a filemark.
3121 * In the former case, we know our new record number (0). In
3122 * the latter case, we have absolutely no idea what the real
3123 * record number is- we've stopped between the end of the last
3124 * record in the previous file and the filemark that stopped
3125 * our spacing backwards.
3128 softc->fileno = softc->blkno = (daddr_t) -1;
3129 } else if (code == SS_SETMARKS || code == SS_EOD) {
3130 softc->fileno = softc->blkno = (daddr_t) -1;
3131 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3132 softc->fileno += (count - softc->last_ctl_resid);
3133 if (softc->fileno < 0) /* we must of hit BOT */
3136 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3137 softc->blkno += (count - softc->last_ctl_resid);
3139 if (softc->last_ctl_resid || softc->blkno < 0) {
3140 if (softc->fileno == 0) {
3143 softc->blkno = (daddr_t) -1;
3152 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3155 struct sa_softc *softc;
3158 softc = (struct sa_softc *)periph->softc;
3160 ccb = cam_periph_getccb(periph, 1);
3162 * Clear residual because we will be using it.
3164 softc->last_ctl_resid = 0;
3166 softc->dsreg = MTIO_DSREG_FMK;
3167 /* this *must* not be retried */
3168 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3169 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3170 softc->dsreg = MTIO_DSREG_REST;
3173 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3175 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3176 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3178 if (error == 0 && nmarks) {
3179 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3180 nwm = nmarks - softc->last_ctl_resid;
3181 softc->filemarks += nwm;
3184 xpt_release_ccb(ccb);
3187 * Update relative positions (if we're doing that).
3190 softc->fileno = softc->blkno = (daddr_t) -1;
3191 } else if (softc->fileno != (daddr_t) -1) {
3192 softc->fileno += nwm;
3199 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3201 struct scsi_tape_position_data loc;
3203 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3207 * We try and flush any buffered writes here if we were writing
3208 * and we're trying to get hardware block position. It eats
3209 * up performance substantially, but I'm wary of drive firmware.
3211 * I think that *logical* block position is probably okay-
3212 * but hardware block position might have to wait for data
3213 * to hit media to be valid. Caveat Emptor.
3216 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3217 error = sawritefilemarks(periph, 0, 0);
3218 if (error && error != EACCES)
3222 ccb = cam_periph_getccb(periph, 1);
3223 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3224 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3225 softc->dsreg = MTIO_DSREG_RBSY;
3226 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3227 softc->dsreg = MTIO_DSREG_REST;
3228 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3229 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3232 if (loc.flags & SA_RPOS_UNCERTAIN) {
3233 error = EINVAL; /* nothing is certain */
3235 *blkptr = scsi_4btoul(loc.firstblk);
3239 xpt_release_ccb(ccb);
3244 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3247 struct sa_softc *softc;
3251 * We used to try and flush any buffered writes here.
3252 * Now we push this onto user applications to either
3253 * flush the pending writes themselves (via a zero count
3254 * WRITE FILEMARKS command) or they can trust their tape
3255 * drive to do this correctly for them.
3258 softc = (struct sa_softc *)periph->softc;
3259 ccb = cam_periph_getccb(periph, 1);
3262 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3263 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3266 softc->dsreg = MTIO_DSREG_POS;
3267 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3268 softc->dsreg = MTIO_DSREG_REST;
3269 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3270 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3271 xpt_release_ccb(ccb);
3273 * Note relative file && block number position as now unknown.
3275 softc->fileno = softc->blkno = (daddr_t) -1;
3280 saretension(struct cam_periph *periph)
3283 struct sa_softc *softc;
3286 softc = (struct sa_softc *)periph->softc;
3288 ccb = cam_periph_getccb(periph, 1);
3290 /* It is safe to retry this operation */
3291 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3292 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3294 softc->dsreg = MTIO_DSREG_TEN;
3295 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3296 softc->dsreg = MTIO_DSREG_REST;
3298 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3299 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3300 xpt_release_ccb(ccb);
3302 softc->fileno = softc->blkno = (daddr_t) 0;
3304 softc->fileno = softc->blkno = (daddr_t) -1;
3309 sareservereleaseunit(struct cam_periph *periph, int reserve)
3312 struct sa_softc *softc;
3315 softc = (struct sa_softc *)periph->softc;
3316 ccb = cam_periph_getccb(periph, 1);
3318 /* It is safe to retry this operation */
3319 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3320 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3321 softc->dsreg = MTIO_DSREG_RBSY;
3322 error = cam_periph_runccb(ccb, saerror, 0,
3323 SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats);
3324 softc->dsreg = MTIO_DSREG_REST;
3326 xpt_release_ccb(ccb);
3329 * If the error was Illegal Request, then the device doesn't support
3330 * RESERVE/RELEASE. This is not an error.
3332 if (error == EINVAL) {
3340 saloadunload(struct cam_periph *periph, int load)
3343 struct sa_softc *softc;
3346 softc = (struct sa_softc *)periph->softc;
3348 ccb = cam_periph_getccb(periph, 1);
3350 /* It is safe to retry this operation */
3351 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3352 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3354 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3355 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3356 softc->dsreg = MTIO_DSREG_REST;
3358 xpt_release_ccb(ccb);
3360 if (error || load == 0)
3361 softc->fileno = softc->blkno = (daddr_t) -1;
3362 else if (error == 0)
3363 softc->fileno = softc->blkno = (daddr_t) 0;
3368 saerase(struct cam_periph *periph, int longerase)
3372 struct sa_softc *softc;
3375 softc = (struct sa_softc *)periph->softc;
3377 ccb = cam_periph_getccb(periph, 1);
3379 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3380 SSD_FULL_SIZE, ERASE_TIMEOUT);
3382 softc->dsreg = MTIO_DSREG_ZER;
3383 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3384 softc->dsreg = MTIO_DSREG_REST;
3386 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3387 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3388 xpt_release_ccb(ccb);
3392 #endif /* _KERNEL */
3395 * Read tape block limits command.
3398 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3399 void (*cbfcnp)(struct cam_periph *, union ccb *),
3400 u_int8_t tag_action,
3401 struct scsi_read_block_limits_data *rlimit_buf,
3402 u_int8_t sense_len, u_int32_t timeout)
3404 struct scsi_read_block_limits *scsi_cmd;
3406 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3407 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3408 sizeof(*scsi_cmd), timeout);
3410 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3411 bzero(scsi_cmd, sizeof(*scsi_cmd));
3412 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3416 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3417 void (*cbfcnp)(struct cam_periph *, union ccb *),
3418 u_int8_t tag_action, int readop, int sli,
3419 int fixed, u_int32_t length, u_int8_t *data_ptr,
3420 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3422 struct scsi_sa_rw *scsi_cmd;
3424 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3425 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3426 scsi_cmd->sli_fixed = 0;
3428 scsi_cmd->sli_fixed |= SAR_SLI;
3430 scsi_cmd->sli_fixed |= SARW_FIXED;
3431 scsi_ulto3b(length, scsi_cmd->length);
3432 scsi_cmd->control = 0;
3434 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3435 tag_action, data_ptr, dxfer_len, sense_len,
3436 sizeof(*scsi_cmd), timeout);
3440 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3441 void (*cbfcnp)(struct cam_periph *, union ccb *),
3442 u_int8_t tag_action, int immediate, int eot,
3443 int reten, int load, u_int8_t sense_len,
3446 struct scsi_load_unload *scsi_cmd;
3448 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3449 bzero(scsi_cmd, sizeof(*scsi_cmd));
3450 scsi_cmd->opcode = LOAD_UNLOAD;
3452 scsi_cmd->immediate = SLU_IMMED;
3454 scsi_cmd->eot_reten_load |= SLU_EOT;
3456 scsi_cmd->eot_reten_load |= SLU_RETEN;
3458 scsi_cmd->eot_reten_load |= SLU_LOAD;
3460 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3461 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3465 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3466 void (*cbfcnp)(struct cam_periph *, union ccb *),
3467 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3470 struct scsi_rewind *scsi_cmd;
3472 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3473 bzero(scsi_cmd, sizeof(*scsi_cmd));
3474 scsi_cmd->opcode = REWIND;
3476 scsi_cmd->immediate = SREW_IMMED;
3478 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3479 0, sense_len, sizeof(*scsi_cmd), timeout);
3483 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3484 void (*cbfcnp)(struct cam_periph *, union ccb *),
3485 u_int8_t tag_action, scsi_space_code code,
3486 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3488 struct scsi_space *scsi_cmd;
3490 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3491 scsi_cmd->opcode = SPACE;
3492 scsi_cmd->code = code;
3493 scsi_ulto3b(count, scsi_cmd->count);
3494 scsi_cmd->control = 0;
3496 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3497 0, sense_len, sizeof(*scsi_cmd), timeout);
3501 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3502 void (*cbfcnp)(struct cam_periph *, union ccb *),
3503 u_int8_t tag_action, int immediate, int setmark,
3504 u_int32_t num_marks, u_int8_t sense_len,
3507 struct scsi_write_filemarks *scsi_cmd;
3509 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3510 bzero(scsi_cmd, sizeof(*scsi_cmd));
3511 scsi_cmd->opcode = WRITE_FILEMARKS;
3513 scsi_cmd->byte2 |= SWFMRK_IMMED;
3515 scsi_cmd->byte2 |= SWFMRK_WSMK;
3517 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3519 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3520 0, sense_len, sizeof(*scsi_cmd), timeout);
3524 * The reserve and release unit commands differ only by their opcodes.
3527 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3528 void (*cbfcnp)(struct cam_periph *, union ccb *),
3529 u_int8_t tag_action, int third_party,
3530 int third_party_id, u_int8_t sense_len,
3531 u_int32_t timeout, int reserve)
3533 struct scsi_reserve_release_unit *scsi_cmd;
3535 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3536 bzero(scsi_cmd, sizeof(*scsi_cmd));
3539 scsi_cmd->opcode = RESERVE_UNIT;
3541 scsi_cmd->opcode = RELEASE_UNIT;
3544 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3545 scsi_cmd->lun_thirdparty |=
3546 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3549 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3550 0, sense_len, sizeof(*scsi_cmd), timeout);
3554 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3555 void (*cbfcnp)(struct cam_periph *, union ccb *),
3556 u_int8_t tag_action, int immediate, int long_erase,
3557 u_int8_t sense_len, u_int32_t timeout)
3559 struct scsi_erase *scsi_cmd;
3561 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3562 bzero(scsi_cmd, sizeof(*scsi_cmd));
3564 scsi_cmd->opcode = ERASE;
3567 scsi_cmd->lun_imm_long |= SE_IMMED;
3570 scsi_cmd->lun_imm_long |= SE_LONG;
3572 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3573 0, sense_len, sizeof(*scsi_cmd), timeout);
3577 * Read Tape Position command.
3580 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3581 void (*cbfcnp)(struct cam_periph *, union ccb *),
3582 u_int8_t tag_action, int hardsoft,
3583 struct scsi_tape_position_data *sbp,
3584 u_int8_t sense_len, u_int32_t timeout)
3586 struct scsi_tape_read_position *scmd;
3588 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3589 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3590 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3591 bzero(scmd, sizeof(*scmd));
3592 scmd->opcode = READ_POSITION;
3593 scmd->byte1 = hardsoft;
3597 * Set Tape Position command.
3600 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3601 void (*cbfcnp)(struct cam_periph *, union ccb *),
3602 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3603 u_int8_t sense_len, u_int32_t timeout)
3605 struct scsi_tape_locate *scmd;
3607 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3608 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3609 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3610 bzero(scmd, sizeof(*scmd));
3611 scmd->opcode = LOCATE;
3613 scmd->byte1 |= SA_SPOS_BT;
3614 scsi_ulto4b(blkno, scmd->blkaddr);