2 * Implementation of SCSI Sequential Access Peripheral driver for CAM.
4 * Copyright (c) 1999, 2000 Matthew Jacob
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/queue.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/types.h>
41 #include <sys/limits.h>
42 #include <sys/malloc.h>
47 #include <sys/fcntl.h>
48 #include <sys/devicestat.h>
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_periph.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_debug.h>
61 #include <cam/scsi/scsi_all.h>
62 #include <cam/scsi/scsi_message.h>
63 #include <cam/scsi/scsi_sa.h>
70 #define SA_IO_TIMEOUT 4
72 #ifndef SA_SPACE_TIMEOUT
73 #define SA_SPACE_TIMEOUT 1 * 60
75 #ifndef SA_REWIND_TIMEOUT
76 #define SA_REWIND_TIMEOUT 2 * 60
78 #ifndef SA_ERASE_TIMEOUT
79 #define SA_ERASE_TIMEOUT 4 * 60
82 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
84 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
85 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
86 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
87 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
90 * Additional options that can be set for config: SA_1FM_AT_EOT
93 #ifndef UNUSED_PARAMETER
94 #define UNUSED_PARAMETER(x) x = x
98 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
99 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
105 static MALLOC_DEFINE(M_SCSISA, "SCSI sa", "SCSI sequential access buffers");
108 SA_STATE_NORMAL, SA_STATE_ABNORMAL
111 #define ccb_pflags ppriv_field0
112 #define ccb_bp ppriv_ptr1
114 #define SA_CCB_BUFFER_IO 0x0
115 #define SA_CCB_WAITING 0x1
116 #define SA_CCB_TYPEMASK 0x1
117 #define SA_POSITION_UPDATED 0x2
119 #define Set_CCB_Type(x, type) \
120 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
121 x->ccb_h.ccb_pflags |= type
123 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
128 SA_FLAG_OPEN = 0x0001,
129 SA_FLAG_FIXED = 0x0002,
130 SA_FLAG_TAPE_LOCKED = 0x0004,
131 SA_FLAG_TAPE_MOUNTED = 0x0008,
132 SA_FLAG_TAPE_WP = 0x0010,
133 SA_FLAG_TAPE_WRITTEN = 0x0020,
134 SA_FLAG_EOM_PENDING = 0x0040,
135 SA_FLAG_EIO_PENDING = 0x0080,
136 SA_FLAG_EOF_PENDING = 0x0100,
137 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
138 SA_FLAG_EOF_PENDING),
139 SA_FLAG_INVALID = 0x0200,
140 SA_FLAG_COMP_ENABLED = 0x0400,
141 SA_FLAG_COMP_SUPP = 0x0800,
142 SA_FLAG_COMP_UNSUPP = 0x1000,
143 SA_FLAG_TAPE_FROZEN = 0x2000
147 SA_MODE_REWIND = 0x00,
148 SA_MODE_NOREWIND = 0x01,
149 SA_MODE_OFFLINE = 0x02
153 SA_PARAM_NONE = 0x00,
154 SA_PARAM_BLOCKSIZE = 0x01,
155 SA_PARAM_DENSITY = 0x02,
156 SA_PARAM_COMPRESSION = 0x04,
157 SA_PARAM_BUFF_MODE = 0x08,
158 SA_PARAM_NUMBLOCKS = 0x10,
160 SA_PARAM_SPEED = 0x40,
165 SA_QUIRK_NONE = 0x00,
166 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
167 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
168 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
169 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
170 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
171 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
172 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
173 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
176 #define SAMODE(z) (dev2unit(z) & 0x3)
177 #define SADENSITY(z) ((dev2unit(z) >> 2) & 0x3)
178 #define SA_IS_CTRL(z) (dev2unit(z) & (1 << 4))
180 #define SA_NOT_CTLDEV 0
184 #define SA_ATYPE_NR 1
185 #define SA_ATYPE_ER 2
187 #define SAMINOR(ctl, mode, access) \
188 ((ctl << 4) | (mode << 2) | (access & 0x3))
190 #define SA_NUM_MODES 4
192 struct cdev *ctl_dev;
193 struct sa_mode_devs {
197 } mode_devs[SA_NUM_MODES];
204 struct bio_queue_head bio_queue;
206 struct devstat *device_stats;
213 u_int32_t comp_algorithm;
214 u_int32_t saved_comp_algorithm;
215 u_int32_t media_blksize;
216 u_int32_t last_media_blksize;
217 u_int32_t media_numblks;
218 u_int8_t media_density;
221 u_int8_t dsreg; /* mtio mt_dsreg, redux */
225 int last_resid_was_io;
228 * Relative to BOT Location.
237 struct scsi_sense_data _last_io_sense;
238 u_int64_t _last_io_resid;
239 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
240 struct scsi_sense_data _last_ctl_sense;
241 u_int64_t _last_ctl_resid;
242 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
243 #define last_io_sense errinfo._last_io_sense
244 #define last_io_resid errinfo._last_io_resid
245 #define last_io_cdb errinfo._last_io_cdb
246 #define last_ctl_sense errinfo._last_ctl_sense
247 #define last_ctl_resid errinfo._last_ctl_resid
248 #define last_ctl_cdb errinfo._last_ctl_cdb
251 * Misc other flags/state
255 open_rdonly : 1, /* open read-only */
256 open_pending_mount : 1, /* open pending mount */
257 ctrl_mode : 1; /* control device open */
260 struct sa_quirk_entry {
261 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
262 sa_quirks quirks; /* specific quirk type */
263 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
266 static struct sa_quirk_entry sa_quirk_table[] =
269 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
270 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
271 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
274 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
275 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
278 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
279 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
282 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
283 "Python*", "*"}, SA_QUIRK_NODREAD, 0
286 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
287 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
290 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
291 "VIPER 2525 25462", "-011"},
292 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
295 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
296 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
300 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
301 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
305 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
306 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
309 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
310 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
313 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
314 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
317 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
318 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
321 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
322 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
325 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
326 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
328 { /* jreynold@primenet.com */
329 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
330 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
332 { /* mike@sentex.net */
333 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
334 "STT20000*", "*"}, SA_QUIRK_1FM, 0
337 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "SEAGATE",
338 "DAT 06241-XXX", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 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, struct cdev *);
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 PERIPHDRIVER_DECLARE(sa, sadriver);
424 /* For 2.2-stable support */
430 static struct cdevsw sa_cdevsw = {
431 .d_version = D_VERSION,
435 .d_write = physwrite,
437 .d_strategy = sastrategy,
439 .d_flags = D_TAPE | D_NEEDGIANT,
443 saopen(struct cdev *dev, int flags, int fmt, struct thread *td)
445 struct cam_periph *periph;
446 struct sa_softc *softc;
449 periph = (struct cam_periph *)dev->si_drv1;
450 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
454 cam_periph_lock(periph);
456 softc = (struct sa_softc *)periph->softc;
458 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
459 ("saopen(%s): softc=0x%x\n", devtoname(dev), softc->flags));
461 if (SA_IS_CTRL(dev)) {
462 softc->ctrl_mode = 1;
463 cam_periph_unlock(periph);
467 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
468 cam_periph_unlock(periph);
469 cam_periph_release(periph);
473 if (softc->flags & SA_FLAG_OPEN) {
475 } else if (softc->flags & SA_FLAG_INVALID) {
479 * Preserve whether this is a read_only open.
481 softc->open_rdonly = (flags & O_RDWR) == O_RDONLY;
484 * The function samount ensures media is loaded and ready.
485 * It also does a device RESERVE if the tape isn't yet mounted.
487 * If the mount fails and this was a non-blocking open,
488 * make this a 'open_pending_mount' action.
490 error = samount(periph, flags, dev);
491 if (error && (flags & O_NONBLOCK)) {
492 softc->flags |= SA_FLAG_OPEN;
493 softc->open_pending_mount = 1;
494 cam_periph_unhold(periph);
495 cam_periph_unlock(periph);
501 cam_periph_unhold(periph);
502 cam_periph_unlock(periph);
503 cam_periph_release(periph);
507 saprevent(periph, PR_PREVENT);
508 softc->flags |= SA_FLAG_OPEN;
510 cam_periph_unhold(periph);
511 cam_periph_unlock(periph);
516 saclose(struct cdev *dev, int flag, int fmt, struct thread *td)
518 struct cam_periph *periph;
519 struct sa_softc *softc;
520 int mode, error, writing, tmp;
521 int closedbits = SA_FLAG_OPEN;
524 periph = (struct cam_periph *)dev->si_drv1;
528 cam_periph_lock(periph);
530 softc = (struct sa_softc *)periph->softc;
532 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
533 ("saclose(%s): softc=0x%x\n", devtoname(dev), softc->flags));
536 softc->open_rdonly = 0;
537 if (SA_IS_CTRL(dev)) {
538 softc->ctrl_mode = 0;
539 cam_periph_unlock(periph);
540 cam_periph_release(periph);
544 if (softc->open_pending_mount) {
545 softc->flags &= ~SA_FLAG_OPEN;
546 softc->open_pending_mount = 0;
547 cam_periph_unlock(periph);
548 cam_periph_release(periph);
552 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
553 cam_periph_unlock(periph);
558 * Were we writing the tape?
560 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
563 * See whether or not we need to write filemarks. If this
564 * fails, we probably have to assume we've lost tape
567 error = sacheckeod(periph);
569 xpt_print(periph->path,
570 "failed to write terminating filemark(s)\n");
571 softc->flags |= SA_FLAG_TAPE_FROZEN;
575 * Whatever we end up doing, allow users to eject tapes from here on.
577 saprevent(periph, PR_ALLOW);
580 * Decide how to end...
582 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
583 closedbits |= SA_FLAG_TAPE_FROZEN;
584 } else switch (mode) {
585 case SA_MODE_OFFLINE:
587 * An 'offline' close is an unconditional release of
588 * frozen && mount conditions, irrespective of whether
589 * these operations succeeded. The reason for this is
590 * to allow at least some kind of programmatic way
591 * around our state getting all fouled up. If somebody
592 * issues an 'offline' command, that will be allowed
595 (void) sarewind(periph);
596 (void) saloadunload(periph, FALSE);
597 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
601 * If the rewind fails, return an error- if anyone cares,
602 * but not overwriting any previous error.
604 * We don't clear the notion of mounted here, but we do
605 * clear the notion of frozen if we successfully rewound.
607 tmp = sarewind(periph);
612 closedbits |= SA_FLAG_TAPE_FROZEN;
615 case SA_MODE_NOREWIND:
617 * If we're not rewinding/unloading the tape, find out
618 * whether we need to back up over one of two filemarks
619 * we wrote (if we wrote two filemarks) so that appends
620 * from this point on will be sane.
622 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
623 tmp = saspace(periph, -1, SS_FILEMARKS);
625 xpt_print(periph->path, "unable to backspace "
626 "over one of double filemarks at end of "
628 xpt_print(periph->path, "it is possible that "
629 "this device needs a SA_QUIRK_1FM quirk set"
631 softc->flags |= SA_FLAG_TAPE_FROZEN;
636 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode);
642 * We wish to note here that there are no more filemarks to be written.
644 softc->filemarks = 0;
645 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
648 * And we are no longer open for business.
650 softc->flags &= ~closedbits;
653 * Inform users if tape state if frozen....
655 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
656 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, "
657 "REWIND or MTEOM command to clear this state.\n");
660 /* release the device if it is no longer mounted */
661 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
662 sareservereleaseunit(periph, FALSE);
664 cam_periph_unhold(periph);
665 cam_periph_unlock(periph);
666 cam_periph_release(periph);
672 * Actually translate the requested transfer into one the physical driver
673 * can understand. The transfer is described by a buf and will include
674 * only one physical transfer.
677 sastrategy(struct bio *bp)
679 struct cam_periph *periph;
680 struct sa_softc *softc;
682 bp->bio_resid = bp->bio_bcount;
683 if (SA_IS_CTRL(bp->bio_dev)) {
684 biofinish(bp, NULL, EINVAL);
687 periph = (struct cam_periph *)bp->bio_dev->si_drv1;
688 if (periph == NULL) {
689 biofinish(bp, NULL, ENXIO);
692 cam_periph_lock(periph);
694 softc = (struct sa_softc *)periph->softc;
696 if (softc->flags & SA_FLAG_INVALID) {
697 cam_periph_unlock(periph);
698 biofinish(bp, NULL, ENXIO);
702 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
703 cam_periph_unlock(periph);
704 biofinish(bp, NULL, EPERM);
709 * This should actually never occur as the write(2)
710 * system call traps attempts to write to a read-only
713 if (bp->bio_cmd == BIO_WRITE && softc->open_rdonly) {
714 cam_periph_unlock(periph);
715 biofinish(bp, NULL, EBADF);
719 if (softc->open_pending_mount) {
720 int error = samount(periph, 0, bp->bio_dev);
722 cam_periph_unlock(periph);
723 biofinish(bp, NULL, ENXIO);
726 saprevent(periph, PR_PREVENT);
727 softc->open_pending_mount = 0;
732 * If it's a null transfer, return immediately
734 if (bp->bio_bcount == 0) {
735 cam_periph_unlock(periph);
741 if (softc->flags & SA_FLAG_FIXED) {
743 * Fixed block device. The byte count must
744 * be a multiple of our block size.
746 if (((softc->blk_mask != ~0) &&
747 ((bp->bio_bcount & softc->blk_mask) != 0)) ||
748 ((softc->blk_mask == ~0) &&
749 ((bp->bio_bcount % softc->min_blk) != 0))) {
750 xpt_print(periph->path, "Invalid request. Fixed block "
751 "device requests must be a multiple of %d bytes\n",
753 cam_periph_unlock(periph);
754 biofinish(bp, NULL, EINVAL);
757 } else if ((bp->bio_bcount > softc->max_blk) ||
758 (bp->bio_bcount < softc->min_blk) ||
759 (bp->bio_bcount & softc->blk_mask) != 0) {
761 xpt_print_path(periph->path);
762 printf("Invalid request. Variable block "
763 "device requests must be ");
764 if (softc->blk_mask != 0) {
765 printf("a multiple of %d ", (0x1 << softc->blk_gran));
767 printf("between %d and %d bytes\n", softc->min_blk,
769 cam_periph_unlock(periph);
770 biofinish(bp, NULL, EINVAL);
775 * Place it at the end of the queue.
777 bioq_insert_tail(&softc->bio_queue, bp);
778 softc->queue_count++;
780 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
781 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
782 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
783 (bp->bio_cmd == BIO_READ)? "read" : "write"));
785 if (softc->queue_count > 1) {
786 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
787 ("sastrategy: queue count now %d\n", softc->queue_count));
791 * Schedule ourselves for performing the work.
793 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
794 cam_periph_unlock(periph);
800 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
801 if (softc->open_pending_mount) { \
802 error = samount(periph, 0, dev); \
806 saprevent(periph, PR_PREVENT); \
807 softc->open_pending_mount = 0; \
811 saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
813 struct cam_periph *periph;
814 struct sa_softc *softc;
815 scsi_space_code spaceop;
816 int didlockperiph = 0;
821 error = 0; /* shut up gcc */
822 spaceop = 0; /* shut up gcc */
824 periph = (struct cam_periph *)dev->si_drv1;
828 cam_periph_lock(periph);
829 softc = (struct sa_softc *)periph->softc;
832 * Check for control mode accesses. We allow MTIOCGET and
833 * MTIOCERRSTAT (but need to be the only one open in order
834 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
835 * and MTCOMP (but need to be the only one accessing this
836 * device to run those).
839 if (SA_IS_CTRL(dev)) {
841 case MTIOCGETEOTMODEL:
846 * If the periph isn't already locked, lock it
847 * so our MTIOCERRSTAT can reset latched error stats.
849 * If the periph is already locked, skip it because
850 * we're just getting status and it'll be up to the
851 * other thread that has this device open to do
852 * an MTIOCERRSTAT that would clear latched status.
854 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
855 error = cam_periph_hold(periph, PRIBIO|PCATCH);
857 cam_periph_unlock(periph);
866 struct mtop *mt = (struct mtop *) arg;
869 * Check to make sure it's an OP we can perform
870 * with no media inserted.
886 case MTIOCSETEOTMODEL:
888 * We need to acquire the peripheral here rather
889 * than at open time because we are sharing writable
890 * access to data structures.
892 error = cam_periph_hold(periph, PRIBIO|PCATCH);
894 cam_periph_unlock(periph);
901 cam_periph_unlock(periph);
907 * Find the device that the user is talking about
912 struct mtget *g = (struct mtget *)arg;
915 * If this isn't the control mode device, actually go out
916 * and ask the drive again what it's set to.
918 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) {
919 u_int8_t write_protect;
920 int comp_enabled, comp_supported;
921 error = sagetparams(periph, SA_PARAM_ALL,
922 &softc->media_blksize, &softc->media_density,
923 &softc->media_numblks, &softc->buffer_mode,
924 &write_protect, &softc->speed, &comp_supported,
925 &comp_enabled, &softc->comp_algorithm, NULL);
929 softc->flags |= SA_FLAG_TAPE_WP;
931 softc->flags &= ~SA_FLAG_TAPE_WP;
932 softc->flags &= ~(SA_FLAG_COMP_SUPP|
933 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
934 if (comp_supported) {
935 if (softc->saved_comp_algorithm == 0)
936 softc->saved_comp_algorithm =
937 softc->comp_algorithm;
938 softc->flags |= SA_FLAG_COMP_SUPP;
940 softc->flags |= SA_FLAG_COMP_ENABLED;
942 softc->flags |= SA_FLAG_COMP_UNSUPP;
944 bzero(g, sizeof(struct mtget));
945 g->mt_type = MT_ISAR;
946 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
947 g->mt_comp = MT_COMP_UNSUPP;
948 g->mt_comp0 = MT_COMP_UNSUPP;
949 g->mt_comp1 = MT_COMP_UNSUPP;
950 g->mt_comp2 = MT_COMP_UNSUPP;
951 g->mt_comp3 = MT_COMP_UNSUPP;
953 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
954 g->mt_comp = MT_COMP_DISABLED;
956 g->mt_comp = softc->comp_algorithm;
958 g->mt_comp0 = softc->comp_algorithm;
959 g->mt_comp1 = softc->comp_algorithm;
960 g->mt_comp2 = softc->comp_algorithm;
961 g->mt_comp3 = softc->comp_algorithm;
963 g->mt_density = softc->media_density;
964 g->mt_density0 = softc->media_density;
965 g->mt_density1 = softc->media_density;
966 g->mt_density2 = softc->media_density;
967 g->mt_density3 = softc->media_density;
968 g->mt_blksiz = softc->media_blksize;
969 g->mt_blksiz0 = softc->media_blksize;
970 g->mt_blksiz1 = softc->media_blksize;
971 g->mt_blksiz2 = softc->media_blksize;
972 g->mt_blksiz3 = softc->media_blksize;
973 g->mt_fileno = softc->fileno;
974 g->mt_blkno = softc->blkno;
975 g->mt_dsreg = (short) softc->dsreg;
977 * Yes, we know that this is likely to overflow
979 if (softc->last_resid_was_io) {
980 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
981 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
982 softc->last_io_resid = 0;
986 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
987 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
988 softc->last_ctl_resid = 0;
997 struct scsi_tape_errors *sep =
998 &((union mterrstat *)arg)->scsi_errstat;
1000 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1001 ("saioctl: MTIOCERRSTAT\n"));
1003 bzero(sep, sizeof(*sep));
1004 sep->io_resid = softc->last_io_resid;
1005 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1006 sizeof (sep->io_sense));
1007 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1008 sizeof (sep->io_cdb));
1009 sep->ctl_resid = softc->last_ctl_resid;
1010 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1011 sizeof (sep->ctl_sense));
1012 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1013 sizeof (sep->ctl_cdb));
1015 if ((SA_IS_CTRL(dev) == 0 && softc->open_pending_mount) ||
1017 bzero((caddr_t) &softc->errinfo,
1018 sizeof (softc->errinfo));
1027 PENDING_MOUNT_CHECK(softc, periph, dev);
1029 mt = (struct mtop *)arg;
1032 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1033 ("saioctl: op=0x%x count=0x%x\n",
1034 mt->mt_op, mt->mt_count));
1036 count = mt->mt_count;
1037 switch (mt->mt_op) {
1038 case MTWEOF: /* write an end-of-file marker */
1040 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1041 * flag because by keeping track of filemarks
1042 * we have last written we know ehether or not
1043 * we need to write more when we close the device.
1045 error = sawritefilemarks(periph, count, FALSE);
1047 case MTWSS: /* write a setmark */
1048 error = sawritefilemarks(periph, count, TRUE);
1050 case MTBSR: /* backward space record */
1051 case MTFSR: /* forward space record */
1052 case MTBSF: /* backward space file */
1053 case MTFSF: /* forward space file */
1054 case MTBSS: /* backward space setmark */
1055 case MTFSS: /* forward space setmark */
1056 case MTEOD: /* space to end of recorded medium */
1060 spaceop = SS_FILEMARKS;
1061 nmarks = softc->filemarks;
1062 error = sacheckeod(periph);
1064 xpt_print(periph->path,
1065 "EOD check prior to spacing failed\n");
1066 softc->flags |= SA_FLAG_EIO_PENDING;
1069 nmarks -= softc->filemarks;
1075 spaceop = SS_BLOCKS;
1086 spaceop = SS_SETMARKS;
1100 nmarks = softc->filemarks;
1102 * XXX: Why are we checking again?
1104 error = sacheckeod(periph);
1107 nmarks -= softc->filemarks;
1108 error = saspace(periph, count - nmarks, spaceop);
1110 * At this point, clear that we've written the tape
1111 * and that we've written any filemarks. We really
1112 * don't know what the applications wishes to do next-
1113 * the sacheckeod's will make sure we terminated the
1114 * tape correctly if we'd been writing, but the next
1115 * action the user application takes will set again
1116 * whether we need to write filemarks.
1119 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1120 softc->filemarks = 0;
1123 case MTREW: /* rewind */
1124 PENDING_MOUNT_CHECK(softc, periph, dev);
1125 (void) sacheckeod(periph);
1126 error = sarewind(periph);
1129 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1130 softc->flags &= ~SA_FLAG_ERR_PENDING;
1131 softc->filemarks = 0;
1133 case MTERASE: /* erase */
1134 PENDING_MOUNT_CHECK(softc, periph, dev);
1135 error = saerase(periph, count);
1137 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1138 softc->flags &= ~SA_FLAG_ERR_PENDING;
1140 case MTRETENS: /* re-tension tape */
1141 PENDING_MOUNT_CHECK(softc, periph, dev);
1142 error = saretension(periph);
1144 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1145 softc->flags &= ~SA_FLAG_ERR_PENDING;
1147 case MTOFFL: /* rewind and put the drive offline */
1149 PENDING_MOUNT_CHECK(softc, periph, dev);
1151 (void) sacheckeod(periph);
1153 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1154 softc->filemarks = 0;
1156 error = sarewind(periph);
1157 /* clear the frozen flag anyway */
1158 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1161 * Be sure to allow media removal before ejecting.
1164 saprevent(periph, PR_ALLOW);
1166 error = saloadunload(periph, FALSE);
1168 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1173 case MTNOP: /* no operation, sets status only */
1174 case MTCACHE: /* enable controller cache */
1175 case MTNOCACHE: /* disable controller cache */
1179 case MTSETBSIZ: /* Set block size for device */
1181 PENDING_MOUNT_CHECK(softc, periph, dev);
1183 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1186 softc->last_media_blksize =
1187 softc->media_blksize;
1188 softc->media_blksize = count;
1190 softc->flags |= SA_FLAG_FIXED;
1191 if (powerof2(count)) {
1194 softc->blk_mask = count - 1;
1196 softc->blk_mask = ~0;
1197 softc->blk_shift = 0;
1200 * Make the user's desire 'persistent'.
1202 softc->quirks &= ~SA_QUIRK_VARIABLE;
1203 softc->quirks |= SA_QUIRK_FIXED;
1205 softc->flags &= ~SA_FLAG_FIXED;
1206 if (softc->max_blk == 0) {
1207 softc->max_blk = ~0;
1209 softc->blk_shift = 0;
1210 if (softc->blk_gran != 0) {
1212 softc->blk_gran - 1;
1214 softc->blk_mask = 0;
1217 * Make the user's desire 'persistent'.
1219 softc->quirks |= SA_QUIRK_VARIABLE;
1220 softc->quirks &= ~SA_QUIRK_FIXED;
1224 case MTSETDNSTY: /* Set density for device and mode */
1225 PENDING_MOUNT_CHECK(softc, periph, dev);
1227 if (count > UCHAR_MAX) {
1231 error = sasetparams(periph, SA_PARAM_DENSITY,
1235 case MTCOMP: /* enable compression */
1236 PENDING_MOUNT_CHECK(softc, periph, dev);
1238 * Some devices don't support compression, and
1239 * don't like it if you ask them for the
1242 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1243 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1247 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1248 0, 0, count, SF_NO_PRINT);
1260 PENDING_MOUNT_CHECK(softc, periph, dev);
1261 error = sardpos(periph, 0, (u_int32_t *) arg);
1264 PENDING_MOUNT_CHECK(softc, periph, dev);
1265 error = sardpos(periph, 1, (u_int32_t *) arg);
1268 PENDING_MOUNT_CHECK(softc, periph, dev);
1269 error = sasetpos(periph, 0, (u_int32_t *) arg);
1272 PENDING_MOUNT_CHECK(softc, periph, dev);
1273 error = sasetpos(periph, 1, (u_int32_t *) arg);
1275 case MTIOCGETEOTMODEL:
1277 if (softc->quirks & SA_QUIRK_1FM)
1281 *((u_int32_t *) arg) = mode;
1283 case MTIOCSETEOTMODEL:
1285 switch (*((u_int32_t *) arg)) {
1287 softc->quirks &= ~SA_QUIRK_2FM;
1288 softc->quirks |= SA_QUIRK_1FM;
1291 softc->quirks &= ~SA_QUIRK_1FM;
1292 softc->quirks |= SA_QUIRK_2FM;
1300 error = cam_periph_ioctl(periph, cmd, arg, saerror);
1305 * Check to see if we cleared a frozen state
1307 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1313 softc->fileno = (daddr_t) -1;
1314 softc->blkno = (daddr_t) -1;
1315 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1316 xpt_print(periph->path,
1317 "tape state now unfrozen.\n");
1323 if (didlockperiph) {
1324 cam_periph_unhold(periph);
1326 cam_periph_unlock(periph);
1336 * Install a global async callback.
1338 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL);
1340 if (status != CAM_REQ_CMP) {
1341 printf("sa: Failed to attach master async callback "
1342 "due to status 0x%x!\n", status);
1347 saoninvalidate(struct cam_periph *periph)
1349 struct sa_softc *softc;
1351 softc = (struct sa_softc *)periph->softc;
1354 * De-register any async callbacks.
1356 xpt_register_async(0, saasync, periph, periph->path);
1358 softc->flags |= SA_FLAG_INVALID;
1361 * Return all queued I/O with ENXIO.
1362 * XXX Handle any transactions queued to the card
1363 * with XPT_ABORT_CCB.
1365 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1366 softc->queue_count = 0;
1368 xpt_print(periph->path, "lost device\n");
1373 sacleanup(struct cam_periph *periph)
1375 struct sa_softc *softc;
1378 softc = (struct sa_softc *)periph->softc;
1380 xpt_print(periph->path, "removing device entry\n");
1381 devstat_remove_entry(softc->device_stats);
1382 cam_periph_unlock(periph);
1383 destroy_dev(softc->devs.ctl_dev);
1384 for (i = 0; i < SA_NUM_MODES; i++) {
1385 destroy_dev(softc->devs.mode_devs[i].r_dev);
1386 destroy_dev(softc->devs.mode_devs[i].nr_dev);
1387 destroy_dev(softc->devs.mode_devs[i].er_dev);
1389 cam_periph_lock(periph);
1390 free(softc, M_SCSISA);
1394 saasync(void *callback_arg, u_int32_t code,
1395 struct cam_path *path, void *arg)
1397 struct cam_periph *periph;
1399 periph = (struct cam_periph *)callback_arg;
1401 case AC_FOUND_DEVICE:
1403 struct ccb_getdev *cgd;
1406 cgd = (struct ccb_getdev *)arg;
1410 if (cgd->protocol != PROTO_SCSI)
1413 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1417 * Allocate a peripheral instance for
1418 * this device and start the probe
1421 status = cam_periph_alloc(saregister, saoninvalidate,
1423 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1424 saasync, AC_FOUND_DEVICE, cgd);
1426 if (status != CAM_REQ_CMP
1427 && status != CAM_REQ_INPROG)
1428 printf("saasync: Unable to probe new device "
1429 "due to status 0x%x\n", status);
1433 cam_periph_async(periph, code, path, arg);
1439 saregister(struct cam_periph *periph, void *arg)
1441 struct sa_softc *softc;
1442 struct ccb_getdev *cgd;
1443 struct ccb_pathinq cpi;
1447 cgd = (struct ccb_getdev *)arg;
1449 printf("saregister: no getdev CCB, can't register device\n");
1450 return (CAM_REQ_CMP_ERR);
1453 softc = (struct sa_softc *)
1454 malloc(sizeof (*softc), M_SCSISA, M_NOWAIT | M_ZERO);
1455 if (softc == NULL) {
1456 printf("saregister: Unable to probe new device. "
1457 "Unable to allocate softc\n");
1458 return (CAM_REQ_CMP_ERR);
1460 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1461 softc->state = SA_STATE_NORMAL;
1462 softc->fileno = (daddr_t) -1;
1463 softc->blkno = (daddr_t) -1;
1465 bioq_init(&softc->bio_queue);
1466 periph->softc = softc;
1469 * See if this device has any quirks.
1471 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1472 (caddr_t)sa_quirk_table,
1473 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1474 sizeof(*sa_quirk_table), scsi_inquiry_match);
1476 if (match != NULL) {
1477 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1478 softc->last_media_blksize =
1479 ((struct sa_quirk_entry *)match)->prefblk;
1481 softc->quirks = SA_QUIRK_NONE;
1483 bzero(&cpi, sizeof(cpi));
1484 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1485 cpi.ccb_h.func_code = XPT_PATH_INQ;
1486 xpt_action((union ccb *)&cpi);
1489 * The SA driver supports a blocksize, but we don't know the
1490 * blocksize until we media is inserted. So, set a flag to
1491 * indicate that the blocksize is unavailable right now.
1493 cam_periph_unlock(periph);
1494 softc->device_stats = devstat_new_entry("sa", periph->unit_number, 0,
1495 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1496 XPORT_DEVSTAT_TYPE(cpi.transport), DEVSTAT_PRIORITY_TAPE);
1498 softc->devs.ctl_dev = make_dev(&sa_cdevsw, SAMINOR(SA_CTLDEV,
1499 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1500 0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1501 softc->devs.ctl_dev->si_drv1 = periph;
1503 for (i = 0; i < SA_NUM_MODES; i++) {
1505 softc->devs.mode_devs[i].r_dev = make_dev(&sa_cdevsw,
1506 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_R),
1507 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1508 periph->periph_name, periph->unit_number, i);
1509 softc->devs.mode_devs[i].r_dev->si_drv1 = periph;
1511 softc->devs.mode_devs[i].nr_dev = make_dev(&sa_cdevsw,
1512 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_NR),
1513 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1514 periph->periph_name, periph->unit_number, i);
1515 softc->devs.mode_devs[i].nr_dev->si_drv1 = periph;
1517 softc->devs.mode_devs[i].er_dev = make_dev(&sa_cdevsw,
1518 SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_ER),
1519 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1520 periph->periph_name, periph->unit_number, i);
1521 softc->devs.mode_devs[i].er_dev->si_drv1 = periph;
1524 * Make the (well known) aliases for the first mode.
1529 alias = make_dev_alias(softc->devs.mode_devs[i].r_dev,
1530 "%s%d", periph->periph_name, periph->unit_number);
1531 alias->si_drv1 = periph;
1532 alias = make_dev_alias(softc->devs.mode_devs[i].nr_dev,
1533 "n%s%d", periph->periph_name, periph->unit_number);
1534 alias->si_drv1 = periph;
1535 alias = make_dev_alias(softc->devs.mode_devs[i].er_dev,
1536 "e%s%d", periph->periph_name, periph->unit_number);
1537 alias->si_drv1 = periph;
1540 cam_periph_lock(periph);
1543 * Add an async callback so that we get
1544 * notified if this device goes away.
1546 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path);
1548 xpt_announce_periph(periph, NULL);
1550 return (CAM_REQ_CMP);
1554 sastart(struct cam_periph *periph, union ccb *start_ccb)
1556 struct sa_softc *softc;
1558 softc = (struct sa_softc *)periph->softc;
1560 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
1563 switch (softc->state) {
1564 case SA_STATE_NORMAL:
1566 /* Pull a buffer from the queue and get going on it */
1570 * See if there is a buf with work for us to do..
1572 bp = bioq_first(&softc->bio_queue);
1573 if (periph->immediate_priority <= periph->pinfo.priority) {
1574 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1575 ("queuing for immediate ccb\n"));
1576 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1577 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1579 periph->immediate_priority = CAM_PRIORITY_NONE;
1580 wakeup(&periph->ccb_list);
1581 } else if (bp == NULL) {
1582 xpt_release_ccb(start_ccb);
1583 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1584 struct bio *done_bp;
1586 softc->queue_count--;
1587 bioq_remove(&softc->bio_queue, bp);
1588 bp->bio_resid = bp->bio_bcount;
1590 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1592 * We now just clear errors in this case
1593 * and let the residual be the notifier.
1596 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1598 * This can only happen if we're reading
1599 * in fixed length mode. In this case,
1600 * we dump the rest of the list the
1604 if (bioq_first(&softc->bio_queue) != NULL) {
1608 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1609 bp->bio_error = EIO;
1610 bp->bio_flags |= BIO_ERROR;
1612 bp = bioq_first(&softc->bio_queue);
1614 * Only if we have no other buffers queued up
1615 * do we clear the pending error flag.
1618 softc->flags &= ~SA_FLAG_ERR_PENDING;
1619 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1620 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
1621 "%d more buffers queued up\n",
1622 (softc->flags & SA_FLAG_ERR_PENDING),
1623 (bp != NULL)? "not " : " ", softc->queue_count));
1624 xpt_release_ccb(start_ccb);
1629 bioq_remove(&softc->bio_queue, bp);
1630 softc->queue_count--;
1632 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1633 if (softc->blk_shift != 0) {
1635 bp->bio_bcount >> softc->blk_shift;
1636 } else if (softc->media_blksize != 0) {
1637 length = bp->bio_bcount /
1638 softc->media_blksize;
1640 bp->bio_error = EIO;
1641 xpt_print(periph->path, "zero blocksize"
1642 " for FIXED length writes?\n");
1647 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1648 ("issuing a %d fixed record %s\n",
1649 length, (bp->bio_cmd == BIO_READ)? "read" :
1653 length = bp->bio_bcount;
1655 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1656 ("issuing a %d variable byte %s\n",
1657 length, (bp->bio_cmd == BIO_READ)? "read" :
1661 devstat_start_transaction_bio(softc->device_stats, bp);
1663 * Some people have theorized that we should
1664 * suppress illegal length indication if we are
1665 * running in variable block mode so that we don't
1666 * have to request sense every time our requested
1667 * block size is larger than the written block.
1668 * The residual information from the ccb allows
1669 * us to identify this situation anyway. The only
1670 * problem with this is that we will not get
1671 * information about blocks that are larger than
1672 * our read buffer unless we set the block size
1673 * in the mode page to something other than 0.
1675 * I believe that this is a non-issue. If user apps
1676 * don't adjust their read size to match our record
1677 * size, that's just life. Anyway, the typical usage
1678 * would be to issue, e.g., 64KB reads and occasionally
1679 * have to do deal with 512 byte or 1KB intermediate
1682 softc->dsreg = (bp->bio_cmd == BIO_READ)?
1683 MTIO_DSREG_RD : MTIO_DSREG_WR;
1684 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1685 MSG_SIMPLE_Q_TAG, (bp->bio_cmd == BIO_READ),
1686 FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1687 length, bp->bio_data, bp->bio_bcount, SSD_FULL_SIZE,
1689 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1690 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1691 start_ccb->ccb_h.ccb_bp = bp;
1692 bp = bioq_first(&softc->bio_queue);
1693 xpt_action(start_ccb);
1697 /* Have more work to do, so ensure we stay scheduled */
1698 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1702 case SA_STATE_ABNORMAL:
1704 panic("state 0x%x in sastart", softc->state);
1711 sadone(struct cam_periph *periph, union ccb *done_ccb)
1713 struct sa_softc *softc;
1714 struct ccb_scsiio *csio;
1716 softc = (struct sa_softc *)periph->softc;
1717 csio = &done_ccb->csio;
1718 switch (CCB_Type(csio)) {
1719 case SA_CCB_BUFFER_IO:
1724 softc->dsreg = MTIO_DSREG_REST;
1725 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1727 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1728 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1730 * A retry was scheduled, so just return.
1739 * Catastrophic error. Mark the tape as frozen
1740 * (we no longer know tape position).
1742 * Return all queued I/O with EIO, and unfreeze
1743 * our queue so that future transactions that
1744 * attempt to fix this problem can get to the
1749 softc->flags |= SA_FLAG_TAPE_FROZEN;
1750 bioq_flush(&softc->bio_queue, NULL, EIO);
1753 bp->bio_resid = bp->bio_bcount;
1754 bp->bio_error = error;
1755 bp->bio_flags |= BIO_ERROR;
1757 * In the error case, position is updated in saerror.
1760 bp->bio_resid = csio->resid;
1762 if (csio->resid != 0) {
1763 bp->bio_flags |= BIO_ERROR;
1765 if (bp->bio_cmd == BIO_WRITE) {
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) {
1774 l = bp->bio_bcount >>
1777 l = bp->bio_bcount /
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);
1792 if (error || bp->bio_resid) {
1793 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1794 ("error %d resid %ld count %ld\n", error,
1795 bp->bio_resid, bp->bio_bcount));
1797 biofinish(bp, softc->device_stats, 0);
1800 case SA_CCB_WAITING:
1802 /* Caller will release the CCB */
1803 wakeup(&done_ccb->ccb_h.cbfcnp);
1807 xpt_release_ccb(done_ccb);
1811 * Mount the tape (make sure it's ready for I/O).
1814 samount(struct cam_periph *periph, int oflags, struct cdev *dev)
1816 struct sa_softc *softc;
1821 * oflags can be checked for 'kind' of open (read-only check) - later
1822 * dev can be checked for a control-mode or compression open - later
1824 UNUSED_PARAMETER(oflags);
1825 UNUSED_PARAMETER(dev);
1828 softc = (struct sa_softc *)periph->softc;
1831 * This should determine if something has happend since the last
1832 * open/mount that would invalidate the mount. We do *not* want
1833 * to retry this command- we just want the status. But we only
1834 * do this if we're mounted already- if we're not mounted,
1835 * we don't care about the unit read state and can instead use
1836 * this opportunity to attempt to reserve the tape unit.
1839 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1840 ccb = cam_periph_getccb(periph, 1);
1841 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1842 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1843 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1844 softc->device_stats);
1845 if (error == ENXIO) {
1846 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1847 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1848 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1849 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1850 softc->device_stats);
1853 * We don't need to freeze the tape because we
1854 * will now attempt to rewind/load it.
1856 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1857 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
1858 xpt_print(periph->path,
1859 "error %d on TUR in samount\n", error);
1863 error = sareservereleaseunit(periph, TRUE);
1867 ccb = cam_periph_getccb(periph, 1);
1868 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1869 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1870 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1871 softc->device_stats);
1874 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1875 struct scsi_read_block_limits_data *rblim = NULL;
1876 int comp_enabled, comp_supported;
1877 u_int8_t write_protect, guessing = 0;
1880 * Clear out old state.
1882 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1883 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1884 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1885 softc->filemarks = 0;
1888 * *Very* first off, make sure we're loaded to BOT.
1890 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1891 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1892 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1893 softc->device_stats);
1896 * In case this doesn't work, do a REWIND instead
1899 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1900 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1901 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1902 softc->device_stats);
1905 xpt_release_ccb(ccb);
1910 * Do a dummy test read to force access to the
1911 * media so that the drive will really know what's
1912 * there. We actually don't really care what the
1913 * blocksize on tape is and don't expect to really
1914 * read a full record.
1916 rblim = (struct scsi_read_block_limits_data *)
1917 malloc(8192, M_SCSISA, M_NOWAIT);
1918 if (rblim == NULL) {
1919 xpt_print(periph->path, "no memory for test read\n");
1920 xpt_release_ccb(ccb);
1925 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1926 scsi_sa_read_write(&ccb->csio, 0, sadone,
1927 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1928 (void *) rblim, 8192, SSD_FULL_SIZE,
1930 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1931 softc->device_stats);
1932 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1933 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1934 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1935 SF_NO_PRINT | SF_RETRY_UA,
1936 softc->device_stats);
1938 xpt_print(periph->path,
1939 "unable to rewind after test read\n");
1940 xpt_release_ccb(ccb);
1946 * Next off, determine block limits.
1948 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1949 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1951 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1952 SF_NO_PRINT | SF_RETRY_UA, softc->device_stats);
1954 xpt_release_ccb(ccb);
1958 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1959 * a MANDATORY command. Anyway- it doesn't matter-
1960 * we can proceed anyway.
1962 softc->blk_gran = 0;
1963 softc->max_blk = ~0;
1966 if (softc->scsi_rev >= SCSI_REV_SPC) {
1967 softc->blk_gran = RBL_GRAN(rblim);
1969 softc->blk_gran = 0;
1972 * We take max_blk == min_blk to mean a default to
1973 * fixed mode- but note that whatever we get out of
1974 * sagetparams below will actually determine whether
1975 * we are actually *in* fixed mode.
1977 softc->max_blk = scsi_3btoul(rblim->maximum);
1978 softc->min_blk = scsi_2btoul(rblim->minimum);
1983 * Next, perform a mode sense to determine
1984 * current density, blocksize, compression etc.
1986 error = sagetparams(periph, SA_PARAM_ALL,
1987 &softc->media_blksize,
1988 &softc->media_density,
1989 &softc->media_numblks,
1990 &softc->buffer_mode, &write_protect,
1991 &softc->speed, &comp_supported,
1992 &comp_enabled, &softc->comp_algorithm,
1997 * We could work a little harder here. We could
1998 * adjust our attempts to get information. It
1999 * might be an ancient tape drive. If someone
2000 * nudges us, we'll do that.
2006 * If no quirk has determined that this is a device that is
2007 * preferred to be in fixed or variable mode, now is the time
2010 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2013 * This could be expensive to find out. Luckily we
2014 * only need to do this once. If we start out in
2015 * 'default' mode, try and set ourselves to one
2016 * of the densities that would determine a wad
2017 * of other stuff. Go from highest to lowest.
2019 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2021 static u_int8_t ctry[] = {
2022 SCSI_DENSITY_HALFINCH_PE,
2023 SCSI_DENSITY_HALFINCH_6250C,
2024 SCSI_DENSITY_HALFINCH_6250,
2025 SCSI_DENSITY_HALFINCH_1600,
2026 SCSI_DENSITY_HALFINCH_800,
2027 SCSI_DENSITY_QIC_4GB,
2028 SCSI_DENSITY_QIC_2GB,
2029 SCSI_DENSITY_QIC_525_320,
2030 SCSI_DENSITY_QIC_150,
2031 SCSI_DENSITY_QIC_120,
2032 SCSI_DENSITY_QIC_24,
2033 SCSI_DENSITY_QIC_11_9TRK,
2034 SCSI_DENSITY_QIC_11_4TRK,
2035 SCSI_DENSITY_QIC_1320,
2036 SCSI_DENSITY_QIC_3080,
2039 for (i = 0; ctry[i]; i++) {
2040 error = sasetparams(periph,
2041 SA_PARAM_DENSITY, 0, ctry[i],
2044 softc->media_density = ctry[i];
2049 switch (softc->media_density) {
2050 case SCSI_DENSITY_QIC_11_4TRK:
2051 case SCSI_DENSITY_QIC_11_9TRK:
2052 case SCSI_DENSITY_QIC_24:
2053 case SCSI_DENSITY_QIC_120:
2054 case SCSI_DENSITY_QIC_150:
2055 case SCSI_DENSITY_QIC_525_320:
2056 case SCSI_DENSITY_QIC_1320:
2057 case SCSI_DENSITY_QIC_3080:
2058 softc->quirks &= ~SA_QUIRK_2FM;
2059 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2060 softc->last_media_blksize = 512;
2062 case SCSI_DENSITY_QIC_4GB:
2063 case SCSI_DENSITY_QIC_2GB:
2064 softc->quirks &= ~SA_QUIRK_2FM;
2065 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2066 softc->last_media_blksize = 1024;
2069 softc->last_media_blksize =
2070 softc->media_blksize;
2071 softc->quirks |= SA_QUIRK_VARIABLE;
2077 * If no quirk has determined that this is a device that needs
2078 * to have 2 Filemarks at EOD, now is the time to find out.
2081 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2082 switch (softc->media_density) {
2083 case SCSI_DENSITY_HALFINCH_800:
2084 case SCSI_DENSITY_HALFINCH_1600:
2085 case SCSI_DENSITY_HALFINCH_6250:
2086 case SCSI_DENSITY_HALFINCH_6250C:
2087 case SCSI_DENSITY_HALFINCH_PE:
2088 softc->quirks &= ~SA_QUIRK_1FM;
2089 softc->quirks |= SA_QUIRK_2FM;
2097 * Now validate that some info we got makes sense.
2099 if ((softc->max_blk < softc->media_blksize) ||
2100 (softc->min_blk > softc->media_blksize &&
2101 softc->media_blksize)) {
2102 xpt_print(periph->path,
2103 "BLOCK LIMITS (%d..%d) could not match current "
2104 "block settings (%d)- adjusting\n", softc->min_blk,
2105 softc->max_blk, softc->media_blksize);
2106 softc->max_blk = softc->min_blk =
2107 softc->media_blksize;
2111 * Now put ourselves into the right frame of mind based
2116 * If we want to be in FIXED mode and our current blocksize
2117 * is not equal to our last blocksize (if nonzero), try and
2118 * set ourselves to this last blocksize (as the 'preferred'
2119 * block size). The initial quirkmatch at registry sets the
2120 * initial 'last' blocksize. If, for whatever reason, this
2121 * 'last' blocksize is zero, set the blocksize to 512,
2122 * or min_blk if that's larger.
2124 if ((softc->quirks & SA_QUIRK_FIXED) &&
2125 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2126 (softc->media_blksize != softc->last_media_blksize)) {
2127 softc->media_blksize = softc->last_media_blksize;
2128 if (softc->media_blksize == 0) {
2129 softc->media_blksize = 512;
2130 if (softc->media_blksize < softc->min_blk) {
2131 softc->media_blksize = softc->min_blk;
2134 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2135 softc->media_blksize, 0, 0, SF_NO_PRINT);
2137 xpt_print(periph->path,
2138 "unable to set fixed blocksize to %d\n",
2139 softc->media_blksize);
2144 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2145 (softc->media_blksize != 0)) {
2146 softc->last_media_blksize = softc->media_blksize;
2147 softc->media_blksize = 0;
2148 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2149 0, 0, 0, SF_NO_PRINT);
2152 * If this fails and we were guessing, just
2153 * assume that we got it wrong and go try
2154 * fixed block mode. Don't even check against
2155 * density code at this point.
2158 softc->quirks &= ~SA_QUIRK_VARIABLE;
2159 softc->quirks |= SA_QUIRK_FIXED;
2160 if (softc->last_media_blksize == 0)
2161 softc->last_media_blksize = 512;
2164 xpt_print(periph->path,
2165 "unable to set variable blocksize\n");
2171 * Now that we have the current block size,
2172 * set up some parameters for sastart's usage.
2174 if (softc->media_blksize) {
2175 softc->flags |= SA_FLAG_FIXED;
2176 if (powerof2(softc->media_blksize)) {
2178 ffs(softc->media_blksize) - 1;
2179 softc->blk_mask = softc->media_blksize - 1;
2181 softc->blk_mask = ~0;
2182 softc->blk_shift = 0;
2186 * The SCSI-3 spec allows 0 to mean "unspecified".
2187 * The SCSI-1 spec allows 0 to mean 'infinite'.
2189 * Either works here.
2191 if (softc->max_blk == 0) {
2192 softc->max_blk = ~0;
2194 softc->blk_shift = 0;
2195 if (softc->blk_gran != 0) {
2196 softc->blk_mask = softc->blk_gran - 1;
2198 softc->blk_mask = 0;
2203 softc->flags |= SA_FLAG_TAPE_WP;
2205 if (comp_supported) {
2206 if (softc->saved_comp_algorithm == 0)
2207 softc->saved_comp_algorithm =
2208 softc->comp_algorithm;
2209 softc->flags |= SA_FLAG_COMP_SUPP;
2211 softc->flags |= SA_FLAG_COMP_ENABLED;
2213 softc->flags |= SA_FLAG_COMP_UNSUPP;
2215 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2216 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2217 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2220 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2222 xpt_print(periph->path,
2223 "unable to set buffered mode\n");
2225 error = 0; /* not an error */
2230 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2234 free(rblim, M_SCSISA);
2237 softc->dsreg = MTIO_DSREG_NIL;
2239 softc->fileno = softc->blkno = 0;
2240 softc->dsreg = MTIO_DSREG_REST;
2242 #ifdef SA_1FM_AT_EOD
2243 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2244 softc->quirks |= SA_QUIRK_1FM;
2246 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2247 softc->quirks |= SA_QUIRK_2FM;
2250 xpt_release_ccb(ccb);
2253 * If we return an error, we're not mounted any more,
2254 * so release any device reservation.
2257 (void) sareservereleaseunit(periph, FALSE);
2260 * Clear I/O residual.
2262 softc->last_io_resid = 0;
2263 softc->last_ctl_resid = 0;
2269 * How many filemarks do we need to write if we were to terminate the
2270 * tape session right now? Note that this can be a negative number
2274 samarkswanted(struct cam_periph *periph)
2277 struct sa_softc *softc;
2279 softc = (struct sa_softc *)periph->softc;
2281 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2283 if (softc->quirks & SA_QUIRK_2FM)
2286 markswanted -= softc->filemarks;
2287 return (markswanted);
2291 sacheckeod(struct cam_periph *periph)
2296 markswanted = samarkswanted(periph);
2298 if (markswanted > 0) {
2299 error = sawritefilemarks(periph, markswanted, FALSE);
2307 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
2309 static const char *toobig =
2310 "%d-byte tape record bigger than supplied buffer\n";
2311 struct cam_periph *periph;
2312 struct sa_softc *softc;
2313 struct ccb_scsiio *csio;
2314 struct scsi_sense_data *sense;
2318 int error_code, sense_key, asc, ascq, error, aqvalid, stream_valid;
2320 uint8_t stream_bits;
2322 periph = xpt_path_periph(ccb->ccb_h.path);
2323 softc = (struct sa_softc *)periph->softc;
2325 sense = &csio->sense_data;
2326 sense_len = csio->sense_len - csio->sense_resid;
2327 scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
2328 &asc, &ascq, /*show_errors*/ 1);
2329 if (asc != -1 && ascq != -1)
2333 if (scsi_get_stream_info(sense, sense_len, NULL, &stream_bits) == 0)
2339 status = csio->ccb_h.status & CAM_STATUS_MASK;
2342 * Calculate/latch up, any residuals... We do this in a funny 2-step
2343 * so we can print stuff here if we have CAM_DEBUG enabled for this
2346 if (status == CAM_SCSI_STATUS_ERROR) {
2347 if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &resid,
2349 if ((softc->flags & SA_FLAG_FIXED) != 0)
2350 resid *= softc->media_blksize;
2352 resid = csio->dxfer_len;
2354 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2355 if (softc->media_blksize)
2356 info /= softc->media_blksize;
2359 if (CCB_Type(csio) == SA_CCB_BUFFER_IO) {
2360 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
2361 sizeof (struct scsi_sense_data));
2362 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
2363 (int) csio->cdb_len);
2364 softc->last_io_resid = resid;
2365 softc->last_resid_was_io = 1;
2367 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
2368 sizeof (struct scsi_sense_data));
2369 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
2370 (int) csio->cdb_len);
2371 softc->last_ctl_resid = resid;
2372 softc->last_resid_was_io = 0;
2374 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
2375 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %jd "
2376 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
2377 sense_key, asc, ascq, status,
2378 (stream_valid) ? stream_bits : 0, (intmax_t)resid,
2381 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2382 ("Cam Status 0x%x\n", status));
2388 case CAM_SCSI_STATUS_ERROR:
2390 * If a read/write command, we handle it here.
2392 if (CCB_Type(csio) != SA_CCB_WAITING) {
2396 * If this was just EOM/EOP, Filemark, Setmark or ILI detected
2397 * on a non read/write command, we assume it's not an error
2398 * and propagate the residule and return.
2400 if ((aqvalid && asc == 0 && ascq > 0 && ascq <= 5) ||
2401 (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
2402 csio->resid = resid;
2407 * Otherwise, we let the common code handle this.
2409 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2413 * We cannot depend upon CAM honoring retry counts for these.
2415 case CAM_SCSI_BUS_RESET:
2417 if (ccb->ccb_h.retry_count <= 0) {
2422 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2426 * Handle filemark, end of tape, mismatched record sizes....
2427 * From this point out, we're only handling read/write cases.
2428 * Handle writes && reads differently.
2431 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2432 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2433 csio->resid = resid;
2435 } else if ((stream_valid != 0) && (stream_bits & SSD_EOM)) {
2436 softc->flags |= SA_FLAG_EOM_PENDING;
2438 * Grotesque as it seems, the few times
2439 * I've actually seen a non-zero resid,
2440 * the tape drive actually lied and had
2441 * written all the data!.
2446 csio->resid = resid;
2447 if (sense_key == SSD_KEY_BLANK_CHECK) {
2448 if (softc->quirks & SA_QUIRK_1FM) {
2450 softc->flags |= SA_FLAG_EOM_PENDING;
2454 } else if ((stream_valid != 0) && (stream_bits & SSD_FILEMARK)){
2455 if (softc->flags & SA_FLAG_FIXED) {
2457 softc->flags |= SA_FLAG_EOF_PENDING;
2460 * Unconditionally, if we detected a filemark on a read,
2461 * mark that we've run moved a file ahead.
2463 if (softc->fileno != (daddr_t) -1) {
2466 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2472 * Incorrect Length usually applies to read, but can apply to writes.
2474 if (error == 0 && (stream_valid != 0) && (stream_bits & SSD_ILI)) {
2476 xpt_print(csio->ccb_h.path, toobig,
2477 csio->dxfer_len - info);
2478 csio->resid = csio->dxfer_len;
2481 csio->resid = resid;
2482 if (softc->flags & SA_FLAG_FIXED) {
2483 softc->flags |= SA_FLAG_EIO_PENDING;
2486 * Bump the block number if we hadn't seen a filemark.
2487 * Do this independent of errors (we've moved anyway).
2489 if ((stream_valid == 0) ||
2490 (stream_bits & SSD_FILEMARK) == 0) {
2491 if (softc->blkno != (daddr_t) -1) {
2493 csio->ccb_h.ccb_pflags |=
2494 SA_POSITION_UPDATED;
2502 * Unfreeze the queue if frozen as we're not returning anything
2503 * to our waiters that would indicate an I/O error has occurred
2513 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2514 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2515 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2516 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2521 struct scsi_mode_header_6 *mode_hdr;
2522 struct scsi_mode_blk_desc *mode_blk;
2523 int mode_buffer_len;
2524 struct sa_softc *softc;
2529 softc = (struct sa_softc *)periph->softc;
2530 ccb = cam_periph_getccb(periph, 1);
2531 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2532 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2534 cpage = SA_DATA_COMPRESSION_PAGE;
2537 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2539 if (params_to_get & SA_PARAM_COMPRESSION) {
2540 if (softc->quirks & SA_QUIRK_NOCOMP) {
2541 *comp_supported = FALSE;
2542 params_to_get &= ~SA_PARAM_COMPRESSION;
2544 mode_buffer_len += sizeof (sa_comp_t);
2547 /* XXX Fix M_NOWAIT */
2548 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
2549 if (mode_buffer == NULL) {
2550 xpt_release_ccb(ccb);
2553 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2554 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2556 /* it is safe to retry this */
2557 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2558 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2559 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2560 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2562 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2563 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_SCSISA);
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);
2629 goto sagetparamsexit;
2632 if (params_to_get & SA_PARAM_BLOCKSIZE)
2633 *blocksize = scsi_3btoul(mode_blk->blklen);
2635 if (params_to_get & SA_PARAM_NUMBLOCKS)
2636 *numblocks = scsi_3btoul(mode_blk->nblocks);
2638 if (params_to_get & SA_PARAM_BUFF_MODE)
2639 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2641 if (params_to_get & SA_PARAM_DENSITY)
2642 *density = mode_blk->density;
2644 if (params_to_get & SA_PARAM_WP)
2645 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2647 if (params_to_get & SA_PARAM_SPEED)
2648 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2650 if (params_to_get & SA_PARAM_COMPRESSION) {
2651 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2652 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2653 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2655 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2657 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2658 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2660 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2662 * We don't really know whether this device supports
2663 * Data Compression if the algorithm field is
2664 * zero. Just say we do.
2666 *comp_supported = TRUE;
2668 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2669 *comp_algorithm = cp->sel_comp_alg;
2672 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2675 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2677 char *xyz = mode_buffer;
2678 xpt_print_path(periph->path);
2679 printf("Mode Sense Data=");
2680 for (idx = 0; idx < mode_buffer_len; idx++)
2681 printf(" 0x%02x", xyz[idx] & 0xff);
2687 xpt_release_ccb(ccb);
2688 free(mode_buffer, M_SCSISA);
2693 * The purpose of this function is to set one of four different parameters
2697 * - compression / compression algorithm
2700 * The assumption is that this will be called from saioctl(), and therefore
2701 * from a process context. Thus the waiting malloc calls below. If that
2702 * assumption ever changes, the malloc calls should be changed to be
2705 * Any or all of the four parameters may be set when this function is
2706 * called. It should handle setting more than one parameter at once.
2709 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2710 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2711 u_int32_t sense_flags)
2713 struct sa_softc *softc;
2714 u_int32_t current_blocksize;
2715 u_int32_t current_calg;
2716 u_int8_t current_density;
2717 u_int8_t current_speed;
2718 int comp_enabled, comp_supported;
2720 int mode_buffer_len;
2721 struct scsi_mode_header_6 *mode_hdr;
2722 struct scsi_mode_blk_desc *mode_blk;
2723 sa_comp_t *ccomp, *cpage;
2725 union ccb *ccb = NULL;
2728 softc = (struct sa_softc *)periph->softc;
2730 ccomp = malloc(sizeof (sa_comp_t), M_SCSISA, M_NOWAIT);
2735 * Since it doesn't make sense to set the number of blocks, or
2736 * write protection, we won't try to get the current value. We
2737 * always want to get the blocksize, so we can set it back to the
2740 error = sagetparams(periph,
2741 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2742 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
2743 ¤t_speed, &comp_supported, &comp_enabled,
2744 ¤t_calg, ccomp);
2747 free(ccomp, M_SCSISA);
2751 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2752 if (params_to_set & SA_PARAM_COMPRESSION)
2753 mode_buffer_len += sizeof (sa_comp_t);
2755 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
2756 if (mode_buffer == NULL) {
2757 free(ccomp, M_SCSISA);
2761 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2762 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2764 ccb = cam_periph_getccb(periph, 1);
2768 if (params_to_set & SA_PARAM_COMPRESSION) {
2770 cpage = (sa_comp_t *)&mode_blk[1];
2772 cpage = (sa_comp_t *)&mode_hdr[1];
2774 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2775 cpage->hdr.pagecode &= ~0x80;
2780 * If the caller wants us to set the blocksize, use the one they
2781 * pass in. Otherwise, use the blocksize we got back from the
2782 * mode select above.
2785 if (params_to_set & SA_PARAM_BLOCKSIZE)
2786 scsi_ulto3b(blocksize, mode_blk->blklen);
2788 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2791 * Set density if requested, else preserve old density.
2792 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2793 * devices, else density we've latched up in our softc.
2795 if (params_to_set & SA_PARAM_DENSITY) {
2796 mode_blk->density = density;
2797 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2798 mode_blk->density = SCSI_SAME_DENSITY;
2800 mode_blk->density = softc->media_density;
2805 * For mode selects, these two fields must be zero.
2807 mode_hdr->data_length = 0;
2808 mode_hdr->medium_type = 0;
2810 /* set the speed to the current value */
2811 mode_hdr->dev_spec = current_speed;
2813 /* if set, set single-initiator buffering mode */
2814 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
2815 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2819 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2821 mode_hdr->blk_desc_len = 0;
2824 * First, if the user wants us to set the compression algorithm or
2825 * just turn compression on, check to make sure that this drive
2826 * supports compression.
2828 if (params_to_set & SA_PARAM_COMPRESSION) {
2830 * If the compression algorithm is 0, disable compression.
2831 * If the compression algorithm is non-zero, enable
2832 * compression and set the compression type to the
2833 * specified compression algorithm, unless the algorithm is
2834 * MT_COMP_ENABLE. In that case, we look at the
2835 * compression algorithm that is currently set and if it is
2836 * non-zero, we leave it as-is. If it is zero, and we have
2837 * saved a compression algorithm from a time when
2838 * compression was enabled before, set the compression to
2841 switch (ccomp->hdr.pagecode & ~0x80) {
2842 case SA_DEVICE_CONFIGURATION_PAGE:
2844 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2846 dcp->sel_comp_alg = SA_COMP_NONE;
2849 if (calg != MT_COMP_ENABLE) {
2850 dcp->sel_comp_alg = calg;
2851 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2852 softc->saved_comp_algorithm != 0) {
2853 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2857 case SA_DATA_COMPRESSION_PAGE:
2858 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2859 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2862 * Disable compression, but leave the
2863 * decompression and the capability bit
2866 dcp->dce_and_dcc = SA_DCP_DCC;
2867 dcp->dde_and_red |= SA_DCP_DDE;
2870 /* enable compression && decompression */
2871 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2872 dcp->dde_and_red |= SA_DCP_DDE;
2874 * If there, use compression algorithm from caller.
2875 * Otherwise, if there's a saved compression algorithm
2876 * and there is no current algorithm, use the saved
2877 * algorithm. Else parrot back what we got and hope
2880 if (calg != MT_COMP_ENABLE) {
2881 scsi_ulto4b(calg, dcp->comp_algorithm);
2882 scsi_ulto4b(calg, dcp->decomp_algorithm);
2883 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2884 softc->saved_comp_algorithm != 0) {
2885 scsi_ulto4b(softc->saved_comp_algorithm,
2886 dcp->comp_algorithm);
2887 scsi_ulto4b(softc->saved_comp_algorithm,
2888 dcp->decomp_algorithm);
2893 * Compression does not appear to be supported-
2894 * at least via the DATA COMPRESSION page. It
2895 * would be too much to ask us to believe that
2896 * the page itself is supported, but incorrectly
2897 * reports an ability to manipulate data compression,
2898 * so we'll assume that this device doesn't support
2899 * compression. We can just fall through for that.
2904 * The drive doesn't seem to support compression,
2905 * so turn off the set compression bit.
2907 params_to_set &= ~SA_PARAM_COMPRESSION;
2908 xpt_print(periph->path,
2909 "device does not seem to support compression\n");
2912 * If that was the only thing the user wanted us to set,
2913 * clean up allocated resources and return with
2914 * 'operation not supported'.
2916 if (params_to_set == SA_PARAM_NONE) {
2917 free(mode_buffer, M_SCSISA);
2918 xpt_release_ccb(ccb);
2923 * That wasn't the only thing the user wanted us to set.
2924 * So, decrease the stated mode buffer length by the
2925 * size of the compression mode page.
2927 mode_buffer_len -= sizeof(sa_comp_t);
2931 /* It is safe to retry this operation */
2932 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2933 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2934 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2936 error = cam_periph_runccb(ccb, saerror, 0,
2937 sense_flags, softc->device_stats);
2939 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2941 char *xyz = mode_buffer;
2942 xpt_print_path(periph->path);
2943 printf("Err%d, Mode Select Data=", error);
2944 for (idx = 0; idx < mode_buffer_len; idx++)
2945 printf(" 0x%02x", xyz[idx] & 0xff);
2952 * If we can, try without setting density/blocksize.
2955 if ((params_to_set &
2956 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2961 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2962 cpage = (sa_comp_t *)&mode_blk[1];
2966 * If we were setting the blocksize, and that failed, we
2967 * want to set it to its original value. If we weren't
2968 * setting the blocksize, we don't want to change it.
2970 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2973 * Set density if requested, else preserve old density.
2974 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2975 * devices, else density we've latched up in our softc.
2977 if (params_to_set & SA_PARAM_DENSITY) {
2978 mode_blk->density = current_density;
2979 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2980 mode_blk->density = SCSI_SAME_DENSITY;
2982 mode_blk->density = softc->media_density;
2985 if (params_to_set & SA_PARAM_COMPRESSION)
2986 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2989 * The retry count is the only CCB field that might have been
2990 * changed that we care about, so reset it back to 1.
2992 ccb->ccb_h.retry_count = 1;
2993 cam_periph_runccb(ccb, saerror, 0, sense_flags,
2994 softc->device_stats);
2997 xpt_release_ccb(ccb);
3000 free(ccomp, M_SCSISA);
3002 if (params_to_set & SA_PARAM_COMPRESSION) {
3004 softc->flags &= ~SA_FLAG_COMP_ENABLED;
3006 * Even if we get an error setting compression,
3007 * do not say that we don't support it. We could
3008 * have been wrong, or it may be media specific.
3009 * softc->flags &= ~SA_FLAG_COMP_SUPP;
3011 softc->saved_comp_algorithm = softc->comp_algorithm;
3012 softc->comp_algorithm = 0;
3014 softc->flags |= SA_FLAG_COMP_ENABLED;
3015 softc->comp_algorithm = calg;
3019 free(mode_buffer, M_SCSISA);
3024 saprevent(struct cam_periph *periph, int action)
3026 struct sa_softc *softc;
3030 softc = (struct sa_softc *)periph->softc;
3032 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3034 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3038 * We can be quiet about illegal requests.
3040 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3045 ccb = cam_periph_getccb(periph, 1);
3047 /* It is safe to retry this operation */
3048 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3049 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3051 error = cam_periph_runccb(ccb, saerror, 0, sf, softc->device_stats);
3053 if (action == PR_ALLOW)
3054 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3056 softc->flags |= SA_FLAG_TAPE_LOCKED;
3059 xpt_release_ccb(ccb);
3063 sarewind(struct cam_periph *periph)
3066 struct sa_softc *softc;
3069 softc = (struct sa_softc *)periph->softc;
3071 ccb = cam_periph_getccb(periph, 1);
3073 /* It is safe to retry this operation */
3074 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3075 SSD_FULL_SIZE, REWIND_TIMEOUT);
3077 softc->dsreg = MTIO_DSREG_REW;
3078 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3079 softc->dsreg = MTIO_DSREG_REST;
3081 xpt_release_ccb(ccb);
3083 softc->fileno = softc->blkno = (daddr_t) 0;
3085 softc->fileno = softc->blkno = (daddr_t) -1;
3090 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3093 struct sa_softc *softc;
3096 softc = (struct sa_softc *)periph->softc;
3098 ccb = cam_periph_getccb(periph, 1);
3100 /* This cannot be retried */
3102 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3103 SSD_FULL_SIZE, SPACE_TIMEOUT);
3106 * Clear residual because we will be using it.
3108 softc->last_ctl_resid = 0;
3110 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3111 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3112 softc->dsreg = MTIO_DSREG_REST;
3114 xpt_release_ccb(ccb);
3117 * If a spacing operation has failed, we need to invalidate
3120 * If the spacing operation was setmarks or to end of recorded data,
3121 * we no longer know our relative position.
3123 * If the spacing operations was spacing files in reverse, we
3124 * take account of the residual, but still check against less
3125 * than zero- if we've gone negative, we must have hit BOT.
3127 * If the spacing operations was spacing records in reverse and
3128 * we have a residual, we've either hit BOT or hit a filemark.
3129 * In the former case, we know our new record number (0). In
3130 * the latter case, we have absolutely no idea what the real
3131 * record number is- we've stopped between the end of the last
3132 * record in the previous file and the filemark that stopped
3133 * our spacing backwards.
3136 softc->fileno = softc->blkno = (daddr_t) -1;
3137 } else if (code == SS_SETMARKS || code == SS_EOD) {
3138 softc->fileno = softc->blkno = (daddr_t) -1;
3139 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3140 softc->fileno += (count - softc->last_ctl_resid);
3141 if (softc->fileno < 0) /* we must of hit BOT */
3144 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3145 softc->blkno += (count - softc->last_ctl_resid);
3147 if (softc->last_ctl_resid || softc->blkno < 0) {
3148 if (softc->fileno == 0) {
3151 softc->blkno = (daddr_t) -1;
3160 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3163 struct sa_softc *softc;
3166 softc = (struct sa_softc *)periph->softc;
3167 if (softc->open_rdonly)
3170 ccb = cam_periph_getccb(periph, 1);
3172 * Clear residual because we will be using it.
3174 softc->last_ctl_resid = 0;
3176 softc->dsreg = MTIO_DSREG_FMK;
3177 /* this *must* not be retried */
3178 scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG,
3179 FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
3180 softc->dsreg = MTIO_DSREG_REST;
3183 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3185 if (error == 0 && nmarks) {
3186 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3187 nwm = nmarks - softc->last_ctl_resid;
3188 softc->filemarks += nwm;
3191 xpt_release_ccb(ccb);
3194 * Update relative positions (if we're doing that).
3197 softc->fileno = softc->blkno = (daddr_t) -1;
3198 } else if (softc->fileno != (daddr_t) -1) {
3199 softc->fileno += nwm;
3206 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3208 struct scsi_tape_position_data loc;
3210 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3214 * We try and flush any buffered writes here if we were writing
3215 * and we're trying to get hardware block position. It eats
3216 * up performance substantially, but I'm wary of drive firmware.
3218 * I think that *logical* block position is probably okay-
3219 * but hardware block position might have to wait for data
3220 * to hit media to be valid. Caveat Emptor.
3223 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3224 error = sawritefilemarks(periph, 0, 0);
3225 if (error && error != EACCES)
3229 ccb = cam_periph_getccb(periph, 1);
3230 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3231 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3232 softc->dsreg = MTIO_DSREG_RBSY;
3233 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3234 softc->dsreg = MTIO_DSREG_REST;
3237 if (loc.flags & SA_RPOS_UNCERTAIN) {
3238 error = EINVAL; /* nothing is certain */
3240 *blkptr = scsi_4btoul(loc.firstblk);
3244 xpt_release_ccb(ccb);
3249 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3252 struct sa_softc *softc;
3256 * We used to try and flush any buffered writes here.
3257 * Now we push this onto user applications to either
3258 * flush the pending writes themselves (via a zero count
3259 * WRITE FILEMARKS command) or they can trust their tape
3260 * drive to do this correctly for them.
3263 softc = (struct sa_softc *)periph->softc;
3264 ccb = cam_periph_getccb(periph, 1);
3267 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3268 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3271 softc->dsreg = MTIO_DSREG_POS;
3272 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3273 softc->dsreg = MTIO_DSREG_REST;
3274 xpt_release_ccb(ccb);
3276 * Note relative file && block number position as now unknown.
3278 softc->fileno = softc->blkno = (daddr_t) -1;
3283 saretension(struct cam_periph *periph)
3286 struct sa_softc *softc;
3289 softc = (struct sa_softc *)periph->softc;
3291 ccb = cam_periph_getccb(periph, 1);
3293 /* It is safe to retry this operation */
3294 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3295 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3297 softc->dsreg = MTIO_DSREG_TEN;
3298 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3299 softc->dsreg = MTIO_DSREG_REST;
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;
3326 xpt_release_ccb(ccb);
3329 * If the error was Illegal Request, then the device doesn't support
3330 * RESERVE/RELEASE. This is not an error.
3332 if (error == EINVAL) {
3340 saloadunload(struct cam_periph *periph, int load)
3343 struct sa_softc *softc;
3346 softc = (struct sa_softc *)periph->softc;
3348 ccb = cam_periph_getccb(periph, 1);
3350 /* It is safe to retry this operation */
3351 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3352 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3354 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3355 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
3356 softc->dsreg = MTIO_DSREG_REST;
3357 xpt_release_ccb(ccb);
3359 if (error || load == 0)
3360 softc->fileno = softc->blkno = (daddr_t) -1;
3361 else if (error == 0)
3362 softc->fileno = softc->blkno = (daddr_t) 0;
3367 saerase(struct cam_periph *periph, int longerase)
3371 struct sa_softc *softc;
3374 softc = (struct sa_softc *)periph->softc;
3375 if (softc->open_rdonly)
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 xpt_release_ccb(ccb);
3391 #endif /* _KERNEL */
3394 * Read tape block limits command.
3397 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3398 void (*cbfcnp)(struct cam_periph *, union ccb *),
3399 u_int8_t tag_action,
3400 struct scsi_read_block_limits_data *rlimit_buf,
3401 u_int8_t sense_len, u_int32_t timeout)
3403 struct scsi_read_block_limits *scsi_cmd;
3405 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3406 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3407 sizeof(*scsi_cmd), timeout);
3409 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3410 bzero(scsi_cmd, sizeof(*scsi_cmd));
3411 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3415 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3416 void (*cbfcnp)(struct cam_periph *, union ccb *),
3417 u_int8_t tag_action, int readop, int sli,
3418 int fixed, u_int32_t length, u_int8_t *data_ptr,
3419 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3421 struct scsi_sa_rw *scsi_cmd;
3423 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3424 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3425 scsi_cmd->sli_fixed = 0;
3427 scsi_cmd->sli_fixed |= SAR_SLI;
3429 scsi_cmd->sli_fixed |= SARW_FIXED;
3430 scsi_ulto3b(length, scsi_cmd->length);
3431 scsi_cmd->control = 0;
3433 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3434 tag_action, data_ptr, dxfer_len, sense_len,
3435 sizeof(*scsi_cmd), timeout);
3439 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3440 void (*cbfcnp)(struct cam_periph *, union ccb *),
3441 u_int8_t tag_action, int immediate, int eot,
3442 int reten, int load, u_int8_t sense_len,
3445 struct scsi_load_unload *scsi_cmd;
3447 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3448 bzero(scsi_cmd, sizeof(*scsi_cmd));
3449 scsi_cmd->opcode = LOAD_UNLOAD;
3451 scsi_cmd->immediate = SLU_IMMED;
3453 scsi_cmd->eot_reten_load |= SLU_EOT;
3455 scsi_cmd->eot_reten_load |= SLU_RETEN;
3457 scsi_cmd->eot_reten_load |= SLU_LOAD;
3459 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3460 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3464 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3465 void (*cbfcnp)(struct cam_periph *, union ccb *),
3466 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3469 struct scsi_rewind *scsi_cmd;
3471 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3472 bzero(scsi_cmd, sizeof(*scsi_cmd));
3473 scsi_cmd->opcode = REWIND;
3475 scsi_cmd->immediate = SREW_IMMED;
3477 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3478 0, sense_len, sizeof(*scsi_cmd), timeout);
3482 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3483 void (*cbfcnp)(struct cam_periph *, union ccb *),
3484 u_int8_t tag_action, scsi_space_code code,
3485 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3487 struct scsi_space *scsi_cmd;
3489 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3490 scsi_cmd->opcode = SPACE;
3491 scsi_cmd->code = code;
3492 scsi_ulto3b(count, scsi_cmd->count);
3493 scsi_cmd->control = 0;
3495 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3496 0, sense_len, sizeof(*scsi_cmd), timeout);
3500 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3501 void (*cbfcnp)(struct cam_periph *, union ccb *),
3502 u_int8_t tag_action, int immediate, int setmark,
3503 u_int32_t num_marks, u_int8_t sense_len,
3506 struct scsi_write_filemarks *scsi_cmd;
3508 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3509 bzero(scsi_cmd, sizeof(*scsi_cmd));
3510 scsi_cmd->opcode = WRITE_FILEMARKS;
3512 scsi_cmd->byte2 |= SWFMRK_IMMED;
3514 scsi_cmd->byte2 |= SWFMRK_WSMK;
3516 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3518 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3519 0, sense_len, sizeof(*scsi_cmd), timeout);
3523 * The reserve and release unit commands differ only by their opcodes.
3526 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3527 void (*cbfcnp)(struct cam_periph *, union ccb *),
3528 u_int8_t tag_action, int third_party,
3529 int third_party_id, u_int8_t sense_len,
3530 u_int32_t timeout, int reserve)
3532 struct scsi_reserve_release_unit *scsi_cmd;
3534 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3535 bzero(scsi_cmd, sizeof(*scsi_cmd));
3538 scsi_cmd->opcode = RESERVE_UNIT;
3540 scsi_cmd->opcode = RELEASE_UNIT;
3543 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3544 scsi_cmd->lun_thirdparty |=
3545 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3548 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3549 0, sense_len, sizeof(*scsi_cmd), timeout);
3553 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3554 void (*cbfcnp)(struct cam_periph *, union ccb *),
3555 u_int8_t tag_action, int immediate, int long_erase,
3556 u_int8_t sense_len, u_int32_t timeout)
3558 struct scsi_erase *scsi_cmd;
3560 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3561 bzero(scsi_cmd, sizeof(*scsi_cmd));
3563 scsi_cmd->opcode = ERASE;
3566 scsi_cmd->lun_imm_long |= SE_IMMED;
3569 scsi_cmd->lun_imm_long |= SE_LONG;
3571 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3572 0, sense_len, sizeof(*scsi_cmd), timeout);
3576 * Read Tape Position command.
3579 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3580 void (*cbfcnp)(struct cam_periph *, union ccb *),
3581 u_int8_t tag_action, int hardsoft,
3582 struct scsi_tape_position_data *sbp,
3583 u_int8_t sense_len, u_int32_t timeout)
3585 struct scsi_tape_read_position *scmd;
3587 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3588 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3589 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3590 bzero(scmd, sizeof(*scmd));
3591 scmd->opcode = READ_POSITION;
3592 scmd->byte1 = hardsoft;
3596 * Set Tape Position command.
3599 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3600 void (*cbfcnp)(struct cam_periph *, union ccb *),
3601 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3602 u_int8_t sense_len, u_int32_t timeout)
3604 struct scsi_tape_locate *scmd;
3606 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3607 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3608 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3609 bzero(scmd, sizeof(*scmd));
3610 scmd->opcode = LOCATE;
3612 scmd->byte1 |= SA_SPOS_BT;
3613 scsi_ulto4b(blkno, scmd->blkaddr);