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
129 SA_FLAG_OPEN = 0x0001,
130 SA_FLAG_FIXED = 0x0002,
131 SA_FLAG_TAPE_LOCKED = 0x0004,
132 SA_FLAG_TAPE_MOUNTED = 0x0008,
133 SA_FLAG_TAPE_WP = 0x0010,
134 SA_FLAG_TAPE_WRITTEN = 0x0020,
135 SA_FLAG_EOM_PENDING = 0x0040,
136 SA_FLAG_EIO_PENDING = 0x0080,
137 SA_FLAG_EOF_PENDING = 0x0100,
138 SA_FLAG_ERR_PENDING = (SA_FLAG_EOM_PENDING|SA_FLAG_EIO_PENDING|
139 SA_FLAG_EOF_PENDING),
140 SA_FLAG_INVALID = 0x0200,
141 SA_FLAG_COMP_ENABLED = 0x0400,
142 SA_FLAG_COMP_SUPP = 0x0800,
143 SA_FLAG_COMP_UNSUPP = 0x1000,
144 SA_FLAG_TAPE_FROZEN = 0x2000,
145 SA_FLAG_PROTECT_SUPP = 0x4000,
147 SA_FLAG_COMPRESSION = (SA_FLAG_COMP_SUPP|SA_FLAG_COMP_ENABLED|
148 SA_FLAG_COMP_UNSUPP),
149 SA_FLAG_SCTX_INIT = 0x8000
153 SA_MODE_REWIND = 0x00,
154 SA_MODE_NOREWIND = 0x01,
155 SA_MODE_OFFLINE = 0x02
159 SA_PARAM_NONE = 0x000,
160 SA_PARAM_BLOCKSIZE = 0x001,
161 SA_PARAM_DENSITY = 0x002,
162 SA_PARAM_COMPRESSION = 0x004,
163 SA_PARAM_BUFF_MODE = 0x008,
164 SA_PARAM_NUMBLOCKS = 0x010,
166 SA_PARAM_SPEED = 0x040,
167 SA_PARAM_DENSITY_EXT = 0x080,
168 SA_PARAM_LBP = 0x100,
173 SA_QUIRK_NONE = 0x000,
174 SA_QUIRK_NOCOMP = 0x001, /* Can't deal with compression at all*/
175 SA_QUIRK_FIXED = 0x002, /* Force fixed mode */
176 SA_QUIRK_VARIABLE = 0x004, /* Force variable mode */
177 SA_QUIRK_2FM = 0x008, /* Needs Two File Marks at EOD */
178 SA_QUIRK_1FM = 0x010, /* No more than 1 File Mark at EOD */
179 SA_QUIRK_NODREAD = 0x020, /* Don't try and dummy read density */
180 SA_QUIRK_NO_MODESEL = 0x040, /* Don't do mode select at all */
181 SA_QUIRK_NO_CPAGE = 0x080, /* Don't use DEVICE COMPRESSION page */
182 SA_QUIRK_NO_LONG_POS = 0x100 /* No long position information */
185 #define SA_QUIRK_BIT_STRING \
197 #define SAMODE(z) (dev2unit(z) & 0x3)
198 #define SA_IS_CTRL(z) (dev2unit(z) & (1 << 4))
200 #define SA_NOT_CTLDEV 0
204 #define SA_ATYPE_NR 1
205 #define SA_ATYPE_ER 2
206 #define SA_NUM_ATYPES 3
208 #define SAMINOR(ctl, access) \
209 ((ctl << 4) | (access & 0x3))
212 struct cdev *ctl_dev;
218 #define SASBADDBASE(sb, indent, data, xfmt, name, type, xsize, desc) \
219 sbuf_printf(sb, "%*s<%s type=\"%s\" size=\"%zd\" " \
220 "fmt=\"%s\" desc=\"%s\">" #xfmt "</%s>\n", indent, "", \
221 #name, #type, xsize, #xfmt, desc ? desc : "", data, #name);
223 #define SASBADDINT(sb, indent, data, fmt, name) \
224 SASBADDBASE(sb, indent, data, fmt, name, int, sizeof(data), \
227 #define SASBADDINTDESC(sb, indent, data, fmt, name, desc) \
228 SASBADDBASE(sb, indent, data, fmt, name, int, sizeof(data), \
231 #define SASBADDUINT(sb, indent, data, fmt, name) \
232 SASBADDBASE(sb, indent, data, fmt, name, uint, sizeof(data), \
235 #define SASBADDUINTDESC(sb, indent, data, fmt, name, desc) \
236 SASBADDBASE(sb, indent, data, fmt, name, uint, sizeof(data), \
239 #define SASBADDFIXEDSTR(sb, indent, data, fmt, name) \
240 SASBADDBASE(sb, indent, data, fmt, name, str, sizeof(data), \
243 #define SASBADDFIXEDSTRDESC(sb, indent, data, fmt, name, desc) \
244 SASBADDBASE(sb, indent, data, fmt, name, str, sizeof(data), \
247 #define SASBADDVARSTR(sb, indent, data, fmt, name, maxlen) \
248 SASBADDBASE(sb, indent, data, fmt, name, str, maxlen, NULL)
250 #define SASBADDVARSTRDESC(sb, indent, data, fmt, name, maxlen, desc) \
251 SASBADDBASE(sb, indent, data, fmt, name, str, maxlen, desc)
253 #define SASBADDNODE(sb, indent, name) { \
254 sbuf_printf(sb, "%*s<%s type=\"%s\">\n", indent, "", #name, \
259 #define SASBADDNODENUM(sb, indent, name, num) { \
260 sbuf_printf(sb, "%*s<%s type=\"%s\" num=\"%d\">\n", indent, "", \
261 #name, "node", num); \
265 #define SASBENDNODE(sb, indent, name) { \
267 sbuf_printf(sb, "%*s</%s>\n", indent, "", #name); \
270 #define SA_DENSITY_TYPES 4
272 struct sa_prot_state {
274 uint32_t prot_method;
281 struct sa_prot_info {
282 struct sa_prot_state cur_prot_state;
283 struct sa_prot_state pending_prot_state;
287 * A table mapping protection parameters to their types and values.
291 mt_param_set_type param_type;
296 } sa_prot_table[] = {
297 { "prot_method", MT_PARAM_SET_UNSIGNED,
298 __offsetof(struct sa_prot_state, prot_method),
299 /*min_val*/ 0, /*max_val*/ 255, NULL },
300 { "pi_length", MT_PARAM_SET_UNSIGNED,
301 __offsetof(struct sa_prot_state, pi_length),
302 /*min_val*/ 0, /*max_val*/ SA_CTRL_DP_PI_LENGTH_MASK, NULL },
303 { "lbp_w", MT_PARAM_SET_UNSIGNED,
304 __offsetof(struct sa_prot_state, lbp_w),
305 /*min_val*/ 0, /*max_val*/ 1, NULL },
306 { "lbp_r", MT_PARAM_SET_UNSIGNED,
307 __offsetof(struct sa_prot_state, lbp_r),
308 /*min_val*/ 0, /*max_val*/ 1, NULL },
309 { "rbdp", MT_PARAM_SET_UNSIGNED,
310 __offsetof(struct sa_prot_state, rbdp),
311 /*min_val*/ 0, /*max_val*/ 1, NULL }
314 #define SA_NUM_PROT_ENTS nitems(sa_prot_table)
316 #define SA_PROT_ENABLED(softc) ((softc->flags & SA_FLAG_PROTECT_SUPP) \
317 && (softc->prot_info.cur_prot_state.initialized != 0) \
318 && (softc->prot_info.cur_prot_state.prot_method != 0))
320 #define SA_PROT_LEN(softc) softc->prot_info.cur_prot_state.pi_length
327 struct cam_periph *periph;
328 struct bio_queue_head bio_queue;
330 struct devstat *device_stats;
333 int num_devs_to_destroy;
344 u_int32_t comp_algorithm;
345 u_int32_t saved_comp_algorithm;
346 u_int32_t media_blksize;
347 u_int32_t last_media_blksize;
348 u_int32_t media_numblks;
349 u_int8_t media_density;
352 u_int8_t dsreg; /* mtio mt_dsreg, redux */
356 int last_resid_was_io;
357 uint8_t density_type_bits[SA_DENSITY_TYPES];
358 int density_info_valid[SA_DENSITY_TYPES];
359 uint8_t density_info[SA_DENSITY_TYPES][SRDS_MAX_LENGTH];
361 struct sa_prot_info prot_info;
367 * Current position information. -1 means that the given value is
368 * unknown. fileno and blkno are always calculated. blkno is
369 * relative to the previous file mark. rep_fileno and rep_blkno
370 * are as reported by the drive, if it supports the long form
371 * report for the READ POSITION command. rep_blkno is relative to
372 * the beginning of the partition.
374 * bop means that the drive is at the beginning of the partition.
375 * eop means that the drive is between early warning and end of
376 * partition, inside the current partition.
377 * bpew means that the position is in a PEWZ (Programmable Early
380 daddr_t partition; /* Absolute from BOT */
381 daddr_t fileno; /* Relative to beginning of partition */
382 daddr_t blkno; /* Relative to last file mark */
383 daddr_t rep_blkno; /* Relative to beginning of partition */
384 daddr_t rep_fileno; /* Relative to beginning of partition */
385 int bop; /* Beginning of Partition */
386 int eop; /* End of Partition */
387 int bpew; /* Beyond Programmable Early Warning */
393 struct scsi_sense_data _last_io_sense;
394 u_int64_t _last_io_resid;
395 u_int8_t _last_io_cdb[CAM_MAX_CDBLEN];
396 struct scsi_sense_data _last_ctl_sense;
397 u_int64_t _last_ctl_resid;
398 u_int8_t _last_ctl_cdb[CAM_MAX_CDBLEN];
399 #define last_io_sense errinfo._last_io_sense
400 #define last_io_resid errinfo._last_io_resid
401 #define last_io_cdb errinfo._last_io_cdb
402 #define last_ctl_sense errinfo._last_ctl_sense
403 #define last_ctl_resid errinfo._last_ctl_resid
404 #define last_ctl_cdb errinfo._last_ctl_cdb
407 * Misc other flags/state
411 open_rdonly : 1, /* open read-only */
412 open_pending_mount : 1, /* open pending mount */
413 ctrl_mode : 1; /* control device open */
415 struct task sysctl_task;
416 struct sysctl_ctx_list sysctl_ctx;
417 struct sysctl_oid *sysctl_tree;
420 struct sa_quirk_entry {
421 struct scsi_inquiry_pattern inq_pat; /* matching pattern */
422 sa_quirks quirks; /* specific quirk type */
423 u_int32_t prefblk; /* preferred blocksize when in fixed mode */
426 static struct sa_quirk_entry sa_quirk_table[] =
429 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "OnStream",
430 "ADR*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_NODREAD |
431 SA_QUIRK_1FM|SA_QUIRK_NO_MODESEL, 32768
434 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
435 "Python 06408*", "*"}, SA_QUIRK_NODREAD, 0
438 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
439 "Python 25601*", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_NODREAD, 0
442 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
443 "Python*", "*"}, SA_QUIRK_NODREAD, 0
446 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
447 "VIPER 150*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
450 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
451 "VIPER 2525 25462", "-011"},
452 SA_QUIRK_NOCOMP|SA_QUIRK_1FM|SA_QUIRK_NODREAD, 0
455 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "ARCHIVE",
456 "VIPER 2525*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
460 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
461 "C15*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_NO_CPAGE, 0,
465 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
466 "C56*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
469 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
470 "T20*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
473 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
474 "T4000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
477 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "HP",
478 "HP-88780*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
481 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
482 "*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
485 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "M4 DATA",
486 "123107 SCSI*", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
488 { /* jreynold@primenet.com */
489 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
490 "STT8000N*", "*"}, SA_QUIRK_1FM, 0
492 { /* mike@sentex.net */
493 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "Seagate",
494 "STT20000*", "*"}, SA_QUIRK_1FM, 0
497 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "SEAGATE",
498 "DAT 06241-XXX", "*"}, SA_QUIRK_VARIABLE|SA_QUIRK_2FM, 0
501 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
502 " TDC 3600", "U07:"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
505 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
506 " TDC 3800", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
509 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
510 " TDC 4100", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
513 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
514 " TDC 4200", "*"}, SA_QUIRK_NOCOMP|SA_QUIRK_1FM, 512
517 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
518 " SLR*", "*"}, SA_QUIRK_1FM, 0
521 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
522 "5525ES*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 512
525 { T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "WANGTEK",
526 "51000*", "*"}, SA_QUIRK_FIXED|SA_QUIRK_1FM, 1024
530 static d_open_t saopen;
531 static d_close_t saclose;
532 static d_strategy_t sastrategy;
533 static d_ioctl_t saioctl;
534 static periph_init_t sainit;
535 static periph_ctor_t saregister;
536 static periph_oninv_t saoninvalidate;
537 static periph_dtor_t sacleanup;
538 static periph_start_t sastart;
539 static void saasync(void *callback_arg, u_int32_t code,
540 struct cam_path *path, void *arg);
541 static void sadone(struct cam_periph *periph,
542 union ccb *start_ccb);
543 static int saerror(union ccb *ccb, u_int32_t cam_flags,
544 u_int32_t sense_flags);
545 static int samarkswanted(struct cam_periph *);
546 static int sacheckeod(struct cam_periph *periph);
547 static int sagetparams(struct cam_periph *periph,
548 sa_params params_to_get,
549 u_int32_t *blocksize, u_int8_t *density,
550 u_int32_t *numblocks, int *buff_mode,
551 u_int8_t *write_protect, u_int8_t *speed,
552 int *comp_supported, int *comp_enabled,
553 u_int32_t *comp_algorithm,
554 sa_comp_t *comp_page,
555 struct scsi_control_data_prot_subpage
556 *prot_page, int dp_size,
557 int prot_changeable);
558 static int sasetprot(struct cam_periph *periph,
559 struct sa_prot_state *new_prot);
560 static int sasetparams(struct cam_periph *periph,
561 sa_params params_to_set,
562 u_int32_t blocksize, u_int8_t density,
563 u_int32_t comp_algorithm,
564 u_int32_t sense_flags);
565 static int sasetsili(struct cam_periph *periph,
566 struct mtparamset *ps, int num_params);
567 static int saseteotwarn(struct cam_periph *periph,
568 struct mtparamset *ps, int num_params);
569 static void safillprot(struct sa_softc *softc, int *indent,
571 static void sapopulateprots(struct sa_prot_state *cur_state,
572 struct sa_prot_map *new_table,
574 static struct sa_prot_map *safindprotent(char *name, struct sa_prot_map *table,
576 static int sasetprotents(struct cam_periph *periph,
577 struct mtparamset *ps, int num_params);
578 static struct sa_param_ent *safindparament(struct mtparamset *ps);
579 static int saparamsetlist(struct cam_periph *periph,
580 struct mtsetlist *list, int need_copy);
581 static int saextget(struct cdev *dev, struct cam_periph *periph,
582 struct sbuf *sb, struct mtextget *g);
583 static int saparamget(struct sa_softc *softc, struct sbuf *sb);
584 static void saprevent(struct cam_periph *periph, int action);
585 static int sarewind(struct cam_periph *periph);
586 static int saspace(struct cam_periph *periph, int count,
587 scsi_space_code code);
588 static void sadevgonecb(void *arg);
589 static void sasetupdev(struct sa_softc *softc, struct cdev *dev);
590 static int samount(struct cam_periph *, int, struct cdev *);
591 static int saretension(struct cam_periph *periph);
592 static int sareservereleaseunit(struct cam_periph *periph,
594 static int saloadunload(struct cam_periph *periph, int load);
595 static int saerase(struct cam_periph *periph, int longerase);
596 static int sawritefilemarks(struct cam_periph *periph,
597 int nmarks, int setmarks, int immed);
598 static int sagetpos(struct cam_periph *periph);
599 static int sardpos(struct cam_periph *periph, int, u_int32_t *);
600 static int sasetpos(struct cam_periph *periph, int,
602 static void safilldenstypesb(struct sbuf *sb, int *indent,
603 uint8_t *buf, int buf_len,
605 static void safilldensitysb(struct sa_softc *softc, int *indent,
609 #ifndef SA_DEFAULT_IO_SPLIT
610 #define SA_DEFAULT_IO_SPLIT 0
613 static int sa_allow_io_split = SA_DEFAULT_IO_SPLIT;
616 * Tunable to allow the user to set a global allow_io_split value. Note
617 * that this WILL GO AWAY in FreeBSD 11.0. Silently splitting the I/O up
618 * is bad behavior, because it hides the true tape block size from the
621 static SYSCTL_NODE(_kern_cam, OID_AUTO, sa, CTLFLAG_RD, 0,
622 "CAM Sequential Access Tape Driver");
623 SYSCTL_INT(_kern_cam_sa, OID_AUTO, allow_io_split, CTLFLAG_RDTUN,
624 &sa_allow_io_split, 0, "Default I/O split value");
626 static struct periph_driver sadriver =
629 TAILQ_HEAD_INITIALIZER(sadriver.units), /* generation */ 0
632 PERIPHDRIVER_DECLARE(sa, sadriver);
634 /* For 2.2-stable support */
640 static struct cdevsw sa_cdevsw = {
641 .d_version = D_VERSION,
645 .d_write = physwrite,
647 .d_strategy = sastrategy,
649 .d_flags = D_TAPE | D_TRACKCLOSE,
653 saopen(struct cdev *dev, int flags, int fmt, struct thread *td)
655 struct cam_periph *periph;
656 struct sa_softc *softc;
659 periph = (struct cam_periph *)dev->si_drv1;
660 if (cam_periph_acquire(periph) != 0) {
664 cam_periph_lock(periph);
666 softc = (struct sa_softc *)periph->softc;
668 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
669 ("saopen(%s): softc=0x%x\n", devtoname(dev), softc->flags));
671 if (SA_IS_CTRL(dev)) {
672 softc->ctrl_mode = 1;
674 cam_periph_unlock(periph);
678 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
679 cam_periph_unlock(periph);
680 cam_periph_release(periph);
684 if (softc->flags & SA_FLAG_OPEN) {
686 } else if (softc->flags & SA_FLAG_INVALID) {
690 * Preserve whether this is a read_only open.
692 softc->open_rdonly = (flags & O_RDWR) == O_RDONLY;
695 * The function samount ensures media is loaded and ready.
696 * It also does a device RESERVE if the tape isn't yet mounted.
698 * If the mount fails and this was a non-blocking open,
699 * make this a 'open_pending_mount' action.
701 error = samount(periph, flags, dev);
702 if (error && (flags & O_NONBLOCK)) {
703 softc->flags |= SA_FLAG_OPEN;
704 softc->open_pending_mount = 1;
706 cam_periph_unhold(periph);
707 cam_periph_unlock(periph);
713 cam_periph_unhold(periph);
714 cam_periph_unlock(periph);
715 cam_periph_release(periph);
719 saprevent(periph, PR_PREVENT);
720 softc->flags |= SA_FLAG_OPEN;
723 cam_periph_unhold(periph);
724 cam_periph_unlock(periph);
729 saclose(struct cdev *dev, int flag, int fmt, struct thread *td)
731 struct cam_periph *periph;
732 struct sa_softc *softc;
733 int mode, error, writing, tmp, i;
734 int closedbits = SA_FLAG_OPEN;
737 periph = (struct cam_periph *)dev->si_drv1;
738 cam_periph_lock(periph);
740 softc = (struct sa_softc *)periph->softc;
742 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE|CAM_DEBUG_INFO,
743 ("saclose(%s): softc=0x%x\n", devtoname(dev), softc->flags));
746 softc->open_rdonly = 0;
747 if (SA_IS_CTRL(dev)) {
748 softc->ctrl_mode = 0;
750 cam_periph_unlock(periph);
751 cam_periph_release(periph);
755 if (softc->open_pending_mount) {
756 softc->flags &= ~SA_FLAG_OPEN;
757 softc->open_pending_mount = 0;
759 cam_periph_unlock(periph);
760 cam_periph_release(periph);
764 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
765 cam_periph_unlock(periph);
770 * Were we writing the tape?
772 writing = (softc->flags & SA_FLAG_TAPE_WRITTEN) != 0;
775 * See whether or not we need to write filemarks. If this
776 * fails, we probably have to assume we've lost tape
779 error = sacheckeod(periph);
781 xpt_print(periph->path,
782 "failed to write terminating filemark(s)\n");
783 softc->flags |= SA_FLAG_TAPE_FROZEN;
787 * Whatever we end up doing, allow users to eject tapes from here on.
789 saprevent(periph, PR_ALLOW);
792 * Decide how to end...
794 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
795 closedbits |= SA_FLAG_TAPE_FROZEN;
796 } else switch (mode) {
797 case SA_MODE_OFFLINE:
799 * An 'offline' close is an unconditional release of
800 * frozen && mount conditions, irrespective of whether
801 * these operations succeeded. The reason for this is
802 * to allow at least some kind of programmatic way
803 * around our state getting all fouled up. If somebody
804 * issues an 'offline' command, that will be allowed
807 (void) sarewind(periph);
808 (void) saloadunload(periph, FALSE);
809 closedbits |= SA_FLAG_TAPE_MOUNTED|SA_FLAG_TAPE_FROZEN;
813 * If the rewind fails, return an error- if anyone cares,
814 * but not overwriting any previous error.
816 * We don't clear the notion of mounted here, but we do
817 * clear the notion of frozen if we successfully rewound.
819 tmp = sarewind(periph);
824 closedbits |= SA_FLAG_TAPE_FROZEN;
827 case SA_MODE_NOREWIND:
829 * If we're not rewinding/unloading the tape, find out
830 * whether we need to back up over one of two filemarks
831 * we wrote (if we wrote two filemarks) so that appends
832 * from this point on will be sane.
834 if (error == 0 && writing && (softc->quirks & SA_QUIRK_2FM)) {
835 tmp = saspace(periph, -1, SS_FILEMARKS);
837 xpt_print(periph->path, "unable to backspace "
838 "over one of double filemarks at end of "
840 xpt_print(periph->path, "it is possible that "
841 "this device needs a SA_QUIRK_1FM quirk set"
843 softc->flags |= SA_FLAG_TAPE_FROZEN;
848 xpt_print(periph->path, "unknown mode 0x%x in saclose\n", mode);
854 * We wish to note here that there are no more filemarks to be written.
856 softc->filemarks = 0;
857 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
860 * And we are no longer open for business.
862 softc->flags &= ~closedbits;
866 * Invalidate any density information that depends on having tape
867 * media in the drive.
869 for (i = 0; i < SA_DENSITY_TYPES; i++) {
870 if (softc->density_type_bits[i] & SRDS_MEDIA)
871 softc->density_info_valid[i] = 0;
875 * Inform users if tape state if frozen....
877 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
878 xpt_print(periph->path, "tape is now frozen- use an OFFLINE, "
879 "REWIND or MTEOM command to clear this state.\n");
882 /* release the device if it is no longer mounted */
883 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0)
884 sareservereleaseunit(periph, FALSE);
886 cam_periph_unhold(periph);
887 cam_periph_unlock(periph);
888 cam_periph_release(periph);
894 * Actually translate the requested transfer into one the physical driver
895 * can understand. The transfer is described by a buf and will include
896 * only one physical transfer.
899 sastrategy(struct bio *bp)
901 struct cam_periph *periph;
902 struct sa_softc *softc;
904 bp->bio_resid = bp->bio_bcount;
905 if (SA_IS_CTRL(bp->bio_dev)) {
906 biofinish(bp, NULL, EINVAL);
909 periph = (struct cam_periph *)bp->bio_dev->si_drv1;
910 cam_periph_lock(periph);
912 softc = (struct sa_softc *)periph->softc;
914 if (softc->flags & SA_FLAG_INVALID) {
915 cam_periph_unlock(periph);
916 biofinish(bp, NULL, ENXIO);
920 if (softc->flags & SA_FLAG_TAPE_FROZEN) {
921 cam_periph_unlock(periph);
922 biofinish(bp, NULL, EPERM);
927 * This should actually never occur as the write(2)
928 * system call traps attempts to write to a read-only
931 if (bp->bio_cmd == BIO_WRITE && softc->open_rdonly) {
932 cam_periph_unlock(periph);
933 biofinish(bp, NULL, EBADF);
937 if (softc->open_pending_mount) {
938 int error = samount(periph, 0, bp->bio_dev);
940 cam_periph_unlock(periph);
941 biofinish(bp, NULL, ENXIO);
944 saprevent(periph, PR_PREVENT);
945 softc->open_pending_mount = 0;
950 * If it's a null transfer, return immediately
952 if (bp->bio_bcount == 0) {
953 cam_periph_unlock(periph);
959 if (softc->flags & SA_FLAG_FIXED) {
961 * Fixed block device. The byte count must
962 * be a multiple of our block size.
964 if (((softc->blk_mask != ~0) &&
965 ((bp->bio_bcount & softc->blk_mask) != 0)) ||
966 ((softc->blk_mask == ~0) &&
967 ((bp->bio_bcount % softc->min_blk) != 0))) {
968 xpt_print(periph->path, "Invalid request. Fixed block "
969 "device requests must be a multiple of %d bytes\n",
971 cam_periph_unlock(periph);
972 biofinish(bp, NULL, EINVAL);
975 } else if ((bp->bio_bcount > softc->max_blk) ||
976 (bp->bio_bcount < softc->min_blk) ||
977 (bp->bio_bcount & softc->blk_mask) != 0) {
979 xpt_print_path(periph->path);
980 printf("Invalid request. Variable block "
981 "device requests must be ");
982 if (softc->blk_mask != 0) {
983 printf("a multiple of %d ", (0x1 << softc->blk_gran));
985 printf("between %d and %d bytes\n", softc->min_blk,
987 cam_periph_unlock(periph);
988 biofinish(bp, NULL, EINVAL);
993 * Place it at the end of the queue.
995 bioq_insert_tail(&softc->bio_queue, bp);
996 softc->queue_count++;
998 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
999 ("sastrategy: queuing a %ld %s byte %s\n", bp->bio_bcount,
1000 (softc->flags & SA_FLAG_FIXED)? "fixed" : "variable",
1001 (bp->bio_cmd == BIO_READ)? "read" : "write"));
1003 if (softc->queue_count > 1) {
1004 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
1005 ("sastrategy: queue count now %d\n", softc->queue_count));
1009 * Schedule ourselves for performing the work.
1011 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1012 cam_periph_unlock(periph);
1018 sasetsili(struct cam_periph *periph, struct mtparamset *ps, int num_params)
1020 uint32_t sili_blocksize;
1021 struct sa_softc *softc;
1025 softc = (struct sa_softc *)periph->softc;
1027 if (ps->value_type != MT_PARAM_SET_SIGNED) {
1028 snprintf(ps->error_str, sizeof(ps->error_str),
1029 "sili is a signed parameter");
1032 if ((ps->value.value_signed < 0)
1033 || (ps->value.value_signed > 1)) {
1034 snprintf(ps->error_str, sizeof(ps->error_str),
1035 "invalid sili value %jd", (intmax_t)ps->value.value_signed);
1039 * We only set the SILI flag in variable block
1040 * mode. You'll get a check condition in fixed
1041 * block mode if things don't line up in any case.
1043 if (softc->flags & SA_FLAG_FIXED) {
1044 snprintf(ps->error_str, sizeof(ps->error_str),
1045 "can't set sili bit in fixed block mode");
1048 if (softc->sili == ps->value.value_signed)
1051 if (ps->value.value_signed == 1)
1056 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
1057 sili_blocksize, 0, 0, SF_QUIET_IR);
1059 snprintf(ps->error_str, sizeof(ps->error_str),
1060 "sasetparams() returned error %d", error);
1064 softc->sili = ps->value.value_signed;
1067 ps->status = MT_PARAM_STATUS_OK;
1071 ps->status = MT_PARAM_STATUS_ERROR;
1079 saseteotwarn(struct cam_periph *periph, struct mtparamset *ps, int num_params)
1081 struct sa_softc *softc;
1085 softc = (struct sa_softc *)periph->softc;
1087 if (ps->value_type != MT_PARAM_SET_SIGNED) {
1088 snprintf(ps->error_str, sizeof(ps->error_str),
1089 "eot_warn is a signed parameter");
1090 ps->status = MT_PARAM_STATUS_ERROR;
1093 if ((ps->value.value_signed < 0)
1094 || (ps->value.value_signed > 1)) {
1095 snprintf(ps->error_str, sizeof(ps->error_str),
1096 "invalid eot_warn value %jd\n",
1097 (intmax_t)ps->value.value_signed);
1098 ps->status = MT_PARAM_STATUS_ERROR;
1101 softc->eot_warn = ps->value.value_signed;
1102 ps->status = MT_PARAM_STATUS_OK;
1104 if (ps->status != MT_PARAM_STATUS_OK)
1112 safillprot(struct sa_softc *softc, int *indent, struct sbuf *sb)
1116 SASBADDNODE(sb, *indent, protection);
1117 if (softc->flags & SA_FLAG_PROTECT_SUPP)
1121 SASBADDINTDESC(sb, *indent, tmpint, %d, protection_supported,
1122 "Set to 1 if protection information is supported");
1125 && (softc->prot_info.cur_prot_state.initialized != 0)) {
1126 struct sa_prot_state *prot;
1128 prot = &softc->prot_info.cur_prot_state;
1130 SASBADDUINTDESC(sb, *indent, prot->prot_method, %u,
1131 prot_method, "Current Protection Method");
1132 SASBADDUINTDESC(sb, *indent, prot->pi_length, %u,
1133 pi_length, "Length of Protection Information");
1134 SASBADDUINTDESC(sb, *indent, prot->lbp_w, %u,
1135 lbp_w, "Check Protection on Writes");
1136 SASBADDUINTDESC(sb, *indent, prot->lbp_r, %u,
1137 lbp_r, "Check and Include Protection on Reads");
1138 SASBADDUINTDESC(sb, *indent, prot->rbdp, %u,
1139 rbdp, "Transfer Protection Information for RECOVER "
1140 "BUFFERED DATA command");
1142 SASBENDNODE(sb, *indent, protection);
1146 sapopulateprots(struct sa_prot_state *cur_state, struct sa_prot_map *new_table,
1151 bcopy(sa_prot_table, new_table, min(table_ents * sizeof(*new_table),
1152 sizeof(sa_prot_table)));
1154 table_ents = min(table_ents, SA_NUM_PROT_ENTS);
1156 for (i = 0; i < table_ents; i++)
1157 new_table[i].value = (uint32_t *)((uint8_t *)cur_state +
1158 new_table[i].offset);
1163 static struct sa_prot_map *
1164 safindprotent(char *name, struct sa_prot_map *table, int table_ents)
1166 char *prot_name = "protection.";
1169 prot_len = strlen(prot_name);
1172 * This shouldn't happen, but we check just in case.
1174 if (strncmp(name, prot_name, prot_len) != 0)
1177 for (i = 0; i < table_ents; i++) {
1178 if (strcmp(&name[prot_len], table[i].name) != 0)
1187 sasetprotents(struct cam_periph *periph, struct mtparamset *ps, int num_params)
1189 struct sa_softc *softc;
1190 struct sa_prot_map prot_ents[SA_NUM_PROT_ENTS];
1191 struct sa_prot_state new_state;
1195 softc = (struct sa_softc *)periph->softc;
1199 * Make sure that this tape drive supports protection information.
1200 * Otherwise we can't set anything.
1202 if ((softc->flags & SA_FLAG_PROTECT_SUPP) == 0) {
1203 snprintf(ps[0].error_str, sizeof(ps[0].error_str),
1204 "Protection information is not supported for this device");
1205 ps[0].status = MT_PARAM_STATUS_ERROR;
1210 * We can't operate with physio(9) splitting enabled, because there
1211 * is no way to insure (especially in variable block mode) that
1212 * what the user writes (with a checksum block at the end) will
1213 * make it into the sa(4) driver intact.
1215 if ((softc->si_flags & SI_NOSPLIT) == 0) {
1216 snprintf(ps[0].error_str, sizeof(ps[0].error_str),
1217 "Protection information cannot be enabled with I/O "
1219 ps[0].status = MT_PARAM_STATUS_ERROR;
1224 * Take the current cached protection state and use that as the
1225 * basis for our new entries.
1227 bcopy(&softc->prot_info.cur_prot_state, &new_state, sizeof(new_state));
1230 * Populate the table mapping property names to pointers into the
1233 sapopulateprots(&new_state, prot_ents, SA_NUM_PROT_ENTS);
1236 * For each parameter the user passed in, make sure the name, type
1237 * and value are valid.
1239 for (i = 0; i < num_params; i++) {
1240 struct sa_prot_map *ent;
1242 ent = safindprotent(ps[i].value_name, prot_ents,
1245 ps[i].status = MT_PARAM_STATUS_ERROR;
1246 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1247 "Invalid protection entry name %s",
1252 if (ent->param_type != ps[i].value_type) {
1253 ps[i].status = MT_PARAM_STATUS_ERROR;
1254 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1255 "Supplied type %d does not match actual type %d",
1256 ps[i].value_type, ent->param_type);
1260 if ((ps[i].value.value_unsigned < ent->min_val)
1261 || (ps[i].value.value_unsigned > ent->max_val)) {
1262 ps[i].status = MT_PARAM_STATUS_ERROR;
1263 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1264 "Value %ju is outside valid range %u - %u",
1265 (uintmax_t)ps[i].value.value_unsigned, ent->min_val,
1270 *(ent->value) = ps[i].value.value_unsigned;
1274 * Actually send the protection settings to the drive.
1276 error = sasetprot(periph, &new_state);
1278 for (i = 0; i < num_params; i++) {
1279 ps[i].status = MT_PARAM_STATUS_ERROR;
1280 snprintf(ps[i].error_str, sizeof(ps[i].error_str),
1281 "Unable to set parameter, see dmesg(8)");
1287 * Let the user know that his settings were stored successfully.
1289 for (i = 0; i < num_params; i++)
1290 ps[i].status = MT_PARAM_STATUS_OK;
1296 * Entry handlers generally only handle a single entry. Node handlers will
1297 * handle a contiguous range of parameters to set in a single call.
1300 SA_PARAM_TYPE_ENTRY,
1304 struct sa_param_ent {
1306 sa_param_type param_type;
1307 int (*set_func)(struct cam_periph *periph, struct mtparamset *ps,
1309 } sa_param_table[] = {
1310 {"sili", SA_PARAM_TYPE_ENTRY, sasetsili },
1311 {"eot_warn", SA_PARAM_TYPE_ENTRY, saseteotwarn },
1312 {"protection.", SA_PARAM_TYPE_NODE, sasetprotents }
1315 static struct sa_param_ent *
1316 safindparament(struct mtparamset *ps)
1320 for (i = 0; i < nitems(sa_param_table); i++){
1322 * For entries, we compare all of the characters. For
1323 * nodes, we only compare the first N characters. The node
1324 * handler will decode the rest.
1326 if (sa_param_table[i].param_type == SA_PARAM_TYPE_ENTRY) {
1327 if (strcmp(ps->value_name, sa_param_table[i].name) != 0)
1330 if (strncmp(ps->value_name, sa_param_table[i].name,
1331 strlen(sa_param_table[i].name)) != 0)
1334 return (&sa_param_table[i]);
1341 * Go through a list of parameters, coalescing contiguous parameters with
1342 * the same parent node into a single call to a set_func.
1345 saparamsetlist(struct cam_periph *periph, struct mtsetlist *list,
1350 struct mtparamset *params, *first;
1351 struct sa_param_ent *first_ent;
1356 if (list->num_params == 0)
1361 * Verify that the user has the correct structure size.
1363 if ((list->num_params * sizeof(struct mtparamset)) !=
1365 xpt_print(periph->path, "%s: length of params %d != "
1366 "sizeof(struct mtparamset) %zd * num_params %d\n",
1367 __func__, list->param_len, sizeof(struct mtparamset),
1373 if (need_copy != 0) {
1375 * XXX KDM will dropping the lock cause an issue here?
1377 cam_periph_unlock(periph);
1378 params = malloc(list->param_len, M_SCSISA, M_WAITOK | M_ZERO);
1379 error = copyin(list->params, params, list->param_len);
1380 cam_periph_lock(periph);
1385 params = list->params;
1391 for (i = 0; i < list->num_params; i++) {
1392 struct sa_param_ent *ent;
1394 ent = safindparament(¶ms[i]);
1396 snprintf(params[i].error_str,
1397 sizeof(params[i].error_str),
1398 "%s: cannot find parameter %s", __func__,
1399 params[i].value_name);
1400 params[i].status = MT_PARAM_STATUS_ERROR;
1404 if (first != NULL) {
1405 if (first_ent == ent) {
1407 * We're still in a contiguous list of
1408 * parameters that can be handled by one
1414 error = first_ent->set_func(periph, first,
1425 if (ent->param_type == SA_PARAM_TYPE_NODE) {
1430 error = ent->set_func(periph, ¶ms[i], 1);
1438 first_ent->set_func(periph, first, contig_ents);
1441 if (need_copy != 0) {
1442 if (error != EFAULT) {
1443 cam_periph_unlock(periph);
1444 copyout(params, list->params, list->param_len);
1445 cam_periph_lock(periph);
1447 free(params, M_SCSISA);
1453 sagetparams_common(struct cdev *dev, struct cam_periph *periph)
1455 struct sa_softc *softc;
1456 u_int8_t write_protect;
1457 int comp_enabled, comp_supported, error;
1459 softc = (struct sa_softc *)periph->softc;
1461 if (softc->open_pending_mount)
1464 /* The control device may issue getparams() if there are no opens. */
1465 if (SA_IS_CTRL(dev) && (softc->flags & SA_FLAG_OPEN) != 0)
1468 error = sagetparams(periph, SA_PARAM_ALL, &softc->media_blksize,
1469 &softc->media_density, &softc->media_numblks, &softc->buffer_mode,
1470 &write_protect, &softc->speed, &comp_supported, &comp_enabled,
1471 &softc->comp_algorithm, NULL, NULL, 0, 0);
1475 softc->flags |= SA_FLAG_TAPE_WP;
1477 softc->flags &= ~SA_FLAG_TAPE_WP;
1478 softc->flags &= ~SA_FLAG_COMPRESSION;
1479 if (comp_supported) {
1480 if (softc->saved_comp_algorithm == 0)
1481 softc->saved_comp_algorithm =
1482 softc->comp_algorithm;
1483 softc->flags |= SA_FLAG_COMP_SUPP;
1485 softc->flags |= SA_FLAG_COMP_ENABLED;
1487 softc->flags |= SA_FLAG_COMP_UNSUPP;
1492 #define PENDING_MOUNT_CHECK(softc, periph, dev) \
1493 if (softc->open_pending_mount) { \
1494 error = samount(periph, 0, dev); \
1498 saprevent(periph, PR_PREVENT); \
1499 softc->open_pending_mount = 0; \
1503 saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
1505 struct cam_periph *periph;
1506 struct sa_softc *softc;
1507 scsi_space_code spaceop;
1508 int didlockperiph = 0;
1513 error = 0; /* shut up gcc */
1514 spaceop = 0; /* shut up gcc */
1516 periph = (struct cam_periph *)dev->si_drv1;
1517 cam_periph_lock(periph);
1518 softc = (struct sa_softc *)periph->softc;
1521 * Check for control mode accesses. We allow MTIOCGET and
1522 * MTIOCERRSTAT (but need to be the only one open in order
1523 * to clear latched status), and MTSETBSIZE, MTSETDNSTY
1524 * and MTCOMP (but need to be the only one accessing this
1525 * device to run those).
1528 if (SA_IS_CTRL(dev)) {
1530 case MTIOCGETEOTMODEL:
1538 * If the periph isn't already locked, lock it
1539 * so our MTIOCERRSTAT can reset latched error stats.
1541 * If the periph is already locked, skip it because
1542 * we're just getting status and it'll be up to the
1543 * other thread that has this device open to do
1544 * an MTIOCERRSTAT that would clear latched status.
1546 if ((periph->flags & CAM_PERIPH_LOCKED) == 0) {
1547 error = cam_periph_hold(periph, PRIBIO|PCATCH);
1549 cam_periph_unlock(periph);
1558 struct mtop *mt = (struct mtop *) arg;
1561 * Check to make sure it's an OP we can perform
1562 * with no media inserted.
1564 switch (mt->mt_op) {
1578 case MTIOCSETEOTMODEL:
1580 * We need to acquire the peripheral here rather
1581 * than at open time because we are sharing writable
1582 * access to data structures.
1584 error = cam_periph_hold(periph, PRIBIO|PCATCH);
1586 cam_periph_unlock(periph);
1593 cam_periph_unlock(periph);
1599 * Find the device that the user is talking about
1604 struct mtget *g = (struct mtget *)arg;
1606 error = sagetparams_common(dev, periph);
1609 bzero(g, sizeof(struct mtget));
1610 g->mt_type = MT_ISAR;
1611 if (softc->flags & SA_FLAG_COMP_UNSUPP) {
1612 g->mt_comp = MT_COMP_UNSUPP;
1613 g->mt_comp0 = MT_COMP_UNSUPP;
1614 g->mt_comp1 = MT_COMP_UNSUPP;
1615 g->mt_comp2 = MT_COMP_UNSUPP;
1616 g->mt_comp3 = MT_COMP_UNSUPP;
1618 if ((softc->flags & SA_FLAG_COMP_ENABLED) == 0) {
1619 g->mt_comp = MT_COMP_DISABLED;
1621 g->mt_comp = softc->comp_algorithm;
1623 g->mt_comp0 = softc->comp_algorithm;
1624 g->mt_comp1 = softc->comp_algorithm;
1625 g->mt_comp2 = softc->comp_algorithm;
1626 g->mt_comp3 = softc->comp_algorithm;
1628 g->mt_density = softc->media_density;
1629 g->mt_density0 = softc->media_density;
1630 g->mt_density1 = softc->media_density;
1631 g->mt_density2 = softc->media_density;
1632 g->mt_density3 = softc->media_density;
1633 g->mt_blksiz = softc->media_blksize;
1634 g->mt_blksiz0 = softc->media_blksize;
1635 g->mt_blksiz1 = softc->media_blksize;
1636 g->mt_blksiz2 = softc->media_blksize;
1637 g->mt_blksiz3 = softc->media_blksize;
1638 g->mt_fileno = softc->fileno;
1639 g->mt_blkno = softc->blkno;
1640 g->mt_dsreg = (short) softc->dsreg;
1642 * Yes, we know that this is likely to overflow
1644 if (softc->last_resid_was_io) {
1645 if ((g->mt_resid = (short) softc->last_io_resid) != 0) {
1646 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1647 softc->last_io_resid = 0;
1651 if ((g->mt_resid = (short)softc->last_ctl_resid) != 0) {
1652 if (SA_IS_CTRL(dev) == 0 || didlockperiph) {
1653 softc->last_ctl_resid = 0;
1663 struct mtextget *g = (struct mtextget *)arg;
1668 * Report drive status using an XML format.
1672 * XXX KDM will dropping the lock cause any problems here?
1674 cam_periph_unlock(periph);
1675 sb = sbuf_new(NULL, NULL, g->alloc_len, SBUF_FIXEDLEN);
1677 g->status = MT_EXT_GET_ERROR;
1678 snprintf(g->error_str, sizeof(g->error_str),
1679 "Unable to allocate %d bytes for status info",
1681 cam_periph_lock(periph);
1682 goto extget_bailout;
1684 cam_periph_lock(periph);
1686 if (cmd == MTIOCEXTGET)
1687 error = saextget(dev, periph, sb, g);
1689 error = saparamget(softc, sb);
1692 goto extget_bailout;
1694 error = sbuf_finish(sb);
1695 if (error == ENOMEM) {
1696 g->status = MT_EXT_GET_NEED_MORE_SPACE;
1698 } else if (error != 0) {
1699 g->status = MT_EXT_GET_ERROR;
1700 snprintf(g->error_str, sizeof(g->error_str),
1701 "Error %d returned from sbuf_finish()", error);
1703 g->status = MT_EXT_GET_OK;
1706 tmpstr2 = sbuf_data(sb);
1707 g->fill_len = strlen(tmpstr2) + 1;
1708 cam_periph_unlock(periph);
1710 error = copyout(tmpstr2, g->status_xml, g->fill_len);
1712 cam_periph_lock(periph);
1720 struct mtsetlist list;
1721 struct mtparamset *ps = (struct mtparamset *)arg;
1723 bzero(&list, sizeof(list));
1724 list.num_params = 1;
1725 list.param_len = sizeof(*ps);
1728 error = saparamsetlist(periph, &list, /*need_copy*/ 0);
1733 struct mtsetlist *list = (struct mtsetlist *)arg;
1735 error = saparamsetlist(periph, list, /*need_copy*/ 1);
1740 struct scsi_tape_errors *sep =
1741 &((union mterrstat *)arg)->scsi_errstat;
1743 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1744 ("saioctl: MTIOCERRSTAT\n"));
1746 bzero(sep, sizeof(*sep));
1747 sep->io_resid = softc->last_io_resid;
1748 bcopy((caddr_t) &softc->last_io_sense, sep->io_sense,
1749 sizeof (sep->io_sense));
1750 bcopy((caddr_t) &softc->last_io_cdb, sep->io_cdb,
1751 sizeof (sep->io_cdb));
1752 sep->ctl_resid = softc->last_ctl_resid;
1753 bcopy((caddr_t) &softc->last_ctl_sense, sep->ctl_sense,
1754 sizeof (sep->ctl_sense));
1755 bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb,
1756 sizeof (sep->ctl_cdb));
1758 if ((SA_IS_CTRL(dev) == 0 && !softc->open_pending_mount) ||
1760 bzero((caddr_t) &softc->errinfo,
1761 sizeof (softc->errinfo));
1770 PENDING_MOUNT_CHECK(softc, periph, dev);
1772 mt = (struct mtop *)arg;
1775 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
1776 ("saioctl: op=0x%x count=0x%x\n",
1777 mt->mt_op, mt->mt_count));
1779 count = mt->mt_count;
1780 switch (mt->mt_op) {
1781 case MTWEOF: /* write an end-of-file marker */
1783 * We don't need to clear the SA_FLAG_TAPE_WRITTEN
1784 * flag because by keeping track of filemarks
1785 * we have last written we know whether or not
1786 * we need to write more when we close the device.
1788 error = sawritefilemarks(periph, count, FALSE, FALSE);
1791 /* write an end-of-file marker without waiting */
1792 error = sawritefilemarks(periph, count, FALSE, TRUE);
1794 case MTWSS: /* write a setmark */
1795 error = sawritefilemarks(periph, count, TRUE, FALSE);
1797 case MTBSR: /* backward space record */
1798 case MTFSR: /* forward space record */
1799 case MTBSF: /* backward space file */
1800 case MTFSF: /* forward space file */
1801 case MTBSS: /* backward space setmark */
1802 case MTFSS: /* forward space setmark */
1803 case MTEOD: /* space to end of recorded medium */
1807 spaceop = SS_FILEMARKS;
1808 nmarks = softc->filemarks;
1809 error = sacheckeod(periph);
1811 xpt_print(periph->path,
1812 "EOD check prior to spacing failed\n");
1813 softc->flags |= SA_FLAG_EIO_PENDING;
1816 nmarks -= softc->filemarks;
1822 spaceop = SS_BLOCKS;
1833 spaceop = SS_SETMARKS;
1847 nmarks = softc->filemarks;
1849 * XXX: Why are we checking again?
1851 error = sacheckeod(periph);
1854 nmarks -= softc->filemarks;
1855 error = saspace(periph, count - nmarks, spaceop);
1857 * At this point, clear that we've written the tape
1858 * and that we've written any filemarks. We really
1859 * don't know what the applications wishes to do next-
1860 * the sacheckeod's will make sure we terminated the
1861 * tape correctly if we'd been writing, but the next
1862 * action the user application takes will set again
1863 * whether we need to write filemarks.
1866 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1867 softc->filemarks = 0;
1870 case MTREW: /* rewind */
1871 PENDING_MOUNT_CHECK(softc, periph, dev);
1872 (void) sacheckeod(periph);
1873 error = sarewind(periph);
1876 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1877 softc->flags &= ~SA_FLAG_ERR_PENDING;
1878 softc->filemarks = 0;
1880 case MTERASE: /* erase */
1881 PENDING_MOUNT_CHECK(softc, periph, dev);
1882 error = saerase(periph, count);
1884 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1885 softc->flags &= ~SA_FLAG_ERR_PENDING;
1887 case MTRETENS: /* re-tension tape */
1888 PENDING_MOUNT_CHECK(softc, periph, dev);
1889 error = saretension(periph);
1891 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
1892 softc->flags &= ~SA_FLAG_ERR_PENDING;
1894 case MTOFFL: /* rewind and put the drive offline */
1896 PENDING_MOUNT_CHECK(softc, periph, dev);
1898 (void) sacheckeod(periph);
1900 softc->flags &= ~SA_FLAG_TAPE_WRITTEN;
1901 softc->filemarks = 0;
1903 error = sarewind(periph);
1904 /* clear the frozen flag anyway */
1905 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
1908 * Be sure to allow media removal before ejecting.
1911 saprevent(periph, PR_ALLOW);
1913 error = saloadunload(periph, FALSE);
1915 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
1921 error = saloadunload(periph, TRUE);
1923 case MTNOP: /* no operation, sets status only */
1924 case MTCACHE: /* enable controller cache */
1925 case MTNOCACHE: /* disable controller cache */
1929 case MTSETBSIZ: /* Set block size for device */
1931 PENDING_MOUNT_CHECK(softc, periph, dev);
1933 if ((softc->sili != 0)
1935 xpt_print(periph->path, "Can't enter fixed "
1936 "block mode with SILI enabled\n");
1940 error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count,
1943 softc->last_media_blksize =
1944 softc->media_blksize;
1945 softc->media_blksize = count;
1947 softc->flags |= SA_FLAG_FIXED;
1948 if (powerof2(count)) {
1951 softc->blk_mask = count - 1;
1953 softc->blk_mask = ~0;
1954 softc->blk_shift = 0;
1957 * Make the user's desire 'persistent'.
1959 softc->quirks &= ~SA_QUIRK_VARIABLE;
1960 softc->quirks |= SA_QUIRK_FIXED;
1962 softc->flags &= ~SA_FLAG_FIXED;
1963 if (softc->max_blk == 0) {
1964 softc->max_blk = ~0;
1966 softc->blk_shift = 0;
1967 if (softc->blk_gran != 0) {
1969 softc->blk_gran - 1;
1971 softc->blk_mask = 0;
1974 * Make the user's desire 'persistent'.
1976 softc->quirks |= SA_QUIRK_VARIABLE;
1977 softc->quirks &= ~SA_QUIRK_FIXED;
1981 case MTSETDNSTY: /* Set density for device and mode */
1982 PENDING_MOUNT_CHECK(softc, periph, dev);
1984 if (count > UCHAR_MAX) {
1988 error = sasetparams(periph, SA_PARAM_DENSITY,
1992 case MTCOMP: /* enable compression */
1993 PENDING_MOUNT_CHECK(softc, periph, dev);
1995 * Some devices don't support compression, and
1996 * don't like it if you ask them for the
1999 if ((softc->quirks & SA_QUIRK_NOCOMP) ||
2000 (softc->flags & SA_FLAG_COMP_UNSUPP)) {
2004 error = sasetparams(periph, SA_PARAM_COMPRESSION,
2005 0, 0, count, SF_NO_PRINT);
2017 PENDING_MOUNT_CHECK(softc, periph, dev);
2018 error = sardpos(periph, 0, (u_int32_t *) arg);
2021 PENDING_MOUNT_CHECK(softc, periph, dev);
2022 error = sardpos(periph, 1, (u_int32_t *) arg);
2025 case MTIOCHLOCATE: {
2026 struct mtlocate locate_info;
2029 bzero(&locate_info, sizeof(locate_info));
2030 locate_info.logical_id = *((uint32_t *)arg);
2031 if (cmd == MTIOCSLOCATE)
2036 PENDING_MOUNT_CHECK(softc, periph, dev);
2038 error = sasetpos(periph, hard, &locate_info);
2041 case MTIOCEXTLOCATE:
2042 PENDING_MOUNT_CHECK(softc, periph, dev);
2043 error = sasetpos(periph, /*hard*/ 0, (struct mtlocate *)arg);
2045 ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN);
2046 softc->flags &= ~SA_FLAG_ERR_PENDING;
2047 softc->filemarks = 0;
2049 case MTIOCGETEOTMODEL:
2051 if (softc->quirks & SA_QUIRK_1FM)
2055 *((u_int32_t *) arg) = mode;
2057 case MTIOCSETEOTMODEL:
2059 switch (*((u_int32_t *) arg)) {
2061 softc->quirks &= ~SA_QUIRK_2FM;
2062 softc->quirks |= SA_QUIRK_1FM;
2065 softc->quirks &= ~SA_QUIRK_1FM;
2066 softc->quirks |= SA_QUIRK_2FM;
2074 struct mtrblim *rblim;
2076 rblim = (struct mtrblim *)arg;
2078 rblim->granularity = softc->blk_gran;
2079 rblim->min_block_length = softc->min_blk;
2080 rblim->max_block_length = softc->max_blk;
2084 error = cam_periph_ioctl(periph, cmd, arg, saerror);
2089 * Check to see if we cleared a frozen state
2091 if (error == 0 && (softc->flags & SA_FLAG_TAPE_FROZEN)) {
2098 * XXX KDM look at this.
2100 softc->fileno = (daddr_t) -1;
2101 softc->blkno = (daddr_t) -1;
2102 softc->rep_blkno = (daddr_t) -1;
2103 softc->rep_fileno = (daddr_t) -1;
2104 softc->partition = (daddr_t) -1;
2105 softc->flags &= ~SA_FLAG_TAPE_FROZEN;
2106 xpt_print(periph->path,
2107 "tape state now unfrozen.\n");
2113 if (didlockperiph) {
2114 cam_periph_unhold(periph);
2116 cam_periph_unlock(periph);
2126 * Install a global async callback.
2128 status = xpt_register_async(AC_FOUND_DEVICE, saasync, NULL, NULL);
2130 if (status != CAM_REQ_CMP) {
2131 printf("sa: Failed to attach master async callback "
2132 "due to status 0x%x!\n", status);
2137 sadevgonecb(void *arg)
2139 struct cam_periph *periph;
2141 struct sa_softc *softc;
2143 periph = (struct cam_periph *)arg;
2144 softc = (struct sa_softc *)periph->softc;
2146 mtx = cam_periph_mtx(periph);
2149 softc->num_devs_to_destroy--;
2150 if (softc->num_devs_to_destroy == 0) {
2154 * When we have gotten all of our callbacks, we will get
2155 * no more close calls from devfs. So if we have any
2156 * dangling opens, we need to release the reference held
2157 * for that particular context.
2159 for (i = 0; i < softc->open_count; i++)
2160 cam_periph_release_locked(periph);
2162 softc->open_count = 0;
2165 * Release the reference held for devfs, all of our
2166 * instances are gone now.
2168 cam_periph_release_locked(periph);
2172 * We reference the lock directly here, instead of using
2173 * cam_periph_unlock(). The reason is that the final call to
2174 * cam_periph_release_locked() above could result in the periph
2175 * getting freed. If that is the case, dereferencing the periph
2176 * with a cam_periph_unlock() call would cause a page fault.
2182 saoninvalidate(struct cam_periph *periph)
2184 struct sa_softc *softc;
2186 softc = (struct sa_softc *)periph->softc;
2189 * De-register any async callbacks.
2191 xpt_register_async(0, saasync, periph, periph->path);
2193 softc->flags |= SA_FLAG_INVALID;
2196 * Return all queued I/O with ENXIO.
2197 * XXX Handle any transactions queued to the card
2198 * with XPT_ABORT_CCB.
2200 bioq_flush(&softc->bio_queue, NULL, ENXIO);
2201 softc->queue_count = 0;
2204 * Tell devfs that all of our devices have gone away, and ask for a
2205 * callback when it has cleaned up its state.
2207 destroy_dev_sched_cb(softc->devs.ctl_dev, sadevgonecb, periph);
2208 destroy_dev_sched_cb(softc->devs.r_dev, sadevgonecb, periph);
2209 destroy_dev_sched_cb(softc->devs.nr_dev, sadevgonecb, periph);
2210 destroy_dev_sched_cb(softc->devs.er_dev, sadevgonecb, periph);
2214 sacleanup(struct cam_periph *periph)
2216 struct sa_softc *softc;
2218 softc = (struct sa_softc *)periph->softc;
2220 cam_periph_unlock(periph);
2222 if ((softc->flags & SA_FLAG_SCTX_INIT) != 0
2223 && sysctl_ctx_free(&softc->sysctl_ctx) != 0)
2224 xpt_print(periph->path, "can't remove sysctl context\n");
2226 cam_periph_lock(periph);
2228 devstat_remove_entry(softc->device_stats);
2230 free(softc, M_SCSISA);
2234 saasync(void *callback_arg, u_int32_t code,
2235 struct cam_path *path, void *arg)
2237 struct cam_periph *periph;
2239 periph = (struct cam_periph *)callback_arg;
2241 case AC_FOUND_DEVICE:
2243 struct ccb_getdev *cgd;
2246 cgd = (struct ccb_getdev *)arg;
2250 if (cgd->protocol != PROTO_SCSI)
2252 if (SID_QUAL(&cgd->inq_data) != SID_QUAL_LU_CONNECTED)
2254 if (SID_TYPE(&cgd->inq_data) != T_SEQUENTIAL)
2258 * Allocate a peripheral instance for
2259 * this device and start the probe
2262 status = cam_periph_alloc(saregister, saoninvalidate,
2264 "sa", CAM_PERIPH_BIO, path,
2265 saasync, AC_FOUND_DEVICE, cgd);
2267 if (status != CAM_REQ_CMP
2268 && status != CAM_REQ_INPROG)
2269 printf("saasync: Unable to probe new device "
2270 "due to status 0x%x\n", status);
2274 cam_periph_async(periph, code, path, arg);
2280 sasetupdev(struct sa_softc *softc, struct cdev *dev)
2283 dev->si_iosize_max = softc->maxio;
2284 dev->si_flags |= softc->si_flags;
2286 * Keep a count of how many non-alias devices we have created,
2287 * so we can make sure we clean them all up on shutdown. Aliases
2288 * are cleaned up when we destroy the device they're an alias for.
2290 if ((dev->si_flags & SI_ALIAS) == 0)
2291 softc->num_devs_to_destroy++;
2295 sasysctlinit(void *context, int pending)
2297 struct cam_periph *periph;
2298 struct sa_softc *softc;
2299 char tmpstr[32], tmpstr2[16];
2301 periph = (struct cam_periph *)context;
2303 * If the periph is invalid, no need to setup the sysctls.
2305 if (periph->flags & CAM_PERIPH_INVALID)
2308 softc = (struct sa_softc *)periph->softc;
2310 snprintf(tmpstr, sizeof(tmpstr), "CAM SA unit %d", periph->unit_number);
2311 snprintf(tmpstr2, sizeof(tmpstr2), "%u", periph->unit_number);
2313 sysctl_ctx_init(&softc->sysctl_ctx);
2314 softc->flags |= SA_FLAG_SCTX_INIT;
2315 softc->sysctl_tree = SYSCTL_ADD_NODE_WITH_LABEL(&softc->sysctl_ctx,
2316 SYSCTL_STATIC_CHILDREN(_kern_cam_sa), OID_AUTO, tmpstr2,
2317 CTLFLAG_RD, 0, tmpstr, "device_index");
2318 if (softc->sysctl_tree == NULL)
2321 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2322 OID_AUTO, "allow_io_split", CTLFLAG_RDTUN | CTLFLAG_NOFETCH,
2323 &softc->allow_io_split, 0, "Allow Splitting I/O");
2324 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2325 OID_AUTO, "maxio", CTLFLAG_RD,
2326 &softc->maxio, 0, "Maximum I/O size");
2327 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2328 OID_AUTO, "cpi_maxio", CTLFLAG_RD,
2329 &softc->cpi_maxio, 0, "Maximum Controller I/O size");
2330 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
2331 OID_AUTO, "inject_eom", CTLFLAG_RW,
2332 &softc->inject_eom, 0, "Queue EOM for the next write/read");
2336 * Release the reference that was held when this task was enqueued.
2338 cam_periph_release(periph);
2342 saregister(struct cam_periph *periph, void *arg)
2344 struct sa_softc *softc;
2345 struct ccb_getdev *cgd;
2346 struct ccb_pathinq cpi;
2347 struct make_dev_args args;
2352 cgd = (struct ccb_getdev *)arg;
2354 printf("saregister: no getdev CCB, can't register device\n");
2355 return (CAM_REQ_CMP_ERR);
2358 softc = (struct sa_softc *)
2359 malloc(sizeof (*softc), M_SCSISA, M_NOWAIT | M_ZERO);
2360 if (softc == NULL) {
2361 printf("saregister: Unable to probe new device. "
2362 "Unable to allocate softc\n");
2363 return (CAM_REQ_CMP_ERR);
2365 softc->scsi_rev = SID_ANSI_REV(&cgd->inq_data);
2366 softc->state = SA_STATE_NORMAL;
2367 softc->fileno = (daddr_t) -1;
2368 softc->blkno = (daddr_t) -1;
2369 softc->rep_fileno = (daddr_t) -1;
2370 softc->rep_blkno = (daddr_t) -1;
2371 softc->partition = (daddr_t) -1;
2376 bioq_init(&softc->bio_queue);
2377 softc->periph = periph;
2378 periph->softc = softc;
2381 * See if this device has any quirks.
2383 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
2384 (caddr_t)sa_quirk_table,
2385 nitems(sa_quirk_table),
2386 sizeof(*sa_quirk_table), scsi_inquiry_match);
2388 if (match != NULL) {
2389 softc->quirks = ((struct sa_quirk_entry *)match)->quirks;
2390 softc->last_media_blksize =
2391 ((struct sa_quirk_entry *)match)->prefblk;
2393 softc->quirks = SA_QUIRK_NONE;
2396 * Long format data for READ POSITION was introduced in SSC, which
2397 * was after SCSI-2. (Roughly equivalent to SCSI-3.) If the drive
2398 * reports that it is SCSI-2 or older, it is unlikely to support
2399 * long position data, but it might. Some drives from that era
2400 * claim to be SCSI-2, but do support long position information.
2401 * So, instead of immediately disabling long position information
2402 * for SCSI-2 devices, we'll try one pass through sagetpos(), and
2403 * then disable long position information if we get an error.
2405 if (cgd->inq_data.version <= SCSI_REV_CCS)
2406 softc->quirks |= SA_QUIRK_NO_LONG_POS;
2408 if (cgd->inq_data.spc3_flags & SPC3_SID_PROTECT) {
2409 struct ccb_dev_advinfo cdai;
2410 struct scsi_vpd_extended_inquiry_data ext_inq;
2412 bzero(&ext_inq, sizeof(ext_inq));
2414 xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2416 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
2417 cdai.flags = CDAI_FLAG_NONE;
2418 cdai.buftype = CDAI_TYPE_EXT_INQ;
2419 cdai.bufsiz = sizeof(ext_inq);
2420 cdai.buf = (uint8_t *)&ext_inq;
2421 xpt_action((union ccb *)&cdai);
2423 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
2424 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
2425 if ((cdai.ccb_h.status == CAM_REQ_CMP)
2426 && (ext_inq.flags1 & SVPD_EID_SA_SPT_LBP))
2427 softc->flags |= SA_FLAG_PROTECT_SUPP;
2430 xpt_path_inq(&cpi, periph->path);
2433 * The SA driver supports a blocksize, but we don't know the
2434 * blocksize until we media is inserted. So, set a flag to
2435 * indicate that the blocksize is unavailable right now.
2437 cam_periph_unlock(periph);
2438 softc->device_stats = devstat_new_entry("sa", periph->unit_number, 0,
2439 DEVSTAT_BS_UNAVAILABLE, SID_TYPE(&cgd->inq_data) |
2440 XPORT_DEVSTAT_TYPE(cpi.transport), DEVSTAT_PRIORITY_TAPE);
2443 * Load the default value that is either compiled in, or loaded
2444 * in the global kern.cam.sa.allow_io_split tunable.
2446 softc->allow_io_split = sa_allow_io_split;
2449 * Load a per-instance tunable, if it exists. NOTE that this
2450 * tunable WILL GO AWAY in FreeBSD 11.0.
2452 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.sa.%u.allow_io_split",
2453 periph->unit_number);
2454 TUNABLE_INT_FETCH(tmpstr, &softc->allow_io_split);
2457 * If maxio isn't set, we fall back to DFLTPHYS. Otherwise we take
2458 * the smaller of cpi.maxio or MAXPHYS.
2461 softc->maxio = DFLTPHYS;
2462 else if (cpi.maxio > MAXPHYS)
2463 softc->maxio = MAXPHYS;
2465 softc->maxio = cpi.maxio;
2468 * Record the controller's maximum I/O size so we can report it to
2471 softc->cpi_maxio = cpi.maxio;
2474 * By default we tell physio that we do not want our I/O split.
2475 * The user needs to have a 1:1 mapping between the size of his
2476 * write to a tape character device and the size of the write
2477 * that actually goes down to the drive.
2479 if (softc->allow_io_split == 0)
2480 softc->si_flags = SI_NOSPLIT;
2482 softc->si_flags = 0;
2484 TASK_INIT(&softc->sysctl_task, 0, sasysctlinit, periph);
2487 * If the SIM supports unmapped I/O, let physio know that we can
2488 * handle unmapped buffers.
2490 if (cpi.hba_misc & PIM_UNMAPPED)
2491 softc->si_flags |= SI_UNMAPPED;
2494 * Acquire a reference to the periph before we create the devfs
2495 * instances for it. We'll release this reference once the devfs
2496 * instances have been freed.
2498 if (cam_periph_acquire(periph) != 0) {
2499 xpt_print(periph->path, "%s: lost periph during "
2500 "registration!\n", __func__);
2501 cam_periph_lock(periph);
2502 return (CAM_REQ_CMP_ERR);
2505 make_dev_args_init(&args);
2506 args.mda_devsw = &sa_cdevsw;
2507 args.mda_si_drv1 = softc->periph;
2508 args.mda_uid = UID_ROOT;
2509 args.mda_gid = GID_OPERATOR;
2510 args.mda_mode = 0660;
2512 args.mda_unit = SAMINOR(SA_CTLDEV, SA_ATYPE_R);
2513 error = make_dev_s(&args, &softc->devs.ctl_dev, "%s%d.ctl",
2514 periph->periph_name, periph->unit_number);
2516 cam_periph_lock(periph);
2517 return (CAM_REQ_CMP_ERR);
2519 sasetupdev(softc, softc->devs.ctl_dev);
2521 args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_R);
2522 error = make_dev_s(&args, &softc->devs.r_dev, "%s%d",
2523 periph->periph_name, periph->unit_number);
2525 cam_periph_lock(periph);
2526 return (CAM_REQ_CMP_ERR);
2528 sasetupdev(softc, softc->devs.r_dev);
2530 args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_NR);
2531 error = make_dev_s(&args, &softc->devs.nr_dev, "n%s%d",
2532 periph->periph_name, periph->unit_number);
2534 cam_periph_lock(periph);
2535 return (CAM_REQ_CMP_ERR);
2537 sasetupdev(softc, softc->devs.nr_dev);
2539 args.mda_unit = SAMINOR(SA_NOT_CTLDEV, SA_ATYPE_ER);
2540 error = make_dev_s(&args, &softc->devs.er_dev, "e%s%d",
2541 periph->periph_name, periph->unit_number);
2543 cam_periph_lock(periph);
2544 return (CAM_REQ_CMP_ERR);
2546 sasetupdev(softc, softc->devs.er_dev);
2548 cam_periph_lock(periph);
2550 softc->density_type_bits[0] = 0;
2551 softc->density_type_bits[1] = SRDS_MEDIA;
2552 softc->density_type_bits[2] = SRDS_MEDIUM_TYPE;
2553 softc->density_type_bits[3] = SRDS_MEDIUM_TYPE | SRDS_MEDIA;
2555 * Bump the peripheral refcount for the sysctl thread, in case we
2556 * get invalidated before the thread has a chance to run.
2558 cam_periph_acquire(periph);
2559 taskqueue_enqueue(taskqueue_thread, &softc->sysctl_task);
2562 * Add an async callback so that we get
2563 * notified if this device goes away.
2565 xpt_register_async(AC_LOST_DEVICE, saasync, periph, periph->path);
2567 xpt_announce_periph(periph, NULL);
2568 xpt_announce_quirks(periph, softc->quirks, SA_QUIRK_BIT_STRING);
2570 return (CAM_REQ_CMP);
2574 sastart(struct cam_periph *periph, union ccb *start_ccb)
2576 struct sa_softc *softc;
2578 softc = (struct sa_softc *)periph->softc;
2580 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("sastart\n"));
2583 switch (softc->state) {
2584 case SA_STATE_NORMAL:
2586 /* Pull a buffer from the queue and get going on it */
2590 * See if there is a buf with work for us to do..
2592 bp = bioq_first(&softc->bio_queue);
2594 xpt_release_ccb(start_ccb);
2595 } else if (((softc->flags & SA_FLAG_ERR_PENDING) != 0)
2596 || (softc->inject_eom != 0)) {
2597 struct bio *done_bp;
2599 if (softc->inject_eom != 0) {
2600 softc->flags |= SA_FLAG_EOM_PENDING;
2601 softc->inject_eom = 0;
2603 * If we're injecting EOM for writes, we
2604 * need to keep PEWS set for 3 queries
2605 * to cover 2 position requests from the
2606 * kernel via sagetpos(), and then allow
2607 * for one for the user to see the BPEW
2608 * flag (e.g. via mt status). After that,
2609 * it will be cleared.
2611 if (bp->bio_cmd == BIO_WRITE)
2612 softc->set_pews_status = 3;
2614 softc->set_pews_status = 1;
2617 softc->queue_count--;
2618 bioq_remove(&softc->bio_queue, bp);
2619 bp->bio_resid = bp->bio_bcount;
2621 if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) {
2623 * We have two different behaviors for
2624 * writes when we hit either Early Warning
2625 * or the PEWZ (Programmable Early Warning
2626 * Zone). The default behavior is that
2627 * for all writes that are currently
2628 * queued after the write where we saw the
2629 * early warning, we will return the write
2630 * with the residual equal to the count.
2631 * i.e. tell the application that 0 bytes
2634 * The alternate behavior, which is enabled
2635 * when eot_warn is set, is that in
2636 * addition to setting the residual equal
2637 * to the count, we will set the error
2640 * In either case, once queued writes are
2641 * cleared out, we clear the error flag
2642 * (see below) and the application is free to
2643 * attempt to write more.
2645 if (softc->eot_warn != 0) {
2646 bp->bio_flags |= BIO_ERROR;
2647 bp->bio_error = ENOSPC;
2650 } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) {
2652 * This can only happen if we're reading
2653 * in fixed length mode. In this case,
2654 * we dump the rest of the list the
2658 if (bioq_first(&softc->bio_queue) != NULL) {
2662 } else if ((softc->flags & SA_FLAG_EIO_PENDING) != 0) {
2663 bp->bio_error = EIO;
2664 bp->bio_flags |= BIO_ERROR;
2666 bp = bioq_first(&softc->bio_queue);
2668 * Only if we have no other buffers queued up
2669 * do we clear the pending error flag.
2672 softc->flags &= ~SA_FLAG_ERR_PENDING;
2673 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2674 ("sastart- ERR_PENDING now 0x%x, bp is %sNULL, "
2675 "%d more buffers queued up\n",
2676 (softc->flags & SA_FLAG_ERR_PENDING),
2677 (bp != NULL)? "not " : " ", softc->queue_count));
2678 xpt_release_ccb(start_ccb);
2683 bioq_remove(&softc->bio_queue, bp);
2684 softc->queue_count--;
2686 if ((bp->bio_cmd != BIO_READ) &&
2687 (bp->bio_cmd != BIO_WRITE)) {
2688 biofinish(bp, NULL, EOPNOTSUPP);
2689 xpt_release_ccb(start_ccb);
2692 length = bp->bio_bcount;
2694 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2695 if (softc->blk_shift != 0) {
2696 length = length >> softc->blk_shift;
2697 } else if (softc->media_blksize != 0) {
2698 length = length / softc->media_blksize;
2700 bp->bio_error = EIO;
2701 xpt_print(periph->path, "zero blocksize"
2702 " for FIXED length writes?\n");
2707 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
2708 ("issuing a %d fixed record %s\n",
2709 length, (bp->bio_cmd == BIO_READ)? "read" :
2714 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO,
2715 ("issuing a %d variable byte %s\n",
2716 length, (bp->bio_cmd == BIO_READ)? "read" :
2720 devstat_start_transaction_bio(softc->device_stats, bp);
2722 * Some people have theorized that we should
2723 * suppress illegal length indication if we are
2724 * running in variable block mode so that we don't
2725 * have to request sense every time our requested
2726 * block size is larger than the written block.
2727 * The residual information from the ccb allows
2728 * us to identify this situation anyway. The only
2729 * problem with this is that we will not get
2730 * information about blocks that are larger than
2731 * our read buffer unless we set the block size
2732 * in the mode page to something other than 0.
2734 * I believe that this is a non-issue. If user apps
2735 * don't adjust their read size to match our record
2736 * size, that's just life. Anyway, the typical usage
2737 * would be to issue, e.g., 64KB reads and occasionally
2738 * have to do deal with 512 byte or 1KB intermediate
2741 * That said, though, we now support setting the
2742 * SILI bit on reads, and we set the blocksize to 4
2743 * bytes when we do that. This gives us
2744 * compatibility with software that wants this,
2745 * although the only real difference between that
2746 * and not setting the SILI bit on reads is that we
2747 * won't get a check condition on reads where our
2748 * request size is larger than the block on tape.
2749 * That probably only makes a real difference in
2750 * non-packetized SCSI, where you have to go back
2751 * to the drive to request sense and thus incur
2754 softc->dsreg = (bp->bio_cmd == BIO_READ)?
2755 MTIO_DSREG_RD : MTIO_DSREG_WR;
2756 scsi_sa_read_write(&start_ccb->csio, 0, sadone,
2757 MSG_SIMPLE_Q_TAG, (bp->bio_cmd == BIO_READ ?
2758 SCSI_RW_READ : SCSI_RW_WRITE) |
2759 ((bp->bio_flags & BIO_UNMAPPED) != 0 ?
2760 SCSI_RW_BIO : 0), softc->sili,
2761 (softc->flags & SA_FLAG_FIXED) != 0, length,
2762 (bp->bio_flags & BIO_UNMAPPED) != 0 ? (void *)bp :
2763 bp->bio_data, bp->bio_bcount, SSD_FULL_SIZE,
2765 start_ccb->ccb_h.ccb_pflags &= ~SA_POSITION_UPDATED;
2766 start_ccb->ccb_h.ccb_bp = bp;
2767 bp = bioq_first(&softc->bio_queue);
2768 xpt_action(start_ccb);
2772 /* Have more work to do, so ensure we stay scheduled */
2773 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
2777 case SA_STATE_ABNORMAL:
2779 panic("state 0x%x in sastart", softc->state);
2786 sadone(struct cam_periph *periph, union ccb *done_ccb)
2788 struct sa_softc *softc;
2789 struct ccb_scsiio *csio;
2793 softc = (struct sa_softc *)periph->softc;
2794 csio = &done_ccb->csio;
2796 softc->dsreg = MTIO_DSREG_REST;
2797 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
2799 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2800 if ((error = saerror(done_ccb, 0, 0)) == ERESTART) {
2802 * A retry was scheduled, so just return.
2811 * Catastrophic error. Mark the tape as frozen
2812 * (we no longer know tape position).
2814 * Return all queued I/O with EIO, and unfreeze
2815 * our queue so that future transactions that
2816 * attempt to fix this problem can get to the
2821 softc->flags |= SA_FLAG_TAPE_FROZEN;
2822 bioq_flush(&softc->bio_queue, NULL, EIO);
2825 bp->bio_resid = bp->bio_bcount;
2826 bp->bio_error = error;
2827 bp->bio_flags |= BIO_ERROR;
2829 * In the error case, position is updated in saerror.
2832 bp->bio_resid = csio->resid;
2834 if (csio->resid != 0) {
2835 bp->bio_flags |= BIO_ERROR;
2837 if (bp->bio_cmd == BIO_WRITE) {
2838 softc->flags |= SA_FLAG_TAPE_WRITTEN;
2839 softc->filemarks = 0;
2841 if (!(csio->ccb_h.ccb_pflags & SA_POSITION_UPDATED) &&
2842 (softc->blkno != (daddr_t) -1)) {
2843 if ((softc->flags & SA_FLAG_FIXED) != 0) {
2845 if (softc->blk_shift != 0) {
2846 l = bp->bio_bcount >>
2849 l = bp->bio_bcount /
2850 softc->media_blksize;
2852 softc->blkno += (daddr_t) l;
2859 * If we had an error (immediate or pending),
2860 * release the device queue now.
2862 if (error || (softc->flags & SA_FLAG_ERR_PENDING))
2863 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0);
2864 if (error || bp->bio_resid) {
2865 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
2866 ("error %d resid %ld count %ld\n", error,
2867 bp->bio_resid, bp->bio_bcount));
2869 biofinish(bp, softc->device_stats, 0);
2870 xpt_release_ccb(done_ccb);
2874 * Mount the tape (make sure it's ready for I/O).
2877 samount(struct cam_periph *periph, int oflags, struct cdev *dev)
2879 struct sa_softc *softc;
2884 * oflags can be checked for 'kind' of open (read-only check) - later
2885 * dev can be checked for a control-mode or compression open - later
2887 UNUSED_PARAMETER(oflags);
2888 UNUSED_PARAMETER(dev);
2891 softc = (struct sa_softc *)periph->softc;
2894 * This should determine if something has happened since the last
2895 * open/mount that would invalidate the mount. We do *not* want
2896 * to retry this command- we just want the status. But we only
2897 * do this if we're mounted already- if we're not mounted,
2898 * we don't care about the unit read state and can instead use
2899 * this opportunity to attempt to reserve the tape unit.
2902 if (softc->flags & SA_FLAG_TAPE_MOUNTED) {
2903 ccb = cam_periph_getccb(periph, 1);
2904 scsi_test_unit_ready(&ccb->csio, 0, NULL,
2905 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
2906 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2907 softc->device_stats);
2908 if (error == ENXIO) {
2909 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
2910 scsi_test_unit_ready(&ccb->csio, 0, NULL,
2911 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
2912 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2913 softc->device_stats);
2916 * We don't need to freeze the tape because we
2917 * will now attempt to rewind/load it.
2919 softc->flags &= ~SA_FLAG_TAPE_MOUNTED;
2920 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
2921 xpt_print(periph->path,
2922 "error %d on TUR in samount\n", error);
2926 error = sareservereleaseunit(periph, TRUE);
2930 ccb = cam_periph_getccb(periph, 1);
2931 scsi_test_unit_ready(&ccb->csio, 0, NULL,
2932 MSG_SIMPLE_Q_TAG, SSD_FULL_SIZE, IO_TIMEOUT);
2933 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2934 softc->device_stats);
2937 if ((softc->flags & SA_FLAG_TAPE_MOUNTED) == 0) {
2938 struct scsi_read_block_limits_data *rblim = NULL;
2939 int comp_enabled, comp_supported;
2940 u_int8_t write_protect, guessing = 0;
2943 * Clear out old state.
2945 softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN|
2946 SA_FLAG_ERR_PENDING|SA_FLAG_COMPRESSION);
2947 softc->filemarks = 0;
2950 * *Very* first off, make sure we're loaded to BOT.
2952 scsi_load_unload(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG, FALSE,
2953 FALSE, FALSE, 1, SSD_FULL_SIZE, REWIND_TIMEOUT);
2954 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2955 softc->device_stats);
2958 * In case this doesn't work, do a REWIND instead
2961 scsi_rewind(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG,
2962 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
2963 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2964 softc->device_stats);
2967 xpt_release_ccb(ccb);
2972 * Do a dummy test read to force access to the
2973 * media so that the drive will really know what's
2974 * there. We actually don't really care what the
2975 * blocksize on tape is and don't expect to really
2976 * read a full record.
2978 rblim = (struct scsi_read_block_limits_data *)
2979 malloc(8192, M_SCSISA, M_NOWAIT);
2980 if (rblim == NULL) {
2981 xpt_print(periph->path, "no memory for test read\n");
2982 xpt_release_ccb(ccb);
2987 if ((softc->quirks & SA_QUIRK_NODREAD) == 0) {
2988 scsi_sa_read_write(&ccb->csio, 0, NULL,
2989 MSG_SIMPLE_Q_TAG, 1, FALSE, 0, 8192,
2990 (void *) rblim, 8192, SSD_FULL_SIZE,
2992 (void) cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
2993 softc->device_stats);
2994 scsi_rewind(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG,
2995 FALSE, SSD_FULL_SIZE, REWIND_TIMEOUT);
2996 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
2997 SF_NO_PRINT | SF_RETRY_UA,
2998 softc->device_stats);
3000 xpt_print(periph->path,
3001 "unable to rewind after test read\n");
3002 xpt_release_ccb(ccb);
3008 * Next off, determine block limits.
3010 scsi_read_block_limits(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
3011 rblim, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3013 error = cam_periph_runccb(ccb, saerror, CAM_RETRY_SELTO,
3014 SF_NO_PRINT | SF_RETRY_UA, softc->device_stats);
3016 xpt_release_ccb(ccb);
3020 * If it's less than SCSI-2, READ BLOCK LIMITS is not
3021 * a MANDATORY command. Anyway- it doesn't matter-
3022 * we can proceed anyway.
3024 softc->blk_gran = 0;
3025 softc->max_blk = ~0;
3028 if (softc->scsi_rev >= SCSI_REV_SPC) {
3029 softc->blk_gran = RBL_GRAN(rblim);
3031 softc->blk_gran = 0;
3034 * We take max_blk == min_blk to mean a default to
3035 * fixed mode- but note that whatever we get out of
3036 * sagetparams below will actually determine whether
3037 * we are actually *in* fixed mode.
3039 softc->max_blk = scsi_3btoul(rblim->maximum);
3040 softc->min_blk = scsi_2btoul(rblim->minimum);
3045 * Next, perform a mode sense to determine
3046 * current density, blocksize, compression etc.
3048 error = sagetparams(periph, SA_PARAM_ALL,
3049 &softc->media_blksize,
3050 &softc->media_density,
3051 &softc->media_numblks,
3052 &softc->buffer_mode, &write_protect,
3053 &softc->speed, &comp_supported,
3054 &comp_enabled, &softc->comp_algorithm,
3059 * We could work a little harder here. We could
3060 * adjust our attempts to get information. It
3061 * might be an ancient tape drive. If someone
3062 * nudges us, we'll do that.
3068 * If no quirk has determined that this is a device that is
3069 * preferred to be in fixed or variable mode, now is the time
3072 if ((softc->quirks & (SA_QUIRK_FIXED|SA_QUIRK_VARIABLE)) == 0) {
3075 * This could be expensive to find out. Luckily we
3076 * only need to do this once. If we start out in
3077 * 'default' mode, try and set ourselves to one
3078 * of the densities that would determine a wad
3079 * of other stuff. Go from highest to lowest.
3081 if (softc->media_density == SCSI_DEFAULT_DENSITY) {
3083 static u_int8_t ctry[] = {
3084 SCSI_DENSITY_HALFINCH_PE,
3085 SCSI_DENSITY_HALFINCH_6250C,
3086 SCSI_DENSITY_HALFINCH_6250,
3087 SCSI_DENSITY_HALFINCH_1600,
3088 SCSI_DENSITY_HALFINCH_800,
3089 SCSI_DENSITY_QIC_4GB,
3090 SCSI_DENSITY_QIC_2GB,
3091 SCSI_DENSITY_QIC_525_320,
3092 SCSI_DENSITY_QIC_150,
3093 SCSI_DENSITY_QIC_120,
3094 SCSI_DENSITY_QIC_24,
3095 SCSI_DENSITY_QIC_11_9TRK,
3096 SCSI_DENSITY_QIC_11_4TRK,
3097 SCSI_DENSITY_QIC_1320,
3098 SCSI_DENSITY_QIC_3080,
3101 for (i = 0; ctry[i]; i++) {
3102 error = sasetparams(periph,
3103 SA_PARAM_DENSITY, 0, ctry[i],
3106 softc->media_density = ctry[i];
3111 switch (softc->media_density) {
3112 case SCSI_DENSITY_QIC_11_4TRK:
3113 case SCSI_DENSITY_QIC_11_9TRK:
3114 case SCSI_DENSITY_QIC_24:
3115 case SCSI_DENSITY_QIC_120:
3116 case SCSI_DENSITY_QIC_150:
3117 case SCSI_DENSITY_QIC_525_320:
3118 case SCSI_DENSITY_QIC_1320:
3119 case SCSI_DENSITY_QIC_3080:
3120 softc->quirks &= ~SA_QUIRK_2FM;
3121 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
3122 softc->last_media_blksize = 512;
3124 case SCSI_DENSITY_QIC_4GB:
3125 case SCSI_DENSITY_QIC_2GB:
3126 softc->quirks &= ~SA_QUIRK_2FM;
3127 softc->quirks |= SA_QUIRK_FIXED|SA_QUIRK_1FM;
3128 softc->last_media_blksize = 1024;
3131 softc->last_media_blksize =
3132 softc->media_blksize;
3133 softc->quirks |= SA_QUIRK_VARIABLE;
3139 * If no quirk has determined that this is a device that needs
3140 * to have 2 Filemarks at EOD, now is the time to find out.
3143 if ((softc->quirks & SA_QUIRK_2FM) == 0) {
3144 switch (softc->media_density) {
3145 case SCSI_DENSITY_HALFINCH_800:
3146 case SCSI_DENSITY_HALFINCH_1600:
3147 case SCSI_DENSITY_HALFINCH_6250:
3148 case SCSI_DENSITY_HALFINCH_6250C:
3149 case SCSI_DENSITY_HALFINCH_PE:
3150 softc->quirks &= ~SA_QUIRK_1FM;
3151 softc->quirks |= SA_QUIRK_2FM;
3159 * Now validate that some info we got makes sense.
3161 if ((softc->max_blk < softc->media_blksize) ||
3162 (softc->min_blk > softc->media_blksize &&
3163 softc->media_blksize)) {
3164 xpt_print(periph->path,
3165 "BLOCK LIMITS (%d..%d) could not match current "
3166 "block settings (%d)- adjusting\n", softc->min_blk,
3167 softc->max_blk, softc->media_blksize);
3168 softc->max_blk = softc->min_blk =
3169 softc->media_blksize;
3173 * Now put ourselves into the right frame of mind based
3178 * If we want to be in FIXED mode and our current blocksize
3179 * is not equal to our last blocksize (if nonzero), try and
3180 * set ourselves to this last blocksize (as the 'preferred'
3181 * block size). The initial quirkmatch at registry sets the
3182 * initial 'last' blocksize. If, for whatever reason, this
3183 * 'last' blocksize is zero, set the blocksize to 512,
3184 * or min_blk if that's larger.
3186 if ((softc->quirks & SA_QUIRK_FIXED) &&
3187 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0 &&
3188 (softc->media_blksize != softc->last_media_blksize)) {
3189 softc->media_blksize = softc->last_media_blksize;
3190 if (softc->media_blksize == 0) {
3191 softc->media_blksize = 512;
3192 if (softc->media_blksize < softc->min_blk) {
3193 softc->media_blksize = softc->min_blk;
3196 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
3197 softc->media_blksize, 0, 0, SF_NO_PRINT);
3199 xpt_print(periph->path,
3200 "unable to set fixed blocksize to %d\n",
3201 softc->media_blksize);
3206 if ((softc->quirks & SA_QUIRK_VARIABLE) &&
3207 (softc->media_blksize != 0)) {
3208 softc->last_media_blksize = softc->media_blksize;
3209 softc->media_blksize = 0;
3210 error = sasetparams(periph, SA_PARAM_BLOCKSIZE,
3211 0, 0, 0, SF_NO_PRINT);
3214 * If this fails and we were guessing, just
3215 * assume that we got it wrong and go try
3216 * fixed block mode. Don't even check against
3217 * density code at this point.
3220 softc->quirks &= ~SA_QUIRK_VARIABLE;
3221 softc->quirks |= SA_QUIRK_FIXED;
3222 if (softc->last_media_blksize == 0)
3223 softc->last_media_blksize = 512;
3226 xpt_print(periph->path,
3227 "unable to set variable blocksize\n");
3233 * Now that we have the current block size,
3234 * set up some parameters for sastart's usage.
3236 if (softc->media_blksize) {
3237 softc->flags |= SA_FLAG_FIXED;
3238 if (powerof2(softc->media_blksize)) {
3240 ffs(softc->media_blksize) - 1;
3241 softc->blk_mask = softc->media_blksize - 1;
3243 softc->blk_mask = ~0;
3244 softc->blk_shift = 0;
3248 * The SCSI-3 spec allows 0 to mean "unspecified".
3249 * The SCSI-1 spec allows 0 to mean 'infinite'.
3251 * Either works here.
3253 if (softc->max_blk == 0) {
3254 softc->max_blk = ~0;
3256 softc->blk_shift = 0;
3257 if (softc->blk_gran != 0) {
3258 softc->blk_mask = softc->blk_gran - 1;
3260 softc->blk_mask = 0;
3265 softc->flags |= SA_FLAG_TAPE_WP;
3267 if (comp_supported) {
3268 if (softc->saved_comp_algorithm == 0)
3269 softc->saved_comp_algorithm =
3270 softc->comp_algorithm;
3271 softc->flags |= SA_FLAG_COMP_SUPP;
3273 softc->flags |= SA_FLAG_COMP_ENABLED;
3275 softc->flags |= SA_FLAG_COMP_UNSUPP;
3277 if ((softc->buffer_mode == SMH_SA_BUF_MODE_NOBUF) &&
3278 (softc->quirks & SA_QUIRK_NO_MODESEL) == 0) {
3279 error = sasetparams(periph, SA_PARAM_BUFF_MODE, 0,
3282 softc->buffer_mode = SMH_SA_BUF_MODE_SIBUF;
3284 xpt_print(periph->path,
3285 "unable to set buffered mode\n");
3287 error = 0; /* not an error */
3292 softc->flags |= SA_FLAG_TAPE_MOUNTED;
3296 free(rblim, M_SCSISA);
3299 softc->dsreg = MTIO_DSREG_NIL;
3301 softc->fileno = softc->blkno = 0;
3302 softc->rep_fileno = softc->rep_blkno = -1;
3303 softc->partition = 0;
3304 softc->dsreg = MTIO_DSREG_REST;
3306 #ifdef SA_1FM_AT_EOD
3307 if ((softc->quirks & SA_QUIRK_2FM) == 0)
3308 softc->quirks |= SA_QUIRK_1FM;
3310 if ((softc->quirks & SA_QUIRK_1FM) == 0)
3311 softc->quirks |= SA_QUIRK_2FM;
3314 xpt_release_ccb(ccb);
3317 * If we return an error, we're not mounted any more,
3318 * so release any device reservation.
3321 (void) sareservereleaseunit(periph, FALSE);
3324 * Clear I/O residual.
3326 softc->last_io_resid = 0;
3327 softc->last_ctl_resid = 0;
3333 * How many filemarks do we need to write if we were to terminate the
3334 * tape session right now? Note that this can be a negative number
3338 samarkswanted(struct cam_periph *periph)
3341 struct sa_softc *softc;
3343 softc = (struct sa_softc *)periph->softc;
3345 if ((softc->flags & SA_FLAG_TAPE_WRITTEN) != 0) {
3347 if (softc->quirks & SA_QUIRK_2FM)
3350 markswanted -= softc->filemarks;
3351 return (markswanted);
3355 sacheckeod(struct cam_periph *periph)
3360 markswanted = samarkswanted(periph);
3362 if (markswanted > 0) {
3363 error = sawritefilemarks(periph, markswanted, FALSE, FALSE);
3371 saerror(union ccb *ccb, u_int32_t cflgs, u_int32_t sflgs)
3373 static const char *toobig =
3374 "%d-byte tape record bigger than supplied buffer\n";
3375 struct cam_periph *periph;
3376 struct sa_softc *softc;
3377 struct ccb_scsiio *csio;
3378 struct scsi_sense_data *sense;
3382 int error_code, sense_key, asc, ascq, error, aqvalid, stream_valid;
3384 uint8_t stream_bits;
3386 periph = xpt_path_periph(ccb->ccb_h.path);
3387 softc = (struct sa_softc *)periph->softc;
3389 sense = &csio->sense_data;
3390 sense_len = csio->sense_len - csio->sense_resid;
3391 scsi_extract_sense_len(sense, sense_len, &error_code, &sense_key,
3392 &asc, &ascq, /*show_errors*/ 1);
3393 if (asc != -1 && ascq != -1)
3397 if (scsi_get_stream_info(sense, sense_len, NULL, &stream_bits) == 0)
3403 status = csio->ccb_h.status & CAM_STATUS_MASK;
3406 * Calculate/latch up, any residuals... We do this in a funny 2-step
3407 * so we can print stuff here if we have CAM_DEBUG enabled for this
3410 if (status == CAM_SCSI_STATUS_ERROR) {
3411 if (scsi_get_sense_info(sense, sense_len, SSD_DESC_INFO, &resid,
3413 if ((softc->flags & SA_FLAG_FIXED) != 0)
3414 resid *= softc->media_blksize;
3416 resid = csio->dxfer_len;
3418 if ((softc->flags & SA_FLAG_FIXED) != 0) {
3419 if (softc->media_blksize)
3420 info /= softc->media_blksize;
3423 if (csio->cdb_io.cdb_bytes[0] == SA_READ ||
3424 csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
3425 bcopy((caddr_t) sense, (caddr_t) &softc->last_io_sense,
3426 sizeof (struct scsi_sense_data));
3427 bcopy(csio->cdb_io.cdb_bytes, softc->last_io_cdb,
3428 (int) csio->cdb_len);
3429 softc->last_io_resid = resid;
3430 softc->last_resid_was_io = 1;
3432 bcopy((caddr_t) sense, (caddr_t) &softc->last_ctl_sense,
3433 sizeof (struct scsi_sense_data));
3434 bcopy(csio->cdb_io.cdb_bytes, softc->last_ctl_cdb,
3435 (int) csio->cdb_len);
3436 softc->last_ctl_resid = resid;
3437 softc->last_resid_was_io = 0;
3439 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("CDB[0]=0x%x Key 0x%x "
3440 "ASC/ASCQ 0x%x/0x%x CAM STATUS 0x%x flags 0x%x resid %jd "
3441 "dxfer_len %d\n", csio->cdb_io.cdb_bytes[0] & 0xff,
3442 sense_key, asc, ascq, status,
3443 (stream_valid) ? stream_bits : 0, (intmax_t)resid,
3446 CAM_DEBUG(periph->path, CAM_DEBUG_INFO,
3447 ("Cam Status 0x%x\n", status));
3453 case CAM_SCSI_STATUS_ERROR:
3455 * If a read/write command, we handle it here.
3457 if (csio->cdb_io.cdb_bytes[0] == SA_READ ||
3458 csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
3462 * If this was just EOM/EOP, Filemark, Setmark, ILI or
3463 * PEW detected on a non read/write command, we assume
3464 * it's not an error and propagate the residual and return.
3466 if ((aqvalid && asc == 0 && ((ascq > 0 && ascq <= 5)
3468 || (aqvalid == 0 && sense_key == SSD_KEY_NO_SENSE)) {
3469 csio->resid = resid;
3474 * Otherwise, we let the common code handle this.
3476 return (cam_periph_error(ccb, cflgs, sflgs));
3480 * We cannot depend upon CAM honoring retry counts for these.
3482 case CAM_SCSI_BUS_RESET:
3484 if (ccb->ccb_h.retry_count <= 0) {
3489 return (cam_periph_error(ccb, cflgs, sflgs));
3493 * Handle filemark, end of tape, mismatched record sizes....
3494 * From this point out, we're only handling read/write cases.
3495 * Handle writes && reads differently.
3498 if (csio->cdb_io.cdb_bytes[0] == SA_WRITE) {
3499 if (sense_key == SSD_KEY_VOLUME_OVERFLOW) {
3500 csio->resid = resid;
3502 } else if ((stream_valid != 0) && (stream_bits & SSD_EOM)) {
3503 softc->flags |= SA_FLAG_EOM_PENDING;
3505 * Grotesque as it seems, the few times
3506 * I've actually seen a non-zero resid,
3507 * the tape drive actually lied and had
3508 * written all the data!.
3513 csio->resid = resid;
3514 if (sense_key == SSD_KEY_BLANK_CHECK) {
3515 if (softc->quirks & SA_QUIRK_1FM) {
3517 softc->flags |= SA_FLAG_EOM_PENDING;
3521 } else if ((stream_valid != 0) && (stream_bits & SSD_FILEMARK)){
3522 if (softc->flags & SA_FLAG_FIXED) {
3524 softc->flags |= SA_FLAG_EOF_PENDING;
3527 * Unconditionally, if we detected a filemark on a read,
3528 * mark that we've run moved a file ahead.
3530 if (softc->fileno != (daddr_t) -1) {
3533 csio->ccb_h.ccb_pflags |= SA_POSITION_UPDATED;
3539 * Incorrect Length usually applies to read, but can apply to writes.
3541 if (error == 0 && (stream_valid != 0) && (stream_bits & SSD_ILI)) {
3543 xpt_print(csio->ccb_h.path, toobig,
3544 csio->dxfer_len - info);
3545 csio->resid = csio->dxfer_len;
3548 csio->resid = resid;
3549 if (softc->flags & SA_FLAG_FIXED) {
3550 softc->flags |= SA_FLAG_EIO_PENDING;
3553 * Bump the block number if we hadn't seen a filemark.
3554 * Do this independent of errors (we've moved anyway).
3556 if ((stream_valid == 0) ||
3557 (stream_bits & SSD_FILEMARK) == 0) {
3558 if (softc->blkno != (daddr_t) -1) {
3560 csio->ccb_h.ccb_pflags |=
3561 SA_POSITION_UPDATED;
3569 * Unfreeze the queue if frozen as we're not returning anything
3570 * to our waiters that would indicate an I/O error has occurred
3580 sagetparams(struct cam_periph *periph, sa_params params_to_get,
3581 u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks,
3582 int *buff_mode, u_int8_t *write_protect, u_int8_t *speed,
3583 int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm,
3584 sa_comp_t *tcs, struct scsi_control_data_prot_subpage *prot_page,
3585 int dp_size, int prot_changeable)
3589 struct scsi_mode_header_6 *mode_hdr;
3590 struct scsi_mode_blk_desc *mode_blk;
3591 int mode_buffer_len;
3592 struct sa_softc *softc;
3597 softc = (struct sa_softc *)periph->softc;
3598 ccb = cam_periph_getccb(periph, 1);
3599 if (softc->quirks & SA_QUIRK_NO_CPAGE)
3600 cpage = SA_DEVICE_CONFIGURATION_PAGE;
3602 cpage = SA_DATA_COMPRESSION_PAGE;
3605 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
3607 if (params_to_get & SA_PARAM_COMPRESSION) {
3608 if (softc->quirks & SA_QUIRK_NOCOMP) {
3609 *comp_supported = FALSE;
3610 params_to_get &= ~SA_PARAM_COMPRESSION;
3612 mode_buffer_len += sizeof (sa_comp_t);
3615 /* XXX Fix M_NOWAIT */
3616 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
3617 if (mode_buffer == NULL) {
3618 xpt_release_ccb(ccb);
3621 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
3622 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
3624 /* it is safe to retry this */
3625 scsi_mode_sense(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, FALSE,
3626 SMS_PAGE_CTRL_CURRENT, (params_to_get & SA_PARAM_COMPRESSION) ?
3627 cpage : SMS_VENDOR_SPECIFIC_PAGE, mode_buffer, mode_buffer_len,
3628 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
3630 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3631 softc->device_stats);
3633 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3635 if (error == EINVAL && (params_to_get & SA_PARAM_COMPRESSION) != 0) {
3637 * Hmm. Let's see if we can try another page...
3638 * If we've already done that, give up on compression
3639 * for this device and remember this for the future
3640 * and attempt the request without asking for compression
3643 if (cpage == SA_DATA_COMPRESSION_PAGE) {
3644 cpage = SA_DEVICE_CONFIGURATION_PAGE;
3647 softc->quirks |= SA_QUIRK_NOCOMP;
3648 free(mode_buffer, M_SCSISA);
3650 } else if (status == CAM_SCSI_STATUS_ERROR) {
3651 /* Tell the user about the fatal error. */
3652 scsi_sense_print(&ccb->csio);
3653 goto sagetparamsexit;
3657 * If the user only wants the compression information, and
3658 * the device doesn't send back the block descriptor, it's
3659 * no big deal. If the user wants more than just
3660 * compression, though, and the device doesn't pass back the
3661 * block descriptor, we need to send another mode sense to
3662 * get the block descriptor.
3664 if ((mode_hdr->blk_desc_len == 0) &&
3665 (params_to_get & SA_PARAM_COMPRESSION) &&
3666 (params_to_get & ~(SA_PARAM_COMPRESSION))) {
3669 * Decrease the mode buffer length by the size of
3670 * the compression page, to make sure the data
3671 * there doesn't get overwritten.
3673 mode_buffer_len -= sizeof (sa_comp_t);
3676 * Now move the compression page that we presumably
3677 * got back down the memory chunk a little bit so
3678 * it doesn't get spammed.
3680 bcopy(&mode_hdr[0], &mode_hdr[1], sizeof (sa_comp_t));
3681 bzero(&mode_hdr[0], sizeof (mode_hdr[0]));
3684 * Now, we issue another mode sense and just ask
3685 * for the block descriptor, etc.
3688 scsi_mode_sense(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG, FALSE,
3689 SMS_PAGE_CTRL_CURRENT, SMS_VENDOR_SPECIFIC_PAGE,
3690 mode_buffer, mode_buffer_len, SSD_FULL_SIZE,
3693 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3694 softc->device_stats);
3697 goto sagetparamsexit;
3700 if (params_to_get & SA_PARAM_BLOCKSIZE)
3701 *blocksize = scsi_3btoul(mode_blk->blklen);
3703 if (params_to_get & SA_PARAM_NUMBLOCKS)
3704 *numblocks = scsi_3btoul(mode_blk->nblocks);
3706 if (params_to_get & SA_PARAM_BUFF_MODE)
3707 *buff_mode = mode_hdr->dev_spec & SMH_SA_BUF_MODE_MASK;
3709 if (params_to_get & SA_PARAM_DENSITY)
3710 *density = mode_blk->density;
3712 if (params_to_get & SA_PARAM_WP)
3713 *write_protect = (mode_hdr->dev_spec & SMH_SA_WP)? TRUE : FALSE;
3715 if (params_to_get & SA_PARAM_SPEED)
3716 *speed = mode_hdr->dev_spec & SMH_SA_SPEED_MASK;
3718 if (params_to_get & SA_PARAM_COMPRESSION) {
3719 sa_comp_t *ntcs = (sa_comp_t *) &mode_blk[1];
3720 if (cpage == SA_DATA_COMPRESSION_PAGE) {
3721 struct scsi_data_compression_page *cp = &ntcs->dcomp;
3723 (cp->dce_and_dcc & SA_DCP_DCC)? TRUE : FALSE;
3725 (cp->dce_and_dcc & SA_DCP_DCE)? TRUE : FALSE;
3726 *comp_algorithm = scsi_4btoul(cp->comp_algorithm);
3728 struct scsi_dev_conf_page *cp = &ntcs->dconf;
3730 * We don't really know whether this device supports
3731 * Data Compression if the algorithm field is
3732 * zero. Just say we do.
3734 *comp_supported = TRUE;
3736 (cp->sel_comp_alg != SA_COMP_NONE)? TRUE : FALSE;
3737 *comp_algorithm = cp->sel_comp_alg;
3740 bcopy(ntcs, tcs, sizeof (sa_comp_t));
3743 if ((params_to_get & SA_PARAM_DENSITY_EXT)
3744 && (softc->scsi_rev >= SCSI_REV_SPC)) {
3747 for (i = 0; i < SA_DENSITY_TYPES; i++) {
3748 scsi_report_density_support(&ccb->csio,
3751 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3752 /*media*/ softc->density_type_bits[i] & SRDS_MEDIA,
3753 /*medium_type*/ softc->density_type_bits[i] &
3755 /*data_ptr*/ softc->density_info[i],
3756 /*length*/ sizeof(softc->density_info[i]),
3757 /*sense_len*/ SSD_FULL_SIZE,
3758 /*timeout*/ REP_DENSITY_TIMEOUT);
3759 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3760 softc->device_stats);
3761 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3764 * Some tape drives won't support this command at
3765 * all, but hopefully we'll minimize that with the
3766 * check for SPC or greater support above. If they
3767 * don't support the default report (neither the
3768 * MEDIA or MEDIUM_TYPE bits set), then there is
3769 * really no point in continuing on to look for
3773 || (status != CAM_REQ_CMP)) {
3775 softc->density_info_valid[i] = 0;
3776 if (softc->density_type_bits[i] == 0)
3781 softc->density_info_valid[i] = ccb->csio.dxfer_len -
3787 * Get logical block protection parameters if the drive supports it.
3789 if ((params_to_get & SA_PARAM_LBP)
3790 && (softc->flags & SA_FLAG_PROTECT_SUPP)) {
3791 struct scsi_mode_header_10 *mode10_hdr;
3792 struct scsi_control_data_prot_subpage *dp_page;
3793 struct scsi_mode_sense_10 *cdb;
3794 struct sa_prot_state *prot;
3795 int dp_len, returned_len;
3798 dp_size = sizeof(*dp_page);
3800 dp_len = sizeof(*mode10_hdr) + dp_size;
3801 mode10_hdr = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO);
3802 if (mode10_hdr == NULL) {
3804 goto sagetparamsexit;
3807 scsi_mode_sense_len(&ccb->csio,
3810 /*tag_action*/ MSG_SIMPLE_Q_TAG,
3812 /*page_code*/ (prot_changeable == 0) ?
3813 SMS_PAGE_CTRL_CURRENT :
3814 SMS_PAGE_CTRL_CHANGEABLE,
3815 /*page*/ SMS_CONTROL_MODE_PAGE,
3816 /*param_buf*/ (uint8_t *)mode10_hdr,
3817 /*param_len*/ dp_len,
3818 /*minimum_cmd_size*/ 10,
3819 /*sense_len*/ SSD_FULL_SIZE,
3820 /*timeout*/ SCSIOP_TIMEOUT);
3822 * XXX KDM we need to be able to set the subpage in the
3825 cdb = (struct scsi_mode_sense_10 *)ccb->csio.cdb_io.cdb_bytes;
3826 cdb->subpage = SA_CTRL_DP_SUBPAGE_CODE;
3828 error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT,
3829 softc->device_stats);
3831 free(mode10_hdr, M_SCSISA);
3832 goto sagetparamsexit;
3835 status = ccb->ccb_h.status & CAM_STATUS_MASK;
3836 if (status != CAM_REQ_CMP) {
3838 free(mode10_hdr, M_SCSISA);
3839 goto sagetparamsexit;
3843 * The returned data length at least has to be long enough
3844 * for us to look at length in the mode page header.
3846 returned_len = ccb->csio.dxfer_len - ccb->csio.resid;
3847 if (returned_len < sizeof(mode10_hdr->data_length)) {
3849 free(mode10_hdr, M_SCSISA);
3850 goto sagetparamsexit;
3853 returned_len = min(returned_len,
3854 sizeof(mode10_hdr->data_length) +
3855 scsi_2btoul(mode10_hdr->data_length));
3857 dp_page = (struct scsi_control_data_prot_subpage *)
3861 * We also have to have enough data to include the prot_bits
3864 if (returned_len < (sizeof(*mode10_hdr) +
3865 __offsetof(struct scsi_control_data_prot_subpage, prot_bits)
3866 + sizeof(dp_page->prot_bits))) {
3868 free(mode10_hdr, M_SCSISA);
3869 goto sagetparamsexit;
3872 prot = &softc->prot_info.cur_prot_state;
3873 prot->prot_method = dp_page->prot_method;
3874 prot->pi_length = dp_page->pi_length &
3875 SA_CTRL_DP_PI_LENGTH_MASK;
3876 prot->lbp_w = (dp_page->prot_bits & SA_CTRL_DP_LBP_W) ? 1 :0;
3877 prot->lbp_r = (dp_page->prot_bits & SA_CTRL_DP_LBP_R) ? 1 :0;
3878 prot->rbdp = (dp_page->prot_bits & SA_CTRL_DP_RBDP) ? 1 :0;
3879 prot->initialized = 1;
3881 if (prot_page != NULL)
3882 bcopy(dp_page, prot_page, min(sizeof(*prot_page),
3885 free(mode10_hdr, M_SCSISA);
3888 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
3890 char *xyz = mode_buffer;
3891 xpt_print_path(periph->path);
3892 printf("Mode Sense Data=");
3893 for (idx = 0; idx < mode_buffer_len; idx++)
3894 printf(" 0x%02x", xyz[idx] & 0xff);
3900 xpt_release_ccb(ccb);
3901 free(mode_buffer, M_SCSISA);
3906 * Set protection information to the pending protection information stored
3910 sasetprot(struct cam_periph *periph, struct sa_prot_state *new_prot)
3912 struct sa_softc *softc;
3913 struct scsi_control_data_prot_subpage *dp_page, *dp_changeable;
3914 struct scsi_mode_header_10 *mode10_hdr, *mode10_changeable;
3916 uint8_t current_speed;
3917 size_t dp_size, dp_page_length;
3918 int dp_len, buff_mode;
3921 softc = (struct sa_softc *)periph->softc;
3923 mode10_changeable = NULL;
3927 * Start off with the size set to the actual length of the page
3928 * that we have defined.
3930 dp_size = sizeof(*dp_changeable);
3931 dp_page_length = dp_size -
3932 __offsetof(struct scsi_control_data_prot_subpage, prot_method);
3936 dp_len = sizeof(*mode10_changeable) + dp_size;
3937 mode10_changeable = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO);
3938 if (mode10_changeable == NULL) {
3944 (struct scsi_control_data_prot_subpage *)&mode10_changeable[1];
3947 * First get the data protection page changeable parameters mask.
3948 * We need to know which parameters the drive supports changing.
3949 * We also need to know what the drive claims that its page length
3950 * is. The reason is that IBM drives in particular are very picky
3951 * about the page length. They want it (the length set in the
3952 * page structure itself) to be 28 bytes, and they want the
3953 * parameter list length specified in the mode select header to be
3954 * 40 bytes. So, to work with IBM drives as well as any other tape
3955 * drive, find out what the drive claims the page length is, and
3956 * make sure that we match that.
3958 error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP,
3959 NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL,
3960 NULL, NULL, dp_changeable, dp_size, /*prot_changeable*/ 1);
3964 if (scsi_2btoul(dp_changeable->length) > dp_page_length) {
3965 dp_page_length = scsi_2btoul(dp_changeable->length);
3966 dp_size = dp_page_length +
3967 __offsetof(struct scsi_control_data_prot_subpage,
3969 free(mode10_changeable, M_SCSISA);
3970 mode10_changeable = NULL;
3974 mode10_hdr = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO);
3975 if (mode10_hdr == NULL) {
3980 dp_page = (struct scsi_control_data_prot_subpage *)&mode10_hdr[1];
3983 * Now grab the actual current settings in the page.
3985 error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP,
3986 NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL,
3987 NULL, NULL, dp_page, dp_size, /*prot_changeable*/ 0);
3991 /* These two fields need to be 0 for MODE SELECT */
3992 scsi_ulto2b(0, mode10_hdr->data_length);
3993 mode10_hdr->medium_type = 0;
3994 /* We are not including a block descriptor */
3995 scsi_ulto2b(0, mode10_hdr->blk_desc_len);
3997 mode10_hdr->dev_spec = current_speed;
3998 /* if set, set single-initiator buffering mode */
3999 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
4000 mode10_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
4004 * For each field, make sure that the drive allows changing it
4005 * before bringing in the user's setting.
4007 if (dp_changeable->prot_method != 0)
4008 dp_page->prot_method = new_prot->prot_method;
4010 if (dp_changeable->pi_length & SA_CTRL_DP_PI_LENGTH_MASK) {
4011 dp_page->pi_length &= ~SA_CTRL_DP_PI_LENGTH_MASK;
4012 dp_page->pi_length |= (new_prot->pi_length &
4013 SA_CTRL_DP_PI_LENGTH_MASK);
4015 if (dp_changeable->prot_bits & SA_CTRL_DP_LBP_W) {
4016 if (new_prot->lbp_w)
4017 dp_page->prot_bits |= SA_CTRL_DP_LBP_W;
4019 dp_page->prot_bits &= ~SA_CTRL_DP_LBP_W;
4022 if (dp_changeable->prot_bits & SA_CTRL_DP_LBP_R) {
4023 if (new_prot->lbp_r)
4024 dp_page->prot_bits |= SA_CTRL_DP_LBP_R;
4026 dp_page->prot_bits &= ~SA_CTRL_DP_LBP_R;
4029 if (dp_changeable->prot_bits & SA_CTRL_DP_RBDP) {
4031 dp_page->prot_bits |= SA_CTRL_DP_RBDP;
4033 dp_page->prot_bits &= ~SA_CTRL_DP_RBDP;
4036 ccb = cam_periph_getccb(periph, 1);
4038 scsi_mode_select_len(&ccb->csio,
4041 /*tag_action*/ MSG_SIMPLE_Q_TAG,
4042 /*scsi_page_fmt*/ TRUE,
4043 /*save_pages*/ FALSE,
4044 /*param_buf*/ (uint8_t *)mode10_hdr,
4045 /*param_len*/ dp_len,
4046 /*minimum_cmd_size*/ 10,
4047 /*sense_len*/ SSD_FULL_SIZE,
4048 /*timeout*/ SCSIOP_TIMEOUT);
4050 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4054 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4060 * The operation was successful. We could just copy the settings
4061 * the user requested, but just in case the drive ignored some of
4062 * our settings, let's ask for status again.
4064 error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP,
4065 NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL,
4066 NULL, NULL, dp_page, dp_size, 0);
4070 xpt_release_ccb(ccb);
4071 free(mode10_hdr, M_SCSISA);
4072 free(mode10_changeable, M_SCSISA);
4077 * The purpose of this function is to set one of four different parameters
4081 * - compression / compression algorithm
4084 * The assumption is that this will be called from saioctl(), and therefore
4085 * from a process context. Thus the waiting malloc calls below. If that
4086 * assumption ever changes, the malloc calls should be changed to be
4089 * Any or all of the four parameters may be set when this function is
4090 * called. It should handle setting more than one parameter at once.
4093 sasetparams(struct cam_periph *periph, sa_params params_to_set,
4094 u_int32_t blocksize, u_int8_t density, u_int32_t calg,
4095 u_int32_t sense_flags)
4097 struct sa_softc *softc;
4098 u_int32_t current_blocksize;
4099 u_int32_t current_calg;
4100 u_int8_t current_density;
4101 u_int8_t current_speed;
4102 int comp_enabled, comp_supported;
4104 int mode_buffer_len;
4105 struct scsi_mode_header_6 *mode_hdr;
4106 struct scsi_mode_blk_desc *mode_blk;
4107 sa_comp_t *ccomp, *cpage;
4109 union ccb *ccb = NULL;
4112 softc = (struct sa_softc *)periph->softc;
4114 ccomp = malloc(sizeof (sa_comp_t), M_SCSISA, M_NOWAIT);
4119 * Since it doesn't make sense to set the number of blocks, or
4120 * write protection, we won't try to get the current value. We
4121 * always want to get the blocksize, so we can set it back to the
4124 error = sagetparams(periph,
4125 params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED,
4126 ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL,
4127 ¤t_speed, &comp_supported, &comp_enabled,
4128 ¤t_calg, ccomp, NULL, 0, 0);
4131 free(ccomp, M_SCSISA);
4135 mode_buffer_len = sizeof(*mode_hdr) + sizeof(*mode_blk);
4136 if (params_to_set & SA_PARAM_COMPRESSION)
4137 mode_buffer_len += sizeof (sa_comp_t);
4139 mode_buffer = malloc(mode_buffer_len, M_SCSISA, M_NOWAIT | M_ZERO);
4140 if (mode_buffer == NULL) {
4141 free(ccomp, M_SCSISA);
4145 mode_hdr = (struct scsi_mode_header_6 *)mode_buffer;
4146 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
4148 ccb = cam_periph_getccb(periph, 1);
4152 if (params_to_set & SA_PARAM_COMPRESSION) {
4154 cpage = (sa_comp_t *)&mode_blk[1];
4156 cpage = (sa_comp_t *)&mode_hdr[1];
4158 bcopy(ccomp, cpage, sizeof (sa_comp_t));
4159 cpage->hdr.pagecode &= ~0x80;
4164 * If the caller wants us to set the blocksize, use the one they
4165 * pass in. Otherwise, use the blocksize we got back from the
4166 * mode select above.
4169 if (params_to_set & SA_PARAM_BLOCKSIZE)
4170 scsi_ulto3b(blocksize, mode_blk->blklen);
4172 scsi_ulto3b(current_blocksize, mode_blk->blklen);
4175 * Set density if requested, else preserve old density.
4176 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
4177 * devices, else density we've latched up in our softc.
4179 if (params_to_set & SA_PARAM_DENSITY) {
4180 mode_blk->density = density;
4181 } else if (softc->scsi_rev > SCSI_REV_CCS) {
4182 mode_blk->density = SCSI_SAME_DENSITY;
4184 mode_blk->density = softc->media_density;
4189 * For mode selects, these two fields must be zero.
4191 mode_hdr->data_length = 0;
4192 mode_hdr->medium_type = 0;
4194 /* set the speed to the current value */
4195 mode_hdr->dev_spec = current_speed;
4197 /* if set, set single-initiator buffering mode */
4198 if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) {
4199 mode_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF;
4203 mode_hdr->blk_desc_len = sizeof(struct scsi_mode_blk_desc);
4205 mode_hdr->blk_desc_len = 0;
4208 * First, if the user wants us to set the compression algorithm or
4209 * just turn compression on, check to make sure that this drive
4210 * supports compression.
4212 if (params_to_set & SA_PARAM_COMPRESSION) {
4214 * If the compression algorithm is 0, disable compression.
4215 * If the compression algorithm is non-zero, enable
4216 * compression and set the compression type to the
4217 * specified compression algorithm, unless the algorithm is
4218 * MT_COMP_ENABLE. In that case, we look at the
4219 * compression algorithm that is currently set and if it is
4220 * non-zero, we leave it as-is. If it is zero, and we have
4221 * saved a compression algorithm from a time when
4222 * compression was enabled before, set the compression to
4225 switch (ccomp->hdr.pagecode & ~0x80) {
4226 case SA_DEVICE_CONFIGURATION_PAGE:
4228 struct scsi_dev_conf_page *dcp = &cpage->dconf;
4230 dcp->sel_comp_alg = SA_COMP_NONE;
4233 if (calg != MT_COMP_ENABLE) {
4234 dcp->sel_comp_alg = calg;
4235 } else if (dcp->sel_comp_alg == SA_COMP_NONE &&
4236 softc->saved_comp_algorithm != 0) {
4237 dcp->sel_comp_alg = softc->saved_comp_algorithm;
4241 case SA_DATA_COMPRESSION_PAGE:
4242 if (ccomp->dcomp.dce_and_dcc & SA_DCP_DCC) {
4243 struct scsi_data_compression_page *dcp = &cpage->dcomp;
4246 * Disable compression, but leave the
4247 * decompression and the capability bit
4250 dcp->dce_and_dcc = SA_DCP_DCC;
4251 dcp->dde_and_red |= SA_DCP_DDE;
4254 /* enable compression && decompression */
4255 dcp->dce_and_dcc = SA_DCP_DCE | SA_DCP_DCC;
4256 dcp->dde_and_red |= SA_DCP_DDE;
4258 * If there, use compression algorithm from caller.
4259 * Otherwise, if there's a saved compression algorithm
4260 * and there is no current algorithm, use the saved
4261 * algorithm. Else parrot back what we got and hope
4264 if (calg != MT_COMP_ENABLE) {
4265 scsi_ulto4b(calg, dcp->comp_algorithm);
4266 scsi_ulto4b(calg, dcp->decomp_algorithm);
4267 } else if (scsi_4btoul(dcp->comp_algorithm) == 0 &&
4268 softc->saved_comp_algorithm != 0) {
4269 scsi_ulto4b(softc->saved_comp_algorithm,
4270 dcp->comp_algorithm);
4271 scsi_ulto4b(softc->saved_comp_algorithm,
4272 dcp->decomp_algorithm);
4277 * Compression does not appear to be supported-
4278 * at least via the DATA COMPRESSION page. It
4279 * would be too much to ask us to believe that
4280 * the page itself is supported, but incorrectly
4281 * reports an ability to manipulate data compression,
4282 * so we'll assume that this device doesn't support
4283 * compression. We can just fall through for that.
4288 * The drive doesn't seem to support compression,
4289 * so turn off the set compression bit.
4291 params_to_set &= ~SA_PARAM_COMPRESSION;
4292 xpt_print(periph->path,
4293 "device does not seem to support compression\n");
4296 * If that was the only thing the user wanted us to set,
4297 * clean up allocated resources and return with
4298 * 'operation not supported'.
4300 if (params_to_set == SA_PARAM_NONE) {
4301 free(mode_buffer, M_SCSISA);
4302 xpt_release_ccb(ccb);
4307 * That wasn't the only thing the user wanted us to set.
4308 * So, decrease the stated mode buffer length by the
4309 * size of the compression mode page.
4311 mode_buffer_len -= sizeof(sa_comp_t);
4315 /* It is safe to retry this operation */
4316 scsi_mode_select(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG,
4317 (params_to_set & SA_PARAM_COMPRESSION)? TRUE : FALSE,
4318 FALSE, mode_buffer, mode_buffer_len, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
4320 error = cam_periph_runccb(ccb, saerror, 0,
4321 sense_flags, softc->device_stats);
4323 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
4325 char *xyz = mode_buffer;
4326 xpt_print_path(periph->path);
4327 printf("Err%d, Mode Select Data=", error);
4328 for (idx = 0; idx < mode_buffer_len; idx++)
4329 printf(" 0x%02x", xyz[idx] & 0xff);
4336 * If we can, try without setting density/blocksize.
4339 if ((params_to_set &
4340 (SA_PARAM_DENSITY|SA_PARAM_BLOCKSIZE)) == 0) {
4345 mode_blk = (struct scsi_mode_blk_desc *)&mode_hdr[1];
4346 cpage = (sa_comp_t *)&mode_blk[1];
4350 * If we were setting the blocksize, and that failed, we
4351 * want to set it to its original value. If we weren't
4352 * setting the blocksize, we don't want to change it.
4354 scsi_ulto3b(current_blocksize, mode_blk->blklen);
4357 * Set density if requested, else preserve old density.
4358 * SCSI_SAME_DENSITY only applies to SCSI-2 or better
4359 * devices, else density we've latched up in our softc.
4361 if (params_to_set & SA_PARAM_DENSITY) {
4362 mode_blk->density = current_density;
4363 } else if (softc->scsi_rev > SCSI_REV_CCS) {
4364 mode_blk->density = SCSI_SAME_DENSITY;
4366 mode_blk->density = softc->media_density;
4369 if (params_to_set & SA_PARAM_COMPRESSION)
4370 bcopy(ccomp, cpage, sizeof (sa_comp_t));
4373 * The retry count is the only CCB field that might have been
4374 * changed that we care about, so reset it back to 1.
4376 ccb->ccb_h.retry_count = 1;
4377 cam_periph_runccb(ccb, saerror, 0, sense_flags,
4378 softc->device_stats);
4381 xpt_release_ccb(ccb);
4384 free(ccomp, M_SCSISA);
4386 if (params_to_set & SA_PARAM_COMPRESSION) {
4388 softc->flags &= ~SA_FLAG_COMP_ENABLED;
4390 * Even if we get an error setting compression,
4391 * do not say that we don't support it. We could
4392 * have been wrong, or it may be media specific.
4393 * softc->flags &= ~SA_FLAG_COMP_SUPP;
4395 softc->saved_comp_algorithm = softc->comp_algorithm;
4396 softc->comp_algorithm = 0;
4398 softc->flags |= SA_FLAG_COMP_ENABLED;
4399 softc->comp_algorithm = calg;
4403 free(mode_buffer, M_SCSISA);
4408 saextget(struct cdev *dev, struct cam_periph *periph, struct sbuf *sb,
4413 struct sa_softc *softc;
4416 struct ccb_getdev cgd;
4418 softc = (struct sa_softc *)periph->softc;
4422 error = sagetparams_common(dev, periph);
4424 goto extget_bailout;
4425 if (!SA_IS_CTRL(dev) && !softc->open_pending_mount)
4429 SASBADDNODE(sb, indent, mtextget);
4431 * Basic CAM peripheral information.
4433 SASBADDVARSTR(sb, indent, periph->periph_name, %s, periph_name,
4434 strlen(periph->periph_name) + 1);
4435 SASBADDUINT(sb, indent, periph->unit_number, %u, unit_number);
4436 xpt_setup_ccb(&cgd.ccb_h,
4438 CAM_PRIORITY_NORMAL);
4439 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
4440 xpt_action((union ccb *)&cgd);
4441 if ((cgd.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
4442 g->status = MT_EXT_GET_ERROR;
4443 snprintf(g->error_str, sizeof(g->error_str),
4444 "Error %#x returned for XPT_GDEV_TYPE CCB",
4446 goto extget_bailout;
4449 cam_strvis(tmpstr, cgd.inq_data.vendor,
4450 sizeof(cgd.inq_data.vendor), sizeof(tmpstr));
4451 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, vendor,
4452 sizeof(cgd.inq_data.vendor) + 1, "SCSI Vendor ID");
4454 cam_strvis(tmpstr, cgd.inq_data.product,
4455 sizeof(cgd.inq_data.product), sizeof(tmpstr));
4456 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, product,
4457 sizeof(cgd.inq_data.product) + 1, "SCSI Product ID");
4459 cam_strvis(tmpstr, cgd.inq_data.revision,
4460 sizeof(cgd.inq_data.revision), sizeof(tmpstr));
4461 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, revision,
4462 sizeof(cgd.inq_data.revision) + 1, "SCSI Revision");
4464 if (cgd.serial_num_len > 0) {
4471 if (cgd.serial_num_len > sizeof(tmpstr)) {
4472 ts2_len = cgd.serial_num_len + 1;
4474 tmpstr2 = malloc(ts2_len, M_SCSISA, M_NOWAIT | M_ZERO);
4476 * The 80 characters allocated on the stack above
4477 * will handle the vast majority of serial numbers.
4478 * If we run into one that is larger than that, and
4479 * we can't malloc the length without blocking,
4480 * bail out with an out of memory error.
4482 if (tmpstr2 == NULL) {
4484 goto extget_bailout;
4487 ts2_len = sizeof(tmpstr);
4492 cam_strvis(tmpstr2, cgd.serial_num, cgd.serial_num_len,
4495 SASBADDVARSTRDESC(sb, indent, tmpstr2, %s, serial_num,
4496 (ssize_t)cgd.serial_num_len + 1, "Serial Number");
4497 if (ts2_malloc != 0)
4498 free(tmpstr2, M_SCSISA);
4501 * We return a serial_num element in any case, but it will
4502 * be empty if the device has no serial number.
4505 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, serial_num,
4506 (ssize_t)0, "Serial Number");
4509 SASBADDUINTDESC(sb, indent, softc->maxio, %u, maxio,
4510 "Maximum I/O size allowed by driver and controller");
4512 SASBADDUINTDESC(sb, indent, softc->cpi_maxio, %u, cpi_maxio,
4513 "Maximum I/O size reported by controller");
4515 SASBADDUINTDESC(sb, indent, softc->max_blk, %u, max_blk,
4516 "Maximum block size supported by tape drive and media");
4518 SASBADDUINTDESC(sb, indent, softc->min_blk, %u, min_blk,
4519 "Minimum block size supported by tape drive and media");
4521 SASBADDUINTDESC(sb, indent, softc->blk_gran, %u, blk_gran,
4522 "Block granularity supported by tape drive and media");
4524 maxio_tmp = min(softc->max_blk, softc->maxio);
4526 SASBADDUINTDESC(sb, indent, maxio_tmp, %u, max_effective_iosize,
4527 "Maximum possible I/O size");
4529 SASBADDINTDESC(sb, indent, softc->flags & SA_FLAG_FIXED ? 1 : 0, %d,
4530 fixed_mode, "Set to 1 for fixed block mode, 0 for variable block");
4533 * XXX KDM include SIM, bus, target, LUN?
4535 if (softc->flags & SA_FLAG_COMP_UNSUPP)
4539 SASBADDINTDESC(sb, indent, tmpint, %d, compression_supported,
4540 "Set to 1 if compression is supported, 0 if not");
4541 if (softc->flags & SA_FLAG_COMP_ENABLED)
4545 SASBADDINTDESC(sb, indent, tmpint, %d, compression_enabled,
4546 "Set to 1 if compression is enabled, 0 if not");
4547 SASBADDUINTDESC(sb, indent, softc->comp_algorithm, %u,
4548 compression_algorithm, "Numeric compression algorithm");
4550 safillprot(softc, &indent, sb);
4552 SASBADDUINTDESC(sb, indent, softc->media_blksize, %u,
4553 media_blocksize, "Block size reported by drive or set by user");
4554 SASBADDINTDESC(sb, indent, (intmax_t)softc->fileno, %jd,
4555 calculated_fileno, "Calculated file number, -1 if unknown");
4556 SASBADDINTDESC(sb, indent, (intmax_t)softc->blkno, %jd,
4557 calculated_rel_blkno, "Calculated block number relative to file, "
4558 "set to -1 if unknown");
4559 SASBADDINTDESC(sb, indent, (intmax_t)softc->rep_fileno, %jd,
4560 reported_fileno, "File number reported by drive, -1 if unknown");
4561 SASBADDINTDESC(sb, indent, (intmax_t)softc->rep_blkno, %jd,
4562 reported_blkno, "Block number relative to BOP/BOT reported by "
4563 "drive, -1 if unknown");
4564 SASBADDINTDESC(sb, indent, (intmax_t)softc->partition, %jd,
4565 partition, "Current partition number, 0 is the default");
4566 SASBADDINTDESC(sb, indent, softc->bop, %d, bop,
4567 "Set to 1 if drive is at the beginning of partition/tape, 0 if "
4568 "not, -1 if unknown");
4569 SASBADDINTDESC(sb, indent, softc->eop, %d, eop,
4570 "Set to 1 if drive is past early warning, 0 if not, -1 if unknown");
4571 SASBADDINTDESC(sb, indent, softc->bpew, %d, bpew,
4572 "Set to 1 if drive is past programmable early warning, 0 if not, "
4574 SASBADDINTDESC(sb, indent, (intmax_t)softc->last_io_resid, %jd,
4575 residual, "Residual for the last I/O");
4577 * XXX KDM should we send a string with the current driver
4578 * status already decoded instead of a numeric value?
4580 SASBADDINTDESC(sb, indent, softc->dsreg, %d, dsreg,
4581 "Current state of the driver");
4583 safilldensitysb(softc, &indent, sb);
4585 SASBENDNODE(sb, indent, mtextget);
4593 saparamget(struct sa_softc *softc, struct sbuf *sb)
4598 SASBADDNODE(sb, indent, mtparamget);
4599 SASBADDINTDESC(sb, indent, softc->sili, %d, sili,
4600 "Suppress an error on underlength variable reads");
4601 SASBADDINTDESC(sb, indent, softc->eot_warn, %d, eot_warn,
4602 "Return an error to warn that end of tape is approaching");
4603 safillprot(softc, &indent, sb);
4604 SASBENDNODE(sb, indent, mtparamget);
4610 saprevent(struct cam_periph *periph, int action)
4612 struct sa_softc *softc;
4616 softc = (struct sa_softc *)periph->softc;
4618 if ((action == PR_ALLOW) && (softc->flags & SA_FLAG_TAPE_LOCKED) == 0)
4620 if ((action == PR_PREVENT) && (softc->flags & SA_FLAG_TAPE_LOCKED) != 0)
4624 * We can be quiet about illegal requests.
4626 if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) {
4631 ccb = cam_periph_getccb(periph, 1);
4633 /* It is safe to retry this operation */
4634 scsi_prevent(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, action,
4635 SSD_FULL_SIZE, SCSIOP_TIMEOUT);
4637 error = cam_periph_runccb(ccb, saerror, 0, sf, softc->device_stats);
4639 if (action == PR_ALLOW)
4640 softc->flags &= ~SA_FLAG_TAPE_LOCKED;
4642 softc->flags |= SA_FLAG_TAPE_LOCKED;
4645 xpt_release_ccb(ccb);
4649 sarewind(struct cam_periph *periph)
4652 struct sa_softc *softc;
4655 softc = (struct sa_softc *)periph->softc;
4657 ccb = cam_periph_getccb(periph, 1);
4659 /* It is safe to retry this operation */
4660 scsi_rewind(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG, FALSE,
4661 SSD_FULL_SIZE, REWIND_TIMEOUT);
4663 softc->dsreg = MTIO_DSREG_REW;
4664 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4665 softc->dsreg = MTIO_DSREG_REST;
4667 xpt_release_ccb(ccb);
4669 softc->partition = softc->fileno = softc->blkno = (daddr_t) 0;
4670 softc->rep_fileno = softc->rep_blkno = (daddr_t) 0;
4672 softc->fileno = softc->blkno = (daddr_t) -1;
4673 softc->partition = (daddr_t) -1;
4674 softc->rep_fileno = softc->rep_blkno = (daddr_t) -1;
4680 saspace(struct cam_periph *periph, int count, scsi_space_code code)
4683 struct sa_softc *softc;
4686 softc = (struct sa_softc *)periph->softc;
4688 ccb = cam_periph_getccb(periph, 1);
4690 /* This cannot be retried */
4692 scsi_space(&ccb->csio, 0, NULL, MSG_SIMPLE_Q_TAG, code, count,
4693 SSD_FULL_SIZE, SPACE_TIMEOUT);
4696 * Clear residual because we will be using it.
4698 softc->last_ctl_resid = 0;
4700 softc->dsreg = (count < 0)? MTIO_DSREG_REV : MTIO_DSREG_FWD;
4701 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4702 softc->dsreg = MTIO_DSREG_REST;
4704 xpt_release_ccb(ccb);
4707 * If a spacing operation has failed, we need to invalidate
4710 * If the spacing operation was setmarks or to end of recorded data,
4711 * we no longer know our relative position.
4713 * If the spacing operations was spacing files in reverse, we
4714 * take account of the residual, but still check against less
4715 * than zero- if we've gone negative, we must have hit BOT.
4717 * If the spacing operations was spacing records in reverse and
4718 * we have a residual, we've either hit BOT or hit a filemark.
4719 * In the former case, we know our new record number (0). In
4720 * the latter case, we have absolutely no idea what the real
4721 * record number is- we've stopped between the end of the last
4722 * record in the previous file and the filemark that stopped
4723 * our spacing backwards.
4726 softc->fileno = softc->blkno = (daddr_t) -1;
4727 softc->rep_blkno = softc->partition = (daddr_t) -1;
4728 softc->rep_fileno = (daddr_t) -1;
4729 } else if (code == SS_SETMARKS || code == SS_EOD) {
4730 softc->fileno = softc->blkno = (daddr_t) -1;
4731 } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) {
4732 softc->fileno += (count - softc->last_ctl_resid);
4733 if (softc->fileno < 0) /* we must of hit BOT */
4736 } else if (code == SS_BLOCKS && softc->blkno != (daddr_t) -1) {
4737 softc->blkno += (count - softc->last_ctl_resid);
4739 if (softc->last_ctl_resid || softc->blkno < 0) {
4740 if (softc->fileno == 0) {
4743 softc->blkno = (daddr_t) -1;
4755 sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks, int immed)
4758 struct sa_softc *softc;
4761 softc = (struct sa_softc *)periph->softc;
4762 if (softc->open_rdonly)
4765 ccb = cam_periph_getccb(periph, 1);
4767 * Clear residual because we will be using it.
4769 softc->last_ctl_resid = 0;
4771 softc->dsreg = MTIO_DSREG_FMK;
4772 /* this *must* not be retried */
4773 scsi_write_filemarks(&ccb->csio, 0, NULL, MSG_SIMPLE_Q_TAG,
4774 immed, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT);
4775 softc->dsreg = MTIO_DSREG_REST;
4778 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4780 if (error == 0 && nmarks) {
4781 struct sa_softc *softc = (struct sa_softc *)periph->softc;
4782 nwm = nmarks - softc->last_ctl_resid;
4783 softc->filemarks += nwm;
4786 xpt_release_ccb(ccb);
4789 * Update relative positions (if we're doing that).
4792 softc->fileno = softc->blkno = softc->partition = (daddr_t) -1;
4793 } else if (softc->fileno != (daddr_t) -1) {
4794 softc->fileno += nwm;
4799 * Ask the tape drive for position information.
4804 * If we got valid position information, since we just wrote a file
4805 * mark, we know we're at the file mark and block 0 after that
4808 if (softc->rep_fileno != (daddr_t) -1) {
4809 softc->fileno = softc->rep_fileno;
4817 sagetpos(struct cam_periph *periph)
4820 struct scsi_tape_position_long_data long_pos;
4821 struct sa_softc *softc = (struct sa_softc *)periph->softc;
4824 if (softc->quirks & SA_QUIRK_NO_LONG_POS) {
4825 softc->rep_fileno = (daddr_t) -1;
4826 softc->rep_blkno = (daddr_t) -1;
4827 softc->bop = softc->eop = softc->bpew = -1;
4828 return (EOPNOTSUPP);
4831 bzero(&long_pos, sizeof(long_pos));
4833 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
4834 scsi_read_position_10(&ccb->csio,
4837 /*tag_action*/ MSG_SIMPLE_Q_TAG,
4838 /*service_action*/ SA_RPOS_LONG_FORM,
4839 /*data_ptr*/ (uint8_t *)&long_pos,
4840 /*length*/ sizeof(long_pos),
4841 /*sense_len*/ SSD_FULL_SIZE,
4842 /*timeout*/ SCSIOP_TIMEOUT);
4844 softc->dsreg = MTIO_DSREG_RBSY;
4845 error = cam_periph_runccb(ccb, saerror, 0, SF_QUIET_IR,
4846 softc->device_stats);
4847 softc->dsreg = MTIO_DSREG_REST;
4850 if (long_pos.flags & SA_RPOS_LONG_MPU) {
4852 * If the drive doesn't know what file mark it is
4853 * on, our calculated filemark isn't going to be
4856 softc->fileno = (daddr_t) -1;
4857 softc->rep_fileno = (daddr_t) -1;
4859 softc->fileno = softc->rep_fileno =
4860 scsi_8btou64(long_pos.logical_file_num);
4863 if (long_pos.flags & SA_RPOS_LONG_LONU) {
4864 softc->partition = (daddr_t) -1;
4865 softc->rep_blkno = (daddr_t) -1;
4867 * If the tape drive doesn't know its block
4868 * position, we can't claim to know it either.
4870 softc->blkno = (daddr_t) -1;
4872 softc->partition = scsi_4btoul(long_pos.partition);
4874 scsi_8btou64(long_pos.logical_object_num);
4876 if (long_pos.flags & SA_RPOS_LONG_BOP)
4881 if (long_pos.flags & SA_RPOS_LONG_EOP)
4886 if ((long_pos.flags & SA_RPOS_LONG_BPEW)
4887 || (softc->set_pews_status != 0)) {
4889 if (softc->set_pews_status > 0)
4890 softc->set_pews_status--;
4893 } else if (error == EINVAL) {
4895 * If this drive returned an invalid-request type error,
4896 * then it likely doesn't support the long form report.
4898 softc->quirks |= SA_QUIRK_NO_LONG_POS;
4902 softc->rep_fileno = softc->rep_blkno = (daddr_t) -1;
4903 softc->partition = (daddr_t) -1;
4904 softc->bop = softc->eop = softc->bpew = -1;
4907 xpt_release_ccb(ccb);
4913 sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr)
4915 struct scsi_tape_position_data loc;
4917 struct sa_softc *softc = (struct sa_softc *)periph->softc;
4921 * We try and flush any buffered writes here if we were writing
4922 * and we're trying to get hardware block position. It eats
4923 * up performance substantially, but I'm wary of drive firmware.
4925 * I think that *logical* block position is probably okay-
4926 * but hardware block position might have to wait for data
4927 * to hit media to be valid. Caveat Emptor.
4930 if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) {
4931 error = sawritefilemarks(periph, 0, 0, 0);
4932 if (error && error != EACCES)
4936 ccb = cam_periph_getccb(periph, 1);
4937 scsi_read_position(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG,
4938 hard, &loc, SSD_FULL_SIZE, SCSIOP_TIMEOUT);
4939 softc->dsreg = MTIO_DSREG_RBSY;
4940 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
4941 softc->dsreg = MTIO_DSREG_REST;
4944 if (loc.flags & SA_RPOS_UNCERTAIN) {
4945 error = EINVAL; /* nothing is certain */
4947 *blkptr = scsi_4btoul(loc.firstblk);
4951 xpt_release_ccb(ccb);
4956 sasetpos(struct cam_periph *periph, int hard, struct mtlocate *locate_info)
4959 struct sa_softc *softc;
4965 * We used to try and flush any buffered writes here.
4966 * Now we push this onto user applications to either
4967 * flush the pending writes themselves (via a zero count
4968 * WRITE FILEMARKS command) or they can trust their tape
4969 * drive to do this correctly for them.
4972 softc = (struct sa_softc *)periph->softc;
4973 ccb = cam_periph_getccb(periph, 1);
4975 cp = locate_info->flags & MT_LOCATE_FLAG_CHANGE_PART ? 1 : 0;
4976 immed = locate_info->flags & MT_LOCATE_FLAG_IMMED ? 1 : 0;
4979 * Determine whether we have to use LOCATE or LOCATE16. The hard
4980 * bit is only possible with LOCATE, but the new ioctls do not
4981 * allow setting that bit. So we can't get into the situation of
4982 * having the hard bit set with a block address that is larger than
4987 else if ((locate_info->dest_type != MT_LOCATE_DEST_OBJECT)
4988 || (locate_info->block_address_mode != MT_LOCATE_BAM_IMPLICIT)
4989 || (locate_info->logical_id > SA_SPOS_MAX_BLK))
4994 if (locate16 != 0) {
4995 scsi_locate_16(&ccb->csio,
4998 /*tag_action*/ MSG_SIMPLE_Q_TAG,
5001 /*dest_type*/ locate_info->dest_type,
5002 /*bam*/ locate_info->block_address_mode,
5003 /*partition*/ locate_info->partition,
5004 /*logical_id*/ locate_info->logical_id,
5005 /*sense_len*/ SSD_FULL_SIZE,
5006 /*timeout*/ SPACE_TIMEOUT);
5008 scsi_locate_10(&ccb->csio,
5011 /*tag_action*/ MSG_SIMPLE_Q_TAG,
5015 /*partition*/ locate_info->partition,
5016 /*block_address*/ locate_info->logical_id,
5017 /*sense_len*/ SSD_FULL_SIZE,
5018 /*timeout*/ SPACE_TIMEOUT);
5021 softc->dsreg = MTIO_DSREG_POS;
5022 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5023 softc->dsreg = MTIO_DSREG_REST;
5024 xpt_release_ccb(ccb);
5027 * We assume the calculated file and block numbers are unknown
5028 * unless we have enough information to populate them.
5030 softc->fileno = softc->blkno = (daddr_t) -1;
5033 * If the user requested changing the partition and the request
5034 * succeeded, note the partition.
5038 softc->partition = locate_info->partition;
5040 softc->partition = (daddr_t) -1;
5043 switch (locate_info->dest_type) {
5044 case MT_LOCATE_DEST_FILE:
5046 * This is the only case where we can reliably
5047 * calculate the file and block numbers.
5049 softc->fileno = locate_info->logical_id;
5052 case MT_LOCATE_DEST_OBJECT:
5053 case MT_LOCATE_DEST_SET:
5054 case MT_LOCATE_DEST_EOD:
5061 * Ask the drive for current position information.
5069 saretension(struct cam_periph *periph)
5072 struct sa_softc *softc;
5075 softc = (struct sa_softc *)periph->softc;
5077 ccb = cam_periph_getccb(periph, 1);
5079 /* It is safe to retry this operation */
5080 scsi_load_unload(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, FALSE,
5081 FALSE, TRUE, TRUE, SSD_FULL_SIZE, ERASE_TIMEOUT);
5083 softc->dsreg = MTIO_DSREG_TEN;
5084 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5085 softc->dsreg = MTIO_DSREG_REST;
5087 xpt_release_ccb(ccb);
5089 softc->partition = softc->fileno = softc->blkno = (daddr_t) 0;
5092 softc->partition = softc->fileno = softc->blkno = (daddr_t) -1;
5097 sareservereleaseunit(struct cam_periph *periph, int reserve)
5100 struct sa_softc *softc;
5103 softc = (struct sa_softc *)periph->softc;
5104 ccb = cam_periph_getccb(periph, 1);
5106 /* It is safe to retry this operation */
5107 scsi_reserve_release_unit(&ccb->csio, 2, NULL, MSG_SIMPLE_Q_TAG,
5108 FALSE, 0, SSD_FULL_SIZE, SCSIOP_TIMEOUT, reserve);
5109 softc->dsreg = MTIO_DSREG_RBSY;
5110 error = cam_periph_runccb(ccb, saerror, 0,
5111 SF_RETRY_UA | SF_NO_PRINT, softc->device_stats);
5112 softc->dsreg = MTIO_DSREG_REST;
5113 xpt_release_ccb(ccb);
5116 * If the error was Illegal Request, then the device doesn't support
5117 * RESERVE/RELEASE. This is not an error.
5119 if (error == EINVAL) {
5127 saloadunload(struct cam_periph *periph, int load)
5130 struct sa_softc *softc;
5133 softc = (struct sa_softc *)periph->softc;
5135 ccb = cam_periph_getccb(periph, 1);
5137 /* It is safe to retry this operation */
5138 scsi_load_unload(&ccb->csio, 5, NULL, MSG_SIMPLE_Q_TAG, FALSE,
5139 FALSE, FALSE, load, SSD_FULL_SIZE, REWIND_TIMEOUT);
5141 softc->dsreg = (load)? MTIO_DSREG_LD : MTIO_DSREG_UNL;
5142 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5143 softc->dsreg = MTIO_DSREG_REST;
5144 xpt_release_ccb(ccb);
5146 if (error || load == 0) {
5147 softc->partition = softc->fileno = softc->blkno = (daddr_t) -1;
5148 softc->rep_fileno = softc->rep_blkno = (daddr_t) -1;
5149 } else if (error == 0) {
5150 softc->partition = softc->fileno = softc->blkno = (daddr_t) 0;
5157 saerase(struct cam_periph *periph, int longerase)
5161 struct sa_softc *softc;
5164 softc = (struct sa_softc *)periph->softc;
5165 if (softc->open_rdonly)
5168 ccb = cam_periph_getccb(periph, 1);
5170 scsi_erase(&ccb->csio, 1, NULL, MSG_SIMPLE_Q_TAG, FALSE, longerase,
5171 SSD_FULL_SIZE, ERASE_TIMEOUT);
5173 softc->dsreg = MTIO_DSREG_ZER;
5174 error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats);
5175 softc->dsreg = MTIO_DSREG_REST;
5177 xpt_release_ccb(ccb);
5182 * Fill an sbuf with density data in XML format. This particular macro
5183 * works for multi-byte integer fields.
5185 * Note that 1 byte fields aren't supported here. The reason is that the
5186 * compiler does not evaluate the sizeof(), and assumes that any of the
5187 * sizes are possible for a given field. So passing in a multi-byte
5188 * field will result in a warning that the assignment makes an integer
5189 * from a pointer without a cast, if there is an assignment in the 1 byte
5192 #define SAFILLDENSSB(dens_data, sb, indent, field, desc_remain, \
5193 len_to_go, cur_offset, desc){ \
5194 size_t cur_field_len; \
5196 cur_field_len = sizeof(dens_data->field); \
5197 if (desc_remain < cur_field_len) { \
5198 len_to_go -= desc_remain; \
5199 cur_offset += desc_remain; \
5202 len_to_go -= cur_field_len; \
5203 cur_offset += cur_field_len; \
5204 desc_remain -= cur_field_len; \
5206 switch (sizeof(dens_data->field)) { \
5208 KASSERT(1 == 0, ("Programmer error, invalid 1 byte " \
5209 "field width for SAFILLDENSFIELD")); \
5212 SASBADDUINTDESC(sb, indent, \
5213 scsi_2btoul(dens_data->field), %u, field, desc); \
5216 SASBADDUINTDESC(sb, indent, \
5217 scsi_3btoul(dens_data->field), %u, field, desc); \
5220 SASBADDUINTDESC(sb, indent, \
5221 scsi_4btoul(dens_data->field), %u, field, desc); \
5224 SASBADDUINTDESC(sb, indent, \
5225 (uintmax_t)scsi_8btou64(dens_data->field), %ju, \
5233 * Fill an sbuf with density data in XML format. This particular macro
5234 * works for strings.
5236 #define SAFILLDENSSBSTR(dens_data, sb, indent, field, desc_remain, \
5237 len_to_go, cur_offset, desc){ \
5238 size_t cur_field_len; \
5241 cur_field_len = sizeof(dens_data->field); \
5242 if (desc_remain < cur_field_len) { \
5243 len_to_go -= desc_remain; \
5244 cur_offset += desc_remain; \
5247 len_to_go -= cur_field_len; \
5248 cur_offset += cur_field_len; \
5249 desc_remain -= cur_field_len; \
5251 cam_strvis(tmpstr, dens_data->field, \
5252 sizeof(dens_data->field), sizeof(tmpstr)); \
5253 SASBADDVARSTRDESC(sb, indent, tmpstr, %s, field, \
5254 strlen(tmpstr) + 1, desc); \
5258 * Fill an sbuf with density data descriptors.
5261 safilldenstypesb(struct sbuf *sb, int *indent, uint8_t *buf, int buf_len,
5264 struct scsi_density_hdr *hdr;
5266 int len_to_go, cur_offset;
5268 int num_reports, need_close;
5271 * We need at least the header length. Note that this isn't an
5272 * error, not all tape drives will have every data type.
5274 if (buf_len < sizeof(*hdr))
5278 hdr = (struct scsi_density_hdr *)buf;
5279 hdr_len = scsi_2btoul(hdr->length);
5280 len_to_go = min(buf_len - sizeof(*hdr), hdr_len);
5282 length_offset = __offsetof(struct scsi_density_data,
5285 length_offset = __offsetof(struct scsi_medium_type_data,
5288 cur_offset = sizeof(*hdr);
5293 while (len_to_go > length_offset) {
5294 struct scsi_density_data *dens_data;
5295 struct scsi_medium_type_data *type_data;
5297 size_t cur_field_len;
5303 dens_data =(struct scsi_density_data *)&buf[cur_offset];
5304 if (dens_data->byte2 & SDD_DLV)
5305 desc_remain = scsi_2btoul(dens_data->length);
5307 desc_remain = SDD_DEFAULT_LENGTH -
5310 type_data = (struct scsi_medium_type_data *)
5312 desc_remain = scsi_2btoul(type_data->length);
5315 len_to_go -= length_offset;
5316 desc_remain = min(desc_remain, len_to_go);
5317 cur_offset += length_offset;
5319 if (need_close != 0) {
5320 SASBENDNODE(sb, *indent, density_entry);
5323 SASBADDNODENUM(sb, *indent, density_entry, num_reports);
5328 SASBADDUINTDESC(sb, *indent,
5329 dens_data->primary_density_code, %u,
5330 primary_density_code, "Primary Density Code");
5331 SASBADDUINTDESC(sb, *indent,
5332 dens_data->secondary_density_code, %u,
5333 secondary_density_code, "Secondary Density Code");
5334 SASBADDUINTDESC(sb, *indent,
5335 dens_data->byte2 & ~SDD_DLV, %#x, density_flags,
5338 SAFILLDENSSB(dens_data, sb, *indent, bits_per_mm,
5339 desc_remain, len_to_go, cur_offset, "Bits per mm");
5340 SAFILLDENSSB(dens_data, sb, *indent, media_width,
5341 desc_remain, len_to_go, cur_offset, "Media width");
5342 SAFILLDENSSB(dens_data, sb, *indent, tracks,
5343 desc_remain, len_to_go, cur_offset,
5344 "Number of Tracks");
5345 SAFILLDENSSB(dens_data, sb, *indent, capacity,
5346 desc_remain, len_to_go, cur_offset, "Capacity");
5348 SAFILLDENSSBSTR(dens_data, sb, *indent, assigning_org,
5349 desc_remain, len_to_go, cur_offset,
5350 "Assigning Organization");
5352 SAFILLDENSSBSTR(dens_data, sb, *indent, density_name,
5353 desc_remain, len_to_go, cur_offset, "Density Name");
5355 SAFILLDENSSBSTR(dens_data, sb, *indent, description,
5356 desc_remain, len_to_go, cur_offset, "Description");
5360 SASBADDUINTDESC(sb, *indent, type_data->medium_type,
5361 %u, medium_type, "Medium Type");
5364 __offsetof(struct scsi_medium_type_data,
5366 __offsetof(struct scsi_medium_type_data,
5369 if (desc_remain < cur_field_len) {
5370 len_to_go -= desc_remain;
5371 cur_offset += desc_remain;
5374 len_to_go -= cur_field_len;
5375 cur_offset += cur_field_len;
5376 desc_remain -= cur_field_len;
5378 SASBADDINTDESC(sb, *indent,
5379 type_data->num_density_codes, %d,
5380 num_density_codes, "Number of Density Codes");
5381 SASBADDNODE(sb, *indent, density_code_list);
5382 for (i = 0; i < type_data->num_density_codes;
5384 SASBADDUINTDESC(sb, *indent,
5385 type_data->primary_density_codes[i], %u,
5386 density_code, "Density Code");
5388 SASBENDNODE(sb, *indent, density_code_list);
5390 SAFILLDENSSB(type_data, sb, *indent, media_width,
5391 desc_remain, len_to_go, cur_offset,
5393 SAFILLDENSSB(type_data, sb, *indent, medium_length,
5394 desc_remain, len_to_go, cur_offset,
5398 * Account for the two reserved bytes.
5400 cur_field_len = sizeof(type_data->reserved2);
5401 if (desc_remain < cur_field_len) {
5402 len_to_go -= desc_remain;
5403 cur_offset += desc_remain;
5406 len_to_go -= cur_field_len;
5407 cur_offset += cur_field_len;
5408 desc_remain -= cur_field_len;
5410 SAFILLDENSSBSTR(type_data, sb, *indent, assigning_org,
5411 desc_remain, len_to_go, cur_offset,
5412 "Assigning Organization");
5413 SAFILLDENSSBSTR(type_data, sb, *indent,
5414 medium_type_name, desc_remain, len_to_go,
5415 cur_offset, "Medium type name");
5416 SAFILLDENSSBSTR(type_data, sb, *indent, description,
5417 desc_remain, len_to_go, cur_offset, "Description");
5421 if (need_close != 0) {
5422 SASBENDNODE(sb, *indent, density_entry);
5430 * Fill an sbuf with density data information
5433 safilldensitysb(struct sa_softc *softc, int *indent, struct sbuf *sb)
5437 SASBADDNODE(sb, *indent, mtdensity);
5438 SASBADDUINTDESC(sb, *indent, softc->media_density, %u, media_density,
5439 "Current Medium Density");
5441 for (i = 0; i < SA_DENSITY_TYPES; i++) {
5444 if (softc->density_info_valid[i] == 0)
5447 SASBADDNODE(sb, *indent, density_report);
5448 if (softc->density_type_bits[i] & SRDS_MEDIUM_TYPE) {
5455 SASBADDINTDESC(sb, *indent, tmpint, %d, medium_type_report,
5456 "Medium type report");
5458 if (softc->density_type_bits[i] & SRDS_MEDIA)
5462 SASBADDINTDESC(sb, *indent, tmpint, %d, media_report,
5465 safilldenstypesb(sb, indent, softc->density_info[i],
5466 softc->density_info_valid[i], is_density);
5467 SASBENDNODE(sb, *indent, density_report);
5469 SASBENDNODE(sb, *indent, mtdensity);
5472 #endif /* _KERNEL */
5475 * Read tape block limits command.
5478 scsi_read_block_limits(struct ccb_scsiio *csio, u_int32_t retries,
5479 void (*cbfcnp)(struct cam_periph *, union ccb *),
5480 u_int8_t tag_action,
5481 struct scsi_read_block_limits_data *rlimit_buf,
5482 u_int8_t sense_len, u_int32_t timeout)
5484 struct scsi_read_block_limits *scsi_cmd;
5486 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
5487 (u_int8_t *)rlimit_buf, sizeof(*rlimit_buf), sense_len,
5488 sizeof(*scsi_cmd), timeout);
5490 scsi_cmd = (struct scsi_read_block_limits *)&csio->cdb_io.cdb_bytes;
5491 bzero(scsi_cmd, sizeof(*scsi_cmd));
5492 scsi_cmd->opcode = READ_BLOCK_LIMITS;
5496 scsi_sa_read_write(struct ccb_scsiio *csio, u_int32_t retries,
5497 void (*cbfcnp)(struct cam_periph *, union ccb *),
5498 u_int8_t tag_action, int readop, int sli,
5499 int fixed, u_int32_t length, u_int8_t *data_ptr,
5500 u_int32_t dxfer_len, u_int8_t sense_len, u_int32_t timeout)
5502 struct scsi_sa_rw *scsi_cmd;
5505 read = (readop & SCSI_RW_DIRMASK) == SCSI_RW_READ;
5507 scsi_cmd = (struct scsi_sa_rw *)&csio->cdb_io.cdb_bytes;
5508 scsi_cmd->opcode = read ? SA_READ : SA_WRITE;
5509 scsi_cmd->sli_fixed = 0;
5511 scsi_cmd->sli_fixed |= SAR_SLI;
5513 scsi_cmd->sli_fixed |= SARW_FIXED;
5514 scsi_ulto3b(length, scsi_cmd->length);
5515 scsi_cmd->control = 0;
5517 cam_fill_csio(csio, retries, cbfcnp, (read ? CAM_DIR_IN : CAM_DIR_OUT) |
5518 ((readop & SCSI_RW_BIO) != 0 ? CAM_DATA_BIO : 0),
5519 tag_action, data_ptr, dxfer_len, sense_len,
5520 sizeof(*scsi_cmd), timeout);
5524 scsi_load_unload(struct ccb_scsiio *csio, u_int32_t retries,
5525 void (*cbfcnp)(struct cam_periph *, union ccb *),
5526 u_int8_t tag_action, int immediate, int eot,
5527 int reten, int load, u_int8_t sense_len,
5530 struct scsi_load_unload *scsi_cmd;
5532 scsi_cmd = (struct scsi_load_unload *)&csio->cdb_io.cdb_bytes;
5533 bzero(scsi_cmd, sizeof(*scsi_cmd));
5534 scsi_cmd->opcode = LOAD_UNLOAD;
5536 scsi_cmd->immediate = SLU_IMMED;
5538 scsi_cmd->eot_reten_load |= SLU_EOT;
5540 scsi_cmd->eot_reten_load |= SLU_RETEN;
5542 scsi_cmd->eot_reten_load |= SLU_LOAD;
5544 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
5545 NULL, 0, sense_len, sizeof(*scsi_cmd), timeout);
5549 scsi_rewind(struct ccb_scsiio *csio, u_int32_t retries,
5550 void (*cbfcnp)(struct cam_periph *, union ccb *),
5551 u_int8_t tag_action, int immediate, u_int8_t sense_len,
5554 struct scsi_rewind *scsi_cmd;
5556 scsi_cmd = (struct scsi_rewind *)&csio->cdb_io.cdb_bytes;
5557 bzero(scsi_cmd, sizeof(*scsi_cmd));
5558 scsi_cmd->opcode = REWIND;
5560 scsi_cmd->immediate = SREW_IMMED;
5562 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
5563 0, sense_len, sizeof(*scsi_cmd), timeout);
5567 scsi_space(struct ccb_scsiio *csio, u_int32_t retries,
5568 void (*cbfcnp)(struct cam_periph *, union ccb *),
5569 u_int8_t tag_action, scsi_space_code code,
5570 u_int32_t count, u_int8_t sense_len, u_int32_t timeout)
5572 struct scsi_space *scsi_cmd;
5574 scsi_cmd = (struct scsi_space *)&csio->cdb_io.cdb_bytes;
5575 scsi_cmd->opcode = SPACE;
5576 scsi_cmd->code = code;
5577 scsi_ulto3b(count, scsi_cmd->count);
5578 scsi_cmd->control = 0;
5580 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
5581 0, sense_len, sizeof(*scsi_cmd), timeout);
5585 scsi_write_filemarks(struct ccb_scsiio *csio, u_int32_t retries,
5586 void (*cbfcnp)(struct cam_periph *, union ccb *),
5587 u_int8_t tag_action, int immediate, int setmark,
5588 u_int32_t num_marks, u_int8_t sense_len,
5591 struct scsi_write_filemarks *scsi_cmd;
5593 scsi_cmd = (struct scsi_write_filemarks *)&csio->cdb_io.cdb_bytes;
5594 bzero(scsi_cmd, sizeof(*scsi_cmd));
5595 scsi_cmd->opcode = WRITE_FILEMARKS;
5597 scsi_cmd->byte2 |= SWFMRK_IMMED;
5599 scsi_cmd->byte2 |= SWFMRK_WSMK;
5601 scsi_ulto3b(num_marks, scsi_cmd->num_marks);
5603 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
5604 0, sense_len, sizeof(*scsi_cmd), timeout);
5608 * The reserve and release unit commands differ only by their opcodes.
5611 scsi_reserve_release_unit(struct ccb_scsiio *csio, u_int32_t retries,
5612 void (*cbfcnp)(struct cam_periph *, union ccb *),
5613 u_int8_t tag_action, int third_party,
5614 int third_party_id, u_int8_t sense_len,
5615 u_int32_t timeout, int reserve)
5617 struct scsi_reserve_release_unit *scsi_cmd;
5619 scsi_cmd = (struct scsi_reserve_release_unit *)&csio->cdb_io.cdb_bytes;
5620 bzero(scsi_cmd, sizeof(*scsi_cmd));
5623 scsi_cmd->opcode = RESERVE_UNIT;
5625 scsi_cmd->opcode = RELEASE_UNIT;
5628 scsi_cmd->lun_thirdparty |= SRRU_3RD_PARTY;
5629 scsi_cmd->lun_thirdparty |=
5630 ((third_party_id << SRRU_3RD_SHAMT) & SRRU_3RD_MASK);
5633 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
5634 0, sense_len, sizeof(*scsi_cmd), timeout);
5638 scsi_erase(struct ccb_scsiio *csio, u_int32_t retries,
5639 void (*cbfcnp)(struct cam_periph *, union ccb *),
5640 u_int8_t tag_action, int immediate, int long_erase,
5641 u_int8_t sense_len, u_int32_t timeout)
5643 struct scsi_erase *scsi_cmd;
5645 scsi_cmd = (struct scsi_erase *)&csio->cdb_io.cdb_bytes;
5646 bzero(scsi_cmd, sizeof(*scsi_cmd));
5648 scsi_cmd->opcode = ERASE;
5651 scsi_cmd->lun_imm_long |= SE_IMMED;
5654 scsi_cmd->lun_imm_long |= SE_LONG;
5656 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action, NULL,
5657 0, sense_len, sizeof(*scsi_cmd), timeout);
5661 * Read Tape Position command.
5664 scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries,
5665 void (*cbfcnp)(struct cam_periph *, union ccb *),
5666 u_int8_t tag_action, int hardsoft,
5667 struct scsi_tape_position_data *sbp,
5668 u_int8_t sense_len, u_int32_t timeout)
5670 struct scsi_tape_read_position *scmd;
5672 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_IN, tag_action,
5673 (u_int8_t *)sbp, sizeof (*sbp), sense_len, sizeof(*scmd), timeout);
5674 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
5675 bzero(scmd, sizeof(*scmd));
5676 scmd->opcode = READ_POSITION;
5677 scmd->byte1 = hardsoft;
5681 * Read Tape Position command.
5684 scsi_read_position_10(struct ccb_scsiio *csio, u_int32_t retries,
5685 void (*cbfcnp)(struct cam_periph *, union ccb *),
5686 u_int8_t tag_action, int service_action,
5687 u_int8_t *data_ptr, u_int32_t length,
5688 u_int32_t sense_len, u_int32_t timeout)
5690 struct scsi_tape_read_position *scmd;
5695 /*flags*/CAM_DIR_IN,
5697 /*data_ptr*/data_ptr,
5698 /*dxfer_len*/length,
5704 scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes;
5705 bzero(scmd, sizeof(*scmd));
5706 scmd->opcode = READ_POSITION;
5707 scmd->byte1 = service_action;
5709 * The length is only currently set (as of SSC4r03) if the extended
5710 * form is specified. The other forms have fixed lengths.
5712 if (service_action == SA_RPOS_EXTENDED_FORM)
5713 scsi_ulto2b(length, scmd->length);
5717 * Set Tape Position command.
5720 scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries,
5721 void (*cbfcnp)(struct cam_periph *, union ccb *),
5722 u_int8_t tag_action, int hardsoft, u_int32_t blkno,
5723 u_int8_t sense_len, u_int32_t timeout)
5725 struct scsi_tape_locate *scmd;
5727 cam_fill_csio(csio, retries, cbfcnp, CAM_DIR_NONE, tag_action,
5728 (u_int8_t *)NULL, 0, sense_len, sizeof(*scmd), timeout);
5729 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
5730 bzero(scmd, sizeof(*scmd));
5731 scmd->opcode = LOCATE;
5733 scmd->byte1 |= SA_SPOS_BT;
5734 scsi_ulto4b(blkno, scmd->blkaddr);
5738 * XXX KDM figure out how to make a compatibility function.
5741 scsi_locate_10(struct ccb_scsiio *csio, u_int32_t retries,
5742 void (*cbfcnp)(struct cam_periph *, union ccb *),
5743 u_int8_t tag_action, int immed, int cp, int hard,
5744 int64_t partition, u_int32_t block_address,
5745 int sense_len, u_int32_t timeout)
5747 struct scsi_tape_locate *scmd;
5759 scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes;
5760 bzero(scmd, sizeof(*scmd));
5761 scmd->opcode = LOCATE;
5763 scmd->byte1 |= SA_SPOS_IMMED;
5765 scmd->byte1 |= SA_SPOS_CP;
5767 scmd->byte1 |= SA_SPOS_BT;
5768 scsi_ulto4b(block_address, scmd->blkaddr);
5769 scmd->partition = partition;
5773 scsi_locate_16(struct ccb_scsiio *csio, u_int32_t retries,
5774 void (*cbfcnp)(struct cam_periph *, union ccb *),
5775 u_int8_t tag_action, int immed, int cp, u_int8_t dest_type,
5776 int bam, int64_t partition, u_int64_t logical_id,
5777 int sense_len, u_int32_t timeout)
5780 struct scsi_locate_16 *scsi_cmd;
5785 /*flags*/CAM_DIR_NONE,
5793 scsi_cmd = (struct scsi_locate_16 *)&csio->cdb_io.cdb_bytes;
5794 bzero(scsi_cmd, sizeof(*scsi_cmd));
5795 scsi_cmd->opcode = LOCATE_16;
5797 scsi_cmd->byte1 |= SA_LC_IMMEDIATE;
5799 scsi_cmd->byte1 |= SA_LC_CP;
5800 scsi_cmd->byte1 |= (dest_type << SA_LC_DEST_TYPE_SHIFT);
5802 scsi_cmd->byte2 |= bam;
5803 scsi_cmd->partition = partition;
5804 scsi_u64to8b(logical_id, scsi_cmd->logical_id);
5808 scsi_report_density_support(struct ccb_scsiio *csio, u_int32_t retries,
5809 void (*cbfcnp)(struct cam_periph *, union ccb *),
5810 u_int8_t tag_action, int media, int medium_type,
5811 u_int8_t *data_ptr, u_int32_t length,
5812 u_int32_t sense_len, u_int32_t timeout)
5814 struct scsi_report_density_support *scsi_cmd;
5816 scsi_cmd =(struct scsi_report_density_support *)&csio->cdb_io.cdb_bytes;
5817 bzero(scsi_cmd, sizeof(*scsi_cmd));
5819 scsi_cmd->opcode = REPORT_DENSITY_SUPPORT;
5821 scsi_cmd->byte1 |= SRDS_MEDIA;
5822 if (medium_type != 0)
5823 scsi_cmd->byte1 |= SRDS_MEDIUM_TYPE;
5825 scsi_ulto2b(length, scsi_cmd->length);
5830 /*flags*/CAM_DIR_IN,
5832 /*data_ptr*/data_ptr,
5833 /*dxfer_len*/length,
5840 scsi_set_capacity(struct ccb_scsiio *csio, u_int32_t retries,
5841 void (*cbfcnp)(struct cam_periph *, union ccb *),
5842 u_int8_t tag_action, int byte1, u_int32_t proportion,
5843 u_int32_t sense_len, u_int32_t timeout)
5845 struct scsi_set_capacity *scsi_cmd;
5847 scsi_cmd = (struct scsi_set_capacity *)&csio->cdb_io.cdb_bytes;
5848 bzero(scsi_cmd, sizeof(*scsi_cmd));
5850 scsi_cmd->opcode = SET_CAPACITY;
5852 scsi_cmd->byte1 = byte1;
5853 scsi_ulto2b(proportion, scsi_cmd->cap_proportion);
5858 /*flags*/CAM_DIR_NONE,
5868 scsi_format_medium(struct ccb_scsiio *csio, u_int32_t retries,
5869 void (*cbfcnp)(struct cam_periph *, union ccb *),
5870 u_int8_t tag_action, int byte1, int byte2,
5871 u_int8_t *data_ptr, u_int32_t dxfer_len,
5872 u_int32_t sense_len, u_int32_t timeout)
5874 struct scsi_format_medium *scsi_cmd;
5876 scsi_cmd = (struct scsi_format_medium*)&csio->cdb_io.cdb_bytes;
5877 bzero(scsi_cmd, sizeof(*scsi_cmd));
5879 scsi_cmd->opcode = FORMAT_MEDIUM;
5881 scsi_cmd->byte1 = byte1;
5882 scsi_cmd->byte2 = byte2;
5884 scsi_ulto2b(dxfer_len, scsi_cmd->length);
5889 /*flags*/(dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
5891 /*data_ptr*/ data_ptr,
5892 /*dxfer_len*/ dxfer_len,
5899 scsi_allow_overwrite(struct ccb_scsiio *csio, u_int32_t retries,
5900 void (*cbfcnp)(struct cam_periph *, union ccb *),
5901 u_int8_t tag_action, int allow_overwrite, int partition,
5902 u_int64_t logical_id, u_int32_t sense_len, u_int32_t timeout)
5904 struct scsi_allow_overwrite *scsi_cmd;
5906 scsi_cmd = (struct scsi_allow_overwrite *)&csio->cdb_io.cdb_bytes;
5907 bzero(scsi_cmd, sizeof(*scsi_cmd));
5909 scsi_cmd->opcode = ALLOW_OVERWRITE;
5911 scsi_cmd->allow_overwrite = allow_overwrite;
5912 scsi_cmd->partition = partition;
5913 scsi_u64to8b(logical_id, scsi_cmd->logical_id);