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>
41 #include <sys/malloc.h>
48 #include <sys/thread2.h>
50 #include <sys/fcntl.h>
51 #include <sys/devicestat.h>
52 #include <machine/limits.h>
60 #include "../cam_ccb.h"
61 #include "../cam_extend.h"
62 #include "../cam_periph.h"
63 #include "../cam_xpt_periph.h"
64 #include "../cam_debug.h"
67 #include "scsi_message.h"
75 #define SA_IO_TIMEOUT 4
77 #ifndef SA_SPACE_TIMEOUT
78 #define SA_SPACE_TIMEOUT 1 * 60
80 #ifndef SA_REWIND_TIMEOUT
81 #define SA_REWIND_TIMEOUT 2 * 60
83 #ifndef SA_ERASE_TIMEOUT
84 #define SA_ERASE_TIMEOUT 4 * 60
87 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
89 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
90 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
91 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
92 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
95 * Additional options that can be set for config: SA_1FM_AT_EOT
99 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
100 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
106 MALLOC_DEFINE(M_SCSISA, "SCSI sa", "SCSI sequential access buffers");
109 SA_STATE_NORMAL, SA_STATE_ABNORMAL
112 #define ccb_pflags ppriv_field0
113 #define ccb_bio ppriv_ptr1
115 #define SA_CCB_BUFFER_IO 0x0
116 #define SA_CCB_WAITING 0x1
117 #define SA_CCB_TYPEMASK 0x1
118 #define SA_POSITION_UPDATED 0x2
120 #define Set_CCB_Type(x, type) \
121 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
122 x->ccb_h.ccb_pflags |= type
124 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
129 SA_FLAG_OPEN = 0x0001,
130 SA_FLAG_FIXED = 0x0002,
131 SA_FLAG_TAPE_LOCKED = 0x0004,
132 SA_FLAG_TAPE_MOUNTED = 0x0008,
133 SA_FLAG_TAPE_WP = 0x0010,
134 SA_FLAG_TAPE_WRITTEN = 0x0020,
135 SA_FLAG_EOM_PENDING = 0x0040,
136 SA_FLAG_EIO_PENDING = 0x0080,
137 SA_FLAG_EOF_PENDING = 0x0100,
138 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
139 SA_FLAG_EOF_PENDING),
140 SA_FLAG_INVALID = 0x0200,
141 SA_FLAG_COMP_ENABLED = 0x0400,
142 SA_FLAG_COMP_SUPP = 0x0800,
143 SA_FLAG_COMP_UNSUPP = 0x1000,
144 SA_FLAG_TAPE_FROZEN = 0x2000
148 SA_MODE_REWIND = 0x00,
149 SA_MODE_NOREWIND = 0x01,
150 SA_MODE_OFFLINE = 0x02
154 SA_PARAM_NONE = 0x00,
155 SA_PARAM_BLOCKSIZE = 0x01,
156 SA_PARAM_DENSITY = 0x02,
157 SA_PARAM_COMPRESSION = 0x04,
158 SA_PARAM_BUFF_MODE = 0x08,
159 SA_PARAM_NUMBLOCKS = 0x10,
161 SA_PARAM_SPEED = 0x40,
166 SA_QUIRK_NONE = 0x00,
167 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
168 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
169 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
170 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
171 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
172 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
173 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
174 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
177 /* units are bits 4-7, 16-21 (1024 units) */
178 #define SAUNIT(DEV) \
179 (((minor(DEV) & 0xF0) >> 4) | ((minor(DEV) & 0x3f0000) >> 16))
181 #define SAMODE(z) ((minor(z) & 0x3))
182 #define SADENSITY(z) (((minor(z) >> 2) & 0x3))
183 #define SA_IS_CTRL(z) (minor(z) & (1 << 29))
185 #define SA_NOT_CTLDEV 0
189 #define SA_ATYPE_NR 1
190 #define SA_ATYPE_ER 2
192 #define SAMINOR(ctl, unit, mode, access) \
193 ((ctl << 29) | ((unit & 0x3f0) << 16) | ((unit & 0xf) << 4) | \
194 (mode << 0x2) | (access & 0x3))
196 #define SA_UNITMASK SAMINOR(0, -1, 0, 0)
197 #define SA_UNIT(unit) SAMINOR(0, unit, 0, 0)
199 #define SA_NUM_MODES 4
205 struct bio_queue_head bio_queue;
207 struct devstat device_stats;
213 u_int32_t comp_algorithm;
214 u_int32_t saved_comp_algorithm;
215 u_int32_t media_blksize;
216 u_int32_t last_media_blksize;
217 u_int32_t media_numblks;
218 u_int8_t media_density;
221 u_int8_t dsreg; /* mtio mt_dsreg, redux */
225 int last_resid_was_io;
228 * Relative to BOT Location.
237 struct scsi_sense_data _last_io_sense;
238 u_int32_t _last_io_resid;
239 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
240 struct scsi_sense_data _last_ctl_sense;
241 u_int32_t _last_ctl_resid;
242 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
243 #define last_io_sense errinfo._last_io_sense
244 #define last_io_resid errinfo._last_io_resid
245 #define last_io_cdb errinfo._last_io_cdb
246 #define last_ctl_sense errinfo._last_ctl_sense
247 #define last_ctl_resid errinfo._last_ctl_resid
248 #define last_ctl_cdb errinfo._last_ctl_cdb
251 * Misc other flags/state
255 open_rdonly : 1, /* open read-only */
256 open_pending_mount : 1, /* open pending mount */
257 ctrl_mode : 1; /* control device open */
260 struct sa_quirk_entry {
261 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
262 sa_quirks quirks; /* specific quirk type */
263 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
266 static struct sa_quirk_entry sa_quirk_table[] =
269 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
270 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
271 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
274 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
275 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
278 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
279 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
282 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
283 "Python*", "*"}, SA_QUIRK_NODREAD, 0
286 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
287 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
290 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
291 "VIPER 2525 25462", "-011"},
292 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
295 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
296 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
300 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
301 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
305 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
306 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
309 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
310 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
313 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
314 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
317 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
318 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
321 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
322 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
325 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
326 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
328 { /* jreynold@primenet.com */
329 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
330 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
332 { /* mike@sentex.net */
333 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
334 "STT20000*", "*"}, SA_QUIRK_1FM, 0
337 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
338 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
341 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
342 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
345 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
346 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
349 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
350 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
353 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
354 " SLR*", "*"}, SA_QUIRK_1FM, 0
357 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
358 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
361 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
362 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
366 static d_open_t saopen;
367 static d_close_t saclose;
368 static d_strategy_t sastrategy;
369 static d_ioctl_t saioctl;
370 static periph_init_t sainit;
371 static periph_ctor_t saregister;
372 static periph_oninv_t saoninvalidate;
373 static periph_dtor_t sacleanup;
374 static periph_start_t sastart;
375 static void saasync(void *callback_arg, u_int32_t code,
376 struct cam_path *path, void *arg);
377 static void sadone(struct cam_periph *periph,
378 union ccb *start_ccb);
379 static int saerror(union ccb *ccb, u_int32_t cam_flags,
380 u_int32_t sense_flags);
381 static int samarkswanted(struct cam_periph *);
382 static int sacheckeod(struct cam_periph *periph);
383 static int sagetparams(struct cam_periph *periph,
384 sa_params params_to_get,
385 u_int32_t *blocksize, u_int8_t *density,
386 u_int32_t *numblocks, int *buff_mode,
387 u_int8_t *write_protect, u_int8_t *speed,
388 int *comp_supported, int *comp_enabled,
389 u_int32_t *comp_algorithm,
390 sa_comp_t *comp_page);
391 static int sasetparams(struct cam_periph *periph,
392 sa_params params_to_set,
393 u_int32_t blocksize, u_int8_t density,
394 u_int32_t comp_algorithm,
395 u_int32_t sense_flags);
396 static void saprevent(struct cam_periph *periph, int action);
397 static int sarewind(struct cam_periph *periph);
398 static int saspace(struct cam_periph *periph, int count,
399 scsi_space_code code);
400 static int samount(struct cam_periph *, int, cdev_t);
401 static int saretension(struct cam_periph *periph);
402 static int sareservereleaseunit(struct cam_periph *periph,
404 static int saloadunload(struct cam_periph *periph, int load);
405 static int saerase(struct cam_periph *periph, int longerase);
406 static int sawritefilemarks(struct cam_periph *periph,
407 int nmarks, int setmarks);
408 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
409 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
412 static struct periph_driver sadriver =
415 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
418 PERIPHDRIVER_DECLARE(sa, sadriver);
420 /* For 2.2-stable support */
425 static struct dev_ops sa_ops = {
430 .d_write = physwrite,
432 .d_strategy = sastrategy,
435 static struct extend_array *saperiphs;
438 saopen(struct dev_open_args *ap)
440 cdev_t dev = ap->a_head.a_dev;
441 struct cam_periph *periph;
442 struct sa_softc *softc;
448 periph = cam_extend_get(saperiphs, unit);
449 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
453 cam_periph_lock(periph);
455 softc = (struct sa_softc *)periph->softc;
457 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
458 ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
460 if (SA_IS_CTRL(dev)) {
461 softc->ctrl_mode = 1;
462 cam_periph_unlock(periph);
466 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
467 cam_periph_unlock(periph);
468 cam_periph_release(periph);
472 if (softc->flags & SA_FLAG_OPEN) {
474 } else if (softc->flags & SA_FLAG_INVALID) {
478 * Preserve whether this is a read_only open.
480 softc->open_rdonly = (ap->a_oflags & O_RDWR) == O_RDONLY;
483 * The function samount ensures media is loaded and ready.
484 * It also does a device RESERVE if the tape isn't yet mounted.
486 * If the mount fails and this was a non-blocking open,
487 * make this a 'open_pending_mount' action.
489 error = samount(periph, ap->a_oflags, dev);
490 if (error && (ap->a_oflags & O_NONBLOCK)) {
491 softc->flags |= SA_FLAG_OPEN;
492 softc->open_pending_mount = 1;
493 cam_periph_unhold(periph, 1);
499 cam_periph_unhold(periph, 1);
500 cam_periph_release(periph);
504 saprevent(periph, PR_PREVENT);
505 softc->flags |= SA_FLAG_OPEN;
507 cam_periph_unhold(periph, 1);
512 saclose(struct dev_close_args *ap)
514 cdev_t dev = ap->a_head.a_dev;
515 struct cam_periph *periph;
516 struct sa_softc *softc;
517 int unit, mode, error, writing, tmp;
518 int closedbits = SA_FLAG_OPEN;
522 periph = cam_extend_get(saperiphs, unit);
526 cam_periph_lock(periph);
528 softc = (struct sa_softc *)periph->softc;
530 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
531 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
534 softc->open_rdonly = 0;
535 if (SA_IS_CTRL(dev)) {
536 softc->ctrl_mode = 0;
537 cam_periph_unlock(periph);
538 cam_periph_release(periph);
542 if (softc->open_pending_mount) {
543 softc->flags &= ~SA_FLAG_OPEN;
544 softc->open_pending_mount = 0;
545 cam_periph_unlock(periph);
546 cam_periph_release(periph);
550 if ((error = cam_periph_hold(periph, 0)) != 0) {
551 cam_periph_unlock(periph);
556 * Were we writing the tape?
558 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
561 * See whether or not we need to write filemarks. If this
562 * fails, we probably have to assume we've lost tape
565 error = sacheckeod(periph);
567 xpt_print(periph->path,
568 "failed to write terminating filemark(s)\n");
569 softc->flags |= SA_FLAG_TAPE_FROZEN;
573 * Whatever we end up doing, allow users to eject tapes from here on.
575 saprevent(periph, PR_ALLOW);
578 * Decide how to end...
580 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
581 closedbits |= SA_FLAG_TAPE_FROZEN;
582 } else switch (mode) {
583 case SA_MODE_OFFLINE:
585 * An 'offline' close is an unconditional release of
586 * frozen && mount conditions, irrespective of whether
587 * these operations succeeded. The reason for this is
588 * to allow at least some kind of programmatic way
589 * around our state getting all fouled up. If somebody
590 * issues an 'offline' command, that will be allowed
594 saloadunload(periph, FALSE);
595 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
599 * If the rewind fails, return an error- if anyone cares,
600 * but not overwriting any previous error.
602 * We don't clear the notion of mounted here, but we do
603 * clear the notion of frozen if we successfully rewound.
605 tmp = sarewind(periph);
610 closedbits |= SA_FLAG_TAPE_FROZEN;
613 case SA_MODE_NOREWIND:
615 * If we're not rewinding/unloading the tape, find out
616 * whether we need to back up over one of two filemarks
617 * we wrote (if we wrote two filemarks) so that appends
618 * from this point on will be sane.
620 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
621 tmp = saspace(periph, -1, SS_FILEMARKS);
623 xpt_print(periph->path, "unable to backspace "
624 "over one of double filemarks at end of "
626 xpt_print(periph->path, "it is possible that "
627 "this device needs a SA_QUIRK_1FM quirk set"
629 softc->flags |= SA_FLAG_TAPE_FROZEN;
634 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode);
640 * We wish to note here that there are no more filemarks to be written.
642 softc->filemarks = 0;
643 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
646 * And we are no longer open for business.
648 softc->flags &= ~closedbits;
651 * Inform users if tape state if frozen....
653 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
654 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, "
655 "REWIND or MTEOM command to clear this state.\n");
658 /* release the device if it is no longer mounted */
659 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
660 sareservereleaseunit(periph, FALSE);
662 cam_periph_unhold(periph, 1);
663 cam_periph_release(periph);
669 * Actually translate the requested transfer into one the physical driver
670 * can understand. The transfer is described by a buf and will include
671 * only one physical transfer.
674 sastrategy(struct dev_strategy_args *ap)
676 cdev_t dev = ap->a_head.a_dev;
677 struct bio *bio = ap->a_bio;
678 struct buf *bp = bio->bio_buf;
679 struct cam_periph *periph;
680 struct sa_softc *softc;
683 if (SA_IS_CTRL(dev)) {
684 bp->b_error = EINVAL;
688 periph = cam_extend_get(saperiphs, unit);
689 if (periph == NULL) {
693 cam_periph_lock(periph);
695 softc = (struct sa_softc *)periph->softc;
697 if (softc->flags & SA_FLAG_INVALID) {
698 cam_periph_unlock(periph);
703 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
704 cam_periph_unlock(periph);
710 * This should actually never occur as the write(2)
711 * system call traps attempts to write to a read-only
714 if (bp->b_cmd == BUF_CMD_WRITE && softc->open_rdonly) {
715 cam_periph_unlock(periph);
720 if (softc->open_pending_mount) {
721 int error = samount(periph, 0, dev);
723 cam_periph_unlock(periph);
727 saprevent(periph, PR_PREVENT);
728 softc->open_pending_mount = 0;
732 * If it's a null transfer, return immediately
734 if (bp->b_bcount == 0) {
735 cam_periph_unlock(periph);
740 if (softc->flags & SA_FLAG_FIXED) {
742 * Fixed block device. The byte count must
743 * be a multiple of our block size.
745 if (((softc->blk_mask != ~0) &&
746 ((bp->b_bcount & softc->blk_mask) != 0)) ||
747 ((softc->blk_mask == ~0) &&
748 ((bp->b_bcount % softc->min_blk) != 0))) {
749 xpt_print(periph->path, "Invalid request. Fixed block "
750 "device requests must be a multiple of %d bytes\n",
752 cam_periph_unlock(periph);
753 bp->b_error = EINVAL;
756 } else if ((bp->b_bcount > softc->max_blk) ||
757 (bp->b_bcount < softc->min_blk) ||
758 (bp->b_bcount & softc->blk_mask) != 0) {
760 xpt_print_path(periph->path);
761 kprintf("Invalid request. Variable block "
762 "device requests must be ");
763 if (softc->blk_mask != 0) {
764 kprintf("a multiple of %d ", (0x1 << softc->blk_gran));
766 kprintf("between %d and %d bytes\n", softc->min_blk,
768 cam_periph_unlock(periph);
769 bp->b_error = EINVAL;
774 * Place it at the end of the queue.
776 bioq_insert_tail(&softc->bio_queue, bio);
777 softc->queue_count++;
779 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
780 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
781 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
782 (bp->bio_cmd == BIO_READ)? "read" : "write"));
784 if (softc->queue_count > 1) {
785 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
786 ("sastrategy: queue count now %d\n", softc->queue_count));
790 * Schedule ourselves for performing the work.
792 xpt_schedule(periph, 1);
793 cam_periph_unlock(periph);
797 bp->b_flags |= B_ERROR;
801 * Correctly set the buf to indicate a completed xfer
803 bp->b_resid = bp->b_bcount;
809 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
810 if (softc->open_pending_mount) { \
811 error = samount(periph, 0, dev); \
815 saprevent(periph, PR_PREVENT); \
816 softc->open_pending_mount = 0; \
820 saioctl(struct dev_ioctl_args *ap)
822 cdev_t dev = ap->a_head.a_dev;
823 caddr_t addr = ap->a_data;
824 struct cam_periph *periph;
825 struct sa_softc *softc;
826 scsi_space_code spaceop;
827 int didholdperiph = 0;
834 error = 0; /* shut up gcc */
835 spaceop = 0; /* shut up gcc */
837 periph = cam_extend_get(saperiphs, unit);
841 cam_periph_lock(periph);
842 softc = (struct sa_softc *)periph->softc;
845 * Check for control mode accesses. We allow MTIOCGET and
846 * MTIOCERRSTAT (but need to be the only one open in order
847 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
848 * and MTCOMP (but need to be the only one accessing this
849 * device to run those).
852 if (SA_IS_CTRL(dev)) {
854 case MTIOCGETEOTMODEL:
859 * If the periph isn't already locked, lock it
860 * so our MTIOCERRSTAT can reset latched error stats.
862 * If the periph is already locked, skip it because
863 * we're just getting status and it'll be up to the
864 * other thread that has this device open to do
865 * an MTIOCERRSTAT that would clear latched status.
867 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
868 error = cam_periph_hold(periph, PCATCH);
878 struct mtop *mt = (struct mtop *) addr;
881 * Check to make sure it's an OP we can perform
882 * with no media inserted.
898 case MTIOCSETEOTMODEL:
900 * We need to acquire the peripheral here rather
901 * than at open time because we are sharing writable
902 * access to data structures.
904 error = cam_periph_hold(periph, PCATCH);
917 * Find the device that the user is talking about
922 struct mtget *g = (struct mtget *)addr;
925 * If this isn't the control mode device, actually go out
926 * and ask the drive again what it's set to.
928 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) {
929 u_int8_t write_protect = 0; /* silence gcc */
930 int comp_enabled = 0; /* silence gcc */
931 int comp_supported = 0; /* silence gcc */
933 error = sagetparams(periph, SA_PARAM_ALL,
934 &softc->media_blksize, &softc->media_density,
935 &softc->media_numblks, &softc->buffer_mode,
936 &write_protect, &softc->speed, &comp_supported,
937 &comp_enabled, &softc->comp_algorithm, NULL);
941 softc->flags |= SA_FLAG_TAPE_WP;
943 softc->flags &= ~SA_FLAG_TAPE_WP;
944 softc->flags &= ~(SA_FLAG_COMP_SUPP|
945 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
946 if (comp_supported) {
947 if (softc->saved_comp_algorithm == 0)
948 softc->saved_comp_algorithm =
949 softc->comp_algorithm;
950 softc->flags |= SA_FLAG_COMP_SUPP;
952 softc->flags |= SA_FLAG_COMP_ENABLED;
954 softc->flags |= SA_FLAG_COMP_UNSUPP;
956 bzero(g, sizeof(struct mtget));
957 g->mt_type = MT_ISAR;
958 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
959 g->mt_comp = MT_COMP_UNSUPP;
960 g->mt_comp0 = MT_COMP_UNSUPP;
961 g->mt_comp1 = MT_COMP_UNSUPP;
962 g->mt_comp2 = MT_COMP_UNSUPP;
963 g->mt_comp3 = MT_COMP_UNSUPP;
965 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
966 g->mt_comp = MT_COMP_DISABLED;
968 g->mt_comp = softc->comp_algorithm;
970 g->mt_comp0 = softc->comp_algorithm;
971 g->mt_comp1 = softc->comp_algorithm;
972 g->mt_comp2 = softc->comp_algorithm;
973 g->mt_comp3 = softc->comp_algorithm;
975 g->mt_density = softc->media_density;
976 g->mt_density0 = softc->media_density;
977 g->mt_density1 = softc->media_density;
978 g->mt_density2 = softc->media_density;
979 g->mt_density3 = softc->media_density;
980 g->mt_blksiz = softc->media_blksize;
981 g->mt_blksiz0 = softc->media_blksize;
982 g->mt_blksiz1 = softc->media_blksize;
983 g->mt_blksiz2 = softc->media_blksize;
984 g->mt_blksiz3 = softc->media_blksize;
985 g->mt_fileno = softc->fileno;
986 g->mt_blkno = softc->blkno;
987 g->mt_dsreg = (short) softc->dsreg;
989 * Yes, we know that this is likely to overflow
991 if (softc->last_resid_was_io) {
992 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
993 if (SA_IS_CTRL(dev) == 0 || didholdperiph) {
994 softc->last_io_resid = 0;
998 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
999 if (SA_IS_CTRL(dev) == 0 || didholdperiph) {
1000 softc->last_ctl_resid = 0;
1009 struct scsi_tape_errors *sep =
1010 &((union mterrstat *)addr)->scsi_errstat;
1012 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1013 ("saioctl: MTIOCERRSTAT\n"));
1015 bzero(sep, sizeof(*sep));
1016 sep->io_resid = softc->last_io_resid;
1017 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1018 sizeof (sep->io_sense));
1019 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1020 sizeof (sep->io_cdb));
1021 sep->ctl_resid = softc->last_ctl_resid;
1022 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1023 sizeof (sep->ctl_sense));
1024 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1025 sizeof (sep->ctl_cdb));
1027 if ((SA_IS_CTRL(dev) == 0 && softc->open_pending_mount) ||
1029 bzero((caddr_t) &softc->errinfo,
1030 sizeof (softc->errinfo));
1039 PENDING_MOUNT_CHECK(softc, periph, dev);
1041 mt = (struct mtop *)addr;
1043 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1044 ("saioctl: op=0x%x count=0x%x\n",
1045 mt->mt_op, mt->mt_count));
1047 count = mt->mt_count;
1048 switch (mt->mt_op) {
1049 case MTWEOF: /* write an end-of-file marker */
1051 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1052 * flag because by keeping track of filemarks
1053 * we have last written we know ehether or not
1054 * we need to write more when we close the device.
1056 error = sawritefilemarks(periph, count, FALSE);
1058 case MTWSS: /* write a setmark */
1059 error = sawritefilemarks(periph, count, TRUE);
1061 case MTBSR: /* backward space record */
1062 case MTFSR: /* forward space record */
1063 case MTBSF: /* backward space file */
1064 case MTFSF: /* forward space file */
1065 case MTBSS: /* backward space setmark */
1066 case MTFSS: /* forward space setmark */
1067 case MTEOD: /* space to end of recorded medium */
1071 spaceop = SS_FILEMARKS;
1072 nmarks = softc->filemarks;
1073 error = sacheckeod(periph);
1075 xpt_print(periph->path,
1076 "EOD check prior to spacing failed\n");
1077 softc->flags |= SA_FLAG_EIO_PENDING;
1080 nmarks -= softc->filemarks;
1086 spaceop = SS_BLOCKS;
1097 spaceop = SS_SETMARKS;
1111 nmarks = softc->filemarks;
1113 * XXX: Why are we checking again?
1115 error = sacheckeod(periph);
1118 nmarks -= softc->filemarks;
1119 error = saspace(periph, count - nmarks, spaceop);
1121 * At this point, clear that we've written the tape
1122 * and that we've written any filemarks. We really
1123 * don't know what the applications wishes to do next-
1124 * the sacheckeod's will make sure we terminated the
1125 * tape correctly if we'd been writing, but the next
1126 * action the user application takes will set again
1127 * whether we need to write filemarks.
1130 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1131 softc->filemarks = 0;
1134 case MTREW: /* rewind */
1135 PENDING_MOUNT_CHECK(softc, periph, dev);
1137 error = sarewind(periph);
1140 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1141 softc->flags &= ~SA_FLAG_ERR_PENDING;
1142 softc->filemarks = 0;
1144 case MTERASE: /* erase */
1145 PENDING_MOUNT_CHECK(softc, periph, dev);
1146 error = saerase(periph, count);
1148 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1149 softc->flags &= ~SA_FLAG_ERR_PENDING;
1151 case MTRETENS: /* re-tension tape */
1152 PENDING_MOUNT_CHECK(softc, periph, dev);
1153 error = saretension(periph);
1155 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1156 softc->flags &= ~SA_FLAG_ERR_PENDING;
1158 case MTOFFL: /* rewind and put the drive offline */
1160 PENDING_MOUNT_CHECK(softc, periph, dev);
1164 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1165 softc->filemarks = 0;
1167 error = sarewind(periph);
1168 /* clear the frozen flag anyway */
1169 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1172 * Be sure to allow media removal before ejecting.
1175 saprevent(periph, PR_ALLOW);
1177 error = saloadunload(periph, FALSE);
1179 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1184 case MTNOP: /* no operation, sets status only */
1185 case MTCACHE: /* enable controller cache */
1186 case MTNOCACHE: /* disable controller cache */
1190 case MTSETBSIZ: /* Set block size for device */
1192 PENDING_MOUNT_CHECK(softc, periph, dev);
1194 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1197 softc->last_media_blksize =
1198 softc->media_blksize;
1199 softc->media_blksize = count;
1201 softc->flags |= SA_FLAG_FIXED;
1202 if (powerof2(count)) {
1205 softc->blk_mask = count - 1;
1207 softc->blk_mask = ~0;
1208 softc->blk_shift = 0;
1211 * Make the user's desire 'persistent'.
1213 softc->quirks &= ~SA_QUIRK_VARIABLE;
1214 softc->quirks |= SA_QUIRK_FIXED;
1216 softc->flags &= ~SA_FLAG_FIXED;
1217 if (softc->max_blk == 0) {
1218 softc->max_blk = ~0;
1220 softc->blk_shift = 0;
1221 if (softc->blk_gran != 0) {
1223 softc->blk_gran - 1;
1225 softc->blk_mask = 0;
1228 * Make the user's desire 'persistent'.
1230 softc->quirks |= SA_QUIRK_VARIABLE;
1231 softc->quirks &= ~SA_QUIRK_FIXED;
1235 case MTSETDNSTY: /* Set density for device and mode */
1236 PENDING_MOUNT_CHECK(softc, periph, dev);
1238 if (count > UCHAR_MAX) {
1242 error = sasetparams(periph, SA_PARAM_DENSITY,
1246 case MTCOMP: /* enable compression */
1247 PENDING_MOUNT_CHECK(softc, periph, dev);
1249 * Some devices don't support compression, and
1250 * don't like it if you ask them for the
1253 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1254 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1258 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1259 0, 0, count, SF_NO_PRINT);
1271 PENDING_MOUNT_CHECK(softc, periph, dev);
1272 error = sardpos(periph, 0, (u_int32_t *) addr);
1275 PENDING_MOUNT_CHECK(softc, periph, dev);
1276 error = sardpos(periph, 1, (u_int32_t *) addr);
1279 PENDING_MOUNT_CHECK(softc, periph, dev);
1280 error = sasetpos(periph, 0, (u_int32_t *) addr);
1283 PENDING_MOUNT_CHECK(softc, periph, dev);
1284 error = sasetpos(periph, 1, (u_int32_t *) addr);
1286 case MTIOCGETEOTMODEL:
1288 if (softc->quirks & SA_QUIRK_1FM)
1292 *((u_int32_t *) addr) = mode;
1294 case MTIOCSETEOTMODEL:
1296 switch (*((u_int32_t *) addr)) {
1298 softc->quirks &= ~SA_QUIRK_2FM;
1299 softc->quirks |= SA_QUIRK_1FM;
1302 softc->quirks &= ~SA_QUIRK_1FM;
1303 softc->quirks |= SA_QUIRK_2FM;
1311 error = cam_periph_ioctl(periph, ap->a_cmd, addr, saerror);
1316 * Check to see if we cleared a frozen state
1318 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1324 softc->fileno = (daddr_t) -1;
1325 softc->blkno = (daddr_t) -1;
1326 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1327 xpt_print(periph->path,
1328 "tape state now unfrozen.\n");
1335 cam_periph_unhold(periph, 1);
1337 cam_periph_unlock(periph);
1347 * Create our extend array for storing the devices we attach to.
1349 saperiphs = cam_extend_new();
1350 if (saperiphs == NULL) {
1351 kprintf("sa: Failed to alloc extend array!\n");
1356 * Install a global async callback.
1358 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL);
1360 if (status != CAM_REQ_CMP) {
1361 kprintf("sa: Failed to attach master async callback "
1362 "due to status 0x%x!\n", status);
1367 saoninvalidate(struct cam_periph *periph)
1369 struct sa_softc *softc;
1373 softc = (struct sa_softc *)periph->softc;
1376 * De-register any async callbacks.
1378 xpt_register_async(0, saasync, periph, periph->path);
1380 softc->flags |= SA_FLAG_INVALID;
1383 * Return all queued I/O with ENXIO.
1384 * XXX Handle any transactions queued to the card
1385 * with XPT_ABORT_CCB.
1387 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
1388 bioq_remove(&softc->bio_queue, q_bio);
1389 q_bp = q_bio->bio_buf;
1390 q_bp->b_resid = q_bp->b_bcount;
1391 q_bp->b_error = ENXIO;
1392 q_bp->b_flags |= B_ERROR;
1395 softc->queue_count = 0;
1397 xpt_print(periph->path, "lost device\n");
1402 sacleanup(struct cam_periph *periph)
1404 struct sa_softc *softc;
1406 softc = (struct sa_softc *)periph->softc;
1408 devstat_remove_entry(&softc->device_stats);
1410 cam_extend_release(saperiphs, periph->unit_number);
1411 xpt_print(periph->path, "removing device entry\n");
1412 dev_ops_remove_minor(&sa_ops, /*SA_UNITMASK,*/ SA_UNIT(periph->unit_number));
1413 kfree(softc, M_SCSISA);
1417 saasync(void *callback_arg, u_int32_t code,
1418 struct cam_path *path, void *arg)
1420 struct cam_periph *periph;
1422 periph = (struct cam_periph *)callback_arg;
1424 case AC_FOUND_DEVICE:
1426 struct ccb_getdev *cgd;
1429 cgd = (struct ccb_getdev *)arg;
1433 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1437 * Allocate a peripheral instance for
1438 * this device and start the probe
1441 status = cam_periph_alloc(saregister, saoninvalidate,
1443 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1444 saasync, AC_FOUND_DEVICE, cgd);
1446 if (status != CAM_REQ_CMP
1447 && status != CAM_REQ_INPROG)
1448 kprintf("saasync: Unable to probe new device "
1449 "due to status 0x%x\n", status);
1453 cam_periph_async(periph, code, path, arg);
1459 saregister(struct cam_periph *periph, void *arg)
1461 struct sa_softc *softc;
1462 struct ccb_getdev *cgd;
1466 cgd = (struct ccb_getdev *)arg;
1467 if (periph == NULL) {
1468 kprintf("saregister: periph was NULL!!\n");
1469 return (CAM_REQ_CMP_ERR);
1473 kprintf("saregister: no getdev CCB, can't register device\n");
1474 return (CAM_REQ_CMP_ERR);
1477 softc = kmalloc(sizeof (*softc), M_SCSISA, M_INTWAIT | M_ZERO);
1478 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1479 softc->state = SA_STATE_NORMAL;
1480 softc->fileno = (daddr_t) -1;
1481 softc->blkno = (daddr_t) -1;
1483 bioq_init(&softc->bio_queue);
1484 periph->softc = softc;
1485 cam_extend_set(saperiphs, periph->unit_number, periph);
1488 * See if this device has any quirks.
1490 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1491 (caddr_t)sa_quirk_table,
1492 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1493 sizeof(*sa_quirk_table), scsi_inquiry_match);
1495 if (match != NULL) {
1496 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1497 softc->last_media_blksize =
1498 ((struct sa_quirk_entry *)match)->prefblk;
1500 xpt_print(periph->path, "found quirk entry %d\n",
1501 (int) (((struct sa_quirk_entry *) match) - sa_quirk_table));
1504 softc->quirks = SA_QUIRK_NONE;
1507 * The SA driver supports a blocksize, but we don't know the
1508 * blocksize until we media is inserted. So, set a flag to
1509 * indicate that the blocksize is unavailable right now.
1511 cam_periph_unlock(periph);
1512 devstat_add_entry(&softc->device_stats, "sa", periph->unit_number, 0,
1513 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1514 DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE);
1516 make_dev(&sa_ops, SAMINOR(SA_CTLDEV,
1517 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1518 0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1520 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1521 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1522 0660, "%s%d", periph->periph_name, periph->unit_number);
1524 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1525 periph->unit_number, 0, SA_ATYPE_NR), UID_ROOT, GID_OPERATOR,
1526 0660, "n%s%d", periph->periph_name, periph->unit_number);
1528 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1529 periph->unit_number, 0, SA_ATYPE_ER), UID_ROOT, GID_OPERATOR,
1530 0660, "e%s%d", periph->periph_name, periph->unit_number);
1532 for (i = 0; i < SA_NUM_MODES; i++) {
1535 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R),
1536 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1537 periph->periph_name, periph->unit_number, i);
1540 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR),
1541 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1542 periph->periph_name, periph->unit_number, i);
1546 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER),
1547 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1548 periph->periph_name, periph->unit_number, i);
1550 cam_periph_lock(periph);
1553 * Add an async callback so that we get
1554 * notified if this device goes away.
1556 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path);
1558 xpt_announce_periph(periph, NULL);
1560 return (CAM_REQ_CMP);
1564 sastart(struct cam_periph *periph, union ccb *start_ccb)
1566 struct sa_softc *softc;
1568 softc = (struct sa_softc *)periph->softc;
1570 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
1573 switch (softc->state) {
1574 case SA_STATE_NORMAL:
1576 /* Pull a buffer from the queue and get going on it */
1581 * See if there is a buf with work for us to do..
1583 bio = bioq_first(&softc->bio_queue);
1584 if (periph->immediate_priority <= periph->pinfo.priority) {
1585 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1586 ("queuing for immediate ccb\n"));
1587 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1588 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1590 periph->immediate_priority = CAM_PRIORITY_NONE;
1591 wakeup(&periph->ccb_list);
1592 } else if (bio == NULL) {
1593 xpt_release_ccb(start_ccb);
1594 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1595 struct bio *done_bio;
1597 softc->queue_count--;
1598 bioq_remove(&softc->bio_queue, bio);
1600 bp->b_resid = bp->b_bcount;
1602 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1604 * We now just clear errors in this case
1605 * and let the residual be the notifier.
1608 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1610 * This can only happen if we're reading
1611 * in fixed length mode. In this case,
1612 * we dump the rest of the list the
1616 if (bioq_first(&softc->bio_queue) != NULL) {
1620 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1622 bp->b_flags |= B_ERROR;
1624 bio = bioq_first(&softc->bio_queue);
1626 * Only if we have no other buffers queued up
1627 * do we clear the pending error flag.
1630 softc->flags &= ~SA_FLAG_ERR_PENDING;
1631 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1632 ("sastart- ERR_PENDING now 0x%x, bio is %sNULL, "
1633 "%d more buffers queued up\n",
1634 (softc->flags & SA_FLAG_ERR_PENDING),
1635 (bio != NULL)? "not " : " ", softc->queue_count));
1636 xpt_release_ccb(start_ccb);
1641 bioq_remove(&softc->bio_queue, bio);
1643 softc->queue_count--;
1645 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1646 if (softc->blk_shift != 0) {
1648 bp->b_bcount >> softc->blk_shift;
1649 } else if (softc->media_blksize != 0) {
1651 bp->b_bcount / softc->media_blksize;
1654 xpt_print(periph->path, "zero blocksize"
1655 " for FIXED length writes?\n");
1660 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1661 ("issuing a %d fixed record %s\n",
1662 length, (bp->bio_cmd == BIO_READ)? "read" :
1666 length = bp->b_bcount;
1668 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1669 ("issuing a %d variable byte %s\n",
1670 length, (bp->bio_cmd == BIO_READ)? "read" :
1674 devstat_start_transaction(&softc->device_stats);
1676 * Some people have theorized that we should
1677 * suppress illegal length indication if we are
1678 * running in variable block mode so that we don't
1679 * have to request sense every time our requested
1680 * block size is larger than the written block.
1681 * The residual information from the ccb allows
1682 * us to identify this situation anyway. The only
1683 * problem with this is that we will not get
1684 * information about blocks that are larger than
1685 * our read buffer unless we set the block size
1686 * in the mode page to something other than 0.
1688 * I believe that this is a non-issue. If user apps
1689 * don't adjust their read size to match our record
1690 * size, that's just life. Anyway, the typical usage
1691 * would be to issue, e.g., 64KB reads and occasionally
1692 * have to do deal with 512 byte or 1KB intermediate
1695 softc->dsreg = (bp->b_cmd == BUF_CMD_READ) ?
1696 MTIO_DSREG_RD : MTIO_DSREG_WR;
1697 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1698 MSG_SIMPLE_Q_TAG, (bp->b_cmd == BUF_CMD_READ) != 0,
1699 FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1700 length, bp->b_data, bp->b_bcount, SSD_FULL_SIZE,
1702 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1703 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1704 start_ccb->ccb_h.ccb_bio = bio;
1705 bio = bioq_first(&softc->bio_queue);
1706 xpt_action(start_ccb);
1710 /* Have more work to do, so ensure we stay scheduled */
1711 xpt_schedule(periph, 1);
1715 case SA_STATE_ABNORMAL:
1717 panic("state 0x%x in sastart", softc->state);
1724 sadone(struct cam_periph *periph, union ccb *done_ccb)
1726 struct sa_softc *softc;
1727 struct ccb_scsiio *csio;
1729 softc = (struct sa_softc *)periph->softc;
1730 csio = &done_ccb->csio;
1731 switch (CCB_Type(csio)) {
1732 case SA_CCB_BUFFER_IO:
1738 softc->dsreg = MTIO_DSREG_REST;
1739 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1742 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1743 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1745 * A retry was scheduled, so just return.
1756 * Catastrophic error. Mark the tape as frozen
1757 * (we no longer know tape position).
1759 * Return all queued I/O with EIO, and unfreeze
1760 * our queue so that future transactions that
1761 * attempt to fix this problem can get to the
1766 softc->flags |= SA_FLAG_TAPE_FROZEN;
1767 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) {
1768 bioq_remove(&softc->bio_queue, q_bio);
1769 q_bp = q_bio->bio_buf;
1770 q_bp->b_resid = q_bp->b_bcount;
1771 q_bp->b_error = EIO;
1772 q_bp->b_flags |= B_ERROR;
1777 bp->b_resid = bp->b_bcount;
1778 bp->b_error = error;
1779 bp->b_flags |= B_ERROR;
1781 * In the error case, position is updated in saerror.
1784 bp->b_resid = csio->resid;
1786 if (csio->resid != 0) {
1787 bp->b_flags |= B_ERROR;
1789 if (bp->b_cmd != BUF_CMD_READ) {
1790 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1791 softc->filemarks = 0;
1793 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1794 (softc->blkno != (daddr_t) -1)) {
1795 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1797 if (softc->blk_shift != 0) {
1802 softc->media_blksize;
1804 softc->blkno += (daddr_t) l;
1811 * If we had an error (immediate or pending),
1812 * release the device queue now.
1814 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1815 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1817 if (error || bp->b_resid) {
1818 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1819 ("error %d resid %d count %d\n", error,
1820 bp->b_resid, bp->b_bcount));
1823 devstat_end_transaction_buf(&softc->device_stats, bp);
1827 case SA_CCB_WAITING:
1829 /* Caller will release the CCB */
1830 wakeup(&done_ccb->ccb_h.cbfcnp);
1834 xpt_release_ccb(done_ccb);
1838 * Mount the tape (make sure it's ready for I/O).
1840 * oflags can be checked for 'kind' of open (read-only check) - later
1841 * dev can be checked for a control-mode or compression open - later
1844 samount(struct cam_periph *periph, __unused int oflags, __unused cdev_t dev)
1846 struct sa_softc *softc;
1850 softc = (struct sa_softc *)periph->softc;
1853 * This should determine if something has happend since the last
1854 * open/mount that would invalidate the mount. We do *not* want
1855 * to retry this command- we just want the status. But we only
1856 * do this if we're mounted already- if we're not mounted,
1857 * we don't care about the unit read state and can instead use
1858 * this opportunity to attempt to reserve the tape unit.
1861 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1862 ccb = cam_periph_getccb(periph, 1);
1863 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1864 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1865 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1866 &softc->device_stats);
1868 if (error == ENXIO) {
1869 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1870 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1871 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1872 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1873 &softc->device_stats);
1877 * We don't need to freeze the tape because we
1878 * will now attempt to rewind/load it.
1880 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1881 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
1882 xpt_print(periph->path,
1883 "error %d on TUR in samount\n", error);
1887 error = sareservereleaseunit(periph, TRUE);
1891 ccb = cam_periph_getccb(periph, 1);
1892 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1893 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1894 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1895 &softc->device_stats);
1899 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1900 struct scsi_read_block_limits_data *rblim = NULL;
1901 int comp_enabled = 0; /* silence gcc */
1902 int comp_supported = 0; /* silence gcc */
1903 u_int8_t write_protect = 0; /* silence gcc */
1904 u_int8_t guessing = 0;
1907 * Clear out old state.
1909 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1910 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1911 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1912 softc->filemarks = 0;
1915 * *Very* first off, make sure we're loaded to BOT.
1917 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1918 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1919 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1920 &softc->device_stats);
1924 * In case this doesn't work, do a REWIND instead
1927 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1928 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1929 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1930 &softc->device_stats);
1934 xpt_release_ccb(ccb);
1939 * Do a dummy test read to force access to the
1940 * media so that the drive will really know what's
1941 * there. We actually don't really care what the
1942 * blocksize on tape is and don't expect to really
1943 * read a full record.
1945 rblim = kmalloc(8192, M_SCSISA, M_INTWAIT);
1947 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1948 scsi_sa_read_write(&ccb->csio, 0, sadone,
1949 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1950 (void *) rblim, 8192, SSD_FULL_SIZE,
1952 cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1953 &softc->device_stats);
1955 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1956 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1957 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1958 SF_NO_PRINT | SF_RETRY_UA,
1959 &softc->device_stats);
1962 xpt_print(periph->path,
1963 "unable to rewind after test read\n");
1964 xpt_release_ccb(ccb);
1970 * Next off, determine block limits.
1972 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1973 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1975 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1976 SF_NO_PRINT | SF_RETRY_UA, &softc->device_stats);
1979 xpt_release_ccb(ccb);
1983 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1984 * a MANDATORY command. Anyway- it doesn't matter-
1985 * we can proceed anyway.
1987 softc->blk_gran = 0;
1988 softc->max_blk = ~0;
1991 if (softc->scsi_rev >= SCSI_REV_SPC) {
1992 softc->blk_gran = RBL_GRAN(rblim);
1994 softc->blk_gran = 0;
1997 * We take max_blk == min_blk to mean a default to
1998 * fixed mode- but note that whatever we get out of
1999 * sagetparams below will actually determine whether
2000 * we are actually *in* fixed mode.
2002 softc->max_blk = scsi_3btoul(rblim->maximum);
2003 softc->min_blk = scsi_2btoul(rblim->minimum);
2008 * Next, perform a mode sense to determine
2009 * current density, blocksize, compression etc.
2011 error = sagetparams(periph, SA_PARAM_ALL,
2012 &softc->media_blksize,
2013 &softc->media_density,
2014 &softc->media_numblks,
2015 &softc->buffer_mode, &write_protect,
2016 &softc->speed, &comp_supported,
2017 &comp_enabled, &softc->comp_algorithm,
2022 * We could work a little harder here. We could
2023 * adjust our attempts to get information. It
2024 * might be an ancient tape drive. If someone
2025 * nudges us, we'll do that.
2031 * If no quirk has determined that this is a device that is
2032 * preferred to be in fixed or variable mode, now is the time
2035 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2038 * This could be expensive to find out. Luckily we
2039 * only need to do this once. If we start out in
2040 * 'default' mode, try and set ourselves to one
2041 * of the densities that would determine a wad
2042 * of other stuff. Go from highest to lowest.
2044 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2046 static u_int8_t ctry[] = {
2047 SCSI_DENSITY_HALFINCH_PE,
2048 SCSI_DENSITY_HALFINCH_6250C,
2049 SCSI_DENSITY_HALFINCH_6250,
2050 SCSI_DENSITY_HALFINCH_1600,
2051 SCSI_DENSITY_HALFINCH_800,
2052 SCSI_DENSITY_QIC_4GB,
2053 SCSI_DENSITY_QIC_2GB,
2054 SCSI_DENSITY_QIC_525_320,
2055 SCSI_DENSITY_QIC_150,
2056 SCSI_DENSITY_QIC_120,
2057 SCSI_DENSITY_QIC_24,
2058 SCSI_DENSITY_QIC_11_9TRK,
2059 SCSI_DENSITY_QIC_11_4TRK,
2060 SCSI_DENSITY_QIC_1320,
2061 SCSI_DENSITY_QIC_3080,
2064 for (i = 0; ctry[i]; i++) {
2065 error = sasetparams(periph,
2066 SA_PARAM_DENSITY, 0, ctry[i],
2069 softc->media_density = ctry[i];
2074 switch (softc->media_density) {
2075 case SCSI_DENSITY_QIC_11_4TRK:
2076 case SCSI_DENSITY_QIC_11_9TRK:
2077 case SCSI_DENSITY_QIC_24:
2078 case SCSI_DENSITY_QIC_120:
2079 case SCSI_DENSITY_QIC_150:
2080 case SCSI_DENSITY_QIC_525_320:
2081 case SCSI_DENSITY_QIC_1320:
2082 case SCSI_DENSITY_QIC_3080:
2083 softc->quirks &= ~SA_QUIRK_2FM;
2084 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2085 softc->last_media_blksize = 512;
2087 case SCSI_DENSITY_QIC_4GB:
2088 case SCSI_DENSITY_QIC_2GB:
2089 softc->quirks &= ~SA_QUIRK_2FM;
2090 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2091 softc->last_media_blksize = 1024;
2094 softc->last_media_blksize =
2095 softc->media_blksize;
2096 softc->quirks |= SA_QUIRK_VARIABLE;
2102 * If no quirk has determined that this is a device that needs
2103 * to have 2 Filemarks at EOD, now is the time to find out.
2106 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2107 switch (softc->media_density) {
2108 case SCSI_DENSITY_HALFINCH_800:
2109 case SCSI_DENSITY_HALFINCH_1600:
2110 case SCSI_DENSITY_HALFINCH_6250:
2111 case SCSI_DENSITY_HALFINCH_6250C:
2112 case SCSI_DENSITY_HALFINCH_PE:
2113 softc->quirks &= ~SA_QUIRK_1FM;
2114 softc->quirks |= SA_QUIRK_2FM;
2122 * Now validate that some info we got makes sense.
2124 if ((softc->max_blk < softc->media_blksize) ||
2125 (softc->min_blk > softc->media_blksize &&
2126 softc->media_blksize)) {
2127 xpt_print(periph->path,
2128 "BLOCK LIMITS (%d..%d) could not match current "
2129 "block settings (%d)- adjusting\n", softc->min_blk,
2130 softc->max_blk, softc->media_blksize);
2131 softc->max_blk = softc->min_blk =
2132 softc->media_blksize;
2136 * Now put ourselves into the right frame of mind based
2141 * If we want to be in FIXED mode and our current blocksize
2142 * is not equal to our last blocksize (if nonzero), try and
2143 * set ourselves to this last blocksize (as the 'preferred'
2144 * block size). The initial quirkmatch at registry sets the
2145 * initial 'last' blocksize. If, for whatever reason, this
2146 * 'last' blocksize is zero, set the blocksize to 512,
2147 * or min_blk if that's larger.
2149 if ((softc->quirks & SA_QUIRK_FIXED) &&
2150 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2151 (softc->media_blksize != softc->last_media_blksize)) {
2152 softc->media_blksize = softc->last_media_blksize;
2153 if (softc->media_blksize == 0) {
2154 softc->media_blksize = 512;
2155 if (softc->media_blksize < softc->min_blk) {
2156 softc->media_blksize = softc->min_blk;
2159 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2160 softc->media_blksize, 0, 0, SF_NO_PRINT);
2162 xpt_print(periph->path,
2163 "unable to set fixed blocksize to %d\n",
2164 softc->media_blksize);
2169 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2170 (softc->media_blksize != 0)) {
2171 softc->last_media_blksize = softc->media_blksize;
2172 softc->media_blksize = 0;
2173 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2174 0, 0, 0, SF_NO_PRINT);
2177 * If this fails and we were guessing, just
2178 * assume that we got it wrong and go try
2179 * fixed block mode. Don't even check against
2180 * density code at this point.
2183 softc->quirks &= ~SA_QUIRK_VARIABLE;
2184 softc->quirks |= SA_QUIRK_FIXED;
2185 if (softc->last_media_blksize == 0)
2186 softc->last_media_blksize = 512;
2189 xpt_print(periph->path,
2190 "unable to set variable blocksize\n");
2196 * Now that we have the current block size,
2197 * set up some parameters for sastart's usage.
2199 if (softc->media_blksize) {
2200 softc->flags |= SA_FLAG_FIXED;
2201 if (powerof2(softc->media_blksize)) {
2203 ffs(softc->media_blksize) - 1;
2204 softc->blk_mask = softc->media_blksize - 1;
2206 softc->blk_mask = ~0;
2207 softc->blk_shift = 0;
2211 * The SCSI-3 spec allows 0 to mean "unspecified".
2212 * The SCSI-1 spec allows 0 to mean 'infinite'.
2214 * Either works here.
2216 if (softc->max_blk == 0) {
2217 softc->max_blk = ~0;
2219 softc->blk_shift = 0;
2220 if (softc->blk_gran != 0) {
2221 softc->blk_mask = softc->blk_gran - 1;
2223 softc->blk_mask = 0;
2228 softc->flags |= SA_FLAG_TAPE_WP;
2230 if (comp_supported) {
2231 if (softc->saved_comp_algorithm == 0)
2232 softc->saved_comp_algorithm =
2233 softc->comp_algorithm;
2234 softc->flags |= SA_FLAG_COMP_SUPP;
2236 softc->flags |= SA_FLAG_COMP_ENABLED;
2238 softc->flags |= SA_FLAG_COMP_UNSUPP;
2240 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2241 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2242 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2245 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2247 xpt_print(periph->path,
2248 "unable to set buffered mode\n");
2250 error = 0; /* not an error */
2255 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2259 kfree(rblim, M_SCSISA);
2262 softc->dsreg = MTIO_DSREG_NIL;
2264 softc->fileno = softc->blkno = 0;
2265 softc->dsreg = MTIO_DSREG_REST;
2267 #ifdef SA_1FM_AT_EOD
2268 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2269 softc->quirks |= SA_QUIRK_1FM;
2271 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2272 softc->quirks |= SA_QUIRK_2FM;
2275 xpt_release_ccb(ccb);
2278 * If we return an error, we're not mounted any more,
2279 * so release any device reservation.
2282 sareservereleaseunit(periph, FALSE);
2285 * Clear I/O residual.
2287 softc->last_io_resid = 0;
2288 softc->last_ctl_resid = 0;
2294 * How many filemarks do we need to write if we were to terminate the
2295 * tape session right now? Note that this can be a negative number
2299 samarkswanted(struct cam_periph *periph)
2302 struct sa_softc *softc;
2304 softc = (struct sa_softc *)periph->softc;
2306 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2308 if (softc->quirks & SA_QUIRK_2FM)
2311 markswanted -= softc->filemarks;
2312 return (markswanted);
2316 sacheckeod(struct cam_periph *periph)
2321 markswanted = samarkswanted(periph);
2323 if (markswanted > 0) {
2324 error = sawritefilemarks(periph, markswanted, FALSE);
2332 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2334 static const char *toobig =
2335 "%d-byte tape record bigger than supplied buffer\n";
2336 struct cam_periph *periph;
2337 struct sa_softc *softc;
2338 struct ccb_scsiio *csio;
2339 struct scsi_sense_data *sense;
2340 u_int32_t resid = 0;
2343 int error_code, sense_key, asc, ascq, error, aqvalid;
2345 periph = xpt_path_periph(ccb->ccb_h.path);
2346 softc = (struct sa_softc *)periph->softc;
2348 sense = &csio->sense_data;
2349 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2350 aqvalid = sense->extra_len >= 6;
2353 status = csio->ccb_h.status & CAM_STATUS_MASK;
2356 * Calculate/latch up, any residuals... We do this in a funny 2-step
2357 * so we can print stuff here if we have CAM_DEBUG enabled for this
2360 if (status == CAM_SCSI_STATUS_ERROR) {
2361 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2362 info = (int32_t) scsi_4btoul(sense->info);
2364 if ((softc->flags & SA_FLAG_FIXED) != 0)
2365 resid *= softc->media_blksize;
2367 resid = csio->dxfer_len;
2369 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2370 if (softc->media_blksize)
2371 info /= softc->media_blksize;
2374 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2375 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2376 sizeof (struct scsi_sense_data));
2377 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2378 (int) csio->cdb_len);
2379 softc->last_io_resid = resid;
2380 softc->last_resid_was_io = 1;
2382 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2383 sizeof (struct scsi_sense_data));
2384 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2385 (int) csio->cdb_len);
2386 softc->last_ctl_resid = resid;
2387 softc->last_resid_was_io = 0;
2389 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2390 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %d "
2391 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2392 sense_key, asc, ascq, status,
2393 sense->flags & ~SSD_KEY_RESERVED, resid, csio->dxfer_len));
2395 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2396 ("Cam Status 0x%x\n", status));
2402 case CAM_SCSI_STATUS_ERROR:
2404 * If a read/write command, we handle it here.
2406 if (CCB_Type(csio) != SA_CCB_WAITING) {
2410 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2411 * on a non read/write command, we assume it's not an error
2412 * and propagate the residule and return.
2414 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2415 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2416 csio->resid = resid;
2421 * Otherwise, we let the common code handle this.
2423 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2427 * We cannot depend upon CAM honoring retry counts for these.
2429 case CAM_SCSI_BUS_RESET:
2431 if (ccb->ccb_h.retry_count <= 0) {
2436 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2440 * Handle filemark, end of tape, mismatched record sizes....
2441 * From this point out, we're only handling read/write cases.
2442 * Handle writes && reads differently.
2445 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2446 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2447 csio->resid = resid;
2449 } else if (sense->flags & SSD_EOM) {
2450 softc->flags |= SA_FLAG_EOM_PENDING;
2452 * Grotesque as it seems, the few times
2453 * I've actually seen a non-zero resid,
2454 * the tape drive actually lied and had
2455 * written all the data!.
2460 csio->resid = resid;
2461 if (sense_key == SSD_KEY_BLANK_CHECK) {
2462 if (softc->quirks & SA_QUIRK_1FM) {
2464 softc->flags |= SA_FLAG_EOM_PENDING;
2468 } else if (sense->flags & SSD_FILEMARK) {
2469 if (softc->flags & SA_FLAG_FIXED) {
2471 softc->flags |= SA_FLAG_EOF_PENDING;
2474 * Unconditionally, if we detected a filemark on a read,
2475 * mark that we've run moved a file ahead.
2477 if (softc->fileno != (daddr_t) -1) {
2480 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2486 * Incorrect Length usually applies to read, but can apply to writes.
2488 if (error == 0 && (sense->flags & SSD_ILI)) {
2490 xpt_print(csio->ccb_h.path, toobig,
2491 csio->dxfer_len - info);
2492 csio->resid = csio->dxfer_len;
2495 csio->resid = resid;
2496 if (softc->flags & SA_FLAG_FIXED) {
2497 softc->flags |= SA_FLAG_EIO_PENDING;
2500 * Bump the block number if we hadn't seen a filemark.
2501 * Do this independent of errors (we've moved anyway).
2503 if ((sense->flags & SSD_FILEMARK) == 0) {
2504 if (softc->blkno != (daddr_t) -1) {
2506 csio->ccb_h.ccb_pflags |=
2507 SA_POSITION_UPDATED;
2515 * Unfreeze the queue if frozen as we're not returning anything
2516 * to our waiters that would indicate an I/O error has occurred
2526 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2527 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2528 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2529 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2534 struct scsi_mode_header_6 *mode_hdr;
2535 struct scsi_mode_blk_desc *mode_blk;
2536 int mode_buffer_len;
2537 struct sa_softc *softc;
2542 softc = (struct sa_softc *)periph->softc;
2543 ccb = cam_periph_getccb(periph, 1);
2544 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2545 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2547 cpage = SA_DATA_COMPRESSION_PAGE;
2550 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2552 if (params_to_get & SA_PARAM_COMPRESSION) {
2553 if (softc->quirks & SA_QUIRK_NOCOMP) {
2554 *comp_supported = FALSE;
2555 params_to_get &= ~SA_PARAM_COMPRESSION;
2557 mode_buffer_len += sizeof (sa_comp_t);
2560 mode_buffer = kmalloc(mode_buffer_len, M_SCSISA, M_INTWAIT | M_ZERO);
2561 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2562 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2564 /* it is safe to retry this */
2565 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2566 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2567 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2568 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2570 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2571 &softc->device_stats);
2574 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2576 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2578 * Hmm. Let's see if we can try another page...
2579 * If we've already done that, give up on compression
2580 * for this device and remember this for the future
2581 * and attempt the request without asking for compression
2584 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2585 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2588 softc->quirks |= SA_QUIRK_NOCOMP;
2589 kfree(mode_buffer, M_SCSISA);
2591 } else if (status == CAM_SCSI_STATUS_ERROR) {
2592 /* Tell the user about the fatal error. */
2593 scsi_sense_print(&ccb->csio);
2594 goto sagetparamsexit;
2598 * If the user only wants the compression information, and
2599 * the device doesn't send back the block descriptor, it's
2600 * no big deal. If the user wants more than just
2601 * compression, though, and the device doesn't pass back the
2602 * block descriptor, we need to send another mode sense to
2603 * get the block descriptor.
2605 if ((mode_hdr->blk_desc_len == 0) &&
2606 (params_to_get & SA_PARAM_COMPRESSION) &&
2607 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2610 * Decrease the mode buffer length by the size of
2611 * the compression page, to make sure the data
2612 * there doesn't get overwritten.
2614 mode_buffer_len -= sizeof (sa_comp_t);
2617 * Now move the compression page that we presumably
2618 * got back down the memory chunk a little bit so
2619 * it doesn't get spammed.
2621 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2622 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2625 * Now, we issue another mode sense and just ask
2626 * for the block descriptor, etc.
2629 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2630 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2631 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2634 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2635 &softc->device_stats);
2639 goto sagetparamsexit;
2642 if (params_to_get & SA_PARAM_BLOCKSIZE)
2643 *blocksize = scsi_3btoul(mode_blk->blklen);
2645 if (params_to_get & SA_PARAM_NUMBLOCKS)
2646 *numblocks = scsi_3btoul(mode_blk->nblocks);
2648 if (params_to_get & SA_PARAM_BUFF_MODE)
2649 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2651 if (params_to_get & SA_PARAM_DENSITY)
2652 *density = mode_blk->density;
2654 if (params_to_get & SA_PARAM_WP)
2655 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2657 if (params_to_get & SA_PARAM_SPEED)
2658 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2660 if (params_to_get & SA_PARAM_COMPRESSION) {
2661 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2662 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2663 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2665 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2667 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2668 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2670 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2672 * We don't really know whether this device supports
2673 * Data Compression if the the algorithm field is
2674 * zero. Just say we do.
2676 *comp_supported = TRUE;
2678 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2679 *comp_algorithm = cp->sel_comp_alg;
2682 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2685 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2687 char *xyz = mode_buffer;
2688 xpt_print_path(periph->path);
2689 kprintf("Mode Sense Data=");
2690 for (idx = 0; idx < mode_buffer_len; idx++)
2691 kprintf(" 0x%02x", xyz[idx] & 0xff);
2697 xpt_release_ccb(ccb);
2698 kfree(mode_buffer, M_SCSISA);
2703 * The purpose of this function is to set one of four different parameters
2707 * - compression / compression algorithm
2710 * The assumption is that this will be called from saioctl(), and therefore
2711 * from a process context. Thus the waiting malloc calls below. If that
2712 * assumption ever changes, the malloc calls should be changed to be
2715 * Any or all of the four parameters may be set when this function is
2716 * called. It should handle setting more than one parameter at once.
2719 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2720 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2721 u_int32_t sense_flags)
2723 struct sa_softc *softc;
2724 u_int32_t current_blocksize = 0;/* silence gcc */
2725 u_int32_t current_calg;
2726 u_int8_t current_density = 0; /* silence gcc */
2727 u_int8_t current_speed = 0; /* silence gcc */
2728 int comp_enabled, comp_supported;
2730 int mode_buffer_len;
2731 struct scsi_mode_header_6 *mode_hdr;
2732 struct scsi_mode_blk_desc *mode_blk;
2733 sa_comp_t *ccomp, *cpage;
2735 union ccb *ccb = NULL;
2738 softc = (struct sa_softc *)periph->softc;
2740 ccomp = kmalloc(sizeof (sa_comp_t), M_SCSISA, M_INTWAIT);
2743 * Since it doesn't make sense to set the number of blocks, or
2744 * write protection, we won't try to get the current value. We
2745 * always want to get the blocksize, so we can set it back to the
2748 error = sagetparams(periph,
2749 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2750 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2751 ¤t_speed, &comp_supported, &comp_enabled,
2752 ¤t_calg, ccomp);
2755 kfree(ccomp, M_SCSISA);
2759 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2760 if (params_to_set & SA_PARAM_COMPRESSION)
2761 mode_buffer_len += sizeof (sa_comp_t);
2763 mode_buffer = kmalloc(mode_buffer_len, M_SCSISA, M_INTWAIT | M_ZERO);
2765 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2766 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2768 ccb = cam_periph_getccb(periph, 1);
2772 if (params_to_set & SA_PARAM_COMPRESSION) {
2774 cpage = (sa_comp_t *)&mode_blk[1];
2776 cpage = (sa_comp_t *)&mode_hdr[1];
2778 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2779 cpage->hdr.pagecode &= ~0x80;
2784 * If the caller wants us to set the blocksize, use the one they
2785 * pass in. Otherwise, use the blocksize we got back from the
2786 * mode select above.
2789 if (params_to_set & SA_PARAM_BLOCKSIZE)
2790 scsi_ulto3b(blocksize, mode_blk->blklen);
2792 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2795 * Set density if requested, else preserve old density.
2796 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2797 * devices, else density we've latched up in our softc.
2799 if (params_to_set & SA_PARAM_DENSITY) {
2800 mode_blk->density = density;
2801 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2802 mode_blk->density = SCSI_SAME_DENSITY;
2804 mode_blk->density = softc->media_density;
2809 * For mode selects, these two fields must be zero.
2811 mode_hdr->data_length = 0;
2812 mode_hdr->medium_type = 0;
2814 /* set the speed to the current value */
2815 mode_hdr->dev_spec = current_speed;
2817 /* if set, set single-initiator buffering mode */
2818 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
2819 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2823 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2825 mode_hdr->blk_desc_len = 0;
2828 * First, if the user wants us to set the compression algorithm or
2829 * just turn compression on, check to make sure that this drive
2830 * supports compression.
2832 if (params_to_set & SA_PARAM_COMPRESSION) {
2834 * If the compression algorithm is 0, disable compression.
2835 * If the compression algorithm is non-zero, enable
2836 * compression and set the compression type to the
2837 * specified compression algorithm, unless the algorithm is
2838 * MT_COMP_ENABLE. In that case, we look at the
2839 * compression algorithm that is currently set and if it is
2840 * non-zero, we leave it as-is. If it is zero, and we have
2841 * saved a compression algorithm from a time when
2842 * compression was enabled before, set the compression to
2845 switch (ccomp->hdr.pagecode & ~0x80) {
2846 case SA_DEVICE_CONFIGURATION_PAGE:
2848 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2850 dcp->sel_comp_alg = SA_COMP_NONE;
2853 if (calg != MT_COMP_ENABLE) {
2854 dcp->sel_comp_alg = calg;
2855 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2856 softc->saved_comp_algorithm != 0) {
2857 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2861 case SA_DATA_COMPRESSION_PAGE:
2862 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2863 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2866 * Disable compression, but leave the
2867 * decompression and the capability bit
2870 dcp->dce_and_dcc = SA_DCP_DCC;
2871 dcp->dde_and_red |= SA_DCP_DDE;
2874 /* enable compression && decompression */
2875 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2876 dcp->dde_and_red |= SA_DCP_DDE;
2878 * If there, use compression algorithm from caller.
2879 * Otherwise, if there's a saved compression algorithm
2880 * and there is no current algorithm, use the saved
2881 * algorithm. Else parrot back what we got and hope
2884 if (calg != MT_COMP_ENABLE) {
2885 scsi_ulto4b(calg, dcp->comp_algorithm);
2886 scsi_ulto4b(calg, dcp->decomp_algorithm);
2887 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2888 softc->saved_comp_algorithm != 0) {
2889 scsi_ulto4b(softc->saved_comp_algorithm,
2890 dcp->comp_algorithm);
2891 scsi_ulto4b(softc->saved_comp_algorithm,
2892 dcp->decomp_algorithm);
2897 * Compression does not appear to be supported-
2898 * at least via the DATA COMPRESSION page. It
2899 * would be too much to ask us to believe that
2900 * the page itself is supported, but incorrectly
2901 * reports an ability to manipulate data compression,
2902 * so we'll assume that this device doesn't support
2903 * compression. We can just fall through for that.
2908 * The drive doesn't seem to support compression,
2909 * so turn off the set compression bit.
2911 params_to_set &= ~SA_PARAM_COMPRESSION;
2912 xpt_print(periph->path,
2913 "device does not seem to support compression\n");
2916 * If that was the only thing the user wanted us to set,
2917 * clean up allocated resources and return with
2918 * 'operation not supported'.
2920 if (params_to_set == SA_PARAM_NONE) {
2921 kfree(mode_buffer, M_SCSISA);
2922 xpt_release_ccb(ccb);
2927 * That wasn't the only thing the user wanted us to set.
2928 * So, decrease the stated mode buffer length by the
2929 * size of the compression mode page.
2931 mode_buffer_len -= sizeof(sa_comp_t);
2935 /* It is safe to retry this operation */
2936 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2937 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2938 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2940 error = cam_periph_runccb(ccb, saerror, 0,
2941 sense_flags, &softc->device_stats);
2944 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2946 char *xyz = mode_buffer;
2947 xpt_print_path(periph->path);
2948 kprintf("Err%d, Mode Select Data=", error);
2949 for (idx = 0; idx < mode_buffer_len; idx++)
2950 kprintf(" 0x%02x", xyz[idx] & 0xff);
2957 * If we can, try without setting density/blocksize.
2960 if ((params_to_set &
2961 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2966 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2967 cpage = (sa_comp_t *)&mode_blk[1];
2971 * If we were setting the blocksize, and that failed, we
2972 * want to set it to its original value. If we weren't
2973 * setting the blocksize, we don't want to change it.
2975 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2978 * Set density if requested, else preserve old density.
2979 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2980 * devices, else density we've latched up in our softc.
2982 if (params_to_set & SA_PARAM_DENSITY) {
2983 mode_blk->density = current_density;
2984 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2985 mode_blk->density = SCSI_SAME_DENSITY;
2987 mode_blk->density = softc->media_density;
2990 if (params_to_set & SA_PARAM_COMPRESSION)
2991 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2994 * The retry count is the only CCB field that might have been
2995 * changed that we care about, so reset it back to 1.
2997 ccb->ccb_h.retry_count = 1;
2998 cam_periph_runccb(ccb, saerror, 0, sense_flags,
2999 &softc->device_stats);
3003 xpt_release_ccb(ccb);
3006 kfree(ccomp, M_SCSISA);
3008 if (params_to_set & SA_PARAM_COMPRESSION) {
3010 softc->flags &= ~SA_FLAG_COMP_ENABLED;
3012 * Even if we get an error setting compression,
3013 * do not say that we don't support it. We could
3014 * have been wrong, or it may be media specific.
3015 * softc->flags &= ~SA_FLAG_COMP_SUPP;
3017 softc->saved_comp_algorithm = softc->comp_algorithm;
3018 softc->comp_algorithm = 0;
3020 softc->flags |= SA_FLAG_COMP_ENABLED;
3021 softc->comp_algorithm = calg;
3025 kfree(mode_buffer, M_SCSISA);
3030 saprevent(struct cam_periph *periph, int action)
3032 struct sa_softc *softc;
3036 softc = (struct sa_softc *)periph->softc;
3038 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3040 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3044 * We can be quiet about illegal requests.
3046 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3051 ccb = cam_periph_getccb(periph, 1);
3053 /* It is safe to retry this operation */
3054 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3055 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3057 error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats);
3060 if (action == PR_ALLOW)
3061 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3063 softc->flags |= SA_FLAG_TAPE_LOCKED;
3066 xpt_release_ccb(ccb);
3070 sarewind(struct cam_periph *periph)
3073 struct sa_softc *softc;
3076 softc = (struct sa_softc *)periph->softc;
3078 ccb = cam_periph_getccb(periph, 1);
3080 /* It is safe to retry this operation */
3081 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3082 SSD_FULL_SIZE, REWIND_TIMEOUT);
3084 softc->dsreg = MTIO_DSREG_REW;
3085 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3086 softc->dsreg = MTIO_DSREG_REST;
3088 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3089 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3091 xpt_release_ccb(ccb);
3093 softc->fileno = softc->blkno = (daddr_t) 0;
3095 softc->fileno = softc->blkno = (daddr_t) -1;
3100 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3103 struct sa_softc *softc;
3106 softc = (struct sa_softc *)periph->softc;
3108 ccb = cam_periph_getccb(periph, 1);
3110 /* This cannot be retried */
3112 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3113 SSD_FULL_SIZE, SPACE_TIMEOUT);
3116 * Clear residual because we will be using it.
3118 softc->last_ctl_resid = 0;
3120 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3121 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3122 softc->dsreg = MTIO_DSREG_REST;
3124 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3125 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3127 xpt_release_ccb(ccb);
3130 * If a spacing operation has failed, we need to invalidate
3133 * If the spacing operation was setmarks or to end of recorded data,
3134 * we no longer know our relative position.
3136 * If the spacing operations was spacing files in reverse, we
3137 * take account of the residual, but still check against less
3138 * than zero- if we've gone negative, we must have hit BOT.
3140 * If the spacing operations was spacing records in reverse and
3141 * we have a residual, we've either hit BOT or hit a filemark.
3142 * In the former case, we know our new record number (0). In
3143 * the latter case, we have absolutely no idea what the real
3144 * record number is- we've stopped between the end of the last
3145 * record in the previous file and the filemark that stopped
3146 * our spacing backwards.
3149 softc->fileno = softc->blkno = (daddr_t) -1;
3150 } else if (code == SS_SETMARKS || code == SS_EOD) {
3151 softc->fileno = softc->blkno = (daddr_t) -1;
3152 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3153 softc->fileno += (count - softc->last_ctl_resid);
3154 if (softc->fileno < 0) /* we must of hit BOT */
3157 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3158 softc->blkno += (count - softc->last_ctl_resid);
3160 if (softc->last_ctl_resid || softc->blkno < 0) {
3161 if (softc->fileno == 0) {
3164 softc->blkno = (daddr_t) -1;
3173 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3176 struct sa_softc *softc;
3179 softc = (struct sa_softc *)periph->softc;
3180 if (softc->open_rdonly)
3183 ccb = cam_periph_getccb(periph, 1);
3185 * Clear residual because we will be using it.
3187 softc->last_ctl_resid = 0;
3189 softc->dsreg = MTIO_DSREG_FMK;
3190 /* this *must* not be retried */
3191 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3192 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3193 softc->dsreg = MTIO_DSREG_REST;
3196 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3198 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3199 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3201 if (error == 0 && nmarks) {
3202 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3203 nwm = nmarks - softc->last_ctl_resid;
3204 softc->filemarks += nwm;
3207 xpt_release_ccb(ccb);
3210 * Update relative positions (if we're doing that).
3213 softc->fileno = softc->blkno = (daddr_t) -1;
3214 } else if (softc->fileno != (daddr_t) -1) {
3215 softc->fileno += nwm;
3222 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3224 struct scsi_tape_position_data loc;
3226 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3230 * We try and flush any buffered writes here if we were writing
3231 * and we're trying to get hardware block position. It eats
3232 * up performance substantially, but I'm wary of drive firmware.
3234 * I think that *logical* block position is probably okay-
3235 * but hardware block position might have to wait for data
3236 * to hit media to be valid. Caveat Emptor.
3239 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3240 error = sawritefilemarks(periph, 0, 0);
3241 if (error && error != EACCES)
3245 ccb = cam_periph_getccb(periph, 1);
3246 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3247 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3248 softc->dsreg = MTIO_DSREG_RBSY;
3249 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3250 softc->dsreg = MTIO_DSREG_REST;
3251 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3252 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3255 if (loc.flags & SA_RPOS_UNCERTAIN) {
3256 error = EINVAL; /* nothing is certain */
3258 *blkptr = scsi_4btoul(loc.firstblk);
3262 xpt_release_ccb(ccb);
3267 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3270 struct sa_softc *softc;
3274 * We used to try and flush any buffered writes here.
3275 * Now we push this onto user applications to either
3276 * flush the pending writes themselves (via a zero count
3277 * WRITE FILEMARKS command) or they can trust their tape
3278 * drive to do this correctly for them.
3281 softc = (struct sa_softc *)periph->softc;
3282 ccb = cam_periph_getccb(periph, 1);
3285 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3286 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3289 softc->dsreg = MTIO_DSREG_POS;
3290 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3291 softc->dsreg = MTIO_DSREG_REST;
3292 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3293 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3294 xpt_release_ccb(ccb);
3296 * Note relative file && block number position as now unknown.
3298 softc->fileno = softc->blkno = (daddr_t) -1;
3303 saretension(struct cam_periph *periph)
3306 struct sa_softc *softc;
3309 softc = (struct sa_softc *)periph->softc;
3311 ccb = cam_periph_getccb(periph, 1);
3313 /* It is safe to retry this operation */
3314 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3315 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3317 softc->dsreg = MTIO_DSREG_TEN;
3318 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3319 softc->dsreg = MTIO_DSREG_REST;
3321 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3322 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3323 xpt_release_ccb(ccb);
3325 softc->fileno = softc->blkno = (daddr_t) 0;
3327 softc->fileno = softc->blkno = (daddr_t) -1;
3332 sareservereleaseunit(struct cam_periph *periph, int reserve)
3335 struct sa_softc *softc;
3338 softc = (struct sa_softc *)periph->softc;
3339 ccb = cam_periph_getccb(periph, 1);
3341 /* It is safe to retry this operation */
3342 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3343 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3344 softc->dsreg = MTIO_DSREG_RBSY;
3345 error = cam_periph_runccb(ccb, saerror, 0,
3346 SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats);
3347 softc->dsreg = MTIO_DSREG_REST;
3349 xpt_release_ccb(ccb);
3352 * If the error was Illegal Request, then the device doesn't support
3353 * RESERVE/RELEASE. This is not an error.
3355 if (error == EINVAL) {
3363 saloadunload(struct cam_periph *periph, int load)
3366 struct sa_softc *softc;
3369 softc = (struct sa_softc *)periph->softc;
3371 ccb = cam_periph_getccb(periph, 1);
3373 /* It is safe to retry this operation */
3374 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3375 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3377 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3378 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3379 softc->dsreg = MTIO_DSREG_REST;
3381 xpt_release_ccb(ccb);
3383 if (error || load == 0)
3384 softc->fileno = softc->blkno = (daddr_t) -1;
3385 else if (error == 0)
3386 softc->fileno = softc->blkno = (daddr_t) 0;
3391 saerase(struct cam_periph *periph, int longerase)
3395 struct sa_softc *softc;
3398 softc = (struct sa_softc *)periph->softc;
3399 if (softc->open_rdonly)
3402 ccb = cam_periph_getccb(periph, 1);
3404 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3405 SSD_FULL_SIZE, ERASE_TIMEOUT);
3407 softc->dsreg = MTIO_DSREG_ZER;
3408 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3409 softc->dsreg = MTIO_DSREG_REST;
3411 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3412 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3413 xpt_release_ccb(ccb);
3417 #endif /* _KERNEL */
3420 * Read tape block limits command.
3423 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3424 void (*cbfcnp)(struct cam_periph *, union ccb *),
3425 u_int8_t tag_action,
3426 struct scsi_read_block_limits_data *rlimit_buf,
3427 u_int8_t sense_len, u_int32_t timeout)
3429 struct scsi_read_block_limits *scsi_cmd;
3431 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3432 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3433 sizeof(*scsi_cmd), timeout);
3435 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3436 bzero(scsi_cmd, sizeof(*scsi_cmd));
3437 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3441 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3442 void (*cbfcnp)(struct cam_periph *, union ccb *),
3443 u_int8_t tag_action, int readop, int sli,
3444 int fixed, u_int32_t length, u_int8_t *data_ptr,
3445 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3447 struct scsi_sa_rw *scsi_cmd;
3449 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3450 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3451 scsi_cmd->sli_fixed = 0;
3453 scsi_cmd->sli_fixed |= SAR_SLI;
3455 scsi_cmd->sli_fixed |= SARW_FIXED;
3456 scsi_ulto3b(length, scsi_cmd->length);
3457 scsi_cmd->control = 0;
3459 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3460 tag_action, data_ptr, dxfer_len, sense_len,
3461 sizeof(*scsi_cmd), timeout);
3465 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3466 void (*cbfcnp)(struct cam_periph *, union ccb *),
3467 u_int8_t tag_action, int immediate, int eot,
3468 int reten, int load, u_int8_t sense_len,
3471 struct scsi_load_unload *scsi_cmd;
3473 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3474 bzero(scsi_cmd, sizeof(*scsi_cmd));
3475 scsi_cmd->opcode = LOAD_UNLOAD;
3477 scsi_cmd->immediate = SLU_IMMED;
3479 scsi_cmd->eot_reten_load |= SLU_EOT;
3481 scsi_cmd->eot_reten_load |= SLU_RETEN;
3483 scsi_cmd->eot_reten_load |= SLU_LOAD;
3485 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3486 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3490 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3491 void (*cbfcnp)(struct cam_periph *, union ccb *),
3492 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3495 struct scsi_rewind *scsi_cmd;
3497 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3498 bzero(scsi_cmd, sizeof(*scsi_cmd));
3499 scsi_cmd->opcode = REWIND;
3501 scsi_cmd->immediate = SREW_IMMED;
3503 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3504 0, sense_len, sizeof(*scsi_cmd), timeout);
3508 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3509 void (*cbfcnp)(struct cam_periph *, union ccb *),
3510 u_int8_t tag_action, scsi_space_code code,
3511 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3513 struct scsi_space *scsi_cmd;
3515 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3516 scsi_cmd->opcode = SPACE;
3517 scsi_cmd->code = code;
3518 scsi_ulto3b(count, scsi_cmd->count);
3519 scsi_cmd->control = 0;
3521 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3522 0, sense_len, sizeof(*scsi_cmd), timeout);
3526 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3527 void (*cbfcnp)(struct cam_periph *, union ccb *),
3528 u_int8_t tag_action, int immediate, int setmark,
3529 u_int32_t num_marks, u_int8_t sense_len,
3532 struct scsi_write_filemarks *scsi_cmd;
3534 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3535 bzero(scsi_cmd, sizeof(*scsi_cmd));
3536 scsi_cmd->opcode = WRITE_FILEMARKS;
3538 scsi_cmd->byte2 |= SWFMRK_IMMED;
3540 scsi_cmd->byte2 |= SWFMRK_WSMK;
3542 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3544 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3545 0, sense_len, sizeof(*scsi_cmd), timeout);
3549 * The reserve and release unit commands differ only by their opcodes.
3552 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3553 void (*cbfcnp)(struct cam_periph *, union ccb *),
3554 u_int8_t tag_action, int third_party,
3555 int third_party_id, u_int8_t sense_len,
3556 u_int32_t timeout, int reserve)
3558 struct scsi_reserve_release_unit *scsi_cmd;
3560 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3561 bzero(scsi_cmd, sizeof(*scsi_cmd));
3564 scsi_cmd->opcode = RESERVE_UNIT;
3566 scsi_cmd->opcode = RELEASE_UNIT;
3569 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3570 scsi_cmd->lun_thirdparty |=
3571 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3574 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3575 0, sense_len, sizeof(*scsi_cmd), timeout);
3579 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3580 void (*cbfcnp)(struct cam_periph *, union ccb *),
3581 u_int8_t tag_action, int immediate, int long_erase,
3582 u_int8_t sense_len, u_int32_t timeout)
3584 struct scsi_erase *scsi_cmd;
3586 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3587 bzero(scsi_cmd, sizeof(*scsi_cmd));
3589 scsi_cmd->opcode = ERASE;
3592 scsi_cmd->lun_imm_long |= SE_IMMED;
3595 scsi_cmd->lun_imm_long |= SE_LONG;
3597 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3598 0, sense_len, sizeof(*scsi_cmd), timeout);
3602 * Read Tape Position command.
3605 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3606 void (*cbfcnp)(struct cam_periph *, union ccb *),
3607 u_int8_t tag_action, int hardsoft,
3608 struct scsi_tape_position_data *sbp,
3609 u_int8_t sense_len, u_int32_t timeout)
3611 struct scsi_tape_read_position *scmd;
3613 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3614 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3615 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3616 bzero(scmd, sizeof(*scmd));
3617 scmd->opcode = READ_POSITION;
3618 scmd->byte1 = hardsoft;
3622 * Set Tape Position command.
3625 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3626 void (*cbfcnp)(struct cam_periph *, union ccb *),
3627 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3628 u_int8_t sense_len, u_int32_t timeout)
3630 struct scsi_tape_locate *scmd;
3632 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3633 NULL, 0, sense_len, sizeof(*scmd), timeout);
3634 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3635 bzero(scmd, sizeof(*scmd));
3636 scmd->opcode = LOCATE;
3638 scmd->byte1 |= SA_SPOS_BT;
3639 scsi_ulto4b(blkno, scmd->blkaddr);