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.11 2004/05/13 23:49:11 dillon 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>
50 #include <sys/devicestat.h>
51 #include <machine/limits.h>
59 #include "../cam_ccb.h"
60 #include "../cam_extend.h"
61 #include "../cam_periph.h"
62 #include "../cam_xpt_periph.h"
63 #include "../cam_debug.h"
66 #include "scsi_message.h"
74 #define SA_IO_TIMEOUT 4
76 #ifndef SA_SPACE_TIMEOUT
77 #define SA_SPACE_TIMEOUT 1 * 60
79 #ifndef SA_REWIND_TIMEOUT
80 #define SA_REWIND_TIMEOUT 2 * 60
82 #ifndef SA_ERASE_TIMEOUT
83 #define SA_ERASE_TIMEOUT 4 * 60
86 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
88 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
89 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
90 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
91 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
94 * Additional options that can be set for config: SA_1FM_AT_EOT
97 #ifndef UNUSED_PARAMETER
98 #define UNUSED_PARAMETER(x) x = x
102 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
103 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
111 SA_STATE_NORMAL, SA_STATE_ABNORMAL
114 #define ccb_pflags ppriv_field0
115 #define ccb_bp ppriv_ptr1
117 #define SA_CCB_BUFFER_IO 0x0
118 #define SA_CCB_WAITING 0x1
119 #define SA_CCB_TYPEMASK 0x1
120 #define SA_POSITION_UPDATED 0x2
122 #define Set_CCB_Type(x, type) \
123 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
124 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_NUM_MODES 4
201 struct sa_mode_devs {
205 } mode_devs[SA_NUM_MODES];
215 struct buf_queue_head buf_queue;
217 struct devstat device_stats;
224 u_int32_t comp_algorithm;
225 u_int32_t saved_comp_algorithm;
226 u_int32_t media_blksize;
227 u_int32_t last_media_blksize;
228 u_int32_t media_numblks;
229 u_int8_t media_density;
232 u_int8_t dsreg; /* mtio mt_dsreg, redux */
236 int last_resid_was_io;
239 * Relative to BOT Location.
248 struct scsi_sense_data _last_io_sense;
249 u_int32_t _last_io_resid;
250 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
251 struct scsi_sense_data _last_ctl_sense;
252 u_int32_t _last_ctl_resid;
253 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
254 #define last_io_sense errinfo._last_io_sense
255 #define last_io_resid errinfo._last_io_resid
256 #define last_io_cdb errinfo._last_io_cdb
257 #define last_ctl_sense errinfo._last_ctl_sense
258 #define last_ctl_resid errinfo._last_ctl_resid
259 #define last_ctl_cdb errinfo._last_ctl_cdb
262 * Misc other flags/state
266 ctrl_mode : 1; /* control device open */
269 struct sa_quirk_entry {
270 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
271 sa_quirks quirks; /* specific quirk type */
272 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
275 static struct sa_quirk_entry sa_quirk_table[] =
278 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
279 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
280 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
283 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
284 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
287 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
288 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
291 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
292 "Python*", "*"}, SA_QUIRK_NODREAD, 0
295 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
296 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
299 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
300 "VIPER 2525 25462", "-011"},
301 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
304 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
305 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
309 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
310 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
314 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
315 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
318 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
319 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
322 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
323 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
326 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
327 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
330 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
331 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
334 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
335 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
337 { /* jreynold@primenet.com */
338 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
339 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
341 { /* mike@sentex.net */
342 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
343 "STT20000*", "*"}, SA_QUIRK_1FM, 0
346 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
347 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
350 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
351 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
354 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
355 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
358 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
359 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
362 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
363 " SLR*", "*"}, SA_QUIRK_1FM, 0
366 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
367 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
370 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
371 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
375 static d_open_t saopen;
376 static d_close_t saclose;
377 static d_strategy_t sastrategy;
378 static d_ioctl_t saioctl;
379 static periph_init_t sainit;
380 static periph_ctor_t saregister;
381 static periph_oninv_t saoninvalidate;
382 static periph_dtor_t sacleanup;
383 static periph_start_t sastart;
384 static void saasync(void *callback_arg, u_int32_t code,
385 struct cam_path *path, void *arg);
386 static void sadone(struct cam_periph *periph,
387 union ccb *start_ccb);
388 static int saerror(union ccb *ccb, u_int32_t cam_flags,
389 u_int32_t sense_flags);
390 static int samarkswanted(struct cam_periph *);
391 static int sacheckeod(struct cam_periph *periph);
392 static int sagetparams(struct cam_periph *periph,
393 sa_params params_to_get,
394 u_int32_t *blocksize, u_int8_t *density,
395 u_int32_t *numblocks, int *buff_mode,
396 u_int8_t *write_protect, u_int8_t *speed,
397 int *comp_supported, int *comp_enabled,
398 u_int32_t *comp_algorithm,
399 sa_comp_t *comp_page);
400 static int sasetparams(struct cam_periph *periph,
401 sa_params params_to_set,
402 u_int32_t blocksize, u_int8_t density,
403 u_int32_t comp_algorithm,
404 u_int32_t sense_flags);
405 static void saprevent(struct cam_periph *periph, int action);
406 static int sarewind(struct cam_periph *periph);
407 static int saspace(struct cam_periph *periph, int count,
408 scsi_space_code code);
409 static int samount(struct cam_periph *, int, dev_t);
410 static int saretension(struct cam_periph *periph);
411 static int sareservereleaseunit(struct cam_periph *periph,
413 static int saloadunload(struct cam_periph *periph, int load);
414 static int saerase(struct cam_periph *periph, int longerase);
415 static int sawritefilemarks(struct cam_periph *periph,
416 int nmarks, int setmarks);
417 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
418 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
421 static struct periph_driver sadriver =
424 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
427 DATA_SET(periphdriver_set, sadriver);
429 /* For 2.2-stable support */
434 #define SA_CDEV_MAJOR 14
436 static struct cdevsw sa_cdevsw = {
438 /* maj */ SA_CDEV_MAJOR,
446 /* write */ physwrite,
450 /* strategy */ sastrategy,
455 static struct extend_array *saperiphs;
458 saopen(dev_t dev, int flags, int fmt, struct thread *td)
460 struct cam_periph *periph;
461 struct sa_softc *softc;
470 density = SADENSITY(dev);
473 periph = cam_extend_get(saperiphs, unit);
474 if (periph == NULL) {
478 softc = (struct sa_softc *)periph->softc;
479 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
485 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
486 ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
488 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
489 cam_periph_unlock(periph);
492 if (SA_IS_CTRL(dev)) {
493 softc->ctrl_mode = 1;
494 cam_periph_unlock(periph);
499 if (softc->flags & SA_FLAG_OPEN) {
501 } else if (softc->flags & SA_FLAG_INVALID) {
505 * The function samount ensures media is loaded and ready.
506 * It also does a device RESERVE if the tape isn't yet mounted.
508 error = samount(periph, flags, dev);
512 cam_periph_release(periph);
514 saprevent(periph, PR_PREVENT);
515 softc->flags |= SA_FLAG_OPEN;
517 cam_periph_unlock(periph);
522 saclose(dev_t dev, int flag, int fmt, struct thread *td)
524 struct cam_periph *periph;
525 struct sa_softc *softc;
526 int unit, mode, error, writing, tmp;
527 int closedbits = SA_FLAG_OPEN;
531 periph = cam_extend_get(saperiphs, unit);
535 softc = (struct sa_softc *)periph->softc;
537 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
538 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
541 if ((error = cam_periph_lock(periph, 0)) != 0) {
545 if (SA_IS_CTRL(dev)) {
546 softc->ctrl_mode = 0;
547 cam_periph_release(periph);
548 cam_periph_unlock(periph);
553 * Were we writing the tape?
555 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
558 * See whether or not we need to write filemarks. If this
559 * fails, we probably have to assume we've lost tape
562 error = sacheckeod(periph);
564 xpt_print_path(periph->path);
565 printf("failed to write terminating filemark(s)\n");
566 softc->flags |= SA_FLAG_TAPE_FROZEN;
570 * Whatever we end up doing, allow users to eject tapes from here on.
572 saprevent(periph, PR_ALLOW);
575 * Decide how to end...
577 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
578 closedbits |= SA_FLAG_TAPE_FROZEN;
579 } else switch (mode) {
580 case SA_MODE_OFFLINE:
582 * An 'offline' close is an unconditional release of
583 * frozen && mount conditions, irrespective of whether
584 * these operations succeeded. The reason for this is
585 * to allow at least some kind of programmatic way
586 * around our state getting all fouled up. If somebody
587 * issues an 'offline' command, that will be allowed
590 (void) sarewind(periph);
591 (void) saloadunload(periph, FALSE);
592 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
596 * If the rewind fails, return an error- if anyone cares,
597 * but not overwriting any previous error.
599 * We don't clear the notion of mounted here, but we do
600 * clear the notion of frozen if we successfully rewound.
602 tmp = sarewind(periph);
607 closedbits |= SA_FLAG_TAPE_FROZEN;
610 case SA_MODE_NOREWIND:
612 * If we're not rewinding/unloading the tape, find out
613 * whether we need to back up over one of two filemarks
614 * we wrote (if we wrote two filemarks) so that appends
615 * from this point on will be sane.
617 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
618 tmp = saspace(periph, -1, SS_FILEMARKS);
620 xpt_print_path(periph->path);
621 printf("unable to backspace over one of double"
622 " filemarks at end of tape\n");
623 xpt_print_path(periph->path);
624 printf("it is possible that this device"
625 " needs a SA_QUIRK_1FM quirk set for it\n");
626 softc->flags |= SA_FLAG_TAPE_FROZEN;
631 xpt_print_path(periph->path);
632 panic("unknown mode 0x%x in saclose\n", mode);
638 * We wish to note here that there are no more filemarks to be written.
640 softc->filemarks = 0;
641 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
644 * And we are no longer open for business.
646 softc->flags &= ~closedbits;
649 * Inform users if tape state if frozen....
651 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
652 xpt_print_path(periph->path);
653 printf("tape is now frozen- use an OFFLINE, REWIND or MTEOM "
654 "command to clear this state.\n");
657 /* release the device if it is no longer mounted */
658 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
659 sareservereleaseunit(periph, FALSE);
661 cam_periph_unlock(periph);
662 cam_periph_release(periph);
668 * Actually translate the requested transfer into one the physical driver
669 * can understand. The transfer is described by a buf and will include
670 * only one physical transfer.
673 sastrategy(struct buf *bp)
675 struct cam_periph *periph;
676 struct sa_softc *softc;
680 if (SA_IS_CTRL(bp->b_dev)) {
681 bp->b_error = EINVAL;
684 unit = SAUNIT(bp->b_dev);
685 periph = cam_extend_get(saperiphs, unit);
686 if (periph == NULL) {
690 softc = (struct sa_softc *)periph->softc;
694 if (softc->flags & SA_FLAG_INVALID) {
700 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
709 * If it's a null transfer, return immediatly
711 if (bp->b_bcount == 0)
715 if (softc->flags & SA_FLAG_FIXED) {
717 * Fixed block device. The byte count must
718 * be a multiple of our block size.
720 if (((softc->blk_mask != ~0) &&
721 ((bp->b_bcount & softc->blk_mask) != 0)) ||
722 ((softc->blk_mask == ~0) &&
723 ((bp->b_bcount % softc->min_blk) != 0))) {
724 xpt_print_path(periph->path);
725 printf("Invalid request. Fixed block device "
726 "requests must be a multiple "
727 "of %d bytes\n", softc->media_blksize);
728 bp->b_error = EINVAL;
731 } else if ((bp->b_bcount > softc->max_blk) ||
732 (bp->b_bcount < softc->min_blk) ||
733 (bp->b_bcount & softc->blk_mask) != 0) {
735 xpt_print_path(periph->path);
736 printf("Invalid request. Variable block device "
737 "requests must be ");
738 if (softc->blk_mask != 0) {
739 printf("a multiple of %d ", (0x1 << softc->blk_gran));
741 printf("between %d and %d bytes\n", softc->min_blk,
743 bp->b_error = EINVAL;
748 * Mask interrupts so that the device cannot be invalidated until
749 * after we are in the queue. Otherwise, we might not properly
750 * clean up one of the buffers.
755 * Place it at the end of the queue.
757 bufq_insert_tail(&softc->buf_queue, bp);
759 softc->queue_count++;
760 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("sastrategy: enqueuing a %d "
761 "%s byte %s queue count now %d\n", (int) bp->b_bcount,
762 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
763 (bp->b_flags & B_READ)? "read" : "write", softc->queue_count));
768 * Schedule ourselves for performing the work.
770 xpt_schedule(periph, 1);
774 bp->b_flags |= B_ERROR;
778 * Correctly set the buf to indicate a completed xfer
780 bp->b_resid = bp->b_bcount;
785 saioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
787 struct cam_periph *periph;
788 struct sa_softc *softc;
789 scsi_space_code spaceop;
790 int didlockperiph = 0;
799 density = SADENSITY(dev);
800 error = 0; /* shut up gcc */
801 spaceop = 0; /* shut up gcc */
803 periph = cam_extend_get(saperiphs, unit);
807 softc = (struct sa_softc *)periph->softc;
810 * Check for control mode accesses. We allow MTIOCGET and
811 * MTIOCERRSTAT (but need to be the only one open in order
812 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
813 * and MTCOMP (but need to be the only one accessing this
814 * device to run those).
817 if (SA_IS_CTRL(dev)) {
819 case MTIOCGETEOTMODEL:
824 * If the periph isn't already locked, lock it
825 * so our MTIOCERRSTAT can reset latched error stats.
827 * If the periph is already locked, skip it because
828 * we're just getting status and it'll be up to the
829 * other thread that has this device open to do
830 * an MTIOCERRSTAT that would clear latched status.
833 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
834 error = cam_periph_lock(periph, PCATCH);
843 case MTIOCSETEOTMODEL:
848 * We need to acquire the peripheral here rather
849 * than at open time because we are sharing writable
850 * access to data structures.
853 error = cam_periph_lock(periph, PCATCH);
867 * Find the device that the user is talking about
872 struct mtget *g = (struct mtget *)arg;
875 * If this isn't the control mode device, actually go out
876 * and ask the drive again what it's set to.
878 if (!SA_IS_CTRL(dev)) {
879 u_int8_t write_protect;
880 int comp_enabled, comp_supported;
881 error = sagetparams(periph, SA_PARAM_ALL,
882 &softc->media_blksize, &softc->media_density,
883 &softc->media_numblks, &softc->buffer_mode,
884 &write_protect, &softc->speed, &comp_supported,
885 &comp_enabled, &softc->comp_algorithm, NULL);
889 softc->flags |= SA_FLAG_TAPE_WP;
891 softc->flags &= ~SA_FLAG_TAPE_WP;
892 softc->flags &= ~(SA_FLAG_COMP_SUPP|
893 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
894 if (comp_supported) {
895 if (softc->saved_comp_algorithm == 0)
896 softc->saved_comp_algorithm =
897 softc->comp_algorithm;
898 softc->flags |= SA_FLAG_COMP_SUPP;
900 softc->flags |= SA_FLAG_COMP_ENABLED;
902 softc->flags |= SA_FLAG_COMP_UNSUPP;
904 bzero(g, sizeof(struct mtget));
905 g->mt_type = MT_ISAR;
906 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
907 g->mt_comp = MT_COMP_UNSUPP;
908 g->mt_comp0 = MT_COMP_UNSUPP;
909 g->mt_comp1 = MT_COMP_UNSUPP;
910 g->mt_comp2 = MT_COMP_UNSUPP;
911 g->mt_comp3 = MT_COMP_UNSUPP;
913 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
914 g->mt_comp = MT_COMP_DISABLED;
916 g->mt_comp = softc->comp_algorithm;
918 g->mt_comp0 = softc->comp_algorithm;
919 g->mt_comp1 = softc->comp_algorithm;
920 g->mt_comp2 = softc->comp_algorithm;
921 g->mt_comp3 = softc->comp_algorithm;
923 g->mt_density = softc->media_density;
924 g->mt_density0 = softc->media_density;
925 g->mt_density1 = softc->media_density;
926 g->mt_density2 = softc->media_density;
927 g->mt_density3 = softc->media_density;
928 g->mt_blksiz = softc->media_blksize;
929 g->mt_blksiz0 = softc->media_blksize;
930 g->mt_blksiz1 = softc->media_blksize;
931 g->mt_blksiz2 = softc->media_blksize;
932 g->mt_blksiz3 = softc->media_blksize;
933 g->mt_fileno = softc->fileno;
934 g->mt_blkno = softc->blkno;
935 g->mt_dsreg = (short) softc->dsreg;
937 * Yes, we know that this is likely to overflow
939 if (softc->last_resid_was_io) {
940 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
941 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
942 softc->last_io_resid = 0;
946 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
947 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
948 softc->last_ctl_resid = 0;
957 struct scsi_tape_errors *sep =
958 &((union mterrstat *)arg)->scsi_errstat;
960 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
961 ("saioctl: MTIOCERRSTAT\n"));
963 bzero(sep, sizeof(*sep));
964 sep->io_resid = softc->last_io_resid;
965 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
966 sizeof (sep->io_sense));
967 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
968 sizeof (sep->io_cdb));
969 sep->ctl_resid = softc->last_ctl_resid;
970 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
971 sizeof (sep->ctl_sense));
972 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
973 sizeof (sep->ctl_cdb));
975 if (SA_IS_CTRL(dev) == 0 || didlockperiph)
976 bzero((caddr_t) &softc->errinfo,
977 sizeof (softc->errinfo));
986 mt = (struct mtop *)arg;
988 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
989 ("saioctl: op=0x%x count=0x%x\n",
990 mt->mt_op, mt->mt_count));
992 count = mt->mt_count;
994 case MTWEOF: /* write an end-of-file marker */
996 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
997 * flag because by keeping track of filemarks
998 * we have last written we know ehether or not
999 * we need to write more when we close the device.
1001 error = sawritefilemarks(periph, count, FALSE);
1003 case MTWSS: /* write a setmark */
1004 error = sawritefilemarks(periph, count, TRUE);
1006 case MTBSR: /* backward space record */
1007 case MTFSR: /* forward space record */
1008 case MTBSF: /* backward space file */
1009 case MTFSF: /* forward space file */
1010 case MTBSS: /* backward space setmark */
1011 case MTFSS: /* forward space setmark */
1012 case MTEOD: /* space to end of recorded medium */
1016 spaceop = SS_FILEMARKS;
1017 nmarks = softc->filemarks;
1018 error = sacheckeod(periph);
1020 xpt_print_path(periph->path);
1021 printf("EOD check prior to spacing failed\n");
1022 softc->flags |= SA_FLAG_EIO_PENDING;
1025 nmarks -= softc->filemarks;
1031 spaceop = SS_BLOCKS;
1042 spaceop = SS_SETMARKS;
1056 nmarks = softc->filemarks;
1058 * XXX: Why are we checking again?
1060 error = sacheckeod(periph);
1063 nmarks -= softc->filemarks;
1064 error = saspace(periph, count - nmarks, spaceop);
1066 * At this point, clear that we've written the tape
1067 * and that we've written any filemarks. We really
1068 * don't know what the applications wishes to do next-
1069 * the sacheckeod's will make sure we terminated the
1070 * tape correctly if we'd been writing, but the next
1071 * action the user application takes will set again
1072 * whether we need to write filemarks.
1075 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1076 softc->filemarks = 0;
1079 case MTREW: /* rewind */
1080 (void) sacheckeod(periph);
1081 error = sarewind(periph);
1084 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1085 softc->flags &= ~SA_FLAG_ERR_PENDING;
1086 softc->filemarks = 0;
1088 case MTERASE: /* erase */
1089 error = saerase(periph, count);
1091 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1092 softc->flags &= ~SA_FLAG_ERR_PENDING;
1094 case MTRETENS: /* re-tension tape */
1095 error = saretension(periph);
1097 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1098 softc->flags &= ~SA_FLAG_ERR_PENDING;
1100 case MTOFFL: /* rewind and put the drive offline */
1102 (void) sacheckeod(periph);
1104 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1105 softc->filemarks = 0;
1107 error = sarewind(periph);
1108 /* clear the frozen flag anyway */
1109 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1112 * Be sure to allow media removal before ejecting.
1115 saprevent(periph, PR_ALLOW);
1117 error = saloadunload(periph, FALSE);
1119 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1124 case MTNOP: /* no operation, sets status only */
1125 case MTCACHE: /* enable controller cache */
1126 case MTNOCACHE: /* disable controller cache */
1130 case MTSETBSIZ: /* Set block size for device */
1132 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1135 softc->last_media_blksize =
1136 softc->media_blksize;
1137 softc->media_blksize = count;
1139 softc->flags |= SA_FLAG_FIXED;
1140 if (powerof2(count)) {
1143 softc->blk_mask = count - 1;
1145 softc->blk_mask = ~0;
1146 softc->blk_shift = 0;
1149 * Make the user's desire 'persistent'.
1151 softc->quirks &= ~SA_QUIRK_VARIABLE;
1152 softc->quirks |= SA_QUIRK_FIXED;
1154 softc->flags &= ~SA_FLAG_FIXED;
1155 if (softc->max_blk == 0) {
1156 softc->max_blk = ~0;
1158 softc->blk_shift = 0;
1159 if (softc->blk_gran != 0) {
1161 softc->blk_gran - 1;
1163 softc->blk_mask = 0;
1166 * Make the user's desire 'persistent'.
1168 softc->quirks |= SA_QUIRK_VARIABLE;
1169 softc->quirks &= ~SA_QUIRK_FIXED;
1173 case MTSETDNSTY: /* Set density for device and mode */
1174 if (count > UCHAR_MAX) {
1178 error = sasetparams(periph, SA_PARAM_DENSITY,
1182 case MTCOMP: /* enable compression */
1184 * Some devices don't support compression, and
1185 * don't like it if you ask them for the
1188 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1189 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1193 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1194 0, 0, count, SF_NO_PRINT);
1206 error = sardpos(periph, 0, (u_int32_t *) arg);
1209 error = sardpos(periph, 1, (u_int32_t *) arg);
1212 error = sasetpos(periph, 0, (u_int32_t *) arg);
1215 error = sasetpos(periph, 1, (u_int32_t *) arg);
1217 case MTIOCGETEOTMODEL:
1219 if (softc->quirks & SA_QUIRK_1FM)
1223 *((u_int32_t *) arg) = mode;
1225 case MTIOCSETEOTMODEL:
1227 switch (*((u_int32_t *) arg)) {
1229 softc->quirks &= ~SA_QUIRK_2FM;
1230 softc->quirks |= SA_QUIRK_1FM;
1233 softc->quirks &= ~SA_QUIRK_1FM;
1234 softc->quirks |= SA_QUIRK_2FM;
1242 error = cam_periph_ioctl(periph, cmd, arg, saerror);
1247 * Check to see if we cleared a frozen state
1249 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1255 softc->fileno = (daddr_t) -1;
1256 softc->blkno = (daddr_t) -1;
1257 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1258 xpt_print_path(periph->path);
1259 printf("tape state now unfrozen.\n");
1265 if (didlockperiph) {
1266 cam_periph_unlock(periph);
1275 struct cam_path *path;
1278 * Create our extend array for storing the devices we attach to.
1280 saperiphs = cam_extend_new();
1281 if (saperiphs == NULL) {
1282 printf("sa: Failed to alloc extend array!\n");
1287 * Install a global async callback.
1289 status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1290 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1292 if (status == CAM_REQ_CMP) {
1293 /* Register the async callbacks of interrest */
1294 struct ccb_setasync csa; /*
1295 * This is an immediate CCB,
1296 * so using the stack is OK
1298 xpt_setup_ccb(&csa.ccb_h, path, 5);
1299 csa.ccb_h.func_code = XPT_SASYNC_CB;
1300 csa.event_enable = AC_FOUND_DEVICE;
1301 csa.callback = saasync;
1302 csa.callback_arg = NULL;
1303 xpt_action((union ccb *)&csa);
1304 status = csa.ccb_h.status;
1305 xpt_free_path(path);
1308 if (status != CAM_REQ_CMP) {
1309 printf("sa: Failed to attach master async callback "
1310 "due to status 0x%x!\n", status);
1315 saoninvalidate(struct cam_periph *periph)
1317 struct sa_softc *softc;
1319 struct ccb_setasync csa;
1322 softc = (struct sa_softc *)periph->softc;
1325 * De-register any async callbacks.
1327 xpt_setup_ccb(&csa.ccb_h, periph->path,
1329 csa.ccb_h.func_code = XPT_SASYNC_CB;
1330 csa.event_enable = 0;
1331 csa.callback = saasync;
1332 csa.callback_arg = periph;
1333 xpt_action((union ccb *)&csa);
1335 softc->flags |= SA_FLAG_INVALID;
1338 * Although the oninvalidate() routines are always called at
1339 * splsoftcam, we need to be at splbio() here to keep the buffer
1340 * queue from being modified while we traverse it.
1345 * Return all queued I/O with ENXIO.
1346 * XXX Handle any transactions queued to the card
1347 * with XPT_ABORT_CCB.
1349 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
1350 bufq_remove(&softc->buf_queue, q_bp);
1351 q_bp->b_resid = q_bp->b_bcount;
1352 q_bp->b_error = ENXIO;
1353 q_bp->b_flags |= B_ERROR;
1356 softc->queue_count = 0;
1359 xpt_print_path(periph->path);
1360 printf("lost device\n");
1365 sacleanup(struct cam_periph *periph)
1367 struct sa_softc *softc;
1370 softc = (struct sa_softc *)periph->softc;
1372 devstat_remove_entry(&softc->device_stats);
1374 destroy_dev(softc->devs.ctl_dev);
1375 destroy_dev(softc->devs.r_dev);
1376 destroy_dev(softc->devs.nr_dev);
1377 destroy_dev(softc->devs.er_dev);
1379 for (i = 0; i < SA_NUM_MODES; i++) {
1380 destroy_dev(softc->devs.mode_devs[i].r_dev);
1381 destroy_dev(softc->devs.mode_devs[i].nr_dev);
1382 destroy_dev(softc->devs.mode_devs[i].er_dev);
1385 cam_extend_release(saperiphs, periph->unit_number);
1386 xpt_print_path(periph->path);
1387 printf("removing device entry\n");
1388 free(softc, M_DEVBUF);
1392 saasync(void *callback_arg, u_int32_t code,
1393 struct cam_path *path, void *arg)
1395 struct cam_periph *periph;
1397 periph = (struct cam_periph *)callback_arg;
1399 case AC_FOUND_DEVICE:
1401 struct ccb_getdev *cgd;
1404 cgd = (struct ccb_getdev *)arg;
1408 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1412 * Allocate a peripheral instance for
1413 * this device and start the probe
1416 status = cam_periph_alloc(saregister, saoninvalidate,
1418 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1419 saasync, AC_FOUND_DEVICE, cgd);
1421 if (status != CAM_REQ_CMP
1422 && status != CAM_REQ_INPROG)
1423 printf("saasync: Unable to probe new device "
1424 "due to status 0x%x\n", status);
1428 cam_periph_async(periph, code, path, arg);
1434 saregister(struct cam_periph *periph, void *arg)
1436 struct sa_softc *softc;
1437 struct ccb_setasync csa;
1438 struct ccb_getdev *cgd;
1442 cgd = (struct ccb_getdev *)arg;
1443 if (periph == NULL) {
1444 printf("saregister: periph was NULL!!\n");
1445 return (CAM_REQ_CMP_ERR);
1449 printf("saregister: no getdev CCB, can't register device\n");
1450 return (CAM_REQ_CMP_ERR);
1453 softc = malloc(sizeof (*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1454 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1455 softc->state = SA_STATE_NORMAL;
1456 softc->fileno = (daddr_t) -1;
1457 softc->blkno = (daddr_t) -1;
1459 bufq_init(&softc->buf_queue);
1460 periph->softc = softc;
1461 cam_extend_set(saperiphs, periph->unit_number, periph);
1464 * See if this device has any quirks.
1466 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1467 (caddr_t)sa_quirk_table,
1468 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1469 sizeof(*sa_quirk_table), scsi_inquiry_match);
1471 if (match != NULL) {
1472 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1473 softc->last_media_blksize =
1474 ((struct sa_quirk_entry *)match)->prefblk;
1476 xpt_print_path(periph->path);
1477 printf("found quirk entry %d\n", (int)
1478 (((struct sa_quirk_entry *) match) - sa_quirk_table));
1481 softc->quirks = SA_QUIRK_NONE;
1484 * The SA driver supports a blocksize, but we don't know the
1485 * blocksize until we media is inserted. So, set a flag to
1486 * indicate that the blocksize is unavailable right now.
1488 devstat_add_entry(&softc->device_stats, "sa", periph->unit_number, 0,
1489 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1490 DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE);
1492 softc->devs.ctl_dev = make_dev(&sa_cdevsw, SAMINOR(SA_CTLDEV,
1493 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1494 0660, "r%s%d.ctl", periph->periph_name, periph->unit_number);
1496 softc->devs.r_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV,
1497 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1498 0660, "r%s%d", periph->periph_name, periph->unit_number);
1500 softc->devs.nr_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV,
1501 periph->unit_number, 0, SA_ATYPE_NR), UID_ROOT, GID_OPERATOR,
1502 0660, "nr%s%d", periph->periph_name, periph->unit_number);
1504 softc->devs.er_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV,
1505 periph->unit_number, 0, SA_ATYPE_ER), UID_ROOT, GID_OPERATOR,
1506 0660, "er%s%d", periph->periph_name, periph->unit_number);
1508 for (i = 0; i < SA_NUM_MODES; i++) {
1510 softc->devs.mode_devs[i].r_dev = make_dev(&sa_cdevsw,
1511 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R),
1512 UID_ROOT, GID_OPERATOR, 0660, "r%s%d.%d",
1513 periph->periph_name, periph->unit_number, i);
1515 softc->devs.mode_devs[i].nr_dev = make_dev(&sa_cdevsw,
1516 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR),
1517 UID_ROOT, GID_OPERATOR, 0660, "nr%s%d.%d",
1518 periph->periph_name, periph->unit_number, i);
1521 softc->devs.mode_devs[i].er_dev = make_dev(&sa_cdevsw,
1522 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER),
1523 UID_ROOT, GID_OPERATOR, 0660, "er%s%d.%d",
1524 periph->periph_name, periph->unit_number, i);
1528 * Add an async callback so that we get
1529 * notified if this device goes away.
1531 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
1532 csa.ccb_h.func_code = XPT_SASYNC_CB;
1533 csa.event_enable = AC_LOST_DEVICE;
1534 csa.callback = saasync;
1535 csa.callback_arg = periph;
1536 xpt_action((union ccb *)&csa);
1538 xpt_announce_periph(periph, NULL);
1540 return (CAM_REQ_CMP);
1544 sastart(struct cam_periph *periph, union ccb *start_ccb)
1546 struct sa_softc *softc;
1548 softc = (struct sa_softc *)periph->softc;
1550 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("sastart"));
1553 switch (softc->state) {
1554 case SA_STATE_NORMAL:
1556 /* Pull a buffer from the queue and get going on it */
1561 * See if there is a buf with work for us to do..
1564 bp = bufq_first(&softc->buf_queue);
1565 if (periph->immediate_priority <= periph->pinfo.priority) {
1566 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1567 ("queuing for immediate ccb\n"));
1568 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1569 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1571 periph->immediate_priority = CAM_PRIORITY_NONE;
1573 wakeup(&periph->ccb_list);
1574 } else if (bp == NULL) {
1576 xpt_release_ccb(start_ccb);
1577 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1578 struct buf *done_bp;
1580 softc->queue_count--;
1581 bufq_remove(&softc->buf_queue, bp);
1582 bp->b_resid = bp->b_bcount;
1584 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1586 * We now just clear errors in this case
1587 * and let the residual be the notifier.
1590 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1592 * This can only happen if we're reading
1593 * in fixed length mode. In this case,
1594 * we dump the rest of the list the
1598 if (bufq_first(&softc->buf_queue) != NULL) {
1602 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1604 bp->b_flags |= B_ERROR;
1606 bp = bufq_first(&softc->buf_queue);
1608 * Only if we have no other buffers queued up
1609 * do we clear the pending error flag.
1612 softc->flags &= ~SA_FLAG_ERR_PENDING;
1613 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1614 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
1615 "%d more buffers queued up\n",
1616 (softc->flags & SA_FLAG_ERR_PENDING),
1617 (bp != NULL)? "not " : " ", softc->queue_count));
1619 xpt_release_ccb(start_ccb);
1624 bufq_remove(&softc->buf_queue, bp);
1625 softc->queue_count--;
1627 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1628 if (softc->blk_shift != 0) {
1630 bp->b_bcount >> softc->blk_shift;
1631 } else if (softc->media_blksize != 0) {
1633 bp->b_bcount / softc->media_blksize;
1636 xpt_print_path(periph->path);
1637 printf("zero blocksize for "
1638 "FIXED length writes?\n");
1643 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1644 ("Fixed Record Count is %d\n", length));
1646 length = bp->b_bcount;
1647 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1648 ("Variable Record Count is %d\n", length));
1650 devstat_start_transaction(&softc->device_stats);
1652 * Some people have theorized that we should
1653 * suppress illegal length indication if we are
1654 * running in variable block mode so that we don't
1655 * have to request sense every time our requested
1656 * block size is larger than the written block.
1657 * The residual information from the ccb allows
1658 * us to identify this situation anyway. The only
1659 * problem with this is that we will not get
1660 * information about blocks that are larger than
1661 * our read buffer unless we set the block size
1662 * in the mode page to something other than 0.
1664 * I believe that this is a non-issue. If user apps
1665 * don't adjust their read size to match our record
1666 * size, that's just life. Anyway, the typical usage
1667 * would be to issue, e.g., 64KB reads and occasionally
1668 * have to do deal with 512 byte or 1KB intermediate
1671 softc->dsreg = (bp->b_flags & B_READ)?
1672 MTIO_DSREG_RD : MTIO_DSREG_WR;
1673 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1674 MSG_SIMPLE_Q_TAG, (bp->b_flags & B_READ) != 0,
1675 FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1676 length, bp->b_data, bp->b_bcount, SSD_FULL_SIZE,
1678 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1679 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1680 start_ccb->ccb_h.ccb_bp = bp;
1681 bp = bufq_first(&softc->buf_queue);
1683 xpt_action(start_ccb);
1687 /* Have more work to do, so ensure we stay scheduled */
1688 xpt_schedule(periph, 1);
1692 case SA_STATE_ABNORMAL:
1694 panic("state 0x%x in sastart", softc->state);
1701 sadone(struct cam_periph *periph, union ccb *done_ccb)
1703 struct sa_softc *softc;
1704 struct ccb_scsiio *csio;
1706 softc = (struct sa_softc *)periph->softc;
1707 csio = &done_ccb->csio;
1708 switch (CCB_Type(csio)) {
1709 case SA_CCB_BUFFER_IO:
1714 softc->dsreg = MTIO_DSREG_REST;
1715 bp = (struct buf *)done_ccb->ccb_h.ccb_bp;
1717 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1718 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1720 * A retry was scheduled, so just return.
1731 * Catastrophic error. Mark the tape as frozen
1732 * (we no longer know tape position).
1734 * Return all queued I/O with EIO, and unfreeze
1735 * our queue so that future transactions that
1736 * attempt to fix this problem can get to the
1742 softc->flags |= SA_FLAG_TAPE_FROZEN;
1743 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL) {
1744 bufq_remove(&softc->buf_queue, q_bp);
1745 q_bp->b_resid = q_bp->b_bcount;
1746 q_bp->b_error = EIO;
1747 q_bp->b_flags |= B_ERROR;
1753 bp->b_resid = bp->b_bcount;
1754 bp->b_error = error;
1755 bp->b_flags |= B_ERROR;
1757 * In the error case, position is updated in saerror.
1760 bp->b_resid = csio->resid;
1762 if (csio->resid != 0) {
1763 bp->b_flags |= B_ERROR;
1765 if ((bp->b_flags & B_READ) == 0) {
1766 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1767 softc->filemarks = 0;
1769 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1770 (softc->blkno != (daddr_t) -1)) {
1771 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1773 if (softc->blk_shift != 0) {
1778 softc->media_blksize;
1780 softc->blkno += (daddr_t) l;
1787 * If we had an error (immediate or pending),
1788 * release the device queue now.
1790 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1791 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1793 if (error || bp->b_resid) {
1794 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1795 ("error %d resid %ld count %ld\n", error,
1796 bp->b_resid, bp->b_bcount));
1799 devstat_end_transaction_buf(&softc->device_stats, bp);
1803 case SA_CCB_WAITING:
1805 /* Caller will release the CCB */
1806 wakeup(&done_ccb->ccb_h.cbfcnp);
1810 xpt_release_ccb(done_ccb);
1814 * Mount the tape (make sure it's ready for I/O).
1817 samount(struct cam_periph *periph, int oflags, dev_t dev)
1819 struct sa_softc *softc;
1824 * oflags can be checked for 'kind' of open (read-only check) - later
1825 * dev can be checked for a control-mode or compression open - later
1827 UNUSED_PARAMETER(oflags);
1828 UNUSED_PARAMETER(dev);
1831 softc = (struct sa_softc *)periph->softc;
1834 * This should determine if something has happend since the last
1835 * open/mount that would invalidate the mount. We do *not* want
1836 * to retry this command- we just want the status. But we only
1837 * do this if we're mounted already- if we're not mounted,
1838 * we don't care about the unit read state and can instead use
1839 * this opportunity to attempt to reserve the tape unit.
1842 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1843 ccb = cam_periph_getccb(periph, 1);
1844 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1845 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1846 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1847 &softc->device_stats);
1849 if (error == ENXIO) {
1850 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1851 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1852 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1853 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1854 &softc->device_stats);
1858 * We don't need to freeze the tape because we
1859 * will now attempt to rewind/load it.
1861 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1862 if (CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO)) {
1863 xpt_print_path(ccb->ccb_h.path);
1864 printf("error %d on TUR in samount\n", error);
1868 error = sareservereleaseunit(periph, TRUE);
1872 ccb = cam_periph_getccb(periph, 1);
1873 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1874 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1875 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1876 &softc->device_stats);
1880 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1881 struct scsi_read_block_limits_data *rblim = NULL;
1882 int comp_enabled, comp_supported;
1883 u_int8_t write_protect, guessing = 0;
1886 * Clear out old state.
1888 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1889 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1890 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1891 softc->filemarks = 0;
1894 * *Very* first off, make sure we're loaded to BOT.
1896 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1897 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1898 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1899 &softc->device_stats);
1903 * In case this doesn't work, do a REWIND instead
1906 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1907 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1908 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1909 &softc->device_stats);
1913 xpt_release_ccb(ccb);
1918 * Do a dummy test read to force access to the
1919 * media so that the drive will really know what's
1920 * there. We actually don't really care what the
1921 * blocksize on tape is and don't expect to really
1922 * read a full record.
1924 rblim = malloc(8192, M_TEMP, M_INTWAIT);
1926 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1927 scsi_sa_read_write(&ccb->csio, 0, sadone,
1928 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1929 (void *) rblim, 8192, SSD_FULL_SIZE,
1931 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1932 &softc->device_stats);
1934 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1935 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1936 error = cam_periph_runccb(ccb, saerror, 0,
1937 SF_NO_PRINT | SF_RETRY_SELTO | SF_RETRY_UA,
1938 &softc->device_stats);
1941 xpt_print_path(ccb->ccb_h.path);
1942 printf("unable to rewind after test read\n");
1943 xpt_release_ccb(ccb);
1949 * Next off, determine block limits.
1951 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1952 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1954 error = cam_periph_runccb(ccb, saerror, 0,
1955 SF_NO_PRINT | SF_RETRY_UA | SF_RETRY_SELTO,
1956 &softc->device_stats);
1958 xpt_release_ccb(ccb);
1962 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1963 * a MANDATORY command. Anyway- it doesn't matter-
1964 * we can proceed anyway.
1966 softc->blk_gran = 0;
1967 softc->max_blk = ~0;
1970 if (softc->scsi_rev >= SCSI_REV_3) {
1971 softc->blk_gran = RBL_GRAN(rblim);
1973 softc->blk_gran = 0;
1976 * We take max_blk == min_blk to mean a default to
1977 * fixed mode- but note that whatever we get out of
1978 * sagetparams below will actually determine whether
1979 * we are actually *in* fixed mode.
1981 softc->max_blk = scsi_3btoul(rblim->maximum);
1982 softc->min_blk = scsi_2btoul(rblim->minimum);
1987 * Next, perform a mode sense to determine
1988 * current density, blocksize, compression etc.
1990 error = sagetparams(periph, SA_PARAM_ALL,
1991 &softc->media_blksize,
1992 &softc->media_density,
1993 &softc->media_numblks,
1994 &softc->buffer_mode, &write_protect,
1995 &softc->speed, &comp_supported,
1996 &comp_enabled, &softc->comp_algorithm,
2001 * We could work a little harder here. We could
2002 * adjust our attempts to get information. It
2003 * might be an ancient tape drive. If someone
2004 * nudges us, we'll do that.
2010 * If no quirk has determined that this is a device that is
2011 * preferred to be in fixed or variable mode, now is the time
2014 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2017 * This could be expensive to find out. Luckily we
2018 * only need to do this once. If we start out in
2019 * 'default' mode, try and set ourselves to one
2020 * of the densities that would determine a wad
2021 * of other stuff. Go from highest to lowest.
2023 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2025 static u_int8_t ctry[] = {
2026 SCSI_DENSITY_HALFINCH_PE,
2027 SCSI_DENSITY_HALFINCH_6250C,
2028 SCSI_DENSITY_HALFINCH_6250,
2029 SCSI_DENSITY_HALFINCH_1600,
2030 SCSI_DENSITY_HALFINCH_800,
2031 SCSI_DENSITY_QIC_4GB,
2032 SCSI_DENSITY_QIC_2GB,
2033 SCSI_DENSITY_QIC_525_320,
2034 SCSI_DENSITY_QIC_150,
2035 SCSI_DENSITY_QIC_120,
2036 SCSI_DENSITY_QIC_24,
2037 SCSI_DENSITY_QIC_11_9TRK,
2038 SCSI_DENSITY_QIC_11_4TRK,
2039 SCSI_DENSITY_QIC_1320,
2040 SCSI_DENSITY_QIC_3080,
2043 for (i = 0; ctry[i]; i++) {
2044 error = sasetparams(periph,
2045 SA_PARAM_DENSITY, 0, ctry[i],
2048 softc->media_density = ctry[i];
2053 switch (softc->media_density) {
2054 case SCSI_DENSITY_QIC_11_4TRK:
2055 case SCSI_DENSITY_QIC_11_9TRK:
2056 case SCSI_DENSITY_QIC_24:
2057 case SCSI_DENSITY_QIC_120:
2058 case SCSI_DENSITY_QIC_150:
2059 case SCSI_DENSITY_QIC_525_320:
2060 case SCSI_DENSITY_QIC_1320:
2061 case SCSI_DENSITY_QIC_3080:
2062 softc->quirks &= ~SA_QUIRK_2FM;
2063 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2064 softc->last_media_blksize = 512;
2066 case SCSI_DENSITY_QIC_4GB:
2067 case SCSI_DENSITY_QIC_2GB:
2068 softc->quirks &= ~SA_QUIRK_2FM;
2069 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2070 softc->last_media_blksize = 1024;
2073 softc->last_media_blksize =
2074 softc->media_blksize;
2075 softc->quirks |= SA_QUIRK_VARIABLE;
2081 * If no quirk has determined that this is a device that needs
2082 * to have 2 Filemarks at EOD, now is the time to find out.
2085 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2086 switch (softc->media_density) {
2087 case SCSI_DENSITY_HALFINCH_800:
2088 case SCSI_DENSITY_HALFINCH_1600:
2089 case SCSI_DENSITY_HALFINCH_6250:
2090 case SCSI_DENSITY_HALFINCH_6250C:
2091 case SCSI_DENSITY_HALFINCH_PE:
2092 softc->quirks &= ~SA_QUIRK_1FM;
2093 softc->quirks |= SA_QUIRK_2FM;
2101 * Now validate that some info we got makes sense.
2103 if ((softc->max_blk < softc->media_blksize) ||
2104 (softc->min_blk > softc->media_blksize &&
2105 softc->media_blksize)) {
2106 xpt_print_path(ccb->ccb_h.path);
2107 printf("BLOCK LIMITS (%d..%d) could not match current "
2108 "block settings (%d)- adjusting\n", softc->min_blk,
2109 softc->max_blk, softc->media_blksize);
2110 softc->max_blk = softc->min_blk =
2111 softc->media_blksize;
2115 * Now put ourselves into the right frame of mind based
2120 * If we want to be in FIXED mode and our current blocksize
2121 * is not equal to our last blocksize (if nonzero), try and
2122 * set ourselves to this last blocksize (as the 'preferred'
2123 * block size). The initial quirkmatch at registry sets the
2124 * initial 'last' blocksize. If, for whatever reason, this
2125 * 'last' blocksize is zero, set the blocksize to 512,
2126 * or min_blk if that's larger.
2128 if ((softc->quirks & SA_QUIRK_FIXED) &&
2129 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2130 (softc->media_blksize != softc->last_media_blksize)) {
2131 softc->media_blksize = softc->last_media_blksize;
2132 if (softc->media_blksize == 0) {
2133 softc->media_blksize = 512;
2134 if (softc->media_blksize < softc->min_blk) {
2135 softc->media_blksize = softc->min_blk;
2138 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2139 softc->media_blksize, 0, 0, SF_NO_PRINT);
2141 xpt_print_path(ccb->ccb_h.path);
2142 printf("unable to set fixed blocksize to %d\n",
2143 softc->media_blksize);
2148 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2149 (softc->media_blksize != 0)) {
2150 softc->last_media_blksize = softc->media_blksize;
2151 softc->media_blksize = 0;
2152 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2153 0, 0, 0, SF_NO_PRINT);
2156 * If this fails and we were guessing, just
2157 * assume that we got it wrong and go try
2158 * fixed block mode. Don't even check against
2159 * density code at this point.
2162 softc->quirks &= ~SA_QUIRK_VARIABLE;
2163 softc->quirks |= SA_QUIRK_FIXED;
2164 if (softc->last_media_blksize == 0)
2165 softc->last_media_blksize = 512;
2168 xpt_print_path(ccb->ccb_h.path);
2169 printf("unable to set variable blocksize\n");
2175 * Now that we have the current block size,
2176 * set up some parameters for sastart's usage.
2178 if (softc->media_blksize) {
2179 softc->flags |= SA_FLAG_FIXED;
2180 if (powerof2(softc->media_blksize)) {
2182 ffs(softc->media_blksize) - 1;
2183 softc->blk_mask = softc->media_blksize - 1;
2185 softc->blk_mask = ~0;
2186 softc->blk_shift = 0;
2190 * The SCSI-3 spec allows 0 to mean "unspecified".
2191 * The SCSI-1 spec allows 0 to mean 'infinite'.
2193 * Either works here.
2195 if (softc->max_blk == 0) {
2196 softc->max_blk = ~0;
2198 softc->blk_shift = 0;
2199 if (softc->blk_gran != 0) {
2200 softc->blk_mask = softc->blk_gran - 1;
2202 softc->blk_mask = 0;
2207 softc->flags |= SA_FLAG_TAPE_WP;
2209 if (comp_supported) {
2210 if (softc->saved_comp_algorithm == 0)
2211 softc->saved_comp_algorithm =
2212 softc->comp_algorithm;
2213 softc->flags |= SA_FLAG_COMP_SUPP;
2215 softc->flags |= SA_FLAG_COMP_ENABLED;
2217 softc->flags |= SA_FLAG_COMP_UNSUPP;
2219 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2220 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2221 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2224 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2226 xpt_print_path(ccb->ccb_h.path);
2227 printf("unable to set buffered mode\n");
2229 error = 0; /* not an error */
2234 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2238 free(rblim, M_TEMP);
2241 softc->dsreg = MTIO_DSREG_NIL;
2243 softc->fileno = softc->blkno = 0;
2244 softc->dsreg = MTIO_DSREG_REST;
2246 #ifdef SA_1FM_AT_EOD
2247 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2248 softc->quirks |= SA_QUIRK_1FM;
2250 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2251 softc->quirks |= SA_QUIRK_2FM;
2254 xpt_release_ccb(ccb);
2257 * If we return an error, we're not mounted any more,
2258 * so release any device reservation.
2261 (void) sareservereleaseunit(periph, FALSE);
2264 * Clear I/O residual.
2266 softc->last_io_resid = 0;
2267 softc->last_ctl_resid = 0;
2273 * How many filemarks do we need to write if we were to terminate the
2274 * tape session right now? Note that this can be a negative number
2278 samarkswanted(struct cam_periph *periph)
2281 struct sa_softc *softc;
2283 softc = (struct sa_softc *)periph->softc;
2285 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2287 if (softc->quirks & SA_QUIRK_2FM)
2290 markswanted -= softc->filemarks;
2291 return (markswanted);
2295 sacheckeod(struct cam_periph *periph)
2299 struct sa_softc *softc;
2301 softc = (struct sa_softc *)periph->softc;
2302 markswanted = samarkswanted(periph);
2304 if (markswanted > 0) {
2305 error = sawritefilemarks(periph, markswanted, FALSE);
2313 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2315 static const char *toobig =
2316 "%d-byte tape record bigger than supplied buffer\n";
2317 struct cam_periph *periph;
2318 struct sa_softc *softc;
2319 struct ccb_scsiio *csio;
2320 struct scsi_sense_data *sense;
2321 u_int32_t resid = 0;
2324 int error_code, sense_key, asc, ascq, error, aqvalid;
2326 periph = xpt_path_periph(ccb->ccb_h.path);
2327 softc = (struct sa_softc *)periph->softc;
2329 sense = &csio->sense_data;
2330 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2331 aqvalid = sense->extra_len >= 6;
2334 status = csio->ccb_h.status & CAM_STATUS_MASK;
2337 * Calculate/latch up, any residuals... We do this in a funny 2-step
2338 * so we can print stuff here if we have CAM_DEBUG enabled for this
2341 if (status == CAM_SCSI_STATUS_ERROR) {
2342 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2343 info = (int32_t) scsi_4btoul(sense->info);
2345 if ((softc->flags & SA_FLAG_FIXED) != 0)
2346 resid *= softc->media_blksize;
2348 resid = csio->dxfer_len;
2350 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2351 if (softc->media_blksize)
2352 info /= softc->media_blksize;
2355 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2356 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2357 sizeof (struct scsi_sense_data));
2358 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2359 (int) csio->cdb_len);
2360 softc->last_io_resid = resid;
2361 softc->last_resid_was_io = 1;
2363 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2364 sizeof (struct scsi_sense_data));
2365 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2366 (int) csio->cdb_len);
2367 softc->last_ctl_resid = resid;
2368 softc->last_resid_was_io = 0;
2370 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2371 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %d "
2372 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2373 sense_key, asc, ascq, status,
2374 sense->flags & ~SSD_KEY_RESERVED, resid, csio->dxfer_len));
2376 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2377 ("Cam Status 0x%x\n", status));
2383 case CAM_SCSI_STATUS_ERROR:
2385 * If a read/write command, we handle it here.
2387 if (CCB_Type(csio) != SA_CCB_WAITING) {
2391 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2392 * on a non read/write command, we assume it's not an error
2393 * and propagate the residule and return.
2395 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2396 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2397 csio->resid = resid;
2402 * Otherwise, we let the common code handle this.
2404 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2408 * We cannot depend upon CAM honoring retry counts for these.
2410 case CAM_SCSI_BUS_RESET:
2412 if (ccb->ccb_h.retry_count <= 0) {
2418 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2422 * Handle filemark, end of tape, mismatched record sizes....
2423 * From this point out, we're only handling read/write cases.
2424 * Handle writes && reads differently.
2427 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2428 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2429 csio->resid = resid;
2431 } else if (sense->flags & SSD_EOM) {
2432 softc->flags |= SA_FLAG_EOM_PENDING;
2434 * Grotesque as it seems, the few times
2435 * I've actually seen a non-zero resid,
2436 * the tape drive actually lied and had
2437 * writtent all the data!.
2442 csio->resid = resid;
2443 if (sense_key == SSD_KEY_BLANK_CHECK) {
2444 if (softc->quirks & SA_QUIRK_1FM) {
2446 softc->flags |= SA_FLAG_EOM_PENDING;
2450 } else if (sense->flags & SSD_FILEMARK) {
2451 if (softc->flags & SA_FLAG_FIXED) {
2453 softc->flags |= SA_FLAG_EOF_PENDING;
2456 * Unconditionally, if we detected a filemark on a read,
2457 * mark that we've run moved a file ahead.
2459 if (softc->fileno != (daddr_t) -1) {
2462 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2468 * Incorrect Length usually applies to read, but can apply to writes.
2470 if (error == 0 && (sense->flags & SSD_ILI)) {
2472 xpt_print_path(csio->ccb_h.path);
2473 printf(toobig, csio->dxfer_len - info);
2474 csio->resid = csio->dxfer_len;
2477 csio->resid = resid;
2478 if (softc->flags & SA_FLAG_FIXED) {
2479 softc->flags |= SA_FLAG_EIO_PENDING;
2482 * Bump the block number if we hadn't seen a filemark.
2483 * Do this independent of errors (we've moved anyway).
2485 if ((sense->flags & SSD_FILEMARK) == 0) {
2486 if (softc->blkno != (daddr_t) -1) {
2488 csio->ccb_h.ccb_pflags |=
2489 SA_POSITION_UPDATED;
2497 * Unfreeze the queue if frozen as we're not returning anything
2498 * to our waiters that would indicate an I/O error has occurred
2508 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2509 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2510 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2511 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2516 struct scsi_mode_header_6 *mode_hdr;
2517 struct scsi_mode_blk_desc *mode_blk;
2518 int mode_buffer_len;
2519 struct sa_softc *softc;
2524 softc = (struct sa_softc *)periph->softc;
2525 ccb = cam_periph_getccb(periph, 1);
2526 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2527 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2529 cpage = SA_DATA_COMPRESSION_PAGE;
2532 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2534 if (params_to_get & SA_PARAM_COMPRESSION) {
2535 if (softc->quirks & SA_QUIRK_NOCOMP) {
2536 *comp_supported = FALSE;
2537 params_to_get &= ~SA_PARAM_COMPRESSION;
2539 mode_buffer_len += sizeof (sa_comp_t);
2542 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_INTWAIT | M_ZERO);
2543 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2544 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2546 /* it is safe to retry this */
2547 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2548 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2549 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2550 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2552 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2553 &softc->device_stats);
2556 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2558 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2560 * Hmm. Let's see if we can try another page...
2561 * If we've already done that, give up on compression
2562 * for this device and remember this for the future
2563 * and attempt the request without asking for compression
2566 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2567 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2570 softc->quirks |= SA_QUIRK_NOCOMP;
2571 free(mode_buffer, M_TEMP);
2573 } else if (status == CAM_SCSI_STATUS_ERROR) {
2574 /* Tell the user about the fatal error. */
2575 scsi_sense_print(&ccb->csio);
2576 goto sagetparamsexit;
2580 * If the user only wants the compression information, and
2581 * the device doesn't send back the block descriptor, it's
2582 * no big deal. If the user wants more than just
2583 * compression, though, and the device doesn't pass back the
2584 * block descriptor, we need to send another mode sense to
2585 * get the block descriptor.
2587 if ((mode_hdr->blk_desc_len == 0) &&
2588 (params_to_get & SA_PARAM_COMPRESSION) &&
2589 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2592 * Decrease the mode buffer length by the size of
2593 * the compression page, to make sure the data
2594 * there doesn't get overwritten.
2596 mode_buffer_len -= sizeof (sa_comp_t);
2599 * Now move the compression page that we presumably
2600 * got back down the memory chunk a little bit so
2601 * it doesn't get spammed.
2603 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2604 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2607 * Now, we issue another mode sense and just ask
2608 * for the block descriptor, etc.
2611 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2612 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2613 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2616 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2617 &softc->device_stats);
2621 goto sagetparamsexit;
2624 if (params_to_get & SA_PARAM_BLOCKSIZE)
2625 *blocksize = scsi_3btoul(mode_blk->blklen);
2627 if (params_to_get & SA_PARAM_NUMBLOCKS)
2628 *numblocks = scsi_3btoul(mode_blk->nblocks);
2630 if (params_to_get & SA_PARAM_BUFF_MODE)
2631 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2633 if (params_to_get & SA_PARAM_DENSITY)
2634 *density = mode_blk->density;
2636 if (params_to_get & SA_PARAM_WP)
2637 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2639 if (params_to_get & SA_PARAM_SPEED)
2640 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2642 if (params_to_get & SA_PARAM_COMPRESSION) {
2643 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2644 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2645 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2647 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2649 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2650 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2652 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2654 * We don't really know whether this device supports
2655 * Data Compression if the the algorithm field is
2656 * zero. Just say we do.
2658 *comp_supported = TRUE;
2660 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2661 *comp_algorithm = cp->sel_comp_alg;
2664 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2667 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2669 char *xyz = mode_buffer;
2670 xpt_print_path(periph->path);
2671 printf("Mode Sense Data=");
2672 for (idx = 0; idx < mode_buffer_len; idx++)
2673 printf(" 0x%02x", xyz[idx] & 0xff);
2679 xpt_release_ccb(ccb);
2680 free(mode_buffer, M_TEMP);
2685 * The purpose of this function is to set one of four different parameters
2689 * - compression / compression algorithm
2692 * The assumption is that this will be called from saioctl(), and therefore
2693 * from a process context. Thus the waiting malloc calls below. If that
2694 * assumption ever changes, the malloc calls should be changed to be
2697 * Any or all of the four parameters may be set when this function is
2698 * called. It should handle setting more than one parameter at once.
2701 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2702 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2703 u_int32_t sense_flags)
2705 struct sa_softc *softc;
2706 u_int32_t current_blocksize;
2707 u_int32_t current_calg;
2708 u_int8_t current_density;
2709 u_int8_t current_speed;
2710 int comp_enabled, comp_supported;
2712 int mode_buffer_len;
2713 struct scsi_mode_header_6 *mode_hdr;
2714 struct scsi_mode_blk_desc *mode_blk;
2715 sa_comp_t *ccomp, *cpage;
2717 union ccb *ccb = NULL;
2720 softc = (struct sa_softc *)periph->softc;
2722 ccomp = malloc(sizeof (sa_comp_t), M_TEMP, M_INTWAIT);
2725 * Since it doesn't make sense to set the number of blocks, or
2726 * write protection, we won't try to get the current value. We
2727 * always want to get the blocksize, so we can set it back to the
2730 error = sagetparams(periph,
2731 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2732 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2733 ¤t_speed, &comp_supported, &comp_enabled,
2734 ¤t_calg, ccomp);
2737 free(ccomp, M_TEMP);
2741 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2742 if (params_to_set & SA_PARAM_COMPRESSION)
2743 mode_buffer_len += sizeof (sa_comp_t);
2745 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_INTWAIT | M_ZERO);
2747 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2748 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2750 ccb = cam_periph_getccb(periph, 1);
2754 if (params_to_set & SA_PARAM_COMPRESSION) {
2756 cpage = (sa_comp_t *)&mode_blk[1];
2758 cpage = (sa_comp_t *)&mode_hdr[1];
2760 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2761 cpage->hdr.pagecode &= ~0x80;
2766 * If the caller wants us to set the blocksize, use the one they
2767 * pass in. Otherwise, use the blocksize we got back from the
2768 * mode select above.
2771 if (params_to_set & SA_PARAM_BLOCKSIZE)
2772 scsi_ulto3b(blocksize, mode_blk->blklen);
2774 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2777 * Set density if requested, else preserve old density.
2778 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2779 * devices, else density we've latched up in our softc.
2781 if (params_to_set & SA_PARAM_DENSITY) {
2782 mode_blk->density = density;
2783 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2784 mode_blk->density = SCSI_SAME_DENSITY;
2786 mode_blk->density = softc->media_density;
2791 * For mode selects, these two fields must be zero.
2793 mode_hdr->data_length = 0;
2794 mode_hdr->medium_type = 0;
2796 /* set the speed to the current value */
2797 mode_hdr->dev_spec = current_speed;
2799 /* set single-initiator buffering mode */
2800 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2803 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2805 mode_hdr->blk_desc_len = 0;
2808 * First, if the user wants us to set the compression algorithm or
2809 * just turn compression on, check to make sure that this drive
2810 * supports compression.
2812 if (params_to_set & SA_PARAM_COMPRESSION) {
2814 * If the compression algorithm is 0, disable compression.
2815 * If the compression algorithm is non-zero, enable
2816 * compression and set the compression type to the
2817 * specified compression algorithm, unless the algorithm is
2818 * MT_COMP_ENABLE. In that case, we look at the
2819 * compression algorithm that is currently set and if it is
2820 * non-zero, we leave it as-is. If it is zero, and we have
2821 * saved a compression algorithm from a time when
2822 * compression was enabled before, set the compression to
2825 switch (ccomp->hdr.pagecode & ~0x80) {
2826 case SA_DATA_COMPRESSION_PAGE:
2827 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2828 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2831 * Disable compression, but leave the
2832 * decompression and the capability bit
2835 dcp->dce_and_dcc = SA_DCP_DCC;
2836 dcp->dde_and_red |= SA_DCP_DDE;
2839 /* enable compression && decompression */
2840 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2841 dcp->dde_and_red |= SA_DCP_DDE;
2843 * If there, use compression algorithm from caller.
2844 * Otherwise, if there's a saved compression algorithm
2845 * and there is no current algorithm, use the saved
2846 * algorithm. Else parrot back what we got and hope
2849 if (calg != MT_COMP_ENABLE) {
2850 scsi_ulto4b(calg, dcp->comp_algorithm);
2851 scsi_ulto4b(calg, dcp->decomp_algorithm);
2852 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2853 softc->saved_comp_algorithm != 0) {
2854 scsi_ulto4b(softc->saved_comp_algorithm,
2855 dcp->comp_algorithm);
2856 scsi_ulto4b(softc->saved_comp_algorithm,
2857 dcp->decomp_algorithm);
2861 case SA_DEVICE_CONFIGURATION_PAGE:
2863 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2865 dcp->sel_comp_alg = SA_COMP_NONE;
2868 if (calg != MT_COMP_ENABLE) {
2869 dcp->sel_comp_alg = calg;
2870 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2871 softc->saved_comp_algorithm != 0) {
2872 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2878 * The drive doesn't seem to support compression,
2879 * so turn off the set compression bit.
2881 params_to_set &= ~SA_PARAM_COMPRESSION;
2882 xpt_print_path(periph->path);
2883 printf("device does not seem to support compression\n");
2886 * If that was the only thing the user wanted us to set,
2887 * clean up allocated resources and return with
2888 * 'operation not supported'.
2890 if (params_to_set == SA_PARAM_NONE) {
2891 free(mode_buffer, M_TEMP);
2892 xpt_release_ccb(ccb);
2897 * That wasn't the only thing the user wanted us to set.
2898 * So, decrease the stated mode buffer length by the
2899 * size of the compression mode page.
2901 mode_buffer_len -= sizeof(sa_comp_t);
2905 /* It is safe to retry this operation */
2906 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2907 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2908 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2910 error = cam_periph_runccb(ccb, saerror, 0,
2911 sense_flags, &softc->device_stats);
2914 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2916 char *xyz = mode_buffer;
2917 xpt_print_path(periph->path);
2918 printf("Err%d, Mode Select Data=", error);
2919 for (idx = 0; idx < mode_buffer_len; idx++)
2920 printf(" 0x%02x", xyz[idx] & 0xff);
2927 * If we can, try without setting density/blocksize.
2930 if ((params_to_set &
2931 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2936 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2937 cpage = (sa_comp_t *)&mode_blk[1];
2941 * If we were setting the blocksize, and that failed, we
2942 * want to set it to its original value. If we weren't
2943 * setting the blocksize, we don't want to change it.
2945 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2948 * Set density if requested, else preserve old density.
2949 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2950 * devices, else density we've latched up in our softc.
2952 if (params_to_set & SA_PARAM_DENSITY) {
2953 mode_blk->density = current_density;
2954 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2955 mode_blk->density = SCSI_SAME_DENSITY;
2957 mode_blk->density = softc->media_density;
2960 if (params_to_set & SA_PARAM_COMPRESSION)
2961 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2964 * The retry count is the only CCB field that might have been
2965 * changed that we care about, so reset it back to 1.
2967 ccb->ccb_h.retry_count = 1;
2968 cam_periph_runccb(ccb, saerror, 0, sense_flags,
2969 &softc->device_stats);
2973 xpt_release_ccb(ccb);
2976 free(ccomp, M_TEMP);
2978 if (params_to_set & SA_PARAM_COMPRESSION) {
2980 softc->flags &= ~SA_FLAG_COMP_ENABLED;
2982 * Even if we get an error setting compression,
2983 * do not say that we don't support it. We could
2984 * have been wrong, or it may be media specific.
2985 * softc->flags &= ~SA_FLAG_COMP_SUPP;
2987 softc->saved_comp_algorithm = softc->comp_algorithm;
2988 softc->comp_algorithm = 0;
2990 softc->flags |= SA_FLAG_COMP_ENABLED;
2991 softc->comp_algorithm = calg;
2995 free(mode_buffer, M_TEMP);
3000 saprevent(struct cam_periph *periph, int action)
3002 struct sa_softc *softc;
3006 softc = (struct sa_softc *)periph->softc;
3008 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3010 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3014 * We can be quiet about illegal requests.
3016 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3021 ccb = cam_periph_getccb(periph, 1);
3023 /* It is safe to retry this operation */
3024 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3025 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3027 error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats);
3030 if (action == PR_ALLOW)
3031 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3033 softc->flags |= SA_FLAG_TAPE_LOCKED;
3036 xpt_release_ccb(ccb);
3040 sarewind(struct cam_periph *periph)
3043 struct sa_softc *softc;
3046 softc = (struct sa_softc *)periph->softc;
3048 ccb = cam_periph_getccb(periph, 1);
3050 /* It is safe to retry this operation */
3051 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3052 SSD_FULL_SIZE, REWIND_TIMEOUT);
3054 softc->dsreg = MTIO_DSREG_REW;
3055 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3056 softc->dsreg = MTIO_DSREG_REST;
3058 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3059 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3061 xpt_release_ccb(ccb);
3063 softc->fileno = softc->blkno = (daddr_t) 0;
3065 softc->fileno = softc->blkno = (daddr_t) -1;
3070 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3073 struct sa_softc *softc;
3076 softc = (struct sa_softc *)periph->softc;
3078 ccb = cam_periph_getccb(periph, 1);
3080 /* This cannot be retried */
3082 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3083 SSD_FULL_SIZE, SPACE_TIMEOUT);
3086 * Clear residual because we will be using it.
3088 softc->last_ctl_resid = 0;
3090 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3091 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3092 softc->dsreg = MTIO_DSREG_REST;
3094 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3095 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3097 xpt_release_ccb(ccb);
3100 * If a spacing operation has failed, we need to invalidate
3103 * If the spacing operation was setmarks or to end of recorded data,
3104 * we no longer know our relative position.
3106 * If the spacing operations was spacing files in reverse, we
3107 * take account of the residual, but still check against less
3108 * than zero- if we've gone negative, we must have hit BOT.
3110 * If the spacing operations was spacing records in reverse and
3111 * we have a residual, we've either hit BOT or hit a filemark.
3112 * In the former case, we know our new record number (0). In
3113 * the latter case, we have absolutely no idea what the real
3114 * record number is- we've stopped between the end of the last
3115 * record in the previous file and the filemark that stopped
3116 * our spacing backwards.
3119 softc->fileno = softc->blkno = (daddr_t) -1;
3120 } else if (code == SS_SETMARKS || code == SS_EOD) {
3121 softc->fileno = softc->blkno = (daddr_t) -1;
3122 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3123 softc->fileno += (count - softc->last_ctl_resid);
3124 if (softc->fileno < 0) /* we must of hit BOT */
3127 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3128 softc->blkno += (count - softc->last_ctl_resid);
3130 if (softc->last_ctl_resid || softc->blkno < 0) {
3131 if (softc->fileno == 0) {
3134 softc->blkno = (daddr_t) -1;
3143 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3146 struct sa_softc *softc;
3149 softc = (struct sa_softc *)periph->softc;
3151 ccb = cam_periph_getccb(periph, 1);
3153 * Clear residual because we will be using it.
3155 softc->last_ctl_resid = 0;
3157 softc->dsreg = MTIO_DSREG_FMK;
3158 /* this *must* not be retried */
3159 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3160 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3161 softc->dsreg = MTIO_DSREG_REST;
3164 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3166 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3167 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3169 if (error == 0 && nmarks) {
3170 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3171 nwm = nmarks - softc->last_ctl_resid;
3172 softc->filemarks += nwm;
3175 xpt_release_ccb(ccb);
3178 * Update relative positions (if we're doing that).
3181 softc->fileno = softc->blkno = (daddr_t) -1;
3182 } else if (softc->fileno != (daddr_t) -1) {
3183 softc->fileno += nwm;
3190 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3192 struct scsi_tape_position_data loc;
3194 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3198 * We try and flush any buffered writes here if we were writing
3199 * and we're trying to get hardware block position. It eats
3200 * up performance substantially, but I'm wary of drive firmware.
3202 * I think that *logical* block position is probably okay-
3203 * but hardware block position might have to wait for data
3204 * to hit media to be valid. Caveat Emptor.
3207 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3208 error = sawritefilemarks(periph, 0, 0);
3209 if (error && error != EACCES)
3213 ccb = cam_periph_getccb(periph, 1);
3214 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3215 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3216 softc->dsreg = MTIO_DSREG_RBSY;
3217 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3218 softc->dsreg = MTIO_DSREG_REST;
3219 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3220 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3223 if (loc.flags & SA_RPOS_UNCERTAIN) {
3224 error = EINVAL; /* nothing is certain */
3226 *blkptr = scsi_4btoul(loc.firstblk);
3230 xpt_release_ccb(ccb);
3235 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3238 struct sa_softc *softc;
3242 * We used to try and flush any buffered writes here.
3243 * Now we push this onto user applications to either
3244 * flush the pending writes themselves (via a zero count
3245 * WRITE FILEMARKS command) or they can trust their tape
3246 * drive to do this correctly for them.
3249 softc = (struct sa_softc *)periph->softc;
3250 ccb = cam_periph_getccb(periph, 1);
3253 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3254 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3257 softc->dsreg = MTIO_DSREG_POS;
3258 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3259 softc->dsreg = MTIO_DSREG_REST;
3260 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3261 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3262 xpt_release_ccb(ccb);
3264 * Note relative file && block number position as now unknown.
3266 softc->fileno = softc->blkno = (daddr_t) -1;
3271 saretension(struct cam_periph *periph)
3274 struct sa_softc *softc;
3277 softc = (struct sa_softc *)periph->softc;
3279 ccb = cam_periph_getccb(periph, 1);
3281 /* It is safe to retry this operation */
3282 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3283 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3285 softc->dsreg = MTIO_DSREG_TEN;
3286 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3287 softc->dsreg = MTIO_DSREG_REST;
3289 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3290 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3291 xpt_release_ccb(ccb);
3293 softc->fileno = softc->blkno = (daddr_t) 0;
3295 softc->fileno = softc->blkno = (daddr_t) -1;
3300 sareservereleaseunit(struct cam_periph *periph, int reserve)
3303 struct sa_softc *softc;
3306 softc = (struct sa_softc *)periph->softc;
3307 ccb = cam_periph_getccb(periph, 1);
3309 /* It is safe to retry this operation */
3310 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3311 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3312 softc->dsreg = MTIO_DSREG_RBSY;
3313 error = cam_periph_runccb(ccb, saerror, 0,
3314 SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats);
3315 softc->dsreg = MTIO_DSREG_REST;
3317 xpt_release_ccb(ccb);
3320 * If the error was Illegal Request, then the device doesn't support
3321 * RESERVE/RELEASE. This is not an error.
3323 if (error == EINVAL) {
3331 saloadunload(struct cam_periph *periph, int load)
3334 struct sa_softc *softc;
3337 softc = (struct sa_softc *)periph->softc;
3339 ccb = cam_periph_getccb(periph, 1);
3341 /* It is safe to retry this operation */
3342 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3343 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3345 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3346 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3347 softc->dsreg = MTIO_DSREG_REST;
3349 xpt_release_ccb(ccb);
3351 if (error || load == 0)
3352 softc->fileno = softc->blkno = (daddr_t) -1;
3353 else if (error == 0)
3354 softc->fileno = softc->blkno = (daddr_t) 0;
3359 saerase(struct cam_periph *periph, int longerase)
3363 struct sa_softc *softc;
3366 softc = (struct sa_softc *)periph->softc;
3368 ccb = cam_periph_getccb(periph, 1);
3370 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3371 SSD_FULL_SIZE, ERASE_TIMEOUT);
3373 softc->dsreg = MTIO_DSREG_ZER;
3374 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3375 softc->dsreg = MTIO_DSREG_REST;
3377 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3378 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3379 xpt_release_ccb(ccb);
3383 #endif /* _KERNEL */
3386 * Read tape block limits command.
3389 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3390 void (*cbfcnp)(struct cam_periph *, union ccb *),
3391 u_int8_t tag_action,
3392 struct scsi_read_block_limits_data *rlimit_buf,
3393 u_int8_t sense_len, u_int32_t timeout)
3395 struct scsi_read_block_limits *scsi_cmd;
3397 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3398 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3399 sizeof(*scsi_cmd), timeout);
3401 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3402 bzero(scsi_cmd, sizeof(*scsi_cmd));
3403 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3407 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3408 void (*cbfcnp)(struct cam_periph *, union ccb *),
3409 u_int8_t tag_action, int readop, int sli,
3410 int fixed, u_int32_t length, u_int8_t *data_ptr,
3411 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3413 struct scsi_sa_rw *scsi_cmd;
3415 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3416 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3417 scsi_cmd->sli_fixed = 0;
3419 scsi_cmd->sli_fixed |= SAR_SLI;
3421 scsi_cmd->sli_fixed |= SARW_FIXED;
3422 scsi_ulto3b(length, scsi_cmd->length);
3423 scsi_cmd->control = 0;
3425 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3426 tag_action, data_ptr, dxfer_len, sense_len,
3427 sizeof(*scsi_cmd), timeout);
3431 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3432 void (*cbfcnp)(struct cam_periph *, union ccb *),
3433 u_int8_t tag_action, int immediate, int eot,
3434 int reten, int load, u_int8_t sense_len,
3437 struct scsi_load_unload *scsi_cmd;
3439 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3440 bzero(scsi_cmd, sizeof(*scsi_cmd));
3441 scsi_cmd->opcode = LOAD_UNLOAD;
3443 scsi_cmd->immediate = SLU_IMMED;
3445 scsi_cmd->eot_reten_load |= SLU_EOT;
3447 scsi_cmd->eot_reten_load |= SLU_RETEN;
3449 scsi_cmd->eot_reten_load |= SLU_LOAD;
3451 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3452 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3456 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3457 void (*cbfcnp)(struct cam_periph *, union ccb *),
3458 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3461 struct scsi_rewind *scsi_cmd;
3463 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3464 bzero(scsi_cmd, sizeof(*scsi_cmd));
3465 scsi_cmd->opcode = REWIND;
3467 scsi_cmd->immediate = SREW_IMMED;
3469 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3470 0, sense_len, sizeof(*scsi_cmd), timeout);
3474 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3475 void (*cbfcnp)(struct cam_periph *, union ccb *),
3476 u_int8_t tag_action, scsi_space_code code,
3477 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3479 struct scsi_space *scsi_cmd;
3481 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3482 scsi_cmd->opcode = SPACE;
3483 scsi_cmd->code = code;
3484 scsi_ulto3b(count, scsi_cmd->count);
3485 scsi_cmd->control = 0;
3487 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3488 0, sense_len, sizeof(*scsi_cmd), timeout);
3492 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3493 void (*cbfcnp)(struct cam_periph *, union ccb *),
3494 u_int8_t tag_action, int immediate, int setmark,
3495 u_int32_t num_marks, u_int8_t sense_len,
3498 struct scsi_write_filemarks *scsi_cmd;
3500 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3501 bzero(scsi_cmd, sizeof(*scsi_cmd));
3502 scsi_cmd->opcode = WRITE_FILEMARKS;
3504 scsi_cmd->byte2 |= SWFMRK_IMMED;
3506 scsi_cmd->byte2 |= SWFMRK_WSMK;
3508 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3510 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3511 0, sense_len, sizeof(*scsi_cmd), timeout);
3515 * The reserve and release unit commands differ only by their opcodes.
3518 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3519 void (*cbfcnp)(struct cam_periph *, union ccb *),
3520 u_int8_t tag_action, int third_party,
3521 int third_party_id, u_int8_t sense_len,
3522 u_int32_t timeout, int reserve)
3524 struct scsi_reserve_release_unit *scsi_cmd;
3526 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3527 bzero(scsi_cmd, sizeof(*scsi_cmd));
3530 scsi_cmd->opcode = RESERVE_UNIT;
3532 scsi_cmd->opcode = RELEASE_UNIT;
3535 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3536 scsi_cmd->lun_thirdparty |=
3537 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3540 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3541 0, sense_len, sizeof(*scsi_cmd), timeout);
3545 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3546 void (*cbfcnp)(struct cam_periph *, union ccb *),
3547 u_int8_t tag_action, int immediate, int long_erase,
3548 u_int8_t sense_len, u_int32_t timeout)
3550 struct scsi_erase *scsi_cmd;
3552 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3553 bzero(scsi_cmd, sizeof(*scsi_cmd));
3555 scsi_cmd->opcode = ERASE;
3558 scsi_cmd->lun_imm_long |= SE_IMMED;
3561 scsi_cmd->lun_imm_long |= SE_LONG;
3563 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3564 0, sense_len, sizeof(*scsi_cmd), timeout);
3568 * Read Tape Position command.
3571 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3572 void (*cbfcnp)(struct cam_periph *, union ccb *),
3573 u_int8_t tag_action, int hardsoft,
3574 struct scsi_tape_position_data *sbp,
3575 u_int8_t sense_len, u_int32_t timeout)
3577 struct scsi_tape_read_position *scmd;
3579 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3580 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3581 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3582 bzero(scmd, sizeof(*scmd));
3583 scmd->opcode = READ_POSITION;
3584 scmd->byte1 = hardsoft;
3588 * Set Tape Position command.
3591 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3592 void (*cbfcnp)(struct cam_periph *, union ccb *),
3593 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3594 u_int8_t sense_len, u_int32_t timeout)
3596 struct scsi_tape_locate *scmd;
3598 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3599 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3600 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3601 bzero(scmd, sizeof(*scmd));
3602 scmd->opcode = LOCATE;
3604 scmd->byte1 |= SA_SPOS_BT;
3605 scsi_ulto4b(blkno, scmd->blkaddr);