2 * $FreeBSD: src/sys/cam/scsi/scsi_sa.c,v 1.45.2.13 2002/12/17 17:08:50 trhodes Exp $
3 * $DragonFly: src/sys/bus/cam/scsi/scsi_sa.c,v 1.26 2007/11/25 18:14:26 pavalos Exp $
5 * Implementation of SCSI Sequential Access Peripheral driver for CAM.
7 * Copyright (c) 1999, 2000 Matthew Jacob
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
34 #include <sys/queue.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/types.h>
42 #include <sys/malloc.h>
49 #include <sys/thread2.h>
51 #include <sys/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
98 #ifndef UNUSED_PARAMETER
99 #define UNUSED_PARAMETER(x) x = x
103 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
104 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
112 SA_STATE_NORMAL, SA_STATE_ABNORMAL
115 #define ccb_pflags ppriv_field0
116 #define ccb_bio ppriv_ptr1
118 #define SA_CCB_BUFFER_IO 0x0
119 #define SA_CCB_WAITING 0x1
120 #define SA_CCB_TYPEMASK 0x1
121 #define SA_POSITION_UPDATED 0x2
123 #define Set_CCB_Type(x, type) \
124 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
125 x->ccb_h.ccb_pflags |= type
127 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
132 SA_FLAG_OPEN = 0x0001,
133 SA_FLAG_FIXED = 0x0002,
134 SA_FLAG_TAPE_LOCKED = 0x0004,
135 SA_FLAG_TAPE_MOUNTED = 0x0008,
136 SA_FLAG_TAPE_WP = 0x0010,
137 SA_FLAG_TAPE_WRITTEN = 0x0020,
138 SA_FLAG_EOM_PENDING = 0x0040,
139 SA_FLAG_EIO_PENDING = 0x0080,
140 SA_FLAG_EOF_PENDING = 0x0100,
141 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
142 SA_FLAG_EOF_PENDING),
143 SA_FLAG_INVALID = 0x0200,
144 SA_FLAG_COMP_ENABLED = 0x0400,
145 SA_FLAG_COMP_SUPP = 0x0800,
146 SA_FLAG_COMP_UNSUPP = 0x1000,
147 SA_FLAG_TAPE_FROZEN = 0x2000
151 SA_MODE_REWIND = 0x00,
152 SA_MODE_NOREWIND = 0x01,
153 SA_MODE_OFFLINE = 0x02
157 SA_PARAM_NONE = 0x00,
158 SA_PARAM_BLOCKSIZE = 0x01,
159 SA_PARAM_DENSITY = 0x02,
160 SA_PARAM_COMPRESSION = 0x04,
161 SA_PARAM_BUFF_MODE = 0x08,
162 SA_PARAM_NUMBLOCKS = 0x10,
164 SA_PARAM_SPEED = 0x40,
169 SA_QUIRK_NONE = 0x00,
170 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
171 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
172 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
173 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
174 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
175 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
176 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
177 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
180 /* units are bits 4-7, 16-21 (1024 units) */
181 #define SAUNIT(DEV) \
182 (((minor(DEV) & 0xF0) >> 4) | ((minor(DEV) & 0x3f0000) >> 16))
184 #define SAMODE(z) ((minor(z) & 0x3))
185 #define SADENSITY(z) (((minor(z) >> 2) & 0x3))
186 #define SA_IS_CTRL(z) (minor(z) & (1 << 29))
188 #define SA_NOT_CTLDEV 0
192 #define SA_ATYPE_NR 1
193 #define SA_ATYPE_ER 2
195 #define SAMINOR(ctl, unit, mode, access) \
196 ((ctl << 29) | ((unit & 0x3f0) << 16) | ((unit & 0xf) << 4) | \
197 (mode << 0x2) | (access & 0x3))
199 #define SA_UNITMASK SAMINOR(0, -1, 0, 0)
200 #define SA_UNIT(unit) SAMINOR(0, unit, 0, 0)
202 #define SA_NUM_MODES 4
208 struct bio_queue_head bio_queue;
210 struct devstat device_stats;
216 u_int32_t comp_algorithm;
217 u_int32_t saved_comp_algorithm;
218 u_int32_t media_blksize;
219 u_int32_t last_media_blksize;
220 u_int32_t media_numblks;
221 u_int8_t media_density;
224 u_int8_t dsreg; /* mtio mt_dsreg, redux */
228 int last_resid_was_io;
231 * Relative to BOT Location.
240 struct scsi_sense_data _last_io_sense;
241 u_int32_t _last_io_resid;
242 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
243 struct scsi_sense_data _last_ctl_sense;
244 u_int32_t _last_ctl_resid;
245 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
246 #define last_io_sense errinfo._last_io_sense
247 #define last_io_resid errinfo._last_io_resid
248 #define last_io_cdb errinfo._last_io_cdb
249 #define last_ctl_sense errinfo._last_ctl_sense
250 #define last_ctl_resid errinfo._last_ctl_resid
251 #define last_ctl_cdb errinfo._last_ctl_cdb
254 * Misc other flags/state
258 ctrl_mode : 1; /* control device open */
261 struct sa_quirk_entry {
262 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
263 sa_quirks quirks; /* specific quirk type */
264 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
267 static struct sa_quirk_entry sa_quirk_table[] =
270 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
271 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
272 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
275 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
276 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
279 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
280 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
283 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
284 "Python*", "*"}, SA_QUIRK_NODREAD, 0
287 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
288 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
291 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
292 "VIPER 2525 25462", "-011"},
293 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
296 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
297 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
301 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
302 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
306 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
307 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
310 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
311 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
314 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
315 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
318 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
319 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
322 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
323 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
326 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
327 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
329 { /* jreynold@primenet.com */
330 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
331 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
333 { /* mike@sentex.net */
334 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
335 "STT20000*", "*"}, SA_QUIRK_1FM, 0
338 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
339 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
342 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
343 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
346 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
347 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
350 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
351 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
354 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
355 " SLR*", "*"}, SA_QUIRK_1FM, 0
358 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
359 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
362 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
363 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
367 static d_open_t saopen;
368 static d_close_t saclose;
369 static d_strategy_t sastrategy;
370 static d_ioctl_t saioctl;
371 static periph_init_t sainit;
372 static periph_ctor_t saregister;
373 static periph_oninv_t saoninvalidate;
374 static periph_dtor_t sacleanup;
375 static periph_start_t sastart;
376 static void saasync(void *callback_arg, u_int32_t code,
377 struct cam_path *path, void *arg);
378 static void sadone(struct cam_periph *periph,
379 union ccb *start_ccb);
380 static int saerror(union ccb *ccb, u_int32_t cam_flags,
381 u_int32_t sense_flags);
382 static int samarkswanted(struct cam_periph *);
383 static int sacheckeod(struct cam_periph *periph);
384 static int sagetparams(struct cam_periph *periph,
385 sa_params params_to_get,
386 u_int32_t *blocksize, u_int8_t *density,
387 u_int32_t *numblocks, int *buff_mode,
388 u_int8_t *write_protect, u_int8_t *speed,
389 int *comp_supported, int *comp_enabled,
390 u_int32_t *comp_algorithm,
391 sa_comp_t *comp_page);
392 static int sasetparams(struct cam_periph *periph,
393 sa_params params_to_set,
394 u_int32_t blocksize, u_int8_t density,
395 u_int32_t comp_algorithm,
396 u_int32_t sense_flags);
397 static void saprevent(struct cam_periph *periph, int action);
398 static int sarewind(struct cam_periph *periph);
399 static int saspace(struct cam_periph *periph, int count,
400 scsi_space_code code);
401 static int samount(struct cam_periph *, int, cdev_t);
402 static int saretension(struct cam_periph *periph);
403 static int sareservereleaseunit(struct cam_periph *periph,
405 static int saloadunload(struct cam_periph *periph, int load);
406 static int saerase(struct cam_periph *periph, int longerase);
407 static int sawritefilemarks(struct cam_periph *periph,
408 int nmarks, int setmarks);
409 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
410 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
413 static struct periph_driver sadriver =
416 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
419 PERIPHDRIVER_DECLARE(sa, sadriver);
421 /* For 2.2-stable support */
426 #define SA_CDEV_MAJOR 14
428 static struct dev_ops sa_ops = {
429 { "sa", SA_CDEV_MAJOR, D_TAPE },
433 .d_write = physwrite,
435 .d_strategy = sastrategy,
438 static struct extend_array *saperiphs;
441 saopen(struct dev_open_args *ap)
443 cdev_t dev = ap->a_head.a_dev;
444 struct cam_periph *periph;
445 struct sa_softc *softc;
452 periph = cam_extend_get(saperiphs, unit);
453 if (periph == NULL) {
457 softc = (struct sa_softc *)periph->softc;
458 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
464 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
465 ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
467 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
468 cam_periph_unlock(periph);
471 if (SA_IS_CTRL(dev)) {
472 softc->ctrl_mode = 1;
473 cam_periph_unlock(periph);
478 if (softc->flags & SA_FLAG_OPEN) {
480 } else if (softc->flags & SA_FLAG_INVALID) {
484 * The function samount ensures media is loaded and ready.
485 * It also does a device RESERVE if the tape isn't yet mounted.
487 error = samount(periph, ap->a_oflags, dev);
491 cam_periph_release(periph);
493 saprevent(periph, PR_PREVENT);
494 softc->flags |= SA_FLAG_OPEN;
496 cam_periph_unlock(periph);
501 saclose(struct dev_close_args *ap)
503 cdev_t dev = ap->a_head.a_dev;
504 struct cam_periph *periph;
505 struct sa_softc *softc;
506 int unit, mode, error, writing, tmp;
507 int closedbits = SA_FLAG_OPEN;
511 periph = cam_extend_get(saperiphs, unit);
515 softc = (struct sa_softc *)periph->softc;
517 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
518 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
521 if ((error = cam_periph_lock(periph, 0)) != 0) {
525 if (SA_IS_CTRL(dev)) {
526 softc->ctrl_mode = 0;
527 cam_periph_release(periph);
528 cam_periph_unlock(periph);
533 * Were we writing the tape?
535 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
538 * See whether or not we need to write filemarks. If this
539 * fails, we probably have to assume we've lost tape
542 error = sacheckeod(periph);
544 xpt_print_path(periph->path);
545 kprintf("failed to write terminating filemark(s)\n");
546 softc->flags |= SA_FLAG_TAPE_FROZEN;
550 * Whatever we end up doing, allow users to eject tapes from here on.
552 saprevent(periph, PR_ALLOW);
555 * Decide how to end...
557 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
558 closedbits |= SA_FLAG_TAPE_FROZEN;
559 } else switch (mode) {
560 case SA_MODE_OFFLINE:
562 * An 'offline' close is an unconditional release of
563 * frozen && mount conditions, irrespective of whether
564 * these operations succeeded. The reason for this is
565 * to allow at least some kind of programmatic way
566 * around our state getting all fouled up. If somebody
567 * issues an 'offline' command, that will be allowed
571 saloadunload(periph, FALSE);
572 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
576 * If the rewind fails, return an error- if anyone cares,
577 * but not overwriting any previous error.
579 * We don't clear the notion of mounted here, but we do
580 * clear the notion of frozen if we successfully rewound.
582 tmp = sarewind(periph);
587 closedbits |= SA_FLAG_TAPE_FROZEN;
590 case SA_MODE_NOREWIND:
592 * If we're not rewinding/unloading the tape, find out
593 * whether we need to back up over one of two filemarks
594 * we wrote (if we wrote two filemarks) so that appends
595 * from this point on will be sane.
597 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
598 tmp = saspace(periph, -1, SS_FILEMARKS);
600 xpt_print_path(periph->path);
601 kprintf("unable to backspace over one of double"
602 " filemarks at end of tape\n");
603 xpt_print_path(periph->path);
604 kprintf("it is possible that this device"
605 " needs a SA_QUIRK_1FM quirk set for it\n");
606 softc->flags |= SA_FLAG_TAPE_FROZEN;
611 xpt_print_path(periph->path);
612 panic("unknown mode 0x%x in saclose", mode);
618 * We wish to note here that there are no more filemarks to be written.
620 softc->filemarks = 0;
621 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
624 * And we are no longer open for business.
626 softc->flags &= ~closedbits;
629 * Inform users if tape state if frozen....
631 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
632 xpt_print_path(periph->path);
633 kprintf("tape is now frozen- use an OFFLINE, REWIND or MTEOM "
634 "command to clear this state.\n");
637 /* release the device if it is no longer mounted */
638 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
639 sareservereleaseunit(periph, FALSE);
641 cam_periph_unlock(periph);
642 cam_periph_release(periph);
648 * Actually translate the requested transfer into one the physical driver
649 * can understand. The transfer is described by a buf and will include
650 * only one physical transfer.
653 sastrategy(struct dev_strategy_args *ap)
655 cdev_t dev = ap->a_head.a_dev;
656 struct bio *bio = ap->a_bio;
657 struct buf *bp = bio->bio_buf;
658 struct cam_periph *periph;
659 struct sa_softc *softc;
662 if (SA_IS_CTRL(dev)) {
663 bp->b_error = EINVAL;
667 periph = cam_extend_get(saperiphs, unit);
668 if (periph == NULL) {
672 softc = (struct sa_softc *)periph->softc;
676 if (softc->flags & SA_FLAG_INVALID) {
682 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
691 * If it's a null transfer, return immediatly
693 if (bp->b_bcount == 0)
697 if (softc->flags & SA_FLAG_FIXED) {
699 * Fixed block device. The byte count must
700 * be a multiple of our block size.
702 if (((softc->blk_mask != ~0) &&
703 ((bp->b_bcount & softc->blk_mask) != 0)) ||
704 ((softc->blk_mask == ~0) &&
705 ((bp->b_bcount % softc->min_blk) != 0))) {
706 xpt_print_path(periph->path);
707 kprintf("Invalid request. Fixed block device "
708 "requests must be a multiple "
709 "of %d bytes\n", softc->media_blksize);
710 bp->b_error = EINVAL;
713 } else if ((bp->b_bcount > softc->max_blk) ||
714 (bp->b_bcount < softc->min_blk) ||
715 (bp->b_bcount & softc->blk_mask) != 0) {
717 xpt_print_path(periph->path);
718 kprintf("Invalid request. Variable block device "
719 "requests must be ");
720 if (softc->blk_mask != 0) {
721 kprintf("a multiple of %d ", (0x1 << softc->blk_gran));
723 kprintf("between %d and %d bytes\n", softc->min_blk,
725 bp->b_error = EINVAL;
730 * Mask interrupts so that the device cannot be invalidated until
731 * after we are in the queue. Otherwise, we might not properly
732 * clean up one of the buffers.
737 * Place it at the end of the queue.
739 bioq_insert_tail(&softc->bio_queue, bio);
741 softc->queue_count++;
742 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("sastrategy: enqueuing a %d "
743 "%s byte %s queue count now %d\n", (int) bp->b_bcount,
744 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
745 (bp->b_cmd == BUF_CMD_READ)? "read" : "write", softc->queue_count));
750 * Schedule ourselves for performing the work.
752 xpt_schedule(periph, 1);
756 bp->b_flags |= B_ERROR;
760 * Correctly set the buf to indicate a completed xfer
762 bp->b_resid = bp->b_bcount;
768 saioctl(struct dev_ioctl_args *ap)
770 cdev_t dev = ap->a_head.a_dev;
771 caddr_t addr = ap->a_data;
772 struct cam_periph *periph;
773 struct sa_softc *softc;
774 scsi_space_code spaceop;
775 int didlockperiph = 0;
782 error = 0; /* shut up gcc */
783 spaceop = 0; /* shut up gcc */
785 periph = cam_extend_get(saperiphs, unit);
789 softc = (struct sa_softc *)periph->softc;
792 * Check for control mode accesses. We allow MTIOCGET and
793 * MTIOCERRSTAT (but need to be the only one open in order
794 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
795 * and MTCOMP (but need to be the only one accessing this
796 * device to run those).
799 if (SA_IS_CTRL(dev)) {
801 case MTIOCGETEOTMODEL:
806 * If the periph isn't already locked, lock it
807 * so our MTIOCERRSTAT can reset latched error stats.
809 * If the periph is already locked, skip it because
810 * we're just getting status and it'll be up to the
811 * other thread that has this device open to do
812 * an MTIOCERRSTAT that would clear latched status.
815 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
816 error = cam_periph_lock(periph, PCATCH);
826 case MTIOCSETEOTMODEL:
831 * We need to acquire the peripheral here rather
832 * than at open time because we are sharing writable
833 * access to data structures.
836 error = cam_periph_lock(periph, PCATCH);
851 * Find the device that the user is talking about
856 struct mtget *g = (struct mtget *)addr;
859 * If this isn't the control mode device, actually go out
860 * and ask the drive again what it's set to.
862 if (!SA_IS_CTRL(dev)) {
863 u_int8_t write_protect;
864 int comp_enabled, comp_supported;
865 error = sagetparams(periph, SA_PARAM_ALL,
866 &softc->media_blksize, &softc->media_density,
867 &softc->media_numblks, &softc->buffer_mode,
868 &write_protect, &softc->speed, &comp_supported,
869 &comp_enabled, &softc->comp_algorithm, NULL);
873 softc->flags |= SA_FLAG_TAPE_WP;
875 softc->flags &= ~SA_FLAG_TAPE_WP;
876 softc->flags &= ~(SA_FLAG_COMP_SUPP|
877 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
878 if (comp_supported) {
879 if (softc->saved_comp_algorithm == 0)
880 softc->saved_comp_algorithm =
881 softc->comp_algorithm;
882 softc->flags |= SA_FLAG_COMP_SUPP;
884 softc->flags |= SA_FLAG_COMP_ENABLED;
886 softc->flags |= SA_FLAG_COMP_UNSUPP;
888 bzero(g, sizeof(struct mtget));
889 g->mt_type = MT_ISAR;
890 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
891 g->mt_comp = MT_COMP_UNSUPP;
892 g->mt_comp0 = MT_COMP_UNSUPP;
893 g->mt_comp1 = MT_COMP_UNSUPP;
894 g->mt_comp2 = MT_COMP_UNSUPP;
895 g->mt_comp3 = MT_COMP_UNSUPP;
897 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
898 g->mt_comp = MT_COMP_DISABLED;
900 g->mt_comp = softc->comp_algorithm;
902 g->mt_comp0 = softc->comp_algorithm;
903 g->mt_comp1 = softc->comp_algorithm;
904 g->mt_comp2 = softc->comp_algorithm;
905 g->mt_comp3 = softc->comp_algorithm;
907 g->mt_density = softc->media_density;
908 g->mt_density0 = softc->media_density;
909 g->mt_density1 = softc->media_density;
910 g->mt_density2 = softc->media_density;
911 g->mt_density3 = softc->media_density;
912 g->mt_blksiz = softc->media_blksize;
913 g->mt_blksiz0 = softc->media_blksize;
914 g->mt_blksiz1 = softc->media_blksize;
915 g->mt_blksiz2 = softc->media_blksize;
916 g->mt_blksiz3 = softc->media_blksize;
917 g->mt_fileno = softc->fileno;
918 g->mt_blkno = softc->blkno;
919 g->mt_dsreg = (short) softc->dsreg;
921 * Yes, we know that this is likely to overflow
923 if (softc->last_resid_was_io) {
924 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
925 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
926 softc->last_io_resid = 0;
930 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
931 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
932 softc->last_ctl_resid = 0;
941 struct scsi_tape_errors *sep =
942 &((union mterrstat *)addr)->scsi_errstat;
944 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
945 ("saioctl: MTIOCERRSTAT\n"));
947 bzero(sep, sizeof(*sep));
948 sep->io_resid = softc->last_io_resid;
949 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
950 sizeof (sep->io_sense));
951 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
952 sizeof (sep->io_cdb));
953 sep->ctl_resid = softc->last_ctl_resid;
954 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
955 sizeof (sep->ctl_sense));
956 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
957 sizeof (sep->ctl_cdb));
959 if (SA_IS_CTRL(dev) == 0 || didlockperiph)
960 bzero((caddr_t) &softc->errinfo,
961 sizeof (softc->errinfo));
970 mt = (struct mtop *)addr;
972 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
973 ("saioctl: op=0x%x count=0x%x\n",
974 mt->mt_op, mt->mt_count));
976 count = mt->mt_count;
978 case MTWEOF: /* write an end-of-file marker */
980 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
981 * flag because by keeping track of filemarks
982 * we have last written we know ehether or not
983 * we need to write more when we close the device.
985 error = sawritefilemarks(periph, count, FALSE);
987 case MTWSS: /* write a setmark */
988 error = sawritefilemarks(periph, count, TRUE);
990 case MTBSR: /* backward space record */
991 case MTFSR: /* forward space record */
992 case MTBSF: /* backward space file */
993 case MTFSF: /* forward space file */
994 case MTBSS: /* backward space setmark */
995 case MTFSS: /* forward space setmark */
996 case MTEOD: /* space to end of recorded medium */
1000 spaceop = SS_FILEMARKS;
1001 nmarks = softc->filemarks;
1002 error = sacheckeod(periph);
1004 xpt_print_path(periph->path);
1005 kprintf("EOD check prior to spacing failed\n");
1006 softc->flags |= SA_FLAG_EIO_PENDING;
1009 nmarks -= softc->filemarks;
1015 spaceop = SS_BLOCKS;
1026 spaceop = SS_SETMARKS;
1040 nmarks = softc->filemarks;
1042 * XXX: Why are we checking again?
1044 error = sacheckeod(periph);
1047 nmarks -= softc->filemarks;
1048 error = saspace(periph, count - nmarks, spaceop);
1050 * At this point, clear that we've written the tape
1051 * and that we've written any filemarks. We really
1052 * don't know what the applications wishes to do next-
1053 * the sacheckeod's will make sure we terminated the
1054 * tape correctly if we'd been writing, but the next
1055 * action the user application takes will set again
1056 * whether we need to write filemarks.
1059 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1060 softc->filemarks = 0;
1063 case MTREW: /* rewind */
1065 error = sarewind(periph);
1068 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1069 softc->flags &= ~SA_FLAG_ERR_PENDING;
1070 softc->filemarks = 0;
1072 case MTERASE: /* erase */
1073 error = saerase(periph, count);
1075 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1076 softc->flags &= ~SA_FLAG_ERR_PENDING;
1078 case MTRETENS: /* re-tension tape */
1079 error = saretension(periph);
1081 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1082 softc->flags &= ~SA_FLAG_ERR_PENDING;
1084 case MTOFFL: /* rewind and put the drive offline */
1088 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1089 softc->filemarks = 0;
1091 error = sarewind(periph);
1092 /* clear the frozen flag anyway */
1093 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1096 * Be sure to allow media removal before ejecting.
1099 saprevent(periph, PR_ALLOW);
1101 error = saloadunload(periph, FALSE);
1103 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1108 case MTNOP: /* no operation, sets status only */
1109 case MTCACHE: /* enable controller cache */
1110 case MTNOCACHE: /* disable controller cache */
1114 case MTSETBSIZ: /* Set block size for device */
1116 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1119 softc->last_media_blksize =
1120 softc->media_blksize;
1121 softc->media_blksize = count;
1123 softc->flags |= SA_FLAG_FIXED;
1124 if (powerof2(count)) {
1127 softc->blk_mask = count - 1;
1129 softc->blk_mask = ~0;
1130 softc->blk_shift = 0;
1133 * Make the user's desire 'persistent'.
1135 softc->quirks &= ~SA_QUIRK_VARIABLE;
1136 softc->quirks |= SA_QUIRK_FIXED;
1138 softc->flags &= ~SA_FLAG_FIXED;
1139 if (softc->max_blk == 0) {
1140 softc->max_blk = ~0;
1142 softc->blk_shift = 0;
1143 if (softc->blk_gran != 0) {
1145 softc->blk_gran - 1;
1147 softc->blk_mask = 0;
1150 * Make the user's desire 'persistent'.
1152 softc->quirks |= SA_QUIRK_VARIABLE;
1153 softc->quirks &= ~SA_QUIRK_FIXED;
1157 case MTSETDNSTY: /* Set density for device and mode */
1158 if (count > UCHAR_MAX) {
1162 error = sasetparams(periph, SA_PARAM_DENSITY,
1166 case MTCOMP: /* enable compression */
1168 * Some devices don't support compression, and
1169 * don't like it if you ask them for the
1172 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1173 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1177 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1178 0, 0, count, SF_NO_PRINT);
1190 error = sardpos(periph, 0, (u_int32_t *) addr);
1193 error = sardpos(periph, 1, (u_int32_t *) addr);
1196 error = sasetpos(periph, 0, (u_int32_t *) addr);
1199 error = sasetpos(periph, 1, (u_int32_t *) addr);
1201 case MTIOCGETEOTMODEL:
1203 if (softc->quirks & SA_QUIRK_1FM)
1207 *((u_int32_t *) addr) = mode;
1209 case MTIOCSETEOTMODEL:
1211 switch (*((u_int32_t *) addr)) {
1213 softc->quirks &= ~SA_QUIRK_2FM;
1214 softc->quirks |= SA_QUIRK_1FM;
1217 softc->quirks &= ~SA_QUIRK_1FM;
1218 softc->quirks |= SA_QUIRK_2FM;
1226 error = cam_periph_ioctl(periph, ap->a_cmd, addr, saerror);
1231 * Check to see if we cleared a frozen state
1233 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1239 softc->fileno = (daddr_t) -1;
1240 softc->blkno = (daddr_t) -1;
1241 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1242 xpt_print_path(periph->path);
1243 kprintf("tape state now unfrozen.\n");
1249 if (didlockperiph) {
1250 cam_periph_unlock(periph);
1259 struct cam_path *path;
1262 * Create our extend array for storing the devices we attach to.
1264 saperiphs = cam_extend_new();
1265 if (saperiphs == NULL) {
1266 kprintf("sa: Failed to alloc extend array!\n");
1271 * Install a global async callback.
1273 status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1274 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1276 if (status == CAM_REQ_CMP) {
1277 /* Register the async callbacks of interrest */
1278 struct ccb_setasync csa; /*
1279 * This is an immediate CCB,
1280 * so using the stack is OK
1282 xpt_setup_ccb(&csa.ccb_h, path, 5);
1283 csa.ccb_h.func_code = XPT_SASYNC_CB;
1284 csa.event_enable = AC_FOUND_DEVICE;
1285 csa.callback = saasync;
1286 csa.callback_arg = NULL;
1287 xpt_action((union ccb *)&csa);
1288 status = csa.ccb_h.status;
1289 xpt_free_path(path);
1292 if (status != CAM_REQ_CMP) {
1293 kprintf("sa: Failed to attach master async callback "
1294 "due to status 0x%x!\n", status);
1299 saoninvalidate(struct cam_periph *periph)
1301 struct sa_softc *softc;
1304 struct ccb_setasync csa;
1306 softc = (struct sa_softc *)periph->softc;
1309 * De-register any async callbacks.
1311 xpt_setup_ccb(&csa.ccb_h, periph->path,
1313 csa.ccb_h.func_code = XPT_SASYNC_CB;
1314 csa.event_enable = 0;
1315 csa.callback = saasync;
1316 csa.callback_arg = periph;
1317 xpt_action((union ccb *)&csa);
1319 softc->flags |= SA_FLAG_INVALID;
1322 * We need to be in a critical section here to keep the buffer
1323 * queue from being modified while we traverse it.
1328 * Return all queued I/O with ENXIO.
1329 * XXX Handle any transactions queued to the card
1330 * with XPT_ABORT_CCB.
1332 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
1333 bioq_remove(&softc->bio_queue, q_bio);
1334 q_bp = q_bio->bio_buf;
1335 q_bp->b_resid = q_bp->b_bcount;
1336 q_bp->b_error = ENXIO;
1337 q_bp->b_flags |= B_ERROR;
1340 softc->queue_count = 0;
1343 xpt_print_path(periph->path);
1344 kprintf("lost device\n");
1349 sacleanup(struct cam_periph *periph)
1351 struct sa_softc *softc;
1353 softc = (struct sa_softc *)periph->softc;
1355 devstat_remove_entry(&softc->device_stats);
1357 cam_extend_release(saperiphs, periph->unit_number);
1358 xpt_print_path(periph->path);
1359 kprintf("removing device entry\n");
1360 dev_ops_remove(&sa_ops, SA_UNITMASK, SA_UNIT(periph->unit_number));
1361 kfree(softc, M_DEVBUF);
1365 saasync(void *callback_arg, u_int32_t code,
1366 struct cam_path *path, void *arg)
1368 struct cam_periph *periph;
1370 periph = (struct cam_periph *)callback_arg;
1372 case AC_FOUND_DEVICE:
1374 struct ccb_getdev *cgd;
1377 cgd = (struct ccb_getdev *)arg;
1381 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1385 * Allocate a peripheral instance for
1386 * this device and start the probe
1389 status = cam_periph_alloc(saregister, saoninvalidate,
1391 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1392 saasync, AC_FOUND_DEVICE, cgd);
1394 if (status != CAM_REQ_CMP
1395 && status != CAM_REQ_INPROG)
1396 kprintf("saasync: Unable to probe new device "
1397 "due to status 0x%x\n", status);
1401 cam_periph_async(periph, code, path, arg);
1407 saregister(struct cam_periph *periph, void *arg)
1409 struct sa_softc *softc;
1410 struct ccb_setasync csa;
1411 struct ccb_getdev *cgd;
1415 cgd = (struct ccb_getdev *)arg;
1416 if (periph == NULL) {
1417 kprintf("saregister: periph was NULL!!\n");
1418 return (CAM_REQ_CMP_ERR);
1422 kprintf("saregister: no getdev CCB, can't register device\n");
1423 return (CAM_REQ_CMP_ERR);
1426 softc = kmalloc(sizeof (*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1427 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1428 softc->state = SA_STATE_NORMAL;
1429 softc->fileno = (daddr_t) -1;
1430 softc->blkno = (daddr_t) -1;
1432 bioq_init(&softc->bio_queue);
1433 periph->softc = softc;
1434 cam_extend_set(saperiphs, periph->unit_number, periph);
1437 * See if this device has any quirks.
1439 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1440 (caddr_t)sa_quirk_table,
1441 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1442 sizeof(*sa_quirk_table), scsi_inquiry_match);
1444 if (match != NULL) {
1445 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1446 softc->last_media_blksize =
1447 ((struct sa_quirk_entry *)match)->prefblk;
1449 xpt_print_path(periph->path);
1450 kprintf("found quirk entry %d\n", (int)
1451 (((struct sa_quirk_entry *) match) - sa_quirk_table));
1454 softc->quirks = SA_QUIRK_NONE;
1457 * The SA driver supports a blocksize, but we don't know the
1458 * blocksize until we media is inserted. So, set a flag to
1459 * indicate that the blocksize is unavailable right now.
1461 devstat_add_entry(&softc->device_stats, "sa", periph->unit_number, 0,
1462 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1463 DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE);
1465 dev_ops_add(&sa_ops, SA_UNITMASK, SA_UNIT(periph->unit_number));
1466 make_dev(&sa_ops, SAMINOR(SA_CTLDEV,
1467 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1468 0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1470 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1471 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1472 0660, "%s%d", periph->periph_name, periph->unit_number);
1474 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1475 periph->unit_number, 0, SA_ATYPE_NR), UID_ROOT, GID_OPERATOR,
1476 0660, "n%s%d", periph->periph_name, periph->unit_number);
1478 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1479 periph->unit_number, 0, SA_ATYPE_ER), UID_ROOT, GID_OPERATOR,
1480 0660, "e%s%d", periph->periph_name, periph->unit_number);
1482 for (i = 0; i < SA_NUM_MODES; i++) {
1485 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R),
1486 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1487 periph->periph_name, periph->unit_number, i);
1490 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR),
1491 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1492 periph->periph_name, periph->unit_number, i);
1496 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER),
1497 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1498 periph->periph_name, periph->unit_number, i);
1502 * Add an async callback so that we get
1503 * notified if this device goes away.
1505 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
1506 csa.ccb_h.func_code = XPT_SASYNC_CB;
1507 csa.event_enable = AC_LOST_DEVICE;
1508 csa.callback = saasync;
1509 csa.callback_arg = periph;
1510 xpt_action((union ccb *)&csa);
1512 xpt_announce_periph(periph, NULL);
1514 return (CAM_REQ_CMP);
1518 sastart(struct cam_periph *periph, union ccb *start_ccb)
1520 struct sa_softc *softc;
1522 softc = (struct sa_softc *)periph->softc;
1524 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("sastart"));
1527 switch (softc->state) {
1528 case SA_STATE_NORMAL:
1530 /* Pull a buffer from the queue and get going on it */
1535 * See if there is a buf with work for us to do..
1538 bio = bioq_first(&softc->bio_queue);
1539 if (periph->immediate_priority <= periph->pinfo.priority) {
1540 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1541 ("queuing for immediate ccb\n"));
1542 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1543 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1545 periph->immediate_priority = CAM_PRIORITY_NONE;
1547 wakeup(&periph->ccb_list);
1548 } else if (bio == NULL) {
1550 xpt_release_ccb(start_ccb);
1551 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1552 struct bio *done_bio;
1554 softc->queue_count--;
1555 bioq_remove(&softc->bio_queue, bio);
1557 bp->b_resid = bp->b_bcount;
1559 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1561 * We now just clear errors in this case
1562 * and let the residual be the notifier.
1565 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1567 * This can only happen if we're reading
1568 * in fixed length mode. In this case,
1569 * we dump the rest of the list the
1573 if (bioq_first(&softc->bio_queue) != NULL) {
1577 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1579 bp->b_flags |= B_ERROR;
1581 bio = bioq_first(&softc->bio_queue);
1583 * Only if we have no other buffers queued up
1584 * do we clear the pending error flag.
1587 softc->flags &= ~SA_FLAG_ERR_PENDING;
1588 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1589 ("sastart- ERR_PENDING now 0x%x, bio is %sNULL, "
1590 "%d more buffers queued up\n",
1591 (softc->flags & SA_FLAG_ERR_PENDING),
1592 (bio != NULL)? "not " : " ", softc->queue_count));
1594 xpt_release_ccb(start_ccb);
1599 bioq_remove(&softc->bio_queue, bio);
1601 softc->queue_count--;
1603 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1604 if (softc->blk_shift != 0) {
1606 bp->b_bcount >> softc->blk_shift;
1607 } else if (softc->media_blksize != 0) {
1609 bp->b_bcount / softc->media_blksize;
1612 xpt_print_path(periph->path);
1613 kprintf("zero blocksize for "
1614 "FIXED length writes?\n");
1619 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1620 ("Fixed Record Count is %d\n", length));
1622 length = bp->b_bcount;
1623 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1624 ("Variable Record Count is %d\n", length));
1626 devstat_start_transaction(&softc->device_stats);
1628 * Some people have theorized that we should
1629 * suppress illegal length indication if we are
1630 * running in variable block mode so that we don't
1631 * have to request sense every time our requested
1632 * block size is larger than the written block.
1633 * The residual information from the ccb allows
1634 * us to identify this situation anyway. The only
1635 * problem with this is that we will not get
1636 * information about blocks that are larger than
1637 * our read buffer unless we set the block size
1638 * in the mode page to something other than 0.
1640 * I believe that this is a non-issue. If user apps
1641 * don't adjust their read size to match our record
1642 * size, that's just life. Anyway, the typical usage
1643 * would be to issue, e.g., 64KB reads and occasionally
1644 * have to do deal with 512 byte or 1KB intermediate
1647 softc->dsreg = (bp->b_cmd == BUF_CMD_READ) ?
1648 MTIO_DSREG_RD : MTIO_DSREG_WR;
1649 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1650 MSG_SIMPLE_Q_TAG, (bp->b_cmd == BUF_CMD_READ) != 0,
1651 FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1652 length, bp->b_data, bp->b_bcount, SSD_FULL_SIZE,
1654 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1655 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1656 start_ccb->ccb_h.ccb_bio = bio;
1657 bio = bioq_first(&softc->bio_queue);
1659 xpt_action(start_ccb);
1663 /* Have more work to do, so ensure we stay scheduled */
1664 xpt_schedule(periph, 1);
1668 case SA_STATE_ABNORMAL:
1670 panic("state 0x%x in sastart", softc->state);
1677 sadone(struct cam_periph *periph, union ccb *done_ccb)
1679 struct sa_softc *softc;
1680 struct ccb_scsiio *csio;
1682 softc = (struct sa_softc *)periph->softc;
1683 csio = &done_ccb->csio;
1684 switch (CCB_Type(csio)) {
1685 case SA_CCB_BUFFER_IO:
1691 softc->dsreg = MTIO_DSREG_REST;
1692 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1695 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1696 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1698 * A retry was scheduled, so just return.
1709 * Catastrophic error. Mark the tape as frozen
1710 * (we no longer know tape position).
1712 * Return all queued I/O with EIO, and unfreeze
1713 * our queue so that future transactions that
1714 * attempt to fix this problem can get to the
1720 softc->flags |= SA_FLAG_TAPE_FROZEN;
1721 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) {
1722 bioq_remove(&softc->bio_queue, q_bio);
1723 q_bp = q_bio->bio_buf;
1724 q_bp->b_resid = q_bp->b_bcount;
1725 q_bp->b_error = EIO;
1726 q_bp->b_flags |= B_ERROR;
1732 bp->b_resid = bp->b_bcount;
1733 bp->b_error = error;
1734 bp->b_flags |= B_ERROR;
1736 * In the error case, position is updated in saerror.
1739 bp->b_resid = csio->resid;
1741 if (csio->resid != 0) {
1742 bp->b_flags |= B_ERROR;
1744 if (bp->b_cmd != BUF_CMD_READ) {
1745 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1746 softc->filemarks = 0;
1748 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1749 (softc->blkno != (daddr_t) -1)) {
1750 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1752 if (softc->blk_shift != 0) {
1757 softc->media_blksize;
1759 softc->blkno += (daddr_t) l;
1766 * If we had an error (immediate or pending),
1767 * release the device queue now.
1769 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1770 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1772 if (error || bp->b_resid) {
1773 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1774 ("error %d resid %d count %d\n", error,
1775 bp->b_resid, bp->b_bcount));
1778 devstat_end_transaction_buf(&softc->device_stats, bp);
1782 case SA_CCB_WAITING:
1784 /* Caller will release the CCB */
1785 wakeup(&done_ccb->ccb_h.cbfcnp);
1789 xpt_release_ccb(done_ccb);
1793 * Mount the tape (make sure it's ready for I/O).
1796 samount(struct cam_periph *periph, int oflags, cdev_t dev)
1798 struct sa_softc *softc;
1803 * oflags can be checked for 'kind' of open (read-only check) - later
1804 * dev can be checked for a control-mode or compression open - later
1806 UNUSED_PARAMETER(oflags);
1807 UNUSED_PARAMETER(dev);
1810 softc = (struct sa_softc *)periph->softc;
1813 * This should determine if something has happend since the last
1814 * open/mount that would invalidate the mount. We do *not* want
1815 * to retry this command- we just want the status. But we only
1816 * do this if we're mounted already- if we're not mounted,
1817 * we don't care about the unit read state and can instead use
1818 * this opportunity to attempt to reserve the tape unit.
1821 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1822 ccb = cam_periph_getccb(periph, 1);
1823 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1824 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1825 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1826 &softc->device_stats);
1828 if (error == ENXIO) {
1829 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1830 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1831 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1832 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1833 &softc->device_stats);
1837 * We don't need to freeze the tape because we
1838 * will now attempt to rewind/load it.
1840 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1841 if (CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO)) {
1842 xpt_print_path(ccb->ccb_h.path);
1843 kprintf("error %d on TUR in samount\n", error);
1847 error = sareservereleaseunit(periph, TRUE);
1851 ccb = cam_periph_getccb(periph, 1);
1852 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1853 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1854 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1855 &softc->device_stats);
1859 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1860 struct scsi_read_block_limits_data *rblim = NULL;
1861 int comp_enabled, comp_supported;
1862 u_int8_t write_protect, guessing = 0;
1865 * Clear out old state.
1867 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1868 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1869 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1870 softc->filemarks = 0;
1873 * *Very* first off, make sure we're loaded to BOT.
1875 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1876 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1877 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1878 &softc->device_stats);
1882 * In case this doesn't work, do a REWIND instead
1885 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1886 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1887 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1888 &softc->device_stats);
1892 xpt_release_ccb(ccb);
1897 * Do a dummy test read to force access to the
1898 * media so that the drive will really know what's
1899 * there. We actually don't really care what the
1900 * blocksize on tape is and don't expect to really
1901 * read a full record.
1903 rblim = kmalloc(8192, M_TEMP, M_INTWAIT);
1905 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1906 scsi_sa_read_write(&ccb->csio, 0, sadone,
1907 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1908 (void *) rblim, 8192, SSD_FULL_SIZE,
1910 cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1911 &softc->device_stats);
1913 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1914 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1915 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1916 SF_NO_PRINT | SF_RETRY_UA,
1917 &softc->device_stats);
1920 xpt_print_path(ccb->ccb_h.path);
1921 kprintf("unable to rewind after test read\n");
1922 xpt_release_ccb(ccb);
1928 * Next off, determine block limits.
1930 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1931 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1933 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1934 SF_NO_PRINT | SF_RETRY_UA, &softc->device_stats);
1937 xpt_release_ccb(ccb);
1941 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1942 * a MANDATORY command. Anyway- it doesn't matter-
1943 * we can proceed anyway.
1945 softc->blk_gran = 0;
1946 softc->max_blk = ~0;
1949 if (softc->scsi_rev >= SCSI_REV_SPC) {
1950 softc->blk_gran = RBL_GRAN(rblim);
1952 softc->blk_gran = 0;
1955 * We take max_blk == min_blk to mean a default to
1956 * fixed mode- but note that whatever we get out of
1957 * sagetparams below will actually determine whether
1958 * we are actually *in* fixed mode.
1960 softc->max_blk = scsi_3btoul(rblim->maximum);
1961 softc->min_blk = scsi_2btoul(rblim->minimum);
1966 * Next, perform a mode sense to determine
1967 * current density, blocksize, compression etc.
1969 error = sagetparams(periph, SA_PARAM_ALL,
1970 &softc->media_blksize,
1971 &softc->media_density,
1972 &softc->media_numblks,
1973 &softc->buffer_mode, &write_protect,
1974 &softc->speed, &comp_supported,
1975 &comp_enabled, &softc->comp_algorithm,
1980 * We could work a little harder here. We could
1981 * adjust our attempts to get information. It
1982 * might be an ancient tape drive. If someone
1983 * nudges us, we'll do that.
1989 * If no quirk has determined that this is a device that is
1990 * preferred to be in fixed or variable mode, now is the time
1993 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
1996 * This could be expensive to find out. Luckily we
1997 * only need to do this once. If we start out in
1998 * 'default' mode, try and set ourselves to one
1999 * of the densities that would determine a wad
2000 * of other stuff. Go from highest to lowest.
2002 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2004 static u_int8_t ctry[] = {
2005 SCSI_DENSITY_HALFINCH_PE,
2006 SCSI_DENSITY_HALFINCH_6250C,
2007 SCSI_DENSITY_HALFINCH_6250,
2008 SCSI_DENSITY_HALFINCH_1600,
2009 SCSI_DENSITY_HALFINCH_800,
2010 SCSI_DENSITY_QIC_4GB,
2011 SCSI_DENSITY_QIC_2GB,
2012 SCSI_DENSITY_QIC_525_320,
2013 SCSI_DENSITY_QIC_150,
2014 SCSI_DENSITY_QIC_120,
2015 SCSI_DENSITY_QIC_24,
2016 SCSI_DENSITY_QIC_11_9TRK,
2017 SCSI_DENSITY_QIC_11_4TRK,
2018 SCSI_DENSITY_QIC_1320,
2019 SCSI_DENSITY_QIC_3080,
2022 for (i = 0; ctry[i]; i++) {
2023 error = sasetparams(periph,
2024 SA_PARAM_DENSITY, 0, ctry[i],
2027 softc->media_density = ctry[i];
2032 switch (softc->media_density) {
2033 case SCSI_DENSITY_QIC_11_4TRK:
2034 case SCSI_DENSITY_QIC_11_9TRK:
2035 case SCSI_DENSITY_QIC_24:
2036 case SCSI_DENSITY_QIC_120:
2037 case SCSI_DENSITY_QIC_150:
2038 case SCSI_DENSITY_QIC_525_320:
2039 case SCSI_DENSITY_QIC_1320:
2040 case SCSI_DENSITY_QIC_3080:
2041 softc->quirks &= ~SA_QUIRK_2FM;
2042 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2043 softc->last_media_blksize = 512;
2045 case SCSI_DENSITY_QIC_4GB:
2046 case SCSI_DENSITY_QIC_2GB:
2047 softc->quirks &= ~SA_QUIRK_2FM;
2048 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2049 softc->last_media_blksize = 1024;
2052 softc->last_media_blksize =
2053 softc->media_blksize;
2054 softc->quirks |= SA_QUIRK_VARIABLE;
2060 * If no quirk has determined that this is a device that needs
2061 * to have 2 Filemarks at EOD, now is the time to find out.
2064 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2065 switch (softc->media_density) {
2066 case SCSI_DENSITY_HALFINCH_800:
2067 case SCSI_DENSITY_HALFINCH_1600:
2068 case SCSI_DENSITY_HALFINCH_6250:
2069 case SCSI_DENSITY_HALFINCH_6250C:
2070 case SCSI_DENSITY_HALFINCH_PE:
2071 softc->quirks &= ~SA_QUIRK_1FM;
2072 softc->quirks |= SA_QUIRK_2FM;
2080 * Now validate that some info we got makes sense.
2082 if ((softc->max_blk < softc->media_blksize) ||
2083 (softc->min_blk > softc->media_blksize &&
2084 softc->media_blksize)) {
2085 xpt_print_path(ccb->ccb_h.path);
2086 kprintf("BLOCK LIMITS (%d..%d) could not match current "
2087 "block settings (%d)- adjusting\n", softc->min_blk,
2088 softc->max_blk, softc->media_blksize);
2089 softc->max_blk = softc->min_blk =
2090 softc->media_blksize;
2094 * Now put ourselves into the right frame of mind based
2099 * If we want to be in FIXED mode and our current blocksize
2100 * is not equal to our last blocksize (if nonzero), try and
2101 * set ourselves to this last blocksize (as the 'preferred'
2102 * block size). The initial quirkmatch at registry sets the
2103 * initial 'last' blocksize. If, for whatever reason, this
2104 * 'last' blocksize is zero, set the blocksize to 512,
2105 * or min_blk if that's larger.
2107 if ((softc->quirks & SA_QUIRK_FIXED) &&
2108 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2109 (softc->media_blksize != softc->last_media_blksize)) {
2110 softc->media_blksize = softc->last_media_blksize;
2111 if (softc->media_blksize == 0) {
2112 softc->media_blksize = 512;
2113 if (softc->media_blksize < softc->min_blk) {
2114 softc->media_blksize = softc->min_blk;
2117 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2118 softc->media_blksize, 0, 0, SF_NO_PRINT);
2120 xpt_print_path(ccb->ccb_h.path);
2121 kprintf("unable to set fixed blocksize to %d\n",
2122 softc->media_blksize);
2127 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2128 (softc->media_blksize != 0)) {
2129 softc->last_media_blksize = softc->media_blksize;
2130 softc->media_blksize = 0;
2131 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2132 0, 0, 0, SF_NO_PRINT);
2135 * If this fails and we were guessing, just
2136 * assume that we got it wrong and go try
2137 * fixed block mode. Don't even check against
2138 * density code at this point.
2141 softc->quirks &= ~SA_QUIRK_VARIABLE;
2142 softc->quirks |= SA_QUIRK_FIXED;
2143 if (softc->last_media_blksize == 0)
2144 softc->last_media_blksize = 512;
2147 xpt_print_path(ccb->ccb_h.path);
2148 kprintf("unable to set variable blocksize\n");
2154 * Now that we have the current block size,
2155 * set up some parameters for sastart's usage.
2157 if (softc->media_blksize) {
2158 softc->flags |= SA_FLAG_FIXED;
2159 if (powerof2(softc->media_blksize)) {
2161 ffs(softc->media_blksize) - 1;
2162 softc->blk_mask = softc->media_blksize - 1;
2164 softc->blk_mask = ~0;
2165 softc->blk_shift = 0;
2169 * The SCSI-3 spec allows 0 to mean "unspecified".
2170 * The SCSI-1 spec allows 0 to mean 'infinite'.
2172 * Either works here.
2174 if (softc->max_blk == 0) {
2175 softc->max_blk = ~0;
2177 softc->blk_shift = 0;
2178 if (softc->blk_gran != 0) {
2179 softc->blk_mask = softc->blk_gran - 1;
2181 softc->blk_mask = 0;
2186 softc->flags |= SA_FLAG_TAPE_WP;
2188 if (comp_supported) {
2189 if (softc->saved_comp_algorithm == 0)
2190 softc->saved_comp_algorithm =
2191 softc->comp_algorithm;
2192 softc->flags |= SA_FLAG_COMP_SUPP;
2194 softc->flags |= SA_FLAG_COMP_ENABLED;
2196 softc->flags |= SA_FLAG_COMP_UNSUPP;
2198 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2199 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2200 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2203 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2205 xpt_print_path(ccb->ccb_h.path);
2206 kprintf("unable to set buffered mode\n");
2208 error = 0; /* not an error */
2213 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2217 kfree(rblim, M_TEMP);
2220 softc->dsreg = MTIO_DSREG_NIL;
2222 softc->fileno = softc->blkno = 0;
2223 softc->dsreg = MTIO_DSREG_REST;
2225 #ifdef SA_1FM_AT_EOD
2226 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2227 softc->quirks |= SA_QUIRK_1FM;
2229 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2230 softc->quirks |= SA_QUIRK_2FM;
2233 xpt_release_ccb(ccb);
2236 * If we return an error, we're not mounted any more,
2237 * so release any device reservation.
2240 sareservereleaseunit(periph, FALSE);
2243 * Clear I/O residual.
2245 softc->last_io_resid = 0;
2246 softc->last_ctl_resid = 0;
2252 * How many filemarks do we need to write if we were to terminate the
2253 * tape session right now? Note that this can be a negative number
2257 samarkswanted(struct cam_periph *periph)
2260 struct sa_softc *softc;
2262 softc = (struct sa_softc *)periph->softc;
2264 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2266 if (softc->quirks & SA_QUIRK_2FM)
2269 markswanted -= softc->filemarks;
2270 return (markswanted);
2274 sacheckeod(struct cam_periph *periph)
2279 markswanted = samarkswanted(periph);
2281 if (markswanted > 0) {
2282 error = sawritefilemarks(periph, markswanted, FALSE);
2290 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2292 static const char *toobig =
2293 "%d-byte tape record bigger than supplied buffer\n";
2294 struct cam_periph *periph;
2295 struct sa_softc *softc;
2296 struct ccb_scsiio *csio;
2297 struct scsi_sense_data *sense;
2298 u_int32_t resid = 0;
2301 int error_code, sense_key, asc, ascq, error, aqvalid;
2303 periph = xpt_path_periph(ccb->ccb_h.path);
2304 softc = (struct sa_softc *)periph->softc;
2306 sense = &csio->sense_data;
2307 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2308 aqvalid = sense->extra_len >= 6;
2311 status = csio->ccb_h.status & CAM_STATUS_MASK;
2314 * Calculate/latch up, any residuals... We do this in a funny 2-step
2315 * so we can print stuff here if we have CAM_DEBUG enabled for this
2318 if (status == CAM_SCSI_STATUS_ERROR) {
2319 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2320 info = (int32_t) scsi_4btoul(sense->info);
2322 if ((softc->flags & SA_FLAG_FIXED) != 0)
2323 resid *= softc->media_blksize;
2325 resid = csio->dxfer_len;
2327 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2328 if (softc->media_blksize)
2329 info /= softc->media_blksize;
2332 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2333 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2334 sizeof (struct scsi_sense_data));
2335 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2336 (int) csio->cdb_len);
2337 softc->last_io_resid = resid;
2338 softc->last_resid_was_io = 1;
2340 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2341 sizeof (struct scsi_sense_data));
2342 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2343 (int) csio->cdb_len);
2344 softc->last_ctl_resid = resid;
2345 softc->last_resid_was_io = 0;
2347 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2348 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %d "
2349 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2350 sense_key, asc, ascq, status,
2351 sense->flags & ~SSD_KEY_RESERVED, resid, csio->dxfer_len));
2353 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2354 ("Cam Status 0x%x\n", status));
2360 case CAM_SCSI_STATUS_ERROR:
2362 * If a read/write command, we handle it here.
2364 if (CCB_Type(csio) != SA_CCB_WAITING) {
2368 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2369 * on a non read/write command, we assume it's not an error
2370 * and propagate the residule and return.
2372 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2373 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2374 csio->resid = resid;
2379 * Otherwise, we let the common code handle this.
2381 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2385 * We cannot depend upon CAM honoring retry counts for these.
2387 case CAM_SCSI_BUS_RESET:
2389 if (ccb->ccb_h.retry_count <= 0) {
2394 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2398 * Handle filemark, end of tape, mismatched record sizes....
2399 * From this point out, we're only handling read/write cases.
2400 * Handle writes && reads differently.
2403 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2404 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2405 csio->resid = resid;
2407 } else if (sense->flags & SSD_EOM) {
2408 softc->flags |= SA_FLAG_EOM_PENDING;
2410 * Grotesque as it seems, the few times
2411 * I've actually seen a non-zero resid,
2412 * the tape drive actually lied and had
2413 * writtent all the data!.
2418 csio->resid = resid;
2419 if (sense_key == SSD_KEY_BLANK_CHECK) {
2420 if (softc->quirks & SA_QUIRK_1FM) {
2422 softc->flags |= SA_FLAG_EOM_PENDING;
2426 } else if (sense->flags & SSD_FILEMARK) {
2427 if (softc->flags & SA_FLAG_FIXED) {
2429 softc->flags |= SA_FLAG_EOF_PENDING;
2432 * Unconditionally, if we detected a filemark on a read,
2433 * mark that we've run moved a file ahead.
2435 if (softc->fileno != (daddr_t) -1) {
2438 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2444 * Incorrect Length usually applies to read, but can apply to writes.
2446 if (error == 0 && (sense->flags & SSD_ILI)) {
2448 xpt_print_path(csio->ccb_h.path);
2449 kprintf(toobig, csio->dxfer_len - info);
2450 csio->resid = csio->dxfer_len;
2453 csio->resid = resid;
2454 if (softc->flags & SA_FLAG_FIXED) {
2455 softc->flags |= SA_FLAG_EIO_PENDING;
2458 * Bump the block number if we hadn't seen a filemark.
2459 * Do this independent of errors (we've moved anyway).
2461 if ((sense->flags & SSD_FILEMARK) == 0) {
2462 if (softc->blkno != (daddr_t) -1) {
2464 csio->ccb_h.ccb_pflags |=
2465 SA_POSITION_UPDATED;
2473 * Unfreeze the queue if frozen as we're not returning anything
2474 * to our waiters that would indicate an I/O error has occurred
2484 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2485 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2486 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2487 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2492 struct scsi_mode_header_6 *mode_hdr;
2493 struct scsi_mode_blk_desc *mode_blk;
2494 int mode_buffer_len;
2495 struct sa_softc *softc;
2500 softc = (struct sa_softc *)periph->softc;
2501 ccb = cam_periph_getccb(periph, 1);
2502 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2503 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2505 cpage = SA_DATA_COMPRESSION_PAGE;
2508 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2510 if (params_to_get & SA_PARAM_COMPRESSION) {
2511 if (softc->quirks & SA_QUIRK_NOCOMP) {
2512 *comp_supported = FALSE;
2513 params_to_get &= ~SA_PARAM_COMPRESSION;
2515 mode_buffer_len += sizeof (sa_comp_t);
2518 mode_buffer = kmalloc(mode_buffer_len, M_TEMP, M_INTWAIT | M_ZERO);
2519 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2520 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2522 /* it is safe to retry this */
2523 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2524 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2525 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2526 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2528 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2529 &softc->device_stats);
2532 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2534 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2536 * Hmm. Let's see if we can try another page...
2537 * If we've already done that, give up on compression
2538 * for this device and remember this for the future
2539 * and attempt the request without asking for compression
2542 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2543 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2546 softc->quirks |= SA_QUIRK_NOCOMP;
2547 kfree(mode_buffer, M_TEMP);
2549 } else if (status == CAM_SCSI_STATUS_ERROR) {
2550 /* Tell the user about the fatal error. */
2551 scsi_sense_print(&ccb->csio);
2552 goto sagetparamsexit;
2556 * If the user only wants the compression information, and
2557 * the device doesn't send back the block descriptor, it's
2558 * no big deal. If the user wants more than just
2559 * compression, though, and the device doesn't pass back the
2560 * block descriptor, we need to send another mode sense to
2561 * get the block descriptor.
2563 if ((mode_hdr->blk_desc_len == 0) &&
2564 (params_to_get & SA_PARAM_COMPRESSION) &&
2565 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2568 * Decrease the mode buffer length by the size of
2569 * the compression page, to make sure the data
2570 * there doesn't get overwritten.
2572 mode_buffer_len -= sizeof (sa_comp_t);
2575 * Now move the compression page that we presumably
2576 * got back down the memory chunk a little bit so
2577 * it doesn't get spammed.
2579 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2580 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2583 * Now, we issue another mode sense and just ask
2584 * for the block descriptor, etc.
2587 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2588 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2589 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2592 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2593 &softc->device_stats);
2597 goto sagetparamsexit;
2600 if (params_to_get & SA_PARAM_BLOCKSIZE)
2601 *blocksize = scsi_3btoul(mode_blk->blklen);
2603 if (params_to_get & SA_PARAM_NUMBLOCKS)
2604 *numblocks = scsi_3btoul(mode_blk->nblocks);
2606 if (params_to_get & SA_PARAM_BUFF_MODE)
2607 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2609 if (params_to_get & SA_PARAM_DENSITY)
2610 *density = mode_blk->density;
2612 if (params_to_get & SA_PARAM_WP)
2613 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2615 if (params_to_get & SA_PARAM_SPEED)
2616 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2618 if (params_to_get & SA_PARAM_COMPRESSION) {
2619 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2620 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2621 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2623 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2625 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2626 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2628 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2630 * We don't really know whether this device supports
2631 * Data Compression if the the algorithm field is
2632 * zero. Just say we do.
2634 *comp_supported = TRUE;
2636 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2637 *comp_algorithm = cp->sel_comp_alg;
2640 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2643 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2645 char *xyz = mode_buffer;
2646 xpt_print_path(periph->path);
2647 kprintf("Mode Sense Data=");
2648 for (idx = 0; idx < mode_buffer_len; idx++)
2649 kprintf(" 0x%02x", xyz[idx] & 0xff);
2655 xpt_release_ccb(ccb);
2656 kfree(mode_buffer, M_TEMP);
2661 * The purpose of this function is to set one of four different parameters
2665 * - compression / compression algorithm
2668 * The assumption is that this will be called from saioctl(), and therefore
2669 * from a process context. Thus the waiting malloc calls below. If that
2670 * assumption ever changes, the malloc calls should be changed to be
2673 * Any or all of the four parameters may be set when this function is
2674 * called. It should handle setting more than one parameter at once.
2677 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2678 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2679 u_int32_t sense_flags)
2681 struct sa_softc *softc;
2682 u_int32_t current_blocksize;
2683 u_int32_t current_calg;
2684 u_int8_t current_density;
2685 u_int8_t current_speed;
2686 int comp_enabled, comp_supported;
2688 int mode_buffer_len;
2689 struct scsi_mode_header_6 *mode_hdr;
2690 struct scsi_mode_blk_desc *mode_blk;
2691 sa_comp_t *ccomp, *cpage;
2693 union ccb *ccb = NULL;
2696 softc = (struct sa_softc *)periph->softc;
2698 ccomp = kmalloc(sizeof (sa_comp_t), M_TEMP, M_INTWAIT);
2701 * Since it doesn't make sense to set the number of blocks, or
2702 * write protection, we won't try to get the current value. We
2703 * always want to get the blocksize, so we can set it back to the
2706 error = sagetparams(periph,
2707 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2708 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2709 ¤t_speed, &comp_supported, &comp_enabled,
2710 ¤t_calg, ccomp);
2713 kfree(ccomp, M_TEMP);
2717 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2718 if (params_to_set & SA_PARAM_COMPRESSION)
2719 mode_buffer_len += sizeof (sa_comp_t);
2721 mode_buffer = kmalloc(mode_buffer_len, M_TEMP, M_INTWAIT | M_ZERO);
2723 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2724 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2726 ccb = cam_periph_getccb(periph, 1);
2730 if (params_to_set & SA_PARAM_COMPRESSION) {
2732 cpage = (sa_comp_t *)&mode_blk[1];
2734 cpage = (sa_comp_t *)&mode_hdr[1];
2736 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2737 cpage->hdr.pagecode &= ~0x80;
2742 * If the caller wants us to set the blocksize, use the one they
2743 * pass in. Otherwise, use the blocksize we got back from the
2744 * mode select above.
2747 if (params_to_set & SA_PARAM_BLOCKSIZE)
2748 scsi_ulto3b(blocksize, mode_blk->blklen);
2750 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2753 * Set density if requested, else preserve old density.
2754 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2755 * devices, else density we've latched up in our softc.
2757 if (params_to_set & SA_PARAM_DENSITY) {
2758 mode_blk->density = density;
2759 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2760 mode_blk->density = SCSI_SAME_DENSITY;
2762 mode_blk->density = softc->media_density;
2767 * For mode selects, these two fields must be zero.
2769 mode_hdr->data_length = 0;
2770 mode_hdr->medium_type = 0;
2772 /* set the speed to the current value */
2773 mode_hdr->dev_spec = current_speed;
2775 /* set single-initiator buffering mode */
2776 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2779 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2781 mode_hdr->blk_desc_len = 0;
2784 * First, if the user wants us to set the compression algorithm or
2785 * just turn compression on, check to make sure that this drive
2786 * supports compression.
2788 if (params_to_set & SA_PARAM_COMPRESSION) {
2790 * If the compression algorithm is 0, disable compression.
2791 * If the compression algorithm is non-zero, enable
2792 * compression and set the compression type to the
2793 * specified compression algorithm, unless the algorithm is
2794 * MT_COMP_ENABLE. In that case, we look at the
2795 * compression algorithm that is currently set and if it is
2796 * non-zero, we leave it as-is. If it is zero, and we have
2797 * saved a compression algorithm from a time when
2798 * compression was enabled before, set the compression to
2801 switch (ccomp->hdr.pagecode & ~0x80) {
2802 case SA_DATA_COMPRESSION_PAGE:
2803 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2804 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2807 * Disable compression, but leave the
2808 * decompression and the capability bit
2811 dcp->dce_and_dcc = SA_DCP_DCC;
2812 dcp->dde_and_red |= SA_DCP_DDE;
2815 /* enable compression && decompression */
2816 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2817 dcp->dde_and_red |= SA_DCP_DDE;
2819 * If there, use compression algorithm from caller.
2820 * Otherwise, if there's a saved compression algorithm
2821 * and there is no current algorithm, use the saved
2822 * algorithm. Else parrot back what we got and hope
2825 if (calg != MT_COMP_ENABLE) {
2826 scsi_ulto4b(calg, dcp->comp_algorithm);
2827 scsi_ulto4b(calg, dcp->decomp_algorithm);
2828 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2829 softc->saved_comp_algorithm != 0) {
2830 scsi_ulto4b(softc->saved_comp_algorithm,
2831 dcp->comp_algorithm);
2832 scsi_ulto4b(softc->saved_comp_algorithm,
2833 dcp->decomp_algorithm);
2837 /* XXX: fall-through intentional ? */
2838 case SA_DEVICE_CONFIGURATION_PAGE:
2840 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2842 dcp->sel_comp_alg = SA_COMP_NONE;
2845 if (calg != MT_COMP_ENABLE) {
2846 dcp->sel_comp_alg = calg;
2847 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2848 softc->saved_comp_algorithm != 0) {
2849 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2855 * The drive doesn't seem to support compression,
2856 * so turn off the set compression bit.
2858 params_to_set &= ~SA_PARAM_COMPRESSION;
2859 xpt_print_path(periph->path);
2860 kprintf("device does not seem to support compression\n");
2863 * If that was the only thing the user wanted us to set,
2864 * clean up allocated resources and return with
2865 * 'operation not supported'.
2867 if (params_to_set == SA_PARAM_NONE) {
2868 kfree(mode_buffer, M_TEMP);
2869 xpt_release_ccb(ccb);
2874 * That wasn't the only thing the user wanted us to set.
2875 * So, decrease the stated mode buffer length by the
2876 * size of the compression mode page.
2878 mode_buffer_len -= sizeof(sa_comp_t);
2882 /* It is safe to retry this operation */
2883 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2884 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2885 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2887 error = cam_periph_runccb(ccb, saerror, 0,
2888 sense_flags, &softc->device_stats);
2891 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2893 char *xyz = mode_buffer;
2894 xpt_print_path(periph->path);
2895 kprintf("Err%d, Mode Select Data=", error);
2896 for (idx = 0; idx < mode_buffer_len; idx++)
2897 kprintf(" 0x%02x", xyz[idx] & 0xff);
2904 * If we can, try without setting density/blocksize.
2907 if ((params_to_set &
2908 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2913 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2914 cpage = (sa_comp_t *)&mode_blk[1];
2918 * If we were setting the blocksize, and that failed, we
2919 * want to set it to its original value. If we weren't
2920 * setting the blocksize, we don't want to change it.
2922 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2925 * Set density if requested, else preserve old density.
2926 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2927 * devices, else density we've latched up in our softc.
2929 if (params_to_set & SA_PARAM_DENSITY) {
2930 mode_blk->density = current_density;
2931 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2932 mode_blk->density = SCSI_SAME_DENSITY;
2934 mode_blk->density = softc->media_density;
2937 if (params_to_set & SA_PARAM_COMPRESSION)
2938 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2941 * The retry count is the only CCB field that might have been
2942 * changed that we care about, so reset it back to 1.
2944 ccb->ccb_h.retry_count = 1;
2945 cam_periph_runccb(ccb, saerror, 0, sense_flags,
2946 &softc->device_stats);
2950 xpt_release_ccb(ccb);
2953 kfree(ccomp, M_TEMP);
2955 if (params_to_set & SA_PARAM_COMPRESSION) {
2957 softc->flags &= ~SA_FLAG_COMP_ENABLED;
2959 * Even if we get an error setting compression,
2960 * do not say that we don't support it. We could
2961 * have been wrong, or it may be media specific.
2962 * softc->flags &= ~SA_FLAG_COMP_SUPP;
2964 softc->saved_comp_algorithm = softc->comp_algorithm;
2965 softc->comp_algorithm = 0;
2967 softc->flags |= SA_FLAG_COMP_ENABLED;
2968 softc->comp_algorithm = calg;
2972 kfree(mode_buffer, M_TEMP);
2977 saprevent(struct cam_periph *periph, int action)
2979 struct sa_softc *softc;
2983 softc = (struct sa_softc *)periph->softc;
2985 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
2987 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
2991 * We can be quiet about illegal requests.
2993 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2998 ccb = cam_periph_getccb(periph, 1);
3000 /* It is safe to retry this operation */
3001 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3002 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3004 error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats);
3007 if (action == PR_ALLOW)
3008 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3010 softc->flags |= SA_FLAG_TAPE_LOCKED;
3013 xpt_release_ccb(ccb);
3017 sarewind(struct cam_periph *periph)
3020 struct sa_softc *softc;
3023 softc = (struct sa_softc *)periph->softc;
3025 ccb = cam_periph_getccb(periph, 1);
3027 /* It is safe to retry this operation */
3028 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3029 SSD_FULL_SIZE, REWIND_TIMEOUT);
3031 softc->dsreg = MTIO_DSREG_REW;
3032 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3033 softc->dsreg = MTIO_DSREG_REST;
3035 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3036 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3038 xpt_release_ccb(ccb);
3040 softc->fileno = softc->blkno = (daddr_t) 0;
3042 softc->fileno = softc->blkno = (daddr_t) -1;
3047 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3050 struct sa_softc *softc;
3053 softc = (struct sa_softc *)periph->softc;
3055 ccb = cam_periph_getccb(periph, 1);
3057 /* This cannot be retried */
3059 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3060 SSD_FULL_SIZE, SPACE_TIMEOUT);
3063 * Clear residual because we will be using it.
3065 softc->last_ctl_resid = 0;
3067 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3068 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3069 softc->dsreg = MTIO_DSREG_REST;
3071 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3072 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3074 xpt_release_ccb(ccb);
3077 * If a spacing operation has failed, we need to invalidate
3080 * If the spacing operation was setmarks or to end of recorded data,
3081 * we no longer know our relative position.
3083 * If the spacing operations was spacing files in reverse, we
3084 * take account of the residual, but still check against less
3085 * than zero- if we've gone negative, we must have hit BOT.
3087 * If the spacing operations was spacing records in reverse and
3088 * we have a residual, we've either hit BOT or hit a filemark.
3089 * In the former case, we know our new record number (0). In
3090 * the latter case, we have absolutely no idea what the real
3091 * record number is- we've stopped between the end of the last
3092 * record in the previous file and the filemark that stopped
3093 * our spacing backwards.
3096 softc->fileno = softc->blkno = (daddr_t) -1;
3097 } else if (code == SS_SETMARKS || code == SS_EOD) {
3098 softc->fileno = softc->blkno = (daddr_t) -1;
3099 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3100 softc->fileno += (count - softc->last_ctl_resid);
3101 if (softc->fileno < 0) /* we must of hit BOT */
3104 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3105 softc->blkno += (count - softc->last_ctl_resid);
3107 if (softc->last_ctl_resid || softc->blkno < 0) {
3108 if (softc->fileno == 0) {
3111 softc->blkno = (daddr_t) -1;
3120 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3123 struct sa_softc *softc;
3126 softc = (struct sa_softc *)periph->softc;
3128 ccb = cam_periph_getccb(periph, 1);
3130 * Clear residual because we will be using it.
3132 softc->last_ctl_resid = 0;
3134 softc->dsreg = MTIO_DSREG_FMK;
3135 /* this *must* not be retried */
3136 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3137 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3138 softc->dsreg = MTIO_DSREG_REST;
3141 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3143 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3144 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3146 if (error == 0 && nmarks) {
3147 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3148 nwm = nmarks - softc->last_ctl_resid;
3149 softc->filemarks += nwm;
3152 xpt_release_ccb(ccb);
3155 * Update relative positions (if we're doing that).
3158 softc->fileno = softc->blkno = (daddr_t) -1;
3159 } else if (softc->fileno != (daddr_t) -1) {
3160 softc->fileno += nwm;
3167 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3169 struct scsi_tape_position_data loc;
3171 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3175 * We try and flush any buffered writes here if we were writing
3176 * and we're trying to get hardware block position. It eats
3177 * up performance substantially, but I'm wary of drive firmware.
3179 * I think that *logical* block position is probably okay-
3180 * but hardware block position might have to wait for data
3181 * to hit media to be valid. Caveat Emptor.
3184 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3185 error = sawritefilemarks(periph, 0, 0);
3186 if (error && error != EACCES)
3190 ccb = cam_periph_getccb(periph, 1);
3191 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3192 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3193 softc->dsreg = MTIO_DSREG_RBSY;
3194 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3195 softc->dsreg = MTIO_DSREG_REST;
3196 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3197 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3200 if (loc.flags & SA_RPOS_UNCERTAIN) {
3201 error = EINVAL; /* nothing is certain */
3203 *blkptr = scsi_4btoul(loc.firstblk);
3207 xpt_release_ccb(ccb);
3212 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3215 struct sa_softc *softc;
3219 * We used to try and flush any buffered writes here.
3220 * Now we push this onto user applications to either
3221 * flush the pending writes themselves (via a zero count
3222 * WRITE FILEMARKS command) or they can trust their tape
3223 * drive to do this correctly for them.
3226 softc = (struct sa_softc *)periph->softc;
3227 ccb = cam_periph_getccb(periph, 1);
3230 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3231 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3234 softc->dsreg = MTIO_DSREG_POS;
3235 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3236 softc->dsreg = MTIO_DSREG_REST;
3237 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3238 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3239 xpt_release_ccb(ccb);
3241 * Note relative file && block number position as now unknown.
3243 softc->fileno = softc->blkno = (daddr_t) -1;
3248 saretension(struct cam_periph *periph)
3251 struct sa_softc *softc;
3254 softc = (struct sa_softc *)periph->softc;
3256 ccb = cam_periph_getccb(periph, 1);
3258 /* It is safe to retry this operation */
3259 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3260 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3262 softc->dsreg = MTIO_DSREG_TEN;
3263 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3264 softc->dsreg = MTIO_DSREG_REST;
3266 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3267 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3268 xpt_release_ccb(ccb);
3270 softc->fileno = softc->blkno = (daddr_t) 0;
3272 softc->fileno = softc->blkno = (daddr_t) -1;
3277 sareservereleaseunit(struct cam_periph *periph, int reserve)
3280 struct sa_softc *softc;
3283 softc = (struct sa_softc *)periph->softc;
3284 ccb = cam_periph_getccb(periph, 1);
3286 /* It is safe to retry this operation */
3287 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3288 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3289 softc->dsreg = MTIO_DSREG_RBSY;
3290 error = cam_periph_runccb(ccb, saerror, 0,
3291 SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats);
3292 softc->dsreg = MTIO_DSREG_REST;
3294 xpt_release_ccb(ccb);
3297 * If the error was Illegal Request, then the device doesn't support
3298 * RESERVE/RELEASE. This is not an error.
3300 if (error == EINVAL) {
3308 saloadunload(struct cam_periph *periph, int load)
3311 struct sa_softc *softc;
3314 softc = (struct sa_softc *)periph->softc;
3316 ccb = cam_periph_getccb(periph, 1);
3318 /* It is safe to retry this operation */
3319 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3320 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3322 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3323 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3324 softc->dsreg = MTIO_DSREG_REST;
3326 xpt_release_ccb(ccb);
3328 if (error || load == 0)
3329 softc->fileno = softc->blkno = (daddr_t) -1;
3330 else if (error == 0)
3331 softc->fileno = softc->blkno = (daddr_t) 0;
3336 saerase(struct cam_periph *periph, int longerase)
3340 struct sa_softc *softc;
3343 softc = (struct sa_softc *)periph->softc;
3345 ccb = cam_periph_getccb(periph, 1);
3347 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3348 SSD_FULL_SIZE, ERASE_TIMEOUT);
3350 softc->dsreg = MTIO_DSREG_ZER;
3351 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3352 softc->dsreg = MTIO_DSREG_REST;
3354 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3355 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3356 xpt_release_ccb(ccb);
3360 #endif /* _KERNEL */
3363 * Read tape block limits command.
3366 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3367 void (*cbfcnp)(struct cam_periph *, union ccb *),
3368 u_int8_t tag_action,
3369 struct scsi_read_block_limits_data *rlimit_buf,
3370 u_int8_t sense_len, u_int32_t timeout)
3372 struct scsi_read_block_limits *scsi_cmd;
3374 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3375 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3376 sizeof(*scsi_cmd), timeout);
3378 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3379 bzero(scsi_cmd, sizeof(*scsi_cmd));
3380 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3384 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3385 void (*cbfcnp)(struct cam_periph *, union ccb *),
3386 u_int8_t tag_action, int readop, int sli,
3387 int fixed, u_int32_t length, u_int8_t *data_ptr,
3388 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3390 struct scsi_sa_rw *scsi_cmd;
3392 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3393 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3394 scsi_cmd->sli_fixed = 0;
3396 scsi_cmd->sli_fixed |= SAR_SLI;
3398 scsi_cmd->sli_fixed |= SARW_FIXED;
3399 scsi_ulto3b(length, scsi_cmd->length);
3400 scsi_cmd->control = 0;
3402 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3403 tag_action, data_ptr, dxfer_len, sense_len,
3404 sizeof(*scsi_cmd), timeout);
3408 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3409 void (*cbfcnp)(struct cam_periph *, union ccb *),
3410 u_int8_t tag_action, int immediate, int eot,
3411 int reten, int load, u_int8_t sense_len,
3414 struct scsi_load_unload *scsi_cmd;
3416 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3417 bzero(scsi_cmd, sizeof(*scsi_cmd));
3418 scsi_cmd->opcode = LOAD_UNLOAD;
3420 scsi_cmd->immediate = SLU_IMMED;
3422 scsi_cmd->eot_reten_load |= SLU_EOT;
3424 scsi_cmd->eot_reten_load |= SLU_RETEN;
3426 scsi_cmd->eot_reten_load |= SLU_LOAD;
3428 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3429 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3433 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3434 void (*cbfcnp)(struct cam_periph *, union ccb *),
3435 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3438 struct scsi_rewind *scsi_cmd;
3440 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3441 bzero(scsi_cmd, sizeof(*scsi_cmd));
3442 scsi_cmd->opcode = REWIND;
3444 scsi_cmd->immediate = SREW_IMMED;
3446 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3447 0, sense_len, sizeof(*scsi_cmd), timeout);
3451 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3452 void (*cbfcnp)(struct cam_periph *, union ccb *),
3453 u_int8_t tag_action, scsi_space_code code,
3454 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3456 struct scsi_space *scsi_cmd;
3458 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3459 scsi_cmd->opcode = SPACE;
3460 scsi_cmd->code = code;
3461 scsi_ulto3b(count, scsi_cmd->count);
3462 scsi_cmd->control = 0;
3464 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3465 0, sense_len, sizeof(*scsi_cmd), timeout);
3469 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3470 void (*cbfcnp)(struct cam_periph *, union ccb *),
3471 u_int8_t tag_action, int immediate, int setmark,
3472 u_int32_t num_marks, u_int8_t sense_len,
3475 struct scsi_write_filemarks *scsi_cmd;
3477 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3478 bzero(scsi_cmd, sizeof(*scsi_cmd));
3479 scsi_cmd->opcode = WRITE_FILEMARKS;
3481 scsi_cmd->byte2 |= SWFMRK_IMMED;
3483 scsi_cmd->byte2 |= SWFMRK_WSMK;
3485 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3487 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3488 0, sense_len, sizeof(*scsi_cmd), timeout);
3492 * The reserve and release unit commands differ only by their opcodes.
3495 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3496 void (*cbfcnp)(struct cam_periph *, union ccb *),
3497 u_int8_t tag_action, int third_party,
3498 int third_party_id, u_int8_t sense_len,
3499 u_int32_t timeout, int reserve)
3501 struct scsi_reserve_release_unit *scsi_cmd;
3503 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3504 bzero(scsi_cmd, sizeof(*scsi_cmd));
3507 scsi_cmd->opcode = RESERVE_UNIT;
3509 scsi_cmd->opcode = RELEASE_UNIT;
3512 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3513 scsi_cmd->lun_thirdparty |=
3514 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3517 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3518 0, sense_len, sizeof(*scsi_cmd), timeout);
3522 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3523 void (*cbfcnp)(struct cam_periph *, union ccb *),
3524 u_int8_t tag_action, int immediate, int long_erase,
3525 u_int8_t sense_len, u_int32_t timeout)
3527 struct scsi_erase *scsi_cmd;
3529 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3530 bzero(scsi_cmd, sizeof(*scsi_cmd));
3532 scsi_cmd->opcode = ERASE;
3535 scsi_cmd->lun_imm_long |= SE_IMMED;
3538 scsi_cmd->lun_imm_long |= SE_LONG;
3540 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3541 0, sense_len, sizeof(*scsi_cmd), timeout);
3545 * Read Tape Position command.
3548 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3549 void (*cbfcnp)(struct cam_periph *, union ccb *),
3550 u_int8_t tag_action, int hardsoft,
3551 struct scsi_tape_position_data *sbp,
3552 u_int8_t sense_len, u_int32_t timeout)
3554 struct scsi_tape_read_position *scmd;
3556 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3557 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3558 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3559 bzero(scmd, sizeof(*scmd));
3560 scmd->opcode = READ_POSITION;
3561 scmd->byte1 = hardsoft;
3565 * Set Tape Position command.
3568 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3569 void (*cbfcnp)(struct cam_periph *, union ccb *),
3570 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3571 u_int8_t sense_len, u_int32_t timeout)
3573 struct scsi_tape_locate *scmd;
3575 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3576 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3577 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3578 bzero(scmd, sizeof(*scmd));
3579 scmd->opcode = LOCATE;
3581 scmd->byte1 |= SA_SPOS_BT;
3582 scsi_ulto4b(blkno, scmd->blkaddr);