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.2 2003/06/17 04:28:19 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>
41 #include <sys/malloc.h>
45 #include <sys/devicestat.h>
46 #include <machine/limits.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_extend.h>
56 #include <cam/cam_periph.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_debug.h>
60 #include <cam/scsi/scsi_all.h>
61 #include <cam/scsi/scsi_message.h>
62 #include <cam/scsi/scsi_sa.h>
69 #define SA_IO_TIMEOUT 4
71 #ifndef SA_SPACE_TIMEOUT
72 #define SA_SPACE_TIMEOUT 1 * 60
74 #ifndef SA_REWIND_TIMEOUT
75 #define SA_REWIND_TIMEOUT 2 * 60
77 #ifndef SA_ERASE_TIMEOUT
78 #define SA_ERASE_TIMEOUT 4 * 60
81 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
83 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
84 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
85 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
86 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
89 * Additional options that can be set for config: SA_1FM_AT_EOT
92 #ifndef UNUSED_PARAMETER
93 #define UNUSED_PARAMETER(x) x = x
97 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
98 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
106 SA_STATE_NORMAL, SA_STATE_ABNORMAL
109 #define ccb_pflags ppriv_field0
110 #define ccb_bp ppriv_ptr1
112 #define SA_CCB_BUFFER_IO 0x0
113 #define SA_CCB_WAITING 0x1
114 #define SA_CCB_TYPEMASK 0x1
115 #define SA_POSITION_UPDATED 0x2
117 #define Set_CCB_Type(x, type) \
118 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
119 x->ccb_h.ccb_pflags |= type
121 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
126 SA_FLAG_OPEN = 0x0001,
127 SA_FLAG_FIXED = 0x0002,
128 SA_FLAG_TAPE_LOCKED = 0x0004,
129 SA_FLAG_TAPE_MOUNTED = 0x0008,
130 SA_FLAG_TAPE_WP = 0x0010,
131 SA_FLAG_TAPE_WRITTEN = 0x0020,
132 SA_FLAG_EOM_PENDING = 0x0040,
133 SA_FLAG_EIO_PENDING = 0x0080,
134 SA_FLAG_EOF_PENDING = 0x0100,
135 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
136 SA_FLAG_EOF_PENDING),
137 SA_FLAG_INVALID = 0x0200,
138 SA_FLAG_COMP_ENABLED = 0x0400,
139 SA_FLAG_COMP_SUPP = 0x0800,
140 SA_FLAG_COMP_UNSUPP = 0x1000,
141 SA_FLAG_TAPE_FROZEN = 0x2000
145 SA_MODE_REWIND = 0x00,
146 SA_MODE_NOREWIND = 0x01,
147 SA_MODE_OFFLINE = 0x02
151 SA_PARAM_NONE = 0x00,
152 SA_PARAM_BLOCKSIZE = 0x01,
153 SA_PARAM_DENSITY = 0x02,
154 SA_PARAM_COMPRESSION = 0x04,
155 SA_PARAM_BUFF_MODE = 0x08,
156 SA_PARAM_NUMBLOCKS = 0x10,
158 SA_PARAM_SPEED = 0x40,
163 SA_QUIRK_NONE = 0x00,
164 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
165 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
166 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
167 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
168 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
169 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
170 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
171 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
174 /* units are bits 4-7, 16-21 (1024 units) */
175 #define SAUNIT(DEV) \
176 (((minor(DEV) & 0xF0) >> 4) | ((minor(DEV) & 0x3f0000) >> 16))
178 #define SAMODE(z) ((minor(z) & 0x3))
179 #define SADENSITY(z) (((minor(z) >> 2) & 0x3))
180 #define SA_IS_CTRL(z) (minor(z) & (1 << 29))
182 #define SA_NOT_CTLDEV 0
186 #define SA_ATYPE_NR 1
187 #define SA_ATYPE_ER 2
189 #define SAMINOR(ctl, unit, mode, access) \
190 ((ctl << 29) | ((unit & 0x3f0) << 16) | ((unit & 0xf) << 4) | \
191 (mode << 0x2) | (access & 0x3))
193 #define SA_NUM_MODES 4
196 struct sa_mode_devs {
200 } mode_devs[SA_NUM_MODES];
210 struct buf_queue_head buf_queue;
212 struct devstat device_stats;
219 u_int32_t comp_algorithm;
220 u_int32_t saved_comp_algorithm;
221 u_int32_t media_blksize;
222 u_int32_t last_media_blksize;
223 u_int32_t media_numblks;
224 u_int8_t media_density;
227 u_int8_t dsreg; /* mtio mt_dsreg, redux */
231 int last_resid_was_io;
234 * Relative to BOT Location.
243 struct scsi_sense_data _last_io_sense;
244 u_int32_t _last_io_resid;
245 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
246 struct scsi_sense_data _last_ctl_sense;
247 u_int32_t _last_ctl_resid;
248 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
249 #define last_io_sense errinfo._last_io_sense
250 #define last_io_resid errinfo._last_io_resid
251 #define last_io_cdb errinfo._last_io_cdb
252 #define last_ctl_sense errinfo._last_ctl_sense
253 #define last_ctl_resid errinfo._last_ctl_resid
254 #define last_ctl_cdb errinfo._last_ctl_cdb
257 * Misc other flags/state
261 ctrl_mode : 1; /* control device open */
264 struct sa_quirk_entry {
265 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
266 sa_quirks quirks; /* specific quirk type */
267 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
270 static struct sa_quirk_entry sa_quirk_table[] =
273 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
274 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
275 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
278 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
279 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
282 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
283 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
286 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
287 "Python*", "*"}, SA_QUIRK_NODREAD, 0
290 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
291 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
294 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
295 "VIPER 2525 25462", "-011"},
296 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
299 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
300 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
304 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
305 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
309 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
310 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
313 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
314 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
317 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
318 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
321 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
322 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
325 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
326 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
329 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
330 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
332 { /* jreynold@primenet.com */
333 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
334 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
336 { /* mike@sentex.net */
337 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
338 "STT20000*", "*"}, SA_QUIRK_1FM, 0
341 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
342 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
345 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
346 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
349 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
350 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
353 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
354 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
357 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
358 " SLR*", "*"}, SA_QUIRK_1FM, 0
361 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
362 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
365 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
366 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
370 static d_open_t saopen;
371 static d_close_t saclose;
372 static d_strategy_t sastrategy;
373 static d_ioctl_t saioctl;
374 static periph_init_t sainit;
375 static periph_ctor_t saregister;
376 static periph_oninv_t saoninvalidate;
377 static periph_dtor_t sacleanup;
378 static periph_start_t sastart;
379 static void saasync(void *callback_arg, u_int32_t code,
380 struct cam_path *path, void *arg);
381 static void sadone(struct cam_periph *periph,
382 union ccb *start_ccb);
383 static int saerror(union ccb *ccb, u_int32_t cam_flags,
384 u_int32_t sense_flags);
385 static int samarkswanted(struct cam_periph *);
386 static int sacheckeod(struct cam_periph *periph);
387 static int sagetparams(struct cam_periph *periph,
388 sa_params params_to_get,
389 u_int32_t *blocksize, u_int8_t *density,
390 u_int32_t *numblocks, int *buff_mode,
391 u_int8_t *write_protect, u_int8_t *speed,
392 int *comp_supported, int *comp_enabled,
393 u_int32_t *comp_algorithm,
394 sa_comp_t *comp_page);
395 static int sasetparams(struct cam_periph *periph,
396 sa_params params_to_set,
397 u_int32_t blocksize, u_int8_t density,
398 u_int32_t comp_algorithm,
399 u_int32_t sense_flags);
400 static void saprevent(struct cam_periph *periph, int action);
401 static int sarewind(struct cam_periph *periph);
402 static int saspace(struct cam_periph *periph, int count,
403 scsi_space_code code);
404 static int samount(struct cam_periph *, int, dev_t);
405 static int saretension(struct cam_periph *periph);
406 static int sareservereleaseunit(struct cam_periph *periph,
408 static int saloadunload(struct cam_periph *periph, int load);
409 static int saerase(struct cam_periph *periph, int longerase);
410 static int sawritefilemarks(struct cam_periph *periph,
411 int nmarks, int setmarks);
412 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
413 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
416 static struct periph_driver sadriver =
419 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
422 DATA_SET(periphdriver_set, sadriver);
424 /* For 2.2-stable support */
429 #define SA_CDEV_MAJOR 14
431 static struct cdevsw sa_cdevsw = {
435 /* write */ physwrite,
439 /* strategy */ sastrategy,
441 /* maj */ SA_CDEV_MAJOR,
448 static struct extend_array *saperiphs;
451 saopen(dev_t dev, int flags, int fmt, struct proc *p)
453 struct cam_periph *periph;
454 struct sa_softc *softc;
463 density = SADENSITY(dev);
466 periph = cam_extend_get(saperiphs, unit);
467 if (periph == NULL) {
471 softc = (struct sa_softc *)periph->softc;
472 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0) {
478 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
479 ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
481 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
482 cam_periph_unlock(periph);
485 if (SA_IS_CTRL(dev)) {
486 softc->ctrl_mode = 1;
487 cam_periph_unlock(periph);
492 if (softc->flags & SA_FLAG_OPEN) {
494 } else if (softc->flags & SA_FLAG_INVALID) {
498 * The function samount ensures media is loaded and ready.
499 * It also does a device RESERVE if the tape isn't yet mounted.
501 error = samount(periph, flags, dev);
505 cam_periph_release(periph);
507 saprevent(periph, PR_PREVENT);
508 softc->flags |= SA_FLAG_OPEN;
510 cam_periph_unlock(periph);
515 saclose(dev_t dev, int flag, int fmt, struct proc *p)
517 struct cam_periph *periph;
518 struct sa_softc *softc;
519 int unit, mode, error, writing, tmp;
520 int closedbits = SA_FLAG_OPEN;
524 periph = cam_extend_get(saperiphs, unit);
528 softc = (struct sa_softc *)periph->softc;
530 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
531 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
534 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
538 if (SA_IS_CTRL(dev)) {
539 softc->ctrl_mode = 0;
540 cam_periph_release(periph);
541 cam_periph_unlock(periph);
546 * Were we writing the tape?
548 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
551 * See whether or not we need to write filemarks. If this
552 * fails, we probably have to assume we've lost tape
555 error = sacheckeod(periph);
557 xpt_print_path(periph->path);
558 printf("failed to write terminating filemark(s)\n");
559 softc->flags |= SA_FLAG_TAPE_FROZEN;
563 * Whatever we end up doing, allow users to eject tapes from here on.
565 saprevent(periph, PR_ALLOW);
568 * Decide how to end...
570 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
571 closedbits |= SA_FLAG_TAPE_FROZEN;
572 } else switch (mode) {
573 case SA_MODE_OFFLINE:
575 * An 'offline' close is an unconditional release of
576 * frozen && mount conditions, irrespective of whether
577 * these operations succeeded. The reason for this is
578 * to allow at least some kind of programmatic way
579 * around our state getting all fouled up. If somebody
580 * issues an 'offline' command, that will be allowed
583 (void) sarewind(periph);
584 (void) saloadunload(periph, FALSE);
585 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
589 * If the rewind fails, return an error- if anyone cares,
590 * but not overwriting any previous error.
592 * We don't clear the notion of mounted here, but we do
593 * clear the notion of frozen if we successfully rewound.
595 tmp = sarewind(periph);
600 closedbits |= SA_FLAG_TAPE_FROZEN;
603 case SA_MODE_NOREWIND:
605 * If we're not rewinding/unloading the tape, find out
606 * whether we need to back up over one of two filemarks
607 * we wrote (if we wrote two filemarks) so that appends
608 * from this point on will be sane.
610 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
611 tmp = saspace(periph, -1, SS_FILEMARKS);
613 xpt_print_path(periph->path);
614 printf("unable to backspace over one of double"
615 " filemarks at end of tape\n");
616 xpt_print_path(periph->path);
617 printf("it is possible that this device"
618 " needs a SA_QUIRK_1FM quirk set for it\n");
619 softc->flags |= SA_FLAG_TAPE_FROZEN;
624 xpt_print_path(periph->path);
625 panic("unknown mode 0x%x in saclose\n", mode);
631 * We wish to note here that there are no more filemarks to be written.
633 softc->filemarks = 0;
634 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
637 * And we are no longer open for business.
639 softc->flags &= ~closedbits;
642 * Inform users if tape state if frozen....
644 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
645 xpt_print_path(periph->path);
646 printf("tape is now frozen- use an OFFLINE, REWIND or MTEOM "
647 "command to clear this state.\n");
650 /* release the device if it is no longer mounted */
651 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
652 sareservereleaseunit(periph, FALSE);
654 cam_periph_unlock(periph);
655 cam_periph_release(periph);
661 * Actually translate the requested transfer into one the physical driver
662 * can understand. The transfer is described by a buf and will include
663 * only one physical transfer.
666 sastrategy(struct buf *bp)
668 struct cam_periph *periph;
669 struct sa_softc *softc;
673 if (SA_IS_CTRL(bp->b_dev)) {
674 bp->b_error = EINVAL;
677 unit = SAUNIT(bp->b_dev);
678 periph = cam_extend_get(saperiphs, unit);
679 if (periph == NULL) {
683 softc = (struct sa_softc *)periph->softc;
687 if (softc->flags & SA_FLAG_INVALID) {
693 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
702 * If it's a null transfer, return immediatly
704 if (bp->b_bcount == 0)
708 if (softc->flags & SA_FLAG_FIXED) {
710 * Fixed block device. The byte count must
711 * be a multiple of our block size.
713 if (((softc->blk_mask != ~0) &&
714 ((bp->b_bcount & softc->blk_mask) != 0)) ||
715 ((softc->blk_mask == ~0) &&
716 ((bp->b_bcount % softc->min_blk) != 0))) {
717 xpt_print_path(periph->path);
718 printf("Invalid request. Fixed block device "
719 "requests must be a multiple "
720 "of %d bytes\n", softc->media_blksize);
721 bp->b_error = EINVAL;
724 } else if ((bp->b_bcount > softc->max_blk) ||
725 (bp->b_bcount < softc->min_blk) ||
726 (bp->b_bcount & softc->blk_mask) != 0) {
728 xpt_print_path(periph->path);
729 printf("Invalid request. Variable block device "
730 "requests must be ");
731 if (softc->blk_mask != 0) {
732 printf("a multiple of %d ", (0x1 << softc->blk_gran));
734 printf("between %d and %d bytes\n", softc->min_blk,
736 bp->b_error = EINVAL;
741 * Mask interrupts so that the device cannot be invalidated until
742 * after we are in the queue. Otherwise, we might not properly
743 * clean up one of the buffers.
748 * Place it at the end of the queue.
750 bufq_insert_tail(&softc->buf_queue, bp);
752 softc->queue_count++;
753 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("sastrategy: enqueuing a %d "
754 "%s byte %s queue count now %d\n", (int) bp->b_bcount,
755 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
756 (bp->b_flags & B_READ)? "read" : "write", softc->queue_count));
761 * Schedule ourselves for performing the work.
763 xpt_schedule(periph, 1);
767 bp->b_flags |= B_ERROR;
771 * Correctly set the buf to indicate a completed xfer
773 bp->b_resid = bp->b_bcount;
778 saioctl(dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
780 struct cam_periph *periph;
781 struct sa_softc *softc;
782 scsi_space_code spaceop;
783 int didlockperiph = 0;
792 density = SADENSITY(dev);
793 error = 0; /* shut up gcc */
794 spaceop = 0; /* shut up gcc */
796 periph = cam_extend_get(saperiphs, unit);
800 softc = (struct sa_softc *)periph->softc;
803 * Check for control mode accesses. We allow MTIOCGET and
804 * MTIOCERRSTAT (but need to be the only one open in order
805 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
806 * and MTCOMP (but need to be the only one accessing this
807 * device to run those).
810 if (SA_IS_CTRL(dev)) {
812 case MTIOCGETEOTMODEL:
817 * If the periph isn't already locked, lock it
818 * so our MTIOCERRSTAT can reset latched error stats.
820 * If the periph is already locked, skip it because
821 * we're just getting status and it'll be up to the
822 * other thread that has this device open to do
823 * an MTIOCERRSTAT that would clear latched status.
826 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
827 error = cam_periph_lock(periph, PRIBIO|PCATCH);
836 case MTIOCSETEOTMODEL:
841 * We need to acquire the peripheral here rather
842 * than at open time because we are sharing writable
843 * access to data structures.
846 error = cam_periph_lock(periph, PRIBIO|PCATCH);
860 * Find the device that the user is talking about
865 struct mtget *g = (struct mtget *)arg;
868 * If this isn't the control mode device, actually go out
869 * and ask the drive again what it's set to.
871 if (!SA_IS_CTRL(dev)) {
872 u_int8_t write_protect;
873 int comp_enabled, comp_supported;
874 error = sagetparams(periph, SA_PARAM_ALL,
875 &softc->media_blksize, &softc->media_density,
876 &softc->media_numblks, &softc->buffer_mode,
877 &write_protect, &softc->speed, &comp_supported,
878 &comp_enabled, &softc->comp_algorithm, NULL);
882 softc->flags |= SA_FLAG_TAPE_WP;
884 softc->flags &= ~SA_FLAG_TAPE_WP;
885 softc->flags &= ~(SA_FLAG_COMP_SUPP|
886 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
887 if (comp_supported) {
888 if (softc->saved_comp_algorithm == 0)
889 softc->saved_comp_algorithm =
890 softc->comp_algorithm;
891 softc->flags |= SA_FLAG_COMP_SUPP;
893 softc->flags |= SA_FLAG_COMP_ENABLED;
895 softc->flags |= SA_FLAG_COMP_UNSUPP;
897 bzero(g, sizeof(struct mtget));
898 g->mt_type = MT_ISAR;
899 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
900 g->mt_comp = MT_COMP_UNSUPP;
901 g->mt_comp0 = MT_COMP_UNSUPP;
902 g->mt_comp1 = MT_COMP_UNSUPP;
903 g->mt_comp2 = MT_COMP_UNSUPP;
904 g->mt_comp3 = MT_COMP_UNSUPP;
906 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
907 g->mt_comp = MT_COMP_DISABLED;
909 g->mt_comp = softc->comp_algorithm;
911 g->mt_comp0 = softc->comp_algorithm;
912 g->mt_comp1 = softc->comp_algorithm;
913 g->mt_comp2 = softc->comp_algorithm;
914 g->mt_comp3 = softc->comp_algorithm;
916 g->mt_density = softc->media_density;
917 g->mt_density0 = softc->media_density;
918 g->mt_density1 = softc->media_density;
919 g->mt_density2 = softc->media_density;
920 g->mt_density3 = softc->media_density;
921 g->mt_blksiz = softc->media_blksize;
922 g->mt_blksiz0 = softc->media_blksize;
923 g->mt_blksiz1 = softc->media_blksize;
924 g->mt_blksiz2 = softc->media_blksize;
925 g->mt_blksiz3 = softc->media_blksize;
926 g->mt_fileno = softc->fileno;
927 g->mt_blkno = softc->blkno;
928 g->mt_dsreg = (short) softc->dsreg;
930 * Yes, we know that this is likely to overflow
932 if (softc->last_resid_was_io) {
933 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
934 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
935 softc->last_io_resid = 0;
939 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
940 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
941 softc->last_ctl_resid = 0;
950 struct scsi_tape_errors *sep =
951 &((union mterrstat *)arg)->scsi_errstat;
953 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
954 ("saioctl: MTIOCERRSTAT\n"));
956 bzero(sep, sizeof(*sep));
957 sep->io_resid = softc->last_io_resid;
958 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
959 sizeof (sep->io_sense));
960 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
961 sizeof (sep->io_cdb));
962 sep->ctl_resid = softc->last_ctl_resid;
963 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
964 sizeof (sep->ctl_sense));
965 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
966 sizeof (sep->ctl_cdb));
968 if (SA_IS_CTRL(dev) == 0 || didlockperiph)
969 bzero((caddr_t) &softc->errinfo,
970 sizeof (softc->errinfo));
979 mt = (struct mtop *)arg;
981 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
982 ("saioctl: op=0x%x count=0x%x\n",
983 mt->mt_op, mt->mt_count));
985 count = mt->mt_count;
987 case MTWEOF: /* write an end-of-file marker */
989 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
990 * flag because by keeping track of filemarks
991 * we have last written we know ehether or not
992 * we need to write more when we close the device.
994 error = sawritefilemarks(periph, count, FALSE);
996 case MTWSS: /* write a setmark */
997 error = sawritefilemarks(periph, count, TRUE);
999 case MTBSR: /* backward space record */
1000 case MTFSR: /* forward space record */
1001 case MTBSF: /* backward space file */
1002 case MTFSF: /* forward space file */
1003 case MTBSS: /* backward space setmark */
1004 case MTFSS: /* forward space setmark */
1005 case MTEOD: /* space to end of recorded medium */
1009 spaceop = SS_FILEMARKS;
1010 nmarks = softc->filemarks;
1011 error = sacheckeod(periph);
1013 xpt_print_path(periph->path);
1014 printf("EOD check prior to spacing failed\n");
1015 softc->flags |= SA_FLAG_EIO_PENDING;
1018 nmarks -= softc->filemarks;
1024 spaceop = SS_BLOCKS;
1035 spaceop = SS_SETMARKS;
1049 nmarks = softc->filemarks;
1051 * XXX: Why are we checking again?
1053 error = sacheckeod(periph);
1056 nmarks -= softc->filemarks;
1057 error = saspace(periph, count - nmarks, spaceop);
1059 * At this point, clear that we've written the tape
1060 * and that we've written any filemarks. We really
1061 * don't know what the applications wishes to do next-
1062 * the sacheckeod's will make sure we terminated the
1063 * tape correctly if we'd been writing, but the next
1064 * action the user application takes will set again
1065 * whether we need to write filemarks.
1068 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1069 softc->filemarks = 0;
1072 case MTREW: /* rewind */
1073 (void) sacheckeod(periph);
1074 error = sarewind(periph);
1077 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1078 softc->flags &= ~SA_FLAG_ERR_PENDING;
1079 softc->filemarks = 0;
1081 case MTERASE: /* erase */
1082 error = saerase(periph, count);
1084 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1085 softc->flags &= ~SA_FLAG_ERR_PENDING;
1087 case MTRETENS: /* re-tension tape */
1088 error = saretension(periph);
1090 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1091 softc->flags &= ~SA_FLAG_ERR_PENDING;
1093 case MTOFFL: /* rewind and put the drive offline */
1095 (void) sacheckeod(periph);
1097 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1098 softc->filemarks = 0;
1100 error = sarewind(periph);
1101 /* clear the frozen flag anyway */
1102 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1105 * Be sure to allow media removal before ejecting.
1108 saprevent(periph, PR_ALLOW);
1110 error = saloadunload(periph, FALSE);
1112 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1117 case MTNOP: /* no operation, sets status only */
1118 case MTCACHE: /* enable controller cache */
1119 case MTNOCACHE: /* disable controller cache */
1123 case MTSETBSIZ: /* Set block size for device */
1125 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1128 softc->last_media_blksize =
1129 softc->media_blksize;
1130 softc->media_blksize = count;
1132 softc->flags |= SA_FLAG_FIXED;
1133 if (powerof2(count)) {
1136 softc->blk_mask = count - 1;
1138 softc->blk_mask = ~0;
1139 softc->blk_shift = 0;
1142 * Make the user's desire 'persistent'.
1144 softc->quirks &= ~SA_QUIRK_VARIABLE;
1145 softc->quirks |= SA_QUIRK_FIXED;
1147 softc->flags &= ~SA_FLAG_FIXED;
1148 if (softc->max_blk == 0) {
1149 softc->max_blk = ~0;
1151 softc->blk_shift = 0;
1152 if (softc->blk_gran != 0) {
1154 softc->blk_gran - 1;
1156 softc->blk_mask = 0;
1159 * Make the user's desire 'persistent'.
1161 softc->quirks |= SA_QUIRK_VARIABLE;
1162 softc->quirks &= ~SA_QUIRK_FIXED;
1166 case MTSETDNSTY: /* Set density for device and mode */
1167 if (count > UCHAR_MAX) {
1171 error = sasetparams(periph, SA_PARAM_DENSITY,
1175 case MTCOMP: /* enable compression */
1177 * Some devices don't support compression, and
1178 * don't like it if you ask them for the
1181 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1182 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1186 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1187 0, 0, count, SF_NO_PRINT);
1199 error = sardpos(periph, 0, (u_int32_t *) arg);
1202 error = sardpos(periph, 1, (u_int32_t *) arg);
1205 error = sasetpos(periph, 0, (u_int32_t *) arg);
1208 error = sasetpos(periph, 1, (u_int32_t *) arg);
1210 case MTIOCGETEOTMODEL:
1212 if (softc->quirks & SA_QUIRK_1FM)
1216 *((u_int32_t *) arg) = mode;
1218 case MTIOCSETEOTMODEL:
1220 switch (*((u_int32_t *) arg)) {
1222 softc->quirks &= ~SA_QUIRK_2FM;
1223 softc->quirks |= SA_QUIRK_1FM;
1226 softc->quirks &= ~SA_QUIRK_1FM;
1227 softc->quirks |= SA_QUIRK_2FM;
1235 error = cam_periph_ioctl(periph, cmd, arg, saerror);
1240 * Check to see if we cleared a frozen state
1242 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1248 softc->fileno = (daddr_t) -1;
1249 softc->blkno = (daddr_t) -1;
1250 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1251 xpt_print_path(periph->path);
1252 printf("tape state now unfrozen.\n");
1258 if (didlockperiph) {
1259 cam_periph_unlock(periph);
1268 struct cam_path *path;
1271 * Create our extend array for storing the devices we attach to.
1273 saperiphs = cam_extend_new();
1274 if (saperiphs == NULL) {
1275 printf("sa: Failed to alloc extend array!\n");
1280 * Install a global async callback.
1282 status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1283 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1285 if (status == CAM_REQ_CMP) {
1286 /* Register the async callbacks of interrest */
1287 struct ccb_setasync csa; /*
1288 * This is an immediate CCB,
1289 * so using the stack is OK
1291 xpt_setup_ccb(&csa.ccb_h, path, 5);
1292 csa.ccb_h.func_code = XPT_SASYNC_CB;
1293 csa.event_enable = AC_FOUND_DEVICE;
1294 csa.callback = saasync;
1295 csa.callback_arg = NULL;
1296 xpt_action((union ccb *)&csa);
1297 status = csa.ccb_h.status;
1298 xpt_free_path(path);
1301 if (status != CAM_REQ_CMP) {
1302 printf("sa: Failed to attach master async callback "
1303 "due to status 0x%x!\n", status);
1308 saoninvalidate(struct cam_periph *periph)
1310 struct sa_softc *softc;
1312 struct ccb_setasync csa;
1315 softc = (struct sa_softc *)periph->softc;
1318 * De-register any async callbacks.
1320 xpt_setup_ccb(&csa.ccb_h, periph->path,
1322 csa.ccb_h.func_code = XPT_SASYNC_CB;
1323 csa.event_enable = 0;
1324 csa.callback = saasync;
1325 csa.callback_arg = periph;
1326 xpt_action((union ccb *)&csa);
1328 softc->flags |= SA_FLAG_INVALID;
1331 * Although the oninvalidate() routines are always called at
1332 * splsoftcam, we need to be at splbio() here to keep the buffer
1333 * queue from being modified while we traverse it.
1338 * Return all queued I/O with ENXIO.
1339 * XXX Handle any transactions queued to the card
1340 * with XPT_ABORT_CCB.
1342 while ((q_bp = bufq_first(&softc->buf_queue)) != NULL){
1343 bufq_remove(&softc->buf_queue, q_bp);
1344 q_bp->b_resid = q_bp->b_bcount;
1345 q_bp->b_error = ENXIO;
1346 q_bp->b_flags |= B_ERROR;
1349 softc->queue_count = 0;
1352 xpt_print_path(periph->path);
1353 printf("lost device\n");
1358 sacleanup(struct cam_periph *periph)
1360 struct sa_softc *softc;
1363 softc = (struct sa_softc *)periph->softc;
1365 devstat_remove_entry(&softc->device_stats);
1367 destroy_dev(softc->devs.ctl_dev);
1368 destroy_dev(softc->devs.r_dev);
1369 destroy_dev(softc->devs.nr_dev);
1370 destroy_dev(softc->devs.er_dev);
1372 for (i = 0; i < SA_NUM_MODES; i++) {
1373 destroy_dev(softc->devs.mode_devs[i].r_dev);
1374 destroy_dev(softc->devs.mode_devs[i].nr_dev);
1375 destroy_dev(softc->devs.mode_devs[i].er_dev);
1378 cam_extend_release(saperiphs, periph->unit_number);
1379 xpt_print_path(periph->path);
1380 printf("removing device entry\n");
1381 free(softc, M_DEVBUF);
1385 saasync(void *callback_arg, u_int32_t code,
1386 struct cam_path *path, void *arg)
1388 struct cam_periph *periph;
1390 periph = (struct cam_periph *)callback_arg;
1392 case AC_FOUND_DEVICE:
1394 struct ccb_getdev *cgd;
1397 cgd = (struct ccb_getdev *)arg;
1401 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1405 * Allocate a peripheral instance for
1406 * this device and start the probe
1409 status = cam_periph_alloc(saregister, saoninvalidate,
1411 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1412 saasync, AC_FOUND_DEVICE, cgd);
1414 if (status != CAM_REQ_CMP
1415 && status != CAM_REQ_INPROG)
1416 printf("saasync: Unable to probe new device "
1417 "due to status 0x%x\n", status);
1421 cam_periph_async(periph, code, path, arg);
1427 saregister(struct cam_periph *periph, void *arg)
1429 struct sa_softc *softc;
1430 struct ccb_setasync csa;
1431 struct ccb_getdev *cgd;
1435 cgd = (struct ccb_getdev *)arg;
1436 if (periph == NULL) {
1437 printf("saregister: periph was NULL!!\n");
1438 return (CAM_REQ_CMP_ERR);
1442 printf("saregister: no getdev CCB, can't register device\n");
1443 return (CAM_REQ_CMP_ERR);
1446 softc = (struct sa_softc *)malloc(sizeof (*softc), M_DEVBUF, M_NOWAIT);
1447 if (softc == NULL) {
1448 printf("saregister: Unable to probe new device. "
1449 "Unable to allocate softc\n");
1450 return (CAM_REQ_CMP_ERR);
1453 bzero(softc, sizeof(*softc));
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 = (struct scsi_read_block_limits_data *)
1925 malloc(8192, M_TEMP, M_WAITOK);
1926 if (rblim == NULL) {
1927 xpt_print_path(ccb->ccb_h.path);
1928 printf("no memory for test read\n");
1929 xpt_release_ccb(ccb);
1934 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1935 scsi_sa_read_write(&ccb->csio, 0, sadone,
1936 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1937 (void *) rblim, 8192, SSD_FULL_SIZE,
1939 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1940 &softc->device_stats);
1942 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1943 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1944 error = cam_periph_runccb(ccb, saerror, 0,
1945 SF_NO_PRINT | SF_RETRY_SELTO | SF_RETRY_UA,
1946 &softc->device_stats);
1949 xpt_print_path(ccb->ccb_h.path);
1950 printf("unable to rewind after test read\n");
1951 xpt_release_ccb(ccb);
1957 * Next off, determine block limits.
1959 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1960 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1962 error = cam_periph_runccb(ccb, saerror, 0,
1963 SF_NO_PRINT | SF_RETRY_UA | SF_RETRY_SELTO,
1964 &softc->device_stats);
1966 xpt_release_ccb(ccb);
1970 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1971 * a MANDATORY command. Anyway- it doesn't matter-
1972 * we can proceed anyway.
1974 softc->blk_gran = 0;
1975 softc->max_blk = ~0;
1978 if (softc->scsi_rev >= SCSI_REV_3) {
1979 softc->blk_gran = RBL_GRAN(rblim);
1981 softc->blk_gran = 0;
1984 * We take max_blk == min_blk to mean a default to
1985 * fixed mode- but note that whatever we get out of
1986 * sagetparams below will actually determine whether
1987 * we are actually *in* fixed mode.
1989 softc->max_blk = scsi_3btoul(rblim->maximum);
1990 softc->min_blk = scsi_2btoul(rblim->minimum);
1995 * Next, perform a mode sense to determine
1996 * current density, blocksize, compression etc.
1998 error = sagetparams(periph, SA_PARAM_ALL,
1999 &softc->media_blksize,
2000 &softc->media_density,
2001 &softc->media_numblks,
2002 &softc->buffer_mode, &write_protect,
2003 &softc->speed, &comp_supported,
2004 &comp_enabled, &softc->comp_algorithm,
2009 * We could work a little harder here. We could
2010 * adjust our attempts to get information. It
2011 * might be an ancient tape drive. If someone
2012 * nudges us, we'll do that.
2018 * If no quirk has determined that this is a device that is
2019 * preferred to be in fixed or variable mode, now is the time
2022 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2025 * This could be expensive to find out. Luckily we
2026 * only need to do this once. If we start out in
2027 * 'default' mode, try and set ourselves to one
2028 * of the densities that would determine a wad
2029 * of other stuff. Go from highest to lowest.
2031 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2033 static u_int8_t ctry[] = {
2034 SCSI_DENSITY_HALFINCH_PE,
2035 SCSI_DENSITY_HALFINCH_6250C,
2036 SCSI_DENSITY_HALFINCH_6250,
2037 SCSI_DENSITY_HALFINCH_1600,
2038 SCSI_DENSITY_HALFINCH_800,
2039 SCSI_DENSITY_QIC_4GB,
2040 SCSI_DENSITY_QIC_2GB,
2041 SCSI_DENSITY_QIC_525_320,
2042 SCSI_DENSITY_QIC_150,
2043 SCSI_DENSITY_QIC_120,
2044 SCSI_DENSITY_QIC_24,
2045 SCSI_DENSITY_QIC_11_9TRK,
2046 SCSI_DENSITY_QIC_11_4TRK,
2047 SCSI_DENSITY_QIC_1320,
2048 SCSI_DENSITY_QIC_3080,
2051 for (i = 0; ctry[i]; i++) {
2052 error = sasetparams(periph,
2053 SA_PARAM_DENSITY, 0, ctry[i],
2056 softc->media_density = ctry[i];
2061 switch (softc->media_density) {
2062 case SCSI_DENSITY_QIC_11_4TRK:
2063 case SCSI_DENSITY_QIC_11_9TRK:
2064 case SCSI_DENSITY_QIC_24:
2065 case SCSI_DENSITY_QIC_120:
2066 case SCSI_DENSITY_QIC_150:
2067 case SCSI_DENSITY_QIC_525_320:
2068 case SCSI_DENSITY_QIC_1320:
2069 case SCSI_DENSITY_QIC_3080:
2070 softc->quirks &= ~SA_QUIRK_2FM;
2071 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2072 softc->last_media_blksize = 512;
2074 case SCSI_DENSITY_QIC_4GB:
2075 case SCSI_DENSITY_QIC_2GB:
2076 softc->quirks &= ~SA_QUIRK_2FM;
2077 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2078 softc->last_media_blksize = 1024;
2081 softc->last_media_blksize =
2082 softc->media_blksize;
2083 softc->quirks |= SA_QUIRK_VARIABLE;
2089 * If no quirk has determined that this is a device that needs
2090 * to have 2 Filemarks at EOD, now is the time to find out.
2093 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2094 switch (softc->media_density) {
2095 case SCSI_DENSITY_HALFINCH_800:
2096 case SCSI_DENSITY_HALFINCH_1600:
2097 case SCSI_DENSITY_HALFINCH_6250:
2098 case SCSI_DENSITY_HALFINCH_6250C:
2099 case SCSI_DENSITY_HALFINCH_PE:
2100 softc->quirks &= ~SA_QUIRK_1FM;
2101 softc->quirks |= SA_QUIRK_2FM;
2109 * Now validate that some info we got makes sense.
2111 if ((softc->max_blk < softc->media_blksize) ||
2112 (softc->min_blk > softc->media_blksize &&
2113 softc->media_blksize)) {
2114 xpt_print_path(ccb->ccb_h.path);
2115 printf("BLOCK LIMITS (%d..%d) could not match current "
2116 "block settings (%d)- adjusting\n", softc->min_blk,
2117 softc->max_blk, softc->media_blksize);
2118 softc->max_blk = softc->min_blk =
2119 softc->media_blksize;
2123 * Now put ourselves into the right frame of mind based
2128 * If we want to be in FIXED mode and our current blocksize
2129 * is not equal to our last blocksize (if nonzero), try and
2130 * set ourselves to this last blocksize (as the 'preferred'
2131 * block size). The initial quirkmatch at registry sets the
2132 * initial 'last' blocksize. If, for whatever reason, this
2133 * 'last' blocksize is zero, set the blocksize to 512,
2134 * or min_blk if that's larger.
2136 if ((softc->quirks & SA_QUIRK_FIXED) &&
2137 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2138 (softc->media_blksize != softc->last_media_blksize)) {
2139 softc->media_blksize = softc->last_media_blksize;
2140 if (softc->media_blksize == 0) {
2141 softc->media_blksize = 512;
2142 if (softc->media_blksize < softc->min_blk) {
2143 softc->media_blksize = softc->min_blk;
2146 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2147 softc->media_blksize, 0, 0, SF_NO_PRINT);
2149 xpt_print_path(ccb->ccb_h.path);
2150 printf("unable to set fixed blocksize to %d\n",
2151 softc->media_blksize);
2156 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2157 (softc->media_blksize != 0)) {
2158 softc->last_media_blksize = softc->media_blksize;
2159 softc->media_blksize = 0;
2160 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2161 0, 0, 0, SF_NO_PRINT);
2164 * If this fails and we were guessing, just
2165 * assume that we got it wrong and go try
2166 * fixed block mode. Don't even check against
2167 * density code at this point.
2170 softc->quirks &= ~SA_QUIRK_VARIABLE;
2171 softc->quirks |= SA_QUIRK_FIXED;
2172 if (softc->last_media_blksize == 0)
2173 softc->last_media_blksize = 512;
2176 xpt_print_path(ccb->ccb_h.path);
2177 printf("unable to set variable blocksize\n");
2183 * Now that we have the current block size,
2184 * set up some parameters for sastart's usage.
2186 if (softc->media_blksize) {
2187 softc->flags |= SA_FLAG_FIXED;
2188 if (powerof2(softc->media_blksize)) {
2190 ffs(softc->media_blksize) - 1;
2191 softc->blk_mask = softc->media_blksize - 1;
2193 softc->blk_mask = ~0;
2194 softc->blk_shift = 0;
2198 * The SCSI-3 spec allows 0 to mean "unspecified".
2199 * The SCSI-1 spec allows 0 to mean 'infinite'.
2201 * Either works here.
2203 if (softc->max_blk == 0) {
2204 softc->max_blk = ~0;
2206 softc->blk_shift = 0;
2207 if (softc->blk_gran != 0) {
2208 softc->blk_mask = softc->blk_gran - 1;
2210 softc->blk_mask = 0;
2215 softc->flags |= SA_FLAG_TAPE_WP;
2217 if (comp_supported) {
2218 if (softc->saved_comp_algorithm == 0)
2219 softc->saved_comp_algorithm =
2220 softc->comp_algorithm;
2221 softc->flags |= SA_FLAG_COMP_SUPP;
2223 softc->flags |= SA_FLAG_COMP_ENABLED;
2225 softc->flags |= SA_FLAG_COMP_UNSUPP;
2227 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2228 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2229 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2232 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2234 xpt_print_path(ccb->ccb_h.path);
2235 printf("unable to set buffered mode\n");
2237 error = 0; /* not an error */
2242 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2246 free(rblim, M_TEMP);
2249 softc->dsreg = MTIO_DSREG_NIL;
2251 softc->fileno = softc->blkno = 0;
2252 softc->dsreg = MTIO_DSREG_REST;
2254 #ifdef SA_1FM_AT_EOD
2255 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2256 softc->quirks |= SA_QUIRK_1FM;
2258 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2259 softc->quirks |= SA_QUIRK_2FM;
2262 xpt_release_ccb(ccb);
2265 * If we return an error, we're not mounted any more,
2266 * so release any device reservation.
2269 (void) sareservereleaseunit(periph, FALSE);
2272 * Clear I/O residual.
2274 softc->last_io_resid = 0;
2275 softc->last_ctl_resid = 0;
2281 * How many filemarks do we need to write if we were to terminate the
2282 * tape session right now? Note that this can be a negative number
2286 samarkswanted(struct cam_periph *periph)
2289 struct sa_softc *softc;
2291 softc = (struct sa_softc *)periph->softc;
2293 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2295 if (softc->quirks & SA_QUIRK_2FM)
2298 markswanted -= softc->filemarks;
2299 return (markswanted);
2303 sacheckeod(struct cam_periph *periph)
2307 struct sa_softc *softc;
2309 softc = (struct sa_softc *)periph->softc;
2310 markswanted = samarkswanted(periph);
2312 if (markswanted > 0) {
2313 error = sawritefilemarks(periph, markswanted, FALSE);
2321 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2323 static const char *toobig =
2324 "%d-byte tape record bigger than supplied buffer\n";
2325 struct cam_periph *periph;
2326 struct sa_softc *softc;
2327 struct ccb_scsiio *csio;
2328 struct scsi_sense_data *sense;
2329 u_int32_t resid = 0;
2332 int error_code, sense_key, asc, ascq, error, aqvalid;
2334 periph = xpt_path_periph(ccb->ccb_h.path);
2335 softc = (struct sa_softc *)periph->softc;
2337 sense = &csio->sense_data;
2338 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2339 aqvalid = sense->extra_len >= 6;
2342 status = csio->ccb_h.status & CAM_STATUS_MASK;
2345 * Calculate/latch up, any residuals... We do this in a funny 2-step
2346 * so we can print stuff here if we have CAM_DEBUG enabled for this
2349 if (status == CAM_SCSI_STATUS_ERROR) {
2350 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2351 info = (int32_t) scsi_4btoul(sense->info);
2353 if ((softc->flags & SA_FLAG_FIXED) != 0)
2354 resid *= softc->media_blksize;
2356 resid = csio->dxfer_len;
2358 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2359 if (softc->media_blksize)
2360 info /= softc->media_blksize;
2363 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2364 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2365 sizeof (struct scsi_sense_data));
2366 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2367 (int) csio->cdb_len);
2368 softc->last_io_resid = resid;
2369 softc->last_resid_was_io = 1;
2371 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2372 sizeof (struct scsi_sense_data));
2373 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2374 (int) csio->cdb_len);
2375 softc->last_ctl_resid = resid;
2376 softc->last_resid_was_io = 0;
2378 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2379 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %d "
2380 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2381 sense_key, asc, ascq, status,
2382 sense->flags & ~SSD_KEY_RESERVED, resid, csio->dxfer_len));
2384 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2385 ("Cam Status 0x%x\n", status));
2391 case CAM_SCSI_STATUS_ERROR:
2393 * If a read/write command, we handle it here.
2395 if (CCB_Type(csio) != SA_CCB_WAITING) {
2399 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2400 * on a non read/write command, we assume it's not an error
2401 * and propagate the residule and return.
2403 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2404 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2405 csio->resid = resid;
2410 * Otherwise, we let the common code handle this.
2412 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2416 * We cannot depend upon CAM honoring retry counts for these.
2418 case CAM_SCSI_BUS_RESET:
2420 if (ccb->ccb_h.retry_count <= 0) {
2426 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2430 * Handle filemark, end of tape, mismatched record sizes....
2431 * From this point out, we're only handling read/write cases.
2432 * Handle writes && reads differently.
2435 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2436 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2437 csio->resid = resid;
2439 } else if (sense->flags & SSD_EOM) {
2440 softc->flags |= SA_FLAG_EOM_PENDING;
2442 * Grotesque as it seems, the few times
2443 * I've actually seen a non-zero resid,
2444 * the tape drive actually lied and had
2445 * writtent all the data!.
2450 csio->resid = resid;
2451 if (sense_key == SSD_KEY_BLANK_CHECK) {
2452 if (softc->quirks & SA_QUIRK_1FM) {
2454 softc->flags |= SA_FLAG_EOM_PENDING;
2458 } else if (sense->flags & SSD_FILEMARK) {
2459 if (softc->flags & SA_FLAG_FIXED) {
2461 softc->flags |= SA_FLAG_EOF_PENDING;
2464 * Unconditionally, if we detected a filemark on a read,
2465 * mark that we've run moved a file ahead.
2467 if (softc->fileno != (daddr_t) -1) {
2470 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2476 * Incorrect Length usually applies to read, but can apply to writes.
2478 if (error == 0 && (sense->flags & SSD_ILI)) {
2480 xpt_print_path(csio->ccb_h.path);
2481 printf(toobig, csio->dxfer_len - info);
2482 csio->resid = csio->dxfer_len;
2485 csio->resid = resid;
2486 if (softc->flags & SA_FLAG_FIXED) {
2487 softc->flags |= SA_FLAG_EIO_PENDING;
2490 * Bump the block number if we hadn't seen a filemark.
2491 * Do this independent of errors (we've moved anyway).
2493 if ((sense->flags & SSD_FILEMARK) == 0) {
2494 if (softc->blkno != (daddr_t) -1) {
2496 csio->ccb_h.ccb_pflags |=
2497 SA_POSITION_UPDATED;
2505 * Unfreeze the queue if frozen as we're not returning anything
2506 * to our waiters that would indicate an I/O error has occurred
2516 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2517 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2518 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2519 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2524 struct scsi_mode_header_6 *mode_hdr;
2525 struct scsi_mode_blk_desc *mode_blk;
2526 int mode_buffer_len;
2527 struct sa_softc *softc;
2532 softc = (struct sa_softc *)periph->softc;
2533 ccb = cam_periph_getccb(periph, 1);
2534 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2535 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2537 cpage = SA_DATA_COMPRESSION_PAGE;
2540 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2542 if (params_to_get & SA_PARAM_COMPRESSION) {
2543 if (softc->quirks & SA_QUIRK_NOCOMP) {
2544 *comp_supported = FALSE;
2545 params_to_get &= ~SA_PARAM_COMPRESSION;
2547 mode_buffer_len += sizeof (sa_comp_t);
2550 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_WAITOK);
2551 bzero(mode_buffer, mode_buffer_len);
2552 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2553 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2555 /* it is safe to retry this */
2556 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2557 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2558 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2559 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2561 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2562 &softc->device_stats);
2565 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2567 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2569 * Hmm. Let's see if we can try another page...
2570 * If we've already done that, give up on compression
2571 * for this device and remember this for the future
2572 * and attempt the request without asking for compression
2575 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2576 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2579 softc->quirks |= SA_QUIRK_NOCOMP;
2580 free(mode_buffer, M_TEMP);
2582 } else if (status == CAM_SCSI_STATUS_ERROR) {
2583 /* Tell the user about the fatal error. */
2584 scsi_sense_print(&ccb->csio);
2585 goto sagetparamsexit;
2589 * If the user only wants the compression information, and
2590 * the device doesn't send back the block descriptor, it's
2591 * no big deal. If the user wants more than just
2592 * compression, though, and the device doesn't pass back the
2593 * block descriptor, we need to send another mode sense to
2594 * get the block descriptor.
2596 if ((mode_hdr->blk_desc_len == 0) &&
2597 (params_to_get & SA_PARAM_COMPRESSION) &&
2598 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2601 * Decrease the mode buffer length by the size of
2602 * the compression page, to make sure the data
2603 * there doesn't get overwritten.
2605 mode_buffer_len -= sizeof (sa_comp_t);
2608 * Now move the compression page that we presumably
2609 * got back down the memory chunk a little bit so
2610 * it doesn't get spammed.
2612 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2613 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2616 * Now, we issue another mode sense and just ask
2617 * for the block descriptor, etc.
2620 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2621 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2622 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2625 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2626 &softc->device_stats);
2630 goto sagetparamsexit;
2633 if (params_to_get & SA_PARAM_BLOCKSIZE)
2634 *blocksize = scsi_3btoul(mode_blk->blklen);
2636 if (params_to_get & SA_PARAM_NUMBLOCKS)
2637 *numblocks = scsi_3btoul(mode_blk->nblocks);
2639 if (params_to_get & SA_PARAM_BUFF_MODE)
2640 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2642 if (params_to_get & SA_PARAM_DENSITY)
2643 *density = mode_blk->density;
2645 if (params_to_get & SA_PARAM_WP)
2646 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2648 if (params_to_get & SA_PARAM_SPEED)
2649 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2651 if (params_to_get & SA_PARAM_COMPRESSION) {
2652 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2653 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2654 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2656 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2658 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2659 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2661 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2663 * We don't really know whether this device supports
2664 * Data Compression if the the algorithm field is
2665 * zero. Just say we do.
2667 *comp_supported = TRUE;
2669 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2670 *comp_algorithm = cp->sel_comp_alg;
2673 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2676 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2678 char *xyz = mode_buffer;
2679 xpt_print_path(periph->path);
2680 printf("Mode Sense Data=");
2681 for (idx = 0; idx < mode_buffer_len; idx++)
2682 printf(" 0x%02x", xyz[idx] & 0xff);
2688 xpt_release_ccb(ccb);
2689 free(mode_buffer, M_TEMP);
2694 * The purpose of this function is to set one of four different parameters
2698 * - compression / compression algorithm
2701 * The assumption is that this will be called from saioctl(), and therefore
2702 * from a process context. Thus the waiting malloc calls below. If that
2703 * assumption ever changes, the malloc calls should be changed to be
2706 * Any or all of the four parameters may be set when this function is
2707 * called. It should handle setting more than one parameter at once.
2710 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2711 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2712 u_int32_t sense_flags)
2714 struct sa_softc *softc;
2715 u_int32_t current_blocksize;
2716 u_int32_t current_calg;
2717 u_int8_t current_density;
2718 u_int8_t current_speed;
2719 int comp_enabled, comp_supported;
2721 int mode_buffer_len;
2722 struct scsi_mode_header_6 *mode_hdr;
2723 struct scsi_mode_blk_desc *mode_blk;
2724 sa_comp_t *ccomp, *cpage;
2726 union ccb *ccb = NULL;
2729 softc = (struct sa_softc *)periph->softc;
2731 ccomp = malloc(sizeof (sa_comp_t), M_TEMP, M_WAITOK);
2734 * Since it doesn't make sense to set the number of blocks, or
2735 * write protection, we won't try to get the current value. We
2736 * always want to get the blocksize, so we can set it back to the
2739 error = sagetparams(periph,
2740 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2741 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2742 ¤t_speed, &comp_supported, &comp_enabled,
2743 ¤t_calg, ccomp);
2746 free(ccomp, M_TEMP);
2750 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2751 if (params_to_set & SA_PARAM_COMPRESSION)
2752 mode_buffer_len += sizeof (sa_comp_t);
2754 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_WAITOK);
2755 bzero(mode_buffer, mode_buffer_len);
2757 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2758 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2760 ccb = cam_periph_getccb(periph, 1);
2764 if (params_to_set & SA_PARAM_COMPRESSION) {
2766 cpage = (sa_comp_t *)&mode_blk[1];
2768 cpage = (sa_comp_t *)&mode_hdr[1];
2770 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2771 cpage->hdr.pagecode &= ~0x80;
2776 * If the caller wants us to set the blocksize, use the one they
2777 * pass in. Otherwise, use the blocksize we got back from the
2778 * mode select above.
2781 if (params_to_set & SA_PARAM_BLOCKSIZE)
2782 scsi_ulto3b(blocksize, mode_blk->blklen);
2784 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2787 * Set density if requested, else preserve old density.
2788 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2789 * devices, else density we've latched up in our softc.
2791 if (params_to_set & SA_PARAM_DENSITY) {
2792 mode_blk->density = density;
2793 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2794 mode_blk->density = SCSI_SAME_DENSITY;
2796 mode_blk->density = softc->media_density;
2801 * For mode selects, these two fields must be zero.
2803 mode_hdr->data_length = 0;
2804 mode_hdr->medium_type = 0;
2806 /* set the speed to the current value */
2807 mode_hdr->dev_spec = current_speed;
2809 /* set single-initiator buffering mode */
2810 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2813 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2815 mode_hdr->blk_desc_len = 0;
2818 * First, if the user wants us to set the compression algorithm or
2819 * just turn compression on, check to make sure that this drive
2820 * supports compression.
2822 if (params_to_set & SA_PARAM_COMPRESSION) {
2824 * If the compression algorithm is 0, disable compression.
2825 * If the compression algorithm is non-zero, enable
2826 * compression and set the compression type to the
2827 * specified compression algorithm, unless the algorithm is
2828 * MT_COMP_ENABLE. In that case, we look at the
2829 * compression algorithm that is currently set and if it is
2830 * non-zero, we leave it as-is. If it is zero, and we have
2831 * saved a compression algorithm from a time when
2832 * compression was enabled before, set the compression to
2835 switch (ccomp->hdr.pagecode & ~0x80) {
2836 case SA_DATA_COMPRESSION_PAGE:
2837 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2838 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2841 * Disable compression, but leave the
2842 * decompression and the capability bit
2845 dcp->dce_and_dcc = SA_DCP_DCC;
2846 dcp->dde_and_red |= SA_DCP_DDE;
2849 /* enable compression && decompression */
2850 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2851 dcp->dde_and_red |= SA_DCP_DDE;
2853 * If there, use compression algorithm from caller.
2854 * Otherwise, if there's a saved compression algorithm
2855 * and there is no current algorithm, use the saved
2856 * algorithm. Else parrot back what we got and hope
2859 if (calg != MT_COMP_ENABLE) {
2860 scsi_ulto4b(calg, dcp->comp_algorithm);
2861 scsi_ulto4b(calg, dcp->decomp_algorithm);
2862 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2863 softc->saved_comp_algorithm != 0) {
2864 scsi_ulto4b(softc->saved_comp_algorithm,
2865 dcp->comp_algorithm);
2866 scsi_ulto4b(softc->saved_comp_algorithm,
2867 dcp->decomp_algorithm);
2871 case SA_DEVICE_CONFIGURATION_PAGE:
2873 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2875 dcp->sel_comp_alg = SA_COMP_NONE;
2878 if (calg != MT_COMP_ENABLE) {
2879 dcp->sel_comp_alg = calg;
2880 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2881 softc->saved_comp_algorithm != 0) {
2882 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2888 * The drive doesn't seem to support compression,
2889 * so turn off the set compression bit.
2891 params_to_set &= ~SA_PARAM_COMPRESSION;
2892 xpt_print_path(periph->path);
2893 printf("device does not seem to support compression\n");
2896 * If that was the only thing the user wanted us to set,
2897 * clean up allocated resources and return with
2898 * 'operation not supported'.
2900 if (params_to_set == SA_PARAM_NONE) {
2901 free(mode_buffer, M_TEMP);
2902 xpt_release_ccb(ccb);
2907 * That wasn't the only thing the user wanted us to set.
2908 * So, decrease the stated mode buffer length by the
2909 * size of the compression mode page.
2911 mode_buffer_len -= sizeof(sa_comp_t);
2915 /* It is safe to retry this operation */
2916 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2917 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2918 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2920 error = cam_periph_runccb(ccb, saerror, 0,
2921 sense_flags, &softc->device_stats);
2924 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2926 char *xyz = mode_buffer;
2927 xpt_print_path(periph->path);
2928 printf("Err%d, Mode Select Data=", error);
2929 for (idx = 0; idx < mode_buffer_len; idx++)
2930 printf(" 0x%02x", xyz[idx] & 0xff);
2937 * If we can, try without setting density/blocksize.
2940 if ((params_to_set &
2941 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2946 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2947 cpage = (sa_comp_t *)&mode_blk[1];
2951 * If we were setting the blocksize, and that failed, we
2952 * want to set it to its original value. If we weren't
2953 * setting the blocksize, we don't want to change it.
2955 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2958 * Set density if requested, else preserve old density.
2959 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2960 * devices, else density we've latched up in our softc.
2962 if (params_to_set & SA_PARAM_DENSITY) {
2963 mode_blk->density = current_density;
2964 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2965 mode_blk->density = SCSI_SAME_DENSITY;
2967 mode_blk->density = softc->media_density;
2970 if (params_to_set & SA_PARAM_COMPRESSION)
2971 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2974 * The retry count is the only CCB field that might have been
2975 * changed that we care about, so reset it back to 1.
2977 ccb->ccb_h.retry_count = 1;
2978 cam_periph_runccb(ccb, saerror, 0, sense_flags,
2979 &softc->device_stats);
2983 xpt_release_ccb(ccb);
2986 free(ccomp, M_TEMP);
2988 if (params_to_set & SA_PARAM_COMPRESSION) {
2990 softc->flags &= ~SA_FLAG_COMP_ENABLED;
2992 * Even if we get an error setting compression,
2993 * do not say that we don't support it. We could
2994 * have been wrong, or it may be media specific.
2995 * softc->flags &= ~SA_FLAG_COMP_SUPP;
2997 softc->saved_comp_algorithm = softc->comp_algorithm;
2998 softc->comp_algorithm = 0;
3000 softc->flags |= SA_FLAG_COMP_ENABLED;
3001 softc->comp_algorithm = calg;
3005 free(mode_buffer, M_TEMP);
3010 saprevent(struct cam_periph *periph, int action)
3012 struct sa_softc *softc;
3016 softc = (struct sa_softc *)periph->softc;
3018 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3020 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3024 * We can be quiet about illegal requests.
3026 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3031 ccb = cam_periph_getccb(periph, 1);
3033 /* It is safe to retry this operation */
3034 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3035 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3037 error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats);
3040 if (action == PR_ALLOW)
3041 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3043 softc->flags |= SA_FLAG_TAPE_LOCKED;
3046 xpt_release_ccb(ccb);
3050 sarewind(struct cam_periph *periph)
3053 struct sa_softc *softc;
3056 softc = (struct sa_softc *)periph->softc;
3058 ccb = cam_periph_getccb(periph, 1);
3060 /* It is safe to retry this operation */
3061 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3062 SSD_FULL_SIZE, REWIND_TIMEOUT);
3064 softc->dsreg = MTIO_DSREG_REW;
3065 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3066 softc->dsreg = MTIO_DSREG_REST;
3068 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3069 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3071 xpt_release_ccb(ccb);
3073 softc->fileno = softc->blkno = (daddr_t) 0;
3075 softc->fileno = softc->blkno = (daddr_t) -1;
3080 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3083 struct sa_softc *softc;
3086 softc = (struct sa_softc *)periph->softc;
3088 ccb = cam_periph_getccb(periph, 1);
3090 /* This cannot be retried */
3092 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3093 SSD_FULL_SIZE, SPACE_TIMEOUT);
3096 * Clear residual because we will be using it.
3098 softc->last_ctl_resid = 0;
3100 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3101 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3102 softc->dsreg = MTIO_DSREG_REST;
3104 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3105 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3107 xpt_release_ccb(ccb);
3110 * If a spacing operation has failed, we need to invalidate
3113 * If the spacing operation was setmarks or to end of recorded data,
3114 * we no longer know our relative position.
3116 * If the spacing operations was spacing files in reverse, we
3117 * take account of the residual, but still check against less
3118 * than zero- if we've gone negative, we must have hit BOT.
3120 * If the spacing operations was spacing records in reverse and
3121 * we have a residual, we've either hit BOT or hit a filemark.
3122 * In the former case, we know our new record number (0). In
3123 * the latter case, we have absolutely no idea what the real
3124 * record number is- we've stopped between the end of the last
3125 * record in the previous file and the filemark that stopped
3126 * our spacing backwards.
3129 softc->fileno = softc->blkno = (daddr_t) -1;
3130 } else if (code == SS_SETMARKS || code == SS_EOD) {
3131 softc->fileno = softc->blkno = (daddr_t) -1;
3132 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3133 softc->fileno += (count - softc->last_ctl_resid);
3134 if (softc->fileno < 0) /* we must of hit BOT */
3137 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3138 softc->blkno += (count - softc->last_ctl_resid);
3140 if (softc->last_ctl_resid || softc->blkno < 0) {
3141 if (softc->fileno == 0) {
3144 softc->blkno = (daddr_t) -1;
3153 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3156 struct sa_softc *softc;
3159 softc = (struct sa_softc *)periph->softc;
3161 ccb = cam_periph_getccb(periph, 1);
3163 * Clear residual because we will be using it.
3165 softc->last_ctl_resid = 0;
3167 softc->dsreg = MTIO_DSREG_FMK;
3168 /* this *must* not be retried */
3169 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3170 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3171 softc->dsreg = MTIO_DSREG_REST;
3174 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3176 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3177 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3179 if (error == 0 && nmarks) {
3180 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3181 nwm = nmarks - softc->last_ctl_resid;
3182 softc->filemarks += nwm;
3185 xpt_release_ccb(ccb);
3188 * Update relative positions (if we're doing that).
3191 softc->fileno = softc->blkno = (daddr_t) -1;
3192 } else if (softc->fileno != (daddr_t) -1) {
3193 softc->fileno += nwm;
3200 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3202 struct scsi_tape_position_data loc;
3204 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3208 * We try and flush any buffered writes here if we were writing
3209 * and we're trying to get hardware block position. It eats
3210 * up performance substantially, but I'm wary of drive firmware.
3212 * I think that *logical* block position is probably okay-
3213 * but hardware block position might have to wait for data
3214 * to hit media to be valid. Caveat Emptor.
3217 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3218 error = sawritefilemarks(periph, 0, 0);
3219 if (error && error != EACCES)
3223 ccb = cam_periph_getccb(periph, 1);
3224 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3225 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3226 softc->dsreg = MTIO_DSREG_RBSY;
3227 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3228 softc->dsreg = MTIO_DSREG_REST;
3229 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3230 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3233 if (loc.flags & SA_RPOS_UNCERTAIN) {
3234 error = EINVAL; /* nothing is certain */
3236 *blkptr = scsi_4btoul(loc.firstblk);
3240 xpt_release_ccb(ccb);
3245 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3248 struct sa_softc *softc;
3252 * We used to try and flush any buffered writes here.
3253 * Now we push this onto user applications to either
3254 * flush the pending writes themselves (via a zero count
3255 * WRITE FILEMARKS command) or they can trust their tape
3256 * drive to do this correctly for them.
3259 softc = (struct sa_softc *)periph->softc;
3260 ccb = cam_periph_getccb(periph, 1);
3263 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3264 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3267 softc->dsreg = MTIO_DSREG_POS;
3268 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3269 softc->dsreg = MTIO_DSREG_REST;
3270 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3271 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3272 xpt_release_ccb(ccb);
3274 * Note relative file && block number position as now unknown.
3276 softc->fileno = softc->blkno = (daddr_t) -1;
3281 saretension(struct cam_periph *periph)
3284 struct sa_softc *softc;
3287 softc = (struct sa_softc *)periph->softc;
3289 ccb = cam_periph_getccb(periph, 1);
3291 /* It is safe to retry this operation */
3292 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3293 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3295 softc->dsreg = MTIO_DSREG_TEN;
3296 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3297 softc->dsreg = MTIO_DSREG_REST;
3299 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3300 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3301 xpt_release_ccb(ccb);
3303 softc->fileno = softc->blkno = (daddr_t) 0;
3305 softc->fileno = softc->blkno = (daddr_t) -1;
3310 sareservereleaseunit(struct cam_periph *periph, int reserve)
3313 struct sa_softc *softc;
3316 softc = (struct sa_softc *)periph->softc;
3317 ccb = cam_periph_getccb(periph, 1);
3319 /* It is safe to retry this operation */
3320 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3321 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3322 softc->dsreg = MTIO_DSREG_RBSY;
3323 error = cam_periph_runccb(ccb, saerror, 0,
3324 SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats);
3325 softc->dsreg = MTIO_DSREG_REST;
3327 xpt_release_ccb(ccb);
3330 * If the error was Illegal Request, then the device doesn't support
3331 * RESERVE/RELEASE. This is not an error.
3333 if (error == EINVAL) {
3341 saloadunload(struct cam_periph *periph, int load)
3344 struct sa_softc *softc;
3347 softc = (struct sa_softc *)periph->softc;
3349 ccb = cam_periph_getccb(periph, 1);
3351 /* It is safe to retry this operation */
3352 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3353 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3355 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3356 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3357 softc->dsreg = MTIO_DSREG_REST;
3359 xpt_release_ccb(ccb);
3361 if (error || load == 0)
3362 softc->fileno = softc->blkno = (daddr_t) -1;
3363 else if (error == 0)
3364 softc->fileno = softc->blkno = (daddr_t) 0;
3369 saerase(struct cam_periph *periph, int longerase)
3373 struct sa_softc *softc;
3376 softc = (struct sa_softc *)periph->softc;
3378 ccb = cam_periph_getccb(periph, 1);
3380 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3381 SSD_FULL_SIZE, ERASE_TIMEOUT);
3383 softc->dsreg = MTIO_DSREG_ZER;
3384 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3385 softc->dsreg = MTIO_DSREG_REST;
3387 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3388 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3389 xpt_release_ccb(ccb);
3393 #endif /* _KERNEL */
3396 * Read tape block limits command.
3399 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3400 void (*cbfcnp)(struct cam_periph *, union ccb *),
3401 u_int8_t tag_action,
3402 struct scsi_read_block_limits_data *rlimit_buf,
3403 u_int8_t sense_len, u_int32_t timeout)
3405 struct scsi_read_block_limits *scsi_cmd;
3407 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3408 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3409 sizeof(*scsi_cmd), timeout);
3411 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3412 bzero(scsi_cmd, sizeof(*scsi_cmd));
3413 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3417 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3418 void (*cbfcnp)(struct cam_periph *, union ccb *),
3419 u_int8_t tag_action, int readop, int sli,
3420 int fixed, u_int32_t length, u_int8_t *data_ptr,
3421 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3423 struct scsi_sa_rw *scsi_cmd;
3425 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3426 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3427 scsi_cmd->sli_fixed = 0;
3429 scsi_cmd->sli_fixed |= SAR_SLI;
3431 scsi_cmd->sli_fixed |= SARW_FIXED;
3432 scsi_ulto3b(length, scsi_cmd->length);
3433 scsi_cmd->control = 0;
3435 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3436 tag_action, data_ptr, dxfer_len, sense_len,
3437 sizeof(*scsi_cmd), timeout);
3441 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3442 void (*cbfcnp)(struct cam_periph *, union ccb *),
3443 u_int8_t tag_action, int immediate, int eot,
3444 int reten, int load, u_int8_t sense_len,
3447 struct scsi_load_unload *scsi_cmd;
3449 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3450 bzero(scsi_cmd, sizeof(*scsi_cmd));
3451 scsi_cmd->opcode = LOAD_UNLOAD;
3453 scsi_cmd->immediate = SLU_IMMED;
3455 scsi_cmd->eot_reten_load |= SLU_EOT;
3457 scsi_cmd->eot_reten_load |= SLU_RETEN;
3459 scsi_cmd->eot_reten_load |= SLU_LOAD;
3461 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3462 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3466 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3467 void (*cbfcnp)(struct cam_periph *, union ccb *),
3468 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3471 struct scsi_rewind *scsi_cmd;
3473 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3474 bzero(scsi_cmd, sizeof(*scsi_cmd));
3475 scsi_cmd->opcode = REWIND;
3477 scsi_cmd->immediate = SREW_IMMED;
3479 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3480 0, sense_len, sizeof(*scsi_cmd), timeout);
3484 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3485 void (*cbfcnp)(struct cam_periph *, union ccb *),
3486 u_int8_t tag_action, scsi_space_code code,
3487 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3489 struct scsi_space *scsi_cmd;
3491 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3492 scsi_cmd->opcode = SPACE;
3493 scsi_cmd->code = code;
3494 scsi_ulto3b(count, scsi_cmd->count);
3495 scsi_cmd->control = 0;
3497 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3498 0, sense_len, sizeof(*scsi_cmd), timeout);
3502 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3503 void (*cbfcnp)(struct cam_periph *, union ccb *),
3504 u_int8_t tag_action, int immediate, int setmark,
3505 u_int32_t num_marks, u_int8_t sense_len,
3508 struct scsi_write_filemarks *scsi_cmd;
3510 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3511 bzero(scsi_cmd, sizeof(*scsi_cmd));
3512 scsi_cmd->opcode = WRITE_FILEMARKS;
3514 scsi_cmd->byte2 |= SWFMRK_IMMED;
3516 scsi_cmd->byte2 |= SWFMRK_WSMK;
3518 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3520 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3521 0, sense_len, sizeof(*scsi_cmd), timeout);
3525 * The reserve and release unit commands differ only by their opcodes.
3528 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3529 void (*cbfcnp)(struct cam_periph *, union ccb *),
3530 u_int8_t tag_action, int third_party,
3531 int third_party_id, u_int8_t sense_len,
3532 u_int32_t timeout, int reserve)
3534 struct scsi_reserve_release_unit *scsi_cmd;
3536 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3537 bzero(scsi_cmd, sizeof(*scsi_cmd));
3540 scsi_cmd->opcode = RESERVE_UNIT;
3542 scsi_cmd->opcode = RELEASE_UNIT;
3545 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3546 scsi_cmd->lun_thirdparty |=
3547 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3550 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3551 0, sense_len, sizeof(*scsi_cmd), timeout);
3555 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3556 void (*cbfcnp)(struct cam_periph *, union ccb *),
3557 u_int8_t tag_action, int immediate, int long_erase,
3558 u_int8_t sense_len, u_int32_t timeout)
3560 struct scsi_erase *scsi_cmd;
3562 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3563 bzero(scsi_cmd, sizeof(*scsi_cmd));
3565 scsi_cmd->opcode = ERASE;
3568 scsi_cmd->lun_imm_long |= SE_IMMED;
3571 scsi_cmd->lun_imm_long |= SE_LONG;
3573 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3574 0, sense_len, sizeof(*scsi_cmd), timeout);
3578 * Read Tape Position command.
3581 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3582 void (*cbfcnp)(struct cam_periph *, union ccb *),
3583 u_int8_t tag_action, int hardsoft,
3584 struct scsi_tape_position_data *sbp,
3585 u_int8_t sense_len, u_int32_t timeout)
3587 struct scsi_tape_read_position *scmd;
3589 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3590 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3591 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3592 bzero(scmd, sizeof(*scmd));
3593 scmd->opcode = READ_POSITION;
3594 scmd->byte1 = hardsoft;
3598 * Set Tape Position command.
3601 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3602 void (*cbfcnp)(struct cam_periph *, union ccb *),
3603 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3604 u_int8_t sense_len, u_int32_t timeout)
3606 struct scsi_tape_locate *scmd;
3608 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3609 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3610 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3611 bzero(scmd, sizeof(*scmd));
3612 scmd->opcode = LOCATE;
3614 scmd->byte1 |= SA_SPOS_BT;
3615 scsi_ulto4b(blkno, scmd->blkaddr);