Make sure we look at the correct sub op codes when
[dragonfly.git] / sys / bus / cam / scsi / scsi_sa.c
blob5a4f831a74eafe244df7e9b592eb3c942fac986e
1 /*
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.31 2007/11/29 03:31:15 pavalos Exp $
5 * Implementation of SCSI Sequential Access Peripheral driver for CAM.
7 * Copyright (c) 1999, 2000 Matthew Jacob
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
29 * SUCH DAMAGE.
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #ifdef _KERNEL
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #endif
39 #include <sys/types.h>
40 #ifdef _KERNEL
41 #include <sys/buf.h>
42 #include <sys/malloc.h>
43 #endif
44 #include <sys/mtio.h>
45 #include <sys/conf.h>
46 #ifdef _KERNEL
47 #include <sys/proc.h>
48 #include <sys/buf2.h>
49 #include <sys/thread2.h>
50 #endif
51 #include <sys/devicestat.h>
52 #include <machine/limits.h>
54 #ifndef _KERNEL
55 #include <stdio.h>
56 #include <string.h>
57 #endif
59 #include "../cam.h"
60 #include "../cam_ccb.h"
61 #include "../cam_extend.h"
62 #include "../cam_periph.h"
63 #include "../cam_xpt_periph.h"
64 #include "../cam_debug.h"
66 #include "scsi_all.h"
67 #include "scsi_message.h"
68 #include "scsi_sa.h"
70 #ifdef _KERNEL
72 #include <opt_sa.h>
74 #ifndef SA_IO_TIMEOUT
75 #define SA_IO_TIMEOUT 4
76 #endif
77 #ifndef SA_SPACE_TIMEOUT
78 #define SA_SPACE_TIMEOUT 1 * 60
79 #endif
80 #ifndef SA_REWIND_TIMEOUT
81 #define SA_REWIND_TIMEOUT 2 * 60
82 #endif
83 #ifndef SA_ERASE_TIMEOUT
84 #define SA_ERASE_TIMEOUT 4 * 60
85 #endif
87 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
89 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
90 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
91 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
92 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
95 * Additional options that can be set for config: SA_1FM_AT_EOT
98 #ifndef UNUSED_PARAMETER
99 #define UNUSED_PARAMETER(x) x = x
100 #endif
102 #define QFRLS(ccb) \
103 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
104 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
107 * Driver states
111 typedef enum {
112 SA_STATE_NORMAL, SA_STATE_ABNORMAL
113 } sa_state;
115 #define ccb_pflags ppriv_field0
116 #define ccb_bio ppriv_ptr1
118 #define SA_CCB_BUFFER_IO 0x0
119 #define SA_CCB_WAITING 0x1
120 #define SA_CCB_TYPEMASK 0x1
121 #define SA_POSITION_UPDATED 0x2
123 #define Set_CCB_Type(x, type) \
124 x->ccb_h.ccb_pflags &= ~SA_CCB_TYPEMASK; \
125 x->ccb_h.ccb_pflags |= type
127 #define CCB_Type(x) (x->ccb_h.ccb_pflags & SA_CCB_TYPEMASK)
131 typedef enum {
132 SA_FLAG_OPEN = 0x0001,
133 SA_FLAG_FIXED = 0x0002,
134 SA_FLAG_TAPE_LOCKED = 0x0004,
135 SA_FLAG_TAPE_MOUNTED = 0x0008,
136 SA_FLAG_TAPE_WP = 0x0010,
137 SA_FLAG_TAPE_WRITTEN = 0x0020,
138 SA_FLAG_EOM_PENDING = 0x0040,
139 SA_FLAG_EIO_PENDING = 0x0080,
140 SA_FLAG_EOF_PENDING = 0x0100,
141 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
142 SA_FLAG_EOF_PENDING),
143 SA_FLAG_INVALID = 0x0200,
144 SA_FLAG_COMP_ENABLED = 0x0400,
145 SA_FLAG_COMP_SUPP = 0x0800,
146 SA_FLAG_COMP_UNSUPP = 0x1000,
147 SA_FLAG_TAPE_FROZEN = 0x2000
148 } sa_flags;
150 typedef enum {
151 SA_MODE_REWIND = 0x00,
152 SA_MODE_NOREWIND = 0x01,
153 SA_MODE_OFFLINE = 0x02
154 } sa_mode;
156 typedef enum {
157 SA_PARAM_NONE = 0x00,
158 SA_PARAM_BLOCKSIZE = 0x01,
159 SA_PARAM_DENSITY = 0x02,
160 SA_PARAM_COMPRESSION = 0x04,
161 SA_PARAM_BUFF_MODE = 0x08,
162 SA_PARAM_NUMBLOCKS = 0x10,
163 SA_PARAM_WP = 0x20,
164 SA_PARAM_SPEED = 0x40,
165 SA_PARAM_ALL = 0x7f
166 } sa_params;
168 typedef enum {
169 SA_QUIRK_NONE = 0x00,
170 SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */
171 SA_QUIRK_FIXED = 0x02, /* Force fixed mode */
172 SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */
173 SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */
174 SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */
175 SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */
176 SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */
177 SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */
178 } sa_quirks;
180 /* units are bits 4-7, 16-21 (1024 units) */
181 #define SAUNIT(DEV) \
182 (((minor(DEV) & 0xF0) >> 4) | ((minor(DEV) & 0x3f0000) >> 16))
184 #define SAMODE(z) ((minor(z) & 0x3))
185 #define SADENSITY(z) (((minor(z) >> 2) & 0x3))
186 #define SA_IS_CTRL(z) (minor(z) & (1 << 29))
188 #define SA_NOT_CTLDEV 0
189 #define SA_CTLDEV 1
191 #define SA_ATYPE_R 0
192 #define SA_ATYPE_NR 1
193 #define SA_ATYPE_ER 2
195 #define SAMINOR(ctl, unit, mode, access) \
196 ((ctl << 29) | ((unit & 0x3f0) << 16) | ((unit & 0xf) << 4) | \
197 (mode << 0x2) | (access & 0x3))
199 #define SA_UNITMASK SAMINOR(0, -1, 0, 0)
200 #define SA_UNIT(unit) SAMINOR(0, unit, 0, 0)
202 #define SA_NUM_MODES 4
204 struct sa_softc {
205 sa_state state;
206 sa_flags flags;
207 sa_quirks quirks;
208 struct bio_queue_head bio_queue;
209 int queue_count;
210 struct devstat device_stats;
211 int blk_gran;
212 int blk_mask;
213 int blk_shift;
214 u_int32_t max_blk;
215 u_int32_t min_blk;
216 u_int32_t comp_algorithm;
217 u_int32_t saved_comp_algorithm;
218 u_int32_t media_blksize;
219 u_int32_t last_media_blksize;
220 u_int32_t media_numblks;
221 u_int8_t media_density;
222 u_int8_t speed;
223 u_int8_t scsi_rev;
224 u_int8_t dsreg; /* mtio mt_dsreg, redux */
225 int buffer_mode;
226 int filemarks;
227 union ccb saved_ccb;
228 int last_resid_was_io;
231 * Relative to BOT Location.
233 daddr_t fileno;
234 daddr_t blkno;
237 * Latched Error Info
239 struct {
240 struct scsi_sense_data _last_io_sense;
241 u_int32_t _last_io_resid;
242 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
243 struct scsi_sense_data _last_ctl_sense;
244 u_int32_t _last_ctl_resid;
245 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
246 #define last_io_sense errinfo._last_io_sense
247 #define last_io_resid errinfo._last_io_resid
248 #define last_io_cdb errinfo._last_io_cdb
249 #define last_ctl_sense errinfo._last_ctl_sense
250 #define last_ctl_resid errinfo._last_ctl_resid
251 #define last_ctl_cdb errinfo._last_ctl_cdb
252 } errinfo;
254 * Misc other flags/state
256 u_int32_t
257 : 31,
258 ctrl_mode : 1; /* control device open */
261 struct sa_quirk_entry {
262 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
263 sa_quirks quirks; /* specific quirk type */
264 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
267 static struct sa_quirk_entry sa_quirk_table[] =
270 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
271 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
272 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
275 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
276 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
279 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
280 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
283 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
284 "Python*", "*"}, SA_QUIRK_NODREAD, 0
287 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
288 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
291 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
292 "VIPER 2525 25462", "-011"},
293 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
296 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
297 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
299 #if 0
301 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
302 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
304 #endif
306 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
307 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
310 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
311 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
314 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
315 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
318 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
319 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
322 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
323 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
326 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
327 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
329 { /* jreynold@primenet.com */
330 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
331 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
333 { /* mike@sentex.net */
334 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
335 "STT20000*", "*"}, SA_QUIRK_1FM, 0
338 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
339 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
342 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
343 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
346 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
347 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
350 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
351 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
354 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
355 " SLR*", "*"}, SA_QUIRK_1FM, 0
358 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
359 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
362 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
363 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
367 static d_open_t saopen;
368 static d_close_t saclose;
369 static d_strategy_t sastrategy;
370 static d_ioctl_t saioctl;
371 static periph_init_t sainit;
372 static periph_ctor_t saregister;
373 static periph_oninv_t saoninvalidate;
374 static periph_dtor_t sacleanup;
375 static periph_start_t sastart;
376 static void saasync(void *callback_arg, u_int32_t code,
377 struct cam_path *path, void *arg);
378 static void sadone(struct cam_periph *periph,
379 union ccb *start_ccb);
380 static int saerror(union ccb *ccb, u_int32_t cam_flags,
381 u_int32_t sense_flags);
382 static int samarkswanted(struct cam_periph *);
383 static int sacheckeod(struct cam_periph *periph);
384 static int sagetparams(struct cam_periph *periph,
385 sa_params params_to_get,
386 u_int32_t *blocksize, u_int8_t *density,
387 u_int32_t *numblocks, int *buff_mode,
388 u_int8_t *write_protect, u_int8_t *speed,
389 int *comp_supported, int *comp_enabled,
390 u_int32_t *comp_algorithm,
391 sa_comp_t *comp_page);
392 static int sasetparams(struct cam_periph *periph,
393 sa_params params_to_set,
394 u_int32_t blocksize, u_int8_t density,
395 u_int32_t comp_algorithm,
396 u_int32_t sense_flags);
397 static void saprevent(struct cam_periph *periph, int action);
398 static int sarewind(struct cam_periph *periph);
399 static int saspace(struct cam_periph *periph, int count,
400 scsi_space_code code);
401 static int samount(struct cam_periph *, int, cdev_t);
402 static int saretension(struct cam_periph *periph);
403 static int sareservereleaseunit(struct cam_periph *periph,
404 int reserve);
405 static int saloadunload(struct cam_periph *periph, int load);
406 static int saerase(struct cam_periph *periph, int longerase);
407 static int sawritefilemarks(struct cam_periph *periph,
408 int nmarks, int setmarks);
409 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
410 static int sasetpos(struct cam_periph *periph, int, u_int32_t *);
413 static struct periph_driver sadriver =
415 sainit, "sa",
416 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
419 PERIPHDRIVER_DECLARE(sa, sadriver);
421 /* For 2.2-stable support */
422 #ifndef D_TAPE
423 #define D_TAPE 0
424 #endif
426 #define SA_CDEV_MAJOR 14
428 static struct dev_ops sa_ops = {
429 { "sa", SA_CDEV_MAJOR, D_TAPE },
430 .d_open = saopen,
431 .d_close = saclose,
432 .d_read = physread,
433 .d_write = physwrite,
434 .d_ioctl = saioctl,
435 .d_strategy = sastrategy,
438 static struct extend_array *saperiphs;
440 static int
441 saopen(struct dev_open_args *ap)
443 cdev_t dev = ap->a_head.a_dev;
444 struct cam_periph *periph;
445 struct sa_softc *softc;
446 int unit;
447 int error;
449 unit = SAUNIT(dev);
451 crit_enter();
452 periph = cam_extend_get(saperiphs, unit);
453 if (periph == NULL) {
454 crit_exit();
455 return (ENXIO);
457 softc = (struct sa_softc *)periph->softc;
458 if ((error = cam_periph_lock(periph, PCATCH)) != 0) {
459 crit_exit();
460 return (error);
462 crit_exit();
464 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
465 ("saopen(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
467 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
468 cam_periph_unlock(periph);
469 return (ENXIO);
471 if (SA_IS_CTRL(dev)) {
472 softc->ctrl_mode = 1;
473 cam_periph_unlock(periph);
474 return (0);
478 if (softc->flags & SA_FLAG_OPEN) {
479 error = EBUSY;
480 } else if (softc->flags & SA_FLAG_INVALID) {
481 error = ENXIO;
482 } else {
484 * The function samount ensures media is loaded and ready.
485 * It also does a device RESERVE if the tape isn't yet mounted.
487 error = samount(periph, ap->a_oflags, dev);
490 if (error) {
491 cam_periph_release(periph);
492 } else {
493 saprevent(periph, PR_PREVENT);
494 softc->flags |= SA_FLAG_OPEN;
496 cam_periph_unlock(periph);
497 return (error);
500 static int
501 saclose(struct dev_close_args *ap)
503 cdev_t dev = ap->a_head.a_dev;
504 struct cam_periph *periph;
505 struct sa_softc *softc;
506 int unit, mode, error, writing, tmp;
507 int closedbits = SA_FLAG_OPEN;
509 unit = SAUNIT(dev);
510 mode = SAMODE(dev);
511 periph = cam_extend_get(saperiphs, unit);
512 if (periph == NULL)
513 return (ENXIO);
515 softc = (struct sa_softc *)periph->softc;
517 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
518 ("saclose(%d): dev=0x%x softc=0x%x\n", unit, unit, softc->flags));
521 if ((error = cam_periph_lock(periph, 0)) != 0) {
522 return (error);
525 if (SA_IS_CTRL(dev)) {
526 softc->ctrl_mode = 0;
527 cam_periph_release(periph);
528 cam_periph_unlock(periph);
529 return (0);
533 * Were we writing the tape?
535 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
538 * See whether or not we need to write filemarks. If this
539 * fails, we probably have to assume we've lost tape
540 * position.
542 error = sacheckeod(periph);
543 if (error) {
544 xpt_print_path(periph->path);
545 kprintf("failed to write terminating filemark(s)\n");
546 softc->flags |= SA_FLAG_TAPE_FROZEN;
550 * Whatever we end up doing, allow users to eject tapes from here on.
552 saprevent(periph, PR_ALLOW);
555 * Decide how to end...
557 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
558 closedbits |= SA_FLAG_TAPE_FROZEN;
559 } else switch (mode) {
560 case SA_MODE_OFFLINE:
562 * An 'offline' close is an unconditional release of
563 * frozen && mount conditions, irrespective of whether
564 * these operations succeeded. The reason for this is
565 * to allow at least some kind of programmatic way
566 * around our state getting all fouled up. If somebody
567 * issues an 'offline' command, that will be allowed
568 * to clear state.
570 sarewind(periph);
571 saloadunload(periph, FALSE);
572 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
573 break;
574 case SA_MODE_REWIND:
576 * If the rewind fails, return an error- if anyone cares,
577 * but not overwriting any previous error.
579 * We don't clear the notion of mounted here, but we do
580 * clear the notion of frozen if we successfully rewound.
582 tmp = sarewind(periph);
583 if (tmp) {
584 if (error != 0)
585 error = tmp;
586 } else {
587 closedbits |= SA_FLAG_TAPE_FROZEN;
589 break;
590 case SA_MODE_NOREWIND:
592 * If we're not rewinding/unloading the tape, find out
593 * whether we need to back up over one of two filemarks
594 * we wrote (if we wrote two filemarks) so that appends
595 * from this point on will be sane.
597 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
598 tmp = saspace(periph, -1, SS_FILEMARKS);
599 if (tmp) {
600 xpt_print_path(periph->path);
601 kprintf("unable to backspace over one of double"
602 " filemarks at end of tape\n");
603 xpt_print_path(periph->path);
604 kprintf("it is possible that this device"
605 " needs a SA_QUIRK_1FM quirk set for it\n");
606 softc->flags |= SA_FLAG_TAPE_FROZEN;
609 break;
610 default:
611 xpt_print_path(periph->path);
612 panic("unknown mode 0x%x in saclose", mode);
613 /* NOTREACHED */
614 break;
618 * We wish to note here that there are no more filemarks to be written.
620 softc->filemarks = 0;
621 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
624 * And we are no longer open for business.
626 softc->flags &= ~closedbits;
629 * Inform users if tape state if frozen....
631 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
632 xpt_print_path(periph->path);
633 kprintf("tape is now frozen- use an OFFLINE, REWIND or MTEOM "
634 "command to clear this state.\n");
637 /* release the device if it is no longer mounted */
638 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
639 sareservereleaseunit(periph, FALSE);
641 cam_periph_unlock(periph);
642 cam_periph_release(periph);
644 return (error);
648 * Actually translate the requested transfer into one the physical driver
649 * can understand. The transfer is described by a buf and will include
650 * only one physical transfer.
652 static int
653 sastrategy(struct dev_strategy_args *ap)
655 cdev_t dev = ap->a_head.a_dev;
656 struct bio *bio = ap->a_bio;
657 struct buf *bp = bio->bio_buf;
658 struct cam_periph *periph;
659 struct sa_softc *softc;
660 u_int unit;
662 if (SA_IS_CTRL(dev)) {
663 bp->b_error = EINVAL;
664 goto bad;
666 unit = SAUNIT(dev);
667 periph = cam_extend_get(saperiphs, unit);
668 if (periph == NULL) {
669 bp->b_error = ENXIO;
670 goto bad;
672 softc = (struct sa_softc *)periph->softc;
674 crit_enter();
676 if (softc->flags & SA_FLAG_INVALID) {
677 crit_exit();
678 bp->b_error = ENXIO;
679 goto bad;
682 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
683 crit_exit();
684 bp->b_error = EPERM;
685 goto bad;
688 crit_exit();
691 * If it's a null transfer, return immediatly
693 if (bp->b_bcount == 0)
694 goto done;
696 /* valid request? */
697 if (softc->flags & SA_FLAG_FIXED) {
699 * Fixed block device. The byte count must
700 * be a multiple of our block size.
702 if (((softc->blk_mask != ~0) &&
703 ((bp->b_bcount & softc->blk_mask) != 0)) ||
704 ((softc->blk_mask == ~0) &&
705 ((bp->b_bcount % softc->min_blk) != 0))) {
706 xpt_print_path(periph->path);
707 kprintf("Invalid request. Fixed block device "
708 "requests must be a multiple "
709 "of %d bytes\n", softc->media_blksize);
710 bp->b_error = EINVAL;
711 goto bad;
713 } else if ((bp->b_bcount > softc->max_blk) ||
714 (bp->b_bcount < softc->min_blk) ||
715 (bp->b_bcount & softc->blk_mask) != 0) {
717 xpt_print_path(periph->path);
718 kprintf("Invalid request. Variable block device "
719 "requests must be ");
720 if (softc->blk_mask != 0) {
721 kprintf("a multiple of %d ", (0x1 << softc->blk_gran));
723 kprintf("between %d and %d bytes\n", softc->min_blk,
724 softc->max_blk);
725 bp->b_error = EINVAL;
726 goto bad;
730 * Mask interrupts so that the device cannot be invalidated until
731 * after we are in the queue. Otherwise, we might not properly
732 * clean up one of the buffers.
734 crit_enter();
737 * Place it at the end of the queue.
739 bioq_insert_tail(&softc->bio_queue, bio);
740 softc->queue_count++;
741 #if 0
742 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
743 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
744 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
745 (bp->bio_cmd == BIO_READ)? "read" : "write"));
746 #endif
747 if (softc->queue_count > 1) {
748 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
749 ("sastrategy: queue count now %d\n", softc->queue_count));
751 crit_exit();
754 * Schedule ourselves for performing the work.
756 xpt_schedule(periph, 1);
758 return(0);
759 bad:
760 bp->b_flags |= B_ERROR;
761 done:
764 * Correctly set the buf to indicate a completed xfer
766 bp->b_resid = bp->b_bcount;
767 biodone(bio);
768 return(0);
771 static int
772 saioctl(struct dev_ioctl_args *ap)
774 cdev_t dev = ap->a_head.a_dev;
775 caddr_t addr = ap->a_data;
776 struct cam_periph *periph;
777 struct sa_softc *softc;
778 scsi_space_code spaceop;
779 int didlockperiph = 0;
780 int unit;
781 int mode;
782 int error = 0;
784 unit = SAUNIT(dev);
785 mode = SAMODE(dev);
786 error = 0; /* shut up gcc */
787 spaceop = 0; /* shut up gcc */
789 periph = cam_extend_get(saperiphs, unit);
790 if (periph == NULL)
791 return (ENXIO);
793 softc = (struct sa_softc *)periph->softc;
796 * Check for control mode accesses. We allow MTIOCGET and
797 * MTIOCERRSTAT (but need to be the only one open in order
798 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
799 * and MTCOMP (but need to be the only one accessing this
800 * device to run those).
803 if (SA_IS_CTRL(dev)) {
804 switch (ap->a_cmd) {
805 case MTIOCGETEOTMODEL:
806 case MTIOCGET:
807 break;
808 case MTIOCERRSTAT:
810 * If the periph isn't already locked, lock it
811 * so our MTIOCERRSTAT can reset latched error stats.
813 * If the periph is already locked, skip it because
814 * we're just getting status and it'll be up to the
815 * other thread that has this device open to do
816 * an MTIOCERRSTAT that would clear latched status.
818 crit_enter();
819 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
820 error = cam_periph_lock(periph, PCATCH);
821 if (error != 0) {
822 crit_exit();
823 return (error);
825 didlockperiph = 1;
827 crit_exit();
828 break;
830 case MTIOCTOP:
832 struct mtop *mt = (struct mtop *) addr;
835 * Check to make sure it's an OP we can perform
836 * with no media inserted.
838 switch (mt->mt_op) {
839 case MTSETBSIZ:
840 case MTSETDNSTY:
841 case MTCOMP:
842 mt = NULL;
843 /* FALLTHROUGH */
844 default:
845 break;
847 if (mt != NULL) {
848 break;
850 /* FALLTHROUGH */
852 case MTIOCSETEOTMODEL:
854 * We need to acquire the peripheral here rather
855 * than at open time because we are sharing writable
856 * access to data structures.
858 crit_enter();
859 error = cam_periph_lock(periph, PCATCH);
860 if (error != 0) {
861 crit_exit();
862 return (error);
864 didlockperiph = 1;
865 crit_exit();
866 break;
868 default:
869 return (EINVAL);
874 * Find the device that the user is talking about
876 switch (ap->a_cmd) {
877 case MTIOCGET:
879 struct mtget *g = (struct mtget *)addr;
882 * If this isn't the control mode device, actually go out
883 * and ask the drive again what it's set to.
885 if (!SA_IS_CTRL(dev)) {
886 u_int8_t write_protect;
887 int comp_enabled, comp_supported;
888 error = sagetparams(periph, SA_PARAM_ALL,
889 &softc->media_blksize, &softc->media_density,
890 &softc->media_numblks, &softc->buffer_mode,
891 &write_protect, &softc->speed, &comp_supported,
892 &comp_enabled, &softc->comp_algorithm, NULL);
893 if (error)
894 break;
895 if (write_protect)
896 softc->flags |= SA_FLAG_TAPE_WP;
897 else
898 softc->flags &= ~SA_FLAG_TAPE_WP;
899 softc->flags &= ~(SA_FLAG_COMP_SUPP|
900 SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP);
901 if (comp_supported) {
902 if (softc->saved_comp_algorithm == 0)
903 softc->saved_comp_algorithm =
904 softc->comp_algorithm;
905 softc->flags |= SA_FLAG_COMP_SUPP;
906 if (comp_enabled)
907 softc->flags |= SA_FLAG_COMP_ENABLED;
908 } else
909 softc->flags |= SA_FLAG_COMP_UNSUPP;
911 bzero(g, sizeof(struct mtget));
912 g->mt_type = MT_ISAR;
913 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
914 g->mt_comp = MT_COMP_UNSUPP;
915 g->mt_comp0 = MT_COMP_UNSUPP;
916 g->mt_comp1 = MT_COMP_UNSUPP;
917 g->mt_comp2 = MT_COMP_UNSUPP;
918 g->mt_comp3 = MT_COMP_UNSUPP;
919 } else {
920 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
921 g->mt_comp = MT_COMP_DISABLED;
922 } else {
923 g->mt_comp = softc->comp_algorithm;
925 g->mt_comp0 = softc->comp_algorithm;
926 g->mt_comp1 = softc->comp_algorithm;
927 g->mt_comp2 = softc->comp_algorithm;
928 g->mt_comp3 = softc->comp_algorithm;
930 g->mt_density = softc->media_density;
931 g->mt_density0 = softc->media_density;
932 g->mt_density1 = softc->media_density;
933 g->mt_density2 = softc->media_density;
934 g->mt_density3 = softc->media_density;
935 g->mt_blksiz = softc->media_blksize;
936 g->mt_blksiz0 = softc->media_blksize;
937 g->mt_blksiz1 = softc->media_blksize;
938 g->mt_blksiz2 = softc->media_blksize;
939 g->mt_blksiz3 = softc->media_blksize;
940 g->mt_fileno = softc->fileno;
941 g->mt_blkno = softc->blkno;
942 g->mt_dsreg = (short) softc->dsreg;
944 * Yes, we know that this is likely to overflow
946 if (softc->last_resid_was_io) {
947 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
948 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
949 softc->last_io_resid = 0;
952 } else {
953 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
954 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
955 softc->last_ctl_resid = 0;
959 error = 0;
960 break;
962 case MTIOCERRSTAT:
964 struct scsi_tape_errors *sep =
965 &((union mterrstat *)addr)->scsi_errstat;
967 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
968 ("saioctl: MTIOCERRSTAT\n"));
970 bzero(sep, sizeof(*sep));
971 sep->io_resid = softc->last_io_resid;
972 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
973 sizeof (sep->io_sense));
974 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
975 sizeof (sep->io_cdb));
976 sep->ctl_resid = softc->last_ctl_resid;
977 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
978 sizeof (sep->ctl_sense));
979 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
980 sizeof (sep->ctl_cdb));
982 if (SA_IS_CTRL(dev) == 0 || didlockperiph)
983 bzero((caddr_t) &softc->errinfo,
984 sizeof (softc->errinfo));
985 error = 0;
986 break;
988 case MTIOCTOP:
990 struct mtop *mt;
991 int count;
993 mt = (struct mtop *)addr;
995 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
996 ("saioctl: op=0x%x count=0x%x\n",
997 mt->mt_op, mt->mt_count));
999 count = mt->mt_count;
1000 switch (mt->mt_op) {
1001 case MTWEOF: /* write an end-of-file marker */
1003 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1004 * flag because by keeping track of filemarks
1005 * we have last written we know ehether or not
1006 * we need to write more when we close the device.
1008 error = sawritefilemarks(periph, count, FALSE);
1009 break;
1010 case MTWSS: /* write a setmark */
1011 error = sawritefilemarks(periph, count, TRUE);
1012 break;
1013 case MTBSR: /* backward space record */
1014 case MTFSR: /* forward space record */
1015 case MTBSF: /* backward space file */
1016 case MTFSF: /* forward space file */
1017 case MTBSS: /* backward space setmark */
1018 case MTFSS: /* forward space setmark */
1019 case MTEOD: /* space to end of recorded medium */
1021 int nmarks;
1023 spaceop = SS_FILEMARKS;
1024 nmarks = softc->filemarks;
1025 error = sacheckeod(periph);
1026 if (error) {
1027 xpt_print_path(periph->path);
1028 kprintf("EOD check prior to spacing failed\n");
1029 softc->flags |= SA_FLAG_EIO_PENDING;
1030 break;
1032 nmarks -= softc->filemarks;
1033 switch(mt->mt_op) {
1034 case MTBSR:
1035 count = -count;
1036 /* FALLTHROUGH */
1037 case MTFSR:
1038 spaceop = SS_BLOCKS;
1039 break;
1040 case MTBSF:
1041 count = -count;
1042 /* FALLTHROUGH */
1043 case MTFSF:
1044 break;
1045 case MTBSS:
1046 count = -count;
1047 /* FALLTHROUGH */
1048 case MTFSS:
1049 spaceop = SS_SETMARKS;
1050 break;
1051 case MTEOD:
1052 spaceop = SS_EOD;
1053 count = 0;
1054 nmarks = 0;
1055 break;
1056 default:
1057 error = EINVAL;
1058 break;
1060 if (error)
1061 break;
1063 nmarks = softc->filemarks;
1065 * XXX: Why are we checking again?
1067 error = sacheckeod(periph);
1068 if (error)
1069 break;
1070 nmarks -= softc->filemarks;
1071 error = saspace(periph, count - nmarks, spaceop);
1073 * At this point, clear that we've written the tape
1074 * and that we've written any filemarks. We really
1075 * don't know what the applications wishes to do next-
1076 * the sacheckeod's will make sure we terminated the
1077 * tape correctly if we'd been writing, but the next
1078 * action the user application takes will set again
1079 * whether we need to write filemarks.
1081 softc->flags &=
1082 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1083 softc->filemarks = 0;
1084 break;
1086 case MTREW: /* rewind */
1087 sacheckeod(periph);
1088 error = sarewind(periph);
1089 /* see above */
1090 softc->flags &=
1091 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1092 softc->flags &= ~SA_FLAG_ERR_PENDING;
1093 softc->filemarks = 0;
1094 break;
1095 case MTERASE: /* erase */
1096 error = saerase(periph, count);
1097 softc->flags &=
1098 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1099 softc->flags &= ~SA_FLAG_ERR_PENDING;
1100 break;
1101 case MTRETENS: /* re-tension tape */
1102 error = saretension(periph);
1103 softc->flags &=
1104 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1105 softc->flags &= ~SA_FLAG_ERR_PENDING;
1106 break;
1107 case MTOFFL: /* rewind and put the drive offline */
1109 sacheckeod(periph);
1110 /* see above */
1111 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1112 softc->filemarks = 0;
1114 error = sarewind(periph);
1115 /* clear the frozen flag anyway */
1116 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1119 * Be sure to allow media removal before ejecting.
1122 saprevent(periph, PR_ALLOW);
1123 if (error == 0) {
1124 error = saloadunload(periph, FALSE);
1125 if (error == 0) {
1126 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1129 break;
1131 case MTNOP: /* no operation, sets status only */
1132 case MTCACHE: /* enable controller cache */
1133 case MTNOCACHE: /* disable controller cache */
1134 error = 0;
1135 break;
1137 case MTSETBSIZ: /* Set block size for device */
1139 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1140 0, 0, 0);
1141 if (error == 0) {
1142 softc->last_media_blksize =
1143 softc->media_blksize;
1144 softc->media_blksize = count;
1145 if (count) {
1146 softc->flags |= SA_FLAG_FIXED;
1147 if (powerof2(count)) {
1148 softc->blk_shift =
1149 ffs(count) - 1;
1150 softc->blk_mask = count - 1;
1151 } else {
1152 softc->blk_mask = ~0;
1153 softc->blk_shift = 0;
1156 * Make the user's desire 'persistent'.
1158 softc->quirks &= ~SA_QUIRK_VARIABLE;
1159 softc->quirks |= SA_QUIRK_FIXED;
1160 } else {
1161 softc->flags &= ~SA_FLAG_FIXED;
1162 if (softc->max_blk == 0) {
1163 softc->max_blk = ~0;
1165 softc->blk_shift = 0;
1166 if (softc->blk_gran != 0) {
1167 softc->blk_mask =
1168 softc->blk_gran - 1;
1169 } else {
1170 softc->blk_mask = 0;
1173 * Make the user's desire 'persistent'.
1175 softc->quirks |= SA_QUIRK_VARIABLE;
1176 softc->quirks &= ~SA_QUIRK_FIXED;
1179 break;
1180 case MTSETDNSTY: /* Set density for device and mode */
1181 if (count > UCHAR_MAX) {
1182 error = EINVAL;
1183 break;
1184 } else {
1185 error = sasetparams(periph, SA_PARAM_DENSITY,
1186 0, count, 0, 0);
1188 break;
1189 case MTCOMP: /* enable compression */
1191 * Some devices don't support compression, and
1192 * don't like it if you ask them for the
1193 * compression page.
1195 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1196 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1197 error = ENODEV;
1198 break;
1200 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1201 0, 0, count, SF_NO_PRINT);
1202 break;
1203 default:
1204 error = EINVAL;
1206 break;
1208 case MTIOCIEOT:
1209 case MTIOCEEOT:
1210 error = 0;
1211 break;
1212 case MTIOCRDSPOS:
1213 error = sardpos(periph, 0, (u_int32_t *) addr);
1214 break;
1215 case MTIOCRDHPOS:
1216 error = sardpos(periph, 1, (u_int32_t *) addr);
1217 break;
1218 case MTIOCSLOCATE:
1219 error = sasetpos(periph, 0, (u_int32_t *) addr);
1220 break;
1221 case MTIOCHLOCATE:
1222 error = sasetpos(periph, 1, (u_int32_t *) addr);
1223 break;
1224 case MTIOCGETEOTMODEL:
1225 error = 0;
1226 if (softc->quirks & SA_QUIRK_1FM)
1227 mode = 1;
1228 else
1229 mode = 2;
1230 *((u_int32_t *) addr) = mode;
1231 break;
1232 case MTIOCSETEOTMODEL:
1233 error = 0;
1234 switch (*((u_int32_t *) addr)) {
1235 case 1:
1236 softc->quirks &= ~SA_QUIRK_2FM;
1237 softc->quirks |= SA_QUIRK_1FM;
1238 break;
1239 case 2:
1240 softc->quirks &= ~SA_QUIRK_1FM;
1241 softc->quirks |= SA_QUIRK_2FM;
1242 break;
1243 default:
1244 error = EINVAL;
1245 break;
1247 break;
1248 default:
1249 error = cam_periph_ioctl(periph, ap->a_cmd, addr, saerror);
1250 break;
1254 * Check to see if we cleared a frozen state
1256 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
1257 switch(ap->a_cmd) {
1258 case MTIOCRDSPOS:
1259 case MTIOCRDHPOS:
1260 case MTIOCSLOCATE:
1261 case MTIOCHLOCATE:
1262 softc->fileno = (daddr_t) -1;
1263 softc->blkno = (daddr_t) -1;
1264 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1265 xpt_print_path(periph->path);
1266 kprintf("tape state now unfrozen.\n");
1267 break;
1268 default:
1269 break;
1272 if (didlockperiph) {
1273 cam_periph_unlock(periph);
1275 return (error);
1278 static void
1279 sainit(void)
1281 cam_status status;
1282 struct cam_path *path;
1285 * Create our extend array for storing the devices we attach to.
1287 saperiphs = cam_extend_new();
1288 if (saperiphs == NULL) {
1289 kprintf("sa: Failed to alloc extend array!\n");
1290 return;
1294 * Install a global async callback.
1296 status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1297 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
1299 if (status == CAM_REQ_CMP) {
1300 /* Register the async callbacks of interrest */
1301 struct ccb_setasync csa; /*
1302 * This is an immediate CCB,
1303 * so using the stack is OK
1305 xpt_setup_ccb(&csa.ccb_h, path, 5);
1306 csa.ccb_h.func_code = XPT_SASYNC_CB;
1307 csa.event_enable = AC_FOUND_DEVICE;
1308 csa.callback = saasync;
1309 csa.callback_arg = NULL;
1310 xpt_action((union ccb *)&csa);
1311 status = csa.ccb_h.status;
1312 xpt_free_path(path);
1315 if (status != CAM_REQ_CMP) {
1316 kprintf("sa: Failed to attach master async callback "
1317 "due to status 0x%x!\n", status);
1321 static void
1322 saoninvalidate(struct cam_periph *periph)
1324 struct sa_softc *softc;
1325 struct buf *q_bp;
1326 struct bio *q_bio;
1327 struct ccb_setasync csa;
1329 softc = (struct sa_softc *)periph->softc;
1332 * De-register any async callbacks.
1334 xpt_setup_ccb(&csa.ccb_h, periph->path,
1335 /* priority */ 5);
1336 csa.ccb_h.func_code = XPT_SASYNC_CB;
1337 csa.event_enable = 0;
1338 csa.callback = saasync;
1339 csa.callback_arg = periph;
1340 xpt_action((union ccb *)&csa);
1342 softc->flags |= SA_FLAG_INVALID;
1345 * We need to be in a critical section here to keep the buffer
1346 * queue from being modified while we traverse it.
1348 crit_enter();
1351 * Return all queued I/O with ENXIO.
1352 * XXX Handle any transactions queued to the card
1353 * with XPT_ABORT_CCB.
1355 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL){
1356 bioq_remove(&softc->bio_queue, q_bio);
1357 q_bp = q_bio->bio_buf;
1358 q_bp->b_resid = q_bp->b_bcount;
1359 q_bp->b_error = ENXIO;
1360 q_bp->b_flags |= B_ERROR;
1361 biodone(q_bio);
1363 softc->queue_count = 0;
1364 crit_exit();
1366 xpt_print_path(periph->path);
1367 kprintf("lost device\n");
1371 static void
1372 sacleanup(struct cam_periph *periph)
1374 struct sa_softc *softc;
1376 softc = (struct sa_softc *)periph->softc;
1378 devstat_remove_entry(&softc->device_stats);
1380 cam_extend_release(saperiphs, periph->unit_number);
1381 xpt_print_path(periph->path);
1382 kprintf("removing device entry\n");
1383 dev_ops_remove(&sa_ops, SA_UNITMASK, SA_UNIT(periph->unit_number));
1384 kfree(softc, M_DEVBUF);
1387 static void
1388 saasync(void *callback_arg, u_int32_t code,
1389 struct cam_path *path, void *arg)
1391 struct cam_periph *periph;
1393 periph = (struct cam_periph *)callback_arg;
1394 switch (code) {
1395 case AC_FOUND_DEVICE:
1397 struct ccb_getdev *cgd;
1398 cam_status status;
1400 cgd = (struct ccb_getdev *)arg;
1401 if (cgd == NULL)
1402 break;
1404 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
1405 break;
1408 * Allocate a peripheral instance for
1409 * this device and start the probe
1410 * process.
1412 status = cam_periph_alloc(saregister, saoninvalidate,
1413 sacleanup, sastart,
1414 "sa", CAM_PERIPH_BIO, cgd->ccb_h.path,
1415 saasync, AC_FOUND_DEVICE, cgd);
1417 if (status != CAM_REQ_CMP
1418 && status != CAM_REQ_INPROG)
1419 kprintf("saasync: Unable to probe new device "
1420 "due to status 0x%x\n", status);
1421 break;
1423 default:
1424 cam_periph_async(periph, code, path, arg);
1425 break;
1429 static cam_status
1430 saregister(struct cam_periph *periph, void *arg)
1432 struct sa_softc *softc;
1433 struct ccb_setasync csa;
1434 struct ccb_getdev *cgd;
1435 caddr_t match;
1436 int i;
1438 cgd = (struct ccb_getdev *)arg;
1439 if (periph == NULL) {
1440 kprintf("saregister: periph was NULL!!\n");
1441 return (CAM_REQ_CMP_ERR);
1444 if (cgd == NULL) {
1445 kprintf("saregister: no getdev CCB, can't register device\n");
1446 return (CAM_REQ_CMP_ERR);
1449 softc = kmalloc(sizeof (*softc), M_DEVBUF, M_INTWAIT | M_ZERO);
1450 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
1451 softc->state = SA_STATE_NORMAL;
1452 softc->fileno = (daddr_t) -1;
1453 softc->blkno = (daddr_t) -1;
1455 bioq_init(&softc->bio_queue);
1456 periph->softc = softc;
1457 cam_extend_set(saperiphs, periph->unit_number, periph);
1460 * See if this device has any quirks.
1462 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1463 (caddr_t)sa_quirk_table,
1464 sizeof(sa_quirk_table)/sizeof(*sa_quirk_table),
1465 sizeof(*sa_quirk_table), scsi_inquiry_match);
1467 if (match != NULL) {
1468 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
1469 softc->last_media_blksize =
1470 ((struct sa_quirk_entry *)match)->prefblk;
1471 #ifdef CAMDEBUG
1472 xpt_print_path(periph->path);
1473 kprintf("found quirk entry %d\n", (int)
1474 (((struct sa_quirk_entry *) match) - sa_quirk_table));
1475 #endif
1476 } else
1477 softc->quirks = SA_QUIRK_NONE;
1480 * The SA driver supports a blocksize, but we don't know the
1481 * blocksize until we media is inserted. So, set a flag to
1482 * indicate that the blocksize is unavailable right now.
1484 devstat_add_entry(&softc->device_stats, "sa", periph->unit_number, 0,
1485 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
1486 DEVSTAT_TYPE_IF_SCSI, DEVSTAT_PRIORITY_TAPE);
1488 dev_ops_add(&sa_ops, SA_UNITMASK, SA_UNIT(periph->unit_number));
1489 make_dev(&sa_ops, SAMINOR(SA_CTLDEV,
1490 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1491 0660, "%s%d.ctl", periph->periph_name, periph->unit_number);
1493 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1494 periph->unit_number, 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR,
1495 0660, "%s%d", periph->periph_name, periph->unit_number);
1497 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1498 periph->unit_number, 0, SA_ATYPE_NR), UID_ROOT, GID_OPERATOR,
1499 0660, "n%s%d", periph->periph_name, periph->unit_number);
1501 make_dev(&sa_ops, SAMINOR(SA_NOT_CTLDEV,
1502 periph->unit_number, 0, SA_ATYPE_ER), UID_ROOT, GID_OPERATOR,
1503 0660, "e%s%d", periph->periph_name, periph->unit_number);
1505 for (i = 0; i < SA_NUM_MODES; i++) {
1507 make_dev(&sa_ops,
1508 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_R),
1509 UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d",
1510 periph->periph_name, periph->unit_number, i);
1512 make_dev(&sa_ops,
1513 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_NR),
1514 UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d",
1515 periph->periph_name, periph->unit_number, i);
1518 make_dev(&sa_ops,
1519 SAMINOR(SA_NOT_CTLDEV, periph->unit_number, i, SA_ATYPE_ER),
1520 UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d",
1521 periph->periph_name, periph->unit_number, i);
1525 * Add an async callback so that we get
1526 * notified if this device goes away.
1528 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
1529 csa.ccb_h.func_code = XPT_SASYNC_CB;
1530 csa.event_enable = AC_LOST_DEVICE;
1531 csa.callback = saasync;
1532 csa.callback_arg = periph;
1533 xpt_action((union ccb *)&csa);
1535 xpt_announce_periph(periph, NULL);
1537 return (CAM_REQ_CMP);
1540 static void
1541 sastart(struct cam_periph *periph, union ccb *start_ccb)
1543 struct sa_softc *softc;
1545 softc = (struct sa_softc *)periph->softc;
1547 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
1550 switch (softc->state) {
1551 case SA_STATE_NORMAL:
1553 /* Pull a buffer from the queue and get going on it */
1554 struct buf *bp;
1555 struct bio *bio;
1558 * See if there is a buf with work for us to do..
1560 crit_enter();
1561 bio = bioq_first(&softc->bio_queue);
1562 if (periph->immediate_priority <= periph->pinfo.priority) {
1563 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1564 ("queuing for immediate ccb\n"));
1565 Set_CCB_Type(start_ccb, SA_CCB_WAITING);
1566 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1567 periph_links.sle);
1568 periph->immediate_priority = CAM_PRIORITY_NONE;
1569 crit_exit();
1570 wakeup(&periph->ccb_list);
1571 } else if (bio == NULL) {
1572 crit_exit();
1573 xpt_release_ccb(start_ccb);
1574 } else if ((softc->flags & SA_FLAG_ERR_PENDING) != 0) {
1575 struct bio *done_bio;
1576 again:
1577 softc->queue_count--;
1578 bioq_remove(&softc->bio_queue, bio);
1579 bp = bio->bio_buf;
1580 bp->b_resid = bp->b_bcount;
1581 done_bio = bio;
1582 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
1584 * We now just clear errors in this case
1585 * and let the residual be the notifier.
1587 bp->b_error = 0;
1588 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
1590 * This can only happen if we're reading
1591 * in fixed length mode. In this case,
1592 * we dump the rest of the list the
1593 * same way.
1595 bp->b_error = 0;
1596 if (bioq_first(&softc->bio_queue) != NULL) {
1597 biodone(done_bio);
1598 goto again;
1600 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
1601 bp->b_error = EIO;
1602 bp->b_flags |= B_ERROR;
1604 bio = bioq_first(&softc->bio_queue);
1606 * Only if we have no other buffers queued up
1607 * do we clear the pending error flag.
1609 if (bio == NULL)
1610 softc->flags &= ~SA_FLAG_ERR_PENDING;
1611 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1612 ("sastart- ERR_PENDING now 0x%x, bio is %sNULL, "
1613 "%d more buffers queued up\n",
1614 (softc->flags & SA_FLAG_ERR_PENDING),
1615 (bio != NULL)? "not " : " ", softc->queue_count));
1616 crit_exit();
1617 xpt_release_ccb(start_ccb);
1618 biodone(done_bio);
1619 } else {
1620 u_int32_t length;
1622 bioq_remove(&softc->bio_queue, bio);
1623 bp = bio->bio_buf;
1624 softc->queue_count--;
1626 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1627 if (softc->blk_shift != 0) {
1628 length =
1629 bp->b_bcount >> softc->blk_shift;
1630 } else if (softc->media_blksize != 0) {
1631 length =
1632 bp->b_bcount / softc->media_blksize;
1633 } else {
1634 bp->b_error = EIO;
1635 xpt_print_path(periph->path);
1636 kprintf("zero blocksize for "
1637 "FIXED length writes?\n");
1638 crit_exit();
1639 biodone(bio);
1640 break;
1642 #if 0
1643 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1644 ("issuing a %d fixed record %s\n",
1645 length, (bp->bio_cmd == BIO_READ)? "read" :
1646 "write"));
1647 #endif
1648 } else {
1649 length = bp->b_bcount;
1650 #if 0
1651 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
1652 ("issuing a %d variable byte %s\n",
1653 length, (bp->bio_cmd == BIO_READ)? "read" :
1654 "write"));
1655 #endif
1657 devstat_start_transaction(&softc->device_stats);
1659 * Some people have theorized that we should
1660 * suppress illegal length indication if we are
1661 * running in variable block mode so that we don't
1662 * have to request sense every time our requested
1663 * block size is larger than the written block.
1664 * The residual information from the ccb allows
1665 * us to identify this situation anyway. The only
1666 * problem with this is that we will not get
1667 * information about blocks that are larger than
1668 * our read buffer unless we set the block size
1669 * in the mode page to something other than 0.
1671 * I believe that this is a non-issue. If user apps
1672 * don't adjust their read size to match our record
1673 * size, that's just life. Anyway, the typical usage
1674 * would be to issue, e.g., 64KB reads and occasionally
1675 * have to do deal with 512 byte or 1KB intermediate
1676 * records.
1678 softc->dsreg = (bp->b_cmd == BUF_CMD_READ) ?
1679 MTIO_DSREG_RD : MTIO_DSREG_WR;
1680 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
1681 MSG_SIMPLE_Q_TAG, (bp->b_cmd == BUF_CMD_READ) != 0,
1682 FALSE, (softc->flags & SA_FLAG_FIXED) != 0,
1683 length, bp->b_data, bp->b_bcount, SSD_FULL_SIZE,
1684 IO_TIMEOUT);
1685 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
1686 Set_CCB_Type(start_ccb, SA_CCB_BUFFER_IO);
1687 start_ccb->ccb_h.ccb_bio = bio;
1688 bio = bioq_first(&softc->bio_queue);
1689 crit_exit();
1690 xpt_action(start_ccb);
1693 if (bio != NULL) {
1694 /* Have more work to do, so ensure we stay scheduled */
1695 xpt_schedule(periph, 1);
1697 break;
1699 case SA_STATE_ABNORMAL:
1700 default:
1701 panic("state 0x%x in sastart", softc->state);
1702 break;
1707 static void
1708 sadone(struct cam_periph *periph, union ccb *done_ccb)
1710 struct sa_softc *softc;
1711 struct ccb_scsiio *csio;
1713 softc = (struct sa_softc *)periph->softc;
1714 csio = &done_ccb->csio;
1715 switch (CCB_Type(csio)) {
1716 case SA_CCB_BUFFER_IO:
1718 struct buf *bp;
1719 struct bio *bio;
1720 int error;
1722 softc->dsreg = MTIO_DSREG_REST;
1723 bio = (struct bio *)done_ccb->ccb_h.ccb_bio;
1724 bp = bio->bio_buf;
1725 error = 0;
1726 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1727 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
1729 * A retry was scheduled, so just return.
1731 return;
1735 if (error == EIO) {
1736 struct buf *q_bp;
1737 struct bio *q_bio;
1740 * Catastrophic error. Mark the tape as frozen
1741 * (we no longer know tape position).
1743 * Return all queued I/O with EIO, and unfreeze
1744 * our queue so that future transactions that
1745 * attempt to fix this problem can get to the
1746 * device.
1750 crit_enter();
1751 softc->flags |= SA_FLAG_TAPE_FROZEN;
1752 while ((q_bio = bioq_first(&softc->bio_queue)) != NULL) {
1753 bioq_remove(&softc->bio_queue, q_bio);
1754 q_bp = q_bio->bio_buf;
1755 q_bp->b_resid = q_bp->b_bcount;
1756 q_bp->b_error = EIO;
1757 q_bp->b_flags |= B_ERROR;
1758 biodone(q_bio);
1760 crit_exit();
1762 if (error != 0) {
1763 bp->b_resid = bp->b_bcount;
1764 bp->b_error = error;
1765 bp->b_flags |= B_ERROR;
1767 * In the error case, position is updated in saerror.
1769 } else {
1770 bp->b_resid = csio->resid;
1771 bp->b_error = 0;
1772 if (csio->resid != 0) {
1773 bp->b_flags |= B_ERROR;
1775 if (bp->b_cmd != BUF_CMD_READ) {
1776 softc->flags |= SA_FLAG_TAPE_WRITTEN;
1777 softc->filemarks = 0;
1779 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
1780 (softc->blkno != (daddr_t) -1)) {
1781 if ((softc->flags & SA_FLAG_FIXED) != 0) {
1782 u_int32_t l;
1783 if (softc->blk_shift != 0) {
1784 l = bp->b_bcount >>
1785 softc->blk_shift;
1786 } else {
1787 l = bp->b_bcount /
1788 softc->media_blksize;
1790 softc->blkno += (daddr_t) l;
1791 } else {
1792 softc->blkno++;
1797 * If we had an error (immediate or pending),
1798 * release the device queue now.
1800 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
1801 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
1802 #ifdef CAMDEBUG
1803 if (error || bp->b_resid) {
1804 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1805 ("error %d resid %d count %d\n", error,
1806 bp->b_resid, bp->b_bcount));
1808 #endif
1809 devstat_end_transaction_buf(&softc->device_stats, bp);
1810 biodone(bio);
1811 break;
1813 case SA_CCB_WAITING:
1815 /* Caller will release the CCB */
1816 wakeup(&done_ccb->ccb_h.cbfcnp);
1817 return;
1820 xpt_release_ccb(done_ccb);
1824 * Mount the tape (make sure it's ready for I/O).
1826 static int
1827 samount(struct cam_periph *periph, int oflags, cdev_t dev)
1829 struct sa_softc *softc;
1830 union ccb *ccb;
1831 int error;
1834 * oflags can be checked for 'kind' of open (read-only check) - later
1835 * dev can be checked for a control-mode or compression open - later
1837 UNUSED_PARAMETER(oflags);
1838 UNUSED_PARAMETER(dev);
1841 softc = (struct sa_softc *)periph->softc;
1844 * This should determine if something has happend since the last
1845 * open/mount that would invalidate the mount. We do *not* want
1846 * to retry this command- we just want the status. But we only
1847 * do this if we're mounted already- if we're not mounted,
1848 * we don't care about the unit read state and can instead use
1849 * this opportunity to attempt to reserve the tape unit.
1852 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
1853 ccb = cam_periph_getccb(periph, 1);
1854 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1855 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1856 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1857 &softc->device_stats);
1858 QFRLS(ccb);
1859 if (error == ENXIO) {
1860 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1861 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1862 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1863 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1864 &softc->device_stats);
1865 QFRLS(ccb);
1866 } else if (error) {
1868 * We don't need to freeze the tape because we
1869 * will now attempt to rewind/load it.
1871 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1872 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
1873 xpt_print_path(periph->path);
1874 kprintf("error %d on TUR in samount\n", error);
1877 } else {
1878 error = sareservereleaseunit(periph, TRUE);
1879 if (error) {
1880 return (error);
1882 ccb = cam_periph_getccb(periph, 1);
1883 scsi_test_unit_ready(&ccb->csio, 0, sadone,
1884 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
1885 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1886 &softc->device_stats);
1887 QFRLS(ccb);
1890 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
1891 struct scsi_read_block_limits_data *rblim = NULL;
1892 int comp_enabled, comp_supported;
1893 u_int8_t write_protect, guessing = 0;
1896 * Clear out old state.
1898 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
1899 SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED|
1900 SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP);
1901 softc->filemarks = 0;
1904 * *Very* first off, make sure we're loaded to BOT.
1906 scsi_load_unload(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
1907 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
1908 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1909 &softc->device_stats);
1910 QFRLS(ccb);
1913 * In case this doesn't work, do a REWIND instead
1915 if (error) {
1916 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
1917 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1918 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1919 &softc->device_stats);
1920 QFRLS(ccb);
1922 if (error) {
1923 xpt_release_ccb(ccb);
1924 goto exit;
1928 * Do a dummy test read to force access to the
1929 * media so that the drive will really know what's
1930 * there. We actually don't really care what the
1931 * blocksize on tape is and don't expect to really
1932 * read a full record.
1934 rblim = kmalloc(8192, M_TEMP, M_INTWAIT);
1936 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
1937 scsi_sa_read_write(&ccb->csio, 0, sadone,
1938 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
1939 (void *) rblim, 8192, SSD_FULL_SIZE,
1940 IO_TIMEOUT);
1941 cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
1942 &softc->device_stats);
1943 QFRLS(ccb);
1944 scsi_rewind(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
1945 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
1946 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1947 SF_NO_PRINT | SF_RETRY_UA,
1948 &softc->device_stats);
1949 QFRLS(ccb);
1950 if (error) {
1951 xpt_print_path(periph->path);
1952 kprintf("unable to rewind after test read\n");
1953 xpt_release_ccb(ccb);
1954 goto exit;
1959 * Next off, determine block limits.
1961 scsi_read_block_limits(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
1962 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
1964 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
1965 SF_NO_PRINT | SF_RETRY_UA, &softc->device_stats);
1967 QFRLS(ccb);
1968 xpt_release_ccb(ccb);
1970 if (error != 0) {
1972 * If it's less than SCSI-2, READ BLOCK LIMITS is not
1973 * a MANDATORY command. Anyway- it doesn't matter-
1974 * we can proceed anyway.
1976 softc->blk_gran = 0;
1977 softc->max_blk = ~0;
1978 softc->min_blk = 0;
1979 } else {
1980 if (softc->scsi_rev >= SCSI_REV_SPC) {
1981 softc->blk_gran = RBL_GRAN(rblim);
1982 } else {
1983 softc->blk_gran = 0;
1986 * We take max_blk == min_blk to mean a default to
1987 * fixed mode- but note that whatever we get out of
1988 * sagetparams below will actually determine whether
1989 * we are actually *in* fixed mode.
1991 softc->max_blk = scsi_3btoul(rblim->maximum);
1992 softc->min_blk = scsi_2btoul(rblim->minimum);
1997 * Next, perform a mode sense to determine
1998 * current density, blocksize, compression etc.
2000 error = sagetparams(periph, SA_PARAM_ALL,
2001 &softc->media_blksize,
2002 &softc->media_density,
2003 &softc->media_numblks,
2004 &softc->buffer_mode, &write_protect,
2005 &softc->speed, &comp_supported,
2006 &comp_enabled, &softc->comp_algorithm,
2007 NULL);
2009 if (error != 0) {
2011 * We could work a little harder here. We could
2012 * adjust our attempts to get information. It
2013 * might be an ancient tape drive. If someone
2014 * nudges us, we'll do that.
2016 goto exit;
2020 * If no quirk has determined that this is a device that is
2021 * preferred to be in fixed or variable mode, now is the time
2022 * to find out.
2024 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
2025 guessing = 1;
2027 * This could be expensive to find out. Luckily we
2028 * only need to do this once. If we start out in
2029 * 'default' mode, try and set ourselves to one
2030 * of the densities that would determine a wad
2031 * of other stuff. Go from highest to lowest.
2033 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
2034 int i;
2035 static u_int8_t ctry[] = {
2036 SCSI_DENSITY_HALFINCH_PE,
2037 SCSI_DENSITY_HALFINCH_6250C,
2038 SCSI_DENSITY_HALFINCH_6250,
2039 SCSI_DENSITY_HALFINCH_1600,
2040 SCSI_DENSITY_HALFINCH_800,
2041 SCSI_DENSITY_QIC_4GB,
2042 SCSI_DENSITY_QIC_2GB,
2043 SCSI_DENSITY_QIC_525_320,
2044 SCSI_DENSITY_QIC_150,
2045 SCSI_DENSITY_QIC_120,
2046 SCSI_DENSITY_QIC_24,
2047 SCSI_DENSITY_QIC_11_9TRK,
2048 SCSI_DENSITY_QIC_11_4TRK,
2049 SCSI_DENSITY_QIC_1320,
2050 SCSI_DENSITY_QIC_3080,
2053 for (i = 0; ctry[i]; i++) {
2054 error = sasetparams(periph,
2055 SA_PARAM_DENSITY, 0, ctry[i],
2056 0, SF_NO_PRINT);
2057 if (error == 0) {
2058 softc->media_density = ctry[i];
2059 break;
2063 switch (softc->media_density) {
2064 case SCSI_DENSITY_QIC_11_4TRK:
2065 case SCSI_DENSITY_QIC_11_9TRK:
2066 case SCSI_DENSITY_QIC_24:
2067 case SCSI_DENSITY_QIC_120:
2068 case SCSI_DENSITY_QIC_150:
2069 case SCSI_DENSITY_QIC_525_320:
2070 case SCSI_DENSITY_QIC_1320:
2071 case SCSI_DENSITY_QIC_3080:
2072 softc->quirks &= ~SA_QUIRK_2FM;
2073 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2074 softc->last_media_blksize = 512;
2075 break;
2076 case SCSI_DENSITY_QIC_4GB:
2077 case SCSI_DENSITY_QIC_2GB:
2078 softc->quirks &= ~SA_QUIRK_2FM;
2079 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
2080 softc->last_media_blksize = 1024;
2081 break;
2082 default:
2083 softc->last_media_blksize =
2084 softc->media_blksize;
2085 softc->quirks |= SA_QUIRK_VARIABLE;
2086 break;
2091 * If no quirk has determined that this is a device that needs
2092 * to have 2 Filemarks at EOD, now is the time to find out.
2095 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
2096 switch (softc->media_density) {
2097 case SCSI_DENSITY_HALFINCH_800:
2098 case SCSI_DENSITY_HALFINCH_1600:
2099 case SCSI_DENSITY_HALFINCH_6250:
2100 case SCSI_DENSITY_HALFINCH_6250C:
2101 case SCSI_DENSITY_HALFINCH_PE:
2102 softc->quirks &= ~SA_QUIRK_1FM;
2103 softc->quirks |= SA_QUIRK_2FM;
2104 break;
2105 default:
2106 break;
2111 * Now validate that some info we got makes sense.
2113 if ((softc->max_blk < softc->media_blksize) ||
2114 (softc->min_blk > softc->media_blksize &&
2115 softc->media_blksize)) {
2116 xpt_print_path(periph->path);
2117 kprintf("BLOCK LIMITS (%d..%d) could not match current "
2118 "block settings (%d)- adjusting\n", softc->min_blk,
2119 softc->max_blk, softc->media_blksize);
2120 softc->max_blk = softc->min_blk =
2121 softc->media_blksize;
2125 * Now put ourselves into the right frame of mind based
2126 * upon quirks...
2128 tryagain:
2130 * If we want to be in FIXED mode and our current blocksize
2131 * is not equal to our last blocksize (if nonzero), try and
2132 * set ourselves to this last blocksize (as the 'preferred'
2133 * block size). The initial quirkmatch at registry sets the
2134 * initial 'last' blocksize. If, for whatever reason, this
2135 * 'last' blocksize is zero, set the blocksize to 512,
2136 * or min_blk if that's larger.
2138 if ((softc->quirks & SA_QUIRK_FIXED) &&
2139 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
2140 (softc->media_blksize != softc->last_media_blksize)) {
2141 softc->media_blksize = softc->last_media_blksize;
2142 if (softc->media_blksize == 0) {
2143 softc->media_blksize = 512;
2144 if (softc->media_blksize < softc->min_blk) {
2145 softc->media_blksize = softc->min_blk;
2148 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2149 softc->media_blksize, 0, 0, SF_NO_PRINT);
2150 if (error) {
2151 xpt_print_path(periph->path);
2152 kprintf("unable to set fixed blocksize to %d\n",
2153 softc->media_blksize);
2154 goto exit;
2158 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
2159 (softc->media_blksize != 0)) {
2160 softc->last_media_blksize = softc->media_blksize;
2161 softc->media_blksize = 0;
2162 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
2163 0, 0, 0, SF_NO_PRINT);
2164 if (error) {
2166 * If this fails and we were guessing, just
2167 * assume that we got it wrong and go try
2168 * fixed block mode. Don't even check against
2169 * density code at this point.
2171 if (guessing) {
2172 softc->quirks &= ~SA_QUIRK_VARIABLE;
2173 softc->quirks |= SA_QUIRK_FIXED;
2174 if (softc->last_media_blksize == 0)
2175 softc->last_media_blksize = 512;
2176 goto tryagain;
2178 xpt_print_path(periph->path);
2179 kprintf("unable to set variable blocksize\n");
2180 goto exit;
2185 * Now that we have the current block size,
2186 * set up some parameters for sastart's usage.
2188 if (softc->media_blksize) {
2189 softc->flags |= SA_FLAG_FIXED;
2190 if (powerof2(softc->media_blksize)) {
2191 softc->blk_shift =
2192 ffs(softc->media_blksize) - 1;
2193 softc->blk_mask = softc->media_blksize - 1;
2194 } else {
2195 softc->blk_mask = ~0;
2196 softc->blk_shift = 0;
2198 } else {
2200 * The SCSI-3 spec allows 0 to mean "unspecified".
2201 * The SCSI-1 spec allows 0 to mean 'infinite'.
2203 * Either works here.
2205 if (softc->max_blk == 0) {
2206 softc->max_blk = ~0;
2208 softc->blk_shift = 0;
2209 if (softc->blk_gran != 0) {
2210 softc->blk_mask = softc->blk_gran - 1;
2211 } else {
2212 softc->blk_mask = 0;
2216 if (write_protect)
2217 softc->flags |= SA_FLAG_TAPE_WP;
2219 if (comp_supported) {
2220 if (softc->saved_comp_algorithm == 0)
2221 softc->saved_comp_algorithm =
2222 softc->comp_algorithm;
2223 softc->flags |= SA_FLAG_COMP_SUPP;
2224 if (comp_enabled)
2225 softc->flags |= SA_FLAG_COMP_ENABLED;
2226 } else
2227 softc->flags |= SA_FLAG_COMP_UNSUPP;
2229 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
2230 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
2231 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
2232 0, 0, SF_NO_PRINT);
2233 if (error == 0) {
2234 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
2235 } else {
2236 xpt_print_path(periph->path);
2237 kprintf("unable to set buffered mode\n");
2239 error = 0; /* not an error */
2243 if (error == 0) {
2244 softc->flags |= SA_FLAG_TAPE_MOUNTED;
2246 exit:
2247 if (rblim != NULL)
2248 kfree(rblim, M_TEMP);
2250 if (error != 0) {
2251 softc->dsreg = MTIO_DSREG_NIL;
2252 } else {
2253 softc->fileno = softc->blkno = 0;
2254 softc->dsreg = MTIO_DSREG_REST;
2256 #ifdef SA_1FM_AT_EOD
2257 if ((softc->quirks & SA_QUIRK_2FM) == 0)
2258 softc->quirks |= SA_QUIRK_1FM;
2259 #else
2260 if ((softc->quirks & SA_QUIRK_1FM) == 0)
2261 softc->quirks |= SA_QUIRK_2FM;
2262 #endif
2263 } else
2264 xpt_release_ccb(ccb);
2267 * If we return an error, we're not mounted any more,
2268 * so release any device reservation.
2270 if (error != 0) {
2271 sareservereleaseunit(periph, FALSE);
2272 } else {
2274 * Clear I/O residual.
2276 softc->last_io_resid = 0;
2277 softc->last_ctl_resid = 0;
2279 return (error);
2283 * How many filemarks do we need to write if we were to terminate the
2284 * tape session right now? Note that this can be a negative number
2287 static int
2288 samarkswanted(struct cam_periph *periph)
2290 int markswanted;
2291 struct sa_softc *softc;
2293 softc = (struct sa_softc *)periph->softc;
2294 markswanted = 0;
2295 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
2296 markswanted++;
2297 if (softc->quirks & SA_QUIRK_2FM)
2298 markswanted++;
2300 markswanted -= softc->filemarks;
2301 return (markswanted);
2304 static int
2305 sacheckeod(struct cam_periph *periph)
2307 int error;
2308 int markswanted;
2310 markswanted = samarkswanted(periph);
2312 if (markswanted > 0) {
2313 error = sawritefilemarks(periph, markswanted, FALSE);
2314 } else {
2315 error = 0;
2317 return (error);
2320 static int
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;
2330 int32_t info = 0;
2331 cam_status status;
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;
2336 csio = &ccb->csio;
2337 sense = &csio->sense_data;
2338 scsi_extract_sense(sense, &error_code, &sense_key, &asc, &ascq);
2339 aqvalid = sense->extra_len >= 6;
2340 error = 0;
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
2347 * unit.
2349 if (status == CAM_SCSI_STATUS_ERROR) {
2350 if ((sense->error_code & SSD_ERRCODE_VALID) != 0) {
2351 info = (int32_t) scsi_4btoul(sense->info);
2352 resid = info;
2353 if ((softc->flags & SA_FLAG_FIXED) != 0)
2354 resid *= softc->media_blksize;
2355 } else {
2356 resid = csio->dxfer_len;
2357 info = resid;
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;
2370 } else {
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));
2383 } else {
2384 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2385 ("Cam Status 0x%x\n", status));
2388 switch (status) {
2389 case CAM_REQ_CMP:
2390 return (0);
2391 case CAM_SCSI_STATUS_ERROR:
2393 * If a read/write command, we handle it here.
2395 if (CCB_Type(csio) != SA_CCB_WAITING) {
2396 break;
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;
2406 QFRLS(ccb);
2407 return (0);
2410 * Otherwise, we let the common code handle this.
2412 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2415 * XXX: To Be Fixed
2416 * We cannot depend upon CAM honoring retry counts for these.
2418 case CAM_SCSI_BUS_RESET:
2419 case CAM_BDR_SENT:
2420 if (ccb->ccb_h.retry_count <= 0) {
2421 return (EIO);
2423 /* FALLTHROUGH */
2424 default:
2425 return (cam_periph_error(ccb, cflgs, sflgs, &softc->saved_ccb));
2429 * Handle filemark, end of tape, mismatched record sizes....
2430 * From this point out, we're only handling read/write cases.
2431 * Handle writes && reads differently.
2434 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
2435 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
2436 csio->resid = resid;
2437 error = ENOSPC;
2438 } else if (sense->flags & SSD_EOM) {
2439 softc->flags |= SA_FLAG_EOM_PENDING;
2441 * Grotesque as it seems, the few times
2442 * I've actually seen a non-zero resid,
2443 * the tape drive actually lied and had
2444 * written all the data!.
2446 csio->resid = 0;
2448 } else {
2449 csio->resid = resid;
2450 if (sense_key == SSD_KEY_BLANK_CHECK) {
2451 if (softc->quirks & SA_QUIRK_1FM) {
2452 error = 0;
2453 softc->flags |= SA_FLAG_EOM_PENDING;
2454 } else {
2455 error = EIO;
2457 } else if (sense->flags & SSD_FILEMARK) {
2458 if (softc->flags & SA_FLAG_FIXED) {
2459 error = -1;
2460 softc->flags |= SA_FLAG_EOF_PENDING;
2463 * Unconditionally, if we detected a filemark on a read,
2464 * mark that we've run moved a file ahead.
2466 if (softc->fileno != (daddr_t) -1) {
2467 softc->fileno++;
2468 softc->blkno = 0;
2469 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
2475 * Incorrect Length usually applies to read, but can apply to writes.
2477 if (error == 0 && (sense->flags & SSD_ILI)) {
2478 if (info < 0) {
2479 xpt_print_path(csio->ccb_h.path);
2480 kprintf(toobig, csio->dxfer_len - info);
2481 csio->resid = csio->dxfer_len;
2482 error = EIO;
2483 } else {
2484 csio->resid = resid;
2485 if (softc->flags & SA_FLAG_FIXED) {
2486 softc->flags |= SA_FLAG_EIO_PENDING;
2489 * Bump the block number if we hadn't seen a filemark.
2490 * Do this independent of errors (we've moved anyway).
2492 if ((sense->flags & SSD_FILEMARK) == 0) {
2493 if (softc->blkno != (daddr_t) -1) {
2494 softc->blkno++;
2495 csio->ccb_h.ccb_pflags |=
2496 SA_POSITION_UPDATED;
2502 if (error <= 0) {
2504 * Unfreeze the queue if frozen as we're not returning anything
2505 * to our waiters that would indicate an I/O error has occurred
2506 * (yet).
2508 QFRLS(ccb);
2509 error = 0;
2511 return (error);
2514 static int
2515 sagetparams(struct cam_periph *periph, sa_params params_to_get,
2516 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
2517 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
2518 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
2519 sa_comp_t *tcs)
2521 union ccb *ccb;
2522 void *mode_buffer;
2523 struct scsi_mode_header_6 *mode_hdr;
2524 struct scsi_mode_blk_desc *mode_blk;
2525 int mode_buffer_len;
2526 struct sa_softc *softc;
2527 u_int8_t cpage;
2528 int error;
2529 cam_status status;
2531 softc = (struct sa_softc *)periph->softc;
2532 ccb = cam_periph_getccb(periph, 1);
2533 if (softc->quirks & SA_QUIRK_NO_CPAGE)
2534 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2535 else
2536 cpage = SA_DATA_COMPRESSION_PAGE;
2538 retry:
2539 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2541 if (params_to_get & SA_PARAM_COMPRESSION) {
2542 if (softc->quirks & SA_QUIRK_NOCOMP) {
2543 *comp_supported = FALSE;
2544 params_to_get &= ~SA_PARAM_COMPRESSION;
2545 } else
2546 mode_buffer_len += sizeof (sa_comp_t);
2549 mode_buffer = kmalloc(mode_buffer_len, M_TEMP, M_INTWAIT | M_ZERO);
2550 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2551 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2553 /* it is safe to retry this */
2554 scsi_mode_sense(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2555 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
2556 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
2557 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2559 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2560 &softc->device_stats);
2561 QFRLS(ccb);
2563 status = ccb->ccb_h.status & CAM_STATUS_MASK;
2565 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
2567 * Hmm. Let's see if we can try another page...
2568 * If we've already done that, give up on compression
2569 * for this device and remember this for the future
2570 * and attempt the request without asking for compression
2571 * info.
2573 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2574 cpage = SA_DEVICE_CONFIGURATION_PAGE;
2575 goto retry;
2577 softc->quirks |= SA_QUIRK_NOCOMP;
2578 kfree(mode_buffer, M_TEMP);
2579 goto retry;
2580 } else if (status == CAM_SCSI_STATUS_ERROR) {
2581 /* Tell the user about the fatal error. */
2582 scsi_sense_print(&ccb->csio);
2583 goto sagetparamsexit;
2587 * If the user only wants the compression information, and
2588 * the device doesn't send back the block descriptor, it's
2589 * no big deal. If the user wants more than just
2590 * compression, though, and the device doesn't pass back the
2591 * block descriptor, we need to send another mode sense to
2592 * get the block descriptor.
2594 if ((mode_hdr->blk_desc_len == 0) &&
2595 (params_to_get & SA_PARAM_COMPRESSION) &&
2596 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
2599 * Decrease the mode buffer length by the size of
2600 * the compression page, to make sure the data
2601 * there doesn't get overwritten.
2603 mode_buffer_len -= sizeof (sa_comp_t);
2606 * Now move the compression page that we presumably
2607 * got back down the memory chunk a little bit so
2608 * it doesn't get spammed.
2610 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
2611 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
2614 * Now, we issue another mode sense and just ask
2615 * for the block descriptor, etc.
2618 scsi_mode_sense(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
2619 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
2620 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
2621 SCSIOP_TIMEOUT);
2623 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2624 &softc->device_stats);
2625 QFRLS(ccb);
2627 if (error != 0)
2628 goto sagetparamsexit;
2631 if (params_to_get & SA_PARAM_BLOCKSIZE)
2632 *blocksize = scsi_3btoul(mode_blk->blklen);
2634 if (params_to_get & SA_PARAM_NUMBLOCKS)
2635 *numblocks = scsi_3btoul(mode_blk->nblocks);
2637 if (params_to_get & SA_PARAM_BUFF_MODE)
2638 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
2640 if (params_to_get & SA_PARAM_DENSITY)
2641 *density = mode_blk->density;
2643 if (params_to_get & SA_PARAM_WP)
2644 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
2646 if (params_to_get & SA_PARAM_SPEED)
2647 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
2649 if (params_to_get & SA_PARAM_COMPRESSION) {
2650 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
2651 if (cpage == SA_DATA_COMPRESSION_PAGE) {
2652 struct scsi_data_compression_page *cp = &ntcs->dcomp;
2653 *comp_supported =
2654 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
2655 *comp_enabled =
2656 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
2657 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
2658 } else {
2659 struct scsi_dev_conf_page *cp = &ntcs->dconf;
2661 * We don't really know whether this device supports
2662 * Data Compression if the the algorithm field is
2663 * zero. Just say we do.
2665 *comp_supported = TRUE;
2666 *comp_enabled =
2667 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
2668 *comp_algorithm = cp->sel_comp_alg;
2670 if (tcs != NULL)
2671 bcopy(ntcs, tcs, sizeof (sa_comp_t));
2674 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2675 int idx;
2676 char *xyz = mode_buffer;
2677 xpt_print_path(periph->path);
2678 kprintf("Mode Sense Data=");
2679 for (idx = 0; idx < mode_buffer_len; idx++)
2680 kprintf(" 0x%02x", xyz[idx] & 0xff);
2681 kprintf("\n");
2684 sagetparamsexit:
2686 xpt_release_ccb(ccb);
2687 kfree(mode_buffer, M_TEMP);
2688 return (error);
2692 * The purpose of this function is to set one of four different parameters
2693 * for a tape drive:
2694 * - blocksize
2695 * - density
2696 * - compression / compression algorithm
2697 * - buffering mode
2699 * The assumption is that this will be called from saioctl(), and therefore
2700 * from a process context. Thus the waiting malloc calls below. If that
2701 * assumption ever changes, the malloc calls should be changed to be
2702 * NOWAIT mallocs.
2704 * Any or all of the four parameters may be set when this function is
2705 * called. It should handle setting more than one parameter at once.
2707 static int
2708 sasetparams(struct cam_periph *periph, sa_params params_to_set,
2709 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
2710 u_int32_t sense_flags)
2712 struct sa_softc *softc;
2713 u_int32_t current_blocksize;
2714 u_int32_t current_calg;
2715 u_int8_t current_density;
2716 u_int8_t current_speed;
2717 int comp_enabled, comp_supported;
2718 void *mode_buffer;
2719 int mode_buffer_len;
2720 struct scsi_mode_header_6 *mode_hdr;
2721 struct scsi_mode_blk_desc *mode_blk;
2722 sa_comp_t *ccomp, *cpage;
2723 int buff_mode;
2724 union ccb *ccb = NULL;
2725 int error;
2727 softc = (struct sa_softc *)periph->softc;
2729 ccomp = kmalloc(sizeof (sa_comp_t), M_TEMP, M_INTWAIT);
2732 * Since it doesn't make sense to set the number of blocks, or
2733 * write protection, we won't try to get the current value. We
2734 * always want to get the blocksize, so we can set it back to the
2735 * proper value.
2737 error = sagetparams(periph,
2738 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
2739 &current_blocksize, &current_density, NULL, &buff_mode, NULL,
2740 &current_speed, &comp_supported, &comp_enabled,
2741 &current_calg, ccomp);
2743 if (error != 0) {
2744 kfree(ccomp, M_TEMP);
2745 return (error);
2748 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
2749 if (params_to_set & SA_PARAM_COMPRESSION)
2750 mode_buffer_len += sizeof (sa_comp_t);
2752 mode_buffer = kmalloc(mode_buffer_len, M_TEMP, M_INTWAIT | M_ZERO);
2754 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
2755 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2757 ccb = cam_periph_getccb(periph, 1);
2759 retry:
2761 if (params_to_set & SA_PARAM_COMPRESSION) {
2762 if (mode_blk) {
2763 cpage = (sa_comp_t *)&mode_blk[1];
2764 } else {
2765 cpage = (sa_comp_t *)&mode_hdr[1];
2767 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2768 cpage->hdr.pagecode &= ~0x80;
2769 } else
2770 cpage = NULL;
2773 * If the caller wants us to set the blocksize, use the one they
2774 * pass in. Otherwise, use the blocksize we got back from the
2775 * mode select above.
2777 if (mode_blk) {
2778 if (params_to_set & SA_PARAM_BLOCKSIZE)
2779 scsi_ulto3b(blocksize, mode_blk->blklen);
2780 else
2781 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2784 * Set density if requested, else preserve old density.
2785 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2786 * devices, else density we've latched up in our softc.
2788 if (params_to_set & SA_PARAM_DENSITY) {
2789 mode_blk->density = density;
2790 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2791 mode_blk->density = SCSI_SAME_DENSITY;
2792 } else {
2793 mode_blk->density = softc->media_density;
2798 * For mode selects, these two fields must be zero.
2800 mode_hdr->data_length = 0;
2801 mode_hdr->medium_type = 0;
2803 /* set the speed to the current value */
2804 mode_hdr->dev_spec = current_speed;
2806 /* if set, set single-initiator buffering mode */
2807 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
2808 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
2811 if (mode_blk)
2812 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
2813 else
2814 mode_hdr->blk_desc_len = 0;
2817 * First, if the user wants us to set the compression algorithm or
2818 * just turn compression on, check to make sure that this drive
2819 * supports compression.
2821 if (params_to_set & SA_PARAM_COMPRESSION) {
2823 * If the compression algorithm is 0, disable compression.
2824 * If the compression algorithm is non-zero, enable
2825 * compression and set the compression type to the
2826 * specified compression algorithm, unless the algorithm is
2827 * MT_COMP_ENABLE. In that case, we look at the
2828 * compression algorithm that is currently set and if it is
2829 * non-zero, we leave it as-is. If it is zero, and we have
2830 * saved a compression algorithm from a time when
2831 * compression was enabled before, set the compression to
2832 * the saved value.
2834 switch (ccomp->hdr.pagecode & ~0x80) {
2835 case SA_DEVICE_CONFIGURATION_PAGE:
2837 struct scsi_dev_conf_page *dcp = &cpage->dconf;
2838 if (calg == 0) {
2839 dcp->sel_comp_alg = SA_COMP_NONE;
2840 break;
2842 if (calg != MT_COMP_ENABLE) {
2843 dcp->sel_comp_alg = calg;
2844 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
2845 softc->saved_comp_algorithm != 0) {
2846 dcp->sel_comp_alg = softc->saved_comp_algorithm;
2848 break;
2850 case SA_DATA_COMPRESSION_PAGE:
2851 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
2852 struct scsi_data_compression_page *dcp = &cpage->dcomp;
2853 if (calg == 0) {
2855 * Disable compression, but leave the
2856 * decompression and the capability bit
2857 * alone.
2859 dcp->dce_and_dcc = SA_DCP_DCC;
2860 dcp->dde_and_red |= SA_DCP_DDE;
2861 break;
2863 /* enable compression && decompression */
2864 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
2865 dcp->dde_and_red |= SA_DCP_DDE;
2867 * If there, use compression algorithm from caller.
2868 * Otherwise, if there's a saved compression algorithm
2869 * and there is no current algorithm, use the saved
2870 * algorithm. Else parrot back what we got and hope
2871 * for the best.
2873 if (calg != MT_COMP_ENABLE) {
2874 scsi_ulto4b(calg, dcp->comp_algorithm);
2875 scsi_ulto4b(calg, dcp->decomp_algorithm);
2876 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
2877 softc->saved_comp_algorithm != 0) {
2878 scsi_ulto4b(softc->saved_comp_algorithm,
2879 dcp->comp_algorithm);
2880 scsi_ulto4b(softc->saved_comp_algorithm,
2881 dcp->decomp_algorithm);
2883 break;
2886 * Compression does not appear to be supported-
2887 * at least via the DATA COMPRESSION page. It
2888 * would be too much to ask us to believe that
2889 * the page itself is supported, but incorrectly
2890 * reports an ability to manipulate data compression,
2891 * so we'll assume that this device doesn't support
2892 * compression. We can just fall through for that.
2894 /* FALLTHROUGH */
2895 default:
2897 * The drive doesn't seem to support compression,
2898 * so turn off the set compression bit.
2900 params_to_set &= ~SA_PARAM_COMPRESSION;
2901 xpt_print_path(periph->path);
2902 kprintf("device does not seem to support compression\n");
2905 * If that was the only thing the user wanted us to set,
2906 * clean up allocated resources and return with
2907 * 'operation not supported'.
2909 if (params_to_set == SA_PARAM_NONE) {
2910 kfree(mode_buffer, M_TEMP);
2911 xpt_release_ccb(ccb);
2912 return (ENODEV);
2916 * That wasn't the only thing the user wanted us to set.
2917 * So, decrease the stated mode buffer length by the
2918 * size of the compression mode page.
2920 mode_buffer_len -= sizeof(sa_comp_t);
2924 /* It is safe to retry this operation */
2925 scsi_mode_select(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG,
2926 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
2927 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
2929 error = cam_periph_runccb(ccb, saerror, 0,
2930 sense_flags, &softc->device_stats);
2931 QFRLS(ccb);
2933 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2934 int idx;
2935 char *xyz = mode_buffer;
2936 xpt_print_path(periph->path);
2937 kprintf("Err%d, Mode Select Data=", error);
2938 for (idx = 0; idx < mode_buffer_len; idx++)
2939 kprintf(" 0x%02x", xyz[idx] & 0xff);
2940 kprintf("\n");
2944 if (error) {
2946 * If we can, try without setting density/blocksize.
2948 if (mode_blk) {
2949 if ((params_to_set &
2950 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
2951 mode_blk = NULL;
2952 goto retry;
2954 } else {
2955 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
2956 cpage = (sa_comp_t *)&mode_blk[1];
2960 * If we were setting the blocksize, and that failed, we
2961 * want to set it to its original value. If we weren't
2962 * setting the blocksize, we don't want to change it.
2964 scsi_ulto3b(current_blocksize, mode_blk->blklen);
2967 * Set density if requested, else preserve old density.
2968 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
2969 * devices, else density we've latched up in our softc.
2971 if (params_to_set & SA_PARAM_DENSITY) {
2972 mode_blk->density = current_density;
2973 } else if (softc->scsi_rev > SCSI_REV_CCS) {
2974 mode_blk->density = SCSI_SAME_DENSITY;
2975 } else {
2976 mode_blk->density = softc->media_density;
2979 if (params_to_set & SA_PARAM_COMPRESSION)
2980 bcopy(ccomp, cpage, sizeof (sa_comp_t));
2983 * The retry count is the only CCB field that might have been
2984 * changed that we care about, so reset it back to 1.
2986 ccb->ccb_h.retry_count = 1;
2987 cam_periph_runccb(ccb, saerror, 0, sense_flags,
2988 &softc->device_stats);
2989 QFRLS(ccb);
2992 xpt_release_ccb(ccb);
2994 if (ccomp != NULL)
2995 kfree(ccomp, M_TEMP);
2997 if (params_to_set & SA_PARAM_COMPRESSION) {
2998 if (error) {
2999 softc->flags &= ~SA_FLAG_COMP_ENABLED;
3001 * Even if we get an error setting compression,
3002 * do not say that we don't support it. We could
3003 * have been wrong, or it may be media specific.
3004 * softc->flags &= ~SA_FLAG_COMP_SUPP;
3006 softc->saved_comp_algorithm = softc->comp_algorithm;
3007 softc->comp_algorithm = 0;
3008 } else {
3009 softc->flags |= SA_FLAG_COMP_ENABLED;
3010 softc->comp_algorithm = calg;
3014 kfree(mode_buffer, M_TEMP);
3015 return (error);
3018 static void
3019 saprevent(struct cam_periph *periph, int action)
3021 struct sa_softc *softc;
3022 union ccb *ccb;
3023 int error, sf;
3025 softc = (struct sa_softc *)periph->softc;
3027 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
3028 return;
3029 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
3030 return;
3033 * We can be quiet about illegal requests.
3035 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3036 sf = 0;
3037 } else
3038 sf = SF_QUIET_IR;
3040 ccb = cam_periph_getccb(periph, 1);
3042 /* It is safe to retry this operation */
3043 scsi_prevent(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, action,
3044 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3046 error = cam_periph_runccb(ccb, saerror, 0, sf, &softc->device_stats);
3047 QFRLS(ccb);
3048 if (error == 0) {
3049 if (action == PR_ALLOW)
3050 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
3051 else
3052 softc->flags |= SA_FLAG_TAPE_LOCKED;
3055 xpt_release_ccb(ccb);
3058 static int
3059 sarewind(struct cam_periph *periph)
3061 union ccb *ccb;
3062 struct sa_softc *softc;
3063 int error;
3065 softc = (struct sa_softc *)periph->softc;
3067 ccb = cam_periph_getccb(periph, 1);
3069 /* It is safe to retry this operation */
3070 scsi_rewind(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3071 SSD_FULL_SIZE, REWIND_TIMEOUT);
3073 softc->dsreg = MTIO_DSREG_REW;
3074 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3075 softc->dsreg = MTIO_DSREG_REST;
3077 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3078 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3080 xpt_release_ccb(ccb);
3081 if (error == 0)
3082 softc->fileno = softc->blkno = (daddr_t) 0;
3083 else
3084 softc->fileno = softc->blkno = (daddr_t) -1;
3085 return (error);
3088 static int
3089 saspace(struct cam_periph *periph, int count, scsi_space_code code)
3091 union ccb *ccb;
3092 struct sa_softc *softc;
3093 int error;
3095 softc = (struct sa_softc *)periph->softc;
3097 ccb = cam_periph_getccb(periph, 1);
3099 /* This cannot be retried */
3101 scsi_space(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, code, count,
3102 SSD_FULL_SIZE, SPACE_TIMEOUT);
3105 * Clear residual because we will be using it.
3107 softc->last_ctl_resid = 0;
3109 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
3110 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3111 softc->dsreg = MTIO_DSREG_REST;
3113 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3114 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3116 xpt_release_ccb(ccb);
3119 * If a spacing operation has failed, we need to invalidate
3120 * this mount.
3122 * If the spacing operation was setmarks or to end of recorded data,
3123 * we no longer know our relative position.
3125 * If the spacing operations was spacing files in reverse, we
3126 * take account of the residual, but still check against less
3127 * than zero- if we've gone negative, we must have hit BOT.
3129 * If the spacing operations was spacing records in reverse and
3130 * we have a residual, we've either hit BOT or hit a filemark.
3131 * In the former case, we know our new record number (0). In
3132 * the latter case, we have absolutely no idea what the real
3133 * record number is- we've stopped between the end of the last
3134 * record in the previous file and the filemark that stopped
3135 * our spacing backwards.
3137 if (error) {
3138 softc->fileno = softc->blkno = (daddr_t) -1;
3139 } else if (code == SS_SETMARKS || code == SS_EOD) {
3140 softc->fileno = softc->blkno = (daddr_t) -1;
3141 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
3142 softc->fileno += (count - softc->last_ctl_resid);
3143 if (softc->fileno < 0) /* we must of hit BOT */
3144 softc->fileno = 0;
3145 softc->blkno = 0;
3146 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
3147 softc->blkno += (count - softc->last_ctl_resid);
3148 if (count < 0) {
3149 if (softc->last_ctl_resid || softc->blkno < 0) {
3150 if (softc->fileno == 0) {
3151 softc->blkno = 0;
3152 } else {
3153 softc->blkno = (daddr_t) -1;
3158 return (error);
3161 static int
3162 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks)
3164 union ccb *ccb;
3165 struct sa_softc *softc;
3166 int error, nwm = 0;
3168 softc = (struct sa_softc *)periph->softc;
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 ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3186 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3188 if (error == 0 && nmarks) {
3189 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3190 nwm = nmarks - softc->last_ctl_resid;
3191 softc->filemarks += nwm;
3194 xpt_release_ccb(ccb);
3197 * Update relative positions (if we're doing that).
3199 if (error) {
3200 softc->fileno = softc->blkno = (daddr_t) -1;
3201 } else if (softc->fileno != (daddr_t) -1) {
3202 softc->fileno += nwm;
3203 softc->blkno = 0;
3205 return (error);
3208 static int
3209 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3211 struct scsi_tape_position_data loc;
3212 union ccb *ccb;
3213 struct sa_softc *softc = (struct sa_softc *)periph->softc;
3214 int error;
3217 * We try and flush any buffered writes here if we were writing
3218 * and we're trying to get hardware block position. It eats
3219 * up performance substantially, but I'm wary of drive firmware.
3221 * I think that *logical* block position is probably okay-
3222 * but hardware block position might have to wait for data
3223 * to hit media to be valid. Caveat Emptor.
3226 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
3227 error = sawritefilemarks(periph, 0, 0);
3228 if (error && error != EACCES)
3229 return (error);
3232 ccb = cam_periph_getccb(periph, 1);
3233 scsi_read_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3234 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3235 softc->dsreg = MTIO_DSREG_RBSY;
3236 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3237 softc->dsreg = MTIO_DSREG_REST;
3238 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3239 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3241 if (error == 0) {
3242 if (loc.flags & SA_RPOS_UNCERTAIN) {
3243 error = EINVAL; /* nothing is certain */
3244 } else {
3245 *blkptr = scsi_4btoul(loc.firstblk);
3249 xpt_release_ccb(ccb);
3250 return (error);
3253 static int
3254 sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
3256 union ccb *ccb;
3257 struct sa_softc *softc;
3258 int error;
3261 * We used to try and flush any buffered writes here.
3262 * Now we push this onto user applications to either
3263 * flush the pending writes themselves (via a zero count
3264 * WRITE FILEMARKS command) or they can trust their tape
3265 * drive to do this correctly for them.
3268 softc = (struct sa_softc *)periph->softc;
3269 ccb = cam_periph_getccb(periph, 1);
3272 scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG,
3273 hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT);
3276 softc->dsreg = MTIO_DSREG_POS;
3277 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3278 softc->dsreg = MTIO_DSREG_REST;
3279 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3280 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, 0);
3281 xpt_release_ccb(ccb);
3283 * Note relative file && block number position as now unknown.
3285 softc->fileno = softc->blkno = (daddr_t) -1;
3286 return (error);
3289 static int
3290 saretension(struct cam_periph *periph)
3292 union ccb *ccb;
3293 struct sa_softc *softc;
3294 int error;
3296 softc = (struct sa_softc *)periph->softc;
3298 ccb = cam_periph_getccb(periph, 1);
3300 /* It is safe to retry this operation */
3301 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3302 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
3304 softc->dsreg = MTIO_DSREG_TEN;
3305 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3306 softc->dsreg = MTIO_DSREG_REST;
3308 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3309 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3310 xpt_release_ccb(ccb);
3311 if (error == 0)
3312 softc->fileno = softc->blkno = (daddr_t) 0;
3313 else
3314 softc->fileno = softc->blkno = (daddr_t) -1;
3315 return (error);
3318 static int
3319 sareservereleaseunit(struct cam_periph *periph, int reserve)
3321 union ccb *ccb;
3322 struct sa_softc *softc;
3323 int error;
3325 softc = (struct sa_softc *)periph->softc;
3326 ccb = cam_periph_getccb(periph, 1);
3328 /* It is safe to retry this operation */
3329 scsi_reserve_release_unit(&ccb->csio, 2, sadone, MSG_SIMPLE_Q_TAG,
3330 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
3331 softc->dsreg = MTIO_DSREG_RBSY;
3332 error = cam_periph_runccb(ccb, saerror, 0,
3333 SF_RETRY_UA | SF_NO_PRINT, &softc->device_stats);
3334 softc->dsreg = MTIO_DSREG_REST;
3335 QFRLS(ccb);
3336 xpt_release_ccb(ccb);
3339 * If the error was Illegal Request, then the device doesn't support
3340 * RESERVE/RELEASE. This is not an error.
3342 if (error == EINVAL) {
3343 error = 0;
3346 return (error);
3349 static int
3350 saloadunload(struct cam_periph *periph, int load)
3352 union ccb *ccb;
3353 struct sa_softc *softc;
3354 int error;
3356 softc = (struct sa_softc *)periph->softc;
3358 ccb = cam_periph_getccb(periph, 1);
3360 /* It is safe to retry this operation */
3361 scsi_load_unload(&ccb->csio, 5, sadone, MSG_SIMPLE_Q_TAG, FALSE,
3362 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
3364 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
3365 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3366 softc->dsreg = MTIO_DSREG_REST;
3367 QFRLS(ccb);
3368 xpt_release_ccb(ccb);
3370 if (error || load == 0)
3371 softc->fileno = softc->blkno = (daddr_t) -1;
3372 else if (error == 0)
3373 softc->fileno = softc->blkno = (daddr_t) 0;
3374 return (error);
3377 static int
3378 saerase(struct cam_periph *periph, int longerase)
3381 union ccb *ccb;
3382 struct sa_softc *softc;
3383 int error;
3385 softc = (struct sa_softc *)periph->softc;
3387 ccb = cam_periph_getccb(periph, 1);
3389 scsi_erase(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, FALSE, longerase,
3390 SSD_FULL_SIZE, ERASE_TIMEOUT);
3392 softc->dsreg = MTIO_DSREG_ZER;
3393 error = cam_periph_runccb(ccb, saerror, 0, 0, &softc->device_stats);
3394 softc->dsreg = MTIO_DSREG_REST;
3396 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
3397 cam_release_devq(ccb->ccb_h.path, 0, 0, 0, FALSE);
3398 xpt_release_ccb(ccb);
3399 return (error);
3402 #endif /* _KERNEL */
3405 * Read tape block limits command.
3407 void
3408 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
3409 void (*cbfcnp)(struct cam_periph *, union ccb *),
3410 u_int8_t tag_action,
3411 struct scsi_read_block_limits_data *rlimit_buf,
3412 u_int8_t sense_len, u_int32_t timeout)
3414 struct scsi_read_block_limits *scsi_cmd;
3416 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3417 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
3418 sizeof(*scsi_cmd), timeout);
3420 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
3421 bzero(scsi_cmd, sizeof(*scsi_cmd));
3422 scsi_cmd->opcode = READ_BLOCK_LIMITS;
3425 void
3426 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
3427 void (*cbfcnp)(struct cam_periph *, union ccb *),
3428 u_int8_t tag_action, int readop, int sli,
3429 int fixed, u_int32_t length, u_int8_t *data_ptr,
3430 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
3432 struct scsi_sa_rw *scsi_cmd;
3434 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
3435 scsi_cmd->opcode = readop ? SA_READ : SA_WRITE;
3436 scsi_cmd->sli_fixed = 0;
3437 if (sli && readop)
3438 scsi_cmd->sli_fixed |= SAR_SLI;
3439 if (fixed)
3440 scsi_cmd->sli_fixed |= SARW_FIXED;
3441 scsi_ulto3b(length, scsi_cmd->length);
3442 scsi_cmd->control = 0;
3444 cam_fill_csio(csio, retries, cbfcnp, readop ? CAM_DIR_IN : CAM_DIR_OUT,
3445 tag_action, data_ptr, dxfer_len, sense_len,
3446 sizeof(*scsi_cmd), timeout);
3449 void
3450 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
3451 void (*cbfcnp)(struct cam_periph *, union ccb *),
3452 u_int8_t tag_action, int immediate, int eot,
3453 int reten, int load, u_int8_t sense_len,
3454 u_int32_t timeout)
3456 struct scsi_load_unload *scsi_cmd;
3458 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
3459 bzero(scsi_cmd, sizeof(*scsi_cmd));
3460 scsi_cmd->opcode = LOAD_UNLOAD;
3461 if (immediate)
3462 scsi_cmd->immediate = SLU_IMMED;
3463 if (eot)
3464 scsi_cmd->eot_reten_load |= SLU_EOT;
3465 if (reten)
3466 scsi_cmd->eot_reten_load |= SLU_RETEN;
3467 if (load)
3468 scsi_cmd->eot_reten_load |= SLU_LOAD;
3470 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3471 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
3474 void
3475 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
3476 void (*cbfcnp)(struct cam_periph *, union ccb *),
3477 u_int8_t tag_action, int immediate, u_int8_t sense_len,
3478 u_int32_t timeout)
3480 struct scsi_rewind *scsi_cmd;
3482 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
3483 bzero(scsi_cmd, sizeof(*scsi_cmd));
3484 scsi_cmd->opcode = REWIND;
3485 if (immediate)
3486 scsi_cmd->immediate = SREW_IMMED;
3488 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3489 0, sense_len, sizeof(*scsi_cmd), timeout);
3492 void
3493 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
3494 void (*cbfcnp)(struct cam_periph *, union ccb *),
3495 u_int8_t tag_action, scsi_space_code code,
3496 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
3498 struct scsi_space *scsi_cmd;
3500 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
3501 scsi_cmd->opcode = SPACE;
3502 scsi_cmd->code = code;
3503 scsi_ulto3b(count, scsi_cmd->count);
3504 scsi_cmd->control = 0;
3506 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3507 0, sense_len, sizeof(*scsi_cmd), timeout);
3510 void
3511 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
3512 void (*cbfcnp)(struct cam_periph *, union ccb *),
3513 u_int8_t tag_action, int immediate, int setmark,
3514 u_int32_t num_marks, u_int8_t sense_len,
3515 u_int32_t timeout)
3517 struct scsi_write_filemarks *scsi_cmd;
3519 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
3520 bzero(scsi_cmd, sizeof(*scsi_cmd));
3521 scsi_cmd->opcode = WRITE_FILEMARKS;
3522 if (immediate)
3523 scsi_cmd->byte2 |= SWFMRK_IMMED;
3524 if (setmark)
3525 scsi_cmd->byte2 |= SWFMRK_WSMK;
3527 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
3529 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3530 0, sense_len, sizeof(*scsi_cmd), timeout);
3534 * The reserve and release unit commands differ only by their opcodes.
3536 void
3537 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
3538 void (*cbfcnp)(struct cam_periph *, union ccb *),
3539 u_int8_t tag_action, int third_party,
3540 int third_party_id, u_int8_t sense_len,
3541 u_int32_t timeout, int reserve)
3543 struct scsi_reserve_release_unit *scsi_cmd;
3545 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
3546 bzero(scsi_cmd, sizeof(*scsi_cmd));
3548 if (reserve)
3549 scsi_cmd->opcode = RESERVE_UNIT;
3550 else
3551 scsi_cmd->opcode = RELEASE_UNIT;
3553 if (third_party) {
3554 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
3555 scsi_cmd->lun_thirdparty |=
3556 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
3559 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3560 0, sense_len, sizeof(*scsi_cmd), timeout);
3563 void
3564 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
3565 void (*cbfcnp)(struct cam_periph *, union ccb *),
3566 u_int8_t tag_action, int immediate, int long_erase,
3567 u_int8_t sense_len, u_int32_t timeout)
3569 struct scsi_erase *scsi_cmd;
3571 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
3572 bzero(scsi_cmd, sizeof(*scsi_cmd));
3574 scsi_cmd->opcode = ERASE;
3576 if (immediate)
3577 scsi_cmd->lun_imm_long |= SE_IMMED;
3579 if (long_erase)
3580 scsi_cmd->lun_imm_long |= SE_LONG;
3582 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
3583 0, sense_len, sizeof(*scsi_cmd), timeout);
3587 * Read Tape Position command.
3589 void
3590 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
3591 void (*cbfcnp)(struct cam_periph *, union ccb *),
3592 u_int8_t tag_action, int hardsoft,
3593 struct scsi_tape_position_data *sbp,
3594 u_int8_t sense_len, u_int32_t timeout)
3596 struct scsi_tape_read_position *scmd;
3598 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
3599 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
3600 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
3601 bzero(scmd, sizeof(*scmd));
3602 scmd->opcode = READ_POSITION;
3603 scmd->byte1 = hardsoft;
3607 * Set Tape Position command.
3609 void
3610 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
3611 void (*cbfcnp)(struct cam_periph *, union ccb *),
3612 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
3613 u_int8_t sense_len, u_int32_t timeout)
3615 struct scsi_tape_locate *scmd;
3617 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
3618 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
3619 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
3620 bzero(scmd, sizeof(*scmd));
3621 scmd->opcode = LOCATE;
3622 if (hardsoft)
3623 scmd->byte1 |= SA_SPOS_BT;
3624 scsi_ulto4b(blkno, scmd->blkaddr);