2 * Implementation of SCSI Sequential Access Peripheral driver for CAM.
4 * Copyright (c) 1999, 2000 Matthew Jacob
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/queue.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/types.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
47 #include <sys/fcntl.h>
48 #include <sys/devicestat.h>
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_periph.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_debug.h>
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/scsi/scsi_sa.h>
70 #define SA_IO_TIMEOUT 4
72 #ifndef SA_SPACE_TIMEOUT
73 #define SA_SPACE_TIMEOUT 1 * 60
75 #ifndef SA_REWIND_TIMEOUT
76 #define SA_REWIND_TIMEOUT 2 * 60
78 #ifndef SA_ERASE_TIMEOUT
79 #define SA_ERASE_TIMEOUT 4 * 60
82 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
84 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
85 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
86 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
87 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
90 * Additional options that can be set for config: SA_1FM_AT_EOT
93 #ifndef UNUSED_PARAMETER
94 #define UNUSED_PARAMETER(x) x = x
98 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
99 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
105 static MALLOC_DEFINE(M_SCSISA, "SCSI sa", "SCSI sequential access buffers");
108 SA_STATE_NORMAL, SA_STATE_ABNORMAL
111 #define ccb_pflags ppriv_field0
112 #define ccb_bp ppriv_ptr1
114 #define SA_CCB_BUFFER_IO 0x0
115 #define SA_CCB_WAITING 0x1
116 #define SA_CCB_TYPEMASK 0x1
117 #define SA_POSITION_UPDATED 0x2
119 #define Set_CCB_Type(x, type) \
120 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
121 x->ccb_h.ccb_pflags |= type
123 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
128 SA_FLAG_OPEN = 0x0001,
129 SA_FLAG_FIXED = 0x0002,
130 SA_FLAG_TAPE_LOCKED = 0x0004,
131 SA_FLAG_TAPE_MOUNTED = 0x0008,
132 SA_FLAG_TAPE_WP = 0x0010,
133 SA_FLAG_TAPE_WRITTEN = 0x0020,
134 SA_FLAG_EOM_PENDING = 0x0040,
135 SA_FLAG_EIO_PENDING = 0x0080,
136 SA_FLAG_EOF_PENDING = 0x0100,
137 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
138 SA_FLAG_EOF_PENDING),
139 SA_FLAG_INVALID = 0x0200,
140 SA_FLAG_COMP_ENABLED = 0x0400,
141 SA_FLAG_COMP_SUPP = 0x0800,
142 SA_FLAG_COMP_UNSUPP = 0x1000,
143 SA_FLAG_TAPE_FROZEN = 0x2000
147 SA_MODE_REWIND = 0x00,
148 SA_MODE_NOREWIND = 0x01,
149 SA_MODE_OFFLINE = 0x02
153 SA_PARAM_NONE = 0x00,
154 SA_PARAM_BLOCKSIZE = 0x01,
155 SA_PARAM_DENSITY = 0x02,
156 SA_PARAM_COMPRESSION = 0x04,
157 SA_PARAM_BUFF_MODE = 0x08,
158 SA_PARAM_NUMBLOCKS = 0x10,
160 SA_PARAM_SPEED = 0x40,
165 SA_QUIRK_NONE = 0x00,
166 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
167 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
168 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
169 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
170 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
171 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
172 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
173 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
176 #define SA_QUIRK_BIT_STRING \
187 #define SAMODE(z) (dev2unit(z) & 0x3)
188 #define SADENSITY(z) ((dev2unit(z) >> 2) & 0x3)
189 #define SA_IS_CTRL(z) (dev2unit(z) & (1 << 4))
191 #define SA_NOT_CTLDEV 0
195 #define SA_ATYPE_NR 1
196 #define SA_ATYPE_ER 2
198 #define SAMINOR(ctl, mode, access) \
199 ((ctl << 4) | (mode << 2) | (access & 0x3))
201 #define SA_NUM_MODES 4
203 struct cdev *ctl_dev;
204 struct sa_mode_devs {
208 } mode_devs[SA_NUM_MODES];
216 struct bio_queue_head bio_queue;
218 struct devstat *device_stats;
226 u_int32_t comp_algorithm;
227 u_int32_t saved_comp_algorithm;
228 u_int32_t media_blksize;
229 u_int32_t last_media_blksize;
230 u_int32_t media_numblks;
231 u_int8_t media_density;
234 u_int8_t dsreg; /* mtio mt_dsreg, redux */
238 int last_resid_was_io;
241 * Relative to BOT Location.
250 struct scsi_sense_data _last_io_sense;
251 u_int64_t _last_io_resid;
252 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
253 struct scsi_sense_data _last_ctl_sense;
254 u_int64_t _last_ctl_resid;
255 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
256 #define last_io_sense errinfo._last_io_sense
257 #define last_io_resid errinfo._last_io_resid
258 #define last_io_cdb errinfo._last_io_cdb
259 #define last_ctl_sense errinfo._last_ctl_sense
260 #define last_ctl_resid errinfo._last_ctl_resid
261 #define last_ctl_cdb errinfo._last_ctl_cdb
264 * Misc other flags/state
268 open_rdonly : 1, /* open read-only */
269 open_pending_mount : 1, /* open pending mount */
270 ctrl_mode : 1; /* control device open */
273 struct sa_quirk_entry {
274 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
275 sa_quirks quirks; /* specific quirk type */
276 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
279 static struct sa_quirk_entry sa_quirk_table[] =
282 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
283 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
284 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
287 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
288 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
291 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
292 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
295 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
296 "Python*", "*"}, SA_QUIRK_NODREAD, 0
299 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
300 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
303 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
304 "VIPER 2525 25462", "-011"},
305 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
308 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
309 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
313 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
314 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
318 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
319 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
322 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
323 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
326 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
327 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
330 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
331 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
334 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
335 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
338 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
339 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
341 { /* jreynold@primenet.com */
342 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
343 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
345 { /* mike@sentex.net */
346 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
347 "STT20000*", "*"}, SA_QUIRK_1FM, 0
350 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "SEAGATE",
351 "DAT 06241-XXX", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
354 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
355 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
358 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
359 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
362 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
363 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
366 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
367 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
370 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
371 " SLR*", "*"}, SA_QUIRK_1FM, 0
374 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
375 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
378 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
379 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
383 static d_open_t saopen;
384 static d_close_t saclose;
385 static d_strategy_t sastrategy;
386 static d_ioctl_t saioctl;
387 static periph_init_t sainit;
388 static periph_ctor_t saregister;
389 static periph_oninv_t saoninvalidate;
390 static periph_dtor_t sacleanup;
391 static periph_start_t sastart;
392 static void saasync(void *callback_arg, u_int32_t code,
393 struct cam_path *path, void *arg);
394 static void sadone(struct cam_periph *periph,
395 union ccb *start_ccb);
396 static int saerror(union ccb *ccb, u_int32_t cam_flags,
397 u_int32_t sense_flags);
398 static int samarkswanted(struct cam_periph *);
399 static int sacheckeod(struct cam_periph *periph);
400 static int sagetparams(struct cam_periph *periph,
401 sa_params params_to_get,
402 u_int32_t *blocksize, u_int8_t *density,
403 u_int32_t *numblocks, int *buff_mode,
404 u_int8_t *write_protect, u_int8_t *speed,
405 int *comp_supported, int *comp_enabled,
406 u_int32_t *comp_algorithm,
407 sa_comp_t *comp_page);
408 static int sasetparams(struct cam_periph *periph,
409 sa_params params_to_set,
410 u_int32_t blocksize, u_int8_t density,
411 u_int32_t comp_algorithm,
412 u_int32_t sense_flags);
413 static void saprevent(struct cam_periph *periph, int action);
414 static int sarewind(struct cam_periph *periph);
415 static int saspace(struct cam_periph *periph, int count,
416 scsi_space_code code);
417 static int samount(struct cam_periph *, int, struct cdev *);
418 static int saretension(struct cam_periph *periph);
419 static int sareservereleaseunit(struct cam_periph *periph,
421 static int saloadunload(struct cam_periph *periph, int load);
422 static int saerase(struct cam_periph *periph, int longerase);
423 static int sawritefilemarks(struct cam_periph *periph,
424 int nmarks, int setmarks);
425 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
426 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
429 static struct periph_driver sadriver =
432 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
435 PERIPHDRIVER_DECLARE(sa, sadriver);
437 /* For 2.2-stable support */
443 static struct cdevsw sa_cdevsw = {
444 .d_version = D_VERSION,
448 .d_write = physwrite,
450 .d_strategy = sastrategy,
456 saopen(struct cdev *dev, int flags, int fmt, struct thread *td)
458 struct cam_periph *periph;
459 struct sa_softc *softc;
462 periph = (struct cam_periph *)dev->si_drv1;
463 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
467 cam_periph_lock(periph);
469 softc = (struct sa_softc *)periph->softc;
471 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
472 ("saopen(%s): softc=0x%x\n", devtoname(dev), softc->flags));
474 if (SA_IS_CTRL(dev)) {
475 softc->ctrl_mode = 1;
476 cam_periph_unlock(periph);
480 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
481 cam_periph_unlock(periph);
482 cam_periph_release(periph);
486 if (softc->flags & SA_FLAG_OPEN) {
488 } else if (softc->flags & SA_FLAG_INVALID) {
492 * Preserve whether this is a read_only open.
494 softc->open_rdonly = (flags & O_RDWR) == O_RDONLY;
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 * If the mount fails and this was a non-blocking open,
501 * make this a 'open_pending_mount' action.
503 error = samount(periph, flags, dev);
504 if (error && (flags & O_NONBLOCK)) {
505 softc->flags |= SA_FLAG_OPEN;
506 softc->open_pending_mount = 1;
507 cam_periph_unhold(periph);
508 cam_periph_unlock(periph);
514 cam_periph_unhold(periph);
515 cam_periph_unlock(periph);
516 cam_periph_release(periph);
520 saprevent(periph, PR_PREVENT);
521 softc->flags |= SA_FLAG_OPEN;
523 cam_periph_unhold(periph);
524 cam_periph_unlock(periph);
529 saclose(struct cdev *dev, int flag, int fmt, struct thread *td)
531 struct cam_periph *periph;
532 struct sa_softc *softc;
533 int mode, error, writing, tmp;
534 int closedbits = SA_FLAG_OPEN;
537 periph = (struct cam_periph *)dev->si_drv1;
541 cam_periph_lock(periph);
543 softc = (struct sa_softc *)periph->softc;
545 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
546 ("saclose(%s): softc=0x%x\n", devtoname(dev), softc->flags));
549 softc->open_rdonly = 0;
550 if (SA_IS_CTRL(dev)) {
551 softc->ctrl_mode = 0;
552 cam_periph_unlock(periph);
553 cam_periph_release(periph);
557 if (softc->open_pending_mount) {
558 softc->flags &= ~SA_FLAG_OPEN;
559 softc->open_pending_mount = 0;
560 cam_periph_unlock(periph);
561 cam_periph_release(periph);
565 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
566 cam_periph_unlock(periph);
571 * Were we writing the tape?
573 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
576 * See whether or not we need to write filemarks. If this
577 * fails, we probably have to assume we've lost tape
580 error = sacheckeod(periph);
582 xpt_print(periph->path,
583 "failed to write terminating filemark(s)\n");
584 softc->flags |= SA_FLAG_TAPE_FROZEN;
588 * Whatever we end up doing, allow users to eject tapes from here on.
590 saprevent(periph, PR_ALLOW);
593 * Decide how to end...
595 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
596 closedbits |= SA_FLAG_TAPE_FROZEN;
597 } else switch (mode) {
598 case SA_MODE_OFFLINE:
600 * An 'offline' close is an unconditional release of
601 * frozen && mount conditions, irrespective of whether
602 * these operations succeeded. The reason for this is
603 * to allow at least some kind of programmatic way
604 * around our state getting all fouled up. If somebody
605 * issues an 'offline' command, that will be allowed
608 (void) sarewind(periph);
609 (void) saloadunload(periph, FALSE);
610 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
614 * If the rewind fails, return an error- if anyone cares,
615 * but not overwriting any previous error.
617 * We don't clear the notion of mounted here, but we do
618 * clear the notion of frozen if we successfully rewound.
620 tmp = sarewind(periph);
625 closedbits |= SA_FLAG_TAPE_FROZEN;
628 case SA_MODE_NOREWIND:
630 * If we're not rewinding/unloading the tape, find out
631 * whether we need to back up over one of two filemarks
632 * we wrote (if we wrote two filemarks) so that appends
633 * from this point on will be sane.
635 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
636 tmp = saspace(periph, -1, SS_FILEMARKS);
638 xpt_print(periph->path, "unable to backspace "
639 "over one of double filemarks at end of "
641 xpt_print(periph->path, "it is possible that "
642 "this device needs a SA_QUIRK_1FM quirk set"
644 softc->flags |= SA_FLAG_TAPE_FROZEN;
649 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode);
655 * We wish to note here that there are no more filemarks to be written.
657 softc->filemarks = 0;
658 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
661 * And we are no longer open for business.
663 softc->flags &= ~closedbits;
666 * Inform users if tape state if frozen....
668 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
669 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, "
670 "REWIND or MTEOM command to clear this state.\n");
673 /* release the device if it is no longer mounted */
674 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
675 sareservereleaseunit(periph, FALSE);
677 cam_periph_unhold(periph);
678 cam_periph_unlock(periph);
679 cam_periph_release(periph);
685 * Actually translate the requested transfer into one the physical driver
686 * can understand. The transfer is described by a buf and will include
687 * only one physical transfer.
690 sastrategy(struct bio *bp)
692 struct cam_periph *periph;
693 struct sa_softc *softc;
695 bp->bio_resid = bp->bio_bcount;
696 if (SA_IS_CTRL(bp->bio_dev)) {
697 biofinish(bp, NULL, EINVAL);
700 periph = (struct cam_periph *)bp->bio_dev->si_drv1;
701 if (periph == NULL) {
702 biofinish(bp, NULL, ENXIO);
705 cam_periph_lock(periph);
707 softc = (struct sa_softc *)periph->softc;
709 if (softc->flags & SA_FLAG_INVALID) {
710 cam_periph_unlock(periph);
711 biofinish(bp, NULL, ENXIO);
715 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
716 cam_periph_unlock(periph);
717 biofinish(bp, NULL, EPERM);
722 * This should actually never occur as the write(2)
723 * system call traps attempts to write to a read-only
726 if (bp->bio_cmd == BIO_WRITE && softc->open_rdonly) {
727 cam_periph_unlock(periph);
728 biofinish(bp, NULL, EBADF);
732 if (softc->open_pending_mount) {
733 int error = samount(periph, 0, bp->bio_dev);
735 cam_periph_unlock(periph);
736 biofinish(bp, NULL, ENXIO);
739 saprevent(periph, PR_PREVENT);
740 softc->open_pending_mount = 0;
745 * If it's a null transfer, return immediately
747 if (bp->bio_bcount == 0) {
748 cam_periph_unlock(periph);
754 if (softc->flags & SA_FLAG_FIXED) {
756 * Fixed block device. The byte count must
757 * be a multiple of our block size.
759 if (((softc->blk_mask != ~0) &&
760 ((bp->bio_bcount & softc->blk_mask) != 0)) ||
761 ((softc->blk_mask == ~0) &&
762 ((bp->bio_bcount % softc->min_blk) != 0))) {
763 xpt_print(periph->path, "Invalid request. Fixed block "
764 "device requests must be a multiple of %d bytes\n",
766 cam_periph_unlock(periph);
767 biofinish(bp, NULL, EINVAL);
770 } else if ((bp->bio_bcount > softc->max_blk) ||
771 (bp->bio_bcount < softc->min_blk) ||
772 (bp->bio_bcount & softc->blk_mask) != 0) {
774 xpt_print_path(periph->path);
775 printf("Invalid request. Variable block "
776 "device requests must be ");
777 if (softc->blk_mask != 0) {
778 printf("a multiple of %d ", (0x1 << softc->blk_gran));
780 printf("between %d and %d bytes\n", softc->min_blk,
782 cam_periph_unlock(periph);
783 biofinish(bp, NULL, EINVAL);
788 * Place it at the end of the queue.
790 bioq_insert_tail(&softc->bio_queue, bp);
791 softc->queue_count++;
793 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
794 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
795 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
796 (bp->bio_cmd == BIO_READ)? "read" : "write"));
798 if (softc->queue_count > 1) {
799 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
800 ("sastrategy: queue count now %d\n", softc->queue_count));
804 * Schedule ourselves for performing the work.
806 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
807 cam_periph_unlock(periph);
813 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
814 if (softc->open_pending_mount) { \
815 error = samount(periph, 0, dev); \
819 saprevent(periph, PR_PREVENT); \
820 softc->open_pending_mount = 0; \
824 saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
826 struct cam_periph *periph;
827 struct sa_softc *softc;
828 scsi_space_code spaceop;
829 int didlockperiph = 0;
834 error = 0; /* shut up gcc */
835 spaceop = 0; /* shut up gcc */
837 periph = (struct cam_periph *)dev->si_drv1;
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, PRIBIO|PCATCH);
870 cam_periph_unlock(periph);
879 struct mtop *mt = (struct mtop *) arg;
882 * Check to make sure it's an OP we can perform
883 * with no media inserted.
899 case MTIOCSETEOTMODEL:
901 * We need to acquire the peripheral here rather
902 * than at open time because we are sharing writable
903 * access to data structures.
905 error = cam_periph_hold(periph, PRIBIO|PCATCH);
907 cam_periph_unlock(periph);
914 cam_periph_unlock(periph);
920 * Find the device that the user is talking about
925 struct mtget *g = (struct mtget *)arg;
928 * If this isn't the control mode device, actually go out
929 * and ask the drive again what it's set to.
931 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) {
932 u_int8_t write_protect;
933 int comp_enabled, comp_supported;
934 error = sagetparams(periph, SA_PARAM_ALL,
935 &softc->media_blksize, &softc->media_density,
936 &softc->media_numblks, &softc->buffer_mode,
937 &write_protect, &softc->speed, &comp_supported,
938 &comp_enabled, &softc->comp_algorithm, NULL);
942 softc->flags |= SA_FLAG_TAPE_WP;
944 softc->flags &= ~SA_FLAG_TAPE_WP;
945 softc->flags &= ~(SA_FLAG_COMP_SUPP|
946 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
947 if (comp_supported) {
948 if (softc->saved_comp_algorithm == 0)
949 softc->saved_comp_algorithm =
950 softc->comp_algorithm;
951 softc->flags |= SA_FLAG_COMP_SUPP;
953 softc->flags |= SA_FLAG_COMP_ENABLED;
955 softc->flags |= SA_FLAG_COMP_UNSUPP;
957 bzero(g, sizeof(struct mtget));
958 g->mt_type = MT_ISAR;
959 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
960 g->mt_comp = MT_COMP_UNSUPP;
961 g->mt_comp0 = MT_COMP_UNSUPP;
962 g->mt_comp1 = MT_COMP_UNSUPP;
963 g->mt_comp2 = MT_COMP_UNSUPP;
964 g->mt_comp3 = MT_COMP_UNSUPP;
966 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
967 g->mt_comp = MT_COMP_DISABLED;
969 g->mt_comp = softc->comp_algorithm;
971 g->mt_comp0 = softc->comp_algorithm;
972 g->mt_comp1 = softc->comp_algorithm;
973 g->mt_comp2 = softc->comp_algorithm;
974 g->mt_comp3 = softc->comp_algorithm;
976 g->mt_density = softc->media_density;
977 g->mt_density0 = softc->media_density;
978 g->mt_density1 = softc->media_density;
979 g->mt_density2 = softc->media_density;
980 g->mt_density3 = softc->media_density;
981 g->mt_blksiz = softc->media_blksize;
982 g->mt_blksiz0 = softc->media_blksize;
983 g->mt_blksiz1 = softc->media_blksize;
984 g->mt_blksiz2 = softc->media_blksize;
985 g->mt_blksiz3 = softc->media_blksize;
986 g->mt_fileno = softc->fileno;
987 g->mt_blkno = softc->blkno;
988 g->mt_dsreg = (short) softc->dsreg;
990 * Yes, we know that this is likely to overflow
992 if (softc->last_resid_was_io) {
993 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
994 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
995 softc->last_io_resid = 0;
999 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
1000 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1001 softc->last_ctl_resid = 0;
1010 struct scsi_tape_errors *sep =
1011 &((union mterrstat *)arg)->scsi_errstat;
1013 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1014 ("saioctl: MTIOCERRSTAT\n"));
1016 bzero(sep, sizeof(*sep));
1017 sep->io_resid = softc->last_io_resid;
1018 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1019 sizeof (sep->io_sense));
1020 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1021 sizeof (sep->io_cdb));
1022 sep->ctl_resid = softc->last_ctl_resid;
1023 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1024 sizeof (sep->ctl_sense));
1025 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1026 sizeof (sep->ctl_cdb));
1028 if ((SA_IS_CTRL(dev) == 0 && softc->open_pending_mount) ||
1030 bzero((caddr_t) &softc->errinfo,
1031 sizeof (softc->errinfo));
1040 PENDING_MOUNT_CHECK(softc, periph, dev);
1042 mt = (struct mtop *)arg;
1045 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1046 ("saioctl: op=0x%x count=0x%x\n",
1047 mt->mt_op, mt->mt_count));
1049 count = mt->mt_count;
1050 switch (mt->mt_op) {
1051 case MTWEOF: /* write an end-of-file marker */
1053 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1054 * flag because by keeping track of filemarks
1055 * we have last written we know ehether or not
1056 * we need to write more when we close the device.
1058 error = sawritefilemarks(periph, count, FALSE);
1060 case MTWSS: /* write a setmark */
1061 error = sawritefilemarks(periph, count, TRUE);
1063 case MTBSR: /* backward space record */
1064 case MTFSR: /* forward space record */
1065 case MTBSF: /* backward space file */
1066 case MTFSF: /* forward space file */
1067 case MTBSS: /* backward space setmark */
1068 case MTFSS: /* forward space setmark */
1069 case MTEOD: /* space to end of recorded medium */
1073 spaceop = SS_FILEMARKS;
1074 nmarks = softc->filemarks;
1075 error = sacheckeod(periph);
1077 xpt_print(periph->path,
1078 "EOD check prior to spacing failed\n");
1079 softc->flags |= SA_FLAG_EIO_PENDING;
1082 nmarks -= softc->filemarks;
1088 spaceop = SS_BLOCKS;
1099 spaceop = SS_SETMARKS;
1113 nmarks = softc->filemarks;
1115 * XXX: Why are we checking again?
1117 error = sacheckeod(periph);
1120 nmarks -= softc->filemarks;
1121 error = saspace(periph, count - nmarks, spaceop);
1123 * At this point, clear that we've written the tape
1124 * and that we've written any filemarks. We really
1125 * don't know what the applications wishes to do next-
1126 * the sacheckeod's will make sure we terminated the
1127 * tape correctly if we'd been writing, but the next
1128 * action the user application takes will set again
1129 * whether we need to write filemarks.
1132 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1133 softc->filemarks = 0;
1136 case MTREW: /* rewind */
1137 PENDING_MOUNT_CHECK(softc, periph, dev);
1138 (void) sacheckeod(periph);
1139 error = sarewind(periph);
1142 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1143 softc->flags &= ~SA_FLAG_ERR_PENDING;
1144 softc->filemarks = 0;
1146 case MTERASE: /* erase */
1147 PENDING_MOUNT_CHECK(softc, periph, dev);
1148 error = saerase(periph, count);
1150 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1151 softc->flags &= ~SA_FLAG_ERR_PENDING;
1153 case MTRETENS: /* re-tension tape */
1154 PENDING_MOUNT_CHECK(softc, periph, dev);
1155 error = saretension(periph);
1157 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1158 softc->flags &= ~SA_FLAG_ERR_PENDING;
1160 case MTOFFL: /* rewind and put the drive offline */
1162 PENDING_MOUNT_CHECK(softc, periph, dev);
1164 (void) sacheckeod(periph);
1166 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1167 softc->filemarks = 0;
1169 error = sarewind(periph);
1170 /* clear the frozen flag anyway */
1171 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1174 * Be sure to allow media removal before ejecting.
1177 saprevent(periph, PR_ALLOW);
1179 error = saloadunload(periph, FALSE);
1181 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1186 case MTNOP: /* no operation, sets status only */
1187 case MTCACHE: /* enable controller cache */
1188 case MTNOCACHE: /* disable controller cache */
1192 case MTSETBSIZ: /* Set block size for device */
1194 PENDING_MOUNT_CHECK(softc, periph, dev);
1196 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1199 softc->last_media_blksize =
1200 softc->media_blksize;
1201 softc->media_blksize = count;
1203 softc->flags |= SA_FLAG_FIXED;
1204 if (powerof2(count)) {
1207 softc->blk_mask = count - 1;
1209 softc->blk_mask = ~0;
1210 softc->blk_shift = 0;
1213 * Make the user's desire 'persistent'.
1215 softc->quirks &= ~SA_QUIRK_VARIABLE;
1216 softc->quirks |= SA_QUIRK_FIXED;
1218 softc->flags &= ~SA_FLAG_FIXED;
1219 if (softc->max_blk == 0) {
1220 softc->max_blk = ~0;
1222 softc->blk_shift = 0;
1223 if (softc->blk_gran != 0) {
1225 softc->blk_gran - 1;
1227 softc->blk_mask = 0;
1230 * Make the user's desire 'persistent'.
1232 softc->quirks |= SA_QUIRK_VARIABLE;
1233 softc->quirks &= ~SA_QUIRK_FIXED;
1237 case MTSETDNSTY: /* Set density for device and mode */
1238 PENDING_MOUNT_CHECK(softc, periph, dev);
1240 if (count > UCHAR_MAX) {
1244 error = sasetparams(periph, SA_PARAM_DENSITY,
1248 case MTCOMP: /* enable compression */
1249 PENDING_MOUNT_CHECK(softc, periph, dev);
1251 * Some devices don't support compression, and
1252 * don't like it if you ask them for the
1255 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1256 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1260 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1261 0, 0, count, SF_NO_PRINT);
1273 PENDING_MOUNT_CHECK(softc, periph, dev);
1274 error = sardpos(periph, 0, (u_int32_t *) arg);
1277 PENDING_MOUNT_CHECK(softc, periph, dev);
1278 error = sardpos(periph, 1, (u_int32_t *) arg);
1281 PENDING_MOUNT_CHECK(softc, periph, dev);
1282 error = sasetpos(periph, 0, (u_int32_t *) arg);
1285 PENDING_MOUNT_CHECK(softc, periph, dev);
1286 error = sasetpos(periph, 1, (u_int32_t *) arg);
1288 case MTIOCGETEOTMODEL:
1290 if (softc->quirks & SA_QUIRK_1FM)
1294 *((u_int32_t *) arg) = mode;
1296 case MTIOCSETEOTMODEL:
1298 switch (*((u_int32_t *) arg)) {
1300 softc->quirks &= ~SA_QUIRK_2FM;
1301 softc->quirks |= SA_QUIRK_1FM;
1304 softc->quirks &= ~SA_QUIRK_1FM;
1305 softc->quirks |= SA_QUIRK_2FM;
1313 error = cam_periph_ioctl(periph, cmd, arg, saerror);
1318 * Check to see if we cleared a frozen state
1320 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1326 softc->fileno = (daddr_t) -1;
1327 softc->blkno = (daddr_t) -1;
1328 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1329 xpt_print(periph->path,
1330 "tape state now unfrozen.\n");
1336 if (didlockperiph) {
1337 cam_periph_unhold(periph);
1339 cam_periph_unlock(periph);
1349 * Install a global async callback.
1351 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL);
1353 if (status != CAM_REQ_CMP) {
1354 printf("sa: Failed to attach master async callback "
1355 "due to status 0x%x!\n", status);
1360 saoninvalidate(struct cam_periph *periph)
1362 struct sa_softc *softc;
1364 softc = (struct sa_softc *)periph->softc;
1367 * De-register any async callbacks.
1369 xpt_register_async(0, saasync, periph, periph->path);
1371 softc->flags |= SA_FLAG_INVALID;
1374 * Return all queued I/O with ENXIO.
1375 * XXX Handle any transactions queued to the card
1376 * with XPT_ABORT_CCB.
1378 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1379 softc->queue_count = 0;
1381 xpt_print(periph->path, "lost device\n");
1386 sacleanup(struct cam_periph *periph)
1388 struct sa_softc *softc;
1391 softc = (struct sa_softc *)periph->softc;
1393 xpt_print(periph->path, "removing device entry\n");
1394 devstat_remove_entry(softc->device_stats);
1395 cam_periph_unlock(periph);
1396 destroy_dev(softc->devs.ctl_dev);
1397 for (i = 0; i < SA_NUM_MODES; i++) {
1398 destroy_dev(softc->devs.mode_devs[i].r_dev);
1399 destroy_dev(softc->devs.mode_devs[i].nr_dev);
1400 destroy_dev(softc->devs.mode_devs[i].er_dev);
1402 cam_periph_lock(periph);
1403 free(softc, M_SCSISA);
1407 saasync(void *callback_arg, u_int32_t code,
1408 struct cam_path *path, void *arg)
1410 struct cam_periph *periph;
1412 periph = (struct cam_periph *)callback_arg;
1414 case AC_FOUND_DEVICE:
1416 struct ccb_getdev *cgd;
1419 cgd = (struct ccb_getdev *)arg;
1423 if (cgd->protocol != PROTO_SCSI)
1426 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1430 * Allocate a peripheral instance for
1431 * this device and start the probe
1434 status = cam_periph_alloc(saregister, saoninvalidate,
1436 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1437 saasync, AC_FOUND_DEVICE, cgd);
1439 if (status != CAM_REQ_CMP
1440 && status != CAM_REQ_INPROG)
1441 printf("saasync: Unable to probe new device "
1442 "due to status 0x%x\n", status);
1446 cam_periph_async(periph, code, path, arg);
1452 saregister(struct cam_periph *periph, void *arg)
1454 struct sa_softc *softc;
1455 struct ccb_getdev *cgd;
1456 struct ccb_pathinq cpi;
1460 cgd = (struct ccb_getdev *)arg;
1462 printf("saregister: no getdev CCB, can't register device\n");
1463 return (CAM_REQ_CMP_ERR);
1466 softc = (struct sa_softc *)
1467 malloc(sizeof (*softc), M_SCSISA, M_NOWAIT | M_ZERO);
1468 if (softc == NULL) {
1469 printf("saregister: Unable to probe new device. "
1470 "Unable to allocate softc\n");
1471 return (CAM_REQ_CMP_ERR);
1473 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1474 softc->state = SA_STATE_NORMAL;
1475 softc->fileno = (daddr_t) -1;
1476 softc->blkno = (daddr_t) -1;
1478 bioq_init(&softc->bio_queue);
1479 periph->softc = softc;
1482 * See if this device has any quirks.
1484 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1485 (caddr_t)sa_quirk_table,
1486 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1487 sizeof(*sa_quirk_table), scsi_inquiry_match);
1489 if (match != NULL) {
1490 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1491 softc->last_media_blksize =
1492 ((struct sa_quirk_entry *)match)->prefblk;
1494 softc->quirks = SA_QUIRK_NONE;
1496 bzero(&cpi, sizeof(cpi));
1497 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1498 cpi.ccb_h.func_code = XPT_PATH_INQ;
1499 xpt_action((union ccb *)&cpi);
1502 * The SA driver supports a blocksize, but we don't know the
1503 * blocksize until we media is inserted. So, set a flag to
1504 * indicate that the blocksize is unavailable right now.
1506 cam_periph_unlock(periph);
1507 softc->device_stats = devstat_new_entry("sa", periph->unit_number, 0,
1508 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1509 XPORT_DEVSTAT_TYPE(cpi.transport), DEVSTAT_PRIORITY_TAPE);
1512 * If maxio isn't set, we fall back to DFLTPHYS. If it is set, we
1513 * take it whether or not it's larger than MAXPHYS. physio will
1514 * break it down into pieces small enough to fit in a buffer.
1517 softc->maxio = DFLTPHYS;
1519 softc->maxio = cpi.maxio;
1522 * If the SIM supports unmapped I/O, let physio know that we can
1523 * handle unmapped buffers.
1525 if (cpi.hba_misc & PIM_UNMAPPED)
1526 softc->si_flags = SI_UNMAPPED;
1528 softc->devs.ctl_dev = make_dev(&sa_cdevsw, SAMINOR(SA_CTLDEV,
1529 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1530 0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1531 softc->devs.ctl_dev->si_drv1 = periph;
1532 softc->devs.ctl_dev->si_iosize_max = softc->maxio;
1533 softc->devs.ctl_dev->si_flags |= softc->si_flags;
1535 for (i = 0; i < SA_NUM_MODES; i++) {
1537 softc->devs.mode_devs[i].r_dev = make_dev(&sa_cdevsw,
1538 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_R),
1539 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1540 periph->periph_name, periph->unit_number, i);
1541 softc->devs.mode_devs[i].r_dev->si_drv1 = periph;
1542 softc->devs.mode_devs[i].r_dev->si_iosize_max = softc->maxio;
1543 softc->devs.mode_devs[i].r_dev->si_flags |= softc->si_flags;
1545 softc->devs.mode_devs[i].nr_dev = make_dev(&sa_cdevsw,
1546 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_NR),
1547 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1548 periph->periph_name, periph->unit_number, i);
1549 softc->devs.mode_devs[i].nr_dev->si_drv1 = periph;
1550 softc->devs.mode_devs[i].nr_dev->si_iosize_max = softc->maxio;
1551 softc->devs.mode_devs[i].nr_dev->si_flags |= softc->si_flags;
1553 softc->devs.mode_devs[i].er_dev = make_dev(&sa_cdevsw,
1554 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_ER),
1555 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1556 periph->periph_name, periph->unit_number, i);
1557 softc->devs.mode_devs[i].er_dev->si_drv1 = periph;
1558 softc->devs.mode_devs[i].er_dev->si_iosize_max = softc->maxio;
1559 softc->devs.mode_devs[i].er_dev->si_flags |= softc->si_flags;
1562 * Make the (well known) aliases for the first mode.
1567 alias = make_dev_alias(softc->devs.mode_devs[i].r_dev,
1568 "%s%d", periph->periph_name, periph->unit_number);
1569 alias->si_drv1 = periph;
1570 alias->si_iosize_max = softc->maxio;
1571 alias->si_flags |= softc->si_flags;
1573 alias = make_dev_alias(softc->devs.mode_devs[i].nr_dev,
1574 "n%s%d", periph->periph_name, periph->unit_number);
1575 alias->si_drv1 = periph;
1576 alias->si_iosize_max = softc->maxio;
1577 alias->si_flags |= softc->si_flags;
1579 alias = make_dev_alias(softc->devs.mode_devs[i].er_dev,
1580 "e%s%d", periph->periph_name, periph->unit_number);
1581 alias->si_drv1 = periph;
1582 alias->si_iosize_max = softc->maxio;
1583 alias->si_flags |= softc->si_flags;
1586 cam_periph_lock(periph);
1589 * Add an async callback so that we get
1590 * notified if this device goes away.
1592 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path);
1594 xpt_announce_periph(periph, NULL);
1595 xpt_announce_quirks(periph, softc->quirks, SA_QUIRK_BIT_STRING);
1597 return (CAM_REQ_CMP);
1601 sastart(struct cam_periph *periph, union ccb *start_ccb)
1603 struct sa_softc *softc;
1605 softc = (struct sa_softc *)periph->softc;
1607 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
1610 switch (softc->state) {
1611 case SA_STATE_NORMAL:
1613 /* Pull a buffer from the queue and get going on it */
1617 * See if there is a buf with work for us to do..
1619 bp = bioq_first(&softc->bio_queue);
1620 if (periph->immediate_priority <= periph->pinfo.priority) {
1621 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1622 ("queuing for immediate ccb\n"));
1623 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1624 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1626 periph->immediate_priority = CAM_PRIORITY_NONE;
1627 wakeup(&periph->ccb_list);
1628 } else if (bp == NULL) {
1629 xpt_release_ccb(start_ccb);
1630 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1631 struct bio *done_bp;
1633 softc->queue_count--;
1634 bioq_remove(&softc->bio_queue, bp);
1635 bp->bio_resid = bp->bio_bcount;
1637 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1639 * We now just clear errors in this case
1640 * and let the residual be the notifier.
1643 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1645 * This can only happen if we're reading
1646 * in fixed length mode. In this case,
1647 * we dump the rest of the list the
1651 if (bioq_first(&softc->bio_queue) != NULL) {
1655 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1656 bp->bio_error = EIO;
1657 bp->bio_flags |= BIO_ERROR;
1659 bp = bioq_first(&softc->bio_queue);
1661 * Only if we have no other buffers queued up
1662 * do we clear the pending error flag.
1665 softc->flags &= ~SA_FLAG_ERR_PENDING;
1666 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1667 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
1668 "%d more buffers queued up\n",
1669 (softc->flags & SA_FLAG_ERR_PENDING),
1670 (bp != NULL)? "not " : " ", softc->queue_count));
1671 xpt_release_ccb(start_ccb);
1676 bioq_remove(&softc->bio_queue, bp);
1677 softc->queue_count--;
1679 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1680 if (softc->blk_shift != 0) {
1682 bp->bio_bcount >> softc->blk_shift;
1683 } else if (softc->media_blksize != 0) {
1684 length = bp->bio_bcount /
1685 softc->media_blksize;
1687 bp->bio_error = EIO;
1688 xpt_print(periph->path, "zero blocksize"
1689 " for FIXED length writes?\n");
1694 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1695 ("issuing a %d fixed record %s\n",
1696 length, (bp->bio_cmd == BIO_READ)? "read" :
1700 length = bp->bio_bcount;
1702 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1703 ("issuing a %d variable byte %s\n",
1704 length, (bp->bio_cmd == BIO_READ)? "read" :
1708 devstat_start_transaction_bio(softc->device_stats, bp);
1710 * Some people have theorized that we should
1711 * suppress illegal length indication if we are
1712 * running in variable block mode so that we don't
1713 * have to request sense every time our requested
1714 * block size is larger than the written block.
1715 * The residual information from the ccb allows
1716 * us to identify this situation anyway. The only
1717 * problem with this is that we will not get
1718 * information about blocks that are larger than
1719 * our read buffer unless we set the block size
1720 * in the mode page to something other than 0.
1722 * I believe that this is a non-issue. If user apps
1723 * don't adjust their read size to match our record
1724 * size, that's just life. Anyway, the typical usage
1725 * would be to issue, e.g., 64KB reads and occasionally
1726 * have to do deal with 512 byte or 1KB intermediate
1729 softc->dsreg = (bp->bio_cmd == BIO_READ)?
1730 MTIO_DSREG_RD : MTIO_DSREG_WR;
1731 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1732 MSG_SIMPLE_Q_TAG, (bp->bio_cmd == BIO_READ ?
1733 SCSI_RW_READ : SCSI_RW_WRITE) |
1734 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
1735 SCSI_RW_BIO : 0), FALSE,
1736 (softc->flags & SA_FLAG_FIXED) != 0, length,
1737 (bp->bio_flags & BIO_UNMAPPED) != 0 ? (void *)bp :
1738 bp->bio_data, bp->bio_bcount, SSD_FULL_SIZE,
1740 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1741 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1742 start_ccb->ccb_h.ccb_bp = bp;
1743 bp = bioq_first(&softc->bio_queue);
1744 xpt_action(start_ccb);
1748 /* Have more work to do, so ensure we stay scheduled */
1749 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1753 case SA_STATE_ABNORMAL:
1755 panic("state 0x%x in sastart", softc->state);
1762 sadone(struct cam_periph *periph, union ccb *done_ccb)
1764 struct sa_softc *softc;
1765 struct ccb_scsiio *csio;
1767 softc = (struct sa_softc *)periph->softc;
1768 csio = &done_ccb->csio;
1769 switch (CCB_Type(csio)) {
1770 case SA_CCB_BUFFER_IO:
1775 softc->dsreg = MTIO_DSREG_REST;
1776 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1778 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1779 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1781 * A retry was scheduled, so just return.
1790 * Catastrophic error. Mark the tape as frozen
1791 * (we no longer know tape position).
1793 * Return all queued I/O with EIO, and unfreeze
1794 * our queue so that future transactions that
1795 * attempt to fix this problem can get to the
1800 softc->flags |= SA_FLAG_TAPE_FROZEN;
1801 bioq_flush(&softc->bio_queue, NULL, EIO);
1804 bp->bio_resid = bp->bio_bcount;
1805 bp->bio_error = error;
1806 bp->bio_flags |= BIO_ERROR;
1808 * In the error case, position is updated in saerror.
1811 bp->bio_resid = csio->resid;
1813 if (csio->resid != 0) {
1814 bp->bio_flags |= BIO_ERROR;
1816 if (bp->bio_cmd == BIO_WRITE) {
1817 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1818 softc->filemarks = 0;
1820 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1821 (softc->blkno != (daddr_t) -1)) {
1822 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1824 if (softc->blk_shift != 0) {
1825 l = bp->bio_bcount >>
1828 l = bp->bio_bcount /
1829 softc->media_blksize;
1831 softc->blkno += (daddr_t) l;
1838 * If we had an error (immediate or pending),
1839 * release the device queue now.
1841 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1842 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1843 if (error || bp->bio_resid) {
1844 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1845 ("error %d resid %ld count %ld\n", error,
1846 bp->bio_resid, bp->bio_bcount));
1848 biofinish(bp, softc->device_stats, 0);
1851 case SA_CCB_WAITING:
1853 /* Caller will release the CCB */
1854 wakeup(&done_ccb->ccb_h.cbfcnp);
1858 xpt_release_ccb(done_ccb);
1862 * Mount the tape (make sure it's ready for I/O).
1865 samount(struct cam_periph *periph, int oflags, struct cdev *dev)
1867 struct sa_softc *softc;
1872 * oflags can be checked for 'kind' of open (read-only check) - later
1873 * dev can be checked for a control-mode or compression open - later
1875 UNUSED_PARAMETER(oflags);
1876 UNUSED_PARAMETER(dev);
1879 softc = (struct sa_softc *)periph->softc;
1882 * This should determine if something has happend since the last
1883 * open/mount that would invalidate the mount. We do *not* want
1884 * to retry this command- we just want the status. But we only
1885 * do this if we're mounted already- if we're not mounted,
1886 * we don't care about the unit read state and can instead use
1887 * this opportunity to attempt to reserve the tape unit.
1890 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
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);
1896 if (error == ENXIO) {
1897 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1898 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1899 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1900 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1901 softc->device_stats);
1904 * We don't need to freeze the tape because we
1905 * will now attempt to rewind/load it.
1907 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1908 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
1909 xpt_print(periph->path,
1910 "error %d on TUR in samount\n", error);
1914 error = sareservereleaseunit(periph, TRUE);
1918 ccb = cam_periph_getccb(periph, 1);
1919 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1920 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1921 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1922 softc->device_stats);
1925 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1926 struct scsi_read_block_limits_data *rblim = NULL;
1927 int comp_enabled, comp_supported;
1928 u_int8_t write_protect, guessing = 0;
1931 * Clear out old state.
1933 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1934 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1935 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1936 softc->filemarks = 0;
1939 * *Very* first off, make sure we're loaded to BOT.
1941 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1942 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1943 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1944 softc->device_stats);
1947 * In case this doesn't work, do a REWIND instead
1950 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1951 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1952 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1953 softc->device_stats);
1956 xpt_release_ccb(ccb);
1961 * Do a dummy test read to force access to the
1962 * media so that the drive will really know what's
1963 * there. We actually don't really care what the
1964 * blocksize on tape is and don't expect to really
1965 * read a full record.
1967 rblim = (struct scsi_read_block_limits_data *)
1968 malloc(8192, M_SCSISA, M_NOWAIT);
1969 if (rblim == NULL) {
1970 xpt_print(periph->path, "no memory for test read\n");
1971 xpt_release_ccb(ccb);
1976 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1977 scsi_sa_read_write(&ccb->csio, 0, sadone,
1978 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1979 (void *) rblim, 8192, SSD_FULL_SIZE,
1981 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1982 softc->device_stats);
1983 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1984 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1985 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1986 SF_NO_PRINT | SF_RETRY_UA,
1987 softc->device_stats);
1989 xpt_print(periph->path,
1990 "unable to rewind after test read\n");
1991 xpt_release_ccb(ccb);
1997 * Next off, determine block limits.
1999 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2000 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2002 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
2003 SF_NO_PRINT | SF_RETRY_UA, softc->device_stats);
2005 xpt_release_ccb(ccb);
2009 * If it's less than SCSI-2, READ BLOCK LIMITS is not
2010 * a MANDATORY command. Anyway- it doesn't matter-
2011 * we can proceed anyway.
2013 softc->blk_gran = 0;
2014 softc->max_blk = ~0;
2017 if (softc->scsi_rev >= SCSI_REV_SPC) {
2018 softc->blk_gran = RBL_GRAN(rblim);
2020 softc->blk_gran = 0;
2023 * We take max_blk == min_blk to mean a default to
2024 * fixed mode- but note that whatever we get out of
2025 * sagetparams below will actually determine whether
2026 * we are actually *in* fixed mode.
2028 softc->max_blk = scsi_3btoul(rblim->maximum);
2029 softc->min_blk = scsi_2btoul(rblim->minimum);
2034 * Next, perform a mode sense to determine
2035 * current density, blocksize, compression etc.
2037 error = sagetparams(periph, SA_PARAM_ALL,
2038 &softc->media_blksize,
2039 &softc->media_density,
2040 &softc->media_numblks,
2041 &softc->buffer_mode, &write_protect,
2042 &softc->speed, &comp_supported,
2043 &comp_enabled, &softc->comp_algorithm,
2048 * We could work a little harder here. We could
2049 * adjust our attempts to get information. It
2050 * might be an ancient tape drive. If someone
2051 * nudges us, we'll do that.
2057 * If no quirk has determined that this is a device that is
2058 * preferred to be in fixed or variable mode, now is the time
2061 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2064 * This could be expensive to find out. Luckily we
2065 * only need to do this once. If we start out in
2066 * 'default' mode, try and set ourselves to one
2067 * of the densities that would determine a wad
2068 * of other stuff. Go from highest to lowest.
2070 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2072 static u_int8_t ctry[] = {
2073 SCSI_DENSITY_HALFINCH_PE,
2074 SCSI_DENSITY_HALFINCH_6250C,
2075 SCSI_DENSITY_HALFINCH_6250,
2076 SCSI_DENSITY_HALFINCH_1600,
2077 SCSI_DENSITY_HALFINCH_800,
2078 SCSI_DENSITY_QIC_4GB,
2079 SCSI_DENSITY_QIC_2GB,
2080 SCSI_DENSITY_QIC_525_320,
2081 SCSI_DENSITY_QIC_150,
2082 SCSI_DENSITY_QIC_120,
2083 SCSI_DENSITY_QIC_24,
2084 SCSI_DENSITY_QIC_11_9TRK,
2085 SCSI_DENSITY_QIC_11_4TRK,
2086 SCSI_DENSITY_QIC_1320,
2087 SCSI_DENSITY_QIC_3080,
2090 for (i = 0; ctry[i]; i++) {
2091 error = sasetparams(periph,
2092 SA_PARAM_DENSITY, 0, ctry[i],
2095 softc->media_density = ctry[i];
2100 switch (softc->media_density) {
2101 case SCSI_DENSITY_QIC_11_4TRK:
2102 case SCSI_DENSITY_QIC_11_9TRK:
2103 case SCSI_DENSITY_QIC_24:
2104 case SCSI_DENSITY_QIC_120:
2105 case SCSI_DENSITY_QIC_150:
2106 case SCSI_DENSITY_QIC_525_320:
2107 case SCSI_DENSITY_QIC_1320:
2108 case SCSI_DENSITY_QIC_3080:
2109 softc->quirks &= ~SA_QUIRK_2FM;
2110 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2111 softc->last_media_blksize = 512;
2113 case SCSI_DENSITY_QIC_4GB:
2114 case SCSI_DENSITY_QIC_2GB:
2115 softc->quirks &= ~SA_QUIRK_2FM;
2116 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2117 softc->last_media_blksize = 1024;
2120 softc->last_media_blksize =
2121 softc->media_blksize;
2122 softc->quirks |= SA_QUIRK_VARIABLE;
2128 * If no quirk has determined that this is a device that needs
2129 * to have 2 Filemarks at EOD, now is the time to find out.
2132 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2133 switch (softc->media_density) {
2134 case SCSI_DENSITY_HALFINCH_800:
2135 case SCSI_DENSITY_HALFINCH_1600:
2136 case SCSI_DENSITY_HALFINCH_6250:
2137 case SCSI_DENSITY_HALFINCH_6250C:
2138 case SCSI_DENSITY_HALFINCH_PE:
2139 softc->quirks &= ~SA_QUIRK_1FM;
2140 softc->quirks |= SA_QUIRK_2FM;
2148 * Now validate that some info we got makes sense.
2150 if ((softc->max_blk < softc->media_blksize) ||
2151 (softc->min_blk > softc->media_blksize &&
2152 softc->media_blksize)) {
2153 xpt_print(periph->path,
2154 "BLOCK LIMITS (%d..%d) could not match current "
2155 "block settings (%d)- adjusting\n", softc->min_blk,
2156 softc->max_blk, softc->media_blksize);
2157 softc->max_blk = softc->min_blk =
2158 softc->media_blksize;
2162 * Now put ourselves into the right frame of mind based
2167 * If we want to be in FIXED mode and our current blocksize
2168 * is not equal to our last blocksize (if nonzero), try and
2169 * set ourselves to this last blocksize (as the 'preferred'
2170 * block size). The initial quirkmatch at registry sets the
2171 * initial 'last' blocksize. If, for whatever reason, this
2172 * 'last' blocksize is zero, set the blocksize to 512,
2173 * or min_blk if that's larger.
2175 if ((softc->quirks & SA_QUIRK_FIXED) &&
2176 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2177 (softc->media_blksize != softc->last_media_blksize)) {
2178 softc->media_blksize = softc->last_media_blksize;
2179 if (softc->media_blksize == 0) {
2180 softc->media_blksize = 512;
2181 if (softc->media_blksize < softc->min_blk) {
2182 softc->media_blksize = softc->min_blk;
2185 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2186 softc->media_blksize, 0, 0, SF_NO_PRINT);
2188 xpt_print(periph->path,
2189 "unable to set fixed blocksize to %d\n",
2190 softc->media_blksize);
2195 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2196 (softc->media_blksize != 0)) {
2197 softc->last_media_blksize = softc->media_blksize;
2198 softc->media_blksize = 0;
2199 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2200 0, 0, 0, SF_NO_PRINT);
2203 * If this fails and we were guessing, just
2204 * assume that we got it wrong and go try
2205 * fixed block mode. Don't even check against
2206 * density code at this point.
2209 softc->quirks &= ~SA_QUIRK_VARIABLE;
2210 softc->quirks |= SA_QUIRK_FIXED;
2211 if (softc->last_media_blksize == 0)
2212 softc->last_media_blksize = 512;
2215 xpt_print(periph->path,
2216 "unable to set variable blocksize\n");
2222 * Now that we have the current block size,
2223 * set up some parameters for sastart's usage.
2225 if (softc->media_blksize) {
2226 softc->flags |= SA_FLAG_FIXED;
2227 if (powerof2(softc->media_blksize)) {
2229 ffs(softc->media_blksize) - 1;
2230 softc->blk_mask = softc->media_blksize - 1;
2232 softc->blk_mask = ~0;
2233 softc->blk_shift = 0;
2237 * The SCSI-3 spec allows 0 to mean "unspecified".
2238 * The SCSI-1 spec allows 0 to mean 'infinite'.
2240 * Either works here.
2242 if (softc->max_blk == 0) {
2243 softc->max_blk = ~0;
2245 softc->blk_shift = 0;
2246 if (softc->blk_gran != 0) {
2247 softc->blk_mask = softc->blk_gran - 1;
2249 softc->blk_mask = 0;
2254 softc->flags |= SA_FLAG_TAPE_WP;
2256 if (comp_supported) {
2257 if (softc->saved_comp_algorithm == 0)
2258 softc->saved_comp_algorithm =
2259 softc->comp_algorithm;
2260 softc->flags |= SA_FLAG_COMP_SUPP;
2262 softc->flags |= SA_FLAG_COMP_ENABLED;
2264 softc->flags |= SA_FLAG_COMP_UNSUPP;
2266 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2267 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2268 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2271 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2273 xpt_print(periph->path,
2274 "unable to set buffered mode\n");
2276 error = 0; /* not an error */
2281 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2285 free(rblim, M_SCSISA);
2288 softc->dsreg = MTIO_DSREG_NIL;
2290 softc->fileno = softc->blkno = 0;
2291 softc->dsreg = MTIO_DSREG_REST;
2293 #ifdef SA_1FM_AT_EOD
2294 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2295 softc->quirks |= SA_QUIRK_1FM;
2297 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2298 softc->quirks |= SA_QUIRK_2FM;
2301 xpt_release_ccb(ccb);
2304 * If we return an error, we're not mounted any more,
2305 * so release any device reservation.
2308 (void) sareservereleaseunit(periph, FALSE);
2311 * Clear I/O residual.
2313 softc->last_io_resid = 0;
2314 softc->last_ctl_resid = 0;
2320 * How many filemarks do we need to write if we were to terminate the
2321 * tape session right now? Note that this can be a negative number
2325 samarkswanted(struct cam_periph *periph)
2328 struct sa_softc *softc;
2330 softc = (struct sa_softc *)periph->softc;
2332 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2334 if (softc->quirks & SA_QUIRK_2FM)
2337 markswanted -= softc->filemarks;
2338 return (markswanted);
2342 sacheckeod(struct cam_periph *periph)
2347 markswanted = samarkswanted(periph);
2349 if (markswanted > 0) {
2350 error = sawritefilemarks(periph, markswanted, FALSE);
2358 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2360 static const char *toobig =
2361 "%d-byte tape record bigger than supplied buffer\n";
2362 struct cam_periph *periph;
2363 struct sa_softc *softc;
2364 struct ccb_scsiio *csio;
2365 struct scsi_sense_data *sense;
2369 int error_code, sense_key, asc, ascq, error, aqvalid, stream_valid;
2371 uint8_t stream_bits;
2373 periph = xpt_path_periph(ccb->ccb_h.path);
2374 softc = (struct sa_softc *)periph->softc;
2376 sense = &csio->sense_data;
2377 sense_len = csio->sense_len - csio->sense_resid;
2378 scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
2379 &asc, &ascq, /*show_errors*/ 1);
2380 if (asc != -1 && ascq != -1)
2384 if (scsi_get_stream_info(sense, sense_len, NULL, &stream_bits) == 0)
2390 status = csio->ccb_h.status & CAM_STATUS_MASK;
2393 * Calculate/latch up, any residuals... We do this in a funny 2-step
2394 * so we can print stuff here if we have CAM_DEBUG enabled for this
2397 if (status == CAM_SCSI_STATUS_ERROR) {
2398 if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &resid,
2400 if ((softc->flags & SA_FLAG_FIXED) != 0)
2401 resid *= softc->media_blksize;
2403 resid = csio->dxfer_len;
2405 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2406 if (softc->media_blksize)
2407 info /= softc->media_blksize;
2410 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2411 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2412 sizeof (struct scsi_sense_data));
2413 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2414 (int) csio->cdb_len);
2415 softc->last_io_resid = resid;
2416 softc->last_resid_was_io = 1;
2418 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2419 sizeof (struct scsi_sense_data));
2420 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2421 (int) csio->cdb_len);
2422 softc->last_ctl_resid = resid;
2423 softc->last_resid_was_io = 0;
2425 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2426 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %jd "
2427 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2428 sense_key, asc, ascq, status,
2429 (stream_valid) ? stream_bits : 0, (intmax_t)resid,
2432 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2433 ("Cam Status 0x%x\n", status));
2439 case CAM_SCSI_STATUS_ERROR:
2441 * If a read/write command, we handle it here.
2443 if (CCB_Type(csio) != SA_CCB_WAITING) {
2447 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2448 * on a non read/write command, we assume it's not an error
2449 * and propagate the residule and return.
2451 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2452 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2453 csio->resid = resid;
2458 * Otherwise, we let the common code handle this.
2460 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2464 * We cannot depend upon CAM honoring retry counts for these.
2466 case CAM_SCSI_BUS_RESET:
2468 if (ccb->ccb_h.retry_count <= 0) {
2473 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2477 * Handle filemark, end of tape, mismatched record sizes....
2478 * From this point out, we're only handling read/write cases.
2479 * Handle writes && reads differently.
2482 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2483 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2484 csio->resid = resid;
2486 } else if ((stream_valid != 0) && (stream_bits & SSD_EOM)) {
2487 softc->flags |= SA_FLAG_EOM_PENDING;
2489 * Grotesque as it seems, the few times
2490 * I've actually seen a non-zero resid,
2491 * the tape drive actually lied and had
2492 * written all the data!.
2497 csio->resid = resid;
2498 if (sense_key == SSD_KEY_BLANK_CHECK) {
2499 if (softc->quirks & SA_QUIRK_1FM) {
2501 softc->flags |= SA_FLAG_EOM_PENDING;
2505 } else if ((stream_valid != 0) && (stream_bits & SSD_FILEMARK)){
2506 if (softc->flags & SA_FLAG_FIXED) {
2508 softc->flags |= SA_FLAG_EOF_PENDING;
2511 * Unconditionally, if we detected a filemark on a read,
2512 * mark that we've run moved a file ahead.
2514 if (softc->fileno != (daddr_t) -1) {
2517 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2523 * Incorrect Length usually applies to read, but can apply to writes.
2525 if (error == 0 && (stream_valid != 0) && (stream_bits & SSD_ILI)) {
2527 xpt_print(csio->ccb_h.path, toobig,
2528 csio->dxfer_len - info);
2529 csio->resid = csio->dxfer_len;
2532 csio->resid = resid;
2533 if (softc->flags & SA_FLAG_FIXED) {
2534 softc->flags |= SA_FLAG_EIO_PENDING;
2537 * Bump the block number if we hadn't seen a filemark.
2538 * Do this independent of errors (we've moved anyway).
2540 if ((stream_valid == 0) ||
2541 (stream_bits & SSD_FILEMARK) == 0) {
2542 if (softc->blkno != (daddr_t) -1) {
2544 csio->ccb_h.ccb_pflags |=
2545 SA_POSITION_UPDATED;
2553 * Unfreeze the queue if frozen as we're not returning anything
2554 * to our waiters that would indicate an I/O error has occurred
2564 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2565 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2566 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2567 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2572 struct scsi_mode_header_6 *mode_hdr;
2573 struct scsi_mode_blk_desc *mode_blk;
2574 int mode_buffer_len;
2575 struct sa_softc *softc;
2580 softc = (struct sa_softc *)periph->softc;
2581 ccb = cam_periph_getccb(periph, 1);
2582 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2583 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2585 cpage = SA_DATA_COMPRESSION_PAGE;
2588 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2590 if (params_to_get & SA_PARAM_COMPRESSION) {
2591 if (softc->quirks & SA_QUIRK_NOCOMP) {
2592 *comp_supported = FALSE;
2593 params_to_get &= ~SA_PARAM_COMPRESSION;
2595 mode_buffer_len += sizeof (sa_comp_t);
2598 /* XXX Fix M_NOWAIT */
2599 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
2600 if (mode_buffer == NULL) {
2601 xpt_release_ccb(ccb);
2604 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2605 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2607 /* it is safe to retry this */
2608 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2609 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2610 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2611 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2613 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2614 softc->device_stats);
2616 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2618 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2620 * Hmm. Let's see if we can try another page...
2621 * If we've already done that, give up on compression
2622 * for this device and remember this for the future
2623 * and attempt the request without asking for compression
2626 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2627 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2630 softc->quirks |= SA_QUIRK_NOCOMP;
2631 free(mode_buffer, M_SCSISA);
2633 } else if (status == CAM_SCSI_STATUS_ERROR) {
2634 /* Tell the user about the fatal error. */
2635 scsi_sense_print(&ccb->csio);
2636 goto sagetparamsexit;
2640 * If the user only wants the compression information, and
2641 * the device doesn't send back the block descriptor, it's
2642 * no big deal. If the user wants more than just
2643 * compression, though, and the device doesn't pass back the
2644 * block descriptor, we need to send another mode sense to
2645 * get the block descriptor.
2647 if ((mode_hdr->blk_desc_len == 0) &&
2648 (params_to_get & SA_PARAM_COMPRESSION) &&
2649 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2652 * Decrease the mode buffer length by the size of
2653 * the compression page, to make sure the data
2654 * there doesn't get overwritten.
2656 mode_buffer_len -= sizeof (sa_comp_t);
2659 * Now move the compression page that we presumably
2660 * got back down the memory chunk a little bit so
2661 * it doesn't get spammed.
2663 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2664 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2667 * Now, we issue another mode sense and just ask
2668 * for the block descriptor, etc.
2671 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2672 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2673 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2676 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2677 softc->device_stats);
2680 goto sagetparamsexit;
2683 if (params_to_get & SA_PARAM_BLOCKSIZE)
2684 *blocksize = scsi_3btoul(mode_blk->blklen);
2686 if (params_to_get & SA_PARAM_NUMBLOCKS)
2687 *numblocks = scsi_3btoul(mode_blk->nblocks);
2689 if (params_to_get & SA_PARAM_BUFF_MODE)
2690 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2692 if (params_to_get & SA_PARAM_DENSITY)
2693 *density = mode_blk->density;
2695 if (params_to_get & SA_PARAM_WP)
2696 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2698 if (params_to_get & SA_PARAM_SPEED)
2699 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2701 if (params_to_get & SA_PARAM_COMPRESSION) {
2702 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2703 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2704 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2706 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2708 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2709 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2711 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2713 * We don't really know whether this device supports
2714 * Data Compression if the algorithm field is
2715 * zero. Just say we do.
2717 *comp_supported = TRUE;
2719 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2720 *comp_algorithm = cp->sel_comp_alg;
2723 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2726 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2728 char *xyz = mode_buffer;
2729 xpt_print_path(periph->path);
2730 printf("Mode Sense Data=");
2731 for (idx = 0; idx < mode_buffer_len; idx++)
2732 printf(" 0x%02x", xyz[idx] & 0xff);
2738 xpt_release_ccb(ccb);
2739 free(mode_buffer, M_SCSISA);
2744 * The purpose of this function is to set one of four different parameters
2748 * - compression / compression algorithm
2751 * The assumption is that this will be called from saioctl(), and therefore
2752 * from a process context. Thus the waiting malloc calls below. If that
2753 * assumption ever changes, the malloc calls should be changed to be
2756 * Any or all of the four parameters may be set when this function is
2757 * called. It should handle setting more than one parameter at once.
2760 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2761 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2762 u_int32_t sense_flags)
2764 struct sa_softc *softc;
2765 u_int32_t current_blocksize;
2766 u_int32_t current_calg;
2767 u_int8_t current_density;
2768 u_int8_t current_speed;
2769 int comp_enabled, comp_supported;
2771 int mode_buffer_len;
2772 struct scsi_mode_header_6 *mode_hdr;
2773 struct scsi_mode_blk_desc *mode_blk;
2774 sa_comp_t *ccomp, *cpage;
2776 union ccb *ccb = NULL;
2779 softc = (struct sa_softc *)periph->softc;
2781 ccomp = malloc(sizeof (sa_comp_t), M_SCSISA, M_NOWAIT);
2786 * Since it doesn't make sense to set the number of blocks, or
2787 * write protection, we won't try to get the current value. We
2788 * always want to get the blocksize, so we can set it back to the
2791 error = sagetparams(periph,
2792 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2793 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2794 ¤t_speed, &comp_supported, &comp_enabled,
2795 ¤t_calg, ccomp);
2798 free(ccomp, M_SCSISA);
2802 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2803 if (params_to_set & SA_PARAM_COMPRESSION)
2804 mode_buffer_len += sizeof (sa_comp_t);
2806 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
2807 if (mode_buffer == NULL) {
2808 free(ccomp, M_SCSISA);
2812 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2813 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2815 ccb = cam_periph_getccb(periph, 1);
2819 if (params_to_set & SA_PARAM_COMPRESSION) {
2821 cpage = (sa_comp_t *)&mode_blk[1];
2823 cpage = (sa_comp_t *)&mode_hdr[1];
2825 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2826 cpage->hdr.pagecode &= ~0x80;
2831 * If the caller wants us to set the blocksize, use the one they
2832 * pass in. Otherwise, use the blocksize we got back from the
2833 * mode select above.
2836 if (params_to_set & SA_PARAM_BLOCKSIZE)
2837 scsi_ulto3b(blocksize, mode_blk->blklen);
2839 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2842 * Set density if requested, else preserve old density.
2843 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2844 * devices, else density we've latched up in our softc.
2846 if (params_to_set & SA_PARAM_DENSITY) {
2847 mode_blk->density = density;
2848 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2849 mode_blk->density = SCSI_SAME_DENSITY;
2851 mode_blk->density = softc->media_density;
2856 * For mode selects, these two fields must be zero.
2858 mode_hdr->data_length = 0;
2859 mode_hdr->medium_type = 0;
2861 /* set the speed to the current value */
2862 mode_hdr->dev_spec = current_speed;
2864 /* if set, set single-initiator buffering mode */
2865 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
2866 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2870 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2872 mode_hdr->blk_desc_len = 0;
2875 * First, if the user wants us to set the compression algorithm or
2876 * just turn compression on, check to make sure that this drive
2877 * supports compression.
2879 if (params_to_set & SA_PARAM_COMPRESSION) {
2881 * If the compression algorithm is 0, disable compression.
2882 * If the compression algorithm is non-zero, enable
2883 * compression and set the compression type to the
2884 * specified compression algorithm, unless the algorithm is
2885 * MT_COMP_ENABLE. In that case, we look at the
2886 * compression algorithm that is currently set and if it is
2887 * non-zero, we leave it as-is. If it is zero, and we have
2888 * saved a compression algorithm from a time when
2889 * compression was enabled before, set the compression to
2892 switch (ccomp->hdr.pagecode & ~0x80) {
2893 case SA_DEVICE_CONFIGURATION_PAGE:
2895 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2897 dcp->sel_comp_alg = SA_COMP_NONE;
2900 if (calg != MT_COMP_ENABLE) {
2901 dcp->sel_comp_alg = calg;
2902 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2903 softc->saved_comp_algorithm != 0) {
2904 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2908 case SA_DATA_COMPRESSION_PAGE:
2909 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2910 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2913 * Disable compression, but leave the
2914 * decompression and the capability bit
2917 dcp->dce_and_dcc = SA_DCP_DCC;
2918 dcp->dde_and_red |= SA_DCP_DDE;
2921 /* enable compression && decompression */
2922 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2923 dcp->dde_and_red |= SA_DCP_DDE;
2925 * If there, use compression algorithm from caller.
2926 * Otherwise, if there's a saved compression algorithm
2927 * and there is no current algorithm, use the saved
2928 * algorithm. Else parrot back what we got and hope
2931 if (calg != MT_COMP_ENABLE) {
2932 scsi_ulto4b(calg, dcp->comp_algorithm);
2933 scsi_ulto4b(calg, dcp->decomp_algorithm);
2934 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2935 softc->saved_comp_algorithm != 0) {
2936 scsi_ulto4b(softc->saved_comp_algorithm,
2937 dcp->comp_algorithm);
2938 scsi_ulto4b(softc->saved_comp_algorithm,
2939 dcp->decomp_algorithm);
2944 * Compression does not appear to be supported-
2945 * at least via the DATA COMPRESSION page. It
2946 * would be too much to ask us to believe that
2947 * the page itself is supported, but incorrectly
2948 * reports an ability to manipulate data compression,
2949 * so we'll assume that this device doesn't support
2950 * compression. We can just fall through for that.
2955 * The drive doesn't seem to support compression,
2956 * so turn off the set compression bit.
2958 params_to_set &= ~SA_PARAM_COMPRESSION;
2959 xpt_print(periph->path,
2960 "device does not seem to support compression\n");
2963 * If that was the only thing the user wanted us to set,
2964 * clean up allocated resources and return with
2965 * 'operation not supported'.
2967 if (params_to_set == SA_PARAM_NONE) {
2968 free(mode_buffer, M_SCSISA);
2969 xpt_release_ccb(ccb);
2974 * That wasn't the only thing the user wanted us to set.
2975 * So, decrease the stated mode buffer length by the
2976 * size of the compression mode page.
2978 mode_buffer_len -= sizeof(sa_comp_t);
2982 /* It is safe to retry this operation */
2983 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2984 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2985 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2987 error = cam_periph_runccb(ccb, saerror, 0,
2988 sense_flags, softc->device_stats);
2990 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2992 char *xyz = mode_buffer;
2993 xpt_print_path(periph->path);
2994 printf("Err%d, Mode Select Data=", error);
2995 for (idx = 0; idx < mode_buffer_len; idx++)
2996 printf(" 0x%02x", xyz[idx] & 0xff);
3003 * If we can, try without setting density/blocksize.
3006 if ((params_to_set &
3007 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
3012 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
3013 cpage = (sa_comp_t *)&mode_blk[1];
3017 * If we were setting the blocksize, and that failed, we
3018 * want to set it to its original value. If we weren't
3019 * setting the blocksize, we don't want to change it.
3021 scsi_ulto3b(current_blocksize, mode_blk->blklen);
3024 * Set density if requested, else preserve old density.
3025 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
3026 * devices, else density we've latched up in our softc.
3028 if (params_to_set & SA_PARAM_DENSITY) {
3029 mode_blk->density = current_density;
3030 } else if (softc->scsi_rev > SCSI_REV_CCS) {
3031 mode_blk->density = SCSI_SAME_DENSITY;
3033 mode_blk->density = softc->media_density;
3036 if (params_to_set & SA_PARAM_COMPRESSION)
3037 bcopy(ccomp, cpage, sizeof (sa_comp_t));
3040 * The retry count is the only CCB field that might have been
3041 * changed that we care about, so reset it back to 1.
3043 ccb->ccb_h.retry_count = 1;
3044 cam_periph_runccb(ccb, saerror, 0, sense_flags,
3045 softc->device_stats);
3048 xpt_release_ccb(ccb);
3051 free(ccomp, M_SCSISA);
3053 if (params_to_set & SA_PARAM_COMPRESSION) {
3055 softc->flags &= ~SA_FLAG_COMP_ENABLED;
3057 * Even if we get an error setting compression,
3058 * do not say that we don't support it. We could
3059 * have been wrong, or it may be media specific.
3060 * softc->flags &= ~SA_FLAG_COMP_SUPP;
3062 softc->saved_comp_algorithm = softc->comp_algorithm;
3063 softc->comp_algorithm = 0;
3065 softc->flags |= SA_FLAG_COMP_ENABLED;
3066 softc->comp_algorithm = calg;
3070 free(mode_buffer, M_SCSISA);
3075 saprevent(struct cam_periph *periph, int action)
3077 struct sa_softc *softc;
3081 softc = (struct sa_softc *)periph->softc;
3083 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3085 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3089 * We can be quiet about illegal requests.
3091 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3096 ccb = cam_periph_getccb(periph, 1);
3098 /* It is safe to retry this operation */
3099 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3100 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3102 error = cam_periph_runccb(ccb, saerror, 0, sf, softc->device_stats);
3104 if (action == PR_ALLOW)
3105 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3107 softc->flags |= SA_FLAG_TAPE_LOCKED;
3110 xpt_release_ccb(ccb);
3114 sarewind(struct cam_periph *periph)
3117 struct sa_softc *softc;
3120 softc = (struct sa_softc *)periph->softc;
3122 ccb = cam_periph_getccb(periph, 1);
3124 /* It is safe to retry this operation */
3125 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3126 SSD_FULL_SIZE, REWIND_TIMEOUT);
3128 softc->dsreg = MTIO_DSREG_REW;
3129 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3130 softc->dsreg = MTIO_DSREG_REST;
3132 xpt_release_ccb(ccb);
3134 softc->fileno = softc->blkno = (daddr_t) 0;
3136 softc->fileno = softc->blkno = (daddr_t) -1;
3141 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3144 struct sa_softc *softc;
3147 softc = (struct sa_softc *)periph->softc;
3149 ccb = cam_periph_getccb(periph, 1);
3151 /* This cannot be retried */
3153 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3154 SSD_FULL_SIZE, SPACE_TIMEOUT);
3157 * Clear residual because we will be using it.
3159 softc->last_ctl_resid = 0;
3161 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3162 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3163 softc->dsreg = MTIO_DSREG_REST;
3165 xpt_release_ccb(ccb);
3168 * If a spacing operation has failed, we need to invalidate
3171 * If the spacing operation was setmarks or to end of recorded data,
3172 * we no longer know our relative position.
3174 * If the spacing operations was spacing files in reverse, we
3175 * take account of the residual, but still check against less
3176 * than zero- if we've gone negative, we must have hit BOT.
3178 * If the spacing operations was spacing records in reverse and
3179 * we have a residual, we've either hit BOT or hit a filemark.
3180 * In the former case, we know our new record number (0). In
3181 * the latter case, we have absolutely no idea what the real
3182 * record number is- we've stopped between the end of the last
3183 * record in the previous file and the filemark that stopped
3184 * our spacing backwards.
3187 softc->fileno = softc->blkno = (daddr_t) -1;
3188 } else if (code == SS_SETMARKS || code == SS_EOD) {
3189 softc->fileno = softc->blkno = (daddr_t) -1;
3190 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3191 softc->fileno += (count - softc->last_ctl_resid);
3192 if (softc->fileno < 0) /* we must of hit BOT */
3195 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3196 softc->blkno += (count - softc->last_ctl_resid);
3198 if (softc->last_ctl_resid || softc->blkno < 0) {
3199 if (softc->fileno == 0) {
3202 softc->blkno = (daddr_t) -1;
3211 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3214 struct sa_softc *softc;
3217 softc = (struct sa_softc *)periph->softc;
3218 if (softc->open_rdonly)
3221 ccb = cam_periph_getccb(periph, 1);
3223 * Clear residual because we will be using it.
3225 softc->last_ctl_resid = 0;
3227 softc->dsreg = MTIO_DSREG_FMK;
3228 /* this *must* not be retried */
3229 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3230 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3231 softc->dsreg = MTIO_DSREG_REST;
3234 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3236 if (error == 0 && nmarks) {
3237 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3238 nwm = nmarks - softc->last_ctl_resid;
3239 softc->filemarks += nwm;
3242 xpt_release_ccb(ccb);
3245 * Update relative positions (if we're doing that).
3248 softc->fileno = softc->blkno = (daddr_t) -1;
3249 } else if (softc->fileno != (daddr_t) -1) {
3250 softc->fileno += nwm;
3257 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3259 struct scsi_tape_position_data loc;
3261 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3265 * We try and flush any buffered writes here if we were writing
3266 * and we're trying to get hardware block position. It eats
3267 * up performance substantially, but I'm wary of drive firmware.
3269 * I think that *logical* block position is probably okay-
3270 * but hardware block position might have to wait for data
3271 * to hit media to be valid. Caveat Emptor.
3274 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3275 error = sawritefilemarks(periph, 0, 0);
3276 if (error && error != EACCES)
3280 ccb = cam_periph_getccb(periph, 1);
3281 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3282 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3283 softc->dsreg = MTIO_DSREG_RBSY;
3284 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3285 softc->dsreg = MTIO_DSREG_REST;
3288 if (loc.flags & SA_RPOS_UNCERTAIN) {
3289 error = EINVAL; /* nothing is certain */
3291 *blkptr = scsi_4btoul(loc.firstblk);
3295 xpt_release_ccb(ccb);
3300 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3303 struct sa_softc *softc;
3307 * We used to try and flush any buffered writes here.
3308 * Now we push this onto user applications to either
3309 * flush the pending writes themselves (via a zero count
3310 * WRITE FILEMARKS command) or they can trust their tape
3311 * drive to do this correctly for them.
3314 softc = (struct sa_softc *)periph->softc;
3315 ccb = cam_periph_getccb(periph, 1);
3318 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3319 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3322 softc->dsreg = MTIO_DSREG_POS;
3323 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3324 softc->dsreg = MTIO_DSREG_REST;
3325 xpt_release_ccb(ccb);
3327 * Note relative file && block number position as now unknown.
3329 softc->fileno = softc->blkno = (daddr_t) -1;
3334 saretension(struct cam_periph *periph)
3337 struct sa_softc *softc;
3340 softc = (struct sa_softc *)periph->softc;
3342 ccb = cam_periph_getccb(periph, 1);
3344 /* It is safe to retry this operation */
3345 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3346 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3348 softc->dsreg = MTIO_DSREG_TEN;
3349 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3350 softc->dsreg = MTIO_DSREG_REST;
3352 xpt_release_ccb(ccb);
3354 softc->fileno = softc->blkno = (daddr_t) 0;
3356 softc->fileno = softc->blkno = (daddr_t) -1;
3361 sareservereleaseunit(struct cam_periph *periph, int reserve)
3364 struct sa_softc *softc;
3367 softc = (struct sa_softc *)periph->softc;
3368 ccb = cam_periph_getccb(periph, 1);
3370 /* It is safe to retry this operation */
3371 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3372 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3373 softc->dsreg = MTIO_DSREG_RBSY;
3374 error = cam_periph_runccb(ccb, saerror, 0,
3375 SF_RETRY_UA | SF_NO_PRINT, softc->device_stats);
3376 softc->dsreg = MTIO_DSREG_REST;
3377 xpt_release_ccb(ccb);
3380 * If the error was Illegal Request, then the device doesn't support
3381 * RESERVE/RELEASE. This is not an error.
3383 if (error == EINVAL) {
3391 saloadunload(struct cam_periph *periph, int load)
3394 struct sa_softc *softc;
3397 softc = (struct sa_softc *)periph->softc;
3399 ccb = cam_periph_getccb(periph, 1);
3401 /* It is safe to retry this operation */
3402 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3403 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3405 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3406 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3407 softc->dsreg = MTIO_DSREG_REST;
3408 xpt_release_ccb(ccb);
3410 if (error || load == 0)
3411 softc->fileno = softc->blkno = (daddr_t) -1;
3412 else if (error == 0)
3413 softc->fileno = softc->blkno = (daddr_t) 0;
3418 saerase(struct cam_periph *periph, int longerase)
3422 struct sa_softc *softc;
3425 softc = (struct sa_softc *)periph->softc;
3426 if (softc->open_rdonly)
3429 ccb = cam_periph_getccb(periph, 1);
3431 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3432 SSD_FULL_SIZE, ERASE_TIMEOUT);
3434 softc->dsreg = MTIO_DSREG_ZER;
3435 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3436 softc->dsreg = MTIO_DSREG_REST;
3438 xpt_release_ccb(ccb);
3442 #endif /* _KERNEL */
3445 * Read tape block limits command.
3448 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3449 void (*cbfcnp)(struct cam_periph *, union ccb *),
3450 u_int8_t tag_action,
3451 struct scsi_read_block_limits_data *rlimit_buf,
3452 u_int8_t sense_len, u_int32_t timeout)
3454 struct scsi_read_block_limits *scsi_cmd;
3456 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3457 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3458 sizeof(*scsi_cmd), timeout);
3460 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3461 bzero(scsi_cmd, sizeof(*scsi_cmd));
3462 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3466 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3467 void (*cbfcnp)(struct cam_periph *, union ccb *),
3468 u_int8_t tag_action, int readop, int sli,
3469 int fixed, u_int32_t length, u_int8_t *data_ptr,
3470 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3472 struct scsi_sa_rw *scsi_cmd;
3475 read = (readop & SCSI_RW_DIRMASK) == SCSI_RW_READ;
3477 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3478 scsi_cmd->opcode = read ? SA_READ : SA_WRITE;
3479 scsi_cmd->sli_fixed = 0;
3481 scsi_cmd->sli_fixed |= SAR_SLI;
3483 scsi_cmd->sli_fixed |= SARW_FIXED;
3484 scsi_ulto3b(length, scsi_cmd->length);
3485 scsi_cmd->control = 0;
3487 cam_fill_csio(csio, retries, cbfcnp, (read ? CAM_DIR_IN : CAM_DIR_OUT) |
3488 ((readop & SCSI_RW_BIO) != 0 ? CAM_DATA_BIO : 0),
3489 tag_action, data_ptr, dxfer_len, sense_len,
3490 sizeof(*scsi_cmd), timeout);
3494 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3495 void (*cbfcnp)(struct cam_periph *, union ccb *),
3496 u_int8_t tag_action, int immediate, int eot,
3497 int reten, int load, u_int8_t sense_len,
3500 struct scsi_load_unload *scsi_cmd;
3502 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3503 bzero(scsi_cmd, sizeof(*scsi_cmd));
3504 scsi_cmd->opcode = LOAD_UNLOAD;
3506 scsi_cmd->immediate = SLU_IMMED;
3508 scsi_cmd->eot_reten_load |= SLU_EOT;
3510 scsi_cmd->eot_reten_load |= SLU_RETEN;
3512 scsi_cmd->eot_reten_load |= SLU_LOAD;
3514 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3515 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3519 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3520 void (*cbfcnp)(struct cam_periph *, union ccb *),
3521 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3524 struct scsi_rewind *scsi_cmd;
3526 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3527 bzero(scsi_cmd, sizeof(*scsi_cmd));
3528 scsi_cmd->opcode = REWIND;
3530 scsi_cmd->immediate = SREW_IMMED;
3532 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3533 0, sense_len, sizeof(*scsi_cmd), timeout);
3537 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3538 void (*cbfcnp)(struct cam_periph *, union ccb *),
3539 u_int8_t tag_action, scsi_space_code code,
3540 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3542 struct scsi_space *scsi_cmd;
3544 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3545 scsi_cmd->opcode = SPACE;
3546 scsi_cmd->code = code;
3547 scsi_ulto3b(count, scsi_cmd->count);
3548 scsi_cmd->control = 0;
3550 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3551 0, sense_len, sizeof(*scsi_cmd), timeout);
3555 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3556 void (*cbfcnp)(struct cam_periph *, union ccb *),
3557 u_int8_t tag_action, int immediate, int setmark,
3558 u_int32_t num_marks, u_int8_t sense_len,
3561 struct scsi_write_filemarks *scsi_cmd;
3563 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3564 bzero(scsi_cmd, sizeof(*scsi_cmd));
3565 scsi_cmd->opcode = WRITE_FILEMARKS;
3567 scsi_cmd->byte2 |= SWFMRK_IMMED;
3569 scsi_cmd->byte2 |= SWFMRK_WSMK;
3571 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3573 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3574 0, sense_len, sizeof(*scsi_cmd), timeout);
3578 * The reserve and release unit commands differ only by their opcodes.
3581 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3582 void (*cbfcnp)(struct cam_periph *, union ccb *),
3583 u_int8_t tag_action, int third_party,
3584 int third_party_id, u_int8_t sense_len,
3585 u_int32_t timeout, int reserve)
3587 struct scsi_reserve_release_unit *scsi_cmd;
3589 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3590 bzero(scsi_cmd, sizeof(*scsi_cmd));
3593 scsi_cmd->opcode = RESERVE_UNIT;
3595 scsi_cmd->opcode = RELEASE_UNIT;
3598 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3599 scsi_cmd->lun_thirdparty |=
3600 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3603 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3604 0, sense_len, sizeof(*scsi_cmd), timeout);
3608 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3609 void (*cbfcnp)(struct cam_periph *, union ccb *),
3610 u_int8_t tag_action, int immediate, int long_erase,
3611 u_int8_t sense_len, u_int32_t timeout)
3613 struct scsi_erase *scsi_cmd;
3615 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3616 bzero(scsi_cmd, sizeof(*scsi_cmd));
3618 scsi_cmd->opcode = ERASE;
3621 scsi_cmd->lun_imm_long |= SE_IMMED;
3624 scsi_cmd->lun_imm_long |= SE_LONG;
3626 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3627 0, sense_len, sizeof(*scsi_cmd), timeout);
3631 * Read Tape Position command.
3634 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3635 void (*cbfcnp)(struct cam_periph *, union ccb *),
3636 u_int8_t tag_action, int hardsoft,
3637 struct scsi_tape_position_data *sbp,
3638 u_int8_t sense_len, u_int32_t timeout)
3640 struct scsi_tape_read_position *scmd;
3642 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3643 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3644 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3645 bzero(scmd, sizeof(*scmd));
3646 scmd->opcode = READ_POSITION;
3647 scmd->byte1 = hardsoft;
3651 * Set Tape Position command.
3654 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3655 void (*cbfcnp)(struct cam_periph *, union ccb *),
3656 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3657 u_int8_t sense_len, u_int32_t timeout)
3659 struct scsi_tape_locate *scmd;
3661 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3662 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3663 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3664 bzero(scmd, sizeof(*scmd));
3665 scmd->opcode = LOCATE;
3667 scmd->byte1 |= SA_SPOS_BT;
3668 scsi_ulto4b(blkno, scmd->blkaddr);