2 * Implementation of SCSI Sequential Access Peripheral driver for CAM.
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 * Copyright (c) 1999, 2000 Matthew Jacob
7 * Copyright (c) 2013, 2014, 2015 Spectra Logic Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/queue.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/types.h>
44 #include <sys/limits.h>
45 #include <sys/malloc.h>
50 #include <sys/sysctl.h>
51 #include <sys/taskqueue.h>
53 #include <sys/fcntl.h>
54 #include <sys/devicestat.h>
62 #include <cam/cam_ccb.h>
63 #include <cam/cam_periph.h>
64 #include <cam/cam_xpt_periph.h>
65 #include <cam/cam_debug.h>
67 #include <cam/scsi/scsi_all.h>
68 #include <cam/scsi/scsi_message.h>
69 #include <cam/scsi/scsi_sa.h>
76 #define SA_IO_TIMEOUT 32
78 #ifndef SA_SPACE_TIMEOUT
79 #define SA_SPACE_TIMEOUT 1 * 60
81 #ifndef SA_REWIND_TIMEOUT
82 #define SA_REWIND_TIMEOUT 2 * 60
84 #ifndef SA_ERASE_TIMEOUT
85 #define SA_ERASE_TIMEOUT 4 * 60
87 #ifndef SA_REP_DENSITY_TIMEOUT
88 #define SA_REP_DENSITY_TIMEOUT 90
91 #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */
93 #define IO_TIMEOUT (SA_IO_TIMEOUT * 60 * 1000)
94 #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000)
95 #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000)
96 #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000)
97 #define REP_DENSITY_TIMEOUT (SA_REP_DENSITY_TIMEOUT * 60 * 1000)
100 * Additional options that can be set for config: SA_1FM_AT_EOT
103 #ifndef UNUSED_PARAMETER
104 #define UNUSED_PARAMETER(x) x = x
108 if (((ccb)->ccb_h.status & CAM_DEV_QFRZN) != 0) \
109 cam_release_devq((ccb)->ccb_h.path, 0, 0, 0, FALSE)
115 static MALLOC_DEFINE(M_SCSISA, "SCSI sa", "SCSI sequential access buffers");
118 SA_STATE_NORMAL, SA_STATE_ABNORMAL
121 #define ccb_pflags ppriv_field0
122 #define ccb_bp ppriv_ptr1
124 /* bits in ccb_pflags */
125 #define SA_POSITION_UPDATED 0x1
128 SA_FLAG_OPEN = 0x0001,
129 SA_FLAG_FIXED = 0x0002,
130 SA_FLAG_TAPE_LOCKED = 0x0004,
131 SA_FLAG_TAPE_MOUNTED = 0x0008,
132 SA_FLAG_TAPE_WP = 0x0010,
133 SA_FLAG_TAPE_WRITTEN = 0x0020,
134 SA_FLAG_EOM_PENDING = 0x0040,
135 SA_FLAG_EIO_PENDING = 0x0080,
136 SA_FLAG_EOF_PENDING = 0x0100,
137 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
138 SA_FLAG_EOF_PENDING),
139 SA_FLAG_INVALID = 0x0200,
140 SA_FLAG_COMP_ENABLED = 0x0400,
141 SA_FLAG_COMP_SUPP = 0x0800,
142 SA_FLAG_COMP_UNSUPP = 0x1000,
143 SA_FLAG_TAPE_FROZEN = 0x2000,
144 SA_FLAG_PROTECT_SUPP = 0x4000,
146 SA_FLAG_COMPRESSION = (SA_FLAG_COMP_SUPP|SA_FLAG_COMP_ENABLED|
147 SA_FLAG_COMP_UNSUPP),
148 SA_FLAG_SCTX_INIT = 0x8000
152 SA_MODE_REWIND = 0x00,
153 SA_MODE_NOREWIND = 0x01,
154 SA_MODE_OFFLINE = 0x02
158 SA_PARAM_NONE = 0x000,
159 SA_PARAM_BLOCKSIZE = 0x001,
160 SA_PARAM_DENSITY = 0x002,
161 SA_PARAM_COMPRESSION = 0x004,
162 SA_PARAM_BUFF_MODE = 0x008,
163 SA_PARAM_NUMBLOCKS = 0x010,
165 SA_PARAM_SPEED = 0x040,
166 SA_PARAM_DENSITY_EXT = 0x080,
167 SA_PARAM_LBP = 0x100,
172 SA_QUIRK_NONE = 0x000,
173 SA_QUIRK_NOCOMP = 0x001, /* Can't deal with compression at all*/
174 SA_QUIRK_FIXED = 0x002, /* Force fixed mode */
175 SA_QUIRK_VARIABLE = 0x004, /* Force variable mode */
176 SA_QUIRK_2FM = 0x008, /* Needs Two File Marks at EOD */
177 SA_QUIRK_1FM = 0x010, /* No more than 1 File Mark at EOD */
178 SA_QUIRK_NODREAD = 0x020, /* Don't try and dummy read density */
179 SA_QUIRK_NO_MODESEL = 0x040, /* Don't do mode select at all */
180 SA_QUIRK_NO_CPAGE = 0x080, /* Don't use DEVICE COMPRESSION page */
181 SA_QUIRK_NO_LONG_POS = 0x100 /* No long position information */
184 #define SA_QUIRK_BIT_STRING \
196 #define SAMODE(z) (dev2unit(z) & 0x3)
197 #define SA_IS_CTRL(z) (dev2unit(z) & (1 << 4))
199 #define SA_NOT_CTLDEV 0
203 #define SA_ATYPE_NR 1
204 #define SA_ATYPE_ER 2
205 #define SA_NUM_ATYPES 3
207 #define SAMINOR(ctl, access) \
208 ((ctl << 4) | (access & 0x3))
211 struct cdev *ctl_dev;
217 #define SASBADDBASE(sb, indent, data, xfmt, name, type, xsize, desc) \
218 sbuf_printf(sb, "%*s<%s type=\"%s\" size=\"%zd\" " \
219 "fmt=\"%s\" desc=\"%s\">" #xfmt "</%s>\n", indent, "", \
220 #name, #type, xsize, #xfmt, desc ? desc : "", data, #name);
222 #define SASBADDINT(sb, indent, data, fmt, name) \
223 SASBADDBASE(sb, indent, data, fmt, name, int, sizeof(data), \
226 #define SASBADDINTDESC(sb, indent, data, fmt, name, desc) \
227 SASBADDBASE(sb, indent, data, fmt, name, int, sizeof(data), \
230 #define SASBADDUINT(sb, indent, data, fmt, name) \
231 SASBADDBASE(sb, indent, data, fmt, name, uint, sizeof(data), \
234 #define SASBADDUINTDESC(sb, indent, data, fmt, name, desc) \
235 SASBADDBASE(sb, indent, data, fmt, name, uint, sizeof(data), \
238 #define SASBADDFIXEDSTR(sb, indent, data, fmt, name) \
239 SASBADDBASE(sb, indent, data, fmt, name, str, sizeof(data), \
242 #define SASBADDFIXEDSTRDESC(sb, indent, data, fmt, name, desc) \
243 SASBADDBASE(sb, indent, data, fmt, name, str, sizeof(data), \
246 #define SASBADDVARSTR(sb, indent, data, fmt, name, maxlen) \
247 SASBADDBASE(sb, indent, data, fmt, name, str, maxlen, NULL)
249 #define SASBADDVARSTRDESC(sb, indent, data, fmt, name, maxlen, desc) \
250 SASBADDBASE(sb, indent, data, fmt, name, str, maxlen, desc)
252 #define SASBADDNODE(sb, indent, name) { \
253 sbuf_printf(sb, "%*s<%s type=\"%s\">\n", indent, "", #name, \
258 #define SASBADDNODENUM(sb, indent, name, num) { \
259 sbuf_printf(sb, "%*s<%s type=\"%s\" num=\"%d\">\n", indent, "", \
260 #name, "node", num); \
264 #define SASBENDNODE(sb, indent, name) { \
266 sbuf_printf(sb, "%*s</%s>\n", indent, "", #name); \
269 #define SA_DENSITY_TYPES 4
271 struct sa_prot_state {
273 uint32_t prot_method;
280 struct sa_prot_info {
281 struct sa_prot_state cur_prot_state;
282 struct sa_prot_state pending_prot_state;
286 * A table mapping protection parameters to their types and values.
290 mt_param_set_type param_type;
295 } sa_prot_table[] = {
296 { "prot_method", MT_PARAM_SET_UNSIGNED,
297 __offsetof(struct sa_prot_state, prot_method),
298 /*min_val*/ 0, /*max_val*/ 255, NULL },
299 { "pi_length", MT_PARAM_SET_UNSIGNED,
300 __offsetof(struct sa_prot_state, pi_length),
301 /*min_val*/ 0, /*max_val*/ SA_CTRL_DP_PI_LENGTH_MASK, NULL },
302 { "lbp_w", MT_PARAM_SET_UNSIGNED,
303 __offsetof(struct sa_prot_state, lbp_w),
304 /*min_val*/ 0, /*max_val*/ 1, NULL },
305 { "lbp_r", MT_PARAM_SET_UNSIGNED,
306 __offsetof(struct sa_prot_state, lbp_r),
307 /*min_val*/ 0, /*max_val*/ 1, NULL },
308 { "rbdp", MT_PARAM_SET_UNSIGNED,
309 __offsetof(struct sa_prot_state, rbdp),
310 /*min_val*/ 0, /*max_val*/ 1, NULL }
313 #define SA_NUM_PROT_ENTS nitems(sa_prot_table)
315 #define SA_PROT_ENABLED(softc) ((softc->flags & SA_FLAG_PROTECT_SUPP) \
316 && (softc->prot_info.cur_prot_state.initialized != 0) \
317 && (softc->prot_info.cur_prot_state.prot_method != 0))
319 #define SA_PROT_LEN(softc) softc->prot_info.cur_prot_state.pi_length
326 struct cam_periph *periph;
327 struct bio_queue_head bio_queue;
329 struct devstat *device_stats;
332 int num_devs_to_destroy;
343 u_int32_t comp_algorithm;
344 u_int32_t saved_comp_algorithm;
345 u_int32_t media_blksize;
346 u_int32_t last_media_blksize;
347 u_int32_t media_numblks;
348 u_int8_t media_density;
351 u_int8_t dsreg; /* mtio mt_dsreg, redux */
355 int last_resid_was_io;
356 uint8_t density_type_bits[SA_DENSITY_TYPES];
357 int density_info_valid[SA_DENSITY_TYPES];
358 uint8_t density_info[SA_DENSITY_TYPES][SRDS_MAX_LENGTH];
360 struct sa_prot_info prot_info;
366 * Current position information. -1 means that the given value is
367 * unknown. fileno and blkno are always calculated. blkno is
368 * relative to the previous file mark. rep_fileno and rep_blkno
369 * are as reported by the drive, if it supports the long form
370 * report for the READ POSITION command. rep_blkno is relative to
371 * the beginning of the partition.
373 * bop means that the drive is at the beginning of the partition.
374 * eop means that the drive is between early warning and end of
375 * partition, inside the current partition.
376 * bpew means that the position is in a PEWZ (Programmable Early
379 daddr_t partition; /* Absolute from BOT */
380 daddr_t fileno; /* Relative to beginning of partition */
381 daddr_t blkno; /* Relative to last file mark */
382 daddr_t rep_blkno; /* Relative to beginning of partition */
383 daddr_t rep_fileno; /* Relative to beginning of partition */
384 int bop; /* Beginning of Partition */
385 int eop; /* End of Partition */
386 int bpew; /* Beyond Programmable Early Warning */
392 struct scsi_sense_data _last_io_sense;
393 u_int64_t _last_io_resid;
394 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
395 struct scsi_sense_data _last_ctl_sense;
396 u_int64_t _last_ctl_resid;
397 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
398 #define last_io_sense errinfo._last_io_sense
399 #define last_io_resid errinfo._last_io_resid
400 #define last_io_cdb errinfo._last_io_cdb
401 #define last_ctl_sense errinfo._last_ctl_sense
402 #define last_ctl_resid errinfo._last_ctl_resid
403 #define last_ctl_cdb errinfo._last_ctl_cdb
406 * Misc other flags/state
410 open_rdonly : 1, /* open read-only */
411 open_pending_mount : 1, /* open pending mount */
412 ctrl_mode : 1; /* control device open */
414 struct task sysctl_task;
415 struct sysctl_ctx_list sysctl_ctx;
416 struct sysctl_oid *sysctl_tree;
419 struct sa_quirk_entry {
420 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
421 sa_quirks quirks; /* specific quirk type */
422 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
425 static struct sa_quirk_entry sa_quirk_table[] =
428 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
429 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
430 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
433 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
434 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
437 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
438 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
441 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
442 "Python*", "*"}, SA_QUIRK_NODREAD, 0
445 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
446 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
449 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
450 "VIPER 2525 25462", "-011"},
451 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
454 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
455 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
459 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
460 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
464 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
465 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
468 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
469 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
472 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
473 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
476 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
477 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
480 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
481 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
484 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
485 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
487 { /* jreynold@primenet.com */
488 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
489 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
491 { /* mike@sentex.net */
492 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
493 "STT20000*", "*"}, SA_QUIRK_1FM, 0
496 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "SEAGATE",
497 "DAT 06241-XXX", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
500 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
501 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
504 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
505 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
508 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
509 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
512 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
513 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
516 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
517 " SLR*", "*"}, SA_QUIRK_1FM, 0
520 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
521 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
524 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
525 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
529 static d_open_t saopen;
530 static d_close_t saclose;
531 static d_strategy_t sastrategy;
532 static d_ioctl_t saioctl;
533 static periph_init_t sainit;
534 static periph_ctor_t saregister;
535 static periph_oninv_t saoninvalidate;
536 static periph_dtor_t sacleanup;
537 static periph_start_t sastart;
538 static void saasync(void *callback_arg, u_int32_t code,
539 struct cam_path *path, void *arg);
540 static void sadone(struct cam_periph *periph,
541 union ccb *start_ccb);
542 static int saerror(union ccb *ccb, u_int32_t cam_flags,
543 u_int32_t sense_flags);
544 static int samarkswanted(struct cam_periph *);
545 static int sacheckeod(struct cam_periph *periph);
546 static int sagetparams(struct cam_periph *periph,
547 sa_params params_to_get,
548 u_int32_t *blocksize, u_int8_t *density,
549 u_int32_t *numblocks, int *buff_mode,
550 u_int8_t *write_protect, u_int8_t *speed,
551 int *comp_supported, int *comp_enabled,
552 u_int32_t *comp_algorithm,
553 sa_comp_t *comp_page,
554 struct scsi_control_data_prot_subpage
555 *prot_page, int dp_size,
556 int prot_changeable);
557 static int sasetprot(struct cam_periph *periph,
558 struct sa_prot_state *new_prot);
559 static int sasetparams(struct cam_periph *periph,
560 sa_params params_to_set,
561 u_int32_t blocksize, u_int8_t density,
562 u_int32_t comp_algorithm,
563 u_int32_t sense_flags);
564 static int sasetsili(struct cam_periph *periph,
565 struct mtparamset *ps, int num_params);
566 static int saseteotwarn(struct cam_periph *periph,
567 struct mtparamset *ps, int num_params);
568 static void safillprot(struct sa_softc *softc, int *indent,
570 static void sapopulateprots(struct sa_prot_state *cur_state,
571 struct sa_prot_map *new_table,
573 static struct sa_prot_map *safindprotent(char *name, struct sa_prot_map *table,
575 static int sasetprotents(struct cam_periph *periph,
576 struct mtparamset *ps, int num_params);
577 static struct sa_param_ent *safindparament(struct mtparamset *ps);
578 static int saparamsetlist(struct cam_periph *periph,
579 struct mtsetlist *list, int need_copy);
580 static int saextget(struct cdev *dev, struct cam_periph *periph,
581 struct sbuf *sb, struct mtextget *g);
582 static int saparamget(struct sa_softc *softc, struct sbuf *sb);
583 static void saprevent(struct cam_periph *periph, int action);
584 static int sarewind(struct cam_periph *periph);
585 static int saspace(struct cam_periph *periph, int count,
586 scsi_space_code code);
587 static void sadevgonecb(void *arg);
588 static void sasetupdev(struct sa_softc *softc, struct cdev *dev);
589 static int samount(struct cam_periph *, int, struct cdev *);
590 static int saretension(struct cam_periph *periph);
591 static int sareservereleaseunit(struct cam_periph *periph,
593 static int saloadunload(struct cam_periph *periph, int load);
594 static int saerase(struct cam_periph *periph, int longerase);
595 static int sawritefilemarks(struct cam_periph *periph,
596 int nmarks, int setmarks, int immed);
597 static int sagetpos(struct cam_periph *periph);
598 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
599 static int sasetpos(struct cam_periph *periph, int,
601 static void safilldenstypesb(struct sbuf *sb, int *indent,
602 uint8_t *buf, int buf_len,
604 static void safilldensitysb(struct sa_softc *softc, int *indent,
607 #ifndef SA_DEFAULT_IO_SPLIT
608 #define SA_DEFAULT_IO_SPLIT 0
611 static int sa_allow_io_split = SA_DEFAULT_IO_SPLIT;
614 * Tunable to allow the user to set a global allow_io_split value. Note
615 * that this WILL GO AWAY in FreeBSD 11.0. Silently splitting the I/O up
616 * is bad behavior, because it hides the true tape block size from the
619 static SYSCTL_NODE(_kern_cam, OID_AUTO, sa, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
620 "CAM Sequential Access Tape Driver");
621 SYSCTL_INT(_kern_cam_sa, OID_AUTO, allow_io_split, CTLFLAG_RDTUN,
622 &sa_allow_io_split, 0, "Default I/O split value");
624 static struct periph_driver sadriver =
627 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
630 PERIPHDRIVER_DECLARE(sa, sadriver);
632 /* For 2.2-stable support */
637 static struct cdevsw sa_cdevsw = {
638 .d_version = D_VERSION,
642 .d_write = physwrite,
644 .d_strategy = sastrategy,
646 .d_flags = D_TAPE | D_TRACKCLOSE,
650 saopen(struct cdev *dev, int flags, int fmt, struct thread *td)
652 struct cam_periph *periph;
653 struct sa_softc *softc;
656 periph = (struct cam_periph *)dev->si_drv1;
657 if (cam_periph_acquire(periph) != 0) {
661 cam_periph_lock(periph);
663 softc = (struct sa_softc *)periph->softc;
665 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
666 ("saopen(%s): softc=0x%x\n", devtoname(dev), softc->flags));
668 if (SA_IS_CTRL(dev)) {
669 softc->ctrl_mode = 1;
671 cam_periph_unlock(periph);
675 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
676 cam_periph_unlock(periph);
677 cam_periph_release(periph);
681 if (softc->flags & SA_FLAG_OPEN) {
683 } else if (softc->flags & SA_FLAG_INVALID) {
687 * Preserve whether this is a read_only open.
689 softc->open_rdonly = (flags & O_RDWR) == O_RDONLY;
692 * The function samount ensures media is loaded and ready.
693 * It also does a device RESERVE if the tape isn't yet mounted.
695 * If the mount fails and this was a non-blocking open,
696 * make this a 'open_pending_mount' action.
698 error = samount(periph, flags, dev);
699 if (error && (flags & O_NONBLOCK)) {
700 softc->flags |= SA_FLAG_OPEN;
701 softc->open_pending_mount = 1;
703 cam_periph_unhold(periph);
704 cam_periph_unlock(periph);
710 cam_periph_unhold(periph);
711 cam_periph_unlock(periph);
712 cam_periph_release(periph);
716 saprevent(periph, PR_PREVENT);
717 softc->flags |= SA_FLAG_OPEN;
720 cam_periph_unhold(periph);
721 cam_periph_unlock(periph);
726 saclose(struct cdev *dev, int flag, int fmt, struct thread *td)
728 struct cam_periph *periph;
729 struct sa_softc *softc;
730 int mode, error, writing, tmp, i;
731 int closedbits = SA_FLAG_OPEN;
734 periph = (struct cam_periph *)dev->si_drv1;
735 cam_periph_lock(periph);
737 softc = (struct sa_softc *)periph->softc;
739 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
740 ("saclose(%s): softc=0x%x\n", devtoname(dev), softc->flags));
742 softc->open_rdonly = 0;
743 if (SA_IS_CTRL(dev)) {
744 softc->ctrl_mode = 0;
746 cam_periph_unlock(periph);
747 cam_periph_release(periph);
751 if (softc->open_pending_mount) {
752 softc->flags &= ~SA_FLAG_OPEN;
753 softc->open_pending_mount = 0;
755 cam_periph_unlock(periph);
756 cam_periph_release(periph);
760 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
761 cam_periph_unlock(periph);
766 * Were we writing the tape?
768 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
771 * See whether or not we need to write filemarks. If this
772 * fails, we probably have to assume we've lost tape
775 error = sacheckeod(periph);
777 xpt_print(periph->path,
778 "failed to write terminating filemark(s)\n");
779 softc->flags |= SA_FLAG_TAPE_FROZEN;
783 * Whatever we end up doing, allow users to eject tapes from here on.
785 saprevent(periph, PR_ALLOW);
788 * Decide how to end...
790 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
791 closedbits |= SA_FLAG_TAPE_FROZEN;
792 } else switch (mode) {
793 case SA_MODE_OFFLINE:
795 * An 'offline' close is an unconditional release of
796 * frozen && mount conditions, irrespective of whether
797 * these operations succeeded. The reason for this is
798 * to allow at least some kind of programmatic way
799 * around our state getting all fouled up. If somebody
800 * issues an 'offline' command, that will be allowed
803 (void) sarewind(periph);
804 (void) saloadunload(periph, FALSE);
805 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
809 * If the rewind fails, return an error- if anyone cares,
810 * but not overwriting any previous error.
812 * We don't clear the notion of mounted here, but we do
813 * clear the notion of frozen if we successfully rewound.
815 tmp = sarewind(periph);
820 closedbits |= SA_FLAG_TAPE_FROZEN;
823 case SA_MODE_NOREWIND:
825 * If we're not rewinding/unloading the tape, find out
826 * whether we need to back up over one of two filemarks
827 * we wrote (if we wrote two filemarks) so that appends
828 * from this point on will be sane.
830 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
831 tmp = saspace(periph, -1, SS_FILEMARKS);
833 xpt_print(periph->path, "unable to backspace "
834 "over one of double filemarks at end of "
836 xpt_print(periph->path, "it is possible that "
837 "this device needs a SA_QUIRK_1FM quirk set"
839 softc->flags |= SA_FLAG_TAPE_FROZEN;
844 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode);
850 * We wish to note here that there are no more filemarks to be written.
852 softc->filemarks = 0;
853 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
856 * And we are no longer open for business.
858 softc->flags &= ~closedbits;
862 * Invalidate any density information that depends on having tape
863 * media in the drive.
865 for (i = 0; i < SA_DENSITY_TYPES; i++) {
866 if (softc->density_type_bits[i] & SRDS_MEDIA)
867 softc->density_info_valid[i] = 0;
871 * Inform users if tape state if frozen....
873 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
874 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, "
875 "REWIND or MTEOM command to clear this state.\n");
878 /* release the device if it is no longer mounted */
879 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
880 sareservereleaseunit(periph, FALSE);
882 cam_periph_unhold(periph);
883 cam_periph_unlock(periph);
884 cam_periph_release(periph);
890 * Actually translate the requested transfer into one the physical driver
891 * can understand. The transfer is described by a buf and will include
892 * only one physical transfer.
895 sastrategy(struct bio *bp)
897 struct cam_periph *periph;
898 struct sa_softc *softc;
900 bp->bio_resid = bp->bio_bcount;
901 if (SA_IS_CTRL(bp->bio_dev)) {
902 biofinish(bp, NULL, EINVAL);
905 periph = (struct cam_periph *)bp->bio_dev->si_drv1;
906 cam_periph_lock(periph);
908 softc = (struct sa_softc *)periph->softc;
910 if (softc->flags & SA_FLAG_INVALID) {
911 cam_periph_unlock(periph);
912 biofinish(bp, NULL, ENXIO);
916 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
917 cam_periph_unlock(periph);
918 biofinish(bp, NULL, EPERM);
923 * This should actually never occur as the write(2)
924 * system call traps attempts to write to a read-only
927 if (bp->bio_cmd == BIO_WRITE && softc->open_rdonly) {
928 cam_periph_unlock(periph);
929 biofinish(bp, NULL, EBADF);
933 if (softc->open_pending_mount) {
934 int error = samount(periph, 0, bp->bio_dev);
936 cam_periph_unlock(periph);
937 biofinish(bp, NULL, ENXIO);
940 saprevent(periph, PR_PREVENT);
941 softc->open_pending_mount = 0;
945 * If it's a null transfer, return immediately
947 if (bp->bio_bcount == 0) {
948 cam_periph_unlock(periph);
954 if (softc->flags & SA_FLAG_FIXED) {
956 * Fixed block device. The byte count must
957 * be a multiple of our block size.
959 if (((softc->blk_mask != ~0) &&
960 ((bp->bio_bcount & softc->blk_mask) != 0)) ||
961 ((softc->blk_mask == ~0) &&
962 ((bp->bio_bcount % softc->min_blk) != 0))) {
963 xpt_print(periph->path, "Invalid request. Fixed block "
964 "device requests must be a multiple of %d bytes\n",
966 cam_periph_unlock(periph);
967 biofinish(bp, NULL, EINVAL);
970 } else if ((bp->bio_bcount > softc->max_blk) ||
971 (bp->bio_bcount < softc->min_blk) ||
972 (bp->bio_bcount & softc->blk_mask) != 0) {
973 xpt_print_path(periph->path);
974 printf("Invalid request. Variable block "
975 "device requests must be ");
976 if (softc->blk_mask != 0) {
977 printf("a multiple of %d ", (0x1 << softc->blk_gran));
979 printf("between %d and %d bytes\n", softc->min_blk,
981 cam_periph_unlock(periph);
982 biofinish(bp, NULL, EINVAL);
987 * Place it at the end of the queue.
989 bioq_insert_tail(&softc->bio_queue, bp);
990 softc->queue_count++;
992 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
993 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
994 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
995 (bp->bio_cmd == BIO_READ)? "read" : "write"));
997 if (softc->queue_count > 1) {
998 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
999 ("sastrategy: queue count now %d\n", softc->queue_count));
1003 * Schedule ourselves for performing the work.
1005 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1006 cam_periph_unlock(periph);
1012 sasetsili(struct cam_periph *periph, struct mtparamset *ps, int num_params)
1014 uint32_t sili_blocksize;
1015 struct sa_softc *softc;
1019 softc = (struct sa_softc *)periph->softc;
1021 if (ps->value_type != MT_PARAM_SET_SIGNED) {
1022 snprintf(ps->error_str, sizeof(ps->error_str),
1023 "sili is a signed parameter");
1026 if ((ps->value.value_signed < 0)
1027 || (ps->value.value_signed > 1)) {
1028 snprintf(ps->error_str, sizeof(ps->error_str),
1029 "invalid sili value %jd", (intmax_t)ps->value.value_signed);
1033 * We only set the SILI flag in variable block
1034 * mode. You'll get a check condition in fixed
1035 * block mode if things don't line up in any case.
1037 if (softc->flags & SA_FLAG_FIXED) {
1038 snprintf(ps->error_str, sizeof(ps->error_str),
1039 "can't set sili bit in fixed block mode");
1042 if (softc->sili == ps->value.value_signed)
1045 if (ps->value.value_signed == 1)
1050 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
1051 sili_blocksize, 0, 0, SF_QUIET_IR);
1053 snprintf(ps->error_str, sizeof(ps->error_str),
1054 "sasetparams() returned error %d", error);
1058 softc->sili = ps->value.value_signed;
1061 ps->status = MT_PARAM_STATUS_OK;
1065 ps->status = MT_PARAM_STATUS_ERROR;
1073 saseteotwarn(struct cam_periph *periph, struct mtparamset *ps, int num_params)
1075 struct sa_softc *softc;
1079 softc = (struct sa_softc *)periph->softc;
1081 if (ps->value_type != MT_PARAM_SET_SIGNED) {
1082 snprintf(ps->error_str, sizeof(ps->error_str),
1083 "eot_warn is a signed parameter");
1084 ps->status = MT_PARAM_STATUS_ERROR;
1087 if ((ps->value.value_signed < 0)
1088 || (ps->value.value_signed > 1)) {
1089 snprintf(ps->error_str, sizeof(ps->error_str),
1090 "invalid eot_warn value %jd\n",
1091 (intmax_t)ps->value.value_signed);
1092 ps->status = MT_PARAM_STATUS_ERROR;
1095 softc->eot_warn = ps->value.value_signed;
1096 ps->status = MT_PARAM_STATUS_OK;
1098 if (ps->status != MT_PARAM_STATUS_OK)
1105 safillprot(struct sa_softc *softc, int *indent, struct sbuf *sb)
1109 SASBADDNODE(sb, *indent, protection);
1110 if (softc->flags & SA_FLAG_PROTECT_SUPP)
1114 SASBADDINTDESC(sb, *indent, tmpint, %d, protection_supported,
1115 "Set to 1 if protection information is supported");
1118 && (softc->prot_info.cur_prot_state.initialized != 0)) {
1119 struct sa_prot_state *prot;
1121 prot = &softc->prot_info.cur_prot_state;
1123 SASBADDUINTDESC(sb, *indent, prot->prot_method, %u,
1124 prot_method, "Current Protection Method");
1125 SASBADDUINTDESC(sb, *indent, prot->pi_length, %u,
1126 pi_length, "Length of Protection Information");
1127 SASBADDUINTDESC(sb, *indent, prot->lbp_w, %u,
1128 lbp_w, "Check Protection on Writes");
1129 SASBADDUINTDESC(sb, *indent, prot->lbp_r, %u,
1130 lbp_r, "Check and Include Protection on Reads");
1131 SASBADDUINTDESC(sb, *indent, prot->rbdp, %u,
1132 rbdp, "Transfer Protection Information for RECOVER "
1133 "BUFFERED DATA command");
1135 SASBENDNODE(sb, *indent, protection);
1139 sapopulateprots(struct sa_prot_state *cur_state, struct sa_prot_map *new_table,
1144 bcopy(sa_prot_table, new_table, min(table_ents * sizeof(*new_table),
1145 sizeof(sa_prot_table)));
1147 table_ents = min(table_ents, SA_NUM_PROT_ENTS);
1149 for (i = 0; i < table_ents; i++)
1150 new_table[i].value = (uint32_t *)((uint8_t *)cur_state +
1151 new_table[i].offset);
1156 static struct sa_prot_map *
1157 safindprotent(char *name, struct sa_prot_map *table, int table_ents)
1159 char *prot_name = "protection.";
1162 prot_len = strlen(prot_name);
1165 * This shouldn't happen, but we check just in case.
1167 if (strncmp(name, prot_name, prot_len) != 0)
1170 for (i = 0; i < table_ents; i++) {
1171 if (strcmp(&name[prot_len], table[i].name) != 0)
1180 sasetprotents(struct cam_periph *periph, struct mtparamset *ps, int num_params)
1182 struct sa_softc *softc;
1183 struct sa_prot_map prot_ents[SA_NUM_PROT_ENTS];
1184 struct sa_prot_state new_state;
1188 softc = (struct sa_softc *)periph->softc;
1192 * Make sure that this tape drive supports protection information.
1193 * Otherwise we can't set anything.
1195 if ((softc->flags & SA_FLAG_PROTECT_SUPP) == 0) {
1196 snprintf(ps[0].error_str, sizeof(ps[0].error_str),
1197 "Protection information is not supported for this device");
1198 ps[0].status = MT_PARAM_STATUS_ERROR;
1203 * We can't operate with physio(9) splitting enabled, because there
1204 * is no way to insure (especially in variable block mode) that
1205 * what the user writes (with a checksum block at the end) will
1206 * make it into the sa(4) driver intact.
1208 if ((softc->si_flags & SI_NOSPLIT) == 0) {
1209 snprintf(ps[0].error_str, sizeof(ps[0].error_str),
1210 "Protection information cannot be enabled with I/O "
1212 ps[0].status = MT_PARAM_STATUS_ERROR;
1217 * Take the current cached protection state and use that as the
1218 * basis for our new entries.
1220 bcopy(&softc->prot_info.cur_prot_state, &new_state, sizeof(new_state));
1223 * Populate the table mapping property names to pointers into the
1226 sapopulateprots(&new_state, prot_ents, SA_NUM_PROT_ENTS);
1229 * For each parameter the user passed in, make sure the name, type
1230 * and value are valid.
1232 for (i = 0; i < num_params; i++) {
1233 struct sa_prot_map *ent;
1235 ent = safindprotent(ps[i].value_name, prot_ents,
1238 ps[i].status = MT_PARAM_STATUS_ERROR;
1239 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1240 "Invalid protection entry name %s",
1245 if (ent->param_type != ps[i].value_type) {
1246 ps[i].status = MT_PARAM_STATUS_ERROR;
1247 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1248 "Supplied type %d does not match actual type %d",
1249 ps[i].value_type, ent->param_type);
1253 if ((ps[i].value.value_unsigned < ent->min_val)
1254 || (ps[i].value.value_unsigned > ent->max_val)) {
1255 ps[i].status = MT_PARAM_STATUS_ERROR;
1256 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1257 "Value %ju is outside valid range %u - %u",
1258 (uintmax_t)ps[i].value.value_unsigned, ent->min_val,
1263 *(ent->value) = ps[i].value.value_unsigned;
1267 * Actually send the protection settings to the drive.
1269 error = sasetprot(periph, &new_state);
1271 for (i = 0; i < num_params; i++) {
1272 ps[i].status = MT_PARAM_STATUS_ERROR;
1273 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1274 "Unable to set parameter, see dmesg(8)");
1280 * Let the user know that his settings were stored successfully.
1282 for (i = 0; i < num_params; i++)
1283 ps[i].status = MT_PARAM_STATUS_OK;
1289 * Entry handlers generally only handle a single entry. Node handlers will
1290 * handle a contiguous range of parameters to set in a single call.
1293 SA_PARAM_TYPE_ENTRY,
1297 struct sa_param_ent {
1299 sa_param_type param_type;
1300 int (*set_func)(struct cam_periph *periph, struct mtparamset *ps,
1302 } sa_param_table[] = {
1303 {"sili", SA_PARAM_TYPE_ENTRY, sasetsili },
1304 {"eot_warn", SA_PARAM_TYPE_ENTRY, saseteotwarn },
1305 {"protection.", SA_PARAM_TYPE_NODE, sasetprotents }
1308 static struct sa_param_ent *
1309 safindparament(struct mtparamset *ps)
1313 for (i = 0; i < nitems(sa_param_table); i++){
1315 * For entries, we compare all of the characters. For
1316 * nodes, we only compare the first N characters. The node
1317 * handler will decode the rest.
1319 if (sa_param_table[i].param_type == SA_PARAM_TYPE_ENTRY) {
1320 if (strcmp(ps->value_name, sa_param_table[i].name) != 0)
1323 if (strncmp(ps->value_name, sa_param_table[i].name,
1324 strlen(sa_param_table[i].name)) != 0)
1327 return (&sa_param_table[i]);
1334 * Go through a list of parameters, coalescing contiguous parameters with
1335 * the same parent node into a single call to a set_func.
1338 saparamsetlist(struct cam_periph *periph, struct mtsetlist *list,
1343 struct mtparamset *params, *first;
1344 struct sa_param_ent *first_ent;
1349 if (list->num_params == 0)
1354 * Verify that the user has the correct structure size.
1356 if ((list->num_params * sizeof(struct mtparamset)) !=
1358 xpt_print(periph->path, "%s: length of params %d != "
1359 "sizeof(struct mtparamset) %zd * num_params %d\n",
1360 __func__, list->param_len, sizeof(struct mtparamset),
1366 if (need_copy != 0) {
1368 * XXX KDM will dropping the lock cause an issue here?
1370 cam_periph_unlock(periph);
1371 params = malloc(list->param_len, M_SCSISA, M_WAITOK | M_ZERO);
1372 error = copyin(list->params, params, list->param_len);
1373 cam_periph_lock(periph);
1378 params = list->params;
1384 for (i = 0; i < list->num_params; i++) {
1385 struct sa_param_ent *ent;
1387 ent = safindparament(¶ms[i]);
1389 snprintf(params[i].error_str,
1390 sizeof(params[i].error_str),
1391 "%s: cannot find parameter %s", __func__,
1392 params[i].value_name);
1393 params[i].status = MT_PARAM_STATUS_ERROR;
1397 if (first != NULL) {
1398 if (first_ent == ent) {
1400 * We're still in a contiguous list of
1401 * parameters that can be handled by one
1407 error = first_ent->set_func(periph, first,
1418 if (ent->param_type == SA_PARAM_TYPE_NODE) {
1423 error = ent->set_func(periph, ¶ms[i], 1);
1431 first_ent->set_func(periph, first, contig_ents);
1434 if (need_copy != 0) {
1435 if (error != EFAULT) {
1436 cam_periph_unlock(periph);
1437 copyout(params, list->params, list->param_len);
1438 cam_periph_lock(periph);
1440 free(params, M_SCSISA);
1446 sagetparams_common(struct cdev *dev, struct cam_periph *periph)
1448 struct sa_softc *softc;
1449 u_int8_t write_protect;
1450 int comp_enabled, comp_supported, error;
1452 softc = (struct sa_softc *)periph->softc;
1454 if (softc->open_pending_mount)
1457 /* The control device may issue getparams() if there are no opens. */
1458 if (SA_IS_CTRL(dev) && (softc->flags & SA_FLAG_OPEN) != 0)
1461 error = sagetparams(periph, SA_PARAM_ALL, &softc->media_blksize,
1462 &softc->media_density, &softc->media_numblks, &softc->buffer_mode,
1463 &write_protect, &softc->speed, &comp_supported, &comp_enabled,
1464 &softc->comp_algorithm, NULL, NULL, 0, 0);
1468 softc->flags |= SA_FLAG_TAPE_WP;
1470 softc->flags &= ~SA_FLAG_TAPE_WP;
1471 softc->flags &= ~SA_FLAG_COMPRESSION;
1472 if (comp_supported) {
1473 if (softc->saved_comp_algorithm == 0)
1474 softc->saved_comp_algorithm =
1475 softc->comp_algorithm;
1476 softc->flags |= SA_FLAG_COMP_SUPP;
1478 softc->flags |= SA_FLAG_COMP_ENABLED;
1480 softc->flags |= SA_FLAG_COMP_UNSUPP;
1485 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
1486 if (softc->open_pending_mount) { \
1487 error = samount(periph, 0, dev); \
1491 saprevent(periph, PR_PREVENT); \
1492 softc->open_pending_mount = 0; \
1496 saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
1498 struct cam_periph *periph;
1499 struct sa_softc *softc;
1500 scsi_space_code spaceop;
1501 int didlockperiph = 0;
1506 error = 0; /* shut up gcc */
1507 spaceop = 0; /* shut up gcc */
1509 periph = (struct cam_periph *)dev->si_drv1;
1510 cam_periph_lock(periph);
1511 softc = (struct sa_softc *)periph->softc;
1514 * Check for control mode accesses. We allow MTIOCGET and
1515 * MTIOCERRSTAT (but need to be the only one open in order
1516 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
1517 * and MTCOMP (but need to be the only one accessing this
1518 * device to run those).
1521 if (SA_IS_CTRL(dev)) {
1523 case MTIOCGETEOTMODEL:
1531 * If the periph isn't already locked, lock it
1532 * so our MTIOCERRSTAT can reset latched error stats.
1534 * If the periph is already locked, skip it because
1535 * we're just getting status and it'll be up to the
1536 * other thread that has this device open to do
1537 * an MTIOCERRSTAT that would clear latched status.
1539 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
1540 error = cam_periph_hold(periph, PRIBIO|PCATCH);
1542 cam_periph_unlock(periph);
1551 struct mtop *mt = (struct mtop *) arg;
1554 * Check to make sure it's an OP we can perform
1555 * with no media inserted.
1557 switch (mt->mt_op) {
1571 case MTIOCSETEOTMODEL:
1573 * We need to acquire the peripheral here rather
1574 * than at open time because we are sharing writable
1575 * access to data structures.
1577 error = cam_periph_hold(periph, PRIBIO|PCATCH);
1579 cam_periph_unlock(periph);
1586 cam_periph_unlock(periph);
1592 * Find the device that the user is talking about
1597 struct mtget *g = (struct mtget *)arg;
1599 error = sagetparams_common(dev, periph);
1602 bzero(g, sizeof(struct mtget));
1603 g->mt_type = MT_ISAR;
1604 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
1605 g->mt_comp = MT_COMP_UNSUPP;
1606 g->mt_comp0 = MT_COMP_UNSUPP;
1607 g->mt_comp1 = MT_COMP_UNSUPP;
1608 g->mt_comp2 = MT_COMP_UNSUPP;
1609 g->mt_comp3 = MT_COMP_UNSUPP;
1611 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
1612 g->mt_comp = MT_COMP_DISABLED;
1614 g->mt_comp = softc->comp_algorithm;
1616 g->mt_comp0 = softc->comp_algorithm;
1617 g->mt_comp1 = softc->comp_algorithm;
1618 g->mt_comp2 = softc->comp_algorithm;
1619 g->mt_comp3 = softc->comp_algorithm;
1621 g->mt_density = softc->media_density;
1622 g->mt_density0 = softc->media_density;
1623 g->mt_density1 = softc->media_density;
1624 g->mt_density2 = softc->media_density;
1625 g->mt_density3 = softc->media_density;
1626 g->mt_blksiz = softc->media_blksize;
1627 g->mt_blksiz0 = softc->media_blksize;
1628 g->mt_blksiz1 = softc->media_blksize;
1629 g->mt_blksiz2 = softc->media_blksize;
1630 g->mt_blksiz3 = softc->media_blksize;
1631 g->mt_fileno = softc->fileno;
1632 g->mt_blkno = softc->blkno;
1633 g->mt_dsreg = (short) softc->dsreg;
1635 * Yes, we know that this is likely to overflow
1637 if (softc->last_resid_was_io) {
1638 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
1639 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1640 softc->last_io_resid = 0;
1644 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
1645 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1646 softc->last_ctl_resid = 0;
1656 struct mtextget *g = (struct mtextget *)arg;
1661 * Report drive status using an XML format.
1665 * XXX KDM will dropping the lock cause any problems here?
1667 cam_periph_unlock(periph);
1668 sb = sbuf_new(NULL, NULL, g->alloc_len, SBUF_FIXEDLEN);
1670 g->status = MT_EXT_GET_ERROR;
1671 snprintf(g->error_str, sizeof(g->error_str),
1672 "Unable to allocate %d bytes for status info",
1674 cam_periph_lock(periph);
1675 goto extget_bailout;
1677 cam_periph_lock(periph);
1679 if (cmd == MTIOCEXTGET)
1680 error = saextget(dev, periph, sb, g);
1682 error = saparamget(softc, sb);
1685 goto extget_bailout;
1687 error = sbuf_finish(sb);
1688 if (error == ENOMEM) {
1689 g->status = MT_EXT_GET_NEED_MORE_SPACE;
1691 } else if (error != 0) {
1692 g->status = MT_EXT_GET_ERROR;
1693 snprintf(g->error_str, sizeof(g->error_str),
1694 "Error %d returned from sbuf_finish()", error);
1696 g->status = MT_EXT_GET_OK;
1699 tmpstr2 = sbuf_data(sb);
1700 g->fill_len = strlen(tmpstr2) + 1;
1701 cam_periph_unlock(periph);
1703 error = copyout(tmpstr2, g->status_xml, g->fill_len);
1705 cam_periph_lock(periph);
1713 struct mtsetlist list;
1714 struct mtparamset *ps = (struct mtparamset *)arg;
1716 bzero(&list, sizeof(list));
1717 list.num_params = 1;
1718 list.param_len = sizeof(*ps);
1721 error = saparamsetlist(periph, &list, /*need_copy*/ 0);
1726 struct mtsetlist *list = (struct mtsetlist *)arg;
1728 error = saparamsetlist(periph, list, /*need_copy*/ 1);
1733 struct scsi_tape_errors *sep =
1734 &((union mterrstat *)arg)->scsi_errstat;
1736 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1737 ("saioctl: MTIOCERRSTAT\n"));
1739 bzero(sep, sizeof(*sep));
1740 sep->io_resid = softc->last_io_resid;
1741 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1742 sizeof (sep->io_sense));
1743 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1744 sizeof (sep->io_cdb));
1745 sep->ctl_resid = softc->last_ctl_resid;
1746 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1747 sizeof (sep->ctl_sense));
1748 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1749 sizeof (sep->ctl_cdb));
1751 if ((SA_IS_CTRL(dev) == 0 && !softc->open_pending_mount) ||
1753 bzero((caddr_t) &softc->errinfo,
1754 sizeof (softc->errinfo));
1763 PENDING_MOUNT_CHECK(softc, periph, dev);
1765 mt = (struct mtop *)arg;
1767 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1768 ("saioctl: op=0x%x count=0x%x\n",
1769 mt->mt_op, mt->mt_count));
1771 count = mt->mt_count;
1772 switch (mt->mt_op) {
1773 case MTWEOF: /* write an end-of-file marker */
1775 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1776 * flag because by keeping track of filemarks
1777 * we have last written we know whether or not
1778 * we need to write more when we close the device.
1780 error = sawritefilemarks(periph, count, FALSE, FALSE);
1783 /* write an end-of-file marker without waiting */
1784 error = sawritefilemarks(periph, count, FALSE, TRUE);
1786 case MTWSS: /* write a setmark */
1787 error = sawritefilemarks(periph, count, TRUE, FALSE);
1789 case MTBSR: /* backward space record */
1790 case MTFSR: /* forward space record */
1791 case MTBSF: /* backward space file */
1792 case MTFSF: /* forward space file */
1793 case MTBSS: /* backward space setmark */
1794 case MTFSS: /* forward space setmark */
1795 case MTEOD: /* space to end of recorded medium */
1799 spaceop = SS_FILEMARKS;
1800 nmarks = softc->filemarks;
1801 error = sacheckeod(periph);
1803 xpt_print(periph->path,
1804 "EOD check prior to spacing failed\n");
1805 softc->flags |= SA_FLAG_EIO_PENDING;
1808 nmarks -= softc->filemarks;
1814 spaceop = SS_BLOCKS;
1825 spaceop = SS_SETMARKS;
1839 nmarks = softc->filemarks;
1841 * XXX: Why are we checking again?
1843 error = sacheckeod(periph);
1846 nmarks -= softc->filemarks;
1847 error = saspace(periph, count - nmarks, spaceop);
1849 * At this point, clear that we've written the tape
1850 * and that we've written any filemarks. We really
1851 * don't know what the applications wishes to do next-
1852 * the sacheckeod's will make sure we terminated the
1853 * tape correctly if we'd been writing, but the next
1854 * action the user application takes will set again
1855 * whether we need to write filemarks.
1858 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1859 softc->filemarks = 0;
1862 case MTREW: /* rewind */
1863 PENDING_MOUNT_CHECK(softc, periph, dev);
1864 (void) sacheckeod(periph);
1865 error = sarewind(periph);
1868 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1869 softc->flags &= ~SA_FLAG_ERR_PENDING;
1870 softc->filemarks = 0;
1872 case MTERASE: /* erase */
1873 PENDING_MOUNT_CHECK(softc, periph, dev);
1874 error = saerase(periph, count);
1876 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1877 softc->flags &= ~SA_FLAG_ERR_PENDING;
1879 case MTRETENS: /* re-tension tape */
1880 PENDING_MOUNT_CHECK(softc, periph, dev);
1881 error = saretension(periph);
1883 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1884 softc->flags &= ~SA_FLAG_ERR_PENDING;
1886 case MTOFFL: /* rewind and put the drive offline */
1888 PENDING_MOUNT_CHECK(softc, periph, dev);
1890 (void) sacheckeod(periph);
1892 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1893 softc->filemarks = 0;
1895 error = sarewind(periph);
1896 /* clear the frozen flag anyway */
1897 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1900 * Be sure to allow media removal before ejecting.
1903 saprevent(periph, PR_ALLOW);
1905 error = saloadunload(periph, FALSE);
1907 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1913 error = saloadunload(periph, TRUE);
1915 case MTNOP: /* no operation, sets status only */
1916 case MTCACHE: /* enable controller cache */
1917 case MTNOCACHE: /* disable controller cache */
1921 case MTSETBSIZ: /* Set block size for device */
1923 PENDING_MOUNT_CHECK(softc, periph, dev);
1925 if ((softc->sili != 0)
1927 xpt_print(periph->path, "Can't enter fixed "
1928 "block mode with SILI enabled\n");
1932 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1935 softc->last_media_blksize =
1936 softc->media_blksize;
1937 softc->media_blksize = count;
1939 softc->flags |= SA_FLAG_FIXED;
1940 if (powerof2(count)) {
1943 softc->blk_mask = count - 1;
1945 softc->blk_mask = ~0;
1946 softc->blk_shift = 0;
1949 * Make the user's desire 'persistent'.
1951 softc->quirks &= ~SA_QUIRK_VARIABLE;
1952 softc->quirks |= SA_QUIRK_FIXED;
1954 softc->flags &= ~SA_FLAG_FIXED;
1955 if (softc->max_blk == 0) {
1956 softc->max_blk = ~0;
1958 softc->blk_shift = 0;
1959 if (softc->blk_gran != 0) {
1961 softc->blk_gran - 1;
1963 softc->blk_mask = 0;
1966 * Make the user's desire 'persistent'.
1968 softc->quirks |= SA_QUIRK_VARIABLE;
1969 softc->quirks &= ~SA_QUIRK_FIXED;
1973 case MTSETDNSTY: /* Set density for device and mode */
1974 PENDING_MOUNT_CHECK(softc, periph, dev);
1976 if (count > UCHAR_MAX) {
1980 error = sasetparams(periph, SA_PARAM_DENSITY,
1984 case MTCOMP: /* enable compression */
1985 PENDING_MOUNT_CHECK(softc, periph, dev);
1987 * Some devices don't support compression, and
1988 * don't like it if you ask them for the
1991 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
1992 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
1996 error = sasetparams(periph, SA_PARAM_COMPRESSION,
1997 0, 0, count, SF_NO_PRINT);
2009 PENDING_MOUNT_CHECK(softc, periph, dev);
2010 error = sardpos(periph, 0, (u_int32_t *) arg);
2013 PENDING_MOUNT_CHECK(softc, periph, dev);
2014 error = sardpos(periph, 1, (u_int32_t *) arg);
2017 case MTIOCHLOCATE: {
2018 struct mtlocate locate_info;
2021 bzero(&locate_info, sizeof(locate_info));
2022 locate_info.logical_id = *((uint32_t *)arg);
2023 if (cmd == MTIOCSLOCATE)
2028 PENDING_MOUNT_CHECK(softc, periph, dev);
2030 error = sasetpos(periph, hard, &locate_info);
2033 case MTIOCEXTLOCATE:
2034 PENDING_MOUNT_CHECK(softc, periph, dev);
2035 error = sasetpos(periph, /*hard*/ 0, (struct mtlocate *)arg);
2037 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
2038 softc->flags &= ~SA_FLAG_ERR_PENDING;
2039 softc->filemarks = 0;
2041 case MTIOCGETEOTMODEL:
2043 if (softc->quirks & SA_QUIRK_1FM)
2047 *((u_int32_t *) arg) = mode;
2049 case MTIOCSETEOTMODEL:
2051 switch (*((u_int32_t *) arg)) {
2053 softc->quirks &= ~SA_QUIRK_2FM;
2054 softc->quirks |= SA_QUIRK_1FM;
2057 softc->quirks &= ~SA_QUIRK_1FM;
2058 softc->quirks |= SA_QUIRK_2FM;
2066 struct mtrblim *rblim;
2068 rblim = (struct mtrblim *)arg;
2070 rblim->granularity = softc->blk_gran;
2071 rblim->min_block_length = softc->min_blk;
2072 rblim->max_block_length = softc->max_blk;
2076 error = cam_periph_ioctl(periph, cmd, arg, saerror);
2081 * Check to see if we cleared a frozen state
2083 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
2090 * XXX KDM look at this.
2092 softc->fileno = (daddr_t) -1;
2093 softc->blkno = (daddr_t) -1;
2094 softc->rep_blkno = (daddr_t) -1;
2095 softc->rep_fileno = (daddr_t) -1;
2096 softc->partition = (daddr_t) -1;
2097 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
2098 xpt_print(periph->path,
2099 "tape state now unfrozen.\n");
2105 if (didlockperiph) {
2106 cam_periph_unhold(periph);
2108 cam_periph_unlock(periph);
2118 * Install a global async callback.
2120 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL);
2122 if (status != CAM_REQ_CMP) {
2123 printf("sa: Failed to attach master async callback "
2124 "due to status 0x%x!\n", status);
2129 sadevgonecb(void *arg)
2131 struct cam_periph *periph;
2133 struct sa_softc *softc;
2135 periph = (struct cam_periph *)arg;
2136 softc = (struct sa_softc *)periph->softc;
2138 mtx = cam_periph_mtx(periph);
2141 softc->num_devs_to_destroy--;
2142 if (softc->num_devs_to_destroy == 0) {
2146 * When we have gotten all of our callbacks, we will get
2147 * no more close calls from devfs. So if we have any
2148 * dangling opens, we need to release the reference held
2149 * for that particular context.
2151 for (i = 0; i < softc->open_count; i++)
2152 cam_periph_release_locked(periph);
2154 softc->open_count = 0;
2157 * Release the reference held for devfs, all of our
2158 * instances are gone now.
2160 cam_periph_release_locked(periph);
2164 * We reference the lock directly here, instead of using
2165 * cam_periph_unlock(). The reason is that the final call to
2166 * cam_periph_release_locked() above could result in the periph
2167 * getting freed. If that is the case, dereferencing the periph
2168 * with a cam_periph_unlock() call would cause a page fault.
2174 saoninvalidate(struct cam_periph *periph)
2176 struct sa_softc *softc;
2178 softc = (struct sa_softc *)periph->softc;
2181 * De-register any async callbacks.
2183 xpt_register_async(0, saasync, periph, periph->path);
2185 softc->flags |= SA_FLAG_INVALID;
2188 * Return all queued I/O with ENXIO.
2189 * XXX Handle any transactions queued to the card
2190 * with XPT_ABORT_CCB.
2192 bioq_flush(&softc->bio_queue, NULL, ENXIO);
2193 softc->queue_count = 0;
2196 * Tell devfs that all of our devices have gone away, and ask for a
2197 * callback when it has cleaned up its state.
2199 destroy_dev_sched_cb(softc->devs.ctl_dev, sadevgonecb, periph);
2200 destroy_dev_sched_cb(softc->devs.r_dev, sadevgonecb, periph);
2201 destroy_dev_sched_cb(softc->devs.nr_dev, sadevgonecb, periph);
2202 destroy_dev_sched_cb(softc->devs.er_dev, sadevgonecb, periph);
2206 sacleanup(struct cam_periph *periph)
2208 struct sa_softc *softc;
2210 softc = (struct sa_softc *)periph->softc;
2212 cam_periph_unlock(periph);
2214 if ((softc->flags & SA_FLAG_SCTX_INIT) != 0
2215 && sysctl_ctx_free(&softc->sysctl_ctx) != 0)
2216 xpt_print(periph->path, "can't remove sysctl context\n");
2218 cam_periph_lock(periph);
2220 devstat_remove_entry(softc->device_stats);
2222 free(softc, M_SCSISA);
2226 saasync(void *callback_arg, u_int32_t code,
2227 struct cam_path *path, void *arg)
2229 struct cam_periph *periph;
2231 periph = (struct cam_periph *)callback_arg;
2233 case AC_FOUND_DEVICE:
2235 struct ccb_getdev *cgd;
2238 cgd = (struct ccb_getdev *)arg;
2242 if (cgd->protocol != PROTO_SCSI)
2244 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
2246 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
2250 * Allocate a peripheral instance for
2251 * this device and start the probe
2254 status = cam_periph_alloc(saregister, saoninvalidate,
2256 "sa", CAM_PERIPH_BIO, path,
2257 saasync, AC_FOUND_DEVICE, cgd);
2259 if (status != CAM_REQ_CMP
2260 && status != CAM_REQ_INPROG)
2261 printf("saasync: Unable to probe new device "
2262 "due to status 0x%x\n", status);
2266 cam_periph_async(periph, code, path, arg);
2272 sasetupdev(struct sa_softc *softc, struct cdev *dev)
2275 dev->si_iosize_max = softc->maxio;
2276 dev->si_flags |= softc->si_flags;
2278 * Keep a count of how many non-alias devices we have created,
2279 * so we can make sure we clean them all up on shutdown. Aliases
2280 * are cleaned up when we destroy the device they're an alias for.
2282 if ((dev->si_flags & SI_ALIAS) == 0)
2283 softc->num_devs_to_destroy++;
2287 sasysctlinit(void *context, int pending)
2289 struct cam_periph *periph;
2290 struct sa_softc *softc;
2291 char tmpstr[32], tmpstr2[16];
2293 periph = (struct cam_periph *)context;
2295 * If the periph is invalid, no need to setup the sysctls.
2297 if (periph->flags & CAM_PERIPH_INVALID)
2300 softc = (struct sa_softc *)periph->softc;
2302 snprintf(tmpstr, sizeof(tmpstr), "CAM SA unit %d", periph->unit_number);
2303 snprintf(tmpstr2, sizeof(tmpstr2), "%u", periph->unit_number);
2305 sysctl_ctx_init(&softc->sysctl_ctx);
2306 softc->flags |= SA_FLAG_SCTX_INIT;
2307 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
2308 SYSCTL_STATIC_CHILDREN(_kern_cam_sa), OID_AUTO, tmpstr2,
2309 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, tmpstr, "device_index");
2310 if (softc->sysctl_tree == NULL)
2313 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2314 OID_AUTO, "allow_io_split", CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
2315 &softc->allow_io_split, 0, "Allow Splitting I/O");
2316 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2317 OID_AUTO, "maxio", CTLFLAG_RD,
2318 &softc->maxio, 0, "Maximum I/O size");
2319 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2320 OID_AUTO, "cpi_maxio", CTLFLAG_RD,
2321 &softc->cpi_maxio, 0, "Maximum Controller I/O size");
2322 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2323 OID_AUTO, "inject_eom", CTLFLAG_RW,
2324 &softc->inject_eom, 0, "Queue EOM for the next write/read");
2328 * Release the reference that was held when this task was enqueued.
2330 cam_periph_release(periph);
2334 saregister(struct cam_periph *periph, void *arg)
2336 struct sa_softc *softc;
2337 struct ccb_getdev *cgd;
2338 struct ccb_pathinq cpi;
2339 struct make_dev_args args;
2344 cgd = (struct ccb_getdev *)arg;
2346 printf("saregister: no getdev CCB, can't register device\n");
2347 return (CAM_REQ_CMP_ERR);
2350 softc = (struct sa_softc *)
2351 malloc(sizeof (*softc), M_SCSISA, M_NOWAIT | M_ZERO);
2352 if (softc == NULL) {
2353 printf("saregister: Unable to probe new device. "
2354 "Unable to allocate softc\n");
2355 return (CAM_REQ_CMP_ERR);
2357 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
2358 softc->state = SA_STATE_NORMAL;
2359 softc->fileno = (daddr_t) -1;
2360 softc->blkno = (daddr_t) -1;
2361 softc->rep_fileno = (daddr_t) -1;
2362 softc->rep_blkno = (daddr_t) -1;
2363 softc->partition = (daddr_t) -1;
2368 bioq_init(&softc->bio_queue);
2369 softc->periph = periph;
2370 periph->softc = softc;
2373 * See if this device has any quirks.
2375 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2376 (caddr_t)sa_quirk_table,
2377 nitems(sa_quirk_table),
2378 sizeof(*sa_quirk_table), scsi_inquiry_match);
2380 if (match != NULL) {
2381 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
2382 softc->last_media_blksize =
2383 ((struct sa_quirk_entry *)match)->prefblk;
2385 softc->quirks = SA_QUIRK_NONE;
2388 * Long format data for READ POSITION was introduced in SSC, which
2389 * was after SCSI-2. (Roughly equivalent to SCSI-3.) If the drive
2390 * reports that it is SCSI-2 or older, it is unlikely to support
2391 * long position data, but it might. Some drives from that era
2392 * claim to be SCSI-2, but do support long position information.
2393 * So, instead of immediately disabling long position information
2394 * for SCSI-2 devices, we'll try one pass through sagetpos(), and
2395 * then disable long position information if we get an error.
2397 if (cgd->inq_data.version <= SCSI_REV_CCS)
2398 softc->quirks |= SA_QUIRK_NO_LONG_POS;
2400 if (cgd->inq_data.spc3_flags & SPC3_SID_PROTECT) {
2401 struct ccb_dev_advinfo cdai;
2402 struct scsi_vpd_extended_inquiry_data ext_inq;
2404 bzero(&ext_inq, sizeof(ext_inq));
2406 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2408 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
2409 cdai.flags = CDAI_FLAG_NONE;
2410 cdai.buftype = CDAI_TYPE_EXT_INQ;
2411 cdai.bufsiz = sizeof(ext_inq);
2412 cdai.buf = (uint8_t *)&ext_inq;
2413 xpt_action((union ccb *)&cdai);
2415 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
2416 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
2417 if ((cdai.ccb_h.status == CAM_REQ_CMP)
2418 && (ext_inq.flags1 & SVPD_EID_SA_SPT_LBP))
2419 softc->flags |= SA_FLAG_PROTECT_SUPP;
2422 xpt_path_inq(&cpi, periph->path);
2425 * The SA driver supports a blocksize, but we don't know the
2426 * blocksize until we media is inserted. So, set a flag to
2427 * indicate that the blocksize is unavailable right now.
2429 cam_periph_unlock(periph);
2430 softc->device_stats = devstat_new_entry("sa", periph->unit_number, 0,
2431 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
2432 XPORT_DEVSTAT_TYPE(cpi.transport), DEVSTAT_PRIORITY_TAPE);
2435 * Load the default value that is either compiled in, or loaded
2436 * in the global kern.cam.sa.allow_io_split tunable.
2438 softc->allow_io_split = sa_allow_io_split;
2441 * Load a per-instance tunable, if it exists. NOTE that this
2442 * tunable WILL GO AWAY in FreeBSD 11.0.
2444 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.sa.%u.allow_io_split",
2445 periph->unit_number);
2446 TUNABLE_INT_FETCH(tmpstr, &softc->allow_io_split);
2449 * If maxio isn't set, we fall back to DFLTPHYS. Otherwise we take
2450 * the smaller of cpi.maxio or maxphys.
2453 softc->maxio = DFLTPHYS;
2454 else if (cpi.maxio > maxphys)
2455 softc->maxio = maxphys;
2457 softc->maxio = cpi.maxio;
2460 * Record the controller's maximum I/O size so we can report it to
2463 softc->cpi_maxio = cpi.maxio;
2466 * By default we tell physio that we do not want our I/O split.
2467 * The user needs to have a 1:1 mapping between the size of his
2468 * write to a tape character device and the size of the write
2469 * that actually goes down to the drive.
2471 if (softc->allow_io_split == 0)
2472 softc->si_flags = SI_NOSPLIT;
2474 softc->si_flags = 0;
2476 TASK_INIT(&softc->sysctl_task, 0, sasysctlinit, periph);
2479 * If the SIM supports unmapped I/O, let physio know that we can
2480 * handle unmapped buffers.
2482 if (cpi.hba_misc & PIM_UNMAPPED)
2483 softc->si_flags |= SI_UNMAPPED;
2486 * Acquire a reference to the periph before we create the devfs
2487 * instances for it. We'll release this reference once the devfs
2488 * instances have been freed.
2490 if (cam_periph_acquire(periph) != 0) {
2491 xpt_print(periph->path, "%s: lost periph during "
2492 "registration!\n", __func__);
2493 cam_periph_lock(periph);
2494 return (CAM_REQ_CMP_ERR);
2497 make_dev_args_init(&args);
2498 args.mda_devsw = &sa_cdevsw;
2499 args.mda_si_drv1 = softc->periph;
2500 args.mda_uid = UID_ROOT;
2501 args.mda_gid = GID_OPERATOR;
2502 args.mda_mode = 0660;
2504 args.mda_unit = SAMINOR(SA_CTLDEV, SA_ATYPE_R);
2505 error = make_dev_s(&args, &softc->devs.ctl_dev, "%s%d.ctl",
2506 periph->periph_name, periph->unit_number);
2508 cam_periph_lock(periph);
2509 return (CAM_REQ_CMP_ERR);
2511 sasetupdev(softc, softc->devs.ctl_dev);
2513 args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_R);
2514 error = make_dev_s(&args, &softc->devs.r_dev, "%s%d",
2515 periph->periph_name, periph->unit_number);
2517 cam_periph_lock(periph);
2518 return (CAM_REQ_CMP_ERR);
2520 sasetupdev(softc, softc->devs.r_dev);
2522 args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_NR);
2523 error = make_dev_s(&args, &softc->devs.nr_dev, "n%s%d",
2524 periph->periph_name, periph->unit_number);
2526 cam_periph_lock(periph);
2527 return (CAM_REQ_CMP_ERR);
2529 sasetupdev(softc, softc->devs.nr_dev);
2531 args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_ER);
2532 error = make_dev_s(&args, &softc->devs.er_dev, "e%s%d",
2533 periph->periph_name, periph->unit_number);
2535 cam_periph_lock(periph);
2536 return (CAM_REQ_CMP_ERR);
2538 sasetupdev(softc, softc->devs.er_dev);
2540 cam_periph_lock(periph);
2542 softc->density_type_bits[0] = 0;
2543 softc->density_type_bits[1] = SRDS_MEDIA;
2544 softc->density_type_bits[2] = SRDS_MEDIUM_TYPE;
2545 softc->density_type_bits[3] = SRDS_MEDIUM_TYPE | SRDS_MEDIA;
2547 * Bump the peripheral refcount for the sysctl thread, in case we
2548 * get invalidated before the thread has a chance to run.
2550 cam_periph_acquire(periph);
2551 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
2554 * Add an async callback so that we get
2555 * notified if this device goes away.
2557 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path);
2559 xpt_announce_periph(periph, NULL);
2560 xpt_announce_quirks(periph, softc->quirks, SA_QUIRK_BIT_STRING);
2562 return (CAM_REQ_CMP);
2566 sastart(struct cam_periph *periph, union ccb *start_ccb)
2568 struct sa_softc *softc;
2570 softc = (struct sa_softc *)periph->softc;
2572 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
2574 switch (softc->state) {
2575 case SA_STATE_NORMAL:
2577 /* Pull a buffer from the queue and get going on it */
2581 * See if there is a buf with work for us to do..
2583 bp = bioq_first(&softc->bio_queue);
2585 xpt_release_ccb(start_ccb);
2586 } else if (((softc->flags & SA_FLAG_ERR_PENDING) != 0)
2587 || (softc->inject_eom != 0)) {
2588 struct bio *done_bp;
2590 if (softc->inject_eom != 0) {
2591 softc->flags |= SA_FLAG_EOM_PENDING;
2592 softc->inject_eom = 0;
2594 * If we're injecting EOM for writes, we
2595 * need to keep PEWS set for 3 queries
2596 * to cover 2 position requests from the
2597 * kernel via sagetpos(), and then allow
2598 * for one for the user to see the BPEW
2599 * flag (e.g. via mt status). After that,
2600 * it will be cleared.
2602 if (bp->bio_cmd == BIO_WRITE)
2603 softc->set_pews_status = 3;
2605 softc->set_pews_status = 1;
2608 softc->queue_count--;
2609 bioq_remove(&softc->bio_queue, bp);
2610 bp->bio_resid = bp->bio_bcount;
2612 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
2614 * We have two different behaviors for
2615 * writes when we hit either Early Warning
2616 * or the PEWZ (Programmable Early Warning
2617 * Zone). The default behavior is that
2618 * for all writes that are currently
2619 * queued after the write where we saw the
2620 * early warning, we will return the write
2621 * with the residual equal to the count.
2622 * i.e. tell the application that 0 bytes
2625 * The alternate behavior, which is enabled
2626 * when eot_warn is set, is that in
2627 * addition to setting the residual equal
2628 * to the count, we will set the error
2631 * In either case, once queued writes are
2632 * cleared out, we clear the error flag
2633 * (see below) and the application is free to
2634 * attempt to write more.
2636 if (softc->eot_warn != 0) {
2637 bp->bio_flags |= BIO_ERROR;
2638 bp->bio_error = ENOSPC;
2641 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
2643 * This can only happen if we're reading
2644 * in fixed length mode. In this case,
2645 * we dump the rest of the list the
2649 if (bioq_first(&softc->bio_queue) != NULL) {
2653 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
2654 bp->bio_error = EIO;
2655 bp->bio_flags |= BIO_ERROR;
2657 bp = bioq_first(&softc->bio_queue);
2659 * Only if we have no other buffers queued up
2660 * do we clear the pending error flag.
2663 softc->flags &= ~SA_FLAG_ERR_PENDING;
2664 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2665 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
2666 "%d more buffers queued up\n",
2667 (softc->flags & SA_FLAG_ERR_PENDING),
2668 (bp != NULL)? "not " : " ", softc->queue_count));
2669 xpt_release_ccb(start_ccb);
2674 bioq_remove(&softc->bio_queue, bp);
2675 softc->queue_count--;
2677 if ((bp->bio_cmd != BIO_READ) &&
2678 (bp->bio_cmd != BIO_WRITE)) {
2679 biofinish(bp, NULL, EOPNOTSUPP);
2680 xpt_release_ccb(start_ccb);
2683 length = bp->bio_bcount;
2685 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2686 if (softc->blk_shift != 0) {
2687 length = length >> softc->blk_shift;
2688 } else if (softc->media_blksize != 0) {
2689 length = length / softc->media_blksize;
2691 bp->bio_error = EIO;
2692 xpt_print(periph->path, "zero blocksize"
2693 " for FIXED length writes?\n");
2698 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
2699 ("issuing a %d fixed record %s\n",
2700 length, (bp->bio_cmd == BIO_READ)? "read" :
2705 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
2706 ("issuing a %d variable byte %s\n",
2707 length, (bp->bio_cmd == BIO_READ)? "read" :
2711 devstat_start_transaction_bio(softc->device_stats, bp);
2713 * Some people have theorized that we should
2714 * suppress illegal length indication if we are
2715 * running in variable block mode so that we don't
2716 * have to request sense every time our requested
2717 * block size is larger than the written block.
2718 * The residual information from the ccb allows
2719 * us to identify this situation anyway. The only
2720 * problem with this is that we will not get
2721 * information about blocks that are larger than
2722 * our read buffer unless we set the block size
2723 * in the mode page to something other than 0.
2725 * I believe that this is a non-issue. If user apps
2726 * don't adjust their read size to match our record
2727 * size, that's just life. Anyway, the typical usage
2728 * would be to issue, e.g., 64KB reads and occasionally
2729 * have to do deal with 512 byte or 1KB intermediate
2732 * That said, though, we now support setting the
2733 * SILI bit on reads, and we set the blocksize to 4
2734 * bytes when we do that. This gives us
2735 * compatibility with software that wants this,
2736 * although the only real difference between that
2737 * and not setting the SILI bit on reads is that we
2738 * won't get a check condition on reads where our
2739 * request size is larger than the block on tape.
2740 * That probably only makes a real difference in
2741 * non-packetized SCSI, where you have to go back
2742 * to the drive to request sense and thus incur
2745 softc->dsreg = (bp->bio_cmd == BIO_READ)?
2746 MTIO_DSREG_RD : MTIO_DSREG_WR;
2747 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
2748 MSG_SIMPLE_Q_TAG, (bp->bio_cmd == BIO_READ ?
2749 SCSI_RW_READ : SCSI_RW_WRITE) |
2750 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2751 SCSI_RW_BIO : 0), softc->sili,
2752 (softc->flags & SA_FLAG_FIXED) != 0, length,
2753 (bp->bio_flags & BIO_UNMAPPED) != 0 ? (void *)bp :
2754 bp->bio_data, bp->bio_bcount, SSD_FULL_SIZE,
2756 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
2757 start_ccb->ccb_h.ccb_bp = bp;
2758 bp = bioq_first(&softc->bio_queue);
2759 xpt_action(start_ccb);
2763 /* Have more work to do, so ensure we stay scheduled */
2764 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
2768 case SA_STATE_ABNORMAL:
2770 panic("state 0x%x in sastart", softc->state);
2776 sadone(struct cam_periph *periph, union ccb *done_ccb)
2778 struct sa_softc *softc;
2779 struct ccb_scsiio *csio;
2783 softc = (struct sa_softc *)periph->softc;
2784 csio = &done_ccb->csio;
2786 softc->dsreg = MTIO_DSREG_REST;
2787 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2789 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2790 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
2792 * A retry was scheduled, so just return.
2800 * Catastrophic error. Mark the tape as frozen
2801 * (we no longer know tape position).
2803 * Return all queued I/O with EIO, and unfreeze
2804 * our queue so that future transactions that
2805 * attempt to fix this problem can get to the
2810 softc->flags |= SA_FLAG_TAPE_FROZEN;
2811 bioq_flush(&softc->bio_queue, NULL, EIO);
2814 bp->bio_resid = bp->bio_bcount;
2815 bp->bio_error = error;
2816 bp->bio_flags |= BIO_ERROR;
2818 * In the error case, position is updated in saerror.
2821 bp->bio_resid = csio->resid;
2823 if (csio->resid != 0) {
2824 bp->bio_flags |= BIO_ERROR;
2826 if (bp->bio_cmd == BIO_WRITE) {
2827 softc->flags |= SA_FLAG_TAPE_WRITTEN;
2828 softc->filemarks = 0;
2830 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
2831 (softc->blkno != (daddr_t) -1)) {
2832 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2834 if (softc->blk_shift != 0) {
2835 l = bp->bio_bcount >>
2838 l = bp->bio_bcount /
2839 softc->media_blksize;
2841 softc->blkno += (daddr_t) l;
2848 * If we had an error (immediate or pending),
2849 * release the device queue now.
2851 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
2852 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
2853 if (error || bp->bio_resid) {
2854 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2855 ("error %d resid %ld count %ld\n", error,
2856 bp->bio_resid, bp->bio_bcount));
2858 biofinish(bp, softc->device_stats, 0);
2859 xpt_release_ccb(done_ccb);
2863 * Mount the tape (make sure it's ready for I/O).
2866 samount(struct cam_periph *periph, int oflags, struct cdev *dev)
2868 struct sa_softc *softc;
2873 * oflags can be checked for 'kind' of open (read-only check) - later
2874 * dev can be checked for a control-mode or compression open - later
2876 UNUSED_PARAMETER(oflags);
2877 UNUSED_PARAMETER(dev);
2879 softc = (struct sa_softc *)periph->softc;
2882 * This should determine if something has happened since the last
2883 * open/mount that would invalidate the mount. We do *not* want
2884 * to retry this command- we just want the status. But we only
2885 * do this if we're mounted already- if we're not mounted,
2886 * we don't care about the unit read state and can instead use
2887 * this opportunity to attempt to reserve the tape unit.
2890 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
2891 ccb = cam_periph_getccb(periph, 1);
2892 scsi_test_unit_ready(&ccb->csio, 0, NULL,
2893 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
2894 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2895 softc->device_stats);
2896 if (error == ENXIO) {
2897 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
2898 scsi_test_unit_ready(&ccb->csio, 0, NULL,
2899 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
2900 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2901 softc->device_stats);
2904 * We don't need to freeze the tape because we
2905 * will now attempt to rewind/load it.
2907 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
2908 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2909 xpt_print(periph->path,
2910 "error %d on TUR in samount\n", error);
2914 error = sareservereleaseunit(periph, TRUE);
2918 ccb = cam_periph_getccb(periph, 1);
2919 scsi_test_unit_ready(&ccb->csio, 0, NULL,
2920 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
2921 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2922 softc->device_stats);
2925 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
2926 struct scsi_read_block_limits_data *rblim = NULL;
2927 int comp_enabled, comp_supported;
2928 u_int8_t write_protect, guessing = 0;
2931 * Clear out old state.
2933 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
2934 SA_FLAG_ERR_PENDING|SA_FLAG_COMPRESSION);
2935 softc->filemarks = 0;
2938 * *Very* first off, make sure we're loaded to BOT.
2940 scsi_load_unload(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG, FALSE,
2941 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
2942 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2943 softc->device_stats);
2946 * In case this doesn't work, do a REWIND instead
2949 scsi_rewind(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG,
2950 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
2951 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2952 softc->device_stats);
2955 xpt_release_ccb(ccb);
2960 * Do a dummy test read to force access to the
2961 * media so that the drive will really know what's
2962 * there. We actually don't really care what the
2963 * blocksize on tape is and don't expect to really
2964 * read a full record.
2966 rblim = (struct scsi_read_block_limits_data *)
2967 malloc(8192, M_SCSISA, M_NOWAIT);
2968 if (rblim == NULL) {
2969 xpt_print(periph->path, "no memory for test read\n");
2970 xpt_release_ccb(ccb);
2975 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
2976 scsi_sa_read_write(&ccb->csio, 0, NULL,
2977 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
2978 (void *) rblim, 8192, SSD_FULL_SIZE,
2980 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2981 softc->device_stats);
2982 scsi_rewind(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG,
2983 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
2984 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
2985 SF_NO_PRINT | SF_RETRY_UA,
2986 softc->device_stats);
2988 xpt_print(periph->path,
2989 "unable to rewind after test read\n");
2990 xpt_release_ccb(ccb);
2996 * Next off, determine block limits.
2998 scsi_read_block_limits(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
2999 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3001 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
3002 SF_NO_PRINT | SF_RETRY_UA, softc->device_stats);
3004 xpt_release_ccb(ccb);
3008 * If it's less than SCSI-2, READ BLOCK LIMITS is not
3009 * a MANDATORY command. Anyway- it doesn't matter-
3010 * we can proceed anyway.
3012 softc->blk_gran = 0;
3013 softc->max_blk = ~0;
3016 if (softc->scsi_rev >= SCSI_REV_SPC) {
3017 softc->blk_gran = RBL_GRAN(rblim);
3019 softc->blk_gran = 0;
3022 * We take max_blk == min_blk to mean a default to
3023 * fixed mode- but note that whatever we get out of
3024 * sagetparams below will actually determine whether
3025 * we are actually *in* fixed mode.
3027 softc->max_blk = scsi_3btoul(rblim->maximum);
3028 softc->min_blk = scsi_2btoul(rblim->minimum);
3031 * Next, perform a mode sense to determine
3032 * current density, blocksize, compression etc.
3034 error = sagetparams(periph, SA_PARAM_ALL,
3035 &softc->media_blksize,
3036 &softc->media_density,
3037 &softc->media_numblks,
3038 &softc->buffer_mode, &write_protect,
3039 &softc->speed, &comp_supported,
3040 &comp_enabled, &softc->comp_algorithm,
3045 * We could work a little harder here. We could
3046 * adjust our attempts to get information. It
3047 * might be an ancient tape drive. If someone
3048 * nudges us, we'll do that.
3054 * If no quirk has determined that this is a device that is
3055 * preferred to be in fixed or variable mode, now is the time
3058 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
3061 * This could be expensive to find out. Luckily we
3062 * only need to do this once. If we start out in
3063 * 'default' mode, try and set ourselves to one
3064 * of the densities that would determine a wad
3065 * of other stuff. Go from highest to lowest.
3067 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
3069 static u_int8_t ctry[] = {
3070 SCSI_DENSITY_HALFINCH_PE,
3071 SCSI_DENSITY_HALFINCH_6250C,
3072 SCSI_DENSITY_HALFINCH_6250,
3073 SCSI_DENSITY_HALFINCH_1600,
3074 SCSI_DENSITY_HALFINCH_800,
3075 SCSI_DENSITY_QIC_4GB,
3076 SCSI_DENSITY_QIC_2GB,
3077 SCSI_DENSITY_QIC_525_320,
3078 SCSI_DENSITY_QIC_150,
3079 SCSI_DENSITY_QIC_120,
3080 SCSI_DENSITY_QIC_24,
3081 SCSI_DENSITY_QIC_11_9TRK,
3082 SCSI_DENSITY_QIC_11_4TRK,
3083 SCSI_DENSITY_QIC_1320,
3084 SCSI_DENSITY_QIC_3080,
3087 for (i = 0; ctry[i]; i++) {
3088 error = sasetparams(periph,
3089 SA_PARAM_DENSITY, 0, ctry[i],
3092 softc->media_density = ctry[i];
3097 switch (softc->media_density) {
3098 case SCSI_DENSITY_QIC_11_4TRK:
3099 case SCSI_DENSITY_QIC_11_9TRK:
3100 case SCSI_DENSITY_QIC_24:
3101 case SCSI_DENSITY_QIC_120:
3102 case SCSI_DENSITY_QIC_150:
3103 case SCSI_DENSITY_QIC_525_320:
3104 case SCSI_DENSITY_QIC_1320:
3105 case SCSI_DENSITY_QIC_3080:
3106 softc->quirks &= ~SA_QUIRK_2FM;
3107 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
3108 softc->last_media_blksize = 512;
3110 case SCSI_DENSITY_QIC_4GB:
3111 case SCSI_DENSITY_QIC_2GB:
3112 softc->quirks &= ~SA_QUIRK_2FM;
3113 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
3114 softc->last_media_blksize = 1024;
3117 softc->last_media_blksize =
3118 softc->media_blksize;
3119 softc->quirks |= SA_QUIRK_VARIABLE;
3125 * If no quirk has determined that this is a device that needs
3126 * to have 2 Filemarks at EOD, now is the time to find out.
3129 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
3130 switch (softc->media_density) {
3131 case SCSI_DENSITY_HALFINCH_800:
3132 case SCSI_DENSITY_HALFINCH_1600:
3133 case SCSI_DENSITY_HALFINCH_6250:
3134 case SCSI_DENSITY_HALFINCH_6250C:
3135 case SCSI_DENSITY_HALFINCH_PE:
3136 softc->quirks &= ~SA_QUIRK_1FM;
3137 softc->quirks |= SA_QUIRK_2FM;
3145 * Now validate that some info we got makes sense.
3147 if ((softc->max_blk < softc->media_blksize) ||
3148 (softc->min_blk > softc->media_blksize &&
3149 softc->media_blksize)) {
3150 xpt_print(periph->path,
3151 "BLOCK LIMITS (%d..%d) could not match current "
3152 "block settings (%d)- adjusting\n", softc->min_blk,
3153 softc->max_blk, softc->media_blksize);
3154 softc->max_blk = softc->min_blk =
3155 softc->media_blksize;
3159 * Now put ourselves into the right frame of mind based
3164 * If we want to be in FIXED mode and our current blocksize
3165 * is not equal to our last blocksize (if nonzero), try and
3166 * set ourselves to this last blocksize (as the 'preferred'
3167 * block size). The initial quirkmatch at registry sets the
3168 * initial 'last' blocksize. If, for whatever reason, this
3169 * 'last' blocksize is zero, set the blocksize to 512,
3170 * or min_blk if that's larger.
3172 if ((softc->quirks & SA_QUIRK_FIXED) &&
3173 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
3174 (softc->media_blksize != softc->last_media_blksize)) {
3175 softc->media_blksize = softc->last_media_blksize;
3176 if (softc->media_blksize == 0) {
3177 softc->media_blksize = 512;
3178 if (softc->media_blksize < softc->min_blk) {
3179 softc->media_blksize = softc->min_blk;
3182 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
3183 softc->media_blksize, 0, 0, SF_NO_PRINT);
3185 xpt_print(periph->path,
3186 "unable to set fixed blocksize to %d\n",
3187 softc->media_blksize);
3192 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
3193 (softc->media_blksize != 0)) {
3194 softc->last_media_blksize = softc->media_blksize;
3195 softc->media_blksize = 0;
3196 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
3197 0, 0, 0, SF_NO_PRINT);
3200 * If this fails and we were guessing, just
3201 * assume that we got it wrong and go try
3202 * fixed block mode. Don't even check against
3203 * density code at this point.
3206 softc->quirks &= ~SA_QUIRK_VARIABLE;
3207 softc->quirks |= SA_QUIRK_FIXED;
3208 if (softc->last_media_blksize == 0)
3209 softc->last_media_blksize = 512;
3212 xpt_print(periph->path,
3213 "unable to set variable blocksize\n");
3219 * Now that we have the current block size,
3220 * set up some parameters for sastart's usage.
3222 if (softc->media_blksize) {
3223 softc->flags |= SA_FLAG_FIXED;
3224 if (powerof2(softc->media_blksize)) {
3226 ffs(softc->media_blksize) - 1;
3227 softc->blk_mask = softc->media_blksize - 1;
3229 softc->blk_mask = ~0;
3230 softc->blk_shift = 0;
3234 * The SCSI-3 spec allows 0 to mean "unspecified".
3235 * The SCSI-1 spec allows 0 to mean 'infinite'.
3237 * Either works here.
3239 if (softc->max_blk == 0) {
3240 softc->max_blk = ~0;
3242 softc->blk_shift = 0;
3243 if (softc->blk_gran != 0) {
3244 softc->blk_mask = softc->blk_gran - 1;
3246 softc->blk_mask = 0;
3251 softc->flags |= SA_FLAG_TAPE_WP;
3253 if (comp_supported) {
3254 if (softc->saved_comp_algorithm == 0)
3255 softc->saved_comp_algorithm =
3256 softc->comp_algorithm;
3257 softc->flags |= SA_FLAG_COMP_SUPP;
3259 softc->flags |= SA_FLAG_COMP_ENABLED;
3261 softc->flags |= SA_FLAG_COMP_UNSUPP;
3263 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
3264 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
3265 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
3268 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
3270 xpt_print(periph->path,
3271 "unable to set buffered mode\n");
3273 error = 0; /* not an error */
3277 softc->flags |= SA_FLAG_TAPE_MOUNTED;
3281 free(rblim, M_SCSISA);
3284 softc->dsreg = MTIO_DSREG_NIL;
3286 softc->fileno = softc->blkno = 0;
3287 softc->rep_fileno = softc->rep_blkno = -1;
3288 softc->partition = 0;
3289 softc->dsreg = MTIO_DSREG_REST;
3291 #ifdef SA_1FM_AT_EOD
3292 if ((softc->quirks & SA_QUIRK_2FM) == 0)
3293 softc->quirks |= SA_QUIRK_1FM;
3295 if ((softc->quirks & SA_QUIRK_1FM) == 0)
3296 softc->quirks |= SA_QUIRK_2FM;
3299 xpt_release_ccb(ccb);
3302 * If we return an error, we're not mounted any more,
3303 * so release any device reservation.
3306 (void) sareservereleaseunit(periph, FALSE);
3309 * Clear I/O residual.
3311 softc->last_io_resid = 0;
3312 softc->last_ctl_resid = 0;
3318 * How many filemarks do we need to write if we were to terminate the
3319 * tape session right now? Note that this can be a negative number
3323 samarkswanted(struct cam_periph *periph)
3326 struct sa_softc *softc;
3328 softc = (struct sa_softc *)periph->softc;
3330 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
3332 if (softc->quirks & SA_QUIRK_2FM)
3335 markswanted -= softc->filemarks;
3336 return (markswanted);
3340 sacheckeod(struct cam_periph *periph)
3345 markswanted = samarkswanted(periph);
3347 if (markswanted > 0) {
3348 error = sawritefilemarks(periph, markswanted, FALSE, FALSE);
3356 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
3358 static const char *toobig =
3359 "%d-byte tape record bigger than supplied buffer\n";
3360 struct cam_periph *periph;
3361 struct sa_softc *softc;
3362 struct ccb_scsiio *csio;
3363 struct scsi_sense_data *sense;
3367 int error_code, sense_key, asc, ascq, error, aqvalid, stream_valid;
3369 uint8_t stream_bits;
3371 periph = xpt_path_periph(ccb->ccb_h.path);
3372 softc = (struct sa_softc *)periph->softc;
3374 sense = &csio->sense_data;
3375 sense_len = csio->sense_len - csio->sense_resid;
3376 scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
3377 &asc, &ascq, /*show_errors*/ 1);
3378 if (asc != -1 && ascq != -1)
3382 if (scsi_get_stream_info(sense, sense_len, NULL, &stream_bits) == 0)
3388 status = csio->ccb_h.status & CAM_STATUS_MASK;
3391 * Calculate/latch up, any residuals... We do this in a funny 2-step
3392 * so we can print stuff here if we have CAM_DEBUG enabled for this
3395 if (status == CAM_SCSI_STATUS_ERROR) {
3396 if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &resid,
3398 if ((softc->flags & SA_FLAG_FIXED) != 0)
3399 resid *= softc->media_blksize;
3401 resid = csio->dxfer_len;
3403 if ((softc->flags & SA_FLAG_FIXED) != 0) {
3404 if (softc->media_blksize)
3405 info /= softc->media_blksize;
3408 if (csio->cdb_io.cdb_bytes[0] == SA_READ ||
3409 csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
3410 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
3411 sizeof (struct scsi_sense_data));
3412 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
3413 (int) csio->cdb_len);
3414 softc->last_io_resid = resid;
3415 softc->last_resid_was_io = 1;
3417 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
3418 sizeof (struct scsi_sense_data));
3419 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
3420 (int) csio->cdb_len);
3421 softc->last_ctl_resid = resid;
3422 softc->last_resid_was_io = 0;
3424 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
3425 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %jd "
3426 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
3427 sense_key, asc, ascq, status,
3428 (stream_valid) ? stream_bits : 0, (intmax_t)resid,
3431 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
3432 ("Cam Status 0x%x\n", status));
3438 case CAM_SCSI_STATUS_ERROR:
3440 * If a read/write command, we handle it here.
3442 if (csio->cdb_io.cdb_bytes[0] == SA_READ ||
3443 csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
3447 * If this was just EOM/EOP, Filemark, Setmark, ILI or
3448 * PEW detected on a non read/write command, we assume
3449 * it's not an error and propagate the residual and return.
3451 if ((aqvalid && asc == 0 && ((ascq > 0 && ascq <= 5)
3453 || (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
3454 csio->resid = resid;
3459 * Otherwise, we let the common code handle this.
3461 return (cam_periph_error(ccb, cflgs, sflgs));
3465 * We cannot depend upon CAM honoring retry counts for these.
3467 case CAM_SCSI_BUS_RESET:
3469 if (ccb->ccb_h.retry_count <= 0) {
3474 return (cam_periph_error(ccb, cflgs, sflgs));
3478 * Handle filemark, end of tape, mismatched record sizes....
3479 * From this point out, we're only handling read/write cases.
3480 * Handle writes && reads differently.
3483 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
3484 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
3485 csio->resid = resid;
3487 } else if ((stream_valid != 0) && (stream_bits & SSD_EOM)) {
3488 softc->flags |= SA_FLAG_EOM_PENDING;
3490 * Grotesque as it seems, the few times
3491 * I've actually seen a non-zero resid,
3492 * the tape drive actually lied and had
3493 * written all the data!.
3498 csio->resid = resid;
3499 if (sense_key == SSD_KEY_BLANK_CHECK) {
3500 if (softc->quirks & SA_QUIRK_1FM) {
3502 softc->flags |= SA_FLAG_EOM_PENDING;
3506 } else if ((stream_valid != 0) && (stream_bits & SSD_FILEMARK)){
3507 if (softc->flags & SA_FLAG_FIXED) {
3509 softc->flags |= SA_FLAG_EOF_PENDING;
3512 * Unconditionally, if we detected a filemark on a read,
3513 * mark that we've run moved a file ahead.
3515 if (softc->fileno != (daddr_t) -1) {
3518 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
3524 * Incorrect Length usually applies to read, but can apply to writes.
3526 if (error == 0 && (stream_valid != 0) && (stream_bits & SSD_ILI)) {
3528 xpt_print(csio->ccb_h.path, toobig,
3529 csio->dxfer_len - info);
3530 csio->resid = csio->dxfer_len;
3533 csio->resid = resid;
3534 if (softc->flags & SA_FLAG_FIXED) {
3535 softc->flags |= SA_FLAG_EIO_PENDING;
3538 * Bump the block number if we hadn't seen a filemark.
3539 * Do this independent of errors (we've moved anyway).
3541 if ((stream_valid == 0) ||
3542 (stream_bits & SSD_FILEMARK) == 0) {
3543 if (softc->blkno != (daddr_t) -1) {
3545 csio->ccb_h.ccb_pflags |=
3546 SA_POSITION_UPDATED;
3554 * Unfreeze the queue if frozen as we're not returning anything
3555 * to our waiters that would indicate an I/O error has occurred
3565 sagetparams(struct cam_periph *periph, sa_params params_to_get,
3566 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
3567 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
3568 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
3569 sa_comp_t *tcs, struct scsi_control_data_prot_subpage *prot_page,
3570 int dp_size, int prot_changeable)
3574 struct scsi_mode_header_6 *mode_hdr;
3575 struct scsi_mode_blk_desc *mode_blk;
3576 int mode_buffer_len;
3577 struct sa_softc *softc;
3582 softc = (struct sa_softc *)periph->softc;
3583 ccb = cam_periph_getccb(periph, 1);
3584 if (softc->quirks & SA_QUIRK_NO_CPAGE)
3585 cpage = SA_DEVICE_CONFIGURATION_PAGE;
3587 cpage = SA_DATA_COMPRESSION_PAGE;
3590 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
3592 if (params_to_get & SA_PARAM_COMPRESSION) {
3593 if (softc->quirks & SA_QUIRK_NOCOMP) {
3594 *comp_supported = FALSE;
3595 params_to_get &= ~SA_PARAM_COMPRESSION;
3597 mode_buffer_len += sizeof (sa_comp_t);
3600 /* XXX Fix M_NOWAIT */
3601 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
3602 if (mode_buffer == NULL) {
3603 xpt_release_ccb(ccb);
3606 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
3607 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
3609 /* it is safe to retry this */
3610 scsi_mode_sense(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, FALSE,
3611 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
3612 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
3613 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3615 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3616 softc->device_stats);
3618 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3620 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
3622 * Hmm. Let's see if we can try another page...
3623 * If we've already done that, give up on compression
3624 * for this device and remember this for the future
3625 * and attempt the request without asking for compression
3628 if (cpage == SA_DATA_COMPRESSION_PAGE) {
3629 cpage = SA_DEVICE_CONFIGURATION_PAGE;
3632 softc->quirks |= SA_QUIRK_NOCOMP;
3633 free(mode_buffer, M_SCSISA);
3635 } else if (status == CAM_SCSI_STATUS_ERROR) {
3636 /* Tell the user about the fatal error. */
3637 scsi_sense_print(&ccb->csio);
3638 goto sagetparamsexit;
3642 * If the user only wants the compression information, and
3643 * the device doesn't send back the block descriptor, it's
3644 * no big deal. If the user wants more than just
3645 * compression, though, and the device doesn't pass back the
3646 * block descriptor, we need to send another mode sense to
3647 * get the block descriptor.
3649 if ((mode_hdr->blk_desc_len == 0) &&
3650 (params_to_get & SA_PARAM_COMPRESSION) &&
3651 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
3653 * Decrease the mode buffer length by the size of
3654 * the compression page, to make sure the data
3655 * there doesn't get overwritten.
3657 mode_buffer_len -= sizeof (sa_comp_t);
3660 * Now move the compression page that we presumably
3661 * got back down the memory chunk a little bit so
3662 * it doesn't get spammed.
3664 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
3665 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
3668 * Now, we issue another mode sense and just ask
3669 * for the block descriptor, etc.
3672 scsi_mode_sense(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG, FALSE,
3673 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
3674 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
3677 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3678 softc->device_stats);
3681 goto sagetparamsexit;
3684 if (params_to_get & SA_PARAM_BLOCKSIZE)
3685 *blocksize = scsi_3btoul(mode_blk->blklen);
3687 if (params_to_get & SA_PARAM_NUMBLOCKS)
3688 *numblocks = scsi_3btoul(mode_blk->nblocks);
3690 if (params_to_get & SA_PARAM_BUFF_MODE)
3691 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
3693 if (params_to_get & SA_PARAM_DENSITY)
3694 *density = mode_blk->density;
3696 if (params_to_get & SA_PARAM_WP)
3697 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
3699 if (params_to_get & SA_PARAM_SPEED)
3700 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
3702 if (params_to_get & SA_PARAM_COMPRESSION) {
3703 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
3704 if (cpage == SA_DATA_COMPRESSION_PAGE) {
3705 struct scsi_data_compression_page *cp = &ntcs->dcomp;
3707 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
3709 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
3710 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
3712 struct scsi_dev_conf_page *cp = &ntcs->dconf;
3714 * We don't really know whether this device supports
3715 * Data Compression if the algorithm field is
3716 * zero. Just say we do.
3718 *comp_supported = TRUE;
3720 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
3721 *comp_algorithm = cp->sel_comp_alg;
3724 bcopy(ntcs, tcs, sizeof (sa_comp_t));
3727 if ((params_to_get & SA_PARAM_DENSITY_EXT)
3728 && (softc->scsi_rev >= SCSI_REV_SPC)) {
3731 for (i = 0; i < SA_DENSITY_TYPES; i++) {
3732 scsi_report_density_support(&ccb->csio,
3735 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3736 /*media*/ softc->density_type_bits[i] & SRDS_MEDIA,
3737 /*medium_type*/ softc->density_type_bits[i] &
3739 /*data_ptr*/ softc->density_info[i],
3740 /*length*/ sizeof(softc->density_info[i]),
3741 /*sense_len*/ SSD_FULL_SIZE,
3742 /*timeout*/ REP_DENSITY_TIMEOUT);
3743 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3744 softc->device_stats);
3745 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3748 * Some tape drives won't support this command at
3749 * all, but hopefully we'll minimize that with the
3750 * check for SPC or greater support above. If they
3751 * don't support the default report (neither the
3752 * MEDIA or MEDIUM_TYPE bits set), then there is
3753 * really no point in continuing on to look for
3757 || (status != CAM_REQ_CMP)) {
3759 softc->density_info_valid[i] = 0;
3760 if (softc->density_type_bits[i] == 0)
3765 softc->density_info_valid[i] = ccb->csio.dxfer_len -
3771 * Get logical block protection parameters if the drive supports it.
3773 if ((params_to_get & SA_PARAM_LBP)
3774 && (softc->flags & SA_FLAG_PROTECT_SUPP)) {
3775 struct scsi_mode_header_10 *mode10_hdr;
3776 struct scsi_control_data_prot_subpage *dp_page;
3777 struct scsi_mode_sense_10 *cdb;
3778 struct sa_prot_state *prot;
3779 int dp_len, returned_len;
3782 dp_size = sizeof(*dp_page);
3784 dp_len = sizeof(*mode10_hdr) + dp_size;
3785 mode10_hdr = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO);
3786 if (mode10_hdr == NULL) {
3788 goto sagetparamsexit;
3791 scsi_mode_sense_len(&ccb->csio,
3794 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3796 /*page_code*/ (prot_changeable == 0) ?
3797 SMS_PAGE_CTRL_CURRENT :
3798 SMS_PAGE_CTRL_CHANGEABLE,
3799 /*page*/ SMS_CONTROL_MODE_PAGE,
3800 /*param_buf*/ (uint8_t *)mode10_hdr,
3801 /*param_len*/ dp_len,
3802 /*minimum_cmd_size*/ 10,
3803 /*sense_len*/ SSD_FULL_SIZE,
3804 /*timeout*/ SCSIOP_TIMEOUT);
3806 * XXX KDM we need to be able to set the subpage in the
3809 cdb = (struct scsi_mode_sense_10 *)ccb->csio.cdb_io.cdb_bytes;
3810 cdb->subpage = SA_CTRL_DP_SUBPAGE_CODE;
3812 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3813 softc->device_stats);
3815 free(mode10_hdr, M_SCSISA);
3816 goto sagetparamsexit;
3819 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3820 if (status != CAM_REQ_CMP) {
3822 free(mode10_hdr, M_SCSISA);
3823 goto sagetparamsexit;
3827 * The returned data length at least has to be long enough
3828 * for us to look at length in the mode page header.
3830 returned_len = ccb->csio.dxfer_len - ccb->csio.resid;
3831 if (returned_len < sizeof(mode10_hdr->data_length)) {
3833 free(mode10_hdr, M_SCSISA);
3834 goto sagetparamsexit;
3837 returned_len = min(returned_len,
3838 sizeof(mode10_hdr->data_length) +
3839 scsi_2btoul(mode10_hdr->data_length));
3841 dp_page = (struct scsi_control_data_prot_subpage *)
3845 * We also have to have enough data to include the prot_bits
3848 if (returned_len < (sizeof(*mode10_hdr) +
3849 __offsetof(struct scsi_control_data_prot_subpage, prot_bits)
3850 + sizeof(dp_page->prot_bits))) {
3852 free(mode10_hdr, M_SCSISA);
3853 goto sagetparamsexit;
3856 prot = &softc->prot_info.cur_prot_state;
3857 prot->prot_method = dp_page->prot_method;
3858 prot->pi_length = dp_page->pi_length &
3859 SA_CTRL_DP_PI_LENGTH_MASK;
3860 prot->lbp_w = (dp_page->prot_bits & SA_CTRL_DP_LBP_W) ? 1 :0;
3861 prot->lbp_r = (dp_page->prot_bits & SA_CTRL_DP_LBP_R) ? 1 :0;
3862 prot->rbdp = (dp_page->prot_bits & SA_CTRL_DP_RBDP) ? 1 :0;
3863 prot->initialized = 1;
3865 if (prot_page != NULL)
3866 bcopy(dp_page, prot_page, min(sizeof(*prot_page),
3869 free(mode10_hdr, M_SCSISA);
3872 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3874 char *xyz = mode_buffer;
3875 xpt_print_path(periph->path);
3876 printf("Mode Sense Data=");
3877 for (idx = 0; idx < mode_buffer_len; idx++)
3878 printf(" 0x%02x", xyz[idx] & 0xff);
3884 xpt_release_ccb(ccb);
3885 free(mode_buffer, M_SCSISA);
3890 * Set protection information to the pending protection information stored
3894 sasetprot(struct cam_periph *periph, struct sa_prot_state *new_prot)
3896 struct sa_softc *softc;
3897 struct scsi_control_data_prot_subpage *dp_page, *dp_changeable;
3898 struct scsi_mode_header_10 *mode10_hdr, *mode10_changeable;
3900 uint8_t current_speed;
3901 size_t dp_size, dp_page_length;
3902 int dp_len, buff_mode;
3905 softc = (struct sa_softc *)periph->softc;
3907 mode10_changeable = NULL;
3911 * Start off with the size set to the actual length of the page
3912 * that we have defined.
3914 dp_size = sizeof(*dp_changeable);
3915 dp_page_length = dp_size -
3916 __offsetof(struct scsi_control_data_prot_subpage, prot_method);
3920 dp_len = sizeof(*mode10_changeable) + dp_size;
3921 mode10_changeable = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO);
3922 if (mode10_changeable == NULL) {
3928 (struct scsi_control_data_prot_subpage *)&mode10_changeable[1];
3931 * First get the data protection page changeable parameters mask.
3932 * We need to know which parameters the drive supports changing.
3933 * We also need to know what the drive claims that its page length
3934 * is. The reason is that IBM drives in particular are very picky
3935 * about the page length. They want it (the length set in the
3936 * page structure itself) to be 28 bytes, and they want the
3937 * parameter list length specified in the mode select header to be
3938 * 40 bytes. So, to work with IBM drives as well as any other tape
3939 * drive, find out what the drive claims the page length is, and
3940 * make sure that we match that.
3942 error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP,
3943 NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL,
3944 NULL, NULL, dp_changeable, dp_size, /*prot_changeable*/ 1);
3948 if (scsi_2btoul(dp_changeable->length) > dp_page_length) {
3949 dp_page_length = scsi_2btoul(dp_changeable->length);
3950 dp_size = dp_page_length +
3951 __offsetof(struct scsi_control_data_prot_subpage,
3953 free(mode10_changeable, M_SCSISA);
3954 mode10_changeable = NULL;
3958 mode10_hdr = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO);
3959 if (mode10_hdr == NULL) {
3964 dp_page = (struct scsi_control_data_prot_subpage *)&mode10_hdr[1];
3967 * Now grab the actual current settings in the page.
3969 error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP,
3970 NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL,
3971 NULL, NULL, dp_page, dp_size, /*prot_changeable*/ 0);
3975 /* These two fields need to be 0 for MODE SELECT */
3976 scsi_ulto2b(0, mode10_hdr->data_length);
3977 mode10_hdr->medium_type = 0;
3978 /* We are not including a block descriptor */
3979 scsi_ulto2b(0, mode10_hdr->blk_desc_len);
3981 mode10_hdr->dev_spec = current_speed;
3982 /* if set, set single-initiator buffering mode */
3983 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
3984 mode10_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
3988 * For each field, make sure that the drive allows changing it
3989 * before bringing in the user's setting.
3991 if (dp_changeable->prot_method != 0)
3992 dp_page->prot_method = new_prot->prot_method;
3994 if (dp_changeable->pi_length & SA_CTRL_DP_PI_LENGTH_MASK) {
3995 dp_page->pi_length &= ~SA_CTRL_DP_PI_LENGTH_MASK;
3996 dp_page->pi_length |= (new_prot->pi_length &
3997 SA_CTRL_DP_PI_LENGTH_MASK);
3999 if (dp_changeable->prot_bits & SA_CTRL_DP_LBP_W) {
4000 if (new_prot->lbp_w)
4001 dp_page->prot_bits |= SA_CTRL_DP_LBP_W;
4003 dp_page->prot_bits &= ~SA_CTRL_DP_LBP_W;
4006 if (dp_changeable->prot_bits & SA_CTRL_DP_LBP_R) {
4007 if (new_prot->lbp_r)
4008 dp_page->prot_bits |= SA_CTRL_DP_LBP_R;
4010 dp_page->prot_bits &= ~SA_CTRL_DP_LBP_R;
4013 if (dp_changeable->prot_bits & SA_CTRL_DP_RBDP) {
4015 dp_page->prot_bits |= SA_CTRL_DP_RBDP;
4017 dp_page->prot_bits &= ~SA_CTRL_DP_RBDP;
4020 ccb = cam_periph_getccb(periph, 1);
4022 scsi_mode_select_len(&ccb->csio,
4025 /*tag_action*/ MSG_SIMPLE_Q_TAG,
4026 /*scsi_page_fmt*/ TRUE,
4027 /*save_pages*/ FALSE,
4028 /*param_buf*/ (uint8_t *)mode10_hdr,
4029 /*param_len*/ dp_len,
4030 /*minimum_cmd_size*/ 10,
4031 /*sense_len*/ SSD_FULL_SIZE,
4032 /*timeout*/ SCSIOP_TIMEOUT);
4034 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4038 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4044 * The operation was successful. We could just copy the settings
4045 * the user requested, but just in case the drive ignored some of
4046 * our settings, let's ask for status again.
4048 error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP,
4049 NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL,
4050 NULL, NULL, dp_page, dp_size, 0);
4054 xpt_release_ccb(ccb);
4055 free(mode10_hdr, M_SCSISA);
4056 free(mode10_changeable, M_SCSISA);
4061 * The purpose of this function is to set one of four different parameters
4065 * - compression / compression algorithm
4068 * The assumption is that this will be called from saioctl(), and therefore
4069 * from a process context. Thus the waiting malloc calls below. If that
4070 * assumption ever changes, the malloc calls should be changed to be
4073 * Any or all of the four parameters may be set when this function is
4074 * called. It should handle setting more than one parameter at once.
4077 sasetparams(struct cam_periph *periph, sa_params params_to_set,
4078 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
4079 u_int32_t sense_flags)
4081 struct sa_softc *softc;
4082 u_int32_t current_blocksize;
4083 u_int32_t current_calg;
4084 u_int8_t current_density;
4085 u_int8_t current_speed;
4086 int comp_enabled, comp_supported;
4088 int mode_buffer_len;
4089 struct scsi_mode_header_6 *mode_hdr;
4090 struct scsi_mode_blk_desc *mode_blk;
4091 sa_comp_t *ccomp, *cpage;
4093 union ccb *ccb = NULL;
4096 softc = (struct sa_softc *)periph->softc;
4098 ccomp = malloc(sizeof (sa_comp_t), M_SCSISA, M_NOWAIT);
4103 * Since it doesn't make sense to set the number of blocks, or
4104 * write protection, we won't try to get the current value. We
4105 * always want to get the blocksize, so we can set it back to the
4108 error = sagetparams(periph,
4109 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
4110 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
4111 ¤t_speed, &comp_supported, &comp_enabled,
4112 ¤t_calg, ccomp, NULL, 0, 0);
4115 free(ccomp, M_SCSISA);
4119 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
4120 if (params_to_set & SA_PARAM_COMPRESSION)
4121 mode_buffer_len += sizeof (sa_comp_t);
4123 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
4124 if (mode_buffer == NULL) {
4125 free(ccomp, M_SCSISA);
4129 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
4130 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
4132 ccb = cam_periph_getccb(periph, 1);
4136 if (params_to_set & SA_PARAM_COMPRESSION) {
4138 cpage = (sa_comp_t *)&mode_blk[1];
4140 cpage = (sa_comp_t *)&mode_hdr[1];
4142 bcopy(ccomp, cpage, sizeof (sa_comp_t));
4143 cpage->hdr.pagecode &= ~0x80;
4148 * If the caller wants us to set the blocksize, use the one they
4149 * pass in. Otherwise, use the blocksize we got back from the
4150 * mode select above.
4153 if (params_to_set & SA_PARAM_BLOCKSIZE)
4154 scsi_ulto3b(blocksize, mode_blk->blklen);
4156 scsi_ulto3b(current_blocksize, mode_blk->blklen);
4159 * Set density if requested, else preserve old density.
4160 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
4161 * devices, else density we've latched up in our softc.
4163 if (params_to_set & SA_PARAM_DENSITY) {
4164 mode_blk->density = density;
4165 } else if (softc->scsi_rev > SCSI_REV_CCS) {
4166 mode_blk->density = SCSI_SAME_DENSITY;
4168 mode_blk->density = softc->media_density;
4173 * For mode selects, these two fields must be zero.
4175 mode_hdr->data_length = 0;
4176 mode_hdr->medium_type = 0;
4178 /* set the speed to the current value */
4179 mode_hdr->dev_spec = current_speed;
4181 /* if set, set single-initiator buffering mode */
4182 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
4183 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
4187 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
4189 mode_hdr->blk_desc_len = 0;
4192 * First, if the user wants us to set the compression algorithm or
4193 * just turn compression on, check to make sure that this drive
4194 * supports compression.
4196 if (params_to_set & SA_PARAM_COMPRESSION) {
4198 * If the compression algorithm is 0, disable compression.
4199 * If the compression algorithm is non-zero, enable
4200 * compression and set the compression type to the
4201 * specified compression algorithm, unless the algorithm is
4202 * MT_COMP_ENABLE. In that case, we look at the
4203 * compression algorithm that is currently set and if it is
4204 * non-zero, we leave it as-is. If it is zero, and we have
4205 * saved a compression algorithm from a time when
4206 * compression was enabled before, set the compression to
4209 switch (ccomp->hdr.pagecode & ~0x80) {
4210 case SA_DEVICE_CONFIGURATION_PAGE:
4212 struct scsi_dev_conf_page *dcp = &cpage->dconf;
4214 dcp->sel_comp_alg = SA_COMP_NONE;
4217 if (calg != MT_COMP_ENABLE) {
4218 dcp->sel_comp_alg = calg;
4219 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
4220 softc->saved_comp_algorithm != 0) {
4221 dcp->sel_comp_alg = softc->saved_comp_algorithm;
4225 case SA_DATA_COMPRESSION_PAGE:
4226 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
4227 struct scsi_data_compression_page *dcp = &cpage->dcomp;
4230 * Disable compression, but leave the
4231 * decompression and the capability bit
4234 dcp->dce_and_dcc = SA_DCP_DCC;
4235 dcp->dde_and_red |= SA_DCP_DDE;
4238 /* enable compression && decompression */
4239 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
4240 dcp->dde_and_red |= SA_DCP_DDE;
4242 * If there, use compression algorithm from caller.
4243 * Otherwise, if there's a saved compression algorithm
4244 * and there is no current algorithm, use the saved
4245 * algorithm. Else parrot back what we got and hope
4248 if (calg != MT_COMP_ENABLE) {
4249 scsi_ulto4b(calg, dcp->comp_algorithm);
4250 scsi_ulto4b(calg, dcp->decomp_algorithm);
4251 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
4252 softc->saved_comp_algorithm != 0) {
4253 scsi_ulto4b(softc->saved_comp_algorithm,
4254 dcp->comp_algorithm);
4255 scsi_ulto4b(softc->saved_comp_algorithm,
4256 dcp->decomp_algorithm);
4261 * Compression does not appear to be supported-
4262 * at least via the DATA COMPRESSION page. It
4263 * would be too much to ask us to believe that
4264 * the page itself is supported, but incorrectly
4265 * reports an ability to manipulate data compression,
4266 * so we'll assume that this device doesn't support
4267 * compression. We can just fall through for that.
4272 * The drive doesn't seem to support compression,
4273 * so turn off the set compression bit.
4275 params_to_set &= ~SA_PARAM_COMPRESSION;
4276 xpt_print(periph->path,
4277 "device does not seem to support compression\n");
4280 * If that was the only thing the user wanted us to set,
4281 * clean up allocated resources and return with
4282 * 'operation not supported'.
4284 if (params_to_set == SA_PARAM_NONE) {
4285 free(mode_buffer, M_SCSISA);
4286 xpt_release_ccb(ccb);
4291 * That wasn't the only thing the user wanted us to set.
4292 * So, decrease the stated mode buffer length by the
4293 * size of the compression mode page.
4295 mode_buffer_len -= sizeof(sa_comp_t);
4299 /* It is safe to retry this operation */
4300 scsi_mode_select(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
4301 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
4302 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
4304 error = cam_periph_runccb(ccb, saerror, 0,
4305 sense_flags, softc->device_stats);
4307 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
4309 char *xyz = mode_buffer;
4310 xpt_print_path(periph->path);
4311 printf("Err%d, Mode Select Data=", error);
4312 for (idx = 0; idx < mode_buffer_len; idx++)
4313 printf(" 0x%02x", xyz[idx] & 0xff);
4319 * If we can, try without setting density/blocksize.
4322 if ((params_to_set &
4323 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
4328 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
4329 cpage = (sa_comp_t *)&mode_blk[1];
4333 * If we were setting the blocksize, and that failed, we
4334 * want to set it to its original value. If we weren't
4335 * setting the blocksize, we don't want to change it.
4337 scsi_ulto3b(current_blocksize, mode_blk->blklen);
4340 * Set density if requested, else preserve old density.
4341 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
4342 * devices, else density we've latched up in our softc.
4344 if (params_to_set & SA_PARAM_DENSITY) {
4345 mode_blk->density = current_density;
4346 } else if (softc->scsi_rev > SCSI_REV_CCS) {
4347 mode_blk->density = SCSI_SAME_DENSITY;
4349 mode_blk->density = softc->media_density;
4352 if (params_to_set & SA_PARAM_COMPRESSION)
4353 bcopy(ccomp, cpage, sizeof (sa_comp_t));
4356 * The retry count is the only CCB field that might have been
4357 * changed that we care about, so reset it back to 1.
4359 ccb->ccb_h.retry_count = 1;
4360 cam_periph_runccb(ccb, saerror, 0, sense_flags,
4361 softc->device_stats);
4364 xpt_release_ccb(ccb);
4367 free(ccomp, M_SCSISA);
4369 if (params_to_set & SA_PARAM_COMPRESSION) {
4371 softc->flags &= ~SA_FLAG_COMP_ENABLED;
4373 * Even if we get an error setting compression,
4374 * do not say that we don't support it. We could
4375 * have been wrong, or it may be media specific.
4376 * softc->flags &= ~SA_FLAG_COMP_SUPP;
4378 softc->saved_comp_algorithm = softc->comp_algorithm;
4379 softc->comp_algorithm = 0;
4381 softc->flags |= SA_FLAG_COMP_ENABLED;
4382 softc->comp_algorithm = calg;
4386 free(mode_buffer, M_SCSISA);
4391 saextget(struct cdev *dev, struct cam_periph *periph, struct sbuf *sb,
4396 struct sa_softc *softc;
4399 struct ccb_getdev cgd;
4401 softc = (struct sa_softc *)periph->softc;
4405 error = sagetparams_common(dev, periph);
4407 goto extget_bailout;
4408 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount)
4412 SASBADDNODE(sb, indent, mtextget);
4414 * Basic CAM peripheral information.
4416 SASBADDVARSTR(sb, indent, periph->periph_name, %s, periph_name,
4417 strlen(periph->periph_name) + 1);
4418 SASBADDUINT(sb, indent, periph->unit_number, %u, unit_number);
4419 xpt_setup_ccb(&cgd.ccb_h,
4421 CAM_PRIORITY_NORMAL);
4422 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
4423 xpt_action((union ccb *)&cgd);
4424 if ((cgd.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4425 g->status = MT_EXT_GET_ERROR;
4426 snprintf(g->error_str, sizeof(g->error_str),
4427 "Error %#x returned for XPT_GDEV_TYPE CCB",
4429 goto extget_bailout;
4432 cam_strvis(tmpstr, cgd.inq_data.vendor,
4433 sizeof(cgd.inq_data.vendor), sizeof(tmpstr));
4434 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, vendor,
4435 sizeof(cgd.inq_data.vendor) + 1, "SCSI Vendor ID");
4437 cam_strvis(tmpstr, cgd.inq_data.product,
4438 sizeof(cgd.inq_data.product), sizeof(tmpstr));
4439 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, product,
4440 sizeof(cgd.inq_data.product) + 1, "SCSI Product ID");
4442 cam_strvis(tmpstr, cgd.inq_data.revision,
4443 sizeof(cgd.inq_data.revision), sizeof(tmpstr));
4444 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, revision,
4445 sizeof(cgd.inq_data.revision) + 1, "SCSI Revision");
4447 if (cgd.serial_num_len > 0) {
4454 if (cgd.serial_num_len > sizeof(tmpstr)) {
4455 ts2_len = cgd.serial_num_len + 1;
4457 tmpstr2 = malloc(ts2_len, M_SCSISA, M_NOWAIT | M_ZERO);
4459 * The 80 characters allocated on the stack above
4460 * will handle the vast majority of serial numbers.
4461 * If we run into one that is larger than that, and
4462 * we can't malloc the length without blocking,
4463 * bail out with an out of memory error.
4465 if (tmpstr2 == NULL) {
4467 goto extget_bailout;
4470 ts2_len = sizeof(tmpstr);
4475 cam_strvis(tmpstr2, cgd.serial_num, cgd.serial_num_len,
4478 SASBADDVARSTRDESC(sb, indent, tmpstr2, %s, serial_num,
4479 (ssize_t)cgd.serial_num_len + 1, "Serial Number");
4480 if (ts2_malloc != 0)
4481 free(tmpstr2, M_SCSISA);
4484 * We return a serial_num element in any case, but it will
4485 * be empty if the device has no serial number.
4488 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, serial_num,
4489 (ssize_t)0, "Serial Number");
4492 SASBADDUINTDESC(sb, indent, softc->maxio, %u, maxio,
4493 "Maximum I/O size allowed by driver and controller");
4495 SASBADDUINTDESC(sb, indent, softc->cpi_maxio, %u, cpi_maxio,
4496 "Maximum I/O size reported by controller");
4498 SASBADDUINTDESC(sb, indent, softc->max_blk, %u, max_blk,
4499 "Maximum block size supported by tape drive and media");
4501 SASBADDUINTDESC(sb, indent, softc->min_blk, %u, min_blk,
4502 "Minimum block size supported by tape drive and media");
4504 SASBADDUINTDESC(sb, indent, softc->blk_gran, %u, blk_gran,
4505 "Block granularity supported by tape drive and media");
4507 maxio_tmp = min(softc->max_blk, softc->maxio);
4509 SASBADDUINTDESC(sb, indent, maxio_tmp, %u, max_effective_iosize,
4510 "Maximum possible I/O size");
4512 SASBADDINTDESC(sb, indent, softc->flags & SA_FLAG_FIXED ? 1 : 0, %d,
4513 fixed_mode, "Set to 1 for fixed block mode, 0 for variable block");
4516 * XXX KDM include SIM, bus, target, LUN?
4518 if (softc->flags & SA_FLAG_COMP_UNSUPP)
4522 SASBADDINTDESC(sb, indent, tmpint, %d, compression_supported,
4523 "Set to 1 if compression is supported, 0 if not");
4524 if (softc->flags & SA_FLAG_COMP_ENABLED)
4528 SASBADDINTDESC(sb, indent, tmpint, %d, compression_enabled,
4529 "Set to 1 if compression is enabled, 0 if not");
4530 SASBADDUINTDESC(sb, indent, softc->comp_algorithm, %u,
4531 compression_algorithm, "Numeric compression algorithm");
4533 safillprot(softc, &indent, sb);
4535 SASBADDUINTDESC(sb, indent, softc->media_blksize, %u,
4536 media_blocksize, "Block size reported by drive or set by user");
4537 SASBADDINTDESC(sb, indent, (intmax_t)softc->fileno, %jd,
4538 calculated_fileno, "Calculated file number, -1 if unknown");
4539 SASBADDINTDESC(sb, indent, (intmax_t)softc->blkno, %jd,
4540 calculated_rel_blkno, "Calculated block number relative to file, "
4541 "set to -1 if unknown");
4542 SASBADDINTDESC(sb, indent, (intmax_t)softc->rep_fileno, %jd,
4543 reported_fileno, "File number reported by drive, -1 if unknown");
4544 SASBADDINTDESC(sb, indent, (intmax_t)softc->rep_blkno, %jd,
4545 reported_blkno, "Block number relative to BOP/BOT reported by "
4546 "drive, -1 if unknown");
4547 SASBADDINTDESC(sb, indent, (intmax_t)softc->partition, %jd,
4548 partition, "Current partition number, 0 is the default");
4549 SASBADDINTDESC(sb, indent, softc->bop, %d, bop,
4550 "Set to 1 if drive is at the beginning of partition/tape, 0 if "
4551 "not, -1 if unknown");
4552 SASBADDINTDESC(sb, indent, softc->eop, %d, eop,
4553 "Set to 1 if drive is past early warning, 0 if not, -1 if unknown");
4554 SASBADDINTDESC(sb, indent, softc->bpew, %d, bpew,
4555 "Set to 1 if drive is past programmable early warning, 0 if not, "
4557 SASBADDINTDESC(sb, indent, (intmax_t)softc->last_io_resid, %jd,
4558 residual, "Residual for the last I/O");
4560 * XXX KDM should we send a string with the current driver
4561 * status already decoded instead of a numeric value?
4563 SASBADDINTDESC(sb, indent, softc->dsreg, %d, dsreg,
4564 "Current state of the driver");
4566 safilldensitysb(softc, &indent, sb);
4568 SASBENDNODE(sb, indent, mtextget);
4576 saparamget(struct sa_softc *softc, struct sbuf *sb)
4581 SASBADDNODE(sb, indent, mtparamget);
4582 SASBADDINTDESC(sb, indent, softc->sili, %d, sili,
4583 "Suppress an error on underlength variable reads");
4584 SASBADDINTDESC(sb, indent, softc->eot_warn, %d, eot_warn,
4585 "Return an error to warn that end of tape is approaching");
4586 safillprot(softc, &indent, sb);
4587 SASBENDNODE(sb, indent, mtparamget);
4593 saprevent(struct cam_periph *periph, int action)
4595 struct sa_softc *softc;
4599 softc = (struct sa_softc *)periph->softc;
4601 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
4603 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
4607 * We can be quiet about illegal requests.
4609 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
4614 ccb = cam_periph_getccb(periph, 1);
4616 /* It is safe to retry this operation */
4617 scsi_prevent(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, action,
4618 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
4620 error = cam_periph_runccb(ccb, saerror, 0, sf, softc->device_stats);
4622 if (action == PR_ALLOW)
4623 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
4625 softc->flags |= SA_FLAG_TAPE_LOCKED;
4628 xpt_release_ccb(ccb);
4632 sarewind(struct cam_periph *periph)
4635 struct sa_softc *softc;
4638 softc = (struct sa_softc *)periph->softc;
4640 ccb = cam_periph_getccb(periph, 1);
4642 /* It is safe to retry this operation */
4643 scsi_rewind(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG, FALSE,
4644 SSD_FULL_SIZE, REWIND_TIMEOUT);
4646 softc->dsreg = MTIO_DSREG_REW;
4647 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4648 softc->dsreg = MTIO_DSREG_REST;
4650 xpt_release_ccb(ccb);
4652 softc->partition = softc->fileno = softc->blkno = (daddr_t) 0;
4653 softc->rep_fileno = softc->rep_blkno = (daddr_t) 0;
4655 softc->fileno = softc->blkno = (daddr_t) -1;
4656 softc->partition = (daddr_t) -1;
4657 softc->rep_fileno = softc->rep_blkno = (daddr_t) -1;
4663 saspace(struct cam_periph *periph, int count, scsi_space_code code)
4666 struct sa_softc *softc;
4669 softc = (struct sa_softc *)periph->softc;
4671 ccb = cam_periph_getccb(periph, 1);
4673 /* This cannot be retried */
4675 scsi_space(&ccb->csio, 0, NULL, MSG_SIMPLE_Q_TAG, code, count,
4676 SSD_FULL_SIZE, SPACE_TIMEOUT);
4679 * Clear residual because we will be using it.
4681 softc->last_ctl_resid = 0;
4683 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
4684 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4685 softc->dsreg = MTIO_DSREG_REST;
4687 xpt_release_ccb(ccb);
4690 * If a spacing operation has failed, we need to invalidate
4693 * If the spacing operation was setmarks or to end of recorded data,
4694 * we no longer know our relative position.
4696 * If the spacing operations was spacing files in reverse, we
4697 * take account of the residual, but still check against less
4698 * than zero- if we've gone negative, we must have hit BOT.
4700 * If the spacing operations was spacing records in reverse and
4701 * we have a residual, we've either hit BOT or hit a filemark.
4702 * In the former case, we know our new record number (0). In
4703 * the latter case, we have absolutely no idea what the real
4704 * record number is- we've stopped between the end of the last
4705 * record in the previous file and the filemark that stopped
4706 * our spacing backwards.
4709 softc->fileno = softc->blkno = (daddr_t) -1;
4710 softc->rep_blkno = softc->partition = (daddr_t) -1;
4711 softc->rep_fileno = (daddr_t) -1;
4712 } else if (code == SS_SETMARKS || code == SS_EOD) {
4713 softc->fileno = softc->blkno = (daddr_t) -1;
4714 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
4715 softc->fileno += (count - softc->last_ctl_resid);
4716 if (softc->fileno < 0) /* we must of hit BOT */
4719 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
4720 softc->blkno += (count - softc->last_ctl_resid);
4722 if (softc->last_ctl_resid || softc->blkno < 0) {
4723 if (softc->fileno == 0) {
4726 softc->blkno = (daddr_t) -1;
4738 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks, int immed)
4741 struct sa_softc *softc;
4744 softc = (struct sa_softc *)periph->softc;
4745 if (softc->open_rdonly)
4748 ccb = cam_periph_getccb(periph, 1);
4750 * Clear residual because we will be using it.
4752 softc->last_ctl_resid = 0;
4754 softc->dsreg = MTIO_DSREG_FMK;
4755 /* this *must* not be retried */
4756 scsi_write_filemarks(&ccb->csio, 0, NULL, MSG_SIMPLE_Q_TAG,
4757 immed, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
4758 softc->dsreg = MTIO_DSREG_REST;
4760 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4762 if (error == 0 && nmarks) {
4763 struct sa_softc *softc = (struct sa_softc *)periph->softc;
4764 nwm = nmarks - softc->last_ctl_resid;
4765 softc->filemarks += nwm;
4768 xpt_release_ccb(ccb);
4771 * Update relative positions (if we're doing that).
4774 softc->fileno = softc->blkno = softc->partition = (daddr_t) -1;
4775 } else if (softc->fileno != (daddr_t) -1) {
4776 softc->fileno += nwm;
4781 * Ask the tape drive for position information.
4786 * If we got valid position information, since we just wrote a file
4787 * mark, we know we're at the file mark and block 0 after that
4790 if (softc->rep_fileno != (daddr_t) -1) {
4791 softc->fileno = softc->rep_fileno;
4799 sagetpos(struct cam_periph *periph)
4802 struct scsi_tape_position_long_data long_pos;
4803 struct sa_softc *softc = (struct sa_softc *)periph->softc;
4806 if (softc->quirks & SA_QUIRK_NO_LONG_POS) {
4807 softc->rep_fileno = (daddr_t) -1;
4808 softc->rep_blkno = (daddr_t) -1;
4809 softc->bop = softc->eop = softc->bpew = -1;
4810 return (EOPNOTSUPP);
4813 bzero(&long_pos, sizeof(long_pos));
4815 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
4816 scsi_read_position_10(&ccb->csio,
4819 /*tag_action*/ MSG_SIMPLE_Q_TAG,
4820 /*service_action*/ SA_RPOS_LONG_FORM,
4821 /*data_ptr*/ (uint8_t *)&long_pos,
4822 /*length*/ sizeof(long_pos),
4823 /*sense_len*/ SSD_FULL_SIZE,
4824 /*timeout*/ SCSIOP_TIMEOUT);
4826 softc->dsreg = MTIO_DSREG_RBSY;
4827 error = cam_periph_runccb(ccb, saerror, 0, SF_QUIET_IR,
4828 softc->device_stats);
4829 softc->dsreg = MTIO_DSREG_REST;
4832 if (long_pos.flags & SA_RPOS_LONG_MPU) {
4834 * If the drive doesn't know what file mark it is
4835 * on, our calculated filemark isn't going to be
4838 softc->fileno = (daddr_t) -1;
4839 softc->rep_fileno = (daddr_t) -1;
4841 softc->fileno = softc->rep_fileno =
4842 scsi_8btou64(long_pos.logical_file_num);
4845 if (long_pos.flags & SA_RPOS_LONG_LONU) {
4846 softc->partition = (daddr_t) -1;
4847 softc->rep_blkno = (daddr_t) -1;
4849 * If the tape drive doesn't know its block
4850 * position, we can't claim to know it either.
4852 softc->blkno = (daddr_t) -1;
4854 softc->partition = scsi_4btoul(long_pos.partition);
4856 scsi_8btou64(long_pos.logical_object_num);
4858 if (long_pos.flags & SA_RPOS_LONG_BOP)
4863 if (long_pos.flags & SA_RPOS_LONG_EOP)
4868 if ((long_pos.flags & SA_RPOS_LONG_BPEW)
4869 || (softc->set_pews_status != 0)) {
4871 if (softc->set_pews_status > 0)
4872 softc->set_pews_status--;
4875 } else if (error == EINVAL) {
4877 * If this drive returned an invalid-request type error,
4878 * then it likely doesn't support the long form report.
4880 softc->quirks |= SA_QUIRK_NO_LONG_POS;
4884 softc->rep_fileno = softc->rep_blkno = (daddr_t) -1;
4885 softc->partition = (daddr_t) -1;
4886 softc->bop = softc->eop = softc->bpew = -1;
4889 xpt_release_ccb(ccb);
4895 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
4897 struct scsi_tape_position_data loc;
4899 struct sa_softc *softc = (struct sa_softc *)periph->softc;
4903 * We try and flush any buffered writes here if we were writing
4904 * and we're trying to get hardware block position. It eats
4905 * up performance substantially, but I'm wary of drive firmware.
4907 * I think that *logical* block position is probably okay-
4908 * but hardware block position might have to wait for data
4909 * to hit media to be valid. Caveat Emptor.
4912 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
4913 error = sawritefilemarks(periph, 0, 0, 0);
4914 if (error && error != EACCES)
4918 ccb = cam_periph_getccb(periph, 1);
4919 scsi_read_position(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG,
4920 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
4921 softc->dsreg = MTIO_DSREG_RBSY;
4922 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4923 softc->dsreg = MTIO_DSREG_REST;
4926 if (loc.flags & SA_RPOS_UNCERTAIN) {
4927 error = EINVAL; /* nothing is certain */
4929 *blkptr = scsi_4btoul(loc.firstblk);
4933 xpt_release_ccb(ccb);
4938 sasetpos(struct cam_periph *periph, int hard, struct mtlocate *locate_info)
4941 struct sa_softc *softc;
4947 * We used to try and flush any buffered writes here.
4948 * Now we push this onto user applications to either
4949 * flush the pending writes themselves (via a zero count
4950 * WRITE FILEMARKS command) or they can trust their tape
4951 * drive to do this correctly for them.
4954 softc = (struct sa_softc *)periph->softc;
4955 ccb = cam_periph_getccb(periph, 1);
4957 cp = locate_info->flags & MT_LOCATE_FLAG_CHANGE_PART ? 1 : 0;
4958 immed = locate_info->flags & MT_LOCATE_FLAG_IMMED ? 1 : 0;
4961 * Determine whether we have to use LOCATE or LOCATE16. The hard
4962 * bit is only possible with LOCATE, but the new ioctls do not
4963 * allow setting that bit. So we can't get into the situation of
4964 * having the hard bit set with a block address that is larger than
4969 else if ((locate_info->dest_type != MT_LOCATE_DEST_OBJECT)
4970 || (locate_info->block_address_mode != MT_LOCATE_BAM_IMPLICIT)
4971 || (locate_info->logical_id > SA_SPOS_MAX_BLK))
4976 if (locate16 != 0) {
4977 scsi_locate_16(&ccb->csio,
4980 /*tag_action*/ MSG_SIMPLE_Q_TAG,
4983 /*dest_type*/ locate_info->dest_type,
4984 /*bam*/ locate_info->block_address_mode,
4985 /*partition*/ locate_info->partition,
4986 /*logical_id*/ locate_info->logical_id,
4987 /*sense_len*/ SSD_FULL_SIZE,
4988 /*timeout*/ SPACE_TIMEOUT);
4990 scsi_locate_10(&ccb->csio,
4993 /*tag_action*/ MSG_SIMPLE_Q_TAG,
4997 /*partition*/ locate_info->partition,
4998 /*block_address*/ locate_info->logical_id,
4999 /*sense_len*/ SSD_FULL_SIZE,
5000 /*timeout*/ SPACE_TIMEOUT);
5003 softc->dsreg = MTIO_DSREG_POS;
5004 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5005 softc->dsreg = MTIO_DSREG_REST;
5006 xpt_release_ccb(ccb);
5009 * We assume the calculated file and block numbers are unknown
5010 * unless we have enough information to populate them.
5012 softc->fileno = softc->blkno = (daddr_t) -1;
5015 * If the user requested changing the partition and the request
5016 * succeeded, note the partition.
5020 softc->partition = locate_info->partition;
5022 softc->partition = (daddr_t) -1;
5025 switch (locate_info->dest_type) {
5026 case MT_LOCATE_DEST_FILE:
5028 * This is the only case where we can reliably
5029 * calculate the file and block numbers.
5031 softc->fileno = locate_info->logical_id;
5034 case MT_LOCATE_DEST_OBJECT:
5035 case MT_LOCATE_DEST_SET:
5036 case MT_LOCATE_DEST_EOD:
5043 * Ask the drive for current position information.
5051 saretension(struct cam_periph *periph)
5054 struct sa_softc *softc;
5057 softc = (struct sa_softc *)periph->softc;
5059 ccb = cam_periph_getccb(periph, 1);
5061 /* It is safe to retry this operation */
5062 scsi_load_unload(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, FALSE,
5063 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
5065 softc->dsreg = MTIO_DSREG_TEN;
5066 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5067 softc->dsreg = MTIO_DSREG_REST;
5069 xpt_release_ccb(ccb);
5071 softc->partition = softc->fileno = softc->blkno = (daddr_t) 0;
5074 softc->partition = softc->fileno = softc->blkno = (daddr_t) -1;
5079 sareservereleaseunit(struct cam_periph *periph, int reserve)
5082 struct sa_softc *softc;
5085 softc = (struct sa_softc *)periph->softc;
5086 ccb = cam_periph_getccb(periph, 1);
5088 /* It is safe to retry this operation */
5089 scsi_reserve_release_unit(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG,
5090 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
5091 softc->dsreg = MTIO_DSREG_RBSY;
5092 error = cam_periph_runccb(ccb, saerror, 0,
5093 SF_RETRY_UA | SF_NO_PRINT, softc->device_stats);
5094 softc->dsreg = MTIO_DSREG_REST;
5095 xpt_release_ccb(ccb);
5098 * If the error was Illegal Request, then the device doesn't support
5099 * RESERVE/RELEASE. This is not an error.
5101 if (error == EINVAL) {
5109 saloadunload(struct cam_periph *periph, int load)
5112 struct sa_softc *softc;
5115 softc = (struct sa_softc *)periph->softc;
5117 ccb = cam_periph_getccb(periph, 1);
5119 /* It is safe to retry this operation */
5120 scsi_load_unload(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, FALSE,
5121 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
5123 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
5124 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5125 softc->dsreg = MTIO_DSREG_REST;
5126 xpt_release_ccb(ccb);
5128 if (error || load == 0) {
5129 softc->partition = softc->fileno = softc->blkno = (daddr_t) -1;
5130 softc->rep_fileno = softc->rep_blkno = (daddr_t) -1;
5131 } else if (error == 0) {
5132 softc->partition = softc->fileno = softc->blkno = (daddr_t) 0;
5139 saerase(struct cam_periph *periph, int longerase)
5143 struct sa_softc *softc;
5146 softc = (struct sa_softc *)periph->softc;
5147 if (softc->open_rdonly)
5150 ccb = cam_periph_getccb(periph, 1);
5152 scsi_erase(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, FALSE, longerase,
5153 SSD_FULL_SIZE, ERASE_TIMEOUT);
5155 softc->dsreg = MTIO_DSREG_ZER;
5156 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5157 softc->dsreg = MTIO_DSREG_REST;
5159 xpt_release_ccb(ccb);
5164 * Fill an sbuf with density data in XML format. This particular macro
5165 * works for multi-byte integer fields.
5167 * Note that 1 byte fields aren't supported here. The reason is that the
5168 * compiler does not evaluate the sizeof(), and assumes that any of the
5169 * sizes are possible for a given field. So passing in a multi-byte
5170 * field will result in a warning that the assignment makes an integer
5171 * from a pointer without a cast, if there is an assignment in the 1 byte
5174 #define SAFILLDENSSB(dens_data, sb, indent, field, desc_remain, \
5175 len_to_go, cur_offset, desc){ \
5176 size_t cur_field_len; \
5178 cur_field_len = sizeof(dens_data->field); \
5179 if (desc_remain < cur_field_len) { \
5180 len_to_go -= desc_remain; \
5181 cur_offset += desc_remain; \
5184 len_to_go -= cur_field_len; \
5185 cur_offset += cur_field_len; \
5186 desc_remain -= cur_field_len; \
5188 switch (sizeof(dens_data->field)) { \
5190 KASSERT(1 == 0, ("Programmer error, invalid 1 byte " \
5191 "field width for SAFILLDENSFIELD")); \
5194 SASBADDUINTDESC(sb, indent, \
5195 scsi_2btoul(dens_data->field), %u, field, desc); \
5198 SASBADDUINTDESC(sb, indent, \
5199 scsi_3btoul(dens_data->field), %u, field, desc); \
5202 SASBADDUINTDESC(sb, indent, \
5203 scsi_4btoul(dens_data->field), %u, field, desc); \
5206 SASBADDUINTDESC(sb, indent, \
5207 (uintmax_t)scsi_8btou64(dens_data->field), %ju, \
5215 * Fill an sbuf with density data in XML format. This particular macro
5216 * works for strings.
5218 #define SAFILLDENSSBSTR(dens_data, sb, indent, field, desc_remain, \
5219 len_to_go, cur_offset, desc){ \
5220 size_t cur_field_len; \
5223 cur_field_len = sizeof(dens_data->field); \
5224 if (desc_remain < cur_field_len) { \
5225 len_to_go -= desc_remain; \
5226 cur_offset += desc_remain; \
5229 len_to_go -= cur_field_len; \
5230 cur_offset += cur_field_len; \
5231 desc_remain -= cur_field_len; \
5233 cam_strvis(tmpstr, dens_data->field, \
5234 sizeof(dens_data->field), sizeof(tmpstr)); \
5235 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, field, \
5236 strlen(tmpstr) + 1, desc); \
5240 * Fill an sbuf with density data descriptors.
5243 safilldenstypesb(struct sbuf *sb, int *indent, uint8_t *buf, int buf_len,
5246 struct scsi_density_hdr *hdr;
5248 int len_to_go, cur_offset;
5250 int num_reports, need_close;
5253 * We need at least the header length. Note that this isn't an
5254 * error, not all tape drives will have every data type.
5256 if (buf_len < sizeof(*hdr))
5259 hdr = (struct scsi_density_hdr *)buf;
5260 hdr_len = scsi_2btoul(hdr->length);
5261 len_to_go = min(buf_len - sizeof(*hdr), hdr_len);
5263 length_offset = __offsetof(struct scsi_density_data,
5266 length_offset = __offsetof(struct scsi_medium_type_data,
5269 cur_offset = sizeof(*hdr);
5274 while (len_to_go > length_offset) {
5275 struct scsi_density_data *dens_data;
5276 struct scsi_medium_type_data *type_data;
5278 size_t cur_field_len;
5284 dens_data =(struct scsi_density_data *)&buf[cur_offset];
5285 if (dens_data->byte2 & SDD_DLV)
5286 desc_remain = scsi_2btoul(dens_data->length);
5288 desc_remain = SDD_DEFAULT_LENGTH -
5291 type_data = (struct scsi_medium_type_data *)
5293 desc_remain = scsi_2btoul(type_data->length);
5296 len_to_go -= length_offset;
5297 desc_remain = min(desc_remain, len_to_go);
5298 cur_offset += length_offset;
5300 if (need_close != 0) {
5301 SASBENDNODE(sb, *indent, density_entry);
5304 SASBADDNODENUM(sb, *indent, density_entry, num_reports);
5309 SASBADDUINTDESC(sb, *indent,
5310 dens_data->primary_density_code, %u,
5311 primary_density_code, "Primary Density Code");
5312 SASBADDUINTDESC(sb, *indent,
5313 dens_data->secondary_density_code, %u,
5314 secondary_density_code, "Secondary Density Code");
5315 SASBADDUINTDESC(sb, *indent,
5316 dens_data->byte2 & ~SDD_DLV, %#x, density_flags,
5319 SAFILLDENSSB(dens_data, sb, *indent, bits_per_mm,
5320 desc_remain, len_to_go, cur_offset, "Bits per mm");
5321 SAFILLDENSSB(dens_data, sb, *indent, media_width,
5322 desc_remain, len_to_go, cur_offset, "Media width");
5323 SAFILLDENSSB(dens_data, sb, *indent, tracks,
5324 desc_remain, len_to_go, cur_offset,
5325 "Number of Tracks");
5326 SAFILLDENSSB(dens_data, sb, *indent, capacity,
5327 desc_remain, len_to_go, cur_offset, "Capacity");
5329 SAFILLDENSSBSTR(dens_data, sb, *indent, assigning_org,
5330 desc_remain, len_to_go, cur_offset,
5331 "Assigning Organization");
5333 SAFILLDENSSBSTR(dens_data, sb, *indent, density_name,
5334 desc_remain, len_to_go, cur_offset, "Density Name");
5336 SAFILLDENSSBSTR(dens_data, sb, *indent, description,
5337 desc_remain, len_to_go, cur_offset, "Description");
5341 SASBADDUINTDESC(sb, *indent, type_data->medium_type,
5342 %u, medium_type, "Medium Type");
5345 __offsetof(struct scsi_medium_type_data,
5347 __offsetof(struct scsi_medium_type_data,
5350 if (desc_remain < cur_field_len) {
5351 len_to_go -= desc_remain;
5352 cur_offset += desc_remain;
5355 len_to_go -= cur_field_len;
5356 cur_offset += cur_field_len;
5357 desc_remain -= cur_field_len;
5359 SASBADDINTDESC(sb, *indent,
5360 type_data->num_density_codes, %d,
5361 num_density_codes, "Number of Density Codes");
5362 SASBADDNODE(sb, *indent, density_code_list);
5363 for (i = 0; i < type_data->num_density_codes;
5365 SASBADDUINTDESC(sb, *indent,
5366 type_data->primary_density_codes[i], %u,
5367 density_code, "Density Code");
5369 SASBENDNODE(sb, *indent, density_code_list);
5371 SAFILLDENSSB(type_data, sb, *indent, media_width,
5372 desc_remain, len_to_go, cur_offset,
5374 SAFILLDENSSB(type_data, sb, *indent, medium_length,
5375 desc_remain, len_to_go, cur_offset,
5379 * Account for the two reserved bytes.
5381 cur_field_len = sizeof(type_data->reserved2);
5382 if (desc_remain < cur_field_len) {
5383 len_to_go -= desc_remain;
5384 cur_offset += desc_remain;
5387 len_to_go -= cur_field_len;
5388 cur_offset += cur_field_len;
5389 desc_remain -= cur_field_len;
5391 SAFILLDENSSBSTR(type_data, sb, *indent, assigning_org,
5392 desc_remain, len_to_go, cur_offset,
5393 "Assigning Organization");
5394 SAFILLDENSSBSTR(type_data, sb, *indent,
5395 medium_type_name, desc_remain, len_to_go,
5396 cur_offset, "Medium type name");
5397 SAFILLDENSSBSTR(type_data, sb, *indent, description,
5398 desc_remain, len_to_go, cur_offset, "Description");
5401 if (need_close != 0) {
5402 SASBENDNODE(sb, *indent, density_entry);
5410 * Fill an sbuf with density data information
5413 safilldensitysb(struct sa_softc *softc, int *indent, struct sbuf *sb)
5417 SASBADDNODE(sb, *indent, mtdensity);
5418 SASBADDUINTDESC(sb, *indent, softc->media_density, %u, media_density,
5419 "Current Medium Density");
5421 for (i = 0; i < SA_DENSITY_TYPES; i++) {
5424 if (softc->density_info_valid[i] == 0)
5427 SASBADDNODE(sb, *indent, density_report);
5428 if (softc->density_type_bits[i] & SRDS_MEDIUM_TYPE) {
5435 SASBADDINTDESC(sb, *indent, tmpint, %d, medium_type_report,
5436 "Medium type report");
5438 if (softc->density_type_bits[i] & SRDS_MEDIA)
5442 SASBADDINTDESC(sb, *indent, tmpint, %d, media_report,
5445 safilldenstypesb(sb, indent, softc->density_info[i],
5446 softc->density_info_valid[i], is_density);
5447 SASBENDNODE(sb, *indent, density_report);
5449 SASBENDNODE(sb, *indent, mtdensity);
5452 #endif /* _KERNEL */
5455 * Read tape block limits command.
5458 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
5459 void (*cbfcnp)(struct cam_periph *, union ccb *),
5460 u_int8_t tag_action,
5461 struct scsi_read_block_limits_data *rlimit_buf,
5462 u_int8_t sense_len, u_int32_t timeout)
5464 struct scsi_read_block_limits *scsi_cmd;
5466 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
5467 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
5468 sizeof(*scsi_cmd), timeout);
5470 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
5471 bzero(scsi_cmd, sizeof(*scsi_cmd));
5472 scsi_cmd->opcode = READ_BLOCK_LIMITS;
5476 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
5477 void (*cbfcnp)(struct cam_periph *, union ccb *),
5478 u_int8_t tag_action, int readop, int sli,
5479 int fixed, u_int32_t length, u_int8_t *data_ptr,
5480 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
5482 struct scsi_sa_rw *scsi_cmd;
5485 read = (readop & SCSI_RW_DIRMASK) == SCSI_RW_READ;
5487 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
5488 scsi_cmd->opcode = read ? SA_READ : SA_WRITE;
5489 scsi_cmd->sli_fixed = 0;
5491 scsi_cmd->sli_fixed |= SAR_SLI;
5493 scsi_cmd->sli_fixed |= SARW_FIXED;
5494 scsi_ulto3b(length, scsi_cmd->length);
5495 scsi_cmd->control = 0;
5497 cam_fill_csio(csio, retries, cbfcnp, (read ? CAM_DIR_IN : CAM_DIR_OUT) |
5498 ((readop & SCSI_RW_BIO) != 0 ? CAM_DATA_BIO : 0),
5499 tag_action, data_ptr, dxfer_len, sense_len,
5500 sizeof(*scsi_cmd), timeout);
5504 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
5505 void (*cbfcnp)(struct cam_periph *, union ccb *),
5506 u_int8_t tag_action, int immediate, int eot,
5507 int reten, int load, u_int8_t sense_len,
5510 struct scsi_load_unload *scsi_cmd;
5512 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
5513 bzero(scsi_cmd, sizeof(*scsi_cmd));
5514 scsi_cmd->opcode = LOAD_UNLOAD;
5516 scsi_cmd->immediate = SLU_IMMED;
5518 scsi_cmd->eot_reten_load |= SLU_EOT;
5520 scsi_cmd->eot_reten_load |= SLU_RETEN;
5522 scsi_cmd->eot_reten_load |= SLU_LOAD;
5524 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
5525 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
5529 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
5530 void (*cbfcnp)(struct cam_periph *, union ccb *),
5531 u_int8_t tag_action, int immediate, u_int8_t sense_len,
5534 struct scsi_rewind *scsi_cmd;
5536 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
5537 bzero(scsi_cmd, sizeof(*scsi_cmd));
5538 scsi_cmd->opcode = REWIND;
5540 scsi_cmd->immediate = SREW_IMMED;
5542 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
5543 0, sense_len, sizeof(*scsi_cmd), timeout);
5547 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
5548 void (*cbfcnp)(struct cam_periph *, union ccb *),
5549 u_int8_t tag_action, scsi_space_code code,
5550 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
5552 struct scsi_space *scsi_cmd;
5554 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
5555 scsi_cmd->opcode = SPACE;
5556 scsi_cmd->code = code;
5557 scsi_ulto3b(count, scsi_cmd->count);
5558 scsi_cmd->control = 0;
5560 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
5561 0, sense_len, sizeof(*scsi_cmd), timeout);
5565 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
5566 void (*cbfcnp)(struct cam_periph *, union ccb *),
5567 u_int8_t tag_action, int immediate, int setmark,
5568 u_int32_t num_marks, u_int8_t sense_len,
5571 struct scsi_write_filemarks *scsi_cmd;
5573 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
5574 bzero(scsi_cmd, sizeof(*scsi_cmd));
5575 scsi_cmd->opcode = WRITE_FILEMARKS;
5577 scsi_cmd->byte2 |= SWFMRK_IMMED;
5579 scsi_cmd->byte2 |= SWFMRK_WSMK;
5581 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
5583 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
5584 0, sense_len, sizeof(*scsi_cmd), timeout);
5588 * The reserve and release unit commands differ only by their opcodes.
5591 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
5592 void (*cbfcnp)(struct cam_periph *, union ccb *),
5593 u_int8_t tag_action, int third_party,
5594 int third_party_id, u_int8_t sense_len,
5595 u_int32_t timeout, int reserve)
5597 struct scsi_reserve_release_unit *scsi_cmd;
5599 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
5600 bzero(scsi_cmd, sizeof(*scsi_cmd));
5603 scsi_cmd->opcode = RESERVE_UNIT;
5605 scsi_cmd->opcode = RELEASE_UNIT;
5608 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
5609 scsi_cmd->lun_thirdparty |=
5610 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
5613 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
5614 0, sense_len, sizeof(*scsi_cmd), timeout);
5618 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
5619 void (*cbfcnp)(struct cam_periph *, union ccb *),
5620 u_int8_t tag_action, int immediate, int long_erase,
5621 u_int8_t sense_len, u_int32_t timeout)
5623 struct scsi_erase *scsi_cmd;
5625 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
5626 bzero(scsi_cmd, sizeof(*scsi_cmd));
5628 scsi_cmd->opcode = ERASE;
5631 scsi_cmd->lun_imm_long |= SE_IMMED;
5634 scsi_cmd->lun_imm_long |= SE_LONG;
5636 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
5637 0, sense_len, sizeof(*scsi_cmd), timeout);
5641 * Read Tape Position command.
5644 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
5645 void (*cbfcnp)(struct cam_periph *, union ccb *),
5646 u_int8_t tag_action, int hardsoft,
5647 struct scsi_tape_position_data *sbp,
5648 u_int8_t sense_len, u_int32_t timeout)
5650 struct scsi_tape_read_position *scmd;
5652 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
5653 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
5654 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
5655 bzero(scmd, sizeof(*scmd));
5656 scmd->opcode = READ_POSITION;
5657 scmd->byte1 = hardsoft;
5661 * Read Tape Position command.
5664 scsi_read_position_10(struct ccb_scsiio *csio, u_int32_t retries,
5665 void (*cbfcnp)(struct cam_periph *, union ccb *),
5666 u_int8_t tag_action, int service_action,
5667 u_int8_t *data_ptr, u_int32_t length,
5668 u_int32_t sense_len, u_int32_t timeout)
5670 struct scsi_tape_read_position *scmd;
5675 /*flags*/CAM_DIR_IN,
5677 /*data_ptr*/data_ptr,
5678 /*dxfer_len*/length,
5683 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
5684 bzero(scmd, sizeof(*scmd));
5685 scmd->opcode = READ_POSITION;
5686 scmd->byte1 = service_action;
5688 * The length is only currently set (as of SSC4r03) if the extended
5689 * form is specified. The other forms have fixed lengths.
5691 if (service_action == SA_RPOS_EXTENDED_FORM)
5692 scsi_ulto2b(length, scmd->length);
5696 * Set Tape Position command.
5699 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
5700 void (*cbfcnp)(struct cam_periph *, union ccb *),
5701 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
5702 u_int8_t sense_len, u_int32_t timeout)
5704 struct scsi_tape_locate *scmd;
5706 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
5707 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
5708 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
5709 bzero(scmd, sizeof(*scmd));
5710 scmd->opcode = LOCATE;
5712 scmd->byte1 |= SA_SPOS_BT;
5713 scsi_ulto4b(blkno, scmd->blkaddr);
5717 * XXX KDM figure out how to make a compatibility function.
5720 scsi_locate_10(struct ccb_scsiio *csio, u_int32_t retries,
5721 void (*cbfcnp)(struct cam_periph *, union ccb *),
5722 u_int8_t tag_action, int immed, int cp, int hard,
5723 int64_t partition, u_int32_t block_address,
5724 int sense_len, u_int32_t timeout)
5726 struct scsi_tape_locate *scmd;
5738 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
5739 bzero(scmd, sizeof(*scmd));
5740 scmd->opcode = LOCATE;
5742 scmd->byte1 |= SA_SPOS_IMMED;
5744 scmd->byte1 |= SA_SPOS_CP;
5746 scmd->byte1 |= SA_SPOS_BT;
5747 scsi_ulto4b(block_address, scmd->blkaddr);
5748 scmd->partition = partition;
5752 scsi_locate_16(struct ccb_scsiio *csio, u_int32_t retries,
5753 void (*cbfcnp)(struct cam_periph *, union ccb *),
5754 u_int8_t tag_action, int immed, int cp, u_int8_t dest_type,
5755 int bam, int64_t partition, u_int64_t logical_id,
5756 int sense_len, u_int32_t timeout)
5759 struct scsi_locate_16 *scsi_cmd;
5764 /*flags*/CAM_DIR_NONE,
5772 scsi_cmd = (struct scsi_locate_16 *)&csio->cdb_io.cdb_bytes;
5773 bzero(scsi_cmd, sizeof(*scsi_cmd));
5774 scsi_cmd->opcode = LOCATE_16;
5776 scsi_cmd->byte1 |= SA_LC_IMMEDIATE;
5778 scsi_cmd->byte1 |= SA_LC_CP;
5779 scsi_cmd->byte1 |= (dest_type << SA_LC_DEST_TYPE_SHIFT);
5781 scsi_cmd->byte2 |= bam;
5782 scsi_cmd->partition = partition;
5783 scsi_u64to8b(logical_id, scsi_cmd->logical_id);
5787 scsi_report_density_support(struct ccb_scsiio *csio, u_int32_t retries,
5788 void (*cbfcnp)(struct cam_periph *, union ccb *),
5789 u_int8_t tag_action, int media, int medium_type,
5790 u_int8_t *data_ptr, u_int32_t length,
5791 u_int32_t sense_len, u_int32_t timeout)
5793 struct scsi_report_density_support *scsi_cmd;
5795 scsi_cmd =(struct scsi_report_density_support *)&csio->cdb_io.cdb_bytes;
5796 bzero(scsi_cmd, sizeof(*scsi_cmd));
5798 scsi_cmd->opcode = REPORT_DENSITY_SUPPORT;
5800 scsi_cmd->byte1 |= SRDS_MEDIA;
5801 if (medium_type != 0)
5802 scsi_cmd->byte1 |= SRDS_MEDIUM_TYPE;
5804 scsi_ulto2b(length, scsi_cmd->length);
5809 /*flags*/CAM_DIR_IN,
5811 /*data_ptr*/data_ptr,
5812 /*dxfer_len*/length,
5819 scsi_set_capacity(struct ccb_scsiio *csio, u_int32_t retries,
5820 void (*cbfcnp)(struct cam_periph *, union ccb *),
5821 u_int8_t tag_action, int byte1, u_int32_t proportion,
5822 u_int32_t sense_len, u_int32_t timeout)
5824 struct scsi_set_capacity *scsi_cmd;
5826 scsi_cmd = (struct scsi_set_capacity *)&csio->cdb_io.cdb_bytes;
5827 bzero(scsi_cmd, sizeof(*scsi_cmd));
5829 scsi_cmd->opcode = SET_CAPACITY;
5831 scsi_cmd->byte1 = byte1;
5832 scsi_ulto2b(proportion, scsi_cmd->cap_proportion);
5837 /*flags*/CAM_DIR_NONE,
5847 scsi_format_medium(struct ccb_scsiio *csio, u_int32_t retries,
5848 void (*cbfcnp)(struct cam_periph *, union ccb *),
5849 u_int8_t tag_action, int byte1, int byte2,
5850 u_int8_t *data_ptr, u_int32_t dxfer_len,
5851 u_int32_t sense_len, u_int32_t timeout)
5853 struct scsi_format_medium *scsi_cmd;
5855 scsi_cmd = (struct scsi_format_medium*)&csio->cdb_io.cdb_bytes;
5856 bzero(scsi_cmd, sizeof(*scsi_cmd));
5858 scsi_cmd->opcode = FORMAT_MEDIUM;
5860 scsi_cmd->byte1 = byte1;
5861 scsi_cmd->byte2 = byte2;
5863 scsi_ulto2b(dxfer_len, scsi_cmd->length);
5868 /*flags*/(dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5870 /*data_ptr*/ data_ptr,
5871 /*dxfer_len*/ dxfer_len,
5878 scsi_allow_overwrite(struct ccb_scsiio *csio, u_int32_t retries,
5879 void (*cbfcnp)(struct cam_periph *, union ccb *),
5880 u_int8_t tag_action, int allow_overwrite, int partition,
5881 u_int64_t logical_id, u_int32_t sense_len, u_int32_t timeout)
5883 struct scsi_allow_overwrite *scsi_cmd;
5885 scsi_cmd = (struct scsi_allow_overwrite *)&csio->cdb_io.cdb_bytes;
5886 bzero(scsi_cmd, sizeof(*scsi_cmd));
5888 scsi_cmd->opcode = ALLOW_OVERWRITE;
5890 scsi_cmd->allow_overwrite = allow_overwrite;
5891 scsi_cmd->partition = partition;
5892 scsi_u64to8b(logical_id, scsi_cmd->logical_id);