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_POLLED 0x00
116 #define SA_CCB_BUFFER_IO 0x01
117 #define SA_CCB_WAITING 0x02
118 #define SA_CCB_TYPEMASK 0x0F
119 #define SA_POSITION_UPDATED 0x10
121 #define Set_CCB_Type(x, type) do { \
122 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
123 x->ccb_h.ccb_pflags |= type; \
126 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
131 SA_FLAG_OPEN = 0x0001,
132 SA_FLAG_FIXED = 0x0002,
133 SA_FLAG_TAPE_LOCKED = 0x0004,
134 SA_FLAG_TAPE_MOUNTED = 0x0008,
135 SA_FLAG_TAPE_WP = 0x0010,
136 SA_FLAG_TAPE_WRITTEN = 0x0020,
137 SA_FLAG_EOM_PENDING = 0x0040,
138 SA_FLAG_EIO_PENDING = 0x0080,
139 SA_FLAG_EOF_PENDING = 0x0100,
140 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
141 SA_FLAG_EOF_PENDING),
142 SA_FLAG_INVALID = 0x0200,
143 SA_FLAG_COMP_ENABLED = 0x0400,
144 SA_FLAG_COMP_SUPP = 0x0800,
145 SA_FLAG_COMP_UNSUPP = 0x1000,
146 SA_FLAG_TAPE_FROZEN = 0x2000
150 SA_MODE_REWIND = 0x00,
151 SA_MODE_NOREWIND = 0x01,
152 SA_MODE_OFFLINE = 0x02
156 SA_PARAM_NONE = 0x00,
157 SA_PARAM_BLOCKSIZE = 0x01,
158 SA_PARAM_DENSITY = 0x02,
159 SA_PARAM_COMPRESSION = 0x04,
160 SA_PARAM_BUFF_MODE = 0x08,
161 SA_PARAM_NUMBLOCKS = 0x10,
163 SA_PARAM_SPEED = 0x40,
168 SA_QUIRK_NONE = 0x00,
169 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
170 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
171 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
172 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
173 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
174 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
175 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
176 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
179 /* units are bits 4-7, 16-21 (1024 units) */
180 #define SAUNIT(DEV) \
181 (((minor(DEV) & 0xF0) >> 4) | ((minor(DEV) & 0x3f0000) >> 16))
183 #define SAMODE(z) ((minor(z) & 0x3))
184 #define SADENSITY(z) (((minor(z) >> 2) & 0x3))
185 #define SA_IS_CTRL(z) (minor(z) & (1 << 29))
187 #define SA_NOT_CTLDEV 0
191 #define SA_ATYPE_NR 1
192 #define SA_ATYPE_ER 2
194 #define SAMINOR(ctl, unit, mode, access) \
195 ((ctl << 29) | ((unit & 0x3f0) << 16) | ((unit & 0xf) << 4) | \
196 (mode << 0x2) | (access & 0x3))
198 #define SA_UNITMASK SAMINOR(0, -1, 0, 0)
199 #define SA_UNIT(unit) SAMINOR(0, unit, 0, 0)
201 #define SA_NUM_MODES 4
207 struct bio_queue_head bio_queue;
209 struct devstat device_stats;
215 u_int32_t comp_algorithm;
216 u_int32_t saved_comp_algorithm;
217 u_int32_t media_blksize;
218 u_int32_t last_media_blksize;
219 u_int32_t media_numblks;
220 u_int8_t media_density;
223 u_int8_t dsreg; /* mtio mt_dsreg, redux */
227 int last_resid_was_io;
230 * Relative to BOT Location.
239 struct scsi_sense_data _last_io_sense;
240 u_int32_t _last_io_resid;
241 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
242 struct scsi_sense_data _last_ctl_sense;
243 u_int32_t _last_ctl_resid;
244 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
245 #define last_io_sense errinfo._last_io_sense
246 #define last_io_resid errinfo._last_io_resid
247 #define last_io_cdb errinfo._last_io_cdb
248 #define last_ctl_sense errinfo._last_ctl_sense
249 #define last_ctl_resid errinfo._last_ctl_resid
250 #define last_ctl_cdb errinfo._last_ctl_cdb
253 * Misc other flags/state
257 open_rdonly : 1, /* open read-only */
258 open_pending_mount : 1, /* open pending mount */
259 ctrl_mode : 1; /* control device open */
262 struct sa_quirk_entry {
263 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
264 sa_quirks quirks; /* specific quirk type */
265 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
268 static struct sa_quirk_entry sa_quirk_table[] =
271 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
272 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
273 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
276 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
277 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
280 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
281 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
284 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
285 "Python*", "*"}, SA_QUIRK_NODREAD, 0
288 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
289 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
292 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
293 "VIPER 2525 25462", "-011"},
294 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
297 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
298 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
302 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
303 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
307 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
308 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
311 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
312 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
315 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
316 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
319 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
320 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
323 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
324 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
327 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
328 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
330 { /* jreynold@primenet.com */
331 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
332 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
334 { /* mike@sentex.net */
335 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
336 "STT20000*", "*"}, SA_QUIRK_1FM, 0
339 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
340 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
343 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
344 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
347 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
348 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
351 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
352 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
355 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
356 " SLR*", "*"}, SA_QUIRK_1FM, 0
359 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
360 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
363 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
364 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
368 static d_open_t saopen;
369 static d_close_t saclose;
370 static d_strategy_t sastrategy;
371 static d_ioctl_t saioctl;
372 static periph_init_t sainit;
373 static periph_ctor_t saregister;
374 static periph_oninv_t saoninvalidate;
375 static periph_dtor_t sacleanup;
376 static periph_start_t sastart;
377 static void saasync(void *callback_arg, u_int32_t code,
378 struct cam_path *path, void *arg);
379 static void sadone(struct cam_periph *periph,
380 union ccb *start_ccb);
381 static int saerror(union ccb *ccb, u_int32_t cam_flags,
382 u_int32_t sense_flags);
383 static int samarkswanted(struct cam_periph *);
384 static int sacheckeod(struct cam_periph *periph);
385 static int sagetparams(struct cam_periph *periph,
386 sa_params params_to_get,
387 u_int32_t *blocksize, u_int8_t *density,
388 u_int32_t *numblocks, int *buff_mode,
389 u_int8_t *write_protect, u_int8_t *speed,
390 int *comp_supported, int *comp_enabled,
391 u_int32_t *comp_algorithm,
392 sa_comp_t *comp_page);
393 static int sasetparams(struct cam_periph *periph,
394 sa_params params_to_set,
395 u_int32_t blocksize, u_int8_t density,
396 u_int32_t comp_algorithm,
397 u_int32_t sense_flags);
398 static void saprevent(struct cam_periph *periph, int action);
399 static int sarewind(struct cam_periph *periph);
400 static int saspace(struct cam_periph *periph, int count,
401 scsi_space_code code);
402 static int samount(struct cam_periph *, int, cdev_t);
403 static int saretension(struct cam_periph *periph);
404 static int sareservereleaseunit(struct cam_periph *periph,
406 static int saloadunload(struct cam_periph *periph, int load);
407 static int saerase(struct cam_periph *periph, int longerase);
408 static int sawritefilemarks(struct cam_periph *periph,
409 int nmarks, int setmarks);
410 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
411 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
414 static struct periph_driver sadriver =
417 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
420 PERIPHDRIVER_DECLARE(sa, sadriver);
422 /* For 2.2-stable support */
427 static struct dev_ops sa_ops = {
432 .d_write = physwrite,
434 .d_strategy = sastrategy,
437 static struct extend_array *saperiphs;
440 saopen(struct dev_open_args *ap)
442 cdev_t dev = ap->a_head.a_dev;
443 struct cam_periph *periph;
444 struct sa_softc *softc;
450 periph = cam_extend_get(saperiphs, unit);
451 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
455 cam_periph_lock(periph);
457 softc = (struct sa_softc *)periph->softc;
459 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
460 ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
462 if (SA_IS_CTRL(dev)) {
463 softc->ctrl_mode = 1;
464 cam_periph_unlock(periph);
468 if ((error = cam_periph_hold(periph, PCATCH)) != 0) {
469 cam_periph_unlock(periph);
470 cam_periph_release(periph);
474 if (softc->flags & SA_FLAG_OPEN) {
476 } else if (softc->flags & SA_FLAG_INVALID) {
480 * Preserve whether this is a read_only open.
482 softc->open_rdonly = (ap->a_oflags & O_RDWR) == O_RDONLY;
485 * The function samount ensures media is loaded and ready.
486 * It also does a device RESERVE if the tape isn't yet mounted.
488 * If the mount fails and this was a non-blocking open,
489 * make this a 'open_pending_mount' action.
491 error = samount(periph, ap->a_oflags, dev);
492 if (error && (ap->a_oflags & O_NONBLOCK)) {
493 softc->flags |= SA_FLAG_OPEN;
494 softc->open_pending_mount = 1;
495 cam_periph_unhold(periph, 1);
501 cam_periph_unhold(periph, 1);
502 cam_periph_release(periph);
506 saprevent(periph, PR_PREVENT);
507 softc->flags |= SA_FLAG_OPEN;
509 cam_periph_unhold(periph, 1);
514 saclose(struct dev_close_args *ap)
516 cdev_t dev = ap->a_head.a_dev;
517 struct cam_periph *periph;
518 struct sa_softc *softc;
519 int unit, mode, error, writing, tmp;
520 int closedbits = SA_FLAG_OPEN;
524 periph = cam_extend_get(saperiphs, unit);
528 cam_periph_lock(periph);
530 softc = (struct sa_softc *)periph->softc;
532 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
533 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
536 softc->open_rdonly = 0;
537 if (SA_IS_CTRL(dev)) {
538 softc->ctrl_mode = 0;
539 cam_periph_unlock(periph);
540 cam_periph_release(periph);
544 if (softc->open_pending_mount) {
545 softc->flags &= ~SA_FLAG_OPEN;
546 softc->open_pending_mount = 0;
547 cam_periph_unlock(periph);
548 cam_periph_release(periph);
552 if ((error = cam_periph_hold(periph, 0)) != 0) {
553 cam_periph_unlock(periph);
558 * Were we writing the tape?
560 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
563 * See whether or not we need to write filemarks. If this
564 * fails, we probably have to assume we've lost tape
567 error = sacheckeod(periph);
569 xpt_print(periph->path,
570 "failed to write terminating filemark(s)\n");
571 softc->flags |= SA_FLAG_TAPE_FROZEN;
575 * Whatever we end up doing, allow users to eject tapes from here on.
577 saprevent(periph, PR_ALLOW);
580 * Decide how to end...
582 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
583 closedbits |= SA_FLAG_TAPE_FROZEN;
584 } else switch (mode) {
585 case SA_MODE_OFFLINE:
587 * An 'offline' close is an unconditional release of
588 * frozen && mount conditions, irrespective of whether
589 * these operations succeeded. The reason for this is
590 * to allow at least some kind of programmatic way
591 * around our state getting all fouled up. If somebody
592 * issues an 'offline' command, that will be allowed
596 saloadunload(periph, FALSE);
597 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
601 * If the rewind fails, return an error- if anyone cares,
602 * but not overwriting any previous error.
604 * We don't clear the notion of mounted here, but we do
605 * clear the notion of frozen if we successfully rewound.
607 tmp = sarewind(periph);
612 closedbits |= SA_FLAG_TAPE_FROZEN;
615 case SA_MODE_NOREWIND:
617 * If we're not rewinding/unloading the tape, find out
618 * whether we need to back up over one of two filemarks
619 * we wrote (if we wrote two filemarks) so that appends
620 * from this point on will be sane.
622 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
623 tmp = saspace(periph, -1, SS_FILEMARKS);
625 xpt_print(periph->path, "unable to backspace "
626 "over one of double filemarks at end of "
628 xpt_print(periph->path, "it is possible that "
629 "this device needs a SA_QUIRK_1FM quirk set"
631 softc->flags |= SA_FLAG_TAPE_FROZEN;
636 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode);
642 * We wish to note here that there are no more filemarks to be written.
644 softc->filemarks = 0;
645 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
648 * And we are no longer open for business.
650 softc->flags &= ~closedbits;
653 * Inform users if tape state if frozen....
655 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
656 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, "
657 "REWIND or MTEOM command to clear this state.\n");
660 /* release the device if it is no longer mounted */
661 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
662 sareservereleaseunit(periph, FALSE);
664 cam_periph_unhold(periph, 1);
665 cam_periph_release(periph);
671 * Actually translate the requested transfer into one the physical driver
672 * can understand. The transfer is described by a buf and will include
673 * only one physical transfer.
676 sastrategy(struct dev_strategy_args *ap)
678 cdev_t dev = ap->a_head.a_dev;
679 struct bio *bio = ap->a_bio;
680 struct buf *bp = bio->bio_buf;
681 struct cam_periph *periph;
682 struct sa_softc *softc;
685 if (SA_IS_CTRL(dev)) {
686 bp->b_error = EINVAL;
690 periph = cam_extend_get(saperiphs, unit);
691 if (periph == NULL) {
695 cam_periph_lock(periph);
697 softc = (struct sa_softc *)periph->softc;
699 if (softc->flags & SA_FLAG_INVALID) {
700 cam_periph_unlock(periph);
705 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
706 cam_periph_unlock(periph);
712 * This should actually never occur as the write(2)
713 * system call traps attempts to write to a read-only
716 if (bp->b_cmd == BUF_CMD_WRITE && softc->open_rdonly) {
717 cam_periph_unlock(periph);
722 if (softc->open_pending_mount) {
723 int error = samount(periph, 0, dev);
725 cam_periph_unlock(periph);
729 saprevent(periph, PR_PREVENT);
730 softc->open_pending_mount = 0;
734 * If it's a null transfer, return immediately
736 if (bp->b_bcount == 0) {
737 cam_periph_unlock(periph);
742 if (softc->flags & SA_FLAG_FIXED) {
744 * Fixed block device. The byte count must
745 * be a multiple of our block size.
747 if (((softc->blk_mask != ~0) &&
748 ((bp->b_bcount & softc->blk_mask) != 0)) ||
749 ((softc->blk_mask == ~0) &&
750 ((bp->b_bcount % softc->min_blk) != 0))) {
751 xpt_print(periph->path, "Invalid request. Fixed block "
752 "device requests must be a multiple of %d bytes\n",
754 cam_periph_unlock(periph);
755 bp->b_error = EINVAL;
758 } else if ((bp->b_bcount > softc->max_blk) ||
759 (bp->b_bcount < softc->min_blk) ||
760 (bp->b_bcount & softc->blk_mask) != 0) {
762 xpt_print_path(periph->path);
763 kprintf("Invalid request. Variable block "
764 "device requests must be ");
765 if (softc->blk_mask != 0) {
766 kprintf("a multiple of %d ", (0x1 << softc->blk_gran));
768 kprintf("between %d and %d bytes\n", softc->min_blk,
770 cam_periph_unlock(periph);
771 bp->b_error = EINVAL;
776 * Place it at the end of the queue.
778 bioq_insert_tail(&softc->bio_queue, bio);
779 softc->queue_count++;
781 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
782 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
783 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
784 (bp->bio_cmd == BIO_READ)? "read" : "write"));
786 if (softc->queue_count > 1) {
787 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
788 ("sastrategy: queue count now %d\n", softc->queue_count));
792 * Schedule ourselves for performing the work.
794 xpt_schedule(periph, 1);
795 cam_periph_unlock(periph);
799 bp->b_flags |= B_ERROR;
803 * Correctly set the buf to indicate a completed xfer
805 bp->b_resid = bp->b_bcount;
811 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
812 if (softc->open_pending_mount) { \
813 error = samount(periph, 0, dev); \
817 saprevent(periph, PR_PREVENT); \
818 softc->open_pending_mount = 0; \
822 saioctl(struct dev_ioctl_args *ap)
824 cdev_t dev = ap->a_head.a_dev;
825 caddr_t addr = ap->a_data;
826 struct cam_periph *periph;
827 struct sa_softc *softc;
828 scsi_space_code spaceop;
829 int didholdperiph = 0;
836 error = 0; /* shut up gcc */
837 spaceop = 0; /* shut up gcc */
839 periph = cam_extend_get(saperiphs, unit);
843 cam_periph_lock(periph);
844 softc = (struct sa_softc *)periph->softc;
847 * Check for control mode accesses. We allow MTIOCGET and
848 * MTIOCERRSTAT (but need to be the only one open in order
849 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
850 * and MTCOMP (but need to be the only one accessing this
851 * device to run those).
854 if (SA_IS_CTRL(dev)) {
856 case MTIOCGETEOTMODEL:
861 * If the periph isn't already locked, lock it
862 * so our MTIOCERRSTAT can reset latched error stats.
864 * If the periph is already locked, skip it because
865 * we're just getting status and it'll be up to the
866 * other thread that has this device open to do
867 * an MTIOCERRSTAT that would clear latched status.
869 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
870 error = cam_periph_hold(periph, PCATCH);
880 struct mtop *mt = (struct mtop *) addr;
883 * Check to make sure it's an OP we can perform
884 * with no media inserted.
900 case MTIOCSETEOTMODEL:
902 * We need to acquire the peripheral here rather
903 * than at open time because we are sharing writable
904 * access to data structures.
906 error = cam_periph_hold(periph, PCATCH);
919 * Find the device that the user is talking about
924 struct mtget *g = (struct mtget *)addr;
927 * If this isn't the control mode device, actually go out
928 * and ask the drive again what it's set to.
930 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) {
931 u_int8_t write_protect = 0; /* silence gcc */
932 int comp_enabled = 0; /* silence gcc */
933 int comp_supported = 0; /* silence gcc */
935 error = sagetparams(periph, SA_PARAM_ALL,
936 &softc->media_blksize, &softc->media_density,
937 &softc->media_numblks, &softc->buffer_mode,
938 &write_protect, &softc->speed, &comp_supported,
939 &comp_enabled, &softc->comp_algorithm, NULL);
943 softc->flags |= SA_FLAG_TAPE_WP;
945 softc->flags &= ~SA_FLAG_TAPE_WP;
946 softc->flags &= ~(SA_FLAG_COMP_SUPP|
947 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
948 if (comp_supported) {
949 if (softc->saved_comp_algorithm == 0)
950 softc->saved_comp_algorithm =
951 softc->comp_algorithm;
952 softc->flags |= SA_FLAG_COMP_SUPP;
954 softc->flags |= SA_FLAG_COMP_ENABLED;
956 softc->flags |= SA_FLAG_COMP_UNSUPP;
958 bzero(g, sizeof(struct mtget));
959 g->mt_type = MT_ISAR;
960 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
961 g->mt_comp = MT_COMP_UNSUPP;
962 g->mt_comp0 = MT_COMP_UNSUPP;
963 g->mt_comp1 = MT_COMP_UNSUPP;
964 g->mt_comp2 = MT_COMP_UNSUPP;
965 g->mt_comp3 = MT_COMP_UNSUPP;
967 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
968 g->mt_comp = MT_COMP_DISABLED;
970 g->mt_comp = softc->comp_algorithm;
972 g->mt_comp0 = softc->comp_algorithm;
973 g->mt_comp1 = softc->comp_algorithm;
974 g->mt_comp2 = softc->comp_algorithm;
975 g->mt_comp3 = softc->comp_algorithm;
977 g->mt_density = softc->media_density;
978 g->mt_density0 = softc->media_density;
979 g->mt_density1 = softc->media_density;
980 g->mt_density2 = softc->media_density;
981 g->mt_density3 = softc->media_density;
982 g->mt_blksiz = softc->media_blksize;
983 g->mt_blksiz0 = softc->media_blksize;
984 g->mt_blksiz1 = softc->media_blksize;
985 g->mt_blksiz2 = softc->media_blksize;
986 g->mt_blksiz3 = softc->media_blksize;
987 g->mt_fileno = softc->fileno;
988 g->mt_blkno = softc->blkno;
989 g->mt_dsreg = (short) softc->dsreg;
991 * Yes, we know that this is likely to overflow
993 if (softc->last_resid_was_io) {
994 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
995 if (SA_IS_CTRL(dev) == 0 || didholdperiph) {
996 softc->last_io_resid = 0;
1000 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
1001 if (SA_IS_CTRL(dev) == 0 || didholdperiph) {
1002 softc->last_ctl_resid = 0;
1011 struct scsi_tape_errors *sep =
1012 &((union mterrstat *)addr)->scsi_errstat;
1014 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1015 ("saioctl: MTIOCERRSTAT\n"));
1017 bzero(sep, sizeof(*sep));
1018 sep->io_resid = softc->last_io_resid;
1019 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1020 sizeof (sep->io_sense));
1021 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1022 sizeof (sep->io_cdb));
1023 sep->ctl_resid = softc->last_ctl_resid;
1024 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1025 sizeof (sep->ctl_sense));
1026 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1027 sizeof (sep->ctl_cdb));
1029 if ((SA_IS_CTRL(dev) == 0 && softc->open_pending_mount) ||
1031 bzero((caddr_t) &softc->errinfo,
1032 sizeof (softc->errinfo));
1041 PENDING_MOUNT_CHECK(softc, periph, dev);
1043 mt = (struct mtop *)addr;
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);
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);
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 *) addr);
1277 PENDING_MOUNT_CHECK(softc, periph, dev);
1278 error = sardpos(periph, 1, (u_int32_t *) addr);
1281 PENDING_MOUNT_CHECK(softc, periph, dev);
1282 error = sasetpos(periph, 0, (u_int32_t *) addr);
1285 PENDING_MOUNT_CHECK(softc, periph, dev);
1286 error = sasetpos(periph, 1, (u_int32_t *) addr);
1288 case MTIOCGETEOTMODEL:
1290 if (softc->quirks & SA_QUIRK_1FM)
1294 *((u_int32_t *) addr) = mode;
1296 case MTIOCSETEOTMODEL:
1298 switch (*((u_int32_t *) addr)) {
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, ap->a_cmd, addr, 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");
1337 cam_periph_unhold(periph, 1);
1339 cam_periph_unlock(periph);
1349 * Create our extend array for storing the devices we attach to.
1351 saperiphs = cam_extend_new();
1352 if (saperiphs == NULL) {
1353 kprintf("sa: Failed to alloc extend array!\n");
1358 * Install a global async callback.
1360 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL);
1362 if (status != CAM_REQ_CMP) {
1363 kprintf("sa: Failed to attach master async callback "
1364 "due to status 0x%x!\n", status);
1369 saoninvalidate(struct cam_periph *periph)
1371 struct sa_softc *softc;
1375 softc = (struct sa_softc *)periph->softc;
1378 * De-register any async callbacks.
1380 xpt_register_async(0, saasync, periph, periph->path);
1382 softc->flags |= SA_FLAG_INVALID;
1385 * Return all queued I/O with ENXIO.
1386 * XXX Handle any transactions queued to the card
1387 * with XPT_ABORT_CCB.
1389 while ((q_bio = bioq_takefirst(&softc->bio_queue)) != NULL) {
1390 q_bp = q_bio->bio_buf;
1391 q_bp->b_resid = q_bp->b_bcount;
1392 q_bp->b_error = ENXIO;
1393 q_bp->b_flags |= B_ERROR;
1396 softc->queue_count = 0;
1398 xpt_print(periph->path, "lost device\n");
1403 sacleanup(struct cam_periph *periph)
1405 struct sa_softc *softc;
1407 softc = (struct sa_softc *)periph->softc;
1409 devstat_remove_entry(&softc->device_stats);
1411 cam_extend_release(saperiphs, periph->unit_number);
1412 xpt_print(periph->path, "removing device entry\n");
1413 dev_ops_remove_minor(&sa_ops, /*SA_UNITMASK,*/ SA_UNIT(periph->unit_number));
1414 kfree(softc, M_SCSISA);
1418 saasync(void *callback_arg, u_int32_t code,
1419 struct cam_path *path, void *arg)
1421 struct cam_periph *periph;
1423 periph = (struct cam_periph *)callback_arg;
1425 case AC_FOUND_DEVICE:
1427 struct ccb_getdev *cgd;
1430 cgd = (struct ccb_getdev *)arg;
1434 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1438 * Allocate a peripheral instance for
1439 * this device and start the probe
1442 status = cam_periph_alloc(saregister, saoninvalidate,
1444 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1445 saasync, AC_FOUND_DEVICE, cgd);
1447 if (status != CAM_REQ_CMP
1448 && status != CAM_REQ_INPROG)
1449 kprintf("saasync: Unable to probe new device "
1450 "due to status 0x%x\n", status);
1454 cam_periph_async(periph, code, path, arg);
1460 saregister(struct cam_periph *periph, void *arg)
1462 struct sa_softc *softc;
1463 struct ccb_getdev *cgd;
1467 cgd = (struct ccb_getdev *)arg;
1468 if (periph == NULL) {
1469 kprintf("saregister: periph was NULL!!\n");
1470 return (CAM_REQ_CMP_ERR);
1474 kprintf("saregister: no getdev CCB, can't register device\n");
1475 return (CAM_REQ_CMP_ERR);
1478 softc = kmalloc(sizeof (*softc), M_SCSISA, M_INTWAIT | M_ZERO);
1479 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1480 softc->state = SA_STATE_NORMAL;
1481 softc->fileno = (daddr_t) -1;
1482 softc->blkno = (daddr_t) -1;
1484 bioq_init(&softc->bio_queue);
1485 periph->softc = softc;
1486 cam_extend_set(saperiphs, periph->unit_number, periph);
1489 * See if this device has any quirks.
1491 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1492 (caddr_t)sa_quirk_table,
1493 NELEM(sa_quirk_table),
1494 sizeof(*sa_quirk_table), scsi_inquiry_match);
1496 if (match != NULL) {
1497 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1498 softc->last_media_blksize =
1499 ((struct sa_quirk_entry *)match)->prefblk;
1501 xpt_print(periph->path, "found quirk entry %d\n",
1502 (int) (((struct sa_quirk_entry *) match) - sa_quirk_table));
1505 softc->quirks = SA_QUIRK_NONE;
1508 * The SA driver supports a blocksize, but we don't know the
1509 * blocksize until we media is inserted. So, set a flag to
1510 * indicate that the blocksize is unavailable right now.
1512 cam_periph_unlock(periph);
1513 devstat_add_entry(&softc->device_stats, "sa", periph->unit_number, 0,
1514 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1515 DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE);
1517 make_dev(&sa_ops, SAMINOR(SA_CTLDEV,
1518 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1519 0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1521 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1522 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1523 0660, "%s%d", periph->periph_name, periph->unit_number);
1525 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1526 periph->unit_number, 0, SA_ATYPE_NR), UID_ROOT, GID_OPERATOR,
1527 0660, "n%s%d", periph->periph_name, periph->unit_number);
1529 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1530 periph->unit_number, 0, SA_ATYPE_ER), UID_ROOT, GID_OPERATOR,
1531 0660, "e%s%d", periph->periph_name, periph->unit_number);
1533 for (i = 0; i < SA_NUM_MODES; i++) {
1536 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R),
1537 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1538 periph->periph_name, periph->unit_number, i);
1541 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR),
1542 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1543 periph->periph_name, periph->unit_number, i);
1547 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER),
1548 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1549 periph->periph_name, periph->unit_number, i);
1551 cam_periph_lock(periph);
1554 * Add an async callback so that we get
1555 * notified if this device goes away.
1557 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path);
1559 xpt_announce_periph(periph, NULL);
1561 return (CAM_REQ_CMP);
1565 sastart(struct cam_periph *periph, union ccb *start_ccb)
1567 struct sa_softc *softc;
1569 softc = (struct sa_softc *)periph->softc;
1571 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
1574 switch (softc->state) {
1575 case SA_STATE_NORMAL:
1577 /* Pull a buffer from the queue and get going on it */
1582 * See if there is a buf with work for us to do..
1584 bio = bioq_first(&softc->bio_queue);
1585 if (periph->immediate_priority <= periph->pinfo.priority) {
1586 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1587 ("queuing for immediate ccb\n"));
1588 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1589 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1591 periph->immediate_priority = CAM_PRIORITY_NONE;
1592 wakeup(&periph->ccb_list);
1593 } else if (bio == NULL) {
1594 xpt_release_ccb(start_ccb);
1595 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1596 struct bio *done_bio;
1598 softc->queue_count--;
1599 bioq_remove(&softc->bio_queue, bio);
1601 bp->b_resid = bp->b_bcount;
1603 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1605 * We now just clear errors in this case
1606 * and let the residual be the notifier.
1609 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1611 * This can only happen if we're reading
1612 * in fixed length mode. In this case,
1613 * we dump the rest of the list the
1617 if (bioq_first(&softc->bio_queue) != NULL) {
1621 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1623 bp->b_flags |= B_ERROR;
1625 bio = bioq_first(&softc->bio_queue);
1627 * Only if we have no other buffers queued up
1628 * do we clear the pending error flag.
1631 softc->flags &= ~SA_FLAG_ERR_PENDING;
1632 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1633 ("sastart- ERR_PENDING now 0x%x, bio is %sNULL, "
1634 "%d more buffers queued up\n",
1635 (softc->flags & SA_FLAG_ERR_PENDING),
1636 (bio != NULL)? "not " : " ", softc->queue_count));
1637 xpt_release_ccb(start_ccb);
1642 bioq_remove(&softc->bio_queue, bio);
1644 softc->queue_count--;
1646 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1647 if (softc->blk_shift != 0) {
1649 bp->b_bcount >> softc->blk_shift;
1650 } else if (softc->media_blksize != 0) {
1652 bp->b_bcount / softc->media_blksize;
1655 xpt_print(periph->path, "zero blocksize"
1656 " for FIXED length writes?\n");
1661 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1662 ("issuing a %d fixed record %s\n",
1663 length, (bp->bio_cmd == BIO_READ)? "read" :
1667 length = bp->b_bcount;
1669 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1670 ("issuing a %d variable byte %s\n",
1671 length, (bp->bio_cmd == BIO_READ)? "read" :
1675 devstat_start_transaction(&softc->device_stats);
1677 * Some people have theorized that we should
1678 * suppress illegal length indication if we are
1679 * running in variable block mode so that we don't
1680 * have to request sense every time our requested
1681 * block size is larger than the written block.
1682 * The residual information from the ccb allows
1683 * us to identify this situation anyway. The only
1684 * problem with this is that we will not get
1685 * information about blocks that are larger than
1686 * our read buffer unless we set the block size
1687 * in the mode page to something other than 0.
1689 * I believe that this is a non-issue. If user apps
1690 * don't adjust their read size to match our record
1691 * size, that's just life. Anyway, the typical usage
1692 * would be to issue, e.g., 64KB reads and occasionally
1693 * have to do deal with 512 byte or 1KB intermediate
1696 softc->dsreg = (bp->b_cmd == BUF_CMD_READ) ?
1697 MTIO_DSREG_RD : MTIO_DSREG_WR;
1698 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1699 MSG_SIMPLE_Q_TAG, (bp->b_cmd == BUF_CMD_READ) != 0,
1700 FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1701 length, bp->b_data, bp->b_bcount, SSD_FULL_SIZE,
1703 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1704 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1705 start_ccb->ccb_h.ccb_bio = bio;
1706 bio = bioq_first(&softc->bio_queue);
1707 xpt_action(start_ccb);
1711 /* Have more work to do, so ensure we stay scheduled */
1712 xpt_schedule(periph, 1);
1716 case SA_STATE_ABNORMAL:
1718 panic("state 0x%x in sastart", softc->state);
1725 sadone(struct cam_periph *periph, union ccb *done_ccb)
1727 struct sa_softc *softc;
1728 struct ccb_scsiio *csio;
1730 softc = (struct sa_softc *)periph->softc;
1731 csio = &done_ccb->csio;
1733 switch (CCB_Type(csio)) {
1734 case SA_CCB_BUFFER_IO:
1740 softc->dsreg = MTIO_DSREG_REST;
1741 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1744 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1745 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1747 * A retry was scheduled, so just return.
1758 * Catastrophic error. Mark the tape as frozen
1759 * (we no longer know tape position).
1761 * Return all queued I/O with EIO, and unfreeze
1762 * our queue so that future transactions that
1763 * attempt to fix this problem can get to the
1768 softc->flags |= SA_FLAG_TAPE_FROZEN;
1769 while ((q_bio = bioq_takefirst(&softc->bio_queue)) != NULL) {
1770 q_bp = q_bio->bio_buf;
1771 q_bp->b_resid = q_bp->b_bcount;
1772 q_bp->b_error = EIO;
1773 q_bp->b_flags |= B_ERROR;
1778 bp->b_resid = bp->b_bcount;
1779 bp->b_error = error;
1780 bp->b_flags |= B_ERROR;
1782 * In the error case, position is updated in saerror.
1785 bp->b_resid = csio->resid;
1787 if (csio->resid != 0) {
1788 bp->b_flags |= B_ERROR;
1790 if (bp->b_cmd != BUF_CMD_READ) {
1791 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1792 softc->filemarks = 0;
1794 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1795 (softc->blkno != (daddr_t) -1)) {
1796 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1798 if (softc->blk_shift != 0) {
1803 softc->media_blksize;
1805 softc->blkno += (daddr_t) l;
1812 * If we had an error (immediate or pending),
1813 * release the device queue now.
1815 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1816 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1818 if (error || bp->b_resid) {
1819 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1820 ("error %d resid %d count %d\n", error,
1821 bp->b_resid, bp->b_bcount));
1824 devstat_end_transaction_buf(&softc->device_stats, bp);
1828 case SA_CCB_WAITING:
1830 /* Caller will release the CCB */
1831 wakeup(&done_ccb->ccb_h.cbfcnp);
1835 /* polled, caller releases ccb */
1836 wakeup(&done_ccb->ccb_h.cbfcnp);
1839 xpt_release_ccb(done_ccb);
1843 * Mount the tape (make sure it's ready for I/O).
1845 * oflags can be checked for 'kind' of open (read-only check) - later
1846 * dev can be checked for a control-mode or compression open - later
1849 samount(struct cam_periph *periph, __unused int oflags, __unused cdev_t dev)
1851 struct sa_softc *softc;
1855 softc = (struct sa_softc *)periph->softc;
1858 * This should determine if something has happend since the last
1859 * open/mount that would invalidate the mount. We do *not* want
1860 * to retry this command- we just want the status. But we only
1861 * do this if we're mounted already- if we're not mounted,
1862 * we don't care about the unit read state and can instead use
1863 * this opportunity to attempt to reserve the tape unit.
1866 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1867 ccb = cam_periph_getccb(periph, 1);
1868 Set_CCB_Type(ccb, SA_CCB_POLLED);
1870 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1871 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE,
1873 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1874 &softc->device_stats);
1876 if (error == ENXIO) {
1877 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1878 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1879 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1880 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1881 &softc->device_stats);
1885 * We don't need to freeze the tape because we
1886 * will now attempt to rewind/load it.
1888 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1889 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
1890 xpt_print(periph->path,
1891 "error %d on TUR in samount\n", error);
1895 error = sareservereleaseunit(periph, TRUE);
1899 ccb = cam_periph_getccb(periph, 1);
1900 Set_CCB_Type(ccb, SA_CCB_POLLED);
1901 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1902 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE,
1904 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1905 &softc->device_stats);
1909 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1910 struct scsi_read_block_limits_data *rblim = NULL;
1911 int comp_enabled = 0; /* silence gcc */
1912 int comp_supported = 0; /* silence gcc */
1913 u_int8_t write_protect = 0; /* silence gcc */
1914 u_int8_t guessing = 0;
1917 * Clear out old state.
1919 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1920 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1921 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1922 softc->filemarks = 0;
1925 * *Very* first off, make sure we're loaded to BOT.
1927 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1928 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1929 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1930 &softc->device_stats);
1934 * In case this doesn't work, do a REWIND instead
1937 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1938 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1939 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1940 &softc->device_stats);
1944 xpt_release_ccb(ccb);
1949 * Do a dummy test read to force access to the
1950 * media so that the drive will really know what's
1951 * there. We actually don't really care what the
1952 * blocksize on tape is and don't expect to really
1953 * read a full record.
1955 rblim = kmalloc(8192, M_SCSISA, M_INTWAIT);
1957 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1958 scsi_sa_read_write(&ccb->csio, 0, sadone,
1959 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1960 (void *) rblim, 8192, SSD_FULL_SIZE,
1962 cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1963 &softc->device_stats);
1965 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1966 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1967 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1968 SF_NO_PRINT | SF_RETRY_UA,
1969 &softc->device_stats);
1972 xpt_print(periph->path,
1973 "unable to rewind after test read\n");
1974 xpt_release_ccb(ccb);
1980 * Next off, determine block limits.
1982 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1983 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1985 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1986 SF_NO_PRINT | SF_RETRY_UA, &softc->device_stats);
1989 xpt_release_ccb(ccb);
1993 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1994 * a MANDATORY command. Anyway- it doesn't matter-
1995 * we can proceed anyway.
1997 softc->blk_gran = 0;
1998 softc->max_blk = ~0;
2001 if (softc->scsi_rev >= SCSI_REV_SPC) {
2002 softc->blk_gran = RBL_GRAN(rblim);
2004 softc->blk_gran = 0;
2007 * We take max_blk == min_blk to mean a default to
2008 * fixed mode- but note that whatever we get out of
2009 * sagetparams below will actually determine whether
2010 * we are actually *in* fixed mode.
2012 softc->max_blk = scsi_3btoul(rblim->maximum);
2013 softc->min_blk = scsi_2btoul(rblim->minimum);
2018 * Next, perform a mode sense to determine
2019 * current density, blocksize, compression etc.
2021 error = sagetparams(periph, SA_PARAM_ALL,
2022 &softc->media_blksize,
2023 &softc->media_density,
2024 &softc->media_numblks,
2025 &softc->buffer_mode, &write_protect,
2026 &softc->speed, &comp_supported,
2027 &comp_enabled, &softc->comp_algorithm,
2032 * We could work a little harder here. We could
2033 * adjust our attempts to get information. It
2034 * might be an ancient tape drive. If someone
2035 * nudges us, we'll do that.
2041 * If no quirk has determined that this is a device that is
2042 * preferred to be in fixed or variable mode, now is the time
2045 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2048 * This could be expensive to find out. Luckily we
2049 * only need to do this once. If we start out in
2050 * 'default' mode, try and set ourselves to one
2051 * of the densities that would determine a wad
2052 * of other stuff. Go from highest to lowest.
2054 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2056 static u_int8_t ctry[] = {
2057 SCSI_DENSITY_HALFINCH_PE,
2058 SCSI_DENSITY_HALFINCH_6250C,
2059 SCSI_DENSITY_HALFINCH_6250,
2060 SCSI_DENSITY_HALFINCH_1600,
2061 SCSI_DENSITY_HALFINCH_800,
2062 SCSI_DENSITY_QIC_4GB,
2063 SCSI_DENSITY_QIC_2GB,
2064 SCSI_DENSITY_QIC_525_320,
2065 SCSI_DENSITY_QIC_150,
2066 SCSI_DENSITY_QIC_120,
2067 SCSI_DENSITY_QIC_24,
2068 SCSI_DENSITY_QIC_11_9TRK,
2069 SCSI_DENSITY_QIC_11_4TRK,
2070 SCSI_DENSITY_QIC_1320,
2071 SCSI_DENSITY_QIC_3080,
2074 for (i = 0; ctry[i]; i++) {
2075 error = sasetparams(periph,
2076 SA_PARAM_DENSITY, 0, ctry[i],
2079 softc->media_density = ctry[i];
2084 switch (softc->media_density) {
2085 case SCSI_DENSITY_QIC_11_4TRK:
2086 case SCSI_DENSITY_QIC_11_9TRK:
2087 case SCSI_DENSITY_QIC_24:
2088 case SCSI_DENSITY_QIC_120:
2089 case SCSI_DENSITY_QIC_150:
2090 case SCSI_DENSITY_QIC_525_320:
2091 case SCSI_DENSITY_QIC_1320:
2092 case SCSI_DENSITY_QIC_3080:
2093 softc->quirks &= ~SA_QUIRK_2FM;
2094 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2095 softc->last_media_blksize = 512;
2097 case SCSI_DENSITY_QIC_4GB:
2098 case SCSI_DENSITY_QIC_2GB:
2099 softc->quirks &= ~SA_QUIRK_2FM;
2100 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2101 softc->last_media_blksize = 1024;
2104 softc->last_media_blksize =
2105 softc->media_blksize;
2106 softc->quirks |= SA_QUIRK_VARIABLE;
2112 * If no quirk has determined that this is a device that needs
2113 * to have 2 Filemarks at EOD, now is the time to find out.
2116 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2117 switch (softc->media_density) {
2118 case SCSI_DENSITY_HALFINCH_800:
2119 case SCSI_DENSITY_HALFINCH_1600:
2120 case SCSI_DENSITY_HALFINCH_6250:
2121 case SCSI_DENSITY_HALFINCH_6250C:
2122 case SCSI_DENSITY_HALFINCH_PE:
2123 softc->quirks &= ~SA_QUIRK_1FM;
2124 softc->quirks |= SA_QUIRK_2FM;
2132 * Now validate that some info we got makes sense.
2134 if ((softc->max_blk < softc->media_blksize) ||
2135 (softc->min_blk > softc->media_blksize &&
2136 softc->media_blksize)) {
2137 xpt_print(periph->path,
2138 "BLOCK LIMITS (%d..%d) could not match current "
2139 "block settings (%d)- adjusting\n", softc->min_blk,
2140 softc->max_blk, softc->media_blksize);
2141 softc->max_blk = softc->min_blk =
2142 softc->media_blksize;
2146 * Now put ourselves into the right frame of mind based
2151 * If we want to be in FIXED mode and our current blocksize
2152 * is not equal to our last blocksize (if nonzero), try and
2153 * set ourselves to this last blocksize (as the 'preferred'
2154 * block size). The initial quirkmatch at registry sets the
2155 * initial 'last' blocksize. If, for whatever reason, this
2156 * 'last' blocksize is zero, set the blocksize to 512,
2157 * or min_blk if that's larger.
2159 if ((softc->quirks & SA_QUIRK_FIXED) &&
2160 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2161 (softc->media_blksize != softc->last_media_blksize)) {
2162 softc->media_blksize = softc->last_media_blksize;
2163 if (softc->media_blksize == 0) {
2164 softc->media_blksize = 512;
2165 if (softc->media_blksize < softc->min_blk) {
2166 softc->media_blksize = softc->min_blk;
2169 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2170 softc->media_blksize, 0, 0, SF_NO_PRINT);
2172 xpt_print(periph->path,
2173 "unable to set fixed blocksize to %d\n",
2174 softc->media_blksize);
2179 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2180 (softc->media_blksize != 0)) {
2181 softc->last_media_blksize = softc->media_blksize;
2182 softc->media_blksize = 0;
2183 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2184 0, 0, 0, SF_NO_PRINT);
2187 * If this fails and we were guessing, just
2188 * assume that we got it wrong and go try
2189 * fixed block mode. Don't even check against
2190 * density code at this point.
2193 softc->quirks &= ~SA_QUIRK_VARIABLE;
2194 softc->quirks |= SA_QUIRK_FIXED;
2195 if (softc->last_media_blksize == 0)
2196 softc->last_media_blksize = 512;
2199 xpt_print(periph->path,
2200 "unable to set variable blocksize\n");
2206 * Now that we have the current block size,
2207 * set up some parameters for sastart's usage.
2209 if (softc->media_blksize) {
2210 softc->flags |= SA_FLAG_FIXED;
2211 if (powerof2(softc->media_blksize)) {
2213 ffs(softc->media_blksize) - 1;
2214 softc->blk_mask = softc->media_blksize - 1;
2216 softc->blk_mask = ~0;
2217 softc->blk_shift = 0;
2221 * The SCSI-3 spec allows 0 to mean "unspecified".
2222 * The SCSI-1 spec allows 0 to mean 'infinite'.
2224 * Either works here.
2226 if (softc->max_blk == 0) {
2227 softc->max_blk = ~0;
2229 softc->blk_shift = 0;
2230 if (softc->blk_gran != 0) {
2231 softc->blk_mask = softc->blk_gran - 1;
2233 softc->blk_mask = 0;
2238 softc->flags |= SA_FLAG_TAPE_WP;
2240 if (comp_supported) {
2241 if (softc->saved_comp_algorithm == 0)
2242 softc->saved_comp_algorithm =
2243 softc->comp_algorithm;
2244 softc->flags |= SA_FLAG_COMP_SUPP;
2246 softc->flags |= SA_FLAG_COMP_ENABLED;
2248 softc->flags |= SA_FLAG_COMP_UNSUPP;
2250 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2251 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2252 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2255 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2257 xpt_print(periph->path,
2258 "unable to set buffered mode\n");
2260 error = 0; /* not an error */
2265 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2269 kfree(rblim, M_SCSISA);
2272 softc->dsreg = MTIO_DSREG_NIL;
2274 softc->fileno = softc->blkno = 0;
2275 softc->dsreg = MTIO_DSREG_REST;
2277 #ifdef SA_1FM_AT_EOD
2278 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2279 softc->quirks |= SA_QUIRK_1FM;
2281 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2282 softc->quirks |= SA_QUIRK_2FM;
2285 xpt_release_ccb(ccb);
2288 * If we return an error, we're not mounted any more,
2289 * so release any device reservation.
2292 sareservereleaseunit(periph, FALSE);
2295 * Clear I/O residual.
2297 softc->last_io_resid = 0;
2298 softc->last_ctl_resid = 0;
2304 * How many filemarks do we need to write if we were to terminate the
2305 * tape session right now? Note that this can be a negative number
2309 samarkswanted(struct cam_periph *periph)
2312 struct sa_softc *softc;
2314 softc = (struct sa_softc *)periph->softc;
2316 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2318 if (softc->quirks & SA_QUIRK_2FM)
2321 markswanted -= softc->filemarks;
2322 return (markswanted);
2326 sacheckeod(struct cam_periph *periph)
2331 markswanted = samarkswanted(periph);
2333 if (markswanted > 0) {
2334 error = sawritefilemarks(periph, markswanted, FALSE);
2342 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2344 static const char *toobig =
2345 "%d-byte tape record bigger than supplied buffer\n";
2346 struct cam_periph *periph;
2347 struct sa_softc *softc;
2348 struct ccb_scsiio *csio;
2349 struct scsi_sense_data *sense;
2350 u_int32_t resid = 0;
2353 int error_code, sense_key, asc, ascq, error, aqvalid;
2355 periph = xpt_path_periph(ccb->ccb_h.path);
2356 softc = (struct sa_softc *)periph->softc;
2358 sense = &csio->sense_data;
2359 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2360 aqvalid = sense->extra_len >= 6;
2363 status = csio->ccb_h.status & CAM_STATUS_MASK;
2366 * Calculate/latch up, any residuals... We do this in a funny 2-step
2367 * so we can print stuff here if we have CAM_DEBUG enabled for this
2370 if (status == CAM_SCSI_STATUS_ERROR) {
2371 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2372 info = (int32_t) scsi_4btoul(sense->info);
2374 if ((softc->flags & SA_FLAG_FIXED) != 0)
2375 resid *= softc->media_blksize;
2377 resid = csio->dxfer_len;
2379 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2380 if (softc->media_blksize)
2381 info /= softc->media_blksize;
2384 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2385 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2386 sizeof (struct scsi_sense_data));
2387 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2388 (int) csio->cdb_len);
2389 softc->last_io_resid = resid;
2390 softc->last_resid_was_io = 1;
2392 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2393 sizeof (struct scsi_sense_data));
2394 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2395 (int) csio->cdb_len);
2396 softc->last_ctl_resid = resid;
2397 softc->last_resid_was_io = 0;
2399 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2400 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %d "
2401 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2402 sense_key, asc, ascq, status,
2403 sense->flags & ~SSD_KEY_RESERVED, resid, csio->dxfer_len));
2405 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2406 ("Cam Status 0x%x\n", status));
2412 case CAM_SCSI_STATUS_ERROR:
2414 * If a read/write command, we handle it here.
2416 if (CCB_Type(csio) != SA_CCB_WAITING) {
2420 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2421 * on a non read/write command, we assume it's not an error
2422 * and propagate the residule and return.
2424 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2425 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2426 csio->resid = resid;
2431 * Otherwise, we let the common code handle this.
2433 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2437 * We cannot depend upon CAM honoring retry counts for these.
2439 case CAM_SCSI_BUS_RESET:
2441 if (ccb->ccb_h.retry_count <= 0) {
2446 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2450 * Handle filemark, end of tape, mismatched record sizes....
2451 * From this point out, we're only handling read/write cases.
2452 * Handle writes && reads differently.
2455 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2456 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2457 csio->resid = resid;
2459 } else if (sense->flags & SSD_EOM) {
2460 softc->flags |= SA_FLAG_EOM_PENDING;
2462 * Grotesque as it seems, the few times
2463 * I've actually seen a non-zero resid,
2464 * the tape drive actually lied and had
2465 * written all the data!.
2470 csio->resid = resid;
2471 if (sense_key == SSD_KEY_BLANK_CHECK) {
2472 if (softc->quirks & SA_QUIRK_1FM) {
2474 softc->flags |= SA_FLAG_EOM_PENDING;
2478 } else if (sense->flags & SSD_FILEMARK) {
2479 if (softc->flags & SA_FLAG_FIXED) {
2481 softc->flags |= SA_FLAG_EOF_PENDING;
2484 * Unconditionally, if we detected a filemark on a read,
2485 * mark that we've run moved a file ahead.
2487 if (softc->fileno != (daddr_t) -1) {
2490 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2496 * Incorrect Length usually applies to read, but can apply to writes.
2498 if (error == 0 && (sense->flags & SSD_ILI)) {
2500 xpt_print(csio->ccb_h.path, toobig,
2501 csio->dxfer_len - info);
2502 csio->resid = csio->dxfer_len;
2505 csio->resid = resid;
2506 if (softc->flags & SA_FLAG_FIXED) {
2507 softc->flags |= SA_FLAG_EIO_PENDING;
2510 * Bump the block number if we hadn't seen a filemark.
2511 * Do this independent of errors (we've moved anyway).
2513 if ((sense->flags & SSD_FILEMARK) == 0) {
2514 if (softc->blkno != (daddr_t) -1) {
2516 csio->ccb_h.ccb_pflags |=
2517 SA_POSITION_UPDATED;
2525 * Unfreeze the queue if frozen as we're not returning anything
2526 * to our waiters that would indicate an I/O error has occurred
2536 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2537 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2538 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2539 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2544 struct scsi_mode_header_6 *mode_hdr;
2545 struct scsi_mode_blk_desc *mode_blk;
2546 int mode_buffer_len;
2547 struct sa_softc *softc;
2552 softc = (struct sa_softc *)periph->softc;
2553 ccb = cam_periph_getccb(periph, 1);
2554 Set_CCB_Type(ccb, SA_CCB_POLLED);
2555 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2556 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2558 cpage = SA_DATA_COMPRESSION_PAGE;
2561 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2563 if (params_to_get & SA_PARAM_COMPRESSION) {
2564 if (softc->quirks & SA_QUIRK_NOCOMP) {
2565 *comp_supported = FALSE;
2566 params_to_get &= ~SA_PARAM_COMPRESSION;
2568 mode_buffer_len += sizeof (sa_comp_t);
2571 mode_buffer = kmalloc(mode_buffer_len, M_SCSISA, M_INTWAIT | M_ZERO);
2572 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2573 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2575 /* it is safe to retry this */
2576 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2577 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2578 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2579 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2581 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2582 &softc->device_stats);
2585 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2587 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2589 * Hmm. Let's see if we can try another page...
2590 * If we've already done that, give up on compression
2591 * for this device and remember this for the future
2592 * and attempt the request without asking for compression
2595 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2596 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2599 softc->quirks |= SA_QUIRK_NOCOMP;
2600 kfree(mode_buffer, M_SCSISA);
2602 } else if (status == CAM_SCSI_STATUS_ERROR) {
2603 /* Tell the user about the fatal error. */
2604 scsi_sense_print(&ccb->csio);
2605 goto sagetparamsexit;
2609 * If the user only wants the compression information, and
2610 * the device doesn't send back the block descriptor, it's
2611 * no big deal. If the user wants more than just
2612 * compression, though, and the device doesn't pass back the
2613 * block descriptor, we need to send another mode sense to
2614 * get the block descriptor.
2616 if ((mode_hdr->blk_desc_len == 0) &&
2617 (params_to_get & SA_PARAM_COMPRESSION) &&
2618 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2621 * Decrease the mode buffer length by the size of
2622 * the compression page, to make sure the data
2623 * there doesn't get overwritten.
2625 mode_buffer_len -= sizeof (sa_comp_t);
2628 * Now move the compression page that we presumably
2629 * got back down the memory chunk a little bit so
2630 * it doesn't get spammed.
2632 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2633 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2636 * Now, we issue another mode sense and just ask
2637 * for the block descriptor, etc.
2640 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2641 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2642 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2645 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2646 &softc->device_stats);
2650 goto sagetparamsexit;
2653 if (params_to_get & SA_PARAM_BLOCKSIZE)
2654 *blocksize = scsi_3btoul(mode_blk->blklen);
2656 if (params_to_get & SA_PARAM_NUMBLOCKS)
2657 *numblocks = scsi_3btoul(mode_blk->nblocks);
2659 if (params_to_get & SA_PARAM_BUFF_MODE)
2660 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2662 if (params_to_get & SA_PARAM_DENSITY)
2663 *density = mode_blk->density;
2665 if (params_to_get & SA_PARAM_WP)
2666 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2668 if (params_to_get & SA_PARAM_SPEED)
2669 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2671 if (params_to_get & SA_PARAM_COMPRESSION) {
2672 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2673 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2674 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2676 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2678 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2679 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2681 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2683 * We don't really know whether this device supports
2684 * Data Compression if the the algorithm field is
2685 * zero. Just say we do.
2687 *comp_supported = TRUE;
2689 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2690 *comp_algorithm = cp->sel_comp_alg;
2693 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2696 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2698 char *xyz = mode_buffer;
2699 xpt_print_path(periph->path);
2700 kprintf("Mode Sense Data=");
2701 for (idx = 0; idx < mode_buffer_len; idx++)
2702 kprintf(" 0x%02x", xyz[idx] & 0xff);
2708 xpt_release_ccb(ccb);
2709 kfree(mode_buffer, M_SCSISA);
2714 * The purpose of this function is to set one of four different parameters
2718 * - compression / compression algorithm
2721 * The assumption is that this will be called from saioctl(), and therefore
2722 * from a process context. Thus the waiting malloc calls below. If that
2723 * assumption ever changes, the malloc calls should be changed to be
2726 * Any or all of the four parameters may be set when this function is
2727 * called. It should handle setting more than one parameter at once.
2730 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2731 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2732 u_int32_t sense_flags)
2734 struct sa_softc *softc;
2735 u_int32_t current_blocksize = 0;/* silence gcc */
2736 u_int32_t current_calg;
2737 u_int8_t current_density = 0; /* silence gcc */
2738 u_int8_t current_speed = 0; /* silence gcc */
2739 int comp_enabled, comp_supported;
2741 int mode_buffer_len;
2742 struct scsi_mode_header_6 *mode_hdr;
2743 struct scsi_mode_blk_desc *mode_blk;
2744 sa_comp_t *ccomp, *cpage;
2746 union ccb *ccb = NULL;
2749 softc = (struct sa_softc *)periph->softc;
2751 ccomp = kmalloc(sizeof (sa_comp_t), M_SCSISA, M_INTWAIT);
2754 * Since it doesn't make sense to set the number of blocks, or
2755 * write protection, we won't try to get the current value. We
2756 * always want to get the blocksize, so we can set it back to the
2759 error = sagetparams(periph,
2760 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2761 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2762 ¤t_speed, &comp_supported, &comp_enabled,
2763 ¤t_calg, ccomp);
2766 kfree(ccomp, M_SCSISA);
2770 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2771 if (params_to_set & SA_PARAM_COMPRESSION)
2772 mode_buffer_len += sizeof (sa_comp_t);
2774 mode_buffer = kmalloc(mode_buffer_len, M_SCSISA, M_INTWAIT | M_ZERO);
2776 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2777 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2779 ccb = cam_periph_getccb(periph, 1);
2780 Set_CCB_Type(ccb, SA_CCB_POLLED);
2784 if (params_to_set & SA_PARAM_COMPRESSION) {
2786 cpage = (sa_comp_t *)&mode_blk[1];
2788 cpage = (sa_comp_t *)&mode_hdr[1];
2790 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2791 cpage->hdr.pagecode &= ~0x80;
2796 * If the caller wants us to set the blocksize, use the one they
2797 * pass in. Otherwise, use the blocksize we got back from the
2798 * mode select above.
2801 if (params_to_set & SA_PARAM_BLOCKSIZE)
2802 scsi_ulto3b(blocksize, mode_blk->blklen);
2804 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2807 * Set density if requested, else preserve old density.
2808 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2809 * devices, else density we've latched up in our softc.
2811 if (params_to_set & SA_PARAM_DENSITY) {
2812 mode_blk->density = density;
2813 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2814 mode_blk->density = SCSI_SAME_DENSITY;
2816 mode_blk->density = softc->media_density;
2821 * For mode selects, these two fields must be zero.
2823 mode_hdr->data_length = 0;
2824 mode_hdr->medium_type = 0;
2826 /* set the speed to the current value */
2827 mode_hdr->dev_spec = current_speed;
2829 /* if set, set single-initiator buffering mode */
2830 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
2831 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2835 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2837 mode_hdr->blk_desc_len = 0;
2840 * First, if the user wants us to set the compression algorithm or
2841 * just turn compression on, check to make sure that this drive
2842 * supports compression.
2844 if (params_to_set & SA_PARAM_COMPRESSION) {
2846 * If the compression algorithm is 0, disable compression.
2847 * If the compression algorithm is non-zero, enable
2848 * compression and set the compression type to the
2849 * specified compression algorithm, unless the algorithm is
2850 * MT_COMP_ENABLE. In that case, we look at the
2851 * compression algorithm that is currently set and if it is
2852 * non-zero, we leave it as-is. If it is zero, and we have
2853 * saved a compression algorithm from a time when
2854 * compression was enabled before, set the compression to
2857 switch (ccomp->hdr.pagecode & ~0x80) {
2858 case SA_DEVICE_CONFIGURATION_PAGE:
2860 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2862 dcp->sel_comp_alg = SA_COMP_NONE;
2865 if (calg != MT_COMP_ENABLE) {
2866 dcp->sel_comp_alg = calg;
2867 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2868 softc->saved_comp_algorithm != 0) {
2869 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2873 case SA_DATA_COMPRESSION_PAGE:
2874 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2875 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2878 * Disable compression, but leave the
2879 * decompression and the capability bit
2882 dcp->dce_and_dcc = SA_DCP_DCC;
2883 dcp->dde_and_red |= SA_DCP_DDE;
2886 /* enable compression && decompression */
2887 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2888 dcp->dde_and_red |= SA_DCP_DDE;
2890 * If there, use compression algorithm from caller.
2891 * Otherwise, if there's a saved compression algorithm
2892 * and there is no current algorithm, use the saved
2893 * algorithm. Else parrot back what we got and hope
2896 if (calg != MT_COMP_ENABLE) {
2897 scsi_ulto4b(calg, dcp->comp_algorithm);
2898 scsi_ulto4b(calg, dcp->decomp_algorithm);
2899 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2900 softc->saved_comp_algorithm != 0) {
2901 scsi_ulto4b(softc->saved_comp_algorithm,
2902 dcp->comp_algorithm);
2903 scsi_ulto4b(softc->saved_comp_algorithm,
2904 dcp->decomp_algorithm);
2909 * Compression does not appear to be supported-
2910 * at least via the DATA COMPRESSION page. It
2911 * would be too much to ask us to believe that
2912 * the page itself is supported, but incorrectly
2913 * reports an ability to manipulate data compression,
2914 * so we'll assume that this device doesn't support
2915 * compression. We can just fall through for that.
2920 * The drive doesn't seem to support compression,
2921 * so turn off the set compression bit.
2923 params_to_set &= ~SA_PARAM_COMPRESSION;
2924 xpt_print(periph->path,
2925 "device does not seem to support compression\n");
2928 * If that was the only thing the user wanted us to set,
2929 * clean up allocated resources and return with
2930 * 'operation not supported'.
2932 if (params_to_set == SA_PARAM_NONE) {
2933 kfree(mode_buffer, M_SCSISA);
2934 xpt_release_ccb(ccb);
2939 * That wasn't the only thing the user wanted us to set.
2940 * So, decrease the stated mode buffer length by the
2941 * size of the compression mode page.
2943 mode_buffer_len -= sizeof(sa_comp_t);
2947 /* It is safe to retry this operation */
2948 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2949 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2950 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2952 error = cam_periph_runccb(ccb, saerror, 0,
2953 sense_flags, &softc->device_stats);
2956 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2958 char *xyz = mode_buffer;
2959 xpt_print_path(periph->path);
2960 kprintf("Err%d, Mode Select Data=", error);
2961 for (idx = 0; idx < mode_buffer_len; idx++)
2962 kprintf(" 0x%02x", xyz[idx] & 0xff);
2969 * If we can, try without setting density/blocksize.
2972 if ((params_to_set &
2973 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2978 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2979 cpage = (sa_comp_t *)&mode_blk[1];
2983 * If we were setting the blocksize, and that failed, we
2984 * want to set it to its original value. If we weren't
2985 * setting the blocksize, we don't want to change it.
2987 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2990 * Set density if requested, else preserve old density.
2991 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2992 * devices, else density we've latched up in our softc.
2994 if (params_to_set & SA_PARAM_DENSITY) {
2995 mode_blk->density = current_density;
2996 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2997 mode_blk->density = SCSI_SAME_DENSITY;
2999 mode_blk->density = softc->media_density;
3002 if (params_to_set & SA_PARAM_COMPRESSION)
3003 bcopy(ccomp, cpage, sizeof (sa_comp_t));
3006 * The retry count is the only CCB field that might have been
3007 * changed that we care about, so reset it back to 1.
3009 ccb->ccb_h.retry_count = 1;
3010 cam_periph_runccb(ccb, saerror, 0, sense_flags,
3011 &softc->device_stats);
3015 xpt_release_ccb(ccb);
3018 kfree(ccomp, M_SCSISA);
3020 if (params_to_set & SA_PARAM_COMPRESSION) {
3022 softc->flags &= ~SA_FLAG_COMP_ENABLED;
3024 * Even if we get an error setting compression,
3025 * do not say that we don't support it. We could
3026 * have been wrong, or it may be media specific.
3027 * softc->flags &= ~SA_FLAG_COMP_SUPP;
3029 softc->saved_comp_algorithm = softc->comp_algorithm;
3030 softc->comp_algorithm = 0;
3032 softc->flags |= SA_FLAG_COMP_ENABLED;
3033 softc->comp_algorithm = calg;
3037 kfree(mode_buffer, M_SCSISA);
3042 saprevent(struct cam_periph *periph, int action)
3044 struct sa_softc *softc;
3048 softc = (struct sa_softc *)periph->softc;
3050 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3052 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3056 * We can be quiet about illegal requests.
3058 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3063 ccb = cam_periph_getccb(periph, 1);
3064 Set_CCB_Type(ccb, SA_CCB_POLLED);
3066 /* It is safe to retry this operation */
3067 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3068 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3070 error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats);
3073 if (action == PR_ALLOW)
3074 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3076 softc->flags |= SA_FLAG_TAPE_LOCKED;
3079 xpt_release_ccb(ccb);
3083 sarewind(struct cam_periph *periph)
3086 struct sa_softc *softc;
3089 softc = (struct sa_softc *)periph->softc;
3091 ccb = cam_periph_getccb(periph, 1);
3092 Set_CCB_Type(ccb, SA_CCB_POLLED);
3094 /* It is safe to retry this operation */
3095 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3096 SSD_FULL_SIZE, REWIND_TIMEOUT);
3098 softc->dsreg = MTIO_DSREG_REW;
3099 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3100 softc->dsreg = MTIO_DSREG_REST;
3102 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3103 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3105 xpt_release_ccb(ccb);
3107 softc->fileno = softc->blkno = (daddr_t) 0;
3109 softc->fileno = softc->blkno = (daddr_t) -1;
3114 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3117 struct sa_softc *softc;
3120 softc = (struct sa_softc *)periph->softc;
3122 ccb = cam_periph_getccb(periph, 1);
3123 Set_CCB_Type(ccb, SA_CCB_POLLED);
3125 /* This cannot be retried */
3127 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3128 SSD_FULL_SIZE, SPACE_TIMEOUT);
3131 * Clear residual because we will be using it.
3133 softc->last_ctl_resid = 0;
3135 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3136 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3137 softc->dsreg = MTIO_DSREG_REST;
3139 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3140 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3142 xpt_release_ccb(ccb);
3145 * If a spacing operation has failed, we need to invalidate
3148 * If the spacing operation was setmarks or to end of recorded data,
3149 * we no longer know our relative position.
3151 * If the spacing operations was spacing files in reverse, we
3152 * take account of the residual, but still check against less
3153 * than zero- if we've gone negative, we must have hit BOT.
3155 * If the spacing operations was spacing records in reverse and
3156 * we have a residual, we've either hit BOT or hit a filemark.
3157 * In the former case, we know our new record number (0). In
3158 * the latter case, we have absolutely no idea what the real
3159 * record number is- we've stopped between the end of the last
3160 * record in the previous file and the filemark that stopped
3161 * our spacing backwards.
3164 softc->fileno = softc->blkno = (daddr_t) -1;
3165 } else if (code == SS_SETMARKS || code == SS_EOD) {
3166 softc->fileno = softc->blkno = (daddr_t) -1;
3167 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3168 softc->fileno += (count - softc->last_ctl_resid);
3169 if (softc->fileno < 0) /* we must of hit BOT */
3172 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3173 softc->blkno += (count - softc->last_ctl_resid);
3175 if (softc->last_ctl_resid || softc->blkno < 0) {
3176 if (softc->fileno == 0) {
3179 softc->blkno = (daddr_t) -1;
3188 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3191 struct sa_softc *softc;
3194 softc = (struct sa_softc *)periph->softc;
3195 if (softc->open_rdonly)
3198 ccb = cam_periph_getccb(periph, 1);
3199 Set_CCB_Type(ccb, SA_CCB_POLLED);
3202 * Clear residual because we will be using it.
3204 softc->last_ctl_resid = 0;
3206 softc->dsreg = MTIO_DSREG_FMK;
3207 /* this *must* not be retried */
3208 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3209 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3210 softc->dsreg = MTIO_DSREG_REST;
3213 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3215 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3216 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3218 if (error == 0 && nmarks) {
3219 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3220 nwm = nmarks - softc->last_ctl_resid;
3221 softc->filemarks += nwm;
3224 xpt_release_ccb(ccb);
3227 * Update relative positions (if we're doing that).
3230 softc->fileno = softc->blkno = (daddr_t) -1;
3231 } else if (softc->fileno != (daddr_t) -1) {
3232 softc->fileno += nwm;
3239 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3241 struct scsi_tape_position_data loc;
3243 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3247 * We try and flush any buffered writes here if we were writing
3248 * and we're trying to get hardware block position. It eats
3249 * up performance substantially, but I'm wary of drive firmware.
3251 * I think that *logical* block position is probably okay-
3252 * but hardware block position might have to wait for data
3253 * to hit media to be valid. Caveat Emptor.
3256 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3257 error = sawritefilemarks(periph, 0, 0);
3258 if (error && error != EACCES)
3262 ccb = cam_periph_getccb(periph, 1);
3263 Set_CCB_Type(ccb, SA_CCB_POLLED);
3264 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3265 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3266 softc->dsreg = MTIO_DSREG_RBSY;
3267 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3268 softc->dsreg = MTIO_DSREG_REST;
3269 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3270 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3273 if (loc.flags & SA_RPOS_UNCERTAIN) {
3274 error = EINVAL; /* nothing is certain */
3276 *blkptr = scsi_4btoul(loc.firstblk);
3280 xpt_release_ccb(ccb);
3285 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3288 struct sa_softc *softc;
3292 * We used to try and flush any buffered writes here.
3293 * Now we push this onto user applications to either
3294 * flush the pending writes themselves (via a zero count
3295 * WRITE FILEMARKS command) or they can trust their tape
3296 * drive to do this correctly for them.
3299 softc = (struct sa_softc *)periph->softc;
3300 ccb = cam_periph_getccb(periph, 1);
3301 Set_CCB_Type(ccb, SA_CCB_POLLED);
3303 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3304 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3307 softc->dsreg = MTIO_DSREG_POS;
3308 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3309 softc->dsreg = MTIO_DSREG_REST;
3310 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3311 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3312 xpt_release_ccb(ccb);
3314 * Note relative file && block number position as now unknown.
3316 softc->fileno = softc->blkno = (daddr_t) -1;
3321 saretension(struct cam_periph *periph)
3324 struct sa_softc *softc;
3327 softc = (struct sa_softc *)periph->softc;
3329 ccb = cam_periph_getccb(periph, 1);
3330 Set_CCB_Type(ccb, SA_CCB_POLLED);
3332 /* It is safe to retry this operation */
3333 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3334 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3336 softc->dsreg = MTIO_DSREG_TEN;
3337 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3338 softc->dsreg = MTIO_DSREG_REST;
3340 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3341 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3342 xpt_release_ccb(ccb);
3344 softc->fileno = softc->blkno = (daddr_t) 0;
3346 softc->fileno = softc->blkno = (daddr_t) -1;
3351 sareservereleaseunit(struct cam_periph *periph, int reserve)
3354 struct sa_softc *softc;
3357 softc = (struct sa_softc *)periph->softc;
3358 ccb = cam_periph_getccb(periph, 1);
3359 Set_CCB_Type(ccb, SA_CCB_POLLED);
3361 /* It is safe to retry this operation */
3362 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3363 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3364 softc->dsreg = MTIO_DSREG_RBSY;
3365 error = cam_periph_runccb(ccb, saerror, 0,
3366 SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats);
3367 softc->dsreg = MTIO_DSREG_REST;
3369 xpt_release_ccb(ccb);
3372 * If the error was Illegal Request, then the device doesn't support
3373 * RESERVE/RELEASE. This is not an error.
3375 if (error == EINVAL) {
3383 saloadunload(struct cam_periph *periph, int load)
3386 struct sa_softc *softc;
3389 softc = (struct sa_softc *)periph->softc;
3391 ccb = cam_periph_getccb(periph, 1);
3392 Set_CCB_Type(ccb, SA_CCB_POLLED);
3394 /* It is safe to retry this operation */
3395 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3396 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3398 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3399 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3400 softc->dsreg = MTIO_DSREG_REST;
3402 xpt_release_ccb(ccb);
3404 if (error || load == 0)
3405 softc->fileno = softc->blkno = (daddr_t) -1;
3406 else if (error == 0)
3407 softc->fileno = softc->blkno = (daddr_t) 0;
3412 saerase(struct cam_periph *periph, int longerase)
3416 struct sa_softc *softc;
3419 softc = (struct sa_softc *)periph->softc;
3420 if (softc->open_rdonly)
3423 ccb = cam_periph_getccb(periph, 1);
3424 Set_CCB_Type(ccb, SA_CCB_POLLED);
3426 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3427 SSD_FULL_SIZE, ERASE_TIMEOUT);
3429 softc->dsreg = MTIO_DSREG_ZER;
3430 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3431 softc->dsreg = MTIO_DSREG_REST;
3433 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3434 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3435 xpt_release_ccb(ccb);
3439 #endif /* _KERNEL */
3442 * Read tape block limits command.
3445 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3446 void (*cbfcnp)(struct cam_periph *, union ccb *),
3447 u_int8_t tag_action,
3448 struct scsi_read_block_limits_data *rlimit_buf,
3449 u_int8_t sense_len, u_int32_t timeout)
3451 struct scsi_read_block_limits *scsi_cmd;
3453 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3454 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3455 sizeof(*scsi_cmd), timeout);
3457 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3458 bzero(scsi_cmd, sizeof(*scsi_cmd));
3459 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3463 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3464 void (*cbfcnp)(struct cam_periph *, union ccb *),
3465 u_int8_t tag_action, int readop, int sli,
3466 int fixed, u_int32_t length, u_int8_t *data_ptr,
3467 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3469 struct scsi_sa_rw *scsi_cmd;
3471 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3472 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3473 scsi_cmd->sli_fixed = 0;
3475 scsi_cmd->sli_fixed |= SAR_SLI;
3477 scsi_cmd->sli_fixed |= SARW_FIXED;
3478 scsi_ulto3b(length, scsi_cmd->length);
3479 scsi_cmd->control = 0;
3481 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3482 tag_action, data_ptr, dxfer_len, sense_len,
3483 sizeof(*scsi_cmd), timeout);
3487 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3488 void (*cbfcnp)(struct cam_periph *, union ccb *),
3489 u_int8_t tag_action, int immediate, int eot,
3490 int reten, int load, u_int8_t sense_len,
3493 struct scsi_load_unload *scsi_cmd;
3495 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3496 bzero(scsi_cmd, sizeof(*scsi_cmd));
3497 scsi_cmd->opcode = LOAD_UNLOAD;
3499 scsi_cmd->immediate = SLU_IMMED;
3501 scsi_cmd->eot_reten_load |= SLU_EOT;
3503 scsi_cmd->eot_reten_load |= SLU_RETEN;
3505 scsi_cmd->eot_reten_load |= SLU_LOAD;
3507 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3508 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3512 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3513 void (*cbfcnp)(struct cam_periph *, union ccb *),
3514 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3517 struct scsi_rewind *scsi_cmd;
3519 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3520 bzero(scsi_cmd, sizeof(*scsi_cmd));
3521 scsi_cmd->opcode = REWIND;
3523 scsi_cmd->immediate = SREW_IMMED;
3525 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3526 0, sense_len, sizeof(*scsi_cmd), timeout);
3530 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3531 void (*cbfcnp)(struct cam_periph *, union ccb *),
3532 u_int8_t tag_action, scsi_space_code code,
3533 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3535 struct scsi_space *scsi_cmd;
3537 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3538 scsi_cmd->opcode = SPACE;
3539 scsi_cmd->code = code;
3540 scsi_ulto3b(count, scsi_cmd->count);
3541 scsi_cmd->control = 0;
3543 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3544 0, sense_len, sizeof(*scsi_cmd), timeout);
3548 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3549 void (*cbfcnp)(struct cam_periph *, union ccb *),
3550 u_int8_t tag_action, int immediate, int setmark,
3551 u_int32_t num_marks, u_int8_t sense_len,
3554 struct scsi_write_filemarks *scsi_cmd;
3556 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3557 bzero(scsi_cmd, sizeof(*scsi_cmd));
3558 scsi_cmd->opcode = WRITE_FILEMARKS;
3560 scsi_cmd->byte2 |= SWFMRK_IMMED;
3562 scsi_cmd->byte2 |= SWFMRK_WSMK;
3564 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3566 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3567 0, sense_len, sizeof(*scsi_cmd), timeout);
3571 * The reserve and release unit commands differ only by their opcodes.
3574 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3575 void (*cbfcnp)(struct cam_periph *, union ccb *),
3576 u_int8_t tag_action, int third_party,
3577 int third_party_id, u_int8_t sense_len,
3578 u_int32_t timeout, int reserve)
3580 struct scsi_reserve_release_unit *scsi_cmd;
3582 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3583 bzero(scsi_cmd, sizeof(*scsi_cmd));
3586 scsi_cmd->opcode = RESERVE_UNIT;
3588 scsi_cmd->opcode = RELEASE_UNIT;
3591 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3592 scsi_cmd->lun_thirdparty |=
3593 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3596 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3597 0, sense_len, sizeof(*scsi_cmd), timeout);
3601 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3602 void (*cbfcnp)(struct cam_periph *, union ccb *),
3603 u_int8_t tag_action, int immediate, int long_erase,
3604 u_int8_t sense_len, u_int32_t timeout)
3606 struct scsi_erase *scsi_cmd;
3608 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3609 bzero(scsi_cmd, sizeof(*scsi_cmd));
3611 scsi_cmd->opcode = ERASE;
3614 scsi_cmd->lun_imm_long |= SE_IMMED;
3617 scsi_cmd->lun_imm_long |= SE_LONG;
3619 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3620 0, sense_len, sizeof(*scsi_cmd), timeout);
3624 * Read Tape Position command.
3627 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3628 void (*cbfcnp)(struct cam_periph *, union ccb *),
3629 u_int8_t tag_action, int hardsoft,
3630 struct scsi_tape_position_data *sbp,
3631 u_int8_t sense_len, u_int32_t timeout)
3633 struct scsi_tape_read_position *scmd;
3635 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3636 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3637 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3638 bzero(scmd, sizeof(*scmd));
3639 scmd->opcode = READ_POSITION;
3640 scmd->byte1 = hardsoft;
3644 * Set Tape Position command.
3647 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3648 void (*cbfcnp)(struct cam_periph *, union ccb *),
3649 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3650 u_int8_t sense_len, u_int32_t timeout)
3652 struct scsi_tape_locate *scmd;
3654 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3655 NULL, 0, sense_len, sizeof(*scmd), timeout);
3656 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3657 bzero(scmd, sizeof(*scmd));
3658 scmd->opcode = LOCATE;
3660 scmd->byte1 |= SA_SPOS_BT;
3661 scsi_ulto4b(blkno, scmd->blkaddr);