From 11abe98c84706eb5c4ac22830a10af05db6d5526 Mon Sep 17 00:00:00 2001 From: ken Date: Tue, 24 Mar 2015 14:36:10 +0000 Subject: [PATCH] MFC sa(4) and mt(1) improvements. This includes these changes: 279219, 279229, 279261, 279534, 279570, 280230, 280231. In addition, bump __FreeBSD_version for the addition of the new mtio(4) / sa(4) ioctls. Thanks to Dan Langille, Harald Schmalzbauer and Rudolf Cejka for spending a significant amount of time and effort testing these changes. ------------------------------------------------------------------------ r279219 | ken | 2015-02-23 14:59:30 -0700 (Mon, 23 Feb 2015) | 282 lines Significant upgrades to sa(4) and mt(1). The primary focus of these changes is to modernize FreeBSD's tape infrastructure so that we can take advantage of some of the features of modern tape drives and allow support for LTFS. Significant changes and new features include: o sa(4) driver status and parameter information is now exported via an XML structure. This will allow for changes and improvements later on that will not break userland applications. The old MTIOCGET status ioctl remains, so applications using the existing interface will not break. o 'mt status' now reports drive-reported tape position information as well as the previously available calculated tape position information. These numbers will be different at times, because the drive-reported block numbers are relative to BOP (Beginning of Partition), but the block numbers calculated previously via sa(4) (and still provided) are relative to the last filemark. Both numbers are now provided. 'mt status' now also shows the drive INQUIRY information, serial number and any position flags (BOP, EOT, etc.) provided with the tape position information. 'mt status -v' adds information on the maximum possible I/O size, and the underlying values used to calculate it. o The extra sa(4) /dev entries (/dev/saN.[0-3]) have been removed. The extra devices were originally added as place holders for density-specific device nodes. Some OSes (NetBSD, NetApp's OnTap and Solaris) have had device nodes that, when you write to them, will automatically select a given density for particular tape drives. This is a convenient way of switching densities, but it was never implemented in FreeBSD. Only the device nodes were there, and that sometimes confused users. For modern tape devices, the density is generally not selectable (e.g. with LTO) or defaults to the highest availble density when the tape is rewritten from BOT (e.g. TS11X0). So, for most users, density selection won't be necessary. If they do need to select the density, it is easy enough to use 'mt density' to change it. o Protection information is now supported. This is either a Reed-Solomon CRC or CRC32 that is included at the end of each block read and written. On write, the tape drive verifies the CRC, and on read, the tape drive provides a CRC for the userland application to verify. o New, extensible tape driver parameter get/set interface. o Density reporting information. For drives that support it, 'mt getdensity' will show detailed information on what formats the tape drive supports, and what formats the tape drive supports. o Some mt(1) functionality moved into a new mt(3) library so that external applications can reuse the code. o The new mt(3) library includes helper routines to aid in parsing the XML output of the sa(4) driver, and build a tree of driver metadata. o Support for the MTLOAD (load a tape in the drive) and MTWEOFI (write filemark immediate) ioctls needed by IBM's LTFS implementation. o Improve device departure behavior for the sa(4) driver. The previous implementation led to hangs when the device was open. o This has been tested on the following types of drives: IBM TS1150 IBM TS1140 IBM LTO-6 IBM LTO-5 HP LTO-2 Seagate DDS-4 Quantum DLT-4000 Exabyte 8505 Sony DDS-2 contrib/groff/tmac/doc-syms, share/mk/bsd.libnames.mk, lib/Makefile, Add libmt. lib/libmt/Makefile, lib/libmt/mt.3, lib/libmt/mtlib.c, lib/libmt/mtlib.h, New mt(3) library that contains functions moved from mt(1) and new functions needed to interact with the updated sa(4) driver. This includes XML parser helper functions that application writers can use when writing code to query tape parameters. rescue/rescue/Makefile: Add -lmt to CRUNCH_LIBS. src/share/man/man4/mtio.4 Clarify this man page a bit, and since it contains what is essentially the mtio.h header file, add new ioctls and structure definitions from mtio.h. src/share/man/man4/sa.4 Update BUGS and maintainer section. sys/cam/scsi/scsi_all.c, sys/cam/scsi/scsi_all.h: Add SCSI SECURITY PROTOCOL IN/OUT CDB definitions and CDB building functions. sys/cam/scsi/scsi_sa.c sys/cam/scsi/scsi_sa.h Many tape driver changes, largely outlined above. Increase the sa(4) driver read/write timeout from 4 to 32 minutes. This is based on the recommended values for IBM LTO 5/6 drives. This may also avoid timeouts for other tape hardware that can take a long time to do retries and error recovery. Longer term, a better way to handle this is to ask the drive for recommended timeout values using the REPORT SUPPORTED OPCODES command. Modern IBM and Oracle tape drives at least support that command, and it would allow for more accurate timeout values. Add XML status generation. This is done with a series of macros to eliminate as much duplicate code as possible. The new XML-based status values are reported through the new MTIOCEXTGET ioctl. Add XML driver parameter reporting, using the new MTIOCPARAMGET ioctl. Add a new driver parameter setting interface, using the new MTIOCPARAMSET and MTIOCSETLIST ioctls. Add a new MTIOCRBLIM ioctl to get block limits information. Add CCB/CDB building routines scsi_locate_16, scsi_locate_10, and scsi_read_position_10(). scsi_locate_10 implements the LOCATE command, as does the existing scsi_set_position() command. It just supports additional arguments and features. If/when we figure out a good way to provide backward compatibility for older applications using the old function API, we can just revamp scsi_set_position(). The same goes for scsi_read_position_10() and the existing scsi_read_position() function. Revamp sasetpos() to take the new mtlocate structure as an argument. It now will use either scsi_locate_10() or scsi_locate_16(), depending upon the arguments the user supplies. As before, once we change position we don't have a clear idea of what the current logical position of the tape drive is. For tape drives that support long form position data, we read the current position and store that for later reporting after changing the position. This should help applications like Bacula speed tape access under FreeBSD once they are modified to support the new ioctls. Add a new quirk, SA_QUIRK_NO_LONG_POS, that is set for all drives that report SCSI-2 or older, as well as drives that report an Illegal Request type error for READ POSITION with the long format. So we should automatically detect drives that don't support the long form and stop asking for it after an initial try. Add a partition number to the sa(4) softc. Improve device departure handling. The previous implementation led to hangs when the device was open. If an application had the sa(4) driver open, and attempted to close it after it went away, the cam_periph_release() call in saclose() would cause the periph to get destroyed because that was the last reference to it. Because destroy_dev() was called from the sa(4) driver's cleanup routine (sacleanup()), and would block waiting for the close to happen, a deadlock would result. So instead of calling destroy_dev() from the cleanup routine, call destroy_dev_sched_cb() from saoninvalidate() and wait for the callback. Acquire a reference for devfs in saregister(), and release it in the new sadevgonecb() routine when all devfs devices for the particular sa(4) driver instance are gone. Add a new function, sasetupdev(), to centralize setting per-instance devfs device parameters instead of repeating the code in saregister(). Add an open count to the softc, so we know how many peripheral driver references are a result of open sessions. Add the D_TRACKCLOSE flag to the cdevsw flags so that we get a 1:1 mapping of open to close calls instead of a N:1 mapping. This should be a no-op for everything except the control device, since we don't allow more than one open on non-control devices. However, since we do allow multiple opens on the control device, the combination of the open count and the D_TRACKCLOSE flag should result in an accurate peripheral driver reference count, and an accurate open count. The accurate open count allows us to release all peripheral driver references that are the result of open contexts once we get the callback from devfs. sys/sys/mtio.h: Add a number of new mt(4) ioctls and the requisite data structures. None of the existing interfaces been removed or changed. This includes definitions for the following new ioctls: MTIOCRBLIM /* get block limits */ MTIOCEXTLOCATE /* seek to position */ MTIOCEXTGET /* get tape status */ MTIOCPARAMGET /* get tape params */ MTIOCPARAMSET /* set tape params */ MTIOCSETLIST /* set N params */ usr.bin/mt/Makefile: mt(1) now depends on libmt, libsbuf and libbsdxml. usr.bin/mt/mt.1: Document new mt(1) features and subcommands. usr.bin/mt/mt.c: Implement support for mt(1) subcommands that need to use getopt(3) for their arguments. Implement a new 'mt status' command to replace the old 'mt status' command. The old status command has been renamed 'ostatus'. The new status function uses the MTIOCEXTGET ioctl, and therefore parses the XML data to determine drive status. The -x argument to 'mt status' allows the user to dump out the raw XML reported by the kernel. The new status display is mostly the same as the old status display, except that it doesn't print the redundant density mode information, and it does print the current partition number and position flags. Add a new command, 'mt locate', that will supersede the old 'mt setspos' and 'mt sethpos' commands. 'mt locate' implements all of the functionality of the MTIOCEXTLOCATE ioctl, and allows the user to change the logical position of the tape drive in a number of ways. (Partition, block number, file number, set mark number, end of data.) The immediate bit and the explicit address bits are implemented, but not documented in the man page. Add a new 'mt weofi' command to use the new MTWEOFI ioctl. This allows the user to ask the drive to write a filemark without waiting around for the operation to complete. Add a new 'mt getdensity' command that gets the XML-based tape drive density report from the sa(4) driver and displays it. This uses the SCSI REPORT DENSITY SUPPORT command to get comprehensive information from the tape drive about what formats it is able to read and write. Add a new 'mt protect' command that allows getting and setting tape drive protection information. The protection information is a CRC tacked on to the end of every read/write from and to the tape drive. Sponsored by: Spectra Logic MFC after: 1 month ------------------------------------------------------------------------ ------------------------------------------------------------------------ r279229 | ken | 2015-02-23 22:43:16 -0700 (Mon, 23 Feb 2015) | 5 lines Fix printf format warnings on sparc64 and mips. Sponsored by: Spectra Logic MFC after: 1 month ------------------------------------------------------------------------ ------------------------------------------------------------------------ r279261 | ken | 2015-02-24 21:30:23 -0700 (Tue, 24 Feb 2015) | 23 lines Fix several problems found by Coverity. lib/libmt/mtlib.c: In mt_start_element(), make sure we don't overflow the cur_sb array. CID 1271325 usr.bin/mt/mt.c: In main(), bzero the mt_com structure so that we aren't using any uninitialized stack variables. CID 1271319 In mt_param(), only allow one -s and one -p argument. This will prevent a memory leak caused by overwriting the param_name and/or param_value variables. CID 1271320 and CID 1271322 To make things simpler in mt_param(), make sure there there is only one exit path for the function. Make sure the arguments are explicitly freed. Sponsored by: Spectra Logic Pointed out by: emaste MFC after: 1 month ------------------------------------------------------------------------ ------------------------------------------------------------------------ r279534 | ken | 2015-03-02 11:09:49 -0700 (Mon, 02 Mar 2015) | 18 lines Change the sa(4) driver to check for long position support on SCSI-2 devices. Some older tape devices claim to be SCSI-2, but actually do support long position information. (Long position information includes the current file mark.) For example, the COMPAQ SuperDLT1. So we now only disable the check on SCSI-1 and older devices. sys/cam/scsi/scsi_sa.c: In saregister(), only disable fetching long position information on SCSI-1 and older drives. Update the comment to explain why. Confirmed by: dvl Sponsored by: Spectra Logic MFC after: 3 weeks ------------------------------------------------------------------------ ------------------------------------------------------------------------ r279570 | ken | 2015-03-03 15:49:07 -0700 (Tue, 03 Mar 2015) | 21 lines Add density code for DAT-72, and notes on DAT-160. As it turns out, the density code for DAT-160 (0x48) is the same as for SDLT220. Since the SDLT values are already in the table, we will leave them in place. Thanks to Harald Schmalzbauer for confirming the DAT-72 density code. lib/libmt/mtlib.c: Add DAT-72 density code, and commented out DAT-160 density code. Explain why DAT-160 is commented out. Add notes explaining where the bpi values for these formats came from. usr.bin/mt/mt.1: Add DAT-72 density code, and add a note explaining that the SDLTTapeI(110) density code (0x48) is the same as DAT-160. Sponsored by: Spectra Logic MFC after: 3 weeks ------------------------------------------------------------------------ ------------------------------------------------------------------------ r280230 | ken | 2015-03-18 14:52:34 -0600 (Wed, 18 Mar 2015) | 25 lines Fix a couple of problems in the sa(4) media type reports. The only drives I have discovered so far that support medium type reports are newer HP LTO (LTO-5 and LTO-6) drives. IBM drives only support the density reports. sys/cam/scsi/scsi_sa.h: The number of possible density codes in the medium type report is 9, not 8. This caused problems parsing all of the medium type report after this point in the structure. usr.bin/mt/mt.c: Run the density codes returned in the medium type report through denstostring(), just like the primary and secondary density codes in the density report. This will print the density code in hex, and give a text description if it is available. Thanks to Rudolf Cejka for doing extensive testing with HP LTO drives and Bacula and discovering these problems. Tested by: Rudolf Cejka Sponsored by: Spectra Logic MFC after: 4 days ------------------------------------------------------------------------ ------------------------------------------------------------------------ r280231 | ken | 2015-03-18 14:54:54 -0600 (Wed, 18 Mar 2015) | 16 lines Improve the mt(1) rblim display. The granularity reported by READ BLOCK LIMITS is an exponent, not a byte value. So a granularity of 0 means 2^0, or 1 byte. A granularity of 1 means 2^1, or 2 bytes. Print out the individual block limits on separate lines to improve readability and avoid exceeding 80 columns. usr.bin/mt/mt.c: Fix and improve the 'mt rblim' output. Add a MT_PLURAL() macro so we can print "byte" or "bytes" as appropriate. Sponsored by: Spectra Logic MFC after: 4 days ------------------------------------------------------------------------ Sponsored by: Spectra Logic git-svn-id: svn://svn.freebsd.org/base/stable/10@280438 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- contrib/groff/tmac/doc-syms | 1 + lib/Makefile | 1 + lib/libmt/Makefile | 13 + lib/libmt/mt.3 | 455 +++++++ lib/libmt/mtlib.c | 773 +++++++++++ lib/libmt/mtlib.h | 122 ++ rescue/rescue/Makefile | 2 +- share/man/man4/mtio.4 | 139 +- share/man/man4/sa.4 | 16 +- share/mk/bsd.libnames.mk | 1 + sys/cam/scsi/scsi_all.c | 65 + sys/cam/scsi/scsi_all.h | 51 + sys/cam/scsi/scsi_sa.c | 2476 ++++++++++++++++++++++++++++++++--- sys/cam/scsi/scsi_sa.h | 714 +++++++++- sys/sys/mtio.h | 116 ++ sys/sys/param.h | 2 +- usr.bin/mt/Makefile | 2 + usr.bin/mt/mt.1 | 271 +++- usr.bin/mt/mt.c | 1266 +++++++++++++++--- 19 files changed, 6100 insertions(+), 386 deletions(-) create mode 100644 lib/libmt/Makefile create mode 100644 lib/libmt/mt.3 create mode 100644 lib/libmt/mtlib.c create mode 100644 lib/libmt/mtlib.h diff --git a/contrib/groff/tmac/doc-syms b/contrib/groff/tmac/doc-syms index dfb09b30b..4e736cb5a 100644 --- a/contrib/groff/tmac/doc-syms +++ b/contrib/groff/tmac/doc-syms @@ -796,6 +796,7 @@ .ds doc-str-Lb-libmd Message Digest (MD4, MD5, etc.) Support Library (libmd, \-lmd) .ds doc-str-Lb-libmemstat Kernel Memory Allocator Statistics Library (libmemstat, \-lmemstat) .ds doc-str-Lb-libmenu Curses Menu Library (libmenu, \-lmenu) +.ds doc-str-Lb-libmt Magnetic Tape Library (libmt, \-lmt) .ds doc-str-Lb-libnetgraph Netgraph User Library (libnetgraph, \-lnetgraph) .ds doc-str-Lb-libnetpgp Netpgp signing, verification, encryption and decryption (libnetpgp, \-lnetpgp) .ds doc-str-Lb-libossaudio OSS Audio Emulation Library (libossaudio, \-lossaudio) diff --git a/lib/Makefile b/lib/Makefile index c5dfc4839..41ea35c16 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -65,6 +65,7 @@ SUBDIR= ${SUBDIR_ORDERED} \ libmd \ ${_libmilter} \ ${_libmp} \ + libmt \ ${_libnandfs} \ libnetbsd \ ${_libnetgraph} \ diff --git a/lib/libmt/Makefile b/lib/libmt/Makefile new file mode 100644 index 000000000..fe54d2fe8 --- /dev/null +++ b/lib/libmt/Makefile @@ -0,0 +1,13 @@ +# $FreeBSD$ + +LIB= mt +SHLIBDIR?= /lib +SRCS= mtlib.c +INCS= mtlib.h + +DPADD= ${LIBSBUF} +LDADD= -lsbuf + +MAN= mt.3 + +.include diff --git a/lib/libmt/mt.3 b/lib/libmt/mt.3 new file mode 100644 index 000000000..da63aa94f --- /dev/null +++ b/lib/libmt/mt.3 @@ -0,0 +1,455 @@ +.\" +.\" Copyright (c) 2013, 2015 Spectra Logic Corporation +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions, and the following disclaimer, +.\" without modification. +.\" 2. Redistributions in binary form must reproduce at minimum a disclaimer +.\" substantially similar to the "NO WARRANTY" disclaimer below +.\" ("Disclaimer") and any redistribution must be conditioned upon +.\" including a substantially similar Disclaimer requirement for further +.\" binary redistribution. +.\" +.\" NO WARRANTY +.\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +.\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR +.\" A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +.\" HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +.\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +.\" POSSIBILITY OF SUCH DAMAGES. +.\" +.\" Authors: Ken Merry (Spectra Logic Corporation) +.\" +.\" $FreeBSD$ +.\" +.Dd February 13, 2015 +.Dt MT 3 +.Os +.Sh NAME +.Nm +.Nm mt_start_element , +.Nm mt_end_element , +.Nm mt_char_handler , +.Nm mt_status_tree_sbuf , +.Nm mt_status_tree_print , +.Nm mt_status_entry_free , +.Nm mt_status_free , +.Nm mt_entry_sbuf , +.Nm mt_param_parent_print , +.Nm mt_param_entry_print , +.Nm mt_protect_print , +.Nm mt_param_list , +.Nm mt_density_name , +.Nm mt_density_bp , +.Nm mt_density_num , +.Nm mt_get_xml_str , +.Nm mt_get_status +.Nd Magnetic Tape library +.Sh LIBRARY +.Lb libmt +.Sh SYNOPSIS +.In sys/sbuf.h +.In bsdxml.h +.In mtlib.h +.Ft void +.Fo mt_start_element +.Fa "void *user_data" +.Fa "const char *name" +.Fa "const char **attr" +.Fc +.Ft void +.Fo mt_end_element +.Fa "void *user_data" +.Fa "const char *name" +.Fc +.Ft void +.Fo mt_char_handler +.Fa "void *user_data" +.Fa "const XML_Char *str" +.Fa "int len" +.Fc +.Ft void +.Fo mt_status_tree_sbuf +.Fa "struct sbuf *sb" +.Fa "struct mt_status_entry *entry" +.Fa "int indent" +.Fa "void (*sbuf_func)(struct sbuf *sb, struct mt_status_entry *entry, void *arg)" +.Fa "void *arg" +.Fc +.Ft void +.Fo mt_status_tree_print +.Fa "struct mt_status_entry *entry" +.Fa "int indent" +.Fa "void (*print_func)(struct mt_status_entry *entry, void *arg)" +.Fa "void *arg" +.Fc +.Ft "struct mt_status_entry *" +.Fo mt_entry_find +.Fa "struct mt_status_entry *entry" +.Fa "char *name" +.Fc +.Ft "struct mt_status_entry *" +.Fo mt_status_entry_find +.Fa "struct mt_status_data *status_data" +.Fa "char *name" +.Fc +.Ft void +.Fo mt_status_entry_free +.Fa "struct mt_status_entry *entry)" +.Fc +.Ft void +.Fo mt_status_free +.Fa "struct mt_status_data *status_data" +.Fc +.Ft void +.Fo mt_entry_sbuf +.Fa "struct sbuf *sb" +.Fa "struct mt_status_entry *entry" +.Fa "char *fmt" +.Fc +.Ft void +.Fo mt_param_parent_sbuf +.Fa "struct sbuf *sb" +.Fa "struct mt_status_entry *entry" +.Fa "struct mt_print_params *print_params" +.Fc +.Ft void +.Fo mt_param_parent_print +.Fa "struct mt_status_entry *entry" +.Fa "struct mt_print_params *print_params" +.Fc +.Ft void +.Fo mt_param_entry_sbuf +.Fa "struct sbuf *sb" +.Fa "struct mt_status_entry *entry" +.Fa "void *arg" +.Fc +.Ft void +.Fo mt_param_entry_print +.Fa "struct mt_status_entry *entry" +.Fa "void *arg" +.Fc +.Ft int +.Fo mt_protect_print +.Fa "struct mt_status_data *status_data" +.Fa "int verbose" +.Fc +.Ft int +.Fo mt_param_list +.Fa "struct mt_status_data *status_data" +.Fa "char *param_name" +.Fa "int quiet" +.Fc +.Ft "const char *" +.Fo mt_density_name +.Fa "int density_num" +.Fc +.Ft int +.Fo mt_density_bp +.Fa "int density_num" +.Fa "int bpi" +.Fc +.Ft int +.Fo mt_density_num +.Fa "const char *density_name" +.Fc +.Ft int +.Fo mt_get_status +.Fa "char *xml_str" +.Fa "struct mt_status_data *status_data" +.Fc +.Sh DESCRIPTION +The MT library consists of a number of functions designed to aid in +interacting with the +.Xr sa 4 +driver. +The +.Xr sa 4 +driver returns some status data as XML-formatted strings, and +the primary purpose of this library is to make it easier for the +software developer to parse those strings and extract the status values. +.Pp +The +.Fn mt_start_element , +.Fn mt_end_element , +and +.Fn mt_char_handler +functions are designed to work with the +.Xr libbbsdxml 3 +library, which is an XML parsing library. +The user data for the XML parser should be set with +.Fn XML_SetUserData +to a zeroed struct +mt_status_data with the entries list initialized. +The element handlers for the XML parser should be set to +.Fn mt_start_element +and +.Fn mt_end_element +with +.Fn XML_SetElementHandler . +The character data handler should be set to +.Fn mt_char_handler +with the +.Fn XML_SetCharacterDataHandler +function. +The error member of the status_data structure will be set to 0 if parsing +is successful, and non-zero if parsing failed. +In the event of a failure, the error_str member will contain an error +message describing the failure. +These functions will build a tree of tape driver status data that can be +searched and printed using the other functions in this library. +.Pp +.Fn mt_status_tree_sbuf +takes the root node of a tree of +.Xr sa 4 +driver status information, and displays it in an +.Xr sbuf 9 . +The +.Ar sb +argument is the destination sbuf. +The +.Ar entry +argument is the root of the tree. +The +.Ar indent +argument is the number of characters to indent the output. +Each recursive call to +.Fn mt_status_tree_sbuf +will have the indent level incremented by 2. +The +.Ar sbuf_func +argument is for a user-supplied alternate printing function. +If it is non-NULL, it will be called instead of the default output printing +code. +The +.Ar arg +argument is an argument for the +.Ar sbuf_func +function. +.Pp +The +.Fn mt_status_tree_print +function is the same as the +.Fn mt_status_tree_sbuf +function, except that the tree is printed to standard out instead of to a +sbuf. +.Pp +The +.Fn mt_entry_find +function returns the first entry in the tree starting at +.Ar entry +that matches +.Ar name . +The supplied node name can be a single level name like "foo", or it can +specify mulitple node names that must be matched, for instance "foo.bar.baz". +In the case of a single level name, it will match any node beneath +.Ar entry +that matches +.Ar name . +In the case of a multi-level name like "foo.bar.baz", it will return the +first entry named "baz" whose immediate parent is "bar" and where the +parent of "bar" is named "foo". +.Pp +The +.Fn mt_status_entry_find +is the same as +.Fn mt_entry_find , +except that it operates on the top level mt_status_data and all +mt_status_entry nodes below it instead of just an mt_status_entry +structure. +.Pp +The +.Fn mt_status_entry_free +function frees the tree of status data underneath +.Ar entry . +.Pp +The +.Fn mt_status_free +function frees the tree of status data underneath +.Ar status_data . +.Pp +The +.Fn mt_entry_sbuf +function prints +.Ar entry +to the supplied sbuf +.Ar sb , +optionally using the +.Xr printf 3 +format +.Ar fmt . +If +.Ar fmt +is NULL, then +.Fn mt_entry_sbuf +will render integer types in base 10 without special formatting and all +other types as they were rendered in the XML. +.Pp +.Fn mt_param_parent_sbuf +prints the parents of the given +.Ar entry +to the supplied sbuf +.Ar sb +subject to the print parameters +.Ar print_params . +The result will be formatted with a period between each level, like +"foo.bar.baz". +.Pp +.Fn mt_param_parent_print +is like +.Fn mt_param_parent_sbuf +except that it prints the results to standard output instead of an sbuf. +.Pp +.Fn mt_param_entry_sbuf +prints the +.Ar entry +to the given sbuf +.Ar sb . +The argument +.Ar arg +is a pointer to struct mt_print_params, which allows the caller to control +the printing output. +This function is intended to be supplied as an argument to +.Fn mt_status_tree_sbuf . +.Pp +.Fn mt_param_entry_print +is like +.Fn mt_param_entry_sbuf +except that it prints to standard output instead of an sbuf. +It is intended to be used as an argument to +.Fn mt_status_tree_print . +.Pp +.Fn mt_protect_print +prints tape drive protection information from the supplied +.Ar status_data +beginning at the node name defined as the root node for protection data. +If the +.Ar verbose +argument is non-zero, protection entry descriptions will be printed. +If it is zero, protection entry descriptions will not be printed. +.Pp +.Fn mt_param_list +prints tape driver parameters information from the supplied +.Ar status_data . +If the +.Ar param_name +is non-NULL, only the named parameter will be printed. +If +.Ar quiet +is non-zero, parameter descriptions will be omitted in the output. +.Pp +.Fn mt_density_name +Returns a text identifier for the supplied numeric +.Ar density_num . +The +.Ar density_num +should currently be a value between 0 and 255 inclusive, since that is the +valid range for +.Tn SCSI +density code values. +See below for notes on the return values. +.Pp +.Fn mt_density_bp +Returns the bits per inch or bits per mm values for a given density entry +specified by the +.Ar density_num . +If the +.Ar bpi +argument is non-zero, the bits per inch value is returned. +Otherwise, the bits per mm value is returned. +.Pp +.Fn mt_density_num +returns a numeric value for a text density description. +It does a case-insensitive comparison of density names in the density table +to the supplied density name. +.Pp +.Fn mt_get_xml_str +gets the current XML status / parameter string from the sa(4) driver +instance referenced by the open file descriptor +.Ar mtfd . +The +.Xr mtio 4 +.Xr ioctl 2 +to be used is supplied as the +.Ar cmd +argument. +Currently the +.Fn mt_get_xml_str +function will work with the +.Dv MTIOCEXTGET +and +.Dv MTIOCPARAMGET +ioctls. +The supplied +.Ar xml_str +will be filled in with a pointer to the complete XML status string. +Multiple calls to the given +.Xr ioctl 2 +are made and more space is malloced until all of the XML string is fetched. +The string returned in the +.Ar xml_str +argument should be freed when it is no longer in use. +.Sh RETURN VALUES +.Fn mt_entry_find +returns the first matching entry, or NULL if it fails to find a match. +.Pp +.Fn mt_status_entry_find +returns the first matching entry, or NULL if it fails to find a match. +.Pp +.Fn mt_protect_print +Returns 0 for success, and non-zero for failure. +.Fn mt_protect_print +can only fail if it cannot find protection information in the supplied +status data. +.Pp +.Fn mt_param_list +Returns 0 for success and non-zero for failure. +.Fn mt_param_list +can only fail if it cannot find parameter information in the supplied +status data. +.Pp +.Fn mt_density_name +returns a text description of a numeric density. +The special density value 0 is decoded as "default". +The special density value 0x7f is decoded as "same". +If the density is not known, +.Fn mt_density_name +will return "UNKNOWN". +.Pp +.Fn mt_density_bp +returns the bits per inch value for the given density (if the +.Ar bpi +field is non-zero), the bits per mm value otherwise, or 0 if the supplied +.Ar density_num +is not in the density table or the table entry does not include bpi / bpmm +values. +.Pp +.Fn mt_density_num +returns a numeric density value between 0 and 255 for the supplied density +name. +It returns 0 if the density name is not recognized. +.Pp +.Fn mt_get_xml_str +returns 0 for success, and -1 for failure. +.Sh SEE ALSO +.Xr mt 1 , +.Xr mtio 4 , +.Xr sa 4 +.Sh HISTORY +The MT library first appeared in +.Fx 10.1 . +.Sh AUTHORS +.An Ken Merry Aq ken@FreeBSD.org +.Sh BUGS +The library interface is not complete, and may change in the future. +Application authors should not rely on the library interface to be +consistent in the immediate future. diff --git a/lib/libmt/mtlib.c b/lib/libmt/mtlib.c new file mode 100644 index 000000000..f6acba21f --- /dev/null +++ b/lib/libmt/mtlib.c @@ -0,0 +1,773 @@ +/*- + * Copyright (c) 2013, 2014, 2015 Spectra Logic Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions, and the following disclaimer, + * without modification. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * substantially similar to the "NO WARRANTY" disclaimer below + * ("Disclaimer") and any redistribution must be conditioned upon + * including a substantially similar Disclaimer requirement for further + * binary redistribution. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + * Authors: Ken Merry (Spectra Logic Corporation) + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * Called at the start of each XML element, and includes the list of + * attributes for the element. + */ +void +mt_start_element(void *user_data, const char *name, const char **attr) +{ + int i; + struct mt_status_data *mtinfo; + struct mt_status_entry *entry; + + mtinfo = (struct mt_status_data *)user_data; + + if (mtinfo->error != 0) + return; + + mtinfo->level++; + if ((u_int)mtinfo->level >= (sizeof(mtinfo->cur_sb) / + sizeof(mtinfo->cur_sb[0]))) { + mtinfo->error = 1; + snprintf(mtinfo->error_str, sizeof(mtinfo->error_str), + "%s: too many nesting levels, %zd max", __func__, + sizeof(mtinfo->cur_sb) / sizeof(mtinfo->cur_sb[0])); + return; + } + + mtinfo->cur_sb[mtinfo->level] = sbuf_new_auto(); + if (mtinfo->cur_sb[mtinfo->level] == NULL) { + mtinfo->error = 1; + snprintf(mtinfo->error_str, sizeof(mtinfo->error_str), + "%s: Unable to allocate sbuf", __func__); + return; + } + + entry = malloc(sizeof(*entry)); + if (entry == NULL) { + mtinfo->error = 1; + snprintf(mtinfo->error_str, sizeof(mtinfo->error_str), + "%s: unable to allocate %zd bytes", __func__, + sizeof(*entry)); + return; + } + bzero(entry, sizeof(*entry)); + STAILQ_INIT(&entry->nv_list); + STAILQ_INIT(&entry->child_entries); + entry->entry_name = strdup(name); + mtinfo->cur_entry[mtinfo->level] = entry; + if (mtinfo->cur_entry[mtinfo->level - 1] == NULL) { + STAILQ_INSERT_TAIL(&mtinfo->entries, entry, links); + } else { + STAILQ_INSERT_TAIL( + &mtinfo->cur_entry[mtinfo->level - 1]->child_entries, + entry, links); + entry->parent = mtinfo->cur_entry[mtinfo->level - 1]; + } + for (i = 0; attr[i] != NULL; i+=2) { + struct mt_status_nv *nv; + int need_nv; + + need_nv = 0; + + if (strcmp(attr[i], "size") == 0) { + entry->size = strtoull(attr[i+1], NULL, 0); + } else if (strcmp(attr[i], "type") == 0) { + if (strcmp(attr[i+1], "int") == 0) { + entry->var_type = MT_TYPE_INT; + } else if (strcmp(attr[i+1], "uint") == 0) { + entry->var_type = MT_TYPE_UINT; + } else if (strcmp(attr[i+1], "str") == 0) { + entry->var_type = MT_TYPE_STRING; + } else if (strcmp(attr[i+1], "node") == 0) { + entry->var_type = MT_TYPE_NODE; + } else { + need_nv = 1; + } + } else if (strcmp(attr[i], "fmt") == 0) { + entry->fmt = strdup(attr[i+1]); + } else if (strcmp(attr[i], "desc") == 0) { + entry->desc = strdup(attr[i+1]); + } else { + need_nv = 1; + } + if (need_nv != 0) { + nv = malloc(sizeof(*nv)); + if (nv == NULL) { + mtinfo->error = 1; + snprintf(mtinfo->error_str, + sizeof(mtinfo->error_str), + "%s: error allocating %zd bytes", + __func__, sizeof(*nv)); + } + bzero(nv, sizeof(*nv)); + nv->name = strdup(attr[i]); + nv->value = strdup(attr[i+1]); + STAILQ_INSERT_TAIL(&entry->nv_list, nv, links); + } + } +} + +/* + * Called on XML element close. + */ +void +mt_end_element(void *user_data, const char *name) +{ + struct mt_status_data *mtinfo; + char *str; + + mtinfo = (struct mt_status_data *)user_data; + + if (mtinfo->error != 0) + return; + + if (mtinfo->cur_sb[mtinfo->level] == NULL) { + mtinfo->error = 1; + snprintf(mtinfo->error_str, sizeof(mtinfo->error_str), + "%s: no valid sbuf at level %d (name %s)", __func__, + mtinfo->level, name); + return; + } + sbuf_finish(mtinfo->cur_sb[mtinfo->level]); + str = strdup(sbuf_data(mtinfo->cur_sb[mtinfo->level])); + if (str == NULL) { + mtinfo->error = 1; + snprintf(mtinfo->error_str, sizeof(mtinfo->error_str), + "%s can't allocate %zd bytes for string", __func__, + sbuf_len(mtinfo->cur_sb[mtinfo->level])); + return; + } + + if (strlen(str) == 0) { + free(str); + str = NULL; + } + if (str != NULL) { + struct mt_status_entry *entry; + + entry = mtinfo->cur_entry[mtinfo->level]; + switch(entry->var_type) { + case MT_TYPE_INT: + entry->value_signed = strtoll(str, NULL, 0); + break; + case MT_TYPE_UINT: + entry->value_unsigned = strtoull(str, NULL, 0); + break; + default: + break; + } + } + + mtinfo->cur_entry[mtinfo->level]->value = str; + + sbuf_delete(mtinfo->cur_sb[mtinfo->level]); + mtinfo->cur_sb[mtinfo->level] = NULL; + mtinfo->cur_entry[mtinfo->level] = NULL; + mtinfo->level--; +} + +/* + * Called to handle character strings in the current element. + */ +void +mt_char_handler(void *user_data, const XML_Char *str, int len) +{ + struct mt_status_data *mtinfo; + + mtinfo = (struct mt_status_data *)user_data; + if (mtinfo->error != 0) + return; + + sbuf_bcat(mtinfo->cur_sb[mtinfo->level], str, len); +} + +void +mt_status_tree_sbuf(struct sbuf *sb, struct mt_status_entry *entry, int indent, + void (*sbuf_func)(struct sbuf *sb, struct mt_status_entry *entry, + void *arg), void *arg) +{ + struct mt_status_nv *nv; + struct mt_status_entry *entry2; + + if (sbuf_func != NULL) { + sbuf_func(sb, entry, arg); + } else { + sbuf_printf(sb, "%*sname: %s, value: %s, fmt: %s, size: %zd, " + "type: %d, desc: %s\n", indent, "", entry->entry_name, + entry->value, entry->fmt, entry->size, entry->var_type, + entry->desc); + STAILQ_FOREACH(nv, &entry->nv_list, links) { + sbuf_printf(sb, "%*snv: name: %s, value: %s\n", + indent + 1, "", nv->name, nv->value); + } + } + + STAILQ_FOREACH(entry2, &entry->child_entries, links) + mt_status_tree_sbuf(sb, entry2, indent + 2, sbuf_func, arg); +} + +void +mt_status_tree_print(struct mt_status_entry *entry, int indent, + void (*print_func)(struct mt_status_entry *entry, void *arg), void *arg) +{ + + if (print_func != NULL) { + struct mt_status_entry *entry2; + + print_func(entry, arg); + STAILQ_FOREACH(entry2, &entry->child_entries, links) + mt_status_tree_print(entry2, indent + 2, print_func, + arg); + } else { + struct sbuf *sb; + + sb = sbuf_new_auto(); + if (sb == NULL) + return; + mt_status_tree_sbuf(sb, entry, indent, NULL, NULL); + sbuf_finish(sb); + + printf("%s", sbuf_data(sb)); + sbuf_delete(sb); + } +} + +/* + * Given a parameter name in the form "foo" or "foo.bar.baz", traverse the + * tree looking for the parameter (the first case) or series of parameters + * (second case). + */ +struct mt_status_entry * +mt_entry_find(struct mt_status_entry *entry, char *name) +{ + struct mt_status_entry *entry2; + char *tmpname = NULL, *tmpname2 = NULL, *tmpstr = NULL; + + tmpname = strdup(name); + if (tmpname == NULL) + goto bailout; + + /* Save a pointer so we can free this later */ + tmpname2 = tmpname; + + tmpstr = strsep(&tmpname, "."); + + /* + * Is this the entry we're looking for? Or do we have further + * child entries that we need to grab? + */ + if (strcmp(entry->entry_name, tmpstr) == 0) { + if (tmpname == NULL) { + /* + * There are no further child entries to find. We + * have a complete match. + */ + free(tmpname2); + return (entry); + } else { + /* + * There are more child entries that we need to find. + * Fall through to the recursive search off of this + * entry, below. Use tmpname, which will contain + * everything after the first period. + */ + name = tmpname; + } + } + + /* + * Recursively look for further entries. + */ + STAILQ_FOREACH(entry2, &entry->child_entries, links) { + struct mt_status_entry *entry3; + + entry3 = mt_entry_find(entry2, name); + if (entry3 != NULL) { + free(tmpname2); + return (entry3); + } + } + +bailout: + free(tmpname2); + + return (NULL); +} + +struct mt_status_entry * +mt_status_entry_find(struct mt_status_data *status_data, char *name) +{ + struct mt_status_entry *entry, *entry2; + + STAILQ_FOREACH(entry, &status_data->entries, links) { + entry2 = mt_entry_find(entry, name); + if (entry2 != NULL) + return (entry2); + } + + return (NULL); +} + +void +mt_status_entry_free(struct mt_status_entry *entry) +{ + struct mt_status_entry *entry2, *entry3; + struct mt_status_nv *nv, *nv2; + + STAILQ_FOREACH_SAFE(entry2, &entry->child_entries, links, entry3) { + STAILQ_REMOVE(&entry->child_entries, entry2, mt_status_entry, + links); + mt_status_entry_free(entry2); + } + + free(entry->entry_name); + free(entry->value); + free(entry->fmt); + free(entry->desc); + + STAILQ_FOREACH_SAFE(nv, &entry->nv_list, links, nv2) { + STAILQ_REMOVE(&entry->nv_list, nv, mt_status_nv, links); + free(nv->name); + free(nv->value); + free(nv); + } + free(entry); +} + +void +mt_status_free(struct mt_status_data *status_data) +{ + struct mt_status_entry *entry, *entry2; + + STAILQ_FOREACH_SAFE(entry, &status_data->entries, links, entry2) { + STAILQ_REMOVE(&status_data->entries, entry, mt_status_entry, + links); + mt_status_entry_free(entry); + } +} + +void +mt_entry_sbuf(struct sbuf *sb, struct mt_status_entry *entry, char *fmt) +{ + switch(entry->var_type) { + case MT_TYPE_INT: + if (fmt != NULL) + sbuf_printf(sb, fmt, (intmax_t)entry->value_signed); + else + sbuf_printf(sb, "%jd", + (intmax_t)entry->value_signed); + break; + case MT_TYPE_UINT: + if (fmt != NULL) + sbuf_printf(sb, fmt, (uintmax_t)entry->value_unsigned); + else + sbuf_printf(sb, "%ju", + (uintmax_t)entry->value_unsigned); + break; + default: + if (fmt != NULL) + sbuf_printf(sb, fmt, entry->value); + else + sbuf_printf(sb, "%s", entry->value); + break; + } +} + +void +mt_param_parent_print(struct mt_status_entry *entry, + struct mt_print_params *print_params) +{ + if (entry->parent != NULL) + mt_param_parent_print(entry->parent, print_params); + + if (((print_params->flags & MT_PF_INCLUDE_ROOT) == 0) + && (strcmp(entry->entry_name, print_params->root_name) == 0)) + return; + + printf("%s.", entry->entry_name); +} + +void +mt_param_parent_sbuf(struct sbuf *sb, struct mt_status_entry *entry, + struct mt_print_params *print_params) +{ + if (entry->parent != NULL) + mt_param_parent_sbuf(sb, entry->parent, print_params); + + if (((print_params->flags & MT_PF_INCLUDE_ROOT) == 0) + && (strcmp(entry->entry_name, print_params->root_name) == 0)) + return; + + sbuf_printf(sb, "%s.", entry->entry_name); +} + +void +mt_param_entry_sbuf(struct sbuf *sb, struct mt_status_entry *entry, void *arg) +{ + struct mt_print_params *print_params; + + print_params = (struct mt_print_params *)arg; + + /* + * We don't want to print nodes. + */ + if (entry->var_type == MT_TYPE_NODE) + return; + + if ((print_params->flags & MT_PF_FULL_PATH) + && (entry->parent != NULL)) + mt_param_parent_sbuf(sb, entry->parent, print_params); + + sbuf_printf(sb, "%s: %s", entry->entry_name, entry->value); + if ((print_params->flags & MT_PF_VERBOSE) + && (entry->desc != NULL) + && (strlen(entry->desc) > 0)) + sbuf_printf(sb, " (%s)", entry->desc); + sbuf_printf(sb, "\n"); + +} + +void +mt_param_entry_print(struct mt_status_entry *entry, void *arg) +{ + struct mt_print_params *print_params; + + print_params = (struct mt_print_params *)arg; + + /* + * We don't want to print nodes. + */ + if (entry->var_type == MT_TYPE_NODE) + return; + + if ((print_params->flags & MT_PF_FULL_PATH) + && (entry->parent != NULL)) + mt_param_parent_print(entry->parent, print_params); + + printf("%s: %s", entry->entry_name, entry->value); + if ((print_params->flags & MT_PF_VERBOSE) + && (entry->desc != NULL) + && (strlen(entry->desc) > 0)) + printf(" (%s)", entry->desc); + printf("\n"); +} + +int +mt_protect_print(struct mt_status_data *status_data, int verbose) +{ + struct mt_status_entry *entry; + const char *prot_name = MT_PROTECTION_NAME; + struct mt_print_params print_params; + + snprintf(print_params.root_name, sizeof(print_params.root_name), + MT_PARAM_ROOT_NAME); + print_params.flags = MT_PF_FULL_PATH; + if (verbose != 0) + print_params.flags |= MT_PF_VERBOSE; + + entry = mt_status_entry_find(status_data, __DECONST(char *,prot_name)); + if (entry == NULL) + return (1); + mt_status_tree_print(entry, 0, mt_param_entry_print, &print_params); + + return (0); +} + +int +mt_param_list(struct mt_status_data *status_data, char *param_name, int quiet) +{ + struct mt_status_entry *entry; + struct mt_print_params print_params; + char root_name[20]; + + snprintf(root_name, sizeof(root_name), "mtparamget"); + strlcpy(print_params.root_name, root_name, + sizeof(print_params.root_name)); + + print_params.flags = MT_PF_FULL_PATH; + if (quiet == 0) + print_params.flags |= MT_PF_VERBOSE; + + if (param_name != NULL) { + entry = mt_status_entry_find(status_data, param_name); + if (entry == NULL) + return (1); + + mt_param_entry_print(entry, &print_params); + + return (0); + } else { + entry = mt_status_entry_find(status_data, root_name); + + STAILQ_FOREACH(entry, &status_data->entries, links) + mt_status_tree_print(entry, 0, mt_param_entry_print, + &print_params); + } + + return (0); +} + +static struct densities { + int dens; + int bpmm; + int bpi; + const char *name; +} dens[] = { + /* + * Taken from T10 Project 997D + * SCSI-3 Stream Device Commands (SSC) + * Revision 11, 4-Nov-97 + * + * LTO 1-6 definitions obtained from the eighth edition of the + * IBM TotalStorage LTO Ultrium Tape Drive SCSI Reference + * (July 2007) and the second edition of the IBM System Storage LTO + * Tape Drive SCSI Reference (February 13, 2013). + * + * IBM 3592 definitions obtained from second edition of the IBM + * System Storage Tape Drive 3592 SCSI Reference (May 25, 2012). + * + * DAT-72 and DAT-160 bpi values taken from "HP StorageWorks DAT160 + * tape drive white paper", dated June 2007. + * + * DAT-160 / SDLT220 density code (0x48) conflict information + * found here: + * + * http://h20564.www2.hp.com/hpsc/doc/public/display?docId=emr_na-c01065117&sp4ts.oid=429311 + * (Document ID c01065117) + */ + /*Num. bpmm bpi Reference */ + { 0x1, 32, 800, "X3.22-1983" }, + { 0x2, 63, 1600, "X3.39-1986" }, + { 0x3, 246, 6250, "X3.54-1986" }, + { 0x5, 315, 8000, "X3.136-1986" }, + { 0x6, 126, 3200, "X3.157-1987" }, + { 0x7, 252, 6400, "X3.116-1986" }, + { 0x8, 315, 8000, "X3.158-1987" }, + { 0x9, 491, 37871, "X3.180" }, + { 0xA, 262, 6667, "X3B5/86-199" }, + { 0xB, 63, 1600, "X3.56-1986" }, + { 0xC, 500, 12690, "HI-TC1" }, + { 0xD, 999, 25380, "HI-TC2" }, + { 0xF, 394, 10000, "QIC-120" }, + { 0x10, 394, 10000, "QIC-150" }, + { 0x11, 630, 16000, "QIC-320" }, + { 0x12, 2034, 51667, "QIC-1350" }, + { 0x13, 2400, 61000, "X3B5/88-185A" }, + { 0x14, 1703, 43245, "X3.202-1991" }, + { 0x15, 1789, 45434, "ECMA TC17" }, + { 0x16, 394, 10000, "X3.193-1990" }, + { 0x17, 1673, 42500, "X3B5/91-174" }, + { 0x18, 1673, 42500, "X3B5/92-50" }, + { 0x19, 2460, 62500, "DLTapeIII" }, + { 0x1A, 3214, 81633, "DLTapeIV(20GB)" }, + { 0x1B, 3383, 85937, "DLTapeIV(35GB)" }, + { 0x1C, 1654, 42000, "QIC-385M" }, + { 0x1D, 1512, 38400, "QIC-410M" }, + { 0x1E, 1385, 36000, "QIC-1000C" }, + { 0x1F, 2666, 67733, "QIC-2100C" }, + { 0x20, 2666, 67733, "QIC-6GB(M)" }, + { 0x21, 2666, 67733, "QIC-20GB(C)" }, + { 0x22, 1600, 40640, "QIC-2GB(C)" }, + { 0x23, 2666, 67733, "QIC-875M" }, + { 0x24, 2400, 61000, "DDS-2" }, + { 0x25, 3816, 97000, "DDS-3" }, + { 0x26, 3816, 97000, "DDS-4" }, + { 0x27, 3056, 77611, "Mammoth" }, + { 0x28, 1491, 37871, "X3.224" }, + { 0x40, 4880, 123952, "LTO-1" }, + { 0x41, 3868, 98250, "DLTapeIV(40GB)" }, + { 0x42, 7398, 187909, "LTO-2" }, + { 0x44, 9638, 244805, "LTO-3" }, + { 0x46, 12725, 323215, "LTO-4" }, + { 0x47, 6417, 163000, "DAT-72" }, + /* + * XXX KDM note that 0x48 is also the density code for DAT-160. + * For some reason they used overlapping density codes. + */ +#if 0 + { 0x48, 6870, 174500, "DAT-160" }, +#endif + { 0x48, 5236, 133000, "SDLTapeI(110)" }, + { 0x49, 7598, 193000, "SDLTapeI(160)" }, + { 0x4a, 0, 0, "T10000A" }, + { 0x4b, 0, 0, "T10000B" }, + { 0x4c, 0, 0, "T10000C" }, + { 0x4d, 0, 0, "T10000D" }, + { 0x51, 11800, 299720, "3592A1 (unencrypted)" }, + { 0x52, 11800, 299720, "3592A2 (unencrypted)" }, + { 0x53, 13452, 341681, "3592A3 (unencrypted)" }, + { 0x54, 19686, 500024, "3592A4 (unencrypted)" }, + { 0x55, 20670, 525018, "3592A5 (unencrypted)" }, + { 0x58, 15142, 384607, "LTO-5" }, + { 0x5A, 15142, 384607, "LTO-6" }, + { 0x71, 11800, 299720, "3592A1 (encrypted)" }, + { 0x72, 11800, 299720, "3592A2 (encrypted)" }, + { 0x73, 13452, 341681, "3592A3 (encrypted)" }, + { 0x74, 19686, 500024, "3592A4 (encrypted)" }, + { 0x75, 20670, 525018, "3592A5 (encrypted)" }, + { 0x8c, 1789, 45434, "EXB-8500c" }, + { 0x90, 1703, 43245, "EXB-8200c" }, + { 0, 0, 0, NULL } +}; + +const char * +mt_density_name(int density_num) +{ + struct densities *sd; + + /* densities 0 and 0x7f are handled as special cases */ + if (density_num == 0) + return ("default"); + if (density_num == 0x7f) + return ("same"); + + for (sd = dens; sd->dens != 0; sd++) + if (sd->dens == density_num) + break; + if (sd->dens == 0) + return ("UNKNOWN"); + return (sd->name); +} + +/* + * Given a specific density number, return either the bits per inch or bits + * per millimeter for the given density. + */ +int +mt_density_bp(int density_num, int bpi) +{ + struct densities *sd; + + for (sd = dens; sd->dens; sd++) + if (sd->dens == density_num) + break; + if (sd->dens == 0) + return (0); + if (bpi) + return (sd->bpi); + else + return (sd->bpmm); +} + +int +mt_density_num(const char *density_name) +{ + struct densities *sd; + size_t l = strlen(density_name); + + for (sd = dens; sd->dens; sd++) + if (strncasecmp(sd->name, density_name, l) == 0) + break; + return (sd->dens); +} + +/* + * Get the current status XML string. + * Returns 0 on success, -1 on failure (with errno set, and *xml_str == NULL). + */ +int +mt_get_xml_str(int mtfd, unsigned long cmd, char **xml_str) +{ + size_t alloc_len = 32768; + struct mtextget extget; + int error; + + *xml_str = NULL; + + for (;;) { + bzero(&extget, sizeof(extget)); + *xml_str = malloc(alloc_len); + if (*xml_str == NULL) + return (-1); + extget.status_xml = *xml_str; + extget.alloc_len = alloc_len; + + error = ioctl(mtfd, cmd, (caddr_t)&extget); + if (error == 0 && extget.status == MT_EXT_GET_OK) + break; + + free(*xml_str); + *xml_str = NULL; + + if (error != 0 || extget.status != MT_EXT_GET_NEED_MORE_SPACE) + return (-1); + + /* The driver needs more space, so double and try again. */ + alloc_len *= 2; + } + return (0); +} + +/* + * Populate a struct mt_status_data from the XML string via mt_get_xml_str(). + * + * Returns XML_STATUS_OK on success. + * If XML_STATUS_ERROR is returned, errno may be set to indicate the reason. + * The caller must check status_data->error. + */ +int +mt_get_status(char *xml_str, struct mt_status_data *status_data) +{ + XML_Parser parser; + int retval; + + bzero(status_data, sizeof(*status_data)); + STAILQ_INIT(&status_data->entries); + + parser = XML_ParserCreate(NULL); + if (parser == NULL) { + errno = ENOMEM; + return (XML_STATUS_ERROR); + } + + XML_SetUserData(parser, status_data); + XML_SetElementHandler(parser, mt_start_element, mt_end_element); + XML_SetCharacterDataHandler(parser, mt_char_handler); + + retval = XML_Parse(parser, xml_str, strlen(xml_str), 1); + XML_ParserFree(parser); + return (retval); +} diff --git a/lib/libmt/mtlib.h b/lib/libmt/mtlib.h new file mode 100644 index 000000000..a61a15d23 --- /dev/null +++ b/lib/libmt/mtlib.h @@ -0,0 +1,122 @@ +/*- + * Copyright (c) 2013, 2014 Spectra Logic Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions, and the following disclaimer, + * without modification. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * substantially similar to the "NO WARRANTY" disclaimer below + * ("Disclaimer") and any redistribution must be conditioned upon + * including a substantially similar Disclaimer requirement for further + * binary redistribution. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + * Authors: Ken Merry (Spectra Logic Corporation) + * + * $FreeBSD$ + */ + +#ifndef _MTLIB_H +#define _MTLIB_H + +typedef enum { + MT_TYPE_NONE, + MT_TYPE_STRING, + MT_TYPE_INT, + MT_TYPE_UINT, + MT_TYPE_NODE +} mt_variable_type; + +struct mt_status_nv { + char *name; + char *value; + STAILQ_ENTRY(mt_status_nv) links; +}; + +struct mt_status_entry { + char *entry_name; + char *value; + uint64_t value_unsigned; + int64_t value_signed; + char *fmt; + char *desc; + size_t size; + mt_variable_type var_type; + struct mt_status_entry *parent; + STAILQ_HEAD(, mt_status_nv) nv_list; + STAILQ_HEAD(, mt_status_entry) child_entries; + STAILQ_ENTRY(mt_status_entry) links; +}; + +struct mt_status_data { + int level; + struct sbuf *cur_sb[32]; + struct mt_status_entry *cur_entry[32]; + int error; + char error_str[128]; + STAILQ_HEAD(, mt_status_entry) entries; +}; + +typedef enum { + MT_PF_NONE = 0x00, + MT_PF_VERBOSE = 0x01, + MT_PF_FULL_PATH = 0x02, + MT_PF_INCLUDE_ROOT = 0x04 +} mt_print_flags; + +struct mt_print_params { + mt_print_flags flags; + char root_name[64]; +}; + +__BEGIN_DECLS +void mt_start_element(void *user_data, const char *name, const char **attr); +void mt_end_element(void *user_data, const char *name); +void mt_char_handler(void *user_data, const XML_Char *str, int len); +void mt_status_tree_sbuf(struct sbuf *sb, struct mt_status_entry *entry, + int indent, void (*sbuf_func)(struct sbuf *sb, + struct mt_status_entry *entry, void *arg), void *arg); +void mt_status_tree_print(struct mt_status_entry *entry, int indent, + void (*print_func)(struct mt_status_entry *entry, + void *arg), void *arg); +struct mt_status_entry *mt_entry_find(struct mt_status_entry *entry, + char *name); +struct mt_status_entry *mt_status_entry_find(struct mt_status_data *status_data, + char *name); +void mt_status_entry_free(struct mt_status_entry *entry); +void mt_status_free(struct mt_status_data *status_data); +void mt_entry_sbuf(struct sbuf *sb, struct mt_status_entry *entry, char *fmt); +void mt_param_parent_print(struct mt_status_entry *entry, + struct mt_print_params *print_params); +void mt_param_parent_sbuf(struct sbuf *sb, struct mt_status_entry *entry, + struct mt_print_params *print_params); +void mt_param_entry_sbuf(struct sbuf *sb, struct mt_status_entry *entry, + void *arg); +void mt_param_entry_print(struct mt_status_entry *entry, void *arg); +int mt_protect_print(struct mt_status_data *status_data, int verbose); +int mt_param_list(struct mt_status_data *status_data, char *param_name, + int quiet); +const char *mt_density_name(int density_num); +int mt_density_bp(int density_num, int bpi); +int mt_density_num(const char *density_name); +int mt_get_xml_str(int mtfd, unsigned long cmd, char **xml_str); +int mt_get_status(char *xml_str, struct mt_status_data *status_data); +__END_DECLS + +#endif /* _MTLIB_H */ diff --git a/rescue/rescue/Makefile b/rescue/rescue/Makefile index d9bd70817..a72e69f46 100644 --- a/rescue/rescue/Makefile +++ b/rescue/rescue/Makefile @@ -131,7 +131,7 @@ CRUNCH_LIBS+= -lgeom -lbsdxml -lkiconv .if ${MK_OPENSSL} == "no" CRUNCH_LIBS+= -lmd .endif -CRUNCH_LIBS+= -lsbuf -lufs -lz +CRUNCH_LIBS+= -lmt -lsbuf -lufs -lz .if ${MACHINE_CPUARCH} == "i386" CRUNCH_PROGS_sbin+= bsdlabel sconfig fdisk diff --git a/share/man/man4/mtio.4 b/share/man/man4/mtio.4 index e8056e6f6..58a4a82d4 100644 --- a/share/man/man4/mtio.4 +++ b/share/man/man4/mtio.4 @@ -35,7 +35,7 @@ .\" @(#)mtio.4 8.1 (Berkeley) 6/5/93 .\" $FreeBSD$ .\" -.Dd November 11, 2011 +.Dd February 12, 2015 .Dt MTIO 4 .Os .Sh NAME @@ -44,10 +44,10 @@ .Sh DESCRIPTION The special files named -.Pa /dev/[n]sa* +.Pa /dev/[en]sa* refer to SCSI tape drives, which may be attached to the system. -.Pa /dev/[n]sa*.ctl +.Pa /dev/sa*.ctl are control devices that can be used to issue ioctls to the SCSI tape driver to set parameters that are required to last beyond the unmounting of a tape. @@ -57,16 +57,19 @@ when the last requested read, write or seek has finished, or the end of the tape has been reached. The letter .Ql n -is usually prepended to +is prepended to the name of the no-rewind devices. +The letter +.Ql e +is prepended to the name of the eject devices. .Pp Tapes can be written with either fixed length records or variable length records. See .Xr sa 4 for more information. -Two end-of-file markers mark the end of a tape, and -one end-of-file marker marks the end of a tape file. +Two filemarks mark the end of a tape, and +one filemark marks the end of a tape file. If the tape is not to be rewound it is positioned with the head in between the two tape marks, where the next write will over write the second end-of-file marker. @@ -132,6 +135,8 @@ struct mtop { #define MTWSS 16 /* write setmark(s) */ #define MTFSS 17 /* forward space setmark */ #define MTBSS 18 /* backward space setmark */ +#define MTLOAD 19 /* load tape in drive */ +#define MTWEOFI 20 /* write an end-of-file record without waiting*/ #define MT_COMP_ENABLE 0xffffffff #define MT_COMP_DISABLED 0xfffffffe @@ -228,6 +233,112 @@ union mterrstat { char _reserved_padding[256]; }; +struct mtrblim { + uint32_t granularity; + uint32_t min_block_length; + uint32_t max_block_length; +}; + +typedef enum { + MT_LOCATE_DEST_OBJECT = 0x00, + MT_LOCATE_DEST_FILE = 0x01, + MT_LOCATE_DEST_SET = 0x02, + MT_LOCATE_DEST_EOD = 0x03 +} mt_locate_dest_type; + +typedef enum { + MT_LOCATE_BAM_IMPLICIT = 0x00, + MT_LOCATE_BAM_EXPLICIT = 0x01 +} mt_locate_bam; + +typedef enum { + MT_LOCATE_FLAG_IMMED = 0x01, + MT_LOCATE_FLAG_CHANGE_PART = 0x02 +} mt_locate_flags; + +struct mtlocate { + mt_locate_flags flags; + mt_locate_dest_type dest_type; + mt_locate_bam block_address_mode; + int64_t partition; + uint64_t logical_id; + uint8_t reserved[64]; +}; + +typedef enum { + MT_EXT_GET_NONE, + MT_EXT_GET_OK, + MT_EXT_GET_NEED_MORE_SPACE, + MT_EXT_GET_ERROR +} mt_ext_get_status; + +struct mtextget { + uint32_t alloc_len; + char *status_xml; + uint32_t fill_len; + mt_ext_get_status status; + char error_str[128]; + uint8_t reserved[64]; +}; + +#define MT_EXT_GET_ROOT_NAME "mtextget" +#define MT_DENSITY_ROOT_NAME "mtdensity" +#define MT_MEDIA_DENSITY_NAME "media_density" +#define MT_DENSITY_REPORT_NAME "density_report" +#define MT_MEDIUM_TYPE_REPORT_NAME "medium_type_report" +#define MT_MEDIA_REPORT_NAME "media_report" +#define MT_DENSITY_ENTRY_NAME "density_entry" + +#define MT_DENS_WRITE_OK 0x80 +#define MT_DENS_DUP 0x40 +#define MT_DENS_DEFLT 0x20 + + +#define MT_PARAM_FIXED_STR_LEN 32 +union mt_param_value { + int64_t value_signed; + uint64_t value_unsigned; + char *value_var_str; + char value_fixed_str[MT_PARAM_FIXED_STR_LEN]; + uint8_t reserved[64]; +}; + +typedef enum { + MT_PARAM_SET_NONE, + MT_PARAM_SET_SIGNED, + MT_PARAM_SET_UNSIGNED, + MT_PARAM_SET_VAR_STR, + MT_PARAM_SET_FIXED_STR +} mt_param_set_type; + +typedef enum { + MT_PARAM_STATUS_NONE, + MT_PARAM_STATUS_OK, + MT_PARAM_STATUS_ERROR +} mt_param_set_status; + +#define MT_PARAM_VALUE_NAME_LEN 64 +struct mtparamset { + char value_name[MT_PARAM_VALUE_NAME_LEN]; + mt_param_set_type value_type; + int value_len; + union mt_param_value value; + mt_param_set_status status; + char error_str[128]; +}; + +#define MT_PARAM_ROOT_NAME "mtparamget" +#define MT_PROTECTION_NAME "protection" + +/* + * Set a list of parameters. + */ +struct mtsetlist { + int num_params; + int param_len; + struct mtparamset *params; +}; + /* * Constants for mt_type byte. These are the same * for controllers compatible with the types listed. @@ -277,6 +388,12 @@ union mterrstat { #define MTIOCSETEOTMODEL _IOW('m', 8, uint32_t) /* Get current EOT model */ #define MTIOCGETEOTMODEL _IOR('m', 8, uint32_t) +#define MTIOCRBLIM _IOR('m', 9, struct mtrblim) /* get block limits */ +#define MTIOCEXTLOCATE _IOW('m', 10, struct mtlocate) /* seek to position */ +#define MTIOCEXTGET _IOWR('m', 11, struct mtextget) /* get tape status */ +#define MTIOCPARAMGET _IOWR('m', 12, struct mtextget) /* get tape params */ +#define MTIOCPARAMSET _IOWR('m', 13, struct mtparamset) /* set tape params */ +#define MTIOCSETLIST _IOWR('m', 14, struct mtsetlist) /* set N params */ #ifndef _KERNEL #define DEFTAPE "/dev/nsa0" @@ -285,13 +402,12 @@ union mterrstat { #endif /* !_SYS_MTIO_H_ */ .Ed .Sh FILES -.Bl -tag -width /dev/[n]sa* -compact -.It Pa /dev/[n]sa* +.Bl -tag -width /dev/[en]sa* -compact +.It Pa /dev/[en]sa* .El .Sh SEE ALSO .Xr mt 1 , .Xr tar 1 , -.Xr ast 4 , .Xr sa 4 .Sh HISTORY The @@ -300,8 +416,3 @@ manual appeared in .Bx 4.2 . An i386 version first appeared in .Fx 2.2 . -.Sh BUGS -The status should be returned in a device independent format. -.Pp -The special file naming should be redone in a more consistent and -understandable manner. diff --git a/share/man/man4/sa.4 b/share/man/man4/sa.4 index aa72f68ec..ef7692f44 100644 --- a/share/man/man4/sa.4 +++ b/share/man/man4/sa.4 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd August 23, 2013 +.Dd February 12, 2015 .Dt SA 4 .Os .Sh NAME @@ -285,10 +285,10 @@ device driver written and ported from by .An Julian Elischer . .Pp -The current owner of record is -.An Matthew Jacob -who has suffered too many -years of breaking tape drivers. +The owner of record for many years was +.An Matthew Jacob . +The current maintainer is +.An Kenneth Merry .Sh BUGS This driver lacks many of the hacks required to deal with older devices. Many older @@ -305,7 +305,5 @@ for your device in order to read tapes written under .Fx 2.X. .Pp -Fine grained density and compression mode support that is bound to specific -device names needs to be added. -.Pp -Support for fast indexing by use of partitions is missing. +Partitions are only supported for status information and location. +It would be nice to add support for creating and editing tape partitions. diff --git a/share/mk/bsd.libnames.mk b/share/mk/bsd.libnames.mk index e07b6112c..6ba1b79f9 100644 --- a/share/mk/bsd.libnames.mk +++ b/share/mk/bsd.libnames.mk @@ -95,6 +95,7 @@ LIBMENU?= ${DESTDIR}${LIBDIR}/libmenu.a LIBMILTER?= ${DESTDIR}${LIBDIR}/libmilter.a .endif LIBMP?= ${DESTDIR}${LIBDIR}/libmp.a +LIBMT?= ${DESTDIR}${LIBDIR}/libmt.a LIBNCURSES?= ${DESTDIR}${LIBDIR}/libncurses.a LIBNCURSESW?= ${DESTDIR}${LIBDIR}/libncursesw.a LIBNETGRAPH?= ${DESTDIR}${LIBDIR}/libnetgraph.a diff --git a/sys/cam/scsi/scsi_all.c b/sys/cam/scsi/scsi_all.c index d4a61f063..dc1d96d21 100644 --- a/sys/cam/scsi/scsi_all.c +++ b/sys/cam/scsi/scsi_all.c @@ -7443,6 +7443,71 @@ scsi_persistent_reserve_out(struct ccb_scsiio *csio, uint32_t retries, scsi_cmd->opcode = PERSISTENT_RES_OUT; scsi_cmd->action = service_action; scsi_cmd->scope_type = scope | res_type; + + cam_fill_csio(csio, + retries, + cbfcnp, + /*flags*/CAM_DIR_OUT, + tag_action, + /*data_ptr*/data_ptr, + /*dxfer_len*/dxfer_len, + sense_len, + sizeof(*scsi_cmd), + timeout); +} + +void +scsi_security_protocol_in(struct ccb_scsiio *csio, uint32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + uint8_t tag_action, uint32_t security_protocol, + uint32_t security_protocol_specific, int byte4, + uint8_t *data_ptr, uint32_t dxfer_len, int sense_len, + int timeout) +{ + struct scsi_security_protocol_in *scsi_cmd; + + scsi_cmd = (struct scsi_security_protocol_in *)&csio->cdb_io.cdb_bytes; + bzero(scsi_cmd, sizeof(*scsi_cmd)); + + scsi_cmd->opcode = SECURITY_PROTOCOL_IN; + + scsi_cmd->security_protocol = security_protocol; + scsi_ulto2b(security_protocol_specific, + scsi_cmd->security_protocol_specific); + scsi_cmd->byte4 = byte4; + scsi_ulto4b(dxfer_len, scsi_cmd->length); + + cam_fill_csio(csio, + retries, + cbfcnp, + /*flags*/CAM_DIR_IN, + tag_action, + data_ptr, + dxfer_len, + sense_len, + sizeof(*scsi_cmd), + timeout); +} + +void +scsi_security_protocol_out(struct ccb_scsiio *csio, uint32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + uint8_t tag_action, uint32_t security_protocol, + uint32_t security_protocol_specific, int byte4, + uint8_t *data_ptr, uint32_t dxfer_len, int sense_len, + int timeout) +{ + struct scsi_security_protocol_out *scsi_cmd; + + scsi_cmd = (struct scsi_security_protocol_out *)&csio->cdb_io.cdb_bytes; + bzero(scsi_cmd, sizeof(*scsi_cmd)); + + scsi_cmd->opcode = SECURITY_PROTOCOL_OUT; + + scsi_cmd->security_protocol = security_protocol; + scsi_ulto2b(security_protocol_specific, + scsi_cmd->security_protocol_specific); + scsi_cmd->byte4 = byte4; scsi_ulto4b(dxfer_len, scsi_cmd->length); cam_fill_csio(csio, diff --git a/sys/cam/scsi/scsi_all.h b/sys/cam/scsi/scsi_all.h index 46d0bfc04..f70b09423 100644 --- a/sys/cam/scsi/scsi_all.h +++ b/sys/cam/scsi/scsi_all.h @@ -1758,6 +1758,7 @@ struct ata_pass_16 { #define SERVICE_ACTION_IN 0x9E #define REPORT_LUNS 0xA0 #define ATA_PASS_12 0xA1 +#define SECURITY_PROTOCOL_IN 0xA2 #define MAINTENANCE_IN 0xA3 #define MAINTENANCE_OUT 0xA4 #define MOVE_MEDIUM 0xA5 @@ -1765,6 +1766,7 @@ struct ata_pass_16 { #define WRITE_12 0xAA #define WRITE_VERIFY_12 0xAE #define VERIFY_12 0xAF +#define SECURITY_PROTOCOL_OUT 0xB5 #define READ_ELEMENT_STATUS 0xB8 #define READ_CD 0xBE @@ -2702,6 +2704,41 @@ struct scsi_target_group_data_extended { struct scsi_target_port_group_descriptor groups[]; }; +struct scsi_security_protocol_in +{ + uint8_t opcode; + uint8_t security_protocol; +#define SPI_PROT_INFORMATION 0x00 +#define SPI_PROT_CBCS 0x07 +#define SPI_PROT_TAPE_DATA_ENC 0x20 +#define SPI_PROT_DATA_ENC_CONFIG 0x21 +#define SPI_PROT_SA_CREATE_CAP 0x40 +#define SPI_PROT_IKEV2_SCSI 0x41 +#define SPI_PROT_JEDEC_UFS 0xEC +#define SPI_PROT_SDCARD_TFSSS 0xED +#define SPI_PROT_AUTH_HOST_TRANSIENT 0xEE +#define SPI_PROT_ATA_DEVICE_PASSWORD 0xEF + uint8_t security_protocol_specific[2]; + uint8_t byte4; +#define SPI_INC_512 0x80 + uint8_t reserved1; + uint8_t length[4]; + uint8_t reserved2; + uint8_t control; +}; + +struct scsi_security_protocol_out +{ + uint8_t opcode; + uint8_t security_protocol; + uint8_t security_protocol_specific[2]; + uint8_t byte4; +#define SPO_INC_512 0x80 + uint8_t reserved1; + uint8_t length[4]; + uint8_t reserved2; + uint8_t control; +}; typedef enum { SSD_TYPE_NONE, @@ -3623,6 +3660,20 @@ void scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries, u_int8_t tag_action, int start, int load_eject, int immediate, u_int8_t sense_len, u_int32_t timeout); +void scsi_security_protocol_in(struct ccb_scsiio *csio, uint32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + uint8_t tag_action, uint32_t security_protocol, + uint32_t security_protocol_specific, int byte4, + uint8_t *data_ptr, uint32_t dxfer_len, + int sense_len, int timeout); + +void scsi_security_protocol_out(struct ccb_scsiio *csio, uint32_t retries, + void (*cbfcnp)(struct cam_periph *,union ccb *), + uint8_t tag_action, uint32_t security_protocol, + uint32_t security_protocol_specific, int byte4, + uint8_t *data_ptr, uint32_t dxfer_len, + int sense_len, int timeout); + void scsi_persistent_reserve_in(struct ccb_scsiio *csio, uint32_t retries, void (*cbfcnp)(struct cam_periph *,union ccb *), uint8_t tag_action, int service_action, diff --git a/sys/cam/scsi/scsi_sa.c b/sys/cam/scsi/scsi_sa.c index 591e843fd..0480854ea 100644 --- a/sys/cam/scsi/scsi_sa.c +++ b/sys/cam/scsi/scsi_sa.c @@ -2,6 +2,7 @@ * Implementation of SCSI Sequential Access Peripheral driver for CAM. * * Copyright (c) 1999, 2000 Matthew Jacob + * Copyright (c) 2013, 2014, 2015 Spectra Logic Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -43,6 +44,7 @@ __FBSDID("$FreeBSD$"); #include #ifdef _KERNEL #include +#include #include #include #endif @@ -69,7 +71,7 @@ __FBSDID("$FreeBSD$"); #include #ifndef SA_IO_TIMEOUT -#define SA_IO_TIMEOUT 4 +#define SA_IO_TIMEOUT 32 #endif #ifndef SA_SPACE_TIMEOUT #define SA_SPACE_TIMEOUT 1 * 60 @@ -80,6 +82,9 @@ __FBSDID("$FreeBSD$"); #ifndef SA_ERASE_TIMEOUT #define SA_ERASE_TIMEOUT 4 * 60 #endif +#ifndef SA_REP_DENSITY_TIMEOUT +#define SA_REP_DENSITY_TIMEOUT 90 +#endif #define SCSIOP_TIMEOUT (60 * 1000) /* not an option */ @@ -87,6 +92,7 @@ __FBSDID("$FreeBSD$"); #define REWIND_TIMEOUT (SA_REWIND_TIMEOUT * 60 * 1000) #define ERASE_TIMEOUT (SA_ERASE_TIMEOUT * 60 * 1000) #define SPACE_TIMEOUT (SA_SPACE_TIMEOUT * 60 * 1000) +#define REP_DENSITY_TIMEOUT (SA_REP_DENSITY_TIMEOUT * 60 * 1000) /* * Additional options that can be set for config: SA_1FM_AT_EOT @@ -133,7 +139,12 @@ typedef enum { SA_FLAG_COMP_ENABLED = 0x0400, SA_FLAG_COMP_SUPP = 0x0800, SA_FLAG_COMP_UNSUPP = 0x1000, - SA_FLAG_TAPE_FROZEN = 0x2000 + SA_FLAG_TAPE_FROZEN = 0x2000, + SA_FLAG_PROTECT_SUPP = 0x4000, + + SA_FLAG_COMPRESSION = (SA_FLAG_COMP_SUPP|SA_FLAG_COMP_ENABLED| + SA_FLAG_COMP_UNSUPP), + SA_FLAG_SCTX_INIT = 0x8000 } sa_flags; typedef enum { @@ -143,27 +154,30 @@ typedef enum { } sa_mode; typedef enum { - SA_PARAM_NONE = 0x00, - SA_PARAM_BLOCKSIZE = 0x01, - SA_PARAM_DENSITY = 0x02, - SA_PARAM_COMPRESSION = 0x04, - SA_PARAM_BUFF_MODE = 0x08, - SA_PARAM_NUMBLOCKS = 0x10, - SA_PARAM_WP = 0x20, - SA_PARAM_SPEED = 0x40, - SA_PARAM_ALL = 0x7f + SA_PARAM_NONE = 0x000, + SA_PARAM_BLOCKSIZE = 0x001, + SA_PARAM_DENSITY = 0x002, + SA_PARAM_COMPRESSION = 0x004, + SA_PARAM_BUFF_MODE = 0x008, + SA_PARAM_NUMBLOCKS = 0x010, + SA_PARAM_WP = 0x020, + SA_PARAM_SPEED = 0x040, + SA_PARAM_DENSITY_EXT = 0x080, + SA_PARAM_LBP = 0x100, + SA_PARAM_ALL = 0x1ff } sa_params; typedef enum { - SA_QUIRK_NONE = 0x00, - SA_QUIRK_NOCOMP = 0x01, /* Can't deal with compression at all */ - SA_QUIRK_FIXED = 0x02, /* Force fixed mode */ - SA_QUIRK_VARIABLE = 0x04, /* Force variable mode */ - SA_QUIRK_2FM = 0x08, /* Needs Two File Marks at EOD */ - SA_QUIRK_1FM = 0x10, /* No more than 1 File Mark at EOD */ - SA_QUIRK_NODREAD = 0x20, /* Don't try and dummy read density */ - SA_QUIRK_NO_MODESEL = 0x40, /* Don't do mode select at all */ - SA_QUIRK_NO_CPAGE = 0x80 /* Don't use DEVICE COMPRESSION page */ + SA_QUIRK_NONE = 0x000, + SA_QUIRK_NOCOMP = 0x001, /* Can't deal with compression at all*/ + SA_QUIRK_FIXED = 0x002, /* Force fixed mode */ + SA_QUIRK_VARIABLE = 0x004, /* Force variable mode */ + SA_QUIRK_2FM = 0x008, /* Needs Two File Marks at EOD */ + SA_QUIRK_1FM = 0x010, /* No more than 1 File Mark at EOD */ + SA_QUIRK_NODREAD = 0x020, /* Don't try and dummy read density */ + SA_QUIRK_NO_MODESEL = 0x040, /* Don't do mode select at all */ + SA_QUIRK_NO_CPAGE = 0x080, /* Don't use DEVICE COMPRESSION page */ + SA_QUIRK_NO_LONG_POS = 0x100 /* No long position information */ } sa_quirks; #define SA_QUIRK_BIT_STRING \ @@ -175,10 +189,10 @@ typedef enum { "\0051FM" \ "\006NODREAD" \ "\007NO_MODESEL" \ - "\010NO_CPAGE" + "\010NO_CPAGE" \ + "\011NO_LONG_POS" #define SAMODE(z) (dev2unit(z) & 0x3) -#define SADENSITY(z) ((dev2unit(z) >> 2) & 0x3) #define SA_IS_CTRL(z) (dev2unit(z) & (1 << 4)) #define SA_NOT_CTLDEV 0 @@ -187,29 +201,134 @@ typedef enum { #define SA_ATYPE_R 0 #define SA_ATYPE_NR 1 #define SA_ATYPE_ER 2 +#define SA_NUM_ATYPES 3 -#define SAMINOR(ctl, mode, access) \ - ((ctl << 4) | (mode << 2) | (access & 0x3)) +#define SAMINOR(ctl, access) \ + ((ctl << 4) | (access & 0x3)) -#define SA_NUM_MODES 4 struct sa_devs { struct cdev *ctl_dev; - struct sa_mode_devs { - struct cdev *r_dev; - struct cdev *nr_dev; - struct cdev *er_dev; - } mode_devs[SA_NUM_MODES]; + struct cdev *r_dev; + struct cdev *nr_dev; + struct cdev *er_dev; +}; + +#define SASBADDBASE(sb, indent, data, xfmt, name, type, xsize, desc) \ + sbuf_printf(sb, "%*s<%s type=\"%s\" size=\"%zd\" " \ + "fmt=\"%s\" desc=\"%s\">" #xfmt "\n", indent, "", \ + #name, #type, xsize, #xfmt, desc ? desc : "", data, #name); + +#define SASBADDINT(sb, indent, data, fmt, name) \ + SASBADDBASE(sb, indent, data, fmt, name, int, sizeof(data), \ + NULL) + +#define SASBADDINTDESC(sb, indent, data, fmt, name, desc) \ + SASBADDBASE(sb, indent, data, fmt, name, int, sizeof(data), \ + desc) + +#define SASBADDUINT(sb, indent, data, fmt, name) \ + SASBADDBASE(sb, indent, data, fmt, name, uint, sizeof(data), \ + NULL) + +#define SASBADDUINTDESC(sb, indent, data, fmt, name, desc) \ + SASBADDBASE(sb, indent, data, fmt, name, uint, sizeof(data), \ + desc) + +#define SASBADDFIXEDSTR(sb, indent, data, fmt, name) \ + SASBADDBASE(sb, indent, data, fmt, name, str, sizeof(data), \ + NULL) + +#define SASBADDFIXEDSTRDESC(sb, indent, data, fmt, name, desc) \ + SASBADDBASE(sb, indent, data, fmt, name, str, sizeof(data), \ + desc) + +#define SASBADDVARSTR(sb, indent, data, fmt, name, maxlen) \ + SASBADDBASE(sb, indent, data, fmt, name, str, maxlen, NULL) + +#define SASBADDVARSTRDESC(sb, indent, data, fmt, name, maxlen, desc) \ + SASBADDBASE(sb, indent, data, fmt, name, str, maxlen, desc) + +#define SASBADDNODE(sb, indent, name) { \ + sbuf_printf(sb, "%*s<%s type=\"%s\">\n", indent, "", #name, \ + "node"); \ + indent += 2; \ +} + +#define SASBADDNODENUM(sb, indent, name, num) { \ + sbuf_printf(sb, "%*s<%s type=\"%s\" num=\"%d\">\n", indent, "", \ + #name, "node", num); \ + indent += 2; \ +} + +#define SASBENDNODE(sb, indent, name) { \ + indent -= 2; \ + sbuf_printf(sb, "%*s\n", indent, "", #name); \ +} + +#define SA_DENSITY_TYPES 4 + +struct sa_prot_state { + int initialized; + uint32_t prot_method; + uint32_t pi_length; + uint32_t lbp_w; + uint32_t lbp_r; + uint32_t rbdp; +}; + +struct sa_prot_info { + struct sa_prot_state cur_prot_state; + struct sa_prot_state pending_prot_state; +}; + +/* + * A table mapping protection parameters to their types and values. + */ +struct sa_prot_map { + char *name; + mt_param_set_type param_type; + off_t offset; + uint32_t min_val; + uint32_t max_val; + uint32_t *value; +} sa_prot_table[] = { + { "prot_method", MT_PARAM_SET_UNSIGNED, + __offsetof(struct sa_prot_state, prot_method), + /*min_val*/ 0, /*max_val*/ 255, NULL }, + { "pi_length", MT_PARAM_SET_UNSIGNED, + __offsetof(struct sa_prot_state, pi_length), + /*min_val*/ 0, /*max_val*/ SA_CTRL_DP_PI_LENGTH_MASK, NULL }, + { "lbp_w", MT_PARAM_SET_UNSIGNED, + __offsetof(struct sa_prot_state, lbp_w), + /*min_val*/ 0, /*max_val*/ 1, NULL }, + { "lbp_r", MT_PARAM_SET_UNSIGNED, + __offsetof(struct sa_prot_state, lbp_r), + /*min_val*/ 0, /*max_val*/ 1, NULL }, + { "rbdp", MT_PARAM_SET_UNSIGNED, + __offsetof(struct sa_prot_state, rbdp), + /*min_val*/ 0, /*max_val*/ 1, NULL } }; +#define SA_NUM_PROT_ENTS sizeof(sa_prot_table)/sizeof(sa_prot_table[0]) + +#define SA_PROT_ENABLED(softc) ((softc->flags & SA_FLAG_PROTECT_SUPP) \ + && (softc->prot_info.cur_prot_state.initialized != 0) \ + && (softc->prot_info.cur_prot_state.prot_method != 0)) + +#define SA_PROT_LEN(softc) softc->prot_info.cur_prot_state.pi_length + struct sa_softc { sa_state state; sa_flags flags; sa_quirks quirks; u_int si_flags; + struct cam_periph *periph; struct bio_queue_head bio_queue; int queue_count; struct devstat *device_stats; struct sa_devs devs; + int open_count; + int num_devs_to_destroy; int blk_gran; int blk_mask; int blk_shift; @@ -231,12 +350,37 @@ struct sa_softc { int filemarks; union ccb saved_ccb; int last_resid_was_io; + uint8_t density_type_bits[SA_DENSITY_TYPES]; + int density_info_valid[SA_DENSITY_TYPES]; + uint8_t density_info[SA_DENSITY_TYPES][SRDS_MAX_LENGTH]; + + struct sa_prot_info prot_info; + + int sili; + int eot_warn; /* - * Relative to BOT Location. + * Current position information. -1 means that the given value is + * unknown. fileno and blkno are always calculated. blkno is + * relative to the previous file mark. rep_fileno and rep_blkno + * are as reported by the drive, if it supports the long form + * report for the READ POSITION command. rep_blkno is relative to + * the beginning of the partition. + * + * bop means that the drive is at the beginning of the partition. + * eop means that the drive is between early warning and end of + * partition, inside the current partition. + * bpew means that the position is in a PEWZ (Programmable Early + * Warning Zone) */ - daddr_t fileno; - daddr_t blkno; + daddr_t partition; /* Absolute from BOT */ + daddr_t fileno; /* Relative to beginning of partition */ + daddr_t blkno; /* Relative to last file mark */ + daddr_t rep_blkno; /* Relative to beginning of partition */ + daddr_t rep_fileno; /* Relative to beginning of partition */ + int bop; /* Beginning of Partition */ + int eop; /* End of Partition */ + int bpew; /* Beyond Programmable Early Warning */ /* * Latched Error Info @@ -403,16 +547,42 @@ static int sagetparams(struct cam_periph *periph, u_int8_t *write_protect, u_int8_t *speed, int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm, - sa_comp_t *comp_page); + sa_comp_t *comp_page, + struct scsi_control_data_prot_subpage + *prot_page, int dp_size, + int prot_changeable); +static int sasetprot(struct cam_periph *periph, + struct sa_prot_state *new_prot); static int sasetparams(struct cam_periph *periph, sa_params params_to_set, u_int32_t blocksize, u_int8_t density, u_int32_t comp_algorithm, u_int32_t sense_flags); +static int sasetsili(struct cam_periph *periph, + struct mtparamset *ps, int num_params); +static int saseteotwarn(struct cam_periph *periph, + struct mtparamset *ps, int num_params); +static void safillprot(struct sa_softc *softc, int *indent, + struct sbuf *sb); +static void sapopulateprots(struct sa_prot_state *cur_state, + struct sa_prot_map *new_table, + int table_ents); +static struct sa_prot_map *safindprotent(char *name, struct sa_prot_map *table, + int table_ents); +static int sasetprotents(struct cam_periph *periph, + struct mtparamset *ps, int num_params); +static struct sa_param_ent *safindparament(struct mtparamset *ps); +static int saparamsetlist(struct cam_periph *periph, + struct mtsetlist *list, int need_copy); +static int saextget(struct cdev *dev, struct cam_periph *periph, + struct sbuf *sb, struct mtextget *g); +static int saparamget(struct sa_softc *softc, struct sbuf *sb); static void saprevent(struct cam_periph *periph, int action); static int sarewind(struct cam_periph *periph); static int saspace(struct cam_periph *periph, int count, scsi_space_code code); +static void sadevgonecb(void *arg); +static void sasetupdev(struct sa_softc *softc, struct cdev *dev); static int samount(struct cam_periph *, int, struct cdev *); static int saretension(struct cam_periph *periph); static int sareservereleaseunit(struct cam_periph *periph, @@ -420,9 +590,16 @@ static int sareservereleaseunit(struct cam_periph *periph, static int saloadunload(struct cam_periph *periph, int load); static int saerase(struct cam_periph *periph, int longerase); static int sawritefilemarks(struct cam_periph *periph, - int nmarks, int setmarks); + int nmarks, int setmarks, int immed); +static int sagetpos(struct cam_periph *periph); static int sardpos(struct cam_periph *periph, int, u_int32_t *); -static int sasetpos(struct cam_periph *periph, int, u_int32_t *); +static int sasetpos(struct cam_periph *periph, int, + struct mtlocate *); +static void safilldenstypesb(struct sbuf *sb, int *indent, + uint8_t *buf, int buf_len, + int is_density); +static void safilldensitysb(struct sa_softc *softc, int *indent, + struct sbuf *sb); #ifndef SA_DEFAULT_IO_SPLIT @@ -464,7 +641,7 @@ static struct cdevsw sa_cdevsw = { .d_ioctl = saioctl, .d_strategy = sastrategy, .d_name = "sa", - .d_flags = D_TAPE, + .d_flags = D_TAPE | D_TRACKCLOSE, }; static int @@ -488,6 +665,7 @@ saopen(struct cdev *dev, int flags, int fmt, struct thread *td) if (SA_IS_CTRL(dev)) { softc->ctrl_mode = 1; + softc->open_count++; cam_periph_unlock(periph); return (0); } @@ -519,6 +697,7 @@ saopen(struct cdev *dev, int flags, int fmt, struct thread *td) if (error && (flags & O_NONBLOCK)) { softc->flags |= SA_FLAG_OPEN; softc->open_pending_mount = 1; + softc->open_count++; cam_periph_unhold(periph); cam_periph_unlock(periph); return (0); @@ -534,6 +713,7 @@ saopen(struct cdev *dev, int flags, int fmt, struct thread *td) saprevent(periph, PR_PREVENT); softc->flags |= SA_FLAG_OPEN; + softc->open_count++; cam_periph_unhold(periph); cam_periph_unlock(periph); @@ -545,7 +725,7 @@ saclose(struct cdev *dev, int flag, int fmt, struct thread *td) { struct cam_periph *periph; struct sa_softc *softc; - int mode, error, writing, tmp; + int mode, error, writing, tmp, i; int closedbits = SA_FLAG_OPEN; mode = SAMODE(dev); @@ -564,6 +744,7 @@ saclose(struct cdev *dev, int flag, int fmt, struct thread *td) softc->open_rdonly = 0; if (SA_IS_CTRL(dev)) { softc->ctrl_mode = 0; + softc->open_count--; cam_periph_unlock(periph); cam_periph_release(periph); return (0); @@ -572,6 +753,7 @@ saclose(struct cdev *dev, int flag, int fmt, struct thread *td) if (softc->open_pending_mount) { softc->flags &= ~SA_FLAG_OPEN; softc->open_pending_mount = 0; + softc->open_count--; cam_periph_unlock(periph); cam_periph_release(periph); return (0); @@ -676,6 +858,16 @@ saclose(struct cdev *dev, int flag, int fmt, struct thread *td) * And we are no longer open for business. */ softc->flags &= ~closedbits; + softc->open_count--; + + /* + * Invalidate any density information that depends on having tape + * media in the drive. + */ + for (i = 0; i < SA_DENSITY_TYPES; i++) { + if (softc->density_type_bits[i] & SRDS_MEDIA) + softc->density_info_valid[i] = 0; + } /* * Inform users if tape state if frozen.... @@ -824,6 +1016,480 @@ sastrategy(struct bio *bp) return; } +static int +sasetsili(struct cam_periph *periph, struct mtparamset *ps, int num_params) +{ + uint32_t sili_blocksize; + struct sa_softc *softc; + int error; + + error = 0; + softc = (struct sa_softc *)periph->softc; + + if (ps->value_type != MT_PARAM_SET_SIGNED) { + snprintf(ps->error_str, sizeof(ps->error_str), + "sili is a signed parameter"); + goto bailout; + } + if ((ps->value.value_signed < 0) + || (ps->value.value_signed > 1)) { + snprintf(ps->error_str, sizeof(ps->error_str), + "invalid sili value %jd", (intmax_t)ps->value.value_signed); + goto bailout_error; + } + /* + * We only set the SILI flag in variable block + * mode. You'll get a check condition in fixed + * block mode if things don't line up in any case. + */ + if (softc->flags & SA_FLAG_FIXED) { + snprintf(ps->error_str, sizeof(ps->error_str), + "can't set sili bit in fixed block mode"); + goto bailout_error; + } + if (softc->sili == ps->value.value_signed) + goto bailout; + + if (ps->value.value_signed == 1) + sili_blocksize = 4; + else + sili_blocksize = 0; + + error = sasetparams(periph, SA_PARAM_BLOCKSIZE, + sili_blocksize, 0, 0, SF_QUIET_IR); + if (error != 0) { + snprintf(ps->error_str, sizeof(ps->error_str), + "sasetparams() returned error %d", error); + goto bailout_error; + } + + softc->sili = ps->value.value_signed; + +bailout: + ps->status = MT_PARAM_STATUS_OK; + return (error); + +bailout_error: + ps->status = MT_PARAM_STATUS_ERROR; + if (error == 0) + error = EINVAL; + + return (error); +} + +static int +saseteotwarn(struct cam_periph *periph, struct mtparamset *ps, int num_params) +{ + struct sa_softc *softc; + int error; + + error = 0; + softc = (struct sa_softc *)periph->softc; + + if (ps->value_type != MT_PARAM_SET_SIGNED) { + snprintf(ps->error_str, sizeof(ps->error_str), + "eot_warn is a signed parameter"); + ps->status = MT_PARAM_STATUS_ERROR; + goto bailout; + } + if ((ps->value.value_signed < 0) + || (ps->value.value_signed > 1)) { + snprintf(ps->error_str, sizeof(ps->error_str), + "invalid eot_warn value %jd\n", + (intmax_t)ps->value.value_signed); + ps->status = MT_PARAM_STATUS_ERROR; + goto bailout; + } + softc->eot_warn = ps->value.value_signed; + ps->status = MT_PARAM_STATUS_OK; +bailout: + if (ps->status != MT_PARAM_STATUS_OK) + error = EINVAL; + + return (error); +} + + +static void +safillprot(struct sa_softc *softc, int *indent, struct sbuf *sb) +{ + int tmpint; + + SASBADDNODE(sb, *indent, protection); + if (softc->flags & SA_FLAG_PROTECT_SUPP) + tmpint = 1; + else + tmpint = 0; + SASBADDINTDESC(sb, *indent, tmpint, %d, protection_supported, + "Set to 1 if protection information is supported"); + + if ((tmpint != 0) + && (softc->prot_info.cur_prot_state.initialized != 0)) { + struct sa_prot_state *prot; + + prot = &softc->prot_info.cur_prot_state; + + SASBADDUINTDESC(sb, *indent, prot->prot_method, %u, + prot_method, "Current Protection Method"); + SASBADDUINTDESC(sb, *indent, prot->pi_length, %u, + pi_length, "Length of Protection Information"); + SASBADDUINTDESC(sb, *indent, prot->lbp_w, %u, + lbp_w, "Check Protection on Writes"); + SASBADDUINTDESC(sb, *indent, prot->lbp_r, %u, + lbp_r, "Check and Include Protection on Reads"); + SASBADDUINTDESC(sb, *indent, prot->rbdp, %u, + rbdp, "Transfer Protection Information for RECOVER " + "BUFFERED DATA command"); + } + SASBENDNODE(sb, *indent, protection); +} + +static void +sapopulateprots(struct sa_prot_state *cur_state, struct sa_prot_map *new_table, + int table_ents) +{ + int i; + + bcopy(sa_prot_table, new_table, min(table_ents * sizeof(*new_table), + sizeof(sa_prot_table))); + + table_ents = min(table_ents, SA_NUM_PROT_ENTS); + + for (i = 0; i < table_ents; i++) + new_table[i].value = (uint32_t *)((uint8_t *)cur_state + + new_table[i].offset); + + return; +} + +static struct sa_prot_map * +safindprotent(char *name, struct sa_prot_map *table, int table_ents) +{ + char *prot_name = "protection."; + int i, prot_len; + + prot_len = strlen(prot_name); + + /* + * This shouldn't happen, but we check just in case. + */ + if (strncmp(name, prot_name, prot_len) != 0) + goto bailout; + + for (i = 0; i < table_ents; i++) { + if (strcmp(&name[prot_len], table[i].name) != 0) + continue; + return (&table[i]); + } +bailout: + return (NULL); +} + +static int +sasetprotents(struct cam_periph *periph, struct mtparamset *ps, int num_params) +{ + struct sa_softc *softc; + struct sa_prot_map prot_ents[SA_NUM_PROT_ENTS]; + struct sa_prot_state new_state; + int error; + int i; + + softc = (struct sa_softc *)periph->softc; + error = 0; + + /* + * Make sure that this tape drive supports protection information. + * Otherwise we can't set anything. + */ + if ((softc->flags & SA_FLAG_PROTECT_SUPP) == 0) { + snprintf(ps[0].error_str, sizeof(ps[0].error_str), + "Protection information is not supported for this device"); + ps[0].status = MT_PARAM_STATUS_ERROR; + goto bailout; + } + + /* + * We can't operate with physio(9) splitting enabled, because there + * is no way to insure (especially in variable block mode) that + * what the user writes (with a checksum block at the end) will + * make it into the sa(4) driver intact. + */ + if ((softc->si_flags & SI_NOSPLIT) == 0) { + snprintf(ps[0].error_str, sizeof(ps[0].error_str), + "Protection information cannot be enabled with I/O " + "splitting"); + ps[0].status = MT_PARAM_STATUS_ERROR; + goto bailout; + } + + /* + * Take the current cached protection state and use that as the + * basis for our new entries. + */ + bcopy(&softc->prot_info.cur_prot_state, &new_state, sizeof(new_state)); + + /* + * Populate the table mapping property names to pointers into the + * state structure. + */ + sapopulateprots(&new_state, prot_ents, SA_NUM_PROT_ENTS); + + /* + * For each parameter the user passed in, make sure the name, type + * and value are valid. + */ + for (i = 0; i < num_params; i++) { + struct sa_prot_map *ent; + + ent = safindprotent(ps[i].value_name, prot_ents, + SA_NUM_PROT_ENTS); + if (ent == NULL) { + ps[i].status = MT_PARAM_STATUS_ERROR; + snprintf(ps[i].error_str, sizeof(ps[i].error_str), + "Invalid protection entry name %s", + ps[i].value_name); + error = EINVAL; + goto bailout; + } + if (ent->param_type != ps[i].value_type) { + ps[i].status = MT_PARAM_STATUS_ERROR; + snprintf(ps[i].error_str, sizeof(ps[i].error_str), + "Supplied type %d does not match actual type %d", + ps[i].value_type, ent->param_type); + error = EINVAL; + goto bailout; + } + if ((ps[i].value.value_unsigned < ent->min_val) + || (ps[i].value.value_unsigned > ent->max_val)) { + ps[i].status = MT_PARAM_STATUS_ERROR; + snprintf(ps[i].error_str, sizeof(ps[i].error_str), + "Value %ju is outside valid range %u - %u", + (uintmax_t)ps[i].value.value_unsigned, ent->min_val, + ent->max_val); + error = EINVAL; + goto bailout; + } + *(ent->value) = ps[i].value.value_unsigned; + } + + /* + * Actually send the protection settings to the drive. + */ + error = sasetprot(periph, &new_state); + if (error != 0) { + for (i = 0; i < num_params; i++) { + ps[i].status = MT_PARAM_STATUS_ERROR; + snprintf(ps[i].error_str, sizeof(ps[i].error_str), + "Unable to set parameter, see dmesg(8)"); + } + goto bailout; + } + + /* + * Let the user know that his settings were stored successfully. + */ + for (i = 0; i < num_params; i++) + ps[i].status = MT_PARAM_STATUS_OK; + +bailout: + return (error); +} +/* + * Entry handlers generally only handle a single entry. Node handlers will + * handle a contiguous range of parameters to set in a single call. + */ +typedef enum { + SA_PARAM_TYPE_ENTRY, + SA_PARAM_TYPE_NODE +} sa_param_type; + +struct sa_param_ent { + char *name; + sa_param_type param_type; + int (*set_func)(struct cam_periph *periph, struct mtparamset *ps, + int num_params); +} sa_param_table[] = { + {"sili", SA_PARAM_TYPE_ENTRY, sasetsili }, + {"eot_warn", SA_PARAM_TYPE_ENTRY, saseteotwarn }, + {"protection.", SA_PARAM_TYPE_NODE, sasetprotents } +}; + +static struct sa_param_ent * +safindparament(struct mtparamset *ps) +{ + unsigned int i; + + for (i = 0; i < sizeof(sa_param_table) /sizeof(sa_param_table[0]); i++){ + /* + * For entries, we compare all of the characters. For + * nodes, we only compare the first N characters. The node + * handler will decode the rest. + */ + if (sa_param_table[i].param_type == SA_PARAM_TYPE_ENTRY) { + if (strcmp(ps->value_name, sa_param_table[i].name) != 0) + continue; + } else { + if (strncmp(ps->value_name, sa_param_table[i].name, + strlen(sa_param_table[i].name)) != 0) + continue; + } + return (&sa_param_table[i]); + } + + return (NULL); +} + +/* + * Go through a list of parameters, coalescing contiguous parameters with + * the same parent node into a single call to a set_func. + */ +static int +saparamsetlist(struct cam_periph *periph, struct mtsetlist *list, + int need_copy) +{ + int i, contig_ents; + int error; + struct mtparamset *params, *first; + struct sa_param_ent *first_ent; + + error = 0; + params = NULL; + + if (list->num_params == 0) + /* Nothing to do */ + goto bailout; + + /* + * Verify that the user has the correct structure size. + */ + if ((list->num_params * sizeof(struct mtparamset)) != + list->param_len) { + xpt_print(periph->path, "%s: length of params %d != " + "sizeof(struct mtparamset) %zd * num_params %d\n", + __func__, list->param_len, sizeof(struct mtparamset), + list->num_params); + error = EINVAL; + goto bailout; + } + + if (need_copy != 0) { + /* + * XXX KDM will dropping the lock cause an issue here? + */ + cam_periph_unlock(periph); + params = malloc(list->param_len, M_SCSISA, M_WAITOK | M_ZERO); + error = copyin(list->params, params, list->param_len); + cam_periph_lock(periph); + + if (error != 0) + goto bailout; + } else { + params = list->params; + } + + contig_ents = 0; + first = NULL; + first_ent = NULL; + for (i = 0; i < list->num_params; i++) { + struct sa_param_ent *ent; + + ent = safindparament(¶ms[i]); + if (ent == NULL) { + snprintf(params[i].error_str, + sizeof(params[i].error_str), + "%s: cannot find parameter %s", __func__, + params[i].value_name); + params[i].status = MT_PARAM_STATUS_ERROR; + break; + } + + if (first != NULL) { + if (first_ent == ent) { + /* + * We're still in a contiguous list of + * parameters that can be handled by one + * node handler. + */ + contig_ents++; + continue; + } else { + error = first_ent->set_func(periph, first, + contig_ents); + first = NULL; + first_ent = NULL; + contig_ents = 0; + if (error != 0) { + error = 0; + break; + } + } + } + if (ent->param_type == SA_PARAM_TYPE_NODE) { + first = ¶ms[i]; + first_ent = ent; + contig_ents = 1; + } else { + error = ent->set_func(periph, ¶ms[i], 1); + if (error != 0) { + error = 0; + break; + } + } + } + if (first != NULL) + first_ent->set_func(periph, first, contig_ents); + +bailout: + if (need_copy != 0) { + if (error != EFAULT) { + cam_periph_unlock(periph); + copyout(params, list->params, list->param_len); + cam_periph_lock(periph); + } + free(params, M_SCSISA); + } + return (error); +} + +static int +sagetparams_common(struct cdev *dev, struct cam_periph *periph) +{ + struct sa_softc *softc; + u_int8_t write_protect; + int comp_enabled, comp_supported, error; + + softc = (struct sa_softc *)periph->softc; + + if (softc->open_pending_mount) + return (0); + + /* The control device may issue getparams() if there are no opens. */ + if (SA_IS_CTRL(dev) && (softc->flags & SA_FLAG_OPEN) != 0) + return (0); + + error = sagetparams(periph, SA_PARAM_ALL, &softc->media_blksize, + &softc->media_density, &softc->media_numblks, &softc->buffer_mode, + &write_protect, &softc->speed, &comp_supported, &comp_enabled, + &softc->comp_algorithm, NULL, NULL, 0, 0); + if (error) + return (error); + if (write_protect) + softc->flags |= SA_FLAG_TAPE_WP; + else + softc->flags &= ~SA_FLAG_TAPE_WP; + softc->flags &= ~SA_FLAG_COMPRESSION; + if (comp_supported) { + if (softc->saved_comp_algorithm == 0) + softc->saved_comp_algorithm = + softc->comp_algorithm; + softc->flags |= SA_FLAG_COMP_SUPP; + if (comp_enabled) + softc->flags |= SA_FLAG_COMP_ENABLED; + } else + softc->flags |= SA_FLAG_COMP_UNSUPP; + + return (0); +} #define PENDING_MOUNT_CHECK(softc, periph, dev) \ if (softc->open_pending_mount) { \ @@ -868,6 +1534,9 @@ saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) switch (cmd) { case MTIOCGETEOTMODEL: case MTIOCGET: + case MTIOCEXTGET: + case MTIOCPARAMGET: + case MTIOCRBLIM: break; case MTIOCERRSTAT: /* @@ -939,36 +1608,9 @@ saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) { struct mtget *g = (struct mtget *)arg; - /* - * If this isn't the control mode device, actually go out - * and ask the drive again what it's set to. - */ - if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) { - u_int8_t write_protect; - int comp_enabled, comp_supported; - error = sagetparams(periph, SA_PARAM_ALL, - &softc->media_blksize, &softc->media_density, - &softc->media_numblks, &softc->buffer_mode, - &write_protect, &softc->speed, &comp_supported, - &comp_enabled, &softc->comp_algorithm, NULL); - if (error) - break; - if (write_protect) - softc->flags |= SA_FLAG_TAPE_WP; - else - softc->flags &= ~SA_FLAG_TAPE_WP; - softc->flags &= ~(SA_FLAG_COMP_SUPP| - SA_FLAG_COMP_ENABLED|SA_FLAG_COMP_UNSUPP); - if (comp_supported) { - if (softc->saved_comp_algorithm == 0) - softc->saved_comp_algorithm = - softc->comp_algorithm; - softc->flags |= SA_FLAG_COMP_SUPP; - if (comp_enabled) - softc->flags |= SA_FLAG_COMP_ENABLED; - } else - softc->flags |= SA_FLAG_COMP_UNSUPP; - } + error = sagetparams_common(dev, periph); + if (error) + break; bzero(g, sizeof(struct mtget)); g->mt_type = MT_ISAR; if (softc->flags & SA_FLAG_COMP_UNSUPP) { @@ -1020,15 +1662,93 @@ saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) error = 0; break; } - case MTIOCERRSTAT: + case MTIOCEXTGET: + case MTIOCPARAMGET: { - struct scsi_tape_errors *sep = - &((union mterrstat *)arg)->scsi_errstat; + struct mtextget *g = (struct mtextget *)arg; + char *tmpstr2; + struct sbuf *sb; - CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, - ("saioctl: MTIOCERRSTAT\n")); + /* + * Report drive status using an XML format. + */ - bzero(sep, sizeof(*sep)); + /* + * XXX KDM will dropping the lock cause any problems here? + */ + cam_periph_unlock(periph); + sb = sbuf_new(NULL, NULL, g->alloc_len, SBUF_FIXEDLEN); + if (sb == NULL) { + g->status = MT_EXT_GET_ERROR; + snprintf(g->error_str, sizeof(g->error_str), + "Unable to allocate %d bytes for status info", + g->alloc_len); + cam_periph_lock(periph); + goto extget_bailout; + } + cam_periph_lock(periph); + + if (cmd == MTIOCEXTGET) + error = saextget(dev, periph, sb, g); + else + error = saparamget(softc, sb); + + if (error != 0) + goto extget_bailout; + + error = sbuf_finish(sb); + if (error == ENOMEM) { + g->status = MT_EXT_GET_NEED_MORE_SPACE; + error = 0; + } else if (error != 0) { + g->status = MT_EXT_GET_ERROR; + snprintf(g->error_str, sizeof(g->error_str), + "Error %d returned from sbuf_finish()", error); + } else + g->status = MT_EXT_GET_OK; + + error = 0; + tmpstr2 = sbuf_data(sb); + g->fill_len = strlen(tmpstr2) + 1; + cam_periph_unlock(periph); + + error = copyout(tmpstr2, g->status_xml, g->fill_len); + + cam_periph_lock(periph); + +extget_bailout: + sbuf_delete(sb); + break; + } + case MTIOCPARAMSET: + { + struct mtsetlist list; + struct mtparamset *ps = (struct mtparamset *)arg; + + bzero(&list, sizeof(list)); + list.num_params = 1; + list.param_len = sizeof(*ps); + list.params = ps; + + error = saparamsetlist(periph, &list, /*need_copy*/ 0); + break; + } + case MTIOCSETLIST: + { + struct mtsetlist *list = (struct mtsetlist *)arg; + + error = saparamsetlist(periph, list, /*need_copy*/ 1); + break; + } + case MTIOCERRSTAT: + { + struct scsi_tape_errors *sep = + &((union mterrstat *)arg)->scsi_errstat; + + CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, + ("saioctl: MTIOCERRSTAT\n")); + + bzero(sep, sizeof(*sep)); sep->io_resid = softc->last_io_resid; bcopy((caddr_t) &softc->last_io_sense, sep->io_sense, sizeof (sep->io_sense)); @@ -1040,7 +1760,7 @@ saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) bcopy((caddr_t) &softc->last_ctl_cdb, sep->ctl_cdb, sizeof (sep->ctl_cdb)); - if ((SA_IS_CTRL(dev) == 0 && softc->open_pending_mount) || + if ((SA_IS_CTRL(dev) == 0 && !softc->open_pending_mount) || didlockperiph) bzero((caddr_t) &softc->errinfo, sizeof (softc->errinfo)); @@ -1067,13 +1787,17 @@ saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) /* * We don't need to clear the SA_FLAG_TAPE_WRITTEN * flag because by keeping track of filemarks - * we have last written we know ehether or not + * we have last written we know whether or not * we need to write more when we close the device. */ - error = sawritefilemarks(periph, count, FALSE); + error = sawritefilemarks(periph, count, FALSE, FALSE); + break; + case MTWEOFI: + /* write an end-of-file marker without waiting */ + error = sawritefilemarks(periph, count, FALSE, TRUE); break; case MTWSS: /* write a setmark */ - error = sawritefilemarks(periph, count, TRUE); + error = sawritefilemarks(periph, count, TRUE, FALSE); break; case MTBSR: /* backward space record */ case MTFSR: /* forward space record */ @@ -1198,6 +1922,9 @@ saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) } break; + case MTLOAD: + error = saloadunload(periph, TRUE); + break; case MTNOP: /* no operation, sets status only */ case MTCACHE: /* enable controller cache */ case MTNOCACHE: /* disable controller cache */ @@ -1208,6 +1935,13 @@ saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) PENDING_MOUNT_CHECK(softc, periph, dev); + if ((softc->sili != 0) + && (count != 0)) { + xpt_print(periph->path, "Can't enter fixed " + "block mode with SILI enabled\n"); + error = EINVAL; + break; + } error = sasetparams(periph, SA_PARAM_BLOCKSIZE, count, 0, 0, 0); if (error == 0) { @@ -1293,12 +2027,29 @@ saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) error = sardpos(periph, 1, (u_int32_t *) arg); break; case MTIOCSLOCATE: + case MTIOCHLOCATE: { + struct mtlocate locate_info; + int hard; + + bzero(&locate_info, sizeof(locate_info)); + locate_info.logical_id = *((uint32_t *)arg); + if (cmd == MTIOCSLOCATE) + hard = 0; + else + hard = 1; + PENDING_MOUNT_CHECK(softc, periph, dev); - error = sasetpos(periph, 0, (u_int32_t *) arg); + + error = sasetpos(periph, hard, &locate_info); break; - case MTIOCHLOCATE: + } + case MTIOCEXTLOCATE: PENDING_MOUNT_CHECK(softc, periph, dev); - error = sasetpos(periph, 1, (u_int32_t *) arg); + error = sasetpos(periph, /*hard*/ 0, (struct mtlocate *)arg); + softc->flags &= + ~(SA_FLAG_TAPE_WRITTEN|SA_FLAG_TAPE_FROZEN); + softc->flags &= ~SA_FLAG_ERR_PENDING; + softc->filemarks = 0; break; case MTIOCGETEOTMODEL: error = 0; @@ -1324,6 +2075,16 @@ saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) break; } break; + case MTIOCRBLIM: { + struct mtrblim *rblim; + + rblim = (struct mtrblim *)arg; + + rblim->granularity = softc->blk_gran; + rblim->min_block_length = softc->min_blk; + rblim->max_block_length = softc->max_blk; + break; + } default: error = cam_periph_ioctl(periph, cmd, arg, saerror); break; @@ -1338,8 +2099,14 @@ saioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) case MTIOCRDHPOS: case MTIOCSLOCATE: case MTIOCHLOCATE: + /* + * XXX KDM look at this. + */ softc->fileno = (daddr_t) -1; softc->blkno = (daddr_t) -1; + softc->rep_blkno = (daddr_t) -1; + softc->rep_fileno = (daddr_t) -1; + softc->partition = (daddr_t) -1; softc->flags &= ~SA_FLAG_TAPE_FROZEN; xpt_print(periph->path, "tape state now unfrozen.\n"); @@ -1371,6 +2138,51 @@ sainit(void) } } +static void +sadevgonecb(void *arg) +{ + struct cam_periph *periph; + struct mtx *mtx; + struct sa_softc *softc; + + periph = (struct cam_periph *)arg; + softc = (struct sa_softc *)periph->softc; + + mtx = cam_periph_mtx(periph); + mtx_lock(mtx); + + softc->num_devs_to_destroy--; + if (softc->num_devs_to_destroy == 0) { + int i; + + /* + * When we have gotten all of our callbacks, we will get + * no more close calls from devfs. So if we have any + * dangling opens, we need to release the reference held + * for that particular context. + */ + for (i = 0; i < softc->open_count; i++) + cam_periph_release_locked(periph); + + softc->open_count = 0; + + /* + * Release the reference held for devfs, all of our + * instances are gone now. + */ + cam_periph_release_locked(periph); + } + + /* + * We reference the lock directly here, instead of using + * cam_periph_unlock(). The reason is that the final call to + * cam_periph_release_locked() above could result in the periph + * getting freed. If that is the case, dereferencing the periph + * with a cam_periph_unlock() call would cause a page fault. + */ + mtx_unlock(mtx); +} + static void saoninvalidate(struct cam_periph *periph) { @@ -1392,25 +2204,34 @@ saoninvalidate(struct cam_periph *periph) */ bioq_flush(&softc->bio_queue, NULL, ENXIO); softc->queue_count = 0; + + /* + * Tell devfs that all of our devices have gone away, and ask for a + * callback when it has cleaned up its state. + */ + destroy_dev_sched_cb(softc->devs.ctl_dev, sadevgonecb, periph); + destroy_dev_sched_cb(softc->devs.r_dev, sadevgonecb, periph); + destroy_dev_sched_cb(softc->devs.nr_dev, sadevgonecb, periph); + destroy_dev_sched_cb(softc->devs.er_dev, sadevgonecb, periph); } static void sacleanup(struct cam_periph *periph) { struct sa_softc *softc; - int i; softc = (struct sa_softc *)periph->softc; - devstat_remove_entry(softc->device_stats); cam_periph_unlock(periph); - destroy_dev(softc->devs.ctl_dev); - for (i = 0; i < SA_NUM_MODES; i++) { - destroy_dev(softc->devs.mode_devs[i].r_dev); - destroy_dev(softc->devs.mode_devs[i].nr_dev); - destroy_dev(softc->devs.mode_devs[i].er_dev); - } + + if ((softc->flags & SA_FLAG_SCTX_INIT) != 0 + && sysctl_ctx_free(&softc->sysctl_ctx) != 0) + xpt_print(periph->path, "can't remove sysctl context\n"); + cam_periph_lock(periph); + + devstat_remove_entry(softc->device_stats); + free(softc, M_SCSISA); } @@ -1459,6 +2280,21 @@ saasync(void *callback_arg, u_int32_t code, } } +static void +sasetupdev(struct sa_softc *softc, struct cdev *dev) +{ + dev->si_drv1 = softc->periph; + dev->si_iosize_max = softc->maxio; + dev->si_flags |= softc->si_flags; + /* + * Keep a count of how many non-alias devices we have created, + * so we can make sure we clean them all up on shutdown. Aliases + * are cleaned up when we destroy the device they're an alias for. + */ + if ((dev->si_flags & SI_ALIAS) == 0) + softc->num_devs_to_destroy++; +} + static void sasysctlinit(void *context, int pending) { @@ -1479,6 +2315,7 @@ sasysctlinit(void *context, int pending) snprintf(tmpstr2, sizeof(tmpstr2), "%u", periph->unit_number); sysctl_ctx_init(&softc->sysctl_ctx); + softc->flags |= SA_FLAG_SCTX_INIT; softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx, SYSCTL_STATIC_CHILDREN(_kern_cam_sa), OID_AUTO, tmpstr2, CTLFLAG_RD, 0, tmpstr); @@ -1510,7 +2347,6 @@ saregister(struct cam_periph *periph, void *arg) struct ccb_pathinq cpi; caddr_t match; char tmpstr[80]; - int i; cgd = (struct ccb_getdev *)arg; if (cgd == NULL) { @@ -1529,8 +2365,15 @@ saregister(struct cam_periph *periph, void *arg) softc->state = SA_STATE_NORMAL; softc->fileno = (daddr_t) -1; softc->blkno = (daddr_t) -1; + softc->rep_fileno = (daddr_t) -1; + softc->rep_blkno = (daddr_t) -1; + softc->partition = (daddr_t) -1; + softc->bop = -1; + softc->eop = -1; + softc->bpew = -1; bioq_init(&softc->bio_queue); + softc->periph = periph; periph->softc = softc; /* @@ -1548,6 +2391,41 @@ saregister(struct cam_periph *periph, void *arg) } else softc->quirks = SA_QUIRK_NONE; + /* + * Long format data for READ POSITION was introduced in SSC, which + * was after SCSI-2. (Roughly equivalent to SCSI-3.) If the drive + * reports that it is SCSI-2 or older, it is unlikely to support + * long position data, but it might. Some drives from that era + * claim to be SCSI-2, but do support long position information. + * So, instead of immediately disabling long position information + * for SCSI-2 devices, we'll try one pass through sagetpos(), and + * then disable long position information if we get an error. + */ + if (cgd->inq_data.version <= SCSI_REV_CCS) + softc->quirks |= SA_QUIRK_NO_LONG_POS; + + if (cgd->inq_data.spc3_flags & SPC3_SID_PROTECT) { + struct ccb_dev_advinfo cdai; + struct scsi_vpd_extended_inquiry_data ext_inq; + + bzero(&ext_inq, sizeof(ext_inq)); + + xpt_setup_ccb(&cdai.ccb_h, periph->path, CAM_PRIORITY_NORMAL); + + cdai.ccb_h.func_code = XPT_DEV_ADVINFO; + cdai.flags = CDAI_FLAG_NONE; + cdai.buftype = CDAI_TYPE_EXT_INQ; + cdai.bufsiz = sizeof(ext_inq); + cdai.buf = (uint8_t *)&ext_inq; + xpt_action((union ccb *)&cdai); + + if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0) + cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE); + if ((cdai.ccb_h.status == CAM_REQ_CMP) + && (ext_inq.flags1 & SVPD_EID_SA_SPT_LBP)) + softc->flags |= SA_FLAG_PROTECT_SUPP; + } + bzero(&cpi, sizeof(cpi)); xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); cpi.ccb_h.func_code = XPT_PATH_INQ; @@ -1614,66 +2492,44 @@ saregister(struct cam_periph *periph, void *arg) if (cpi.hba_misc & PIM_UNMAPPED) softc->si_flags |= SI_UNMAPPED; + /* + * Acquire a reference to the periph before we create the devfs + * instances for it. We'll release this reference once the devfs + * instances have been freed. + */ + if (cam_periph_acquire(periph) != CAM_REQ_CMP) { + xpt_print(periph->path, "%s: lost periph during " + "registration!\n", __func__); + cam_periph_lock(periph); + return (CAM_REQ_CMP_ERR); + } + softc->devs.ctl_dev = make_dev(&sa_cdevsw, SAMINOR(SA_CTLDEV, - 0, SA_ATYPE_R), UID_ROOT, GID_OPERATOR, + SA_ATYPE_R), UID_ROOT, GID_OPERATOR, 0660, "%s%d.ctl", periph->periph_name, periph->unit_number); - softc->devs.ctl_dev->si_drv1 = periph; - softc->devs.ctl_dev->si_iosize_max = softc->maxio; - softc->devs.ctl_dev->si_flags |= softc->si_flags; - - for (i = 0; i < SA_NUM_MODES; i++) { - - softc->devs.mode_devs[i].r_dev = make_dev(&sa_cdevsw, - SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_R), - UID_ROOT, GID_OPERATOR, 0660, "%s%d.%d", - periph->periph_name, periph->unit_number, i); - softc->devs.mode_devs[i].r_dev->si_drv1 = periph; - softc->devs.mode_devs[i].r_dev->si_iosize_max = softc->maxio; - softc->devs.mode_devs[i].r_dev->si_flags |= softc->si_flags; - - softc->devs.mode_devs[i].nr_dev = make_dev(&sa_cdevsw, - SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_NR), - UID_ROOT, GID_OPERATOR, 0660, "n%s%d.%d", - periph->periph_name, periph->unit_number, i); - softc->devs.mode_devs[i].nr_dev->si_drv1 = periph; - softc->devs.mode_devs[i].nr_dev->si_iosize_max = softc->maxio; - softc->devs.mode_devs[i].nr_dev->si_flags |= softc->si_flags; - - softc->devs.mode_devs[i].er_dev = make_dev(&sa_cdevsw, - SAMINOR(SA_NOT_CTLDEV, i, SA_ATYPE_ER), - UID_ROOT, GID_OPERATOR, 0660, "e%s%d.%d", - periph->periph_name, periph->unit_number, i); - softc->devs.mode_devs[i].er_dev->si_drv1 = periph; - softc->devs.mode_devs[i].er_dev->si_iosize_max = softc->maxio; - softc->devs.mode_devs[i].er_dev->si_flags |= softc->si_flags; + sasetupdev(softc, softc->devs.ctl_dev); - /* - * Make the (well known) aliases for the first mode. - */ - if (i == 0) { - struct cdev *alias; + softc->devs.r_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV, + SA_ATYPE_R), UID_ROOT, GID_OPERATOR, + 0660, "%s%d", periph->periph_name, periph->unit_number); + sasetupdev(softc, softc->devs.r_dev); - alias = make_dev_alias(softc->devs.mode_devs[i].r_dev, - "%s%d", periph->periph_name, periph->unit_number); - alias->si_drv1 = periph; - alias->si_iosize_max = softc->maxio; - alias->si_flags |= softc->si_flags; + softc->devs.nr_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV, + SA_ATYPE_NR), UID_ROOT, GID_OPERATOR, + 0660, "n%s%d", periph->periph_name, periph->unit_number); + sasetupdev(softc, softc->devs.nr_dev); - alias = make_dev_alias(softc->devs.mode_devs[i].nr_dev, - "n%s%d", periph->periph_name, periph->unit_number); - alias->si_drv1 = periph; - alias->si_iosize_max = softc->maxio; - alias->si_flags |= softc->si_flags; + softc->devs.er_dev = make_dev(&sa_cdevsw, SAMINOR(SA_NOT_CTLDEV, + SA_ATYPE_ER), UID_ROOT, GID_OPERATOR, + 0660, "e%s%d", periph->periph_name, periph->unit_number); + sasetupdev(softc, softc->devs.er_dev); - alias = make_dev_alias(softc->devs.mode_devs[i].er_dev, - "e%s%d", periph->periph_name, periph->unit_number); - alias->si_drv1 = periph; - alias->si_iosize_max = softc->maxio; - alias->si_flags |= softc->si_flags; - } - } cam_periph_lock(periph); + softc->density_type_bits[0] = 0; + softc->density_type_bits[1] = SRDS_MEDIA; + softc->density_type_bits[2] = SRDS_MEDIUM_TYPE; + softc->density_type_bits[3] = SRDS_MEDIUM_TYPE | SRDS_MEDIA; /* * Bump the peripheral refcount for the sysctl thread, in case we * get invalidated before the thread has a chance to run. @@ -1724,10 +2580,33 @@ again: done_bp = bp; if ((softc->flags & SA_FLAG_EOM_PENDING) != 0) { /* - * We now just clear errors in this case - * and let the residual be the notifier. + * We have two different behaviors for + * writes when we hit either Early Warning + * or the PEWZ (Programmable Early Warning + * Zone). The default behavior is that + * for all writes that are currently + * queued after the write where we saw the + * early warning, we will return the write + * with the residual equal to the count. + * i.e. tell the application that 0 bytes + * were written. + * + * The alternate behavior, which is enabled + * when eot_warn is set, is that in + * addition to setting the residual equal + * to the count, we will set the error + * to ENOSPC. + * + * In either case, once queued writes are + * cleared out, we clear the error flag + * (see below) and the application is free to + * attempt to write more. */ - bp->bio_error = 0; + if (softc->eot_warn != 0) { + bp->bio_flags |= BIO_ERROR; + bp->bio_error = ENOSPC; + } else + bp->bio_error = 0; } else if ((softc->flags & SA_FLAG_EOF_PENDING) != 0) { /* * This can only happen if we're reading @@ -1764,13 +2643,13 @@ again: bioq_remove(&softc->bio_queue, bp); softc->queue_count--; + length = bp->bio_bcount; + if ((softc->flags & SA_FLAG_FIXED) != 0) { if (softc->blk_shift != 0) { - length = - bp->bio_bcount >> softc->blk_shift; + length = length >> softc->blk_shift; } else if (softc->media_blksize != 0) { - length = bp->bio_bcount / - softc->media_blksize; + length = length / softc->media_blksize; } else { bp->bio_error = EIO; xpt_print(periph->path, "zero blocksize" @@ -1785,7 +2664,6 @@ again: "write")); #endif } else { - length = bp->bio_bcount; #if 0 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_INFO, ("issuing a %d variable byte %s\n", @@ -1813,6 +2691,19 @@ again: * would be to issue, e.g., 64KB reads and occasionally * have to do deal with 512 byte or 1KB intermediate * records. + * + * That said, though, we now support setting the + * SILI bit on reads, and we set the blocksize to 4 + * bytes when we do that. This gives us + * compatibility with software that wants this, + * although the only real difference between that + * and not setting the SILI bit on reads is that we + * won't get a check condition on reads where our + * request size is larger than the block on tape. + * That probably only makes a real difference in + * non-packetized SCSI, where you have to go back + * to the drive to request sense and thus incur + * more latency. */ softc->dsreg = (bp->bio_cmd == BIO_READ)? MTIO_DSREG_RD : MTIO_DSREG_WR; @@ -1820,7 +2711,7 @@ again: MSG_SIMPLE_Q_TAG, (bp->bio_cmd == BIO_READ ? SCSI_RW_READ : SCSI_RW_WRITE) | ((bp->bio_flags & BIO_UNMAPPED) != 0 ? - SCSI_RW_BIO : 0), FALSE, + SCSI_RW_BIO : 0), softc->sili, (softc->flags & SA_FLAG_FIXED) != 0, length, (bp->bio_flags & BIO_UNMAPPED) != 0 ? (void *)bp : bp->bio_data, bp->bio_bcount, SSD_FULL_SIZE, @@ -2006,8 +2897,7 @@ samount(struct cam_periph *periph, int oflags, struct cdev *dev) * Clear out old state. */ softc->flags &= ~(SA_FLAG_TAPE_WP|SA_FLAG_TAPE_WRITTEN| - SA_FLAG_ERR_PENDING|SA_FLAG_COMP_ENABLED| - SA_FLAG_COMP_SUPP|SA_FLAG_COMP_UNSUPP); + SA_FLAG_ERR_PENDING|SA_FLAG_COMPRESSION); softc->filemarks = 0; /* @@ -2116,7 +3006,7 @@ samount(struct cam_periph *periph, int oflags, struct cdev *dev) &softc->buffer_mode, &write_protect, &softc->speed, &comp_supported, &comp_enabled, &softc->comp_algorithm, - NULL); + NULL, NULL, 0, 0); if (error != 0) { /* @@ -2363,6 +3253,8 @@ exit: softc->dsreg = MTIO_DSREG_NIL; } else { softc->fileno = softc->blkno = 0; + softc->rep_fileno = softc->rep_blkno = -1; + softc->partition = 0; softc->dsreg = MTIO_DSREG_REST; } #ifdef SA_1FM_AT_EOD @@ -2422,7 +3314,7 @@ sacheckeod(struct cam_periph *periph) markswanted = samarkswanted(periph); if (markswanted > 0) { - error = sawritefilemarks(periph, markswanted, FALSE); + error = sawritefilemarks(periph, markswanted, FALSE, FALSE); } else { error = 0; } @@ -2642,7 +3534,8 @@ sagetparams(struct cam_periph *periph, sa_params params_to_get, u_int32_t *blocksize, u_int8_t *density, u_int32_t *numblocks, int *buff_mode, u_int8_t *write_protect, u_int8_t *speed, int *comp_supported, int *comp_enabled, u_int32_t *comp_algorithm, - sa_comp_t *tcs) + sa_comp_t *tcs, struct scsi_control_data_prot_subpage *prot_page, + int dp_size, int prot_changeable) { union ccb *ccb; void *mode_buffer; @@ -2800,6 +3693,151 @@ retry: bcopy(ntcs, tcs, sizeof (sa_comp_t)); } + if ((params_to_get & SA_PARAM_DENSITY_EXT) + && (softc->scsi_rev >= SCSI_REV_SPC)) { + int i; + + for (i = 0; i < SA_DENSITY_TYPES; i++) { + scsi_report_density_support(&ccb->csio, + /*retries*/ 1, + /*cbfcnp*/ sadone, + /*tag_action*/ MSG_SIMPLE_Q_TAG, + /*media*/ softc->density_type_bits[i] & SRDS_MEDIA, + /*medium_type*/ softc->density_type_bits[i] & + SRDS_MEDIUM_TYPE, + /*data_ptr*/ softc->density_info[i], + /*length*/ sizeof(softc->density_info[i]), + /*sense_len*/ SSD_FULL_SIZE, + /*timeout*/ REP_DENSITY_TIMEOUT); + error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT, + softc->device_stats); + status = ccb->ccb_h.status & CAM_STATUS_MASK; + + /* + * Some tape drives won't support this command at + * all, but hopefully we'll minimize that with the + * check for SPC or greater support above. If they + * don't support the default report (neither the + * MEDIA or MEDIUM_TYPE bits set), then there is + * really no point in continuing on to look for + * other reports. + */ + if ((error != 0) + || (status != CAM_REQ_CMP)) { + error = 0; + softc->density_info_valid[i] = 0; + if (softc->density_type_bits[i] == 0) + break; + else + continue; + } + softc->density_info_valid[i] = ccb->csio.dxfer_len - + ccb->csio.resid; + } + } + + /* + * Get logical block protection parameters if the drive supports it. + */ + if ((params_to_get & SA_PARAM_LBP) + && (softc->flags & SA_FLAG_PROTECT_SUPP)) { + struct scsi_mode_header_10 *mode10_hdr; + struct scsi_control_data_prot_subpage *dp_page; + struct scsi_mode_sense_10 *cdb; + struct sa_prot_state *prot; + int dp_len, returned_len; + + if (dp_size == 0) + dp_size = sizeof(*dp_page); + + dp_len = sizeof(*mode10_hdr) + dp_size; + mode10_hdr = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO); + if (mode10_hdr == NULL) { + error = ENOMEM; + goto sagetparamsexit; + } + + scsi_mode_sense_len(&ccb->csio, + /*retries*/ 5, + /*cbfcnp*/ sadone, + /*tag_action*/ MSG_SIMPLE_Q_TAG, + /*dbd*/ TRUE, + /*page_code*/ (prot_changeable == 0) ? + SMS_PAGE_CTRL_CURRENT : + SMS_PAGE_CTRL_CHANGEABLE, + /*page*/ SMS_CONTROL_MODE_PAGE, + /*param_buf*/ (uint8_t *)mode10_hdr, + /*param_len*/ dp_len, + /*minimum_cmd_size*/ 10, + /*sense_len*/ SSD_FULL_SIZE, + /*timeout*/ SCSIOP_TIMEOUT); + /* + * XXX KDM we need to be able to set the subpage in the + * fill function. + */ + cdb = (struct scsi_mode_sense_10 *)ccb->csio.cdb_io.cdb_bytes; + cdb->subpage = SA_CTRL_DP_SUBPAGE_CODE; + + error = cam_periph_runccb(ccb, saerror, 0, SF_NO_PRINT, + softc->device_stats); + if (error != 0) { + free(mode10_hdr, M_SCSISA); + goto sagetparamsexit; + } + + status = ccb->ccb_h.status & CAM_STATUS_MASK; + if (status != CAM_REQ_CMP) { + error = EINVAL; + free(mode10_hdr, M_SCSISA); + goto sagetparamsexit; + } + + /* + * The returned data length at least has to be long enough + * for us to look at length in the mode page header. + */ + returned_len = ccb->csio.dxfer_len - ccb->csio.resid; + if (returned_len < sizeof(mode10_hdr->data_length)) { + error = EINVAL; + free(mode10_hdr, M_SCSISA); + goto sagetparamsexit; + } + + returned_len = min(returned_len, + sizeof(mode10_hdr->data_length) + + scsi_2btoul(mode10_hdr->data_length)); + + dp_page = (struct scsi_control_data_prot_subpage *) + &mode10_hdr[1]; + + /* + * We also have to have enough data to include the prot_bits + * in the subpage. + */ + if (returned_len < (sizeof(*mode10_hdr) + + __offsetof(struct scsi_control_data_prot_subpage, prot_bits) + + sizeof(dp_page->prot_bits))) { + error = EINVAL; + free(mode10_hdr, M_SCSISA); + goto sagetparamsexit; + } + + prot = &softc->prot_info.cur_prot_state; + prot->prot_method = dp_page->prot_method; + prot->pi_length = dp_page->pi_length & + SA_CTRL_DP_PI_LENGTH_MASK; + prot->lbp_w = (dp_page->prot_bits & SA_CTRL_DP_LBP_W) ? 1 :0; + prot->lbp_r = (dp_page->prot_bits & SA_CTRL_DP_LBP_R) ? 1 :0; + prot->rbdp = (dp_page->prot_bits & SA_CTRL_DP_RBDP) ? 1 :0; + prot->initialized = 1; + + if (prot_page != NULL) + bcopy(dp_page, prot_page, min(sizeof(*prot_page), + sizeof(*dp_page))); + + free(mode10_hdr, M_SCSISA); + } + if (CAM_DEBUGGED(periph->path, CAM_DEBUG_INFO)) { int idx; char *xyz = mode_buffer; @@ -2817,6 +3855,177 @@ sagetparamsexit: return (error); } +/* + * Set protection information to the pending protection information stored + * in the softc. + */ +static int +sasetprot(struct cam_periph *periph, struct sa_prot_state *new_prot) +{ + struct sa_softc *softc; + struct scsi_control_data_prot_subpage *dp_page, *dp_changeable; + struct scsi_mode_header_10 *mode10_hdr, *mode10_changeable; + union ccb *ccb; + uint8_t current_speed; + size_t dp_size, dp_page_length; + int dp_len, buff_mode; + int error; + + softc = (struct sa_softc *)periph->softc; + mode10_hdr = NULL; + mode10_changeable = NULL; + ccb = NULL; + + /* + * Start off with the size set to the actual length of the page + * that we have defined. + */ + dp_size = sizeof(*dp_changeable); + dp_page_length = dp_size - + __offsetof(struct scsi_control_data_prot_subpage, prot_method); + +retry_length: + + dp_len = sizeof(*mode10_changeable) + dp_size; + mode10_changeable = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO); + if (mode10_changeable == NULL) { + error = ENOMEM; + goto bailout; + } + + dp_changeable = + (struct scsi_control_data_prot_subpage *)&mode10_changeable[1]; + + /* + * First get the data protection page changeable parameters mask. + * We need to know which parameters the drive supports changing. + * We also need to know what the drive claims that its page length + * is. The reason is that IBM drives in particular are very picky + * about the page length. They want it (the length set in the + * page structure itself) to be 28 bytes, and they want the + * parameter list length specified in the mode select header to be + * 40 bytes. So, to work with IBM drives as well as any other tape + * drive, find out what the drive claims the page length is, and + * make sure that we match that. + */ + error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP, + NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL, + NULL, NULL, dp_changeable, dp_size, /*prot_changeable*/ 1); + if (error != 0) + goto bailout; + + if (scsi_2btoul(dp_changeable->length) > dp_page_length) { + dp_page_length = scsi_2btoul(dp_changeable->length); + dp_size = dp_page_length + + __offsetof(struct scsi_control_data_prot_subpage, + prot_method); + free(mode10_changeable, M_SCSISA); + mode10_changeable = NULL; + goto retry_length; + } + + mode10_hdr = malloc(dp_len, M_SCSISA, M_NOWAIT | M_ZERO); + if (mode10_hdr == NULL) { + error = ENOMEM; + goto bailout; + } + + dp_page = (struct scsi_control_data_prot_subpage *)&mode10_hdr[1]; + + /* + * Now grab the actual current settings in the page. + */ + error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP, + NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL, + NULL, NULL, dp_page, dp_size, /*prot_changeable*/ 0); + if (error != 0) + goto bailout; + + /* These two fields need to be 0 for MODE SELECT */ + scsi_ulto2b(0, mode10_hdr->data_length); + mode10_hdr->medium_type = 0; + /* We are not including a block descriptor */ + scsi_ulto2b(0, mode10_hdr->blk_desc_len); + + mode10_hdr->dev_spec = current_speed; + /* if set, set single-initiator buffering mode */ + if (softc->buffer_mode == SMH_SA_BUF_MODE_SIBUF) { + mode10_hdr->dev_spec |= SMH_SA_BUF_MODE_SIBUF; + } + + /* + * For each field, make sure that the drive allows changing it + * before bringing in the user's setting. + */ + if (dp_changeable->prot_method != 0) + dp_page->prot_method = new_prot->prot_method; + + if (dp_changeable->pi_length & SA_CTRL_DP_PI_LENGTH_MASK) { + dp_page->pi_length &= ~SA_CTRL_DP_PI_LENGTH_MASK; + dp_page->pi_length |= (new_prot->pi_length & + SA_CTRL_DP_PI_LENGTH_MASK); + } + if (dp_changeable->prot_bits & SA_CTRL_DP_LBP_W) { + if (new_prot->lbp_w) + dp_page->prot_bits |= SA_CTRL_DP_LBP_W; + else + dp_page->prot_bits &= ~SA_CTRL_DP_LBP_W; + } + + if (dp_changeable->prot_bits & SA_CTRL_DP_LBP_R) { + if (new_prot->lbp_r) + dp_page->prot_bits |= SA_CTRL_DP_LBP_R; + else + dp_page->prot_bits &= ~SA_CTRL_DP_LBP_R; + } + + if (dp_changeable->prot_bits & SA_CTRL_DP_RBDP) { + if (new_prot->rbdp) + dp_page->prot_bits |= SA_CTRL_DP_RBDP; + else + dp_page->prot_bits &= ~SA_CTRL_DP_RBDP; + } + + ccb = cam_periph_getccb(periph, 1); + + scsi_mode_select_len(&ccb->csio, + /*retries*/ 5, + /*cbfcnp*/ sadone, + /*tag_action*/ MSG_SIMPLE_Q_TAG, + /*scsi_page_fmt*/ TRUE, + /*save_pages*/ FALSE, + /*param_buf*/ (uint8_t *)mode10_hdr, + /*param_len*/ dp_len, + /*minimum_cmd_size*/ 10, + /*sense_len*/ SSD_FULL_SIZE, + /*timeout*/ SCSIOP_TIMEOUT); + + error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats); + if (error != 0) + goto bailout; + + if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { + error = EINVAL; + goto bailout; + } + + /* + * The operation was successful. We could just copy the settings + * the user requested, but just in case the drive ignored some of + * our settings, let's ask for status again. + */ + error = sagetparams(periph, SA_PARAM_SPEED | SA_PARAM_LBP, + NULL, NULL, NULL, &buff_mode, NULL, ¤t_speed, NULL, NULL, + NULL, NULL, dp_page, dp_size, 0); + +bailout: + if (ccb != NULL) + xpt_release_ccb(ccb); + free(mode10_hdr, M_SCSISA); + free(mode10_changeable, M_SCSISA); + return (error); +} + /* * The purpose of this function is to set one of four different parameters * for a tape drive: @@ -2869,7 +4078,7 @@ sasetparams(struct cam_periph *periph, sa_params params_to_set, params_to_set | SA_PARAM_BLOCKSIZE | SA_PARAM_SPEED, ¤t_blocksize, ¤t_density, NULL, &buff_mode, NULL, ¤t_speed, &comp_supported, &comp_enabled, - ¤t_calg, ccomp); + ¤t_calg, ccomp, NULL, 0, 0); if (error != 0) { free(ccomp, M_SCSISA); @@ -3148,6 +4357,197 @@ retry: return (error); } +static int +saextget(struct cdev *dev, struct cam_periph *periph, struct sbuf *sb, + struct mtextget *g) +{ + int indent, error; + char tmpstr[80]; + struct sa_softc *softc; + int tmpint; + uint32_t maxio_tmp; + struct ccb_getdev cgd; + + softc = (struct sa_softc *)periph->softc; + + error = 0; + + error = sagetparams_common(dev, periph); + if (error) + goto extget_bailout; + if (!SA_IS_CTRL(dev) && !softc->open_pending_mount) + sagetpos(periph); + + indent = 0; + SASBADDNODE(sb, indent, mtextget); + /* + * Basic CAM peripheral information. + */ + SASBADDVARSTR(sb, indent, periph->periph_name, %s, periph_name, + strlen(periph->periph_name) + 1); + SASBADDUINT(sb, indent, periph->unit_number, %u, unit_number); + xpt_setup_ccb(&cgd.ccb_h, + periph->path, + CAM_PRIORITY_NORMAL); + cgd.ccb_h.func_code = XPT_GDEV_TYPE; + xpt_action((union ccb *)&cgd); + if ((cgd.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { + g->status = MT_EXT_GET_ERROR; + snprintf(g->error_str, sizeof(g->error_str), + "Error %#x returned for XPT_GDEV_TYPE CCB", + cgd.ccb_h.status); + goto extget_bailout; + } + + cam_strvis(tmpstr, cgd.inq_data.vendor, + sizeof(cgd.inq_data.vendor), sizeof(tmpstr)); + SASBADDVARSTRDESC(sb, indent, tmpstr, %s, vendor, + sizeof(cgd.inq_data.vendor) + 1, "SCSI Vendor ID"); + + cam_strvis(tmpstr, cgd.inq_data.product, + sizeof(cgd.inq_data.product), sizeof(tmpstr)); + SASBADDVARSTRDESC(sb, indent, tmpstr, %s, product, + sizeof(cgd.inq_data.product) + 1, "SCSI Product ID"); + + cam_strvis(tmpstr, cgd.inq_data.revision, + sizeof(cgd.inq_data.revision), sizeof(tmpstr)); + SASBADDVARSTRDESC(sb, indent, tmpstr, %s, revision, + sizeof(cgd.inq_data.revision) + 1, "SCSI Revision"); + + if (cgd.serial_num_len > 0) { + char *tmpstr2; + size_t ts2_len; + int ts2_malloc; + + ts2_len = 0; + + if (cgd.serial_num_len > sizeof(tmpstr)) { + ts2_len = cgd.serial_num_len + 1; + ts2_malloc = 1; + tmpstr2 = malloc(ts2_len, M_SCSISA, M_WAITOK | M_ZERO); + } else { + ts2_len = sizeof(tmpstr); + ts2_malloc = 0; + tmpstr2 = tmpstr; + } + + cam_strvis(tmpstr2, cgd.serial_num, cgd.serial_num_len, + ts2_len); + + SASBADDVARSTRDESC(sb, indent, tmpstr2, %s, serial_num, + (ssize_t)cgd.serial_num_len + 1, "Serial Number"); + if (ts2_malloc != 0) + free(tmpstr2, M_SCSISA); + } else { + /* + * We return a serial_num element in any case, but it will + * be empty if the device has no serial number. + */ + tmpstr[0] = '\0'; + SASBADDVARSTRDESC(sb, indent, tmpstr, %s, serial_num, + (ssize_t)0, "Serial Number"); + } + + SASBADDUINTDESC(sb, indent, softc->maxio, %u, maxio, + "Maximum I/O size allowed by driver and controller"); + + SASBADDUINTDESC(sb, indent, softc->cpi_maxio, %u, cpi_maxio, + "Maximum I/O size reported by controller"); + + SASBADDUINTDESC(sb, indent, softc->max_blk, %u, max_blk, + "Maximum block size supported by tape drive and media"); + + SASBADDUINTDESC(sb, indent, softc->min_blk, %u, min_blk, + "Minimum block size supported by tape drive and media"); + + SASBADDUINTDESC(sb, indent, softc->blk_gran, %u, blk_gran, + "Block granularity supported by tape drive and media"); + + maxio_tmp = min(softc->max_blk, softc->maxio); + + SASBADDUINTDESC(sb, indent, maxio_tmp, %u, max_effective_iosize, + "Maximum possible I/O size"); + + SASBADDINTDESC(sb, indent, softc->flags & SA_FLAG_FIXED ? 1 : 0, %d, + fixed_mode, "Set to 1 for fixed block mode, 0 for variable block"); + + /* + * XXX KDM include SIM, bus, target, LUN? + */ + if (softc->flags & SA_FLAG_COMP_UNSUPP) + tmpint = 0; + else + tmpint = 1; + SASBADDINTDESC(sb, indent, tmpint, %d, compression_supported, + "Set to 1 if compression is supported, 0 if not"); + if (softc->flags & SA_FLAG_COMP_ENABLED) + tmpint = 1; + else + tmpint = 0; + SASBADDINTDESC(sb, indent, tmpint, %d, compression_enabled, + "Set to 1 if compression is enabled, 0 if not"); + SASBADDUINTDESC(sb, indent, softc->comp_algorithm, %u, + compression_algorithm, "Numeric compression algorithm"); + + safillprot(softc, &indent, sb); + + SASBADDUINTDESC(sb, indent, softc->media_blksize, %u, + media_blocksize, "Block size reported by drive or set by user"); + SASBADDINTDESC(sb, indent, (intmax_t)softc->fileno, %jd, + calculated_fileno, "Calculated file number, -1 if unknown"); + SASBADDINTDESC(sb, indent, (intmax_t)softc->blkno, %jd, + calculated_rel_blkno, "Calculated block number relative to file, " + "set to -1 if unknown"); + SASBADDINTDESC(sb, indent, (intmax_t)softc->rep_fileno, %jd, + reported_fileno, "File number reported by drive, -1 if unknown"); + SASBADDINTDESC(sb, indent, (intmax_t)softc->rep_blkno, %jd, + reported_blkno, "Block number relative to BOP/BOT reported by " + "drive, -1 if unknown"); + SASBADDINTDESC(sb, indent, (intmax_t)softc->partition, %jd, + partition, "Current partition number, 0 is the default"); + SASBADDINTDESC(sb, indent, softc->bop, %d, bop, + "Set to 1 if drive is at the beginning of partition/tape, 0 if " + "not, -1 if unknown"); + SASBADDINTDESC(sb, indent, softc->eop, %d, eop, + "Set to 1 if drive is past early warning, 0 if not, -1 if unknown"); + SASBADDINTDESC(sb, indent, softc->bpew, %d, bpew, + "Set to 1 if drive is past programmable early warning, 0 if not, " + "-1 if unknown"); + SASBADDINTDESC(sb, indent, (intmax_t)softc->last_io_resid, %jd, + residual, "Residual for the last I/O"); + /* + * XXX KDM should we send a string with the current driver + * status already decoded instead of a numeric value? + */ + SASBADDINTDESC(sb, indent, softc->dsreg, %d, dsreg, + "Current state of the driver"); + + safilldensitysb(softc, &indent, sb); + + SASBENDNODE(sb, indent, mtextget); + +extget_bailout: + + return (error); +} + +static int +saparamget(struct sa_softc *softc, struct sbuf *sb) +{ + int indent; + + indent = 0; + SASBADDNODE(sb, indent, mtparamget); + SASBADDINTDESC(sb, indent, softc->sili, %d, sili, + "Suppress an error on underlength variable reads"); + SASBADDINTDESC(sb, indent, softc->eot_warn, %d, eot_warn, + "Return an error to warn that end of tape is approaching"); + safillprot(softc, &indent, sb); + SASBENDNODE(sb, indent, mtparamget); + + return (0); +} + static void saprevent(struct cam_periph *periph, int action) { @@ -3207,10 +4607,14 @@ sarewind(struct cam_periph *periph) softc->dsreg = MTIO_DSREG_REST; xpt_release_ccb(ccb); - if (error == 0) - softc->fileno = softc->blkno = (daddr_t) 0; - else + if (error == 0) { + softc->partition = softc->fileno = softc->blkno = (daddr_t) 0; + softc->rep_fileno = softc->rep_blkno = (daddr_t) 0; + } else { softc->fileno = softc->blkno = (daddr_t) -1; + softc->partition = (daddr_t) -1; + softc->rep_fileno = softc->rep_blkno = (daddr_t) -1; + } return (error); } @@ -3262,6 +4666,8 @@ saspace(struct cam_periph *periph, int count, scsi_space_code code) */ if (error) { softc->fileno = softc->blkno = (daddr_t) -1; + softc->rep_blkno = softc->partition = (daddr_t) -1; + softc->rep_fileno = (daddr_t) -1; } else if (code == SS_SETMARKS || code == SS_EOD) { softc->fileno = softc->blkno = (daddr_t) -1; } else if (code == SS_FILEMARKS && softc->fileno != (daddr_t) -1) { @@ -3281,11 +4687,14 @@ saspace(struct cam_periph *periph, int count, scsi_space_code code) } } } + if (error == 0) + sagetpos(periph); + return (error); } static int -sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks) +sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks, int immed) { union ccb *ccb; struct sa_softc *softc; @@ -3304,7 +4713,7 @@ sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks) softc->dsreg = MTIO_DSREG_FMK; /* this *must* not be retried */ scsi_write_filemarks(&ccb->csio, 0, sadone, MSG_SIMPLE_Q_TAG, - FALSE, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT); + immed, setmarks, nmarks, SSD_FULL_SIZE, IO_TIMEOUT); softc->dsreg = MTIO_DSREG_REST; @@ -3322,11 +4731,120 @@ sawritefilemarks(struct cam_periph *periph, int nmarks, int setmarks) * Update relative positions (if we're doing that). */ if (error) { - softc->fileno = softc->blkno = (daddr_t) -1; + softc->fileno = softc->blkno = softc->partition = (daddr_t) -1; } else if (softc->fileno != (daddr_t) -1) { softc->fileno += nwm; softc->blkno = 0; } + + /* + * Ask the tape drive for position information. + */ + sagetpos(periph); + + /* + * If we got valid position information, since we just wrote a file + * mark, we know we're at the file mark and block 0 after that + * filemark. + */ + if (softc->rep_fileno != (daddr_t) -1) { + softc->fileno = softc->rep_fileno; + softc->blkno = 0; + } + + return (error); +} + +static int +sagetpos(struct cam_periph *periph) +{ + union ccb *ccb; + struct scsi_tape_position_long_data long_pos; + struct sa_softc *softc = (struct sa_softc *)periph->softc; + int error; + + if (softc->quirks & SA_QUIRK_NO_LONG_POS) { + softc->rep_fileno = (daddr_t) -1; + softc->rep_blkno = (daddr_t) -1; + softc->bop = softc->eop = softc->bpew = -1; + return (EOPNOTSUPP); + } + + bzero(&long_pos, sizeof(long_pos)); + + ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); + scsi_read_position_10(&ccb->csio, + /*retries*/ 1, + /*cbfcnp*/ sadone, + /*tag_action*/ MSG_SIMPLE_Q_TAG, + /*service_action*/ SA_RPOS_LONG_FORM, + /*data_ptr*/ (uint8_t *)&long_pos, + /*length*/ sizeof(long_pos), + /*sense_len*/ SSD_FULL_SIZE, + /*timeout*/ SCSIOP_TIMEOUT); + + softc->dsreg = MTIO_DSREG_RBSY; + error = cam_periph_runccb(ccb, saerror, 0, SF_QUIET_IR, + softc->device_stats); + softc->dsreg = MTIO_DSREG_REST; + + if (error == 0) { + if (long_pos.flags & SA_RPOS_LONG_MPU) { + /* + * If the drive doesn't know what file mark it is + * on, our calculated filemark isn't going to be + * accurate either. + */ + softc->fileno = (daddr_t) -1; + softc->rep_fileno = (daddr_t) -1; + } else { + softc->fileno = softc->rep_fileno = + scsi_8btou64(long_pos.logical_file_num); + } + + if (long_pos.flags & SA_RPOS_LONG_LONU) { + softc->partition = (daddr_t) -1; + softc->rep_blkno = (daddr_t) -1; + /* + * If the tape drive doesn't know its block + * position, we can't claim to know it either. + */ + softc->blkno = (daddr_t) -1; + } else { + softc->partition = scsi_4btoul(long_pos.partition); + softc->rep_blkno = + scsi_8btou64(long_pos.logical_object_num); + } + if (long_pos.flags & SA_RPOS_LONG_BOP) + softc->bop = 1; + else + softc->bop = 0; + + if (long_pos.flags & SA_RPOS_LONG_EOP) + softc->eop = 1; + else + softc->eop = 0; + + if (long_pos.flags & SA_RPOS_LONG_BPEW) + softc->bpew = 1; + else + softc->bpew = 0; + } else if (error == EINVAL) { + /* + * If this drive returned an invalid-request type error, + * then it likely doesn't support the long form report. + */ + softc->quirks |= SA_QUIRK_NO_LONG_POS; + } + + if (error != 0) { + softc->rep_fileno = softc->rep_blkno = (daddr_t) -1; + softc->partition = (daddr_t) -1; + softc->bop = softc->eop = softc->bpew = -1; + } + + xpt_release_ccb(ccb); + return (error); } @@ -3349,7 +4867,7 @@ sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr) */ if (hard && (softc->flags & SA_FLAG_TAPE_WRITTEN)) { - error = sawritefilemarks(periph, 0, 0); + error = sawritefilemarks(periph, 0, 0, 0); if (error && error != EACCES) return (error); } @@ -3374,10 +4892,12 @@ sardpos(struct cam_periph *periph, int hard, u_int32_t *blkptr) } static int -sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr) +sasetpos(struct cam_periph *periph, int hard, struct mtlocate *locate_info) { union ccb *ccb; struct sa_softc *softc; + int locate16; + int immed, cp; int error; /* @@ -3391,19 +4911,100 @@ sasetpos(struct cam_periph *periph, int hard, u_int32_t *blkptr) softc = (struct sa_softc *)periph->softc; ccb = cam_periph_getccb(periph, 1); - - scsi_set_position(&ccb->csio, 1, sadone, MSG_SIMPLE_Q_TAG, - hard, *blkptr, SSD_FULL_SIZE, SPACE_TIMEOUT); + cp = locate_info->flags & MT_LOCATE_FLAG_CHANGE_PART ? 1 : 0; + immed = locate_info->flags & MT_LOCATE_FLAG_IMMED ? 1 : 0; + /* + * Determine whether we have to use LOCATE or LOCATE16. The hard + * bit is only possible with LOCATE, but the new ioctls do not + * allow setting that bit. So we can't get into the situation of + * having the hard bit set with a block address that is larger than + * 32-bits. + */ + if (hard != 0) + locate16 = 0; + else if ((locate_info->dest_type != MT_LOCATE_DEST_OBJECT) + || (locate_info->block_address_mode != MT_LOCATE_BAM_IMPLICIT) + || (locate_info->logical_id > SA_SPOS_MAX_BLK)) + locate16 = 1; + else + locate16 = 0; + + if (locate16 != 0) { + scsi_locate_16(&ccb->csio, + /*retries*/ 1, + /*cbfcnp*/ sadone, + /*tag_action*/ MSG_SIMPLE_Q_TAG, + /*immed*/ immed, + /*cp*/ cp, + /*dest_type*/ locate_info->dest_type, + /*bam*/ locate_info->block_address_mode, + /*partition*/ locate_info->partition, + /*logical_id*/ locate_info->logical_id, + /*sense_len*/ SSD_FULL_SIZE, + /*timeout*/ SPACE_TIMEOUT); + } else { + uint32_t blk_pointer; + + blk_pointer = locate_info->logical_id; + + scsi_locate_10(&ccb->csio, + /*retries*/ 1, + /*cbfcnp*/ sadone, + /*tag_action*/ MSG_SIMPLE_Q_TAG, + /*immed*/ immed, + /*cp*/ cp, + /*hard*/ hard, + /*partition*/ locate_info->partition, + /*block_address*/ locate_info->logical_id, + /*sense_len*/ SSD_FULL_SIZE, + /*timeout*/ SPACE_TIMEOUT); + } softc->dsreg = MTIO_DSREG_POS; error = cam_periph_runccb(ccb, saerror, 0, 0, softc->device_stats); softc->dsreg = MTIO_DSREG_REST; xpt_release_ccb(ccb); + /* - * Note relative file && block number position as now unknown. + * We assume the calculated file and block numbers are unknown + * unless we have enough information to populate them. */ softc->fileno = softc->blkno = (daddr_t) -1; + + /* + * If the user requested changing the partition and the request + * succeeded, note the partition. + */ + if ((error == 0) + && (cp != 0)) + softc->partition = locate_info->partition; + else + softc->partition = (daddr_t) -1; + + if (error == 0) { + switch (locate_info->dest_type) { + case MT_LOCATE_DEST_FILE: + /* + * This is the only case where we can reliably + * calculate the file and block numbers. + */ + softc->fileno = locate_info->logical_id; + softc->blkno = 0; + break; + case MT_LOCATE_DEST_OBJECT: + case MT_LOCATE_DEST_SET: + case MT_LOCATE_DEST_EOD: + default: + break; + } + } + + /* + * Ask the drive for current position information. + */ + sagetpos(periph); + return (error); } @@ -3427,10 +5028,11 @@ saretension(struct cam_periph *periph) softc->dsreg = MTIO_DSREG_REST; xpt_release_ccb(ccb); - if (error == 0) - softc->fileno = softc->blkno = (daddr_t) 0; - else - softc->fileno = softc->blkno = (daddr_t) -1; + if (error == 0) { + softc->partition = softc->fileno = softc->blkno = (daddr_t) 0; + sagetpos(periph); + } else + softc->partition = softc->fileno = softc->blkno = (daddr_t) -1; return (error); } @@ -3484,10 +5086,13 @@ saloadunload(struct cam_periph *periph, int load) softc->dsreg = MTIO_DSREG_REST; xpt_release_ccb(ccb); - if (error || load == 0) - softc->fileno = softc->blkno = (daddr_t) -1; - else if (error == 0) - softc->fileno = softc->blkno = (daddr_t) 0; + if (error || load == 0) { + softc->partition = softc->fileno = softc->blkno = (daddr_t) -1; + softc->rep_fileno = softc->rep_blkno = (daddr_t) -1; + } else if (error == 0) { + softc->partition = softc->fileno = softc->blkno = (daddr_t) 0; + sagetpos(periph); + } return (error); } @@ -3516,6 +5121,297 @@ saerase(struct cam_periph *periph, int longerase) return (error); } +/* + * Fill an sbuf with density data in XML format. This particular macro + * works for multi-byte integer fields. + * + * Note that 1 byte fields aren't supported here. The reason is that the + * compiler does not evaluate the sizeof(), and assumes that any of the + * sizes are possible for a given field. So passing in a multi-byte + * field will result in a warning that the assignment makes an integer + * from a pointer without a cast, if there is an assignment in the 1 byte + * case. + */ +#define SAFILLDENSSB(dens_data, sb, indent, field, desc_remain, \ + len_to_go, cur_offset, desc){ \ + size_t cur_field_len; \ + \ + cur_field_len = sizeof(dens_data->field); \ + if (desc_remain < cur_field_len) { \ + len_to_go -= desc_remain; \ + cur_offset += desc_remain; \ + continue; \ + } \ + len_to_go -= cur_field_len; \ + cur_offset += cur_field_len; \ + desc_remain -= cur_field_len; \ + \ + switch (sizeof(dens_data->field)) { \ + case 1: \ + KASSERT(1 == 0, ("Programmer error, invalid 1 byte " \ + "field width for SAFILLDENSFIELD")); \ + break; \ + case 2: \ + SASBADDUINTDESC(sb, indent, \ + scsi_2btoul(dens_data->field), %u, field, desc); \ + break; \ + case 3: \ + SASBADDUINTDESC(sb, indent, \ + scsi_3btoul(dens_data->field), %u, field, desc); \ + break; \ + case 4: \ + SASBADDUINTDESC(sb, indent, \ + scsi_4btoul(dens_data->field), %u, field, desc); \ + break; \ + case 8: \ + SASBADDUINTDESC(sb, indent, \ + (uintmax_t)scsi_8btou64(dens_data->field), %ju, \ + field, desc); \ + break; \ + default: \ + break; \ + } \ +}; +/* + * Fill an sbuf with density data in XML format. This particular macro + * works for strings. + */ +#define SAFILLDENSSBSTR(dens_data, sb, indent, field, desc_remain, \ + len_to_go, cur_offset, desc){ \ + size_t cur_field_len; \ + char tmpstr[32]; \ + \ + cur_field_len = sizeof(dens_data->field); \ + if (desc_remain < cur_field_len) { \ + len_to_go -= desc_remain; \ + cur_offset += desc_remain; \ + continue; \ + } \ + len_to_go -= cur_field_len; \ + cur_offset += cur_field_len; \ + desc_remain -= cur_field_len; \ + \ + cam_strvis(tmpstr, dens_data->field, \ + sizeof(dens_data->field), sizeof(tmpstr)); \ + SASBADDVARSTRDESC(sb, indent, tmpstr, %s, field, \ + strlen(tmpstr) + 1, desc); \ +}; + +/* + * Fill an sbuf with density data descriptors. + */ +static void +safilldenstypesb(struct sbuf *sb, int *indent, uint8_t *buf, int buf_len, + int is_density) +{ + struct scsi_density_hdr *hdr; + uint32_t hdr_len; + int len_to_go, cur_offset; + int length_offset; + int num_reports, need_close; + + /* + * We need at least the header length. Note that this isn't an + * error, not all tape drives will have every data type. + */ + if (buf_len < sizeof(*hdr)) + goto bailout; + + + hdr = (struct scsi_density_hdr *)buf; + hdr_len = scsi_2btoul(hdr->length); + len_to_go = min(buf_len - sizeof(*hdr), hdr_len); + if (is_density) { + length_offset = __offsetof(struct scsi_density_data, + bits_per_mm); + } else { + length_offset = __offsetof(struct scsi_medium_type_data, + num_density_codes); + } + cur_offset = sizeof(*hdr); + + num_reports = 0; + need_close = 0; + + while (len_to_go > length_offset) { + struct scsi_density_data *dens_data; + struct scsi_medium_type_data *type_data; + int desc_remain; + size_t cur_field_len; + + dens_data = NULL; + type_data = NULL; + + if (is_density) { + dens_data =(struct scsi_density_data *)&buf[cur_offset]; + if (dens_data->byte2 & SDD_DLV) + desc_remain = scsi_2btoul(dens_data->length); + else + desc_remain = SDD_DEFAULT_LENGTH - + length_offset; + } else { + type_data = (struct scsi_medium_type_data *) + &buf[cur_offset]; + desc_remain = scsi_2btoul(type_data->length); + } + + len_to_go -= length_offset; + desc_remain = min(desc_remain, len_to_go); + cur_offset += length_offset; + + if (need_close != 0) { + SASBENDNODE(sb, *indent, density_entry); + } + + SASBADDNODENUM(sb, *indent, density_entry, num_reports); + num_reports++; + need_close = 1; + + if (is_density) { + SASBADDUINTDESC(sb, *indent, + dens_data->primary_density_code, %u, + primary_density_code, "Primary Density Code"); + SASBADDUINTDESC(sb, *indent, + dens_data->secondary_density_code, %u, + secondary_density_code, "Secondary Density Code"); + SASBADDUINTDESC(sb, *indent, + dens_data->byte2 & ~SDD_DLV, %#x, density_flags, + "Density Flags"); + + SAFILLDENSSB(dens_data, sb, *indent, bits_per_mm, + desc_remain, len_to_go, cur_offset, "Bits per mm"); + SAFILLDENSSB(dens_data, sb, *indent, media_width, + desc_remain, len_to_go, cur_offset, "Media width"); + SAFILLDENSSB(dens_data, sb, *indent, tracks, + desc_remain, len_to_go, cur_offset, + "Number of Tracks"); + SAFILLDENSSB(dens_data, sb, *indent, capacity, + desc_remain, len_to_go, cur_offset, "Capacity"); + + SAFILLDENSSBSTR(dens_data, sb, *indent, assigning_org, + desc_remain, len_to_go, cur_offset, + "Assigning Organization"); + + SAFILLDENSSBSTR(dens_data, sb, *indent, density_name, + desc_remain, len_to_go, cur_offset, "Density Name"); + + SAFILLDENSSBSTR(dens_data, sb, *indent, description, + desc_remain, len_to_go, cur_offset, "Description"); + } else { + int i; + + SASBADDUINTDESC(sb, *indent, type_data->medium_type, + %u, medium_type, "Medium Type"); + + cur_field_len = + __offsetof(struct scsi_medium_type_data, + media_width) - + __offsetof(struct scsi_medium_type_data, + num_density_codes); + + if (desc_remain < cur_field_len) { + len_to_go -= desc_remain; + cur_offset += desc_remain; + continue; + } + len_to_go -= cur_field_len; + cur_offset += cur_field_len; + desc_remain -= cur_field_len; + + SASBADDINTDESC(sb, *indent, + type_data->num_density_codes, %d, + num_density_codes, "Number of Density Codes"); + SASBADDNODE(sb, *indent, density_code_list); + for (i = 0; i < type_data->num_density_codes; + i++) { + SASBADDUINTDESC(sb, *indent, + type_data->primary_density_codes[i], %u, + density_code, "Density Code"); + } + SASBENDNODE(sb, *indent, density_code_list); + + SAFILLDENSSB(type_data, sb, *indent, media_width, + desc_remain, len_to_go, cur_offset, + "Media width"); + SAFILLDENSSB(type_data, sb, *indent, medium_length, + desc_remain, len_to_go, cur_offset, + "Medium length"); + + /* + * Account for the two reserved bytes. + */ + cur_field_len = sizeof(type_data->reserved2); + if (desc_remain < cur_field_len) { + len_to_go -= desc_remain; + cur_offset += desc_remain; + continue; + } + len_to_go -= cur_field_len; + cur_offset += cur_field_len; + desc_remain -= cur_field_len; + + SAFILLDENSSBSTR(type_data, sb, *indent, assigning_org, + desc_remain, len_to_go, cur_offset, + "Assigning Organization"); + SAFILLDENSSBSTR(type_data, sb, *indent, + medium_type_name, desc_remain, len_to_go, + cur_offset, "Medium type name"); + SAFILLDENSSBSTR(type_data, sb, *indent, description, + desc_remain, len_to_go, cur_offset, "Description"); + + } + } + if (need_close != 0) { + SASBENDNODE(sb, *indent, density_entry); + } + +bailout: + return; +} + +/* + * Fill an sbuf with density data information + */ +static void +safilldensitysb(struct sa_softc *softc, int *indent, struct sbuf *sb) +{ + int i, is_density; + + SASBADDNODE(sb, *indent, mtdensity); + SASBADDUINTDESC(sb, *indent, softc->media_density, %u, media_density, + "Current Medium Density"); + is_density = 0; + for (i = 0; i < SA_DENSITY_TYPES; i++) { + int tmpint; + + if (softc->density_info_valid[i] == 0) + continue; + + SASBADDNODE(sb, *indent, density_report); + if (softc->density_type_bits[i] & SRDS_MEDIUM_TYPE) { + tmpint = 1; + is_density = 0; + } else { + tmpint = 0; + is_density = 1; + } + SASBADDINTDESC(sb, *indent, tmpint, %d, medium_type_report, + "Medium type report"); + + if (softc->density_type_bits[i] & SRDS_MEDIA) + tmpint = 1; + else + tmpint = 0; + SASBADDINTDESC(sb, *indent, tmpint, %d, media_report, + "Media report"); + + safilldenstypesb(sb, indent, softc->density_info[i], + softc->density_info_valid[i], is_density); + SASBENDNODE(sb, *indent, density_report); + } + SASBENDNODE(sb, *indent, mtdensity); +} + #endif /* _KERNEL */ /* @@ -3724,6 +5620,42 @@ scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries, scmd->byte1 = hardsoft; } +/* + * Read Tape Position command. + */ +void +scsi_read_position_10(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int service_action, + u_int8_t *data_ptr, u_int32_t length, + u_int32_t sense_len, u_int32_t timeout) +{ + struct scsi_tape_read_position *scmd; + + cam_fill_csio(csio, + retries, + cbfcnp, + /*flags*/CAM_DIR_IN, + tag_action, + /*data_ptr*/data_ptr, + /*dxfer_len*/length, + sense_len, + sizeof(*scmd), + timeout); + + + scmd = (struct scsi_tape_read_position *)&csio->cdb_io.cdb_bytes; + bzero(scmd, sizeof(*scmd)); + scmd->opcode = READ_POSITION; + scmd->byte1 = service_action; + /* + * The length is only currently set (as of SSC4r03) if the extended + * form is specified. The other forms have fixed lengths. + */ + if (service_action == SA_RPOS_EXTENDED_FORM) + scsi_ulto2b(length, scmd->length); +} + /* * Set Tape Position command. */ @@ -3744,3 +5676,193 @@ scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries, scmd->byte1 |= SA_SPOS_BT; scsi_ulto4b(blkno, scmd->blkaddr); } + +/* + * XXX KDM figure out how to make a compatibility function. + */ +void +scsi_locate_10(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int immed, int cp, int hard, + int64_t partition, u_int32_t block_address, + int sense_len, u_int32_t timeout) +{ + struct scsi_tape_locate *scmd; + + cam_fill_csio(csio, + retries, + cbfcnp, + CAM_DIR_NONE, + tag_action, + /*data_ptr*/ NULL, + /*dxfer_len*/ 0, + sense_len, + sizeof(*scmd), + timeout); + scmd = (struct scsi_tape_locate *)&csio->cdb_io.cdb_bytes; + bzero(scmd, sizeof(*scmd)); + scmd->opcode = LOCATE; + if (immed) + scmd->byte1 |= SA_SPOS_IMMED; + if (cp) + scmd->byte1 |= SA_SPOS_CP; + if (hard) + scmd->byte1 |= SA_SPOS_BT; + scsi_ulto4b(block_address, scmd->blkaddr); + scmd->partition = partition; +} + +void +scsi_locate_16(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int immed, int cp, u_int8_t dest_type, + int bam, int64_t partition, u_int64_t logical_id, + int sense_len, u_int32_t timeout) +{ + + struct scsi_locate_16 *scsi_cmd; + + cam_fill_csio(csio, + retries, + cbfcnp, + /*flags*/CAM_DIR_NONE, + tag_action, + /*data_ptr*/NULL, + /*dxfer_len*/0, + sense_len, + sizeof(*scsi_cmd), + timeout); + + scsi_cmd = (struct scsi_locate_16 *)&csio->cdb_io.cdb_bytes; + bzero(scsi_cmd, sizeof(*scsi_cmd)); + scsi_cmd->opcode = LOCATE_16; + if (immed) + scsi_cmd->byte1 |= SA_LC_IMMEDIATE; + if (cp) + scsi_cmd->byte1 |= SA_LC_CP; + scsi_cmd->byte1 |= (dest_type << SA_LC_DEST_TYPE_SHIFT); + + scsi_cmd->byte2 |= bam; + scsi_cmd->partition = partition; + scsi_u64to8b(logical_id, scsi_cmd->logical_id); +} + +void +scsi_report_density_support(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int media, int medium_type, + u_int8_t *data_ptr, u_int32_t length, + u_int32_t sense_len, u_int32_t timeout) +{ + struct scsi_report_density_support *scsi_cmd; + + scsi_cmd =(struct scsi_report_density_support *)&csio->cdb_io.cdb_bytes; + bzero(scsi_cmd, sizeof(*scsi_cmd)); + + scsi_cmd->opcode = REPORT_DENSITY_SUPPORT; + if (media != 0) + scsi_cmd->byte1 |= SRDS_MEDIA; + if (medium_type != 0) + scsi_cmd->byte1 |= SRDS_MEDIUM_TYPE; + + scsi_ulto2b(length, scsi_cmd->length); + + cam_fill_csio(csio, + retries, + cbfcnp, + /*flags*/CAM_DIR_IN, + tag_action, + /*data_ptr*/data_ptr, + /*dxfer_len*/length, + sense_len, + sizeof(*scsi_cmd), + timeout); +} + +void +scsi_set_capacity(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int byte1, u_int32_t proportion, + u_int32_t sense_len, u_int32_t timeout) +{ + struct scsi_set_capacity *scsi_cmd; + + scsi_cmd = (struct scsi_set_capacity *)&csio->cdb_io.cdb_bytes; + bzero(scsi_cmd, sizeof(*scsi_cmd)); + + scsi_cmd->opcode = SET_CAPACITY; + + scsi_cmd->byte1 = byte1; + scsi_ulto2b(proportion, scsi_cmd->cap_proportion); + + cam_fill_csio(csio, + retries, + cbfcnp, + /*flags*/CAM_DIR_NONE, + tag_action, + /*data_ptr*/NULL, + /*dxfer_len*/0, + sense_len, + sizeof(*scsi_cmd), + timeout); +} + +void +scsi_format_medium(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int byte1, int byte2, + u_int8_t *data_ptr, u_int32_t dxfer_len, + u_int32_t sense_len, u_int32_t timeout) +{ + struct scsi_format_medium *scsi_cmd; + + scsi_cmd = (struct scsi_format_medium*)&csio->cdb_io.cdb_bytes; + bzero(scsi_cmd, sizeof(*scsi_cmd)); + + scsi_cmd->opcode = FORMAT_MEDIUM; + + scsi_cmd->byte1 = byte1; + scsi_cmd->byte2 = byte2; + + scsi_ulto2b(dxfer_len, scsi_cmd->length); + + cam_fill_csio(csio, + retries, + cbfcnp, + /*flags*/(dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE, + tag_action, + /*data_ptr*/ data_ptr, + /*dxfer_len*/ dxfer_len, + sense_len, + sizeof(*scsi_cmd), + timeout); +} + +void +scsi_allow_overwrite(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int allow_overwrite, int partition, + u_int64_t logical_id, u_int32_t sense_len, u_int32_t timeout) +{ + struct scsi_allow_overwrite *scsi_cmd; + + scsi_cmd = (struct scsi_allow_overwrite *)&csio->cdb_io.cdb_bytes; + bzero(scsi_cmd, sizeof(*scsi_cmd)); + + scsi_cmd->opcode = ALLOW_OVERWRITE; + + scsi_cmd->allow_overwrite = allow_overwrite; + scsi_cmd->partition = partition; + scsi_u64to8b(logical_id, scsi_cmd->logical_id); + + cam_fill_csio(csio, + retries, + cbfcnp, + CAM_DIR_NONE, + tag_action, + /*data_ptr*/ NULL, + /*dxfer_len*/ 0, + sense_len, + sizeof(*scsi_cmd), + timeout); +} diff --git a/sys/cam/scsi/scsi_sa.h b/sys/cam/scsi/scsi_sa.h index b1cee795a..6ecef6598 100644 --- a/sys/cam/scsi/scsi_sa.h +++ b/sys/cam/scsi/scsi_sa.h @@ -3,6 +3,7 @@ * SCSI Sequential Access Peripheral driver for CAM. * * Copyright (c) 1999, 2000 Matthew Jacob + * Copyright (c) 2013, 2014, 2015 Spectra Logic Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -141,6 +142,53 @@ struct scsi_erase u_int8_t control; }; +/* + * Set tape capacity. + */ +struct scsi_set_capacity +{ + u_int8_t opcode; + u_int8_t byte1; +#define SA_SSC_IMMED 0x01 + u_int8_t reserved; + u_int8_t cap_proportion[2]; + u_int8_t control; +}; + +/* + * Format tape media. The CDB opcode is the same as the disk-specific + * FORMAT UNIT command, but the fields are different inside the CDB. Thus + * the reason for a separate definition here. + */ +struct scsi_format_medium +{ + u_int8_t opcode; + u_int8_t byte1; +#define SFM_IMMED 0x01 +#define SFM_VERIFY 0x02 + u_int8_t byte2; +#define SFM_FORMAT_DEFAULT 0x00 +#define SFM_FORMAT_PARTITION 0x01 +#define SFM_FORMAT_DEF_PART 0x02 +#define SFM_FORMAT_MASK 0x0f + u_int8_t length[2]; + u_int8_t control; +}; + +struct scsi_allow_overwrite +{ + u_int8_t opcode; + u_int8_t reserved1; + u_int8_t allow_overwrite; +#define SAO_ALLOW_OVERWRITE_DISABLED 0x00 +#define SAO_ALLOW_OVERWRITE_CUR_POS 0x01 +#define SAO_ALLOW_OVERWRITE_FORMAT 0x02 + u_int8_t partition; + u_int8_t logical_id[8]; + u_int8_t reserved2[3]; + u_int8_t control; +}; + /* * Dev specific mode page masks. */ @@ -180,11 +228,18 @@ struct scsi_dev_conf_page { #define SA_BIS 0x40 /* block identifiers supported */ #define SA_RSMK 0x20 /* report setmarks */ #define SA_AVC 0x10 /* automatic velocity control */ -#define SA_SOCF_MASK 0xc0 /* stop on consecutive formats */ -#define SA_RBO 0x20 /* recover buffer order */ -#define SA_REW 0x10 /* report early warning */ +#define SA_SOCF_MASK 0x0c /* stop on consecutive formats */ +#define SA_RBO 0x02 /* recover buffer order */ +#define SA_REW 0x01 /* report early warning */ u_int8_t gap_size; u_int8_t byte10; +/* from SCSI-3: SSC-4 Working draft (2/14) 8.3.3 */ +#define SA_EOD_DEF_MASK 0xe0 /* EOD defined */ +#define SA_EEG 0x10 /* Enable EOD Generation */ +#define SA_SEW 0x08 /* Synchronize at Early Warning */ +#define SA_SOFT_WP 0x04 /* Software Write Protect */ +#define SA_BAML 0x02 /* Block Address Mode Lock */ +#define SA_BAM 0x01 /* Block Address Mode */ u_int8_t ew_bufsize[3]; u_int8_t sel_comp_alg; #define SA_COMP_NONE 0x00 @@ -221,10 +276,78 @@ typedef union { struct scsi_data_compression_page dcomp; } sa_comp_t; +/* + * Control Data Protection subpage. This is as defined in SSC3r03. + */ +struct scsi_control_data_prot_subpage { + uint8_t page_code; +#define SA_CTRL_DP_PAGE_CODE 0x0a + uint8_t subpage_code; +#define SA_CTRL_DP_SUBPAGE_CODE 0xf0 + uint8_t length[2]; + uint8_t prot_method; +#define SA_CTRL_DP_NO_LBP 0x00 +#define SA_CTRL_DP_REED_SOLOMON 0x01 +#define SA_CTRL_DP_METHOD_MAX 0xff + uint8_t pi_length; +#define SA_CTRL_DP_PI_LENGTH_MASK 0x3f +#define SA_CTRL_DP_RS_LENGTH 4 + uint8_t prot_bits; +#define SA_CTRL_DP_LBP_W 0x80 +#define SA_CTRL_DP_LBP_R 0x40 +#define SA_CTRL_DP_RBDP 0x20 + uint8_t reserved[]; +}; + +/* + * This is the Read/Write Control mode page used on IBM Enterprise Tape + * Drives. They are known as 3592, TS, or Jaguar drives. The SCSI inquiry + * data will show a Product ID "03592XXX", where XXX is 'J1A', 'E05' (TS1120), + * 'E06' (TS1130), 'E07' (TS1140) or 'E08' (TS1150). + * + * This page definition is current as of the 3592 SCSI Reference v6, + * released on December 16th, 2014. + */ +struct scsi_tape_ibm_rw_control { + uint8_t page_code; +#define SA_IBM_RW_CTRL_PAGE_CODE 0x25 + uint8_t page_length; + uint8_t ignore_seq_checks; +#define SA_IBM_RW_CTRL_LOC_IGNORE_SEQ 0x04 +#define SA_IBM_RW_CTRL_SPC_BLK_IGNORE_SEQ 0x02 +#define SA_IBM_RW_CTRL_SPC_FM_IGNORE_SEQ 0x01 + uint8_t ignore_data_checks; +#define SA_IBM_RW_CTRL_LOC_IGNORE_DATA 0x04 +#define SA_IBM_RW_CTRL_SPC_BLK_IGNORE_DATA 0x02 +#define SA_IBM_RW_CTRL_SPC_FM_IGNORE_DATA 0x01 + uint8_t reserved1; + uint8_t leop_method; +#define SA_IBM_RW_CTRL_LEOP_DEFAULT 0x00 +#define SA_IBM_RW_CTRL_LEOP_MAX_CAP 0x01 +#define SA_IBM_RW_CTRL_LEOP_CONST_CAP 0x02 + uint8_t leop_ew[2]; + uint8_t byte8; +#define SA_IBM_RW_CTRL_DISABLE_FASTSYNC 0x80 +#define SA_IBM_RW_CTRL_DISABLE_SKIPSYNC 0x40 +#define SA_IBM_RW_CTRL_DISABLE_CROSS_EOD 0x08 +#define SA_IBM_RW_CTRL_DISABLE_CROSS_PERM_ERR 0x04 +#define SA_IBM_RW_CTRL_REPORT_SEG_EW 0x02 +#define SA_IBM_RW_CTRL_REPORT_HOUSEKEEPING_ERR 0x01 + uint8_t default_write_dens_bop_0; + uint8_t pending_write_dens_bop_0; + uint8_t reserved2[21]; +}; + struct scsi_tape_read_position { u_int8_t opcode; /* READ_POSITION */ u_int8_t byte1; /* set LSB to read hardware block pos */ - u_int8_t reserved[8]; +#define SA_RPOS_SHORT_FORM 0x00 +#define SA_RPOS_SHORT_VENDOR 0x01 +#define SA_RPOS_LONG_FORM 0x06 +#define SA_RPOS_EXTENDED_FORM 0x08 + u_int8_t reserved[5]; + u_int8_t length[2]; + u_int8_t control; }; struct scsi_tape_position_data { /* Short Form */ @@ -235,6 +358,7 @@ struct scsi_tape_position_data { /* Short Form */ #define SA_RPOS_BYCU 0x10 /* Byte Count Unknown (SCSI3) */ #define SA_RPOS_BPU 0x04 /* Block Position Unknown */ #define SA_RPOS_PERR 0x02 /* Position Error (SCSI3) */ +#define SA_RPOS_BPEW 0x01 /* Beyond Programmable Early Warning */ #define SA_RPOS_UNCERTAIN SA_RPOS_BPU u_int8_t partition; u_int8_t reserved[2]; @@ -245,6 +369,38 @@ struct scsi_tape_position_data { /* Short Form */ u_int8_t nbufbyte[4]; }; +struct scsi_tape_position_long_data { + u_int8_t flags; +#define SA_RPOS_LONG_BOP 0x80 /* Beginning of Partition */ +#define SA_RPOS_LONG_EOP 0x40 /* End of Partition */ +#define SA_RPOS_LONG_MPU 0x08 /* Mark Position Unknown */ +#define SA_RPOS_LONG_LONU 0x04 /* Logical Object Number Unknown */ +#define SA_RPOS_LONG_BPEW 0x01 /* Beyond Programmable Early Warning */ + u_int8_t reserved[3]; + u_int8_t partition[4]; + u_int8_t logical_object_num[8]; + u_int8_t logical_file_num[8]; + u_int8_t set_id[8]; +}; + +struct scsi_tape_position_ext_data { + u_int8_t flags; +#define SA_RPOS_EXT_BOP 0x80 /* Beginning of Partition */ +#define SA_RPOS_EXT_EOP 0x40 /* End of Partition */ +#define SA_RPOS_EXT_LOCU 0x20 /* Logical Object Count Unknown */ +#define SA_RPOS_EXT_BYCU 0x10 /* Byte Count Unknown */ +#define SA_RPOS_EXT_LOLU 0x04 /* Logical Object Location Unknown */ +#define SA_RPOS_EXT_PERR 0x02 /* Position Error */ +#define SA_RPOS_EXT_BPEW 0x01 /* Beyond Programmable Early Warning */ + u_int8_t partition; + u_int8_t length[2]; + u_int8_t reserved; + u_int8_t num_objects[3]; + u_int8_t first_object[8]; + u_int8_t last_object[8]; + u_int8_t bytes_in_buffer[8]; +}; + struct scsi_tape_locate { u_int8_t opcode; u_int8_t byte1; @@ -253,18 +409,521 @@ struct scsi_tape_locate { #define SA_SPOS_BT 0x04 u_int8_t reserved1; u_int8_t blkaddr[4]; +#define SA_SPOS_MAX_BLK 0xffffffff u_int8_t reserved2; u_int8_t partition; u_int8_t control; }; +struct scsi_locate_16 { + u_int8_t opcode; + u_int8_t byte1; +#define SA_LC_IMMEDIATE 0x01 +#define SA_LC_CP 0x02 +#define SA_LC_DEST_TYPE_MASK 0x38 +#define SA_LC_DEST_TYPE_SHIFT 3 +#define SA_LC_DEST_OBJECT 0x00 +#define SA_LC_DEST_FILE 0x01 +#define SA_LC_DEST_SET 0x02 +#define SA_LC_DEST_EOD 0x03 + u_int8_t byte2; +#define SA_LC_BAM_IMPLICIT 0x00 +#define SA_LC_BAM_EXPLICIT 0x01 + u_int8_t partition; + u_int8_t logical_id[8]; + u_int8_t reserved[3]; + u_int8_t control; +}; + +struct scsi_report_density_support { + u_int8_t opcode; + u_int8_t byte1; +#define SRDS_MEDIA 0x01 +#define SRDS_MEDIUM_TYPE 0x02 + u_int8_t reserved[5]; + u_int8_t length[2]; +#define SRDS_MAX_LENGTH 0xffff + u_int8_t control; +}; + +struct scsi_density_hdr { + u_int8_t length[2]; + u_int8_t reserved[2]; + u_int8_t descriptor[]; +}; + +struct scsi_density_data { + u_int8_t primary_density_code; + u_int8_t secondary_density_code; + u_int8_t byte2; +#define SDD_DLV 0x01 +#define SDD_DEFLT 0x20 +#define SDD_DUP 0x40 +#define SDD_WRTOK 0x80 + u_int8_t length[2]; +#define SDD_DEFAULT_LENGTH 52 + u_int8_t bits_per_mm[3]; + u_int8_t media_width[2]; + u_int8_t tracks[2]; + u_int8_t capacity[4]; + u_int8_t assigning_org[8]; + u_int8_t density_name[8]; + u_int8_t description[20]; +}; + +struct scsi_medium_type_data { + u_int8_t medium_type; + u_int8_t reserved1; + u_int8_t length[2]; +#define SMTD_DEFAULT_LENGTH 52 + u_int8_t num_density_codes; + u_int8_t primary_density_codes[9]; + u_int8_t media_width[2]; + u_int8_t medium_length[2]; + u_int8_t reserved2[2]; + u_int8_t assigning_org[8]; + u_int8_t medium_type_name[8]; + u_int8_t description[20]; +}; + +/* + * Security Protocol Specific values for the Tape Data Encryption protocol + * (0x20) used with SECURITY PROTOCOL IN. See below for values used with + * SECURITY PROTOCOL OUT. Current as of SSC4r03. + */ +#define TDE_IN_SUPPORT_PAGE 0x0000 +#define TDE_OUT_SUPPORT_PAGE 0x0001 +#define TDE_DATA_ENC_CAP_PAGE 0x0010 +#define TDE_SUPPORTED_KEY_FORMATS_PAGE 0x0011 +#define TDE_DATA_ENC_MAN_CAP_PAGE 0x0012 +#define TDE_DATA_ENC_STATUS_PAGE 0x0020 +#define TDE_NEXT_BLOCK_ENC_STATUS_PAGE 0x0021 +#define TDE_GET_ENC_MAN_ATTR_PAGE 0x0022 +#define TDE_RANDOM_NUM_PAGE 0x0030 +#define TDE_KEY_WRAP_PK_PAGE 0x0031 + +/* + * Tape Data Encryption protocol pages used with SECURITY PROTOCOL IN and + * SECURITY PROTOCOL OUT. + */ +/* + * Tape Data Encryption In Support page (0x0000). + */ +struct tde_in_support_page { + uint8_t page_code[2]; + uint8_t page_length[2]; + uint8_t page_codes[]; +}; + +/* + * Tape Data Encryption Out Support page (0x0001). + */ +struct tde_out_support_page { + uint8_t page_code[2]; + uint8_t page_length[2]; + uint8_t page_codes[]; +}; + +/* + * Logical block encryption algorithm descriptor. This is reported in the + * Data Encryption Capabilities page. + */ +struct tde_block_enc_alg_desc { + uint8_t alg_index; + uint8_t reserved1; + uint8_t desc_length[2]; + uint8_t byte4; +#define TDE_BEA_AVFMV 0x80 +#define TDE_BEA_SDK_C 0x40 +#define TDE_BEA_MAC_C 0x20 +#define TDE_BEA_DELB_C 0x10 +#define TDE_BEA_DECRYPT_C_MASK 0x0c +#define TDE_BEA_DECRYPT_C_EXT 0x0c +#define TDE_BEA_DECRYPT_C_HARD 0x08 +#define TDE_BEA_DECRYPT_C_SOFT 0x04 +#define TDE_BEA_DECRYPT_C_NO_CAP 0x00 +#define TDE_BEA_ENCRYPT_C_MASK 0x03 +#define TDE_BEA_ENCRYPT_C_EXT 0x03 +#define TDE_BEA_ENCRYPT_C_HARD 0x02 +#define TDE_BEA_ENCRYPT_C_SOFT 0x01 +#define TDE_BEA_ENCRYPT_C_NO_CAP 0x00 + uint8_t byte5; +#define TDE_BEA_AVFCLP_MASK 0xc0 +#define TDE_BEA_AVFCLP_VALID 0x80 +#define TDE_BEA_AVFCLP_NOT_VALID 0x40 +#define TDE_BEA_AVFCLP_NOT_APP 0x00 +#define TDE_BEA_NONCE_C_MASK 0x30 +#define TDE_BEA_NONCE_C_SUPPORTED 0x30 +#define TDE_BEA_NONCE_C_PROVIDED 0x20 +#define TDE_BEA_NONCE_C_GENERATED 0x10 +#define TDE_BEA_NONCE_C_NOT_REQUIRED 0x00 +#define TDE_BEA_KADF_C 0x08 +#define TDE_BEA_VCELB_C 0x04 +#define TDE_BEA_UKADF 0x02 +#define TDE_BEA_AKADF 0x01 + uint8_t max_unauth_key_bytes[2]; + uint8_t max_auth_key_bytes[2]; + uint8_t lbe_key_size[2]; + uint8_t byte12; +#define TDE_BEA_DKAD_C_MASK 0xc0 +#define TDE_BEA_DKAD_C_CAPABLE 0xc0 +#define TDE_BEA_DKAD_C_NOT_ALLOWED 0x80 +#define TDE_BEA_DKAD_C_REQUIRED 0x40 +#define TDE_BEA_EEMC_C_MASK 0x30 +#define TDE_BEA_EEMC_C_ALLOWED 0x20 +#define TDE_BEA_EEMC_C_NOT_ALLOWED 0x10 +#define TDE_BEA_EEMC_C_NOT_SPECIFIED 0x00 + /* + * Raw Decryption Mode Control Capabilities (RDMC_C) field. The + * descriptions are too complex to represent as a simple name. + */ +#define TDE_BEA_RDMC_C_MASK 0x0e +#define TDE_BEA_RDMC_C_MODE_7 0x0e +#define TDE_BEA_RDMC_C_MODE_6 0x0c +#define TDE_BEA_RDMC_C_MODE_5 0x0a +#define TDE_BEA_RDMC_C_MODE_4 0x08 +#define TDE_BEA_RDMC_C_MODE_1 0x02 +#define TDE_BEA_EAREM 0x01 + uint8_t byte13; +#define TDE_BEA_MAX_EEDKS_MASK 0x0f + uint8_t msdk_count[2]; + uint8_t max_eedk_size[2]; + uint8_t reserved2[2]; + uint8_t security_algo_code[4]; +}; + +/* + * Data Encryption Capabilities page (0x0010). + */ +struct tde_data_enc_cap_page { + uint8_t page_code[2]; + uint8_t page_length; + uint8_t byte4; +#define DATA_ENC_CAP_EXTDECC_MASK 0x0c +#define DATA_ENC_CAP_EXTDECC_NOT_REPORTED 0x00 +#define DATA_ENC_CAP_EXTDECC_NOT_CAPABLE 0x04 +#define DATA_ENC_CAP_EXTDECC_CAPABLE 0x08 +#define DATA_ENC_CAP_CFG_P_MASK 0x03 +#define DATA_ENC_CAP_CFG_P_NOT_REPORTED 0x00 +#define DATA_ENC_CAP_CFG_P_ALLOWED 0x01 +#define DATA_ENC_CAP_CFG_P_NOT_ALLOWED 0x02 + uint8_t reserved[15]; + struct tde_block_enc_alg_desc alg_descs[]; +}; + +/* + * Tape Data Encryption Supported Key Formats page (0x0011). + */ +struct tde_supported_key_formats_page { + uint8_t page_code[2]; + uint8_t page_length[2]; + uint8_t key_formats_list[]; +}; + +/* + * Tape Data Encryption Management Capabilities page (0x0012). + */ +struct tde_data_enc_man_cap_page { + uint8_t page_code[2]; + uint8_t page_length[2]; + uint8_t byte4; +#define TDE_DEMC_LOCK_C 0x01 + uint8_t byte5; +#define TDE_DEMC_CKOD_C 0x04 +#define TDE_DEMC_CKORP_C 0x02 +#define TDE_DEMC_CKORL_C 0x01 + uint8_t reserved1; + uint8_t byte7; +#define TDE_DEMC_AITN_C 0x04 +#define TDE_DEMC_LOCAL_C 0x02 +#define TDE_DEMC_PUBLIC_C 0x01 + uint8_t reserved2[8]; +}; + +/* + * Tape Data Encryption Status Page (0x0020). + */ +struct tde_data_enc_status_page { + uint8_t page_code[2]; + uint8_t page_length[2]; + uint8_t scope; +#define TDE_DES_IT_NEXUS_SCOPE_MASK 0xe0 +#define TDE_DES_LBE_SCOPE_MASK 0x07 + uint8_t encryption_mode; + uint8_t decryption_mode; + uint8_t algo_index; + uint8_t key_instance_counter[4]; + uint8_t byte12; +#define TDE_DES_PARAM_CTRL_MASK 0x70 +#define TDE_DES_PARAM_CTRL_MGMT 0x40 +#define TDE_DES_PARAM_CTRL_CHANGER 0x30 +#define TDE_DES_PARAM_CTRL_DRIVE 0x20 +#define TDE_DES_PARAM_CTRL_EXT 0x10 +#define TDE_DES_PARAM_CTRL_NOT_REPORTED 0x00 +#define TDE_DES_VCELB 0x08 +#define TDE_DES_CEEMS_MASK 0x06 +#define TDE_DES_RDMD 0x01 + uint8_t enc_params_kad_format; + uint8_t asdk_count[2]; + uint8_t reserved[8]; + uint8_t key_assoc_data_desc[]; +}; + +/* + * Tape Data Encryption Next Block Encryption Status page (0x0021). + */ +struct tde_next_block_enc_status_page { + uint8_t page_code[2]; + uint8_t page_length[2]; + uint8_t logical_obj_number[8]; + uint8_t status; +#define TDE_NBES_COMP_STATUS_MASK 0xf0 +#define TDE_NBES_COMP_INCAPABLE 0x00 +#define TDE_NBES_COMP_NOT_YET 0x10 +#define TDE_NBES_COMP_NOT_A_BLOCK 0x20 +#define TDE_NBES_COMP_NOT_COMPRESSED 0x30 +#define TDE_NBES_COMP_COMPRESSED 0x40 +#define TDE_NBES_ENC_STATUS_MASK 0x0f +#define TDE_NBES_ENC_INCAPABLE 0x00 +#define TDE_NBES_ENC_NOT_YET 0x01 +#define TDE_NBES_ENC_NOT_A_BLOCK 0x02 +#define TDE_NBES_ENC_NOT_ENCRYPTED 0x03 +#define TDE_NBES_ENC_ALG_NOT_SUPPORTED 0x04 +#define TDE_NBES_ENC_SUPPORTED_ALG 0x05 +#define TDE_NBES_ENC_NO_KEY 0x06 + uint8_t algo_index; + uint8_t byte14; +#define TDE_NBES_EMES 0x02 +#define TDE_NBES_RDMDS 0x01 + uint8_t next_block_kad_format; + uint8_t key_assoc_data_desc[]; +}; + +/* + * Tape Data Encryption Get Encryption Management Attributes page (0x0022). + */ +struct tde_get_enc_man_attr_page { + uint8_t page_code[2]; + uint8_t reserved[3]; + uint8_t byte5; +#define TDE_GEMA_CAOD 0x01 + uint8_t page_length[2]; + uint8_t enc_mgmt_attr_desc[]; +}; + +/* + * Tape Data Encryption Random Number page (0x0030). + */ +struct tde_random_num_page { + uint8_t page_code[2]; + uint8_t page_length[2]; + uint8_t random_number[32]; +}; + +/* + * Tape Data Encryption Device Server Key Wrapping Public Key page (0x0031). + */ +struct tde_key_wrap_pk_page { + uint8_t page_code[2]; + uint8_t page_length[2]; + uint8_t public_key_type[4]; + uint8_t public_key_format[4]; + uint8_t public_key_length[2]; + uint8_t public_key[]; +}; + +/* + * Security Protocol Specific values for the Tape Data Encryption protocol + * (0x20) used with SECURITY PROTOCOL OUT. See above for values used with + * SECURITY PROTOCOL IN. Current as of SSCr03. + */ +#define TDE_SET_DATA_ENC_PAGE 0x0010 +#define TDE_SA_ENCAP_PAGE 0x0011 +#define TDE_SET_ENC_MGMT_ATTR_PAGE 0x0022 + +/* + * Tape Data Encryption Set Data Encryption page (0x0010). + */ +struct tde_set_data_enc_page { + uint8_t page_code[2]; + uint8_t page_length[2]; + uint8_t byte4; +#define TDE_SDE_SCOPE_MASK 0xe0 +#define TDE_SDE_SCOPE_ALL_IT_NEXUS 0x80 +#define TDE_SDE_SCOPE_LOCAL 0x40 +#define TDE_SDE_SCOPE_PUBLIC 0x00 +#define TDE_SDE_LOCK 0x01 + uint8_t byte5; +#define TDE_SDE_CEEM_MASK 0xc0 +#define TDE_SDE_CEEM_ENCRYPT 0xc0 +#define TDE_SDE_CEEM_EXTERNAL 0x80 +#define TDE_SDE_CEEM_NO_CHECK 0x40 +#define TDE_SDE_RDMC_MASK 0x30 +#define TDE_SDE_RDMC_DISABLED 0x30 +#define TDE_SDE_RDMC_ENABLED 0x20 +#define TDE_SDE_RDMC_DEFAULT 0x00 +#define TDE_SDE_SDK 0x08 +#define TDE_SDE_CKOD 0x04 +#define TDE_SDE_CKORP 0x02 +#define TDE_SDE_CKORL 0x01 + uint8_t encryption_mode; +#define TDE_SDE_ENC_MODE_DISABLE 0x00 +#define TDE_SDE_ENC_MODE_EXTERNAL 0x01 +#define TDE_SDE_ENC_MODE_ENCRYPT 0x02 + uint8_t decryption_mode; +#define TDE_SDE_DEC_MODE_DISABLE 0x00 +#define TDE_SDE_DEC_MODE_RAW 0x01 +#define TDE_SDE_DEC_MODE_DECRYPT 0x02 +#define TDE_SDE_DEC_MODE_MIXED 0x03 + uint8_t algo_index; + uint8_t lbe_key_format; +#define TDE_SDE_KEY_PLAINTEXT 0x00 +#define TDE_SDE_KEY_VENDOR_SPEC 0x01 +#define TDE_SDE_KEY_PUBLIC_WRAP 0x02 +#define TDE_SDE_KEY_ESP_SCSI 0x03 + uint8_t kad_format; +#define TDE_SDE_KAD_ASCII 0x02 +#define TDE_SDE_KAD_BINARY 0x01 +#define TDE_SDE_KAD_UNSPECIFIED 0x00 + uint8_t reserved[7]; + uint8_t lbe_key_length[2]; + uint8_t lbe_key[]; +}; + +/* + * Used for the Vendor Specific key format (0x01). + */ +struct tde_key_format_vendor { + uint8_t t10_vendor_id[8]; + uint8_t vendor_key[]; +}; + +/* + * Used for the public key wrapped format (0x02). + */ +struct tde_key_format_public_wrap { + uint8_t parameter_set[2]; +#define TDE_PARAM_SET_RSA2048 0x0000 +#define TDE_PARAM_SET_ECC521 0x0010 + uint8_t label_length[2]; + uint8_t label[]; +}; + +/* + * Tape Data Encryption SA Encapsulation page (0x0011). + */ +struct tde_sa_encap_page { + uint8_t page_code[2]; + uint8_t data_desc[]; +}; + +/* + * Tape Data Encryption Set Encryption Management Attributes page (0x0022). + */ +struct tde_set_enc_mgmt_attr_page { + uint8_t page_code[2]; + uint8_t reserved[3]; + uint8_t byte5; +#define TDE_SEMA_CAOD 0x01 + uint8_t page_length[2]; + uint8_t attr_desc[]; +}; + +/* + * Tape Data Encryption descriptor format. + * SSC4r03 Section 8.5.4.2.1 Table 197 + */ +struct tde_data_enc_desc { + uint8_t key_desc_type; +#define TDE_KEY_DESC_WK_KAD 0x04 +#define TDE_KEY_DESC_M_KAD 0x03 +#define TDE_KEY_DESC_NONCE_VALUE 0x02 +#define TDE_KEY_DESC_A_KAD 0x01 +#define TDE_KEY_DESC_U_KAD 0x00 + uint8_t byte2; +#define TDE_KEY_DESC_AUTH_MASK 0x07 +#define TDE_KEY_DESC_AUTH_FAILED 0x04 +#define TDE_KEY_DESC_AUTH_SUCCESS 0x03 +#define TDE_KEY_DESC_AUTH_NO_ATTEMPT 0x02 +#define TDE_KEY_DESC_AUTH_U_KAD 0x01 + uint8_t key_desc_length[2]; + uint8_t key_desc[]; +}; + +/* + * Wrapped Key descriptor format. + * SSC4r03 Section 8.5.4.3.1 Table 200 + */ +struct tde_wrapped_key_desc { + uint8_t wrapped_key_type; +#define TDE_WRAP_KEY_DESC_LENGTH 0x04 +#define TDE_WRAP_KEY_DESC_IDENT 0x03 +#define TDE_WRAP_KEY_DESC_INFO 0x02 +#define TDE_WRAP_KEY_DESC_ENTITY_ID 0x01 +#define TDE_WRAP_KEY_DESC_DEVICE_ID 0x00 + uint8_t reserved; + uint8_t wrapped_desc_length[2]; + uint8_t wrapped_desc[]; +}; + +/* + * Encryption management attributes descriptor format. + * SSC4r03 Section 8.5.4.4.1 Table 202 + */ +struct tde_enc_mgmt_attr_desc { + uint8_t enc_mgmt_attr_type[2]; +#define TDE_EMAD_DESIRED_KEY_MGR_OP 0x0000 +#define TDE_EMAD_LOG_BLOCK_ENC_KEY_CRIT 0x0001 +#define TDE_EMAD_LOG_BLOCK_ENC_KEY_WRAP 0x0002 + uint8_t reserved; + uint8_t byte2; +#define TDE_EMAD_CRIT 0x80 + uint8_t attr_length[2]; + uint8_t attributes[]; +#define TDE_EMAD_DESIRED_KEY_CREATE 0x0001 +#define TDE_EMAD_DESIRED_KEY_RESOLVE 0x0002 +}; + +/* + * Logical block encryption key selection criteria descriptor format. + * SSC4r03 Section 8.5.4.4.3.1 Table 206 + */ +struct tde_lb_enc_key_sel_desc { + uint8_t lbe_key_sel_crit_type[2]; + /* + * The CRIT bit is the top bit of the first byte of the type. + */ +#define TDE_LBE_KEY_SEL_CRIT 0x80 +#define TDE_LBE_KEY_SEL_ALGO 0x0001 +#define TDE_LBE_KEY_SEL_ID 0x0002 + uint8_t lbe_key_sel_crit_length[2]; + uint8_t lbe_key_sel_crit[]; +}; + +/* + * Logical block encryption key wrapping attribute descriptor format. + * SSC4r03 Section 8.5.4.4.4.1 Table 209 + */ +struct tde_lb_enc_key_wrap_desc { + uint8_t lbe_key_wrap_type[2]; + /* + * The CRIT bit is the top bit of the first byte of the type. + */ +#define TDE_LBE_KEY_WRAP_CRIT 0x80 +#define TDE_LBE_KEY_WRAP_KEKS 0x0001 + uint8_t lbe_key_wrap_length[2]; + uint8_t lbe_key_wrap_attr[]; +}; + /* * Opcodes */ #define REWIND 0x01 +#define FORMAT_MEDIUM 0x04 #define READ_BLOCK_LIMITS 0x05 #define SA_READ 0x08 #define SA_WRITE 0x0A +#define SET_CAPACITY 0x0B #define WRITE_FILEMARKS 0x10 #define SPACE 0x11 #define RESERVE_UNIT 0x16 @@ -273,6 +932,9 @@ struct scsi_tape_locate { #define LOAD_UNLOAD 0x1B #define LOCATE 0x2B #define READ_POSITION 0x34 +#define REPORT_DENSITY_SUPPORT 0x44 +#define ALLOW_OVERWRITE 0x82 +#define LOCATE_16 0x92 /* * Tape specific density codes- only enough of them here to recognize @@ -352,11 +1014,55 @@ void scsi_read_position(struct ccb_scsiio *csio, u_int32_t retries, u_int8_t tag_action, int hardsoft, struct scsi_tape_position_data *sbp, u_int8_t sense_len, u_int32_t timeout); +void scsi_read_position_10(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int service_action, + u_int8_t *data_ptr, u_int32_t length, + u_int32_t sense_len, u_int32_t timeout); void scsi_set_position(struct ccb_scsiio *csio, u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), u_int8_t tag_action, int hardsoft, u_int32_t blkno, u_int8_t sense_len, u_int32_t timeout); + +void scsi_locate_10(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int immed, int cp, int hard, + int64_t partition, u_int32_t block_address, + int sense_len, u_int32_t timeout); + +void scsi_locate_16(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int immed, int cp, + u_int8_t dest_type, int bam, int64_t partition, + u_int64_t logical_id, int sense_len, + u_int32_t timeout); + +void scsi_report_density_support(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, + union ccb *), + u_int8_t tag_action, int media, + int medium_type, u_int8_t *data_ptr, + u_int32_t length, u_int32_t sense_len, + u_int32_t timeout); + +void scsi_set_capacity(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int byte1, u_int32_t proportion, + u_int32_t sense_len, u_int32_t timeout); + +void scsi_format_medium(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int byte1, int byte2, + u_int8_t *data_ptr, u_int32_t length, + u_int32_t sense_len, u_int32_t timeout); + +void scsi_allow_overwrite(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, int allow_overwrite, + int partition, u_int64_t logical_id, + u_int32_t sense_len, u_int32_t timeout); + __END_DECLS #endif /* _SCSI_SCSI_SA_H */ diff --git a/sys/sys/mtio.h b/sys/sys/mtio.h index 1ad8004e9..0d2701b82 100644 --- a/sys/sys/mtio.h +++ b/sys/sys/mtio.h @@ -80,6 +80,8 @@ struct mtop { #define MTWSS 16 /* write setmark(s) */ #define MTFSS 17 /* forward space setmark */ #define MTBSS 18 /* backward space setmark */ +#define MTLOAD 19 /* load tape in drive */ +#define MTWEOFI 20 /* write an end-of-file record without waiting*/ #define MT_COMP_ENABLE 0xffffffff #define MT_COMP_DISABLED 0xfffffffe @@ -176,6 +178,112 @@ union mterrstat { char _reserved_padding[256]; }; +struct mtrblim { + uint32_t granularity; + uint32_t min_block_length; + uint32_t max_block_length; +}; + +typedef enum { + MT_LOCATE_DEST_OBJECT = 0x00, + MT_LOCATE_DEST_FILE = 0x01, + MT_LOCATE_DEST_SET = 0x02, + MT_LOCATE_DEST_EOD = 0x03 +} mt_locate_dest_type; + +typedef enum { + MT_LOCATE_BAM_IMPLICIT = 0x00, + MT_LOCATE_BAM_EXPLICIT = 0x01 +} mt_locate_bam; + +typedef enum { + MT_LOCATE_FLAG_IMMED = 0x01, + MT_LOCATE_FLAG_CHANGE_PART = 0x02 +} mt_locate_flags; + +struct mtlocate { + mt_locate_flags flags; + mt_locate_dest_type dest_type; + mt_locate_bam block_address_mode; + int64_t partition; + uint64_t logical_id; + uint8_t reserved[64]; +}; + +typedef enum { + MT_EXT_GET_NONE, + MT_EXT_GET_OK, + MT_EXT_GET_NEED_MORE_SPACE, + MT_EXT_GET_ERROR +} mt_ext_get_status; + +struct mtextget { + uint32_t alloc_len; + char *status_xml; + uint32_t fill_len; + mt_ext_get_status status; + char error_str[128]; + uint8_t reserved[64]; +}; + +#define MT_EXT_GET_ROOT_NAME "mtextget" +#define MT_DENSITY_ROOT_NAME "mtdensity" +#define MT_MEDIA_DENSITY_NAME "media_density" +#define MT_DENSITY_REPORT_NAME "density_report" +#define MT_MEDIUM_TYPE_REPORT_NAME "medium_type_report" +#define MT_MEDIA_REPORT_NAME "media_report" +#define MT_DENSITY_ENTRY_NAME "density_entry" + +#define MT_DENS_WRITE_OK 0x80 +#define MT_DENS_DUP 0x40 +#define MT_DENS_DEFLT 0x20 + + +#define MT_PARAM_FIXED_STR_LEN 32 +union mt_param_value { + int64_t value_signed; + uint64_t value_unsigned; + char *value_var_str; + char value_fixed_str[MT_PARAM_FIXED_STR_LEN]; + uint8_t reserved[64]; +}; + +typedef enum { + MT_PARAM_SET_NONE, + MT_PARAM_SET_SIGNED, + MT_PARAM_SET_UNSIGNED, + MT_PARAM_SET_VAR_STR, + MT_PARAM_SET_FIXED_STR +} mt_param_set_type; + +typedef enum { + MT_PARAM_STATUS_NONE, + MT_PARAM_STATUS_OK, + MT_PARAM_STATUS_ERROR +} mt_param_set_status; + +#define MT_PARAM_VALUE_NAME_LEN 64 +struct mtparamset { + char value_name[MT_PARAM_VALUE_NAME_LEN]; + mt_param_set_type value_type; + int value_len; + union mt_param_value value; + mt_param_set_status status; + char error_str[128]; +}; + +#define MT_PARAM_ROOT_NAME "mtparamget" +#define MT_PROTECTION_NAME "protection" + +/* + * Set a list of parameters. + */ +struct mtsetlist { + int num_params; + int param_len; + struct mtparamset *params; +}; + /* * Constants for mt_type byte. These are the same * for controllers compatible with the types listed. @@ -218,6 +326,7 @@ union mterrstat { #define MTIOCSLOCATE _IOW('m', 5, u_int32_t) /* seek to logical blk addr */ #define MTIOCHLOCATE _IOW('m', 6, u_int32_t) /* seek to hardware blk addr */ #define MTIOCERRSTAT _IOR('m', 7, union mterrstat) /* get tape errors */ + /* * Set EOT model- argument is number of filemarks to end a tape with. * Note that not all possible values will be accepted. @@ -226,6 +335,13 @@ union mterrstat { /* Get current EOT model */ #define MTIOCGETEOTMODEL _IOR('m', 8, u_int32_t) +#define MTIOCRBLIM _IOR('m', 9, struct mtrblim) /* get block limits */ +#define MTIOCEXTLOCATE _IOW('m', 10, struct mtlocate) /* seek to position */ +#define MTIOCEXTGET _IOWR('m', 11, struct mtextget) /* get tape status */ +#define MTIOCPARAMGET _IOWR('m', 12, struct mtextget) /* get tape params */ +#define MTIOCPARAMSET _IOWR('m', 13, struct mtparamset) /* set tape params */ +#define MTIOCSETLIST _IOWR('m', 14, struct mtsetlist) /* set N params */ + #ifndef _KERNEL #define DEFTAPE "/dev/nsa0" #endif diff --git a/sys/sys/param.h b/sys/sys/param.h index 9c040f72f..f523c20b8 100644 --- a/sys/sys/param.h +++ b/sys/sys/param.h @@ -58,7 +58,7 @@ * in the range 5 to 9. */ #undef __FreeBSD_version -#define __FreeBSD_version 1001511 /* Master, propagated to newvers */ +#define __FreeBSD_version 1001512 /* Master, propagated to newvers */ /* * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD, diff --git a/usr.bin/mt/Makefile b/usr.bin/mt/Makefile index 25b4af5b8..802eed18e 100644 --- a/usr.bin/mt/Makefile +++ b/usr.bin/mt/Makefile @@ -2,5 +2,7 @@ # $FreeBSD$ PROG= mt +DPADD= ${LIBMT} ${LIBSBUF} ${LIBBSDXML} +LDADD= -lmt -lsbuf -lbsdxml .include diff --git a/usr.bin/mt/mt.1 b/usr.bin/mt/mt.1 index 8e9f63a1e..33c1c1814 100644 --- a/usr.bin/mt/mt.1 +++ b/usr.bin/mt/mt.1 @@ -29,7 +29,7 @@ .\" @(#)mt.1 8.1 (Berkeley) 6/6/93 .\" $FreeBSD$ .\" -.Dd January 20, 2008 +.Dd March 3, 2014 .Dt MT 1 .Os .Sh NAME @@ -71,6 +71,12 @@ which defaults to 1. Write .Ar count end-of-file (EOF) marks at the current position. +This returns when the file mark has been written to the media. +.It Cm weofi +Write +.Ar count +end-of-file (EOF) marks at the current position. +This returns as soon as the command has been validated by the tape drive. .It Cm smk Write .Ar count @@ -129,6 +135,8 @@ Rewind the tape. .It Cm offline , rewoffl Rewind the tape and place the drive off line. Some drives are never off line. +.It Cm load +Load the tape into the drive. .It Cm retension Re-tension the tape. This winds the tape from the current position to the end @@ -136,7 +144,7 @@ and then to the beginning. This sometimes improves subsequent reading and writing, particularly for streaming drives. Some drives do not support this. -.It Cm status +.It Cm ostatus Output status information about the drive. For SCSI magnetic tape devices, the current operating modes of density, blocksize, and whether compression @@ -149,6 +157,9 @@ Note that this information is not definitive (only BOT, End of Recorded Media, and hardware or SCSI logical block position (if the drive supports such) are considered definitive tape positions). +.Pp +Also note that this is the old status command, and will be eliminated in +favor of the new status command (see below) in a future release. .It Cm errstat Output (and clear) error status information about this device. For every normal @@ -165,9 +176,12 @@ many filemarks will be written at close if a tape was being written. .It Cm eod , eom Wind the tape to the end of the recorded data, typically after an EOF mark where another file may be written. +.It Cm rblim +Report the block limits of the tape drive, including the minimum and +maximum block size, and the block granularity if any. .El .Pp -The following commands require an +The following commands may require an .Ar argument . .Bl -tag -width ".Cm seteotmodel" .It Cm sethpos @@ -199,6 +213,180 @@ You may only choose a value of .Ar 1 or .Ar 2 . +.It Cm status +Output status information about the drive. +For SCSI magnetic tape devices, +the current operating modes of density, blocksize, and whether compression +is enabled is reported. +The current state of the driver (what it thinks that +it is doing with the device) is reported. +.Pp +If the driver knows the relative +position from BOT (in terms of filemarks and records), it outputs that. +If the tape drive supports the long form report of the +.Tn SCSI +READ POSITION command, the Reported File Number and Reported Record Number +will be numbers other than -1, and there may be Flags reported as well. +.Pp +The BOP flag means that the logical position of the drive is at the +beginning of the partition. +.Pp +The EOP flag means that the logical position of the drive is between Early +Warning and End of Partition. +.Pp +The BPEW flag means that the logical position of the drive is in a +Programmable Early Warning Zone or on the EOP side of Early Warning. +.Pp +Note that the Reported Record Number is the tape block or object number +relative to the beginning of the partition. +The Calculated Record Number is the tape block or object number relative +to the previous file mark. +.Pp +Note +that the Calculated File and Record Numbers are not definitive. +The Reported File and Record Numbers are definitive, if they are numbers +other than -1. +.Bl -tag -width 6n +.It Fl v +Print additional status information, such as the maximum supported I/O +size. +.It Fl x +Print all available status data to stdout in XML format. +.El +.It Cm getdensity +Report density support information for the tape drive and any media that is +loaded. +Most drives will report at least basic density information similar to that +reported by +.Nm status +command. +Newer tape drives that conform to the T-10 SSC and newer tape +specifications may report more detailed information about the types of +tapes they support and the tape currently in the drive. +.Bl -tag -width 6n +.It Fl x +Print all available density data to stdout in XML format. +Because density information is currently included in the general status XML +report used for +.Nm +status command, this will be the same XML output via +.Do +.Nm +status +.Fl x +.Dc +.El +.It Cm param +Display or set parameters. +One of +.Fl l , +.Fl s , +or +.Fl x +must be specified to indicate which operation to perform. +.Bl -tag -width 8n +.It Fl l +List parameters, values and descriptions. +By default all parameters will be displayed. +To display a specific parameter, specify the parameter with +.Fl p . +.It Fl p Ar name +Specify the parameter name to list (with +.Fl l ) +or set (with +.Fl s ) . +.It Fl q +Enable quiet mode for parameter listing. +This will suppress printing of parameter descriptions. +.It Fl s Ar value +Specify the parameter value to set. +The general type of this argument (integer, unsigned integer, string) is +determined by the type of the variable indicated by the +.Xr sa 4 +driver. +More detailed argument checking is done by the +.Xr sa 4 +driver. +.It Fl x +Print out all parameter information in XML format. +.El +.It Cm protect +Display or set drive protection parameters. +This is used to control checking and reporting a per-block checksum for +tape drives that support it. +Some drives may only support some parameters. +.Bl -tag -width 8n +.It Fl b Ar 0|1 +Set the Recover Buffered Data Protected bit. +If set, this indicates that checksums are transferred with the logical +blocks transferred by the RECOVERED BUFFERED DATA +.Tn SCSI +command. +.It Fl d +Disable all protection information settings. +.It Fl e +Enable all protection information settings. +The default protection method used is Reed-Solomon CRC (protection method +1), as specified in ECMA-319. +The default protection information length used with Reed-Solomon CRC is +4 bytes. +To enable all settings except one more more settings, specify the +.Fl e +argument and then explicitly disable settings that you do not wish to +enable. +For example, specifying +.Fl e +.Fl w Ar 0 +will enable all settings except for LBP_W. +.It Fl l +List available protection parmeters and their current settings. +.It Fl L Ar len +Set the length of the protection information in bytes. +For Reed-Solomon CRC, the protection information length should be 4 bytes. +.It Fl m Ar num +Specify the numeric value for the protection method. +The numeric value for Reed-Solomon CRC is 1. +.It Fl r Ar 0|1 +Set the LBP_R parameter. +When set, this indicates that each block read from the tape drive will +have a checksum at the end. +.It Fl v +Enable verbose mode for parameter listing. +This will include descriptions of each parameter. +.It Fl w Ar 0|1 +Set the LBP_W parameter. +When set, this indicates that each block written to the tape drive will have +a checksum at the end. +The drive will verify the checksum before writing the block to tape. +.El +.It Cm locate +Set the tape drive's logical position. +One of +.Fl b , +.Fl e , +.Fl f , +or +.Fl s +must be specified to indicate the type of position. +If the partition number is specified, the drive will first relocate to the +given partition (if it exists) and then to the position indicated within +that partition. +If the partition number is not specified, the drive will relocate to the +given position within the current partition. +.Bl -tag -width 14n +.It Fl b Ar block_addr +Relocate to the given tape block or logical object identifier. +Note that the block number is the Reported Record Number that is relative +to the beginning of the partition (or beginning of tape). +.It Fl e +Relocate to the end of data. +.It Fl f Ar fileno +Relocate to the given file number. +.It Fl p Ar partition +Specify the partition to change to. +.It Fl s Ar setmark +Relocate to the given set mark. +.El .It Cm comp Set the drive's compression mode. The non-numeric values of @@ -234,6 +422,13 @@ If this is not the case (see the display to see which compression algorithm is currently in use), the user can manually specify one of the supported compression keywords (above), or supply a numeric compression value from the drive's specifications. +.Pp +Note that for some older tape drives (for example the Exabyte 8200 and 8500 +series drives) it is necessary to switch to a different density to tell the +drive to record data in its compressed format. +If the user attempts to turn compression on while the uncompressed density +is selected, the drive will return an error. +This is generally not an issue for modern tape drives. .It Cm density Set the density for the drive. For the density codes, see below. @@ -249,13 +444,14 @@ exactly, an informational message is output about what the given string has been taken for. .El .Pp -The following density table was taken from the +The initial version of the density table below was taken from the .Sq Historical sequential access density codes table (A-1) in Revision 11 of the SCSI-3 Stream Device Commands (SSC) working draft, dated November 11, 1997. +Subsequent additions have come from a number of sources. .Pp The density codes are: -.Bd -literal -offset 3n +.Bd -literal -offset 2n 0x0 default for device 0xE reserved for ECMA @@ -278,8 +474,8 @@ Value Width Tracks Density Code Type Reference Note 0x11 6.3 (0.25) 26 630 (16,000) GCR C QIC-320 1,6 0x12 6.3 (0.25) 30 2,034 (51,667) RLL C QIC-1350 1,6 0x13 3.81 (0.15) 1 2,400 (61,000) DDS CS X3B5/88-185A 5 -0x14 8.0 (0.315) 1 1,703 (43,245) RLL CS X3.202-1991 5 -0x15 8.0 (0.315) 1 1,789 (45,434) RLL CS ECMA TC17 5 +0x14 8.0 (0.315) 1 1,703 (43,245) RLL CS X3.202-1991 5,11 +0x15 8.0 (0.315) 1 1,789 (45,434) RLL CS ECMA TC17 5,12 0x16 12.7 (0.5) 48 394 (10,000) MFM C X3.193-1990 1 0x17 12.7 (0.5) 48 1,673 (42,500) MFM C X3B5/91-174 1 0x18 12.7 (0.5) 112 1,673 (42,500) MFM C X3B5/92-50 1 @@ -302,11 +498,34 @@ Value Width Tracks Density Code Type Reference Note 0x29 12.7 (0.5) 0x2A 0x2B 12.7 (0.5) 3 ? ? ? C X3.267 5 +0x40 12.7 (0.5) 384 4,800 (123,952) C LTO-1 0x41 12.7 (0.5) 208 3,868 (98,250) RLL C DLTapeIV(40) 6,7 -0x48 12.7 (0.5) 448 5,236 (133,000) PRML C SDLTapeI(110) 6,8 +0x42 12.7 (0.5) 512 7,398 (187,909) C LTO-2 +0x44 12.7 (0.5) 704 9,638 (244,805) C LTO-3 +0x46 12.7 (0.5) 896 12,725 (323,215) C LTO-4 +0x47 3.81 (0.25) ? 6,417 (163,000) CS DAT-72 +0x48 12.7 (0.5) 448 5,236 (133,000) PRML C SDLTapeI(110) 6,8,13 0x49 12.7 (0.5) 448 7,598 (193,000) PRML C SDLTapeI(160) 6,8 +0x4A 12.7 (0.5) 768 ? C T10000A 10 +0x4B 12.7 (0.5) 1152 ? C T10000B 10 +0x4C 12.7 (0.5) 3584 ? C T10000C 10 +0x4D 12.7 (0.5) 4608 ? C T10000D 10 +0x51 12.7 (0.5) 512 11,800 (299,720) C 3592A1 (unencrypted) +0x52 12.7 (0.5) 896 11,800 (299,720) C 3592A2 (unencrypted) +0x53 12.7 (0.5) 1152 13,452 (341,681) C 3592A3 (unencrypted) +0x54 12.7 (0.5) 2560 19,686 (500,024) C 3592A4 (unencrypted) +0x55 12.7 (0.5) 5120 20,670 (525,018) C 3592A5 (unencrypted) +0x58 12.7 (0.5) 1280 15,142 (384,607) C LTO-5 +0x5A 12.7 (0.5) 2176 15,142 (384,607) C LTO-6 +0x71 12.7 (0.5) 512 11,800 (299,720) C 3592A1 (encrypted) +0x72 12.7 (0.5) 896 11,800 (299,720) C 3592A2 (encrypted) +0x73 12.7 (0.5) 1152 13,452 (341,681) C 3592A3 (encrypted) +0x74 12.7 (0.5) 2560 19,686 (500,024) C 3592A4 (encrypted) +0x75 12.7 (0.5) 5120 20,670 (525,018) C 3592A5 (encrypted) +0x8c 8.0 (0.315) 1 1,789 (45,434) RLL CS EXB-8500c 5,9 +0x90 8.0 (0.315) 1 1,703 (43,245) RLL CS EXB-8200c 5,9 .Ed -.Bd -literal -offset 3n +.Bd -literal -offset 2n Code Description Type Description ---- -------------------------------------- ---- ----------- NRZI Non return to zero, change on ones R Reel-to-reel @@ -318,18 +537,27 @@ DDS DAT data storage RLL Run length limited PRML Partial Response Maximum Likelihood .Ed -.Bd -literal -offset 3n +.Bd -literal -offset 2n NOTES -1. Serial recorded. -2. Parallel recorded. -3. Old format known as QIC-11. -5. Helical scan. -6. This is not an American National Standard. The reference is based on - an industry standard definition of the media format. -7. DLT recording: serially recorded track pairs (DLTapeIII and - DLTapeIV(20)), or track quads (DLTapeIV(35) and DLTapeIV(40)). -8. Super DLT (SDLT) recording: 56 serially recorded logical tracks with - 8 physical tracks each. +1. Serial recorded. +2. Parallel recorded. +3. Old format known as QIC-11. +5. Helical scan. +6. This is not an American National Standard. The reference is based + on an industry standard definition of the media format. +7. DLT recording: serially recorded track pairs (DLTapeIII and + DLTapeIV(20)), or track quads (DLTapeIV(35) and DLTapeIV(40)). +8. Super DLT (SDLT) recording: 56 serially recorded logical tracks + with 8 physical tracks each. +9. Vendor-specific Exabyte density code for compressed format. +10. bpi/bpmm values for the Oracle/StorageTek T10000 tape drives are + not listed in the manual. Someone with access to a drive can + supply the necessary values by running 'mt getdensity'. +11. This is Exabyte 8200 uncompressed format. The compressed format + density code is 0x90. +12. This is Exabyte 8500 uncompressed format. The compressed format + density code is 0x8c. +13. This density code (0x48) was also used for DAT-160. .Ed .Sh ENVIRONMENT .Bl -tag -width ".Ev TAPE" @@ -343,8 +571,6 @@ option. .El .Sh FILES .Bl -tag -width ".Pa /dev/*sa[0-9]*" -compact -.It Pa /dev/*wt* -QIC-02/QIC-36 magnetic tape interface .It Pa /dev/*sa[0-9]* SCSI magnetic tape interface .El @@ -357,7 +583,6 @@ Some undocumented commands support old software. .Sh SEE ALSO .Xr dd 1 , .Xr ioctl 2 , -.Xr ast 4 , .Xr mtio 4 , .Xr sa 4 , .Xr environ 7 diff --git a/usr.bin/mt/mt.c b/usr.bin/mt/mt.c index 3fbbdf394..745766c3d 100644 --- a/usr.bin/mt/mt.c +++ b/usr.bin/mt/mt.c @@ -26,6 +26,37 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +/*- + * Copyright (c) 2013, 2014, 2015 Spectra Logic Corporation + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions, and the following disclaimer, + * without modification. + * 2. Redistributions in binary form must reproduce at minimum a disclaimer + * substantially similar to the "NO WARRANTY" disclaimer below + * ("Disclaimer") and any redistribution must be conditioned upon + * including a substantially similar Disclaimer requirement for further + * binary redistribution. + * + * NO WARRANTY + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGES. + * + * Authors: Ken Merry (Spectra Logic Corporation) + */ #ifndef lint static const char copyright[] = @@ -49,6 +80,8 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include +#include #include #include @@ -57,6 +90,16 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include +#include +#include +#include + +#include +#include +#include +#include +#include /* the appropriate sections of are also #ifdef'd for FreeBSD */ /* c_flags */ @@ -65,6 +108,7 @@ __FBSDID("$FreeBSD$"); #define IS_DENSITY 0x04 #define DISABLE_THIS 0x08 #define IS_COMP 0x10 +#define USE_GETOPT 0x20 #ifndef TRUE #define TRUE 1 @@ -72,6 +116,16 @@ __FBSDID("$FreeBSD$"); #ifndef FALSE #define FALSE 0 #endif +#ifndef MAX +#define MAX(a, b) (a > b) ? a : b +#endif +#define MT_PLURAL(a) (a == 1) ? "" : "s" + +typedef enum { + MT_CMD_NONE = MTLOAD + 1, + MT_CMD_PROTECT, + MT_CMD_GETDENSITY +} mt_commands; static const struct commands { const char *c_name; @@ -87,10 +141,12 @@ static const struct commands { { "fsf", MTFSF, 1, 0 }, { "fsr", MTFSR, 1, 0 }, { "offline", MTOFFL, 1, 0 }, + { "load", MTLOAD, 1, 0 }, { "rewind", MTREW, 1, 0 }, { "rewoffl", MTOFFL, 1, 0 }, - { "status", MTNOP, 1, 0 }, + { "ostatus", MTNOP, 1, 0 }, { "weof", MTWEOF, 0, ZERO_ALLOWED }, + { "weofi", MTWEOFI, 0, ZERO_ALLOWED }, { "erase", MTERASE, 0, ZERO_ALLOWED}, { "blocksize", MTSETBSIZ, 0, NEED_2ARGS|ZERO_ALLOWED }, { "density", MTSETDNSTY, 0, NEED_2ARGS|ZERO_ALLOWED|IS_DENSITY }, @@ -111,17 +167,39 @@ static const struct commands { { "seteotmodel", MTIOCSETEOTMODEL, 0, NEED_2ARGS|ZERO_ALLOWED }, { "getmodel", MTIOCGETEOTMODEL, 0, 0 }, { "geteotmodel", MTIOCGETEOTMODEL, 0, 0 }, + { "rblim", MTIOCRBLIM, 0, 0}, + { "getdensity", MT_CMD_GETDENSITY, 0, USE_GETOPT}, + { "status", MTIOCEXTGET, 0, USE_GETOPT }, + { "locate", MTIOCEXTLOCATE, 0, USE_GETOPT }, + { "param", MTIOCPARAMGET, 0, USE_GETOPT }, + { "protect", MT_CMD_PROTECT, 0, USE_GETOPT }, { NULL, 0, 0, 0 } }; + static const char *getblksiz(int); static void printreg(const char *, u_int, const char *); static void status(struct mtget *); static void usage(void); -static void st_status(struct mtget *); -static int stringtodens(const char *s); -static const char *denstostring(int d); -static int denstobp(int d, int bpi); +const char *get_driver_state_str(int dsreg); +static void st_status (struct mtget *); +static int mt_locate(int argc, char **argv, int mtfd, const char *tape); +static int nstatus_print(int argc, char **argv, char *xml_str, + struct mt_status_data *status_data); +static int mt_xml_cmd(unsigned long cmd, int argc, char **argv, int mtfd, + const char *tape); +static int mt_print_density_entry(struct mt_status_entry *density_root, int indent); +static int mt_print_density_report(struct mt_status_entry *report_root, int indent); +static int mt_print_density(struct mt_status_entry *density_root, int indent); +static int mt_getdensity(int argc, char **argv, char *xml_str, + struct mt_status_data *status_data); +static int mt_set_param(int mtfd, struct mt_status_data *status_data, + char *param_name, char *param_value); +static int mt_protect(int argc, char **argv, int mtfd, + struct mt_status_data *status_data); +static int mt_param(int argc, char **argv, int mtfd, char *xml_str, + struct mt_status_data *status_data); +static const char *denstostring (int d); static u_int32_t stringtocomp(const char *s); static const char *comptostring(u_int32_t comp); static void warn_eof(void); @@ -135,6 +213,8 @@ main(int argc, char *argv[]) int ch, len, mtfd; const char *p, *tape; + bzero(&mt_com, sizeof(mt_com)); + if ((tape = getenv("TAPE")) == NULL) tape = DEFTAPE; @@ -145,13 +225,15 @@ main(int argc, char *argv[]) tape = optarg; break; case '?': - default: usage(); + break; + default: + break; } argc -= optind; argv += optind; - if (argc < 1 || argc > 2) + if (argc < 1) usage(); len = strlen(p = *argv++); @@ -166,6 +248,11 @@ main(int argc, char *argv[]) if(comp->c_flags & DISABLE_THIS) { warn_eof(); } + if (comp->c_flags & USE_GETOPT) { + argc--; + optind = 0; + } + if ((mtfd = open(tape, comp->c_ronly ? O_RDONLY : O_RDWR)) < 0) err(1, "%s", tape); if (comp->c_code != MTNOP) { @@ -174,7 +261,7 @@ main(int argc, char *argv[]) if (!isdigit(**argv) && (comp->c_flags & IS_DENSITY)) { const char *dcanon; - mt_com.mt_count = stringtodens(*argv); + mt_com.mt_count = mt_density_num(*argv); if (mt_com.mt_count == 0) errx(1, "%s: unknown density", *argv); dcanon = denstostring(mt_com.mt_count); @@ -191,16 +278,17 @@ main(int argc, char *argv[]) errx(1, "%s: unknown compression", *argv); p = ""; - } else { + } else if ((comp->c_flags & USE_GETOPT) == 0) { char *q; /* allow for hex numbers; useful for density */ mt_com.mt_count = strtol(*argv, &q, 0); p = q; } - if ((mt_com.mt_count <= - ((comp->c_flags & ZERO_ALLOWED)? -1: 0) - && ((comp->c_flags & IS_COMP) == 0) - ) || *p) + if (((comp->c_flags & USE_GETOPT) == 0) + && (((mt_com.mt_count <= + ((comp->c_flags & ZERO_ALLOWED)? -1: 0)) + && ((comp->c_flags & IS_COMP) == 0)) + || *p)) errx(1, "%s: illegal count", *argv); } else @@ -289,6 +377,47 @@ main(int argc, char *argv[]) exit(0); /* NOTREACHED */ } + case MTIOCRBLIM: + { + struct mtrblim rblim; + + bzero(&rblim, sizeof(rblim)); + + if (ioctl(mtfd, MTIOCRBLIM, (caddr_t)&rblim) < 0) + err(2, "%s", tape); + (void)printf("%s:\n" + " min blocksize %u byte%s\n" + " max blocksize %u byte%s\n" + " granularity %u byte%s\n", + tape, rblim.min_block_length, + MT_PLURAL(rblim.min_block_length), + rblim.max_block_length, + MT_PLURAL(rblim.max_block_length), + (1 << rblim.granularity), + MT_PLURAL((1 << rblim.granularity))); + exit(0); + /* NOTREACHED */ + } + case MTIOCPARAMGET: + case MTIOCEXTGET: + case MT_CMD_PROTECT: + case MT_CMD_GETDENSITY: + { + int retval = 0; + + retval = mt_xml_cmd(comp->c_code, argc, argv, mtfd, + tape); + + exit(retval); + } + case MTIOCEXTLOCATE: + { + int retval = 0; + + retval = mt_locate(argc, argv, mtfd, tape); + + exit(retval); + } default: break; } @@ -381,62 +510,6 @@ usage(void) exit(1); } -static const struct densities { - int dens; - int bpmm; - int bpi; - const char *name; -} dens[] = { - /* - * Taken from T10 Project 997D - * SCSI-3 Stream Device Commands (SSC) - * Revision 11, 4-Nov-97 - */ - /*Num. bpmm bpi Reference */ - { 0x1, 32, 800, "X3.22-1983" }, - { 0x2, 63, 1600, "X3.39-1986" }, - { 0x3, 246, 6250, "X3.54-1986" }, - { 0x5, 315, 8000, "X3.136-1986" }, - { 0x6, 126, 3200, "X3.157-1987" }, - { 0x7, 252, 6400, "X3.116-1986" }, - { 0x8, 315, 8000, "X3.158-1987" }, - { 0x9, 491, 37871, "X3.180" }, - { 0xA, 262, 6667, "X3B5/86-199" }, - { 0xB, 63, 1600, "X3.56-1986" }, - { 0xC, 500, 12690, "HI-TC1" }, - { 0xD, 999, 25380, "HI-TC2" }, - { 0xF, 394, 10000, "QIC-120" }, - { 0x10, 394, 10000, "QIC-150" }, - { 0x11, 630, 16000, "QIC-320" }, - { 0x12, 2034, 51667, "QIC-1350" }, - { 0x13, 2400, 61000, "X3B5/88-185A" }, - { 0x14, 1703, 43245, "X3.202-1991" }, - { 0x15, 1789, 45434, "ECMA TC17" }, - { 0x16, 394, 10000, "X3.193-1990" }, - { 0x17, 1673, 42500, "X3B5/91-174" }, - { 0x18, 1673, 42500, "X3B5/92-50" }, - { 0x19, 2460, 62500, "DLTapeIII" }, - { 0x1A, 3214, 81633, "DLTapeIV(20GB)" }, - { 0x1B, 3383, 85937, "DLTapeIV(35GB)" }, - { 0x1C, 1654, 42000, "QIC-385M" }, - { 0x1D, 1512, 38400, "QIC-410M" }, - { 0x1E, 1385, 36000, "QIC-1000C" }, - { 0x1F, 2666, 67733, "QIC-2100C" }, - { 0x20, 2666, 67733, "QIC-6GB(M)" }, - { 0x21, 2666, 67733, "QIC-20GB(C)" }, - { 0x22, 1600, 40640, "QIC-2GB(C)" }, - { 0x23, 2666, 67733, "QIC-875M" }, - { 0x24, 2400, 61000, "DDS-2" }, - { 0x25, 3816, 97000, "DDS-3" }, - { 0x26, 3816, 97000, "DDS-4" }, - { 0x27, 3056, 77611, "Mammoth" }, - { 0x28, 1491, 37871, "X3.224" }, - { 0x41, 3868, 98250, "DLTapeIV(40GB)" }, - { 0x48, 5236, 133000, "SDLTapeI(110)" }, - { 0x49, 7598, 193000, "SDLTapeI(160)" }, - { 0, 0, 0, NULL } -}; - static const struct compression_types { u_int32_t comp_number; const char *name; @@ -454,58 +527,15 @@ static const char * denstostring(int d) { static char buf[20]; - const struct densities *sd; + const char *name = mt_density_name(d); - /* densities 0 and 0x7f are handled as special cases */ - if (d == 0) - return "default"; - if (d == 0x7f) - return "same"; - for (sd = dens; sd->dens; sd++) - if (sd->dens == d) - break; - if (sd->dens == 0) + if (name == NULL) sprintf(buf, "0x%02x", d); else - sprintf(buf, "0x%02x:%s", d, sd->name); + sprintf(buf, "0x%02x:%s", d, name); return buf; } -/* - * Given a specific density number, return either the bits per inch or bits - * per millimeter for the given density. - */ -static int -denstobp(int d, int bpi) -{ - const struct densities *sd; - - for (sd = dens; sd->dens; sd++) - if (sd->dens == d) - break; - if (sd->dens == 0) - return(0); - else { - if (bpi) - return(sd->bpi); - else - return(sd->bpmm); - } -} - -static int -stringtodens(const char *s) -{ - const struct densities *sd; - size_t l = strlen(s); - - for (sd = dens; sd->dens; sd++) - if (strncasecmp(sd->name, s, l) == 0) - break; - return sd->dens; -} - - static const char * getblksiz(int bs) { @@ -553,6 +583,38 @@ stringtocomp(const char *s) return(ct->comp_number); } +static struct driver_state { + int dsreg; + const char *desc; +} driver_states[] = { + { MTIO_DSREG_REST, "at rest" }, + { MTIO_DSREG_RBSY, "Communicating with drive" }, + { MTIO_DSREG_WR, "Writing" }, + { MTIO_DSREG_FMK, "Writing Filemarks" }, + { MTIO_DSREG_ZER, "Erasing" }, + { MTIO_DSREG_RD, "Reading" }, + { MTIO_DSREG_FWD, "Spacing Forward" }, + { MTIO_DSREG_REV, "Spacing Reverse" }, + { MTIO_DSREG_POS, "Hardware Positioning (direction unknown)" }, + { MTIO_DSREG_REW, "Rewinding" }, + { MTIO_DSREG_TEN, "Retensioning" }, + { MTIO_DSREG_UNL, "Unloading" }, + { MTIO_DSREG_LD, "Loading" }, +}; + +const char * +get_driver_state_str(int dsreg) +{ + unsigned int i; + + for (i = 0; i < (sizeof(driver_states)/sizeof(driver_states[0])); i++) { + if (driver_states[i].dsreg == dsreg) + return (driver_states[i].desc); + } + + return (NULL); +} + static void st_status(struct mtget *bp) { @@ -565,73 +627,963 @@ st_status(struct mtget *bp) "2: %-17s %-12s %-7d %s\n" "3: %-17s %-12s %-7d %s\n", denstostring(bp->mt_density), getblksiz(bp->mt_blksiz), - denstobp(bp->mt_density, TRUE), comptostring(bp->mt_comp), + mt_density_bp(bp->mt_density, TRUE), comptostring(bp->mt_comp), denstostring(bp->mt_density0), getblksiz(bp->mt_blksiz0), - denstobp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0), + mt_density_bp(bp->mt_density0, TRUE), comptostring(bp->mt_comp0), denstostring(bp->mt_density1), getblksiz(bp->mt_blksiz1), - denstobp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1), + mt_density_bp(bp->mt_density1, TRUE), comptostring(bp->mt_comp1), denstostring(bp->mt_density2), getblksiz(bp->mt_blksiz2), - denstobp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2), + mt_density_bp(bp->mt_density2, TRUE), comptostring(bp->mt_comp2), denstostring(bp->mt_density3), getblksiz(bp->mt_blksiz3), - denstobp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3)); + mt_density_bp(bp->mt_density3, TRUE), comptostring(bp->mt_comp3)); if (bp->mt_dsreg != MTIO_DSREG_NIL) { - auto char foo[32]; const char sfmt[] = "Current Driver State: %s.\n"; printf("---------------------------------\n"); - switch (bp->mt_dsreg) { - case MTIO_DSREG_REST: - printf(sfmt, "at rest"); + const char *state_str; + + state_str = get_driver_state_str(bp->mt_dsreg); + if (state_str == NULL) { + char foo[32]; + (void) sprintf(foo, "Unknown state 0x%x", bp->mt_dsreg); + printf(sfmt, foo); + } else { + printf(sfmt, state_str); + } + } + if (bp->mt_resid == 0 && bp->mt_fileno == (daddr_t) -1 && + bp->mt_blkno == (daddr_t) -1) + return; + printf("---------------------------------\n"); + printf("File Number: %d\tRecord Number: %d\tResidual Count %d\n", + bp->mt_fileno, bp->mt_blkno, bp->mt_resid); +} + +static int +mt_locate(int argc, char **argv, int mtfd, const char *tape) +{ + struct mtlocate mtl; + uint64_t logical_id = 0; + mt_locate_dest_type dest_type = MT_LOCATE_DEST_FILE; + int eod = 0, explicit = 0, immediate = 0; + int64_t partition = 0; + int block_addr_set = 0, partition_set = 0, file_set = 0, set_set = 0; + int c, retval; + + retval = 0; + bzero(&mtl, sizeof(mtl)); + + while ((c = getopt(argc, argv, "b:eEf:ip:s:")) != -1) { + switch (c) { + case 'b': + /* Block address */ + logical_id = strtoull(optarg, NULL, 0); + dest_type = MT_LOCATE_DEST_OBJECT; + block_addr_set = 1; break; - case MTIO_DSREG_RBSY: - printf(sfmt, "Communicating with drive"); + case 'e': + /* end of data */ + eod = 1; + dest_type = MT_LOCATE_DEST_EOD; break; - case MTIO_DSREG_WR: - printf(sfmt, "Writing"); + case 'E': + /* + * XXX KDM explicit address mode. Should we even + * allow this, since the driver doesn't operate in + * explicit address mode? + */ + explicit = 1; break; - case MTIO_DSREG_FMK: - printf(sfmt, "Writing Filemarks"); + case 'f': + /* file number */ + logical_id = strtoull(optarg, NULL, 0); + dest_type = MT_LOCATE_DEST_FILE; + file_set = 1; break; - case MTIO_DSREG_ZER: - printf(sfmt, "Erasing"); + case 'i': + /* + * Immediate address mode. XXX KDM do we want to + * implement this? The other commands in the + * tape driver will need to be able to handle this. + */ + immediate = 1; break; - case MTIO_DSREG_RD: - printf(sfmt, "Reading"); + case 'p': + /* + * Change partition to the given partition. + */ + partition = strtol(optarg, NULL, 0); + partition_set = 1; break; - case MTIO_DSREG_FWD: - printf(sfmt, "Spacing Forward"); + case 's': + /* Go to the given set mark */ + logical_id = strtoull(optarg, NULL, 0); + dest_type = MT_LOCATE_DEST_SET; + set_set = 1; break; - case MTIO_DSREG_REV: - printf(sfmt, "Spacing Reverse"); + default: break; - case MTIO_DSREG_POS: - printf(sfmt, - "Hardware Positioning (direction unknown)"); + } + } + + /* + * These options are mutually exclusive. The user may only specify + * one. + */ + if ((block_addr_set + file_set + eod + set_set) != 1) + errx(1, "You must specify only one of -b, -f, -e, or -s"); + + mtl.dest_type = dest_type; + switch (dest_type) { + case MT_LOCATE_DEST_OBJECT: + case MT_LOCATE_DEST_FILE: + case MT_LOCATE_DEST_SET: + mtl.logical_id = logical_id; + break; + case MT_LOCATE_DEST_EOD: + break; + } + + if (immediate != 0) + mtl.flags |= MT_LOCATE_FLAG_IMMED; + + if (partition_set != 0) { + mtl.flags |= MT_LOCATE_FLAG_CHANGE_PART; + mtl.partition = partition; + } + + if (explicit != 0) + mtl.block_address_mode = MT_LOCATE_BAM_EXPLICIT; + else + mtl.block_address_mode = MT_LOCATE_BAM_IMPLICIT; + + if (ioctl(mtfd, MTIOCEXTLOCATE, &mtl) == -1) + err(1, "MTIOCEXTLOCATE ioctl failed on %s", tape); + + return (retval); +} + +typedef enum { + MT_PERIPH_NAME = 0, + MT_UNIT_NUMBER = 1, + MT_VENDOR = 2, + MT_PRODUCT = 3, + MT_REVISION = 4, + MT_COMPRESSION_SUPPORTED = 5, + MT_COMPRESSION_ENABLED = 6, + MT_COMPRESSION_ALGORITHM = 7, + MT_MEDIA_DENSITY = 8, + MT_MEDIA_BLOCKSIZE = 9, + MT_CALCULATED_FILENO = 10, + MT_CALCULATED_REL_BLKNO = 11, + MT_REPORTED_FILENO = 12, + MT_REPORTED_BLKNO = 13, + MT_PARTITION = 14, + MT_BOP = 15, + MT_EOP = 16, + MT_BPEW = 17, + MT_DSREG = 18, + MT_RESID = 19, + MT_FIXED_MODE = 20, + MT_SERIAL_NUM = 21, + MT_MAXIO = 22, + MT_CPI_MAXIO = 23, + MT_MAX_BLK = 24, + MT_MIN_BLK = 25, + MT_BLK_GRAN = 26, + MT_MAX_EFF_IOSIZE = 27 +} status_item_index; + +static struct mt_status_items { + const char *name; + struct mt_status_entry *entry; +} req_status_items[] = { + { "periph_name", NULL }, + { "unit_number", NULL }, + { "vendor", NULL }, + { "product", NULL }, + { "revision", NULL }, + { "compression_supported", NULL }, + { "compression_enabled", NULL }, + { "compression_algorithm", NULL }, + { "media_density", NULL }, + { "media_blocksize", NULL }, + { "calculated_fileno", NULL }, + { "calculated_rel_blkno", NULL }, + { "reported_fileno", NULL }, + { "reported_blkno", NULL }, + { "partition", NULL }, + { "bop", NULL }, + { "eop", NULL }, + { "bpew", NULL }, + { "dsreg", NULL }, + { "residual", NULL }, + { "fixed_mode", NULL }, + { "serial_num", NULL }, + { "maxio", NULL }, + { "cpi_maxio", NULL }, + { "max_blk", NULL }, + { "min_blk", NULL }, + { "blk_gran", NULL }, + { "max_effective_iosize", NULL } +}; + +int +nstatus_print(int argc, char **argv, char *xml_str, + struct mt_status_data *status_data) +{ + unsigned int i; + int64_t calculated_fileno, calculated_rel_blkno; + int64_t rep_fileno, rep_blkno, partition, resid; + char block_str[32]; + const char *dens_str; + int dsreg, bop, eop, bpew; + int xml_dump = 0; + size_t dens_len; + unsigned int field_width; + int verbose = 0; + int c; + + while ((c = getopt(argc, argv, "xv")) != -1) { + switch (c) { + case 'x': + xml_dump = 1; break; - case MTIO_DSREG_REW: - printf(sfmt, "Rewinding"); + case 'v': + verbose = 1; break; - case MTIO_DSREG_TEN: - printf(sfmt, "Retensioning"); + default: break; - case MTIO_DSREG_UNL: - printf(sfmt, "Unloading"); + } + } + + if (xml_dump != 0) { + printf("%s", xml_str); + return (0); + } + + for (i = 0; i < (sizeof(req_status_items)/sizeof(req_status_items[0])); + i++) { + char *name; + + name = __DECONST(char *, req_status_items[i].name); + req_status_items[i].entry = mt_status_entry_find(status_data, + name); + if (req_status_items[i].entry == NULL) { + errx(1, "Cannot find status entry %s", + req_status_items[i].name); + } + } + + printf("Drive: %s%ju: <%s %s %s> Serial Number: %s\n", + req_status_items[MT_PERIPH_NAME].entry->value, + (uintmax_t)req_status_items[MT_UNIT_NUMBER].entry->value_unsigned, + req_status_items[MT_VENDOR].entry->value, + req_status_items[MT_PRODUCT].entry->value, + req_status_items[MT_REVISION].entry->value, + (req_status_items[MT_SERIAL_NUM].entry->value) ? + req_status_items[MT_SERIAL_NUM].entry->value : "none"); + printf("---------------------------------\n"); + + /* + * We check to see whether we're in fixed mode or not, and don't + * just believe the blocksize. If the SILI bit is turned on, the + * blocksize will be set to 4, even though we're doing variable + * length (well, multiples of 4) blocks. + */ + if (req_status_items[MT_FIXED_MODE].entry->value_signed == 0) + snprintf(block_str, sizeof(block_str), "variable"); + else + snprintf(block_str, sizeof(block_str), "%s", + getblksiz(req_status_items[ + MT_MEDIA_BLOCKSIZE].entry->value_unsigned)); + + dens_str = denstostring(req_status_items[ + MT_MEDIA_DENSITY].entry->value_unsigned); + if (dens_str == NULL) + dens_len = 0; + else + dens_len = strlen(dens_str); + field_width = MAX(dens_len, 17); + printf("Mode %-*s Blocksize bpi Compression\n" + "Current: %-*s %-12s %-7d ", + field_width, "Density", field_width, dens_str, block_str, + mt_density_bp(req_status_items[ + MT_MEDIA_DENSITY].entry->value_unsigned, TRUE)); + + if (req_status_items[MT_COMPRESSION_SUPPORTED].entry->value_signed == 0) + printf("unsupported\n"); + else if (req_status_items[ + MT_COMPRESSION_ENABLED].entry->value_signed == 0) + printf("disabled\n"); + else { + printf("enabled (%s)\n", + comptostring(req_status_items[ + MT_COMPRESSION_ALGORITHM].entry->value_unsigned)); + } + + dsreg = req_status_items[MT_DSREG].entry->value_signed; + if (dsreg != MTIO_DSREG_NIL) { + const char sfmt[] = "Current Driver State: %s.\n"; + printf("---------------------------------\n"); + const char *state_str; + + state_str = get_driver_state_str(dsreg); + if (state_str == NULL) { + char foo[32]; + (void) sprintf(foo, "Unknown state 0x%x", dsreg); + printf(sfmt, foo); + } else { + printf(sfmt, state_str); + } + } + resid = req_status_items[MT_RESID].entry->value_signed; + calculated_fileno = req_status_items[ + MT_CALCULATED_FILENO].entry->value_signed; + calculated_rel_blkno = req_status_items[ + MT_CALCULATED_REL_BLKNO].entry->value_signed; + rep_fileno = req_status_items[ + MT_REPORTED_FILENO].entry->value_signed; + rep_blkno = req_status_items[ + MT_REPORTED_BLKNO].entry->value_signed; + bop = req_status_items[MT_BOP].entry->value_signed; + eop = req_status_items[MT_EOP].entry->value_signed; + bpew = req_status_items[MT_BPEW].entry->value_signed; + partition = req_status_items[MT_PARTITION].entry->value_signed; + + printf("---------------------------------\n"); + printf("Partition: %3jd Calc File Number: %3jd " + " Calc Record Number: %jd\n" + "Residual: %3jd Reported File Number: %3jd " + "Reported Record Number: %jd\n", partition, calculated_fileno, + calculated_rel_blkno, resid, rep_fileno, rep_blkno); + + printf("Flags: "); + if (bop > 0 || eop > 0 || bpew > 0) { + int need_comma = 0; + + if (bop > 0) { + printf("BOP"); + need_comma = 1; + } + if (eop > 0) { + if (need_comma != 0) + printf(","); + printf("EOP"); + need_comma = 1; + } + if (bpew > 0) { + if (need_comma != 0) + printf(","); + printf("BPEW"); + need_comma = 1; + } + } else { + printf("None"); + } + printf("\n"); + if (verbose != 0) { + printf("---------------------------------\n"); + printf("Tape I/O parameters:\n"); + for (i = MT_MAXIO; i <= MT_MAX_EFF_IOSIZE; i++) { + printf(" %s (%s): %ju bytes\n", + req_status_items[i].entry->desc, + req_status_items[i].name, + req_status_items[i].entry->value_unsigned); + } + } + + return (0); +} + +int +mt_xml_cmd(unsigned long cmd, int argc, char **argv, int mtfd, const char *tape) +{ + struct mt_status_data status_data; +#if 0 + struct mt_status_entry *entry; +#endif + char *xml_str; + int retval; + unsigned long ioctl_cmd; + + switch (cmd) { + case MT_CMD_PROTECT: + case MTIOCPARAMGET: + ioctl_cmd = MTIOCPARAMGET; + break; + default: + ioctl_cmd = MTIOCEXTGET; + break; + } + + retval = mt_get_xml_str(mtfd, ioctl_cmd, &xml_str); + if (retval != 0) + err(1, "Couldn't get mt XML string"); + + retval = mt_get_status(xml_str, &status_data); + if (retval != XML_STATUS_OK) { + warn("Couldn't get mt status for %s", tape); + goto bailout; + } + + /* + * This gets set if there are memory allocation or other errors in + * our parsing of the XML. + */ + if (status_data.error != 0) { + warnx("%s", status_data.error_str); + retval = 1; + goto bailout; + } +#if 0 + STAILQ_FOREACH(entry, &status_data.entries, links) + mt_status_tree_print(entry, 0, NULL); +#endif + + switch (cmd) { + case MTIOCEXTGET: + retval = nstatus_print(argc, argv, xml_str, &status_data); + break; + case MTIOCPARAMGET: + retval = mt_param(argc, argv, mtfd, xml_str, &status_data); + break; + case MT_CMD_PROTECT: + retval = mt_protect(argc, argv, mtfd, &status_data); + break; + case MT_CMD_GETDENSITY: + retval = mt_getdensity(argc, argv, xml_str, &status_data); + break; + } + +bailout: + if (xml_str != NULL) + free(xml_str); + + mt_status_free(&status_data); + + return (retval); +} + +static int +mt_set_param(int mtfd, struct mt_status_data *status_data, char *param_name, + char *param_value) +{ + struct mt_status_entry *entry; + struct mtparamset param_set; + + entry = mt_status_entry_find(status_data, + __DECONST(char *, "mtparamget")); + if (entry == NULL) + errx(1, "Cannot find parameter root node"); + + bzero(¶m_set, sizeof(param_set)); + entry = mt_entry_find(entry, param_name); + if (entry == NULL) + errx(1, "Unknown parameter name \"%s\"", param_name); + + strlcpy(param_set.value_name, param_name, sizeof(param_set.value_name)); + + switch (entry->var_type) { + case MT_TYPE_INT: + param_set.value.value_signed = strtoll(param_value, NULL, 0); + param_set.value_type = MT_PARAM_SET_SIGNED; + param_set.value_len = entry->size; + break; + case MT_TYPE_UINT: + param_set.value.value_unsigned = strtoull(param_value, NULL, 0); + param_set.value_type = MT_PARAM_SET_UNSIGNED; + param_set.value_len = entry->size; + break; + case MT_TYPE_STRING: { + size_t param_len; + + param_len = strlen(param_value) + 1; + if (param_len > sizeof(param_set.value.value_fixed_str)) { + param_set.value_type = MT_PARAM_SET_VAR_STR; + param_set.value.value_var_str = param_value; + } else { + param_set.value_type = MT_PARAM_SET_FIXED_STR; + strlcpy(param_set.value.value_fixed_str, param_value, + sizeof(param_set.value.value_fixed_str)); + } + param_set.value_len = param_len; + break; + } + default: + errx(1, "Unknown parameter type %d for %s", entry->var_type, + param_name); + break; + } + + if (ioctl(mtfd, MTIOCPARAMSET, ¶m_set) == -1) + err(1, "MTIOCPARAMSET"); + + if (param_set.status != MT_PARAM_STATUS_OK) + errx(1, "Failed to set %s: %s", param_name, + param_set.error_str); + + return (0); +} + + +typedef enum { + MT_PP_LBP_R, + MT_PP_LBP_W, + MT_PP_RBDP, + MT_PP_PI_LENGTH, + MT_PP_PROT_METHOD +} mt_protect_param; + +static struct mt_protect_info { + const char *name; + struct mt_status_entry *entry; + uint32_t value; +} mt_protect_list[] = { + { "lbp_r", NULL, 0 }, + { "lbp_w", NULL, 0 }, + { "rbdp", NULL, 0 }, + { "pi_length", NULL, 0 }, + { "prot_method", NULL, 0 } +}; + +#define MT_NUM_PROTECT_PARAMS (sizeof(mt_protect_list)/sizeof(mt_protect_list[0])) + +#define MT_PROT_NAME "protection" + +static int +mt_protect(int argc, char **argv, int mtfd, struct mt_status_data *status_data) +{ + int retval = 0; + int do_enable = 0, do_disable = 0, do_list = 0; + int rbdp_set = 0, lbp_w_set = 0, lbp_r_set = 0; + int prot_method_set = 0, pi_length_set = 0; + int verbose = 0; + uint32_t rbdp = 0, lbp_w = 0, lbp_r = 0; + uint32_t prot_method = 0, pi_length = 0; + struct mt_status_entry *prot_entry, *supported_entry; + struct mt_status_entry *entry; + struct mtparamset params[MT_NUM_PROTECT_PARAMS]; + struct mtsetlist param_list; + unsigned int i; + int c; + + while ((c = getopt(argc, argv, "b:delL:m:r:vw:")) != -1) { + switch (c) { + case 'b': + rbdp_set = 1; + rbdp = strtoul(optarg, NULL, 0); + if ((rbdp != 0) && (rbdp != 1)) + errx(1, "valid values for -b are 0 and 1"); break; - case MTIO_DSREG_LD: - printf(sfmt, "Loading"); + case 'd': + do_disable = 1; + break; + case 'e': + do_enable = 1; + break; + case 'l': + do_list = 1; + break; + case 'L': + pi_length_set = 1; + pi_length = strtoul(optarg, NULL, 0); + if (pi_length > SA_CTRL_DP_PI_LENGTH_MASK) + errx(1, "PI length %u > maximum %u", + pi_length, SA_CTRL_DP_PI_LENGTH_MASK); + break; + case 'm': + prot_method_set = 1; + prot_method = strtoul(optarg, NULL, 0); + if (prot_method > SA_CTRL_DP_METHOD_MAX) + errx(1, "Method %u > maximum %u", + prot_method, SA_CTRL_DP_METHOD_MAX); + break; + case 'r': + lbp_r_set = 1; + lbp_r = strtoul(optarg, NULL, 0); + if ((lbp_r != 0) && (lbp_r != 1)) + errx(1, "valid values for -r are 0 and 1"); + break; + case 'v': + verbose = 1; + break; + case 'w': + lbp_w_set = 1; + lbp_w = strtoul(optarg, NULL, 0); + if ((lbp_w != 0) && (lbp_r != 1)) + errx(1, "valid values for -r are 0 and 1"); break; default: - (void) sprintf(foo, "Unknown state 0x%x", bp->mt_dsreg); - printf(sfmt, foo); break; } } - if (bp->mt_resid == 0 && bp->mt_fileno == (daddr_t) -1 && - bp->mt_blkno == (daddr_t) -1) - return; - printf("---------------------------------\n"); - printf("File Number: %d\tRecord Number: %d\tResidual Count %d\n", - bp->mt_fileno, bp->mt_blkno, bp->mt_resid); + + if ((rbdp_set + do_disable + do_enable + do_list + pi_length_set + + prot_method_set + lbp_r_set + lbp_w_set) == 0) + errx(1, "Need an argument for protect"); + + if ((do_disable + do_enable + do_list) != 1) + errx(1, "You must specify only one of -e, -d or -l"); + + if (do_list != 0) { + retval = mt_protect_print(status_data, verbose); + goto bailout; + } + if (do_enable != 0) { + /* + * Enable protection, but allow the user to override + * settings if he doesn't want everything turned on. + */ + if (rbdp_set == 0) + rbdp = 1; + if (lbp_w_set == 0) + lbp_w = 1; + if (lbp_r_set == 0) + lbp_r = 1; + /* + * If the user doesn't override it, we default to enabling + * Reed-Solomon checkums. + */ + if (prot_method_set == 0) + prot_method = SA_CTRL_DP_REED_SOLOMON; + if (pi_length_set == 0) + pi_length = SA_CTRL_DP_RS_LENGTH; + } else if (do_disable != 0) { + /* + * If the user wants to disable protection, we ignore any + * other parameters he has set. Everything gets set to 0. + */ + rbdp = lbp_w = lbp_r = 0; + prot_method = pi_length = 0; + } + + prot_entry = mt_status_entry_find(status_data, + __DECONST(char *, MT_PROT_NAME)); + if (prot_entry == NULL) + errx(1, "Unable to find protection information status"); + + supported_entry = mt_entry_find(prot_entry, + __DECONST(char *, "protection_supported")); + if (supported_entry == NULL) + errx(1, "Unable to find protection support information"); + + if (((supported_entry->var_type == MT_TYPE_INT) + && (supported_entry->value_signed == 0)) + || ((supported_entry->var_type == MT_TYPE_UINT) + && (supported_entry->value_unsigned == 0))) + errx(1, "This device does not support protection information"); + + mt_protect_list[MT_PP_LBP_R].value = lbp_r; + mt_protect_list[MT_PP_LBP_W].value = lbp_w; + mt_protect_list[MT_PP_RBDP].value = rbdp; + mt_protect_list[MT_PP_PI_LENGTH].value = pi_length; + mt_protect_list[MT_PP_PROT_METHOD].value = prot_method; + + bzero(¶ms, sizeof(params)); + bzero(¶m_list, sizeof(param_list)); + + /* + * Go through the list and make sure that we have this parameter, + * and that it is still an unsigned integer. If not, we've got a + * problem. + */ + for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) { + entry = mt_entry_find(prot_entry, + __DECONST(char *, mt_protect_list[i].name)); + if (entry == NULL) { + errx(1, "Unable to find parameter %s", + mt_protect_list[i].name); + } + mt_protect_list[i].entry = entry; + + if (entry->var_type != MT_TYPE_UINT) + errx(1, "Parameter %s is type %d, not unsigned, " + "cannot proceed", mt_protect_list[i].name, + entry->var_type); + snprintf(params[i].value_name, sizeof(params[i].value_name), + "%s.%s", MT_PROT_NAME, mt_protect_list[i].name); + /* XXX KDM unify types here */ + params[i].value_type = MT_PARAM_SET_UNSIGNED; + params[i].value_len = sizeof(mt_protect_list[i].value); + params[i].value.value_unsigned = mt_protect_list[i].value; + + } + param_list.num_params = MT_NUM_PROTECT_PARAMS; + param_list.param_len = sizeof(params); + param_list.params = params; + + if (ioctl(mtfd, MTIOCSETLIST, ¶m_list) == -1) + err(1, "error issuing MTIOCSETLIST ioctl"); + + for (i = 0; i < MT_NUM_PROTECT_PARAMS; i++) { + if (params[i].status != MT_PARAM_STATUS_OK) { + warnx("%s", params[i].error_str); + retval = 1; + } + } +bailout: + + return (retval); +} + +static int +mt_param(int argc, char **argv, int mtfd, char *xml_str, + struct mt_status_data *status_data) +{ + int list = 0, do_set = 0, xml_dump = 0; + char *param_name = NULL, *param_value = NULL; + int retval = 0, quiet = 0; + int c; + + while ((c = getopt(argc, argv, "lp:qs:x")) != -1) { + switch (c) { + case 'l': + list = 1; + break; + case 'p': + if (param_name != NULL) { + warnx("Only one paramter name may be " + "specified"); + retval = 1; + goto bailout; + } + param_name = strdup(optarg); + break; + case 'q': + quiet = 1; + break; + case 's': + if (param_value != NULL) { + warnx("Only one paramter value may be " + "specified"); + retval = 1; + goto bailout; + } + param_value = strdup(optarg); + do_set = 1; + break; + case 'x': + xml_dump = 1; + break; + default: + break; + } + } + + if ((list + do_set + xml_dump) != 1) { + warnx("You must specify only one of -s, -l or -x"); + retval = 1; + goto bailout; + } + + if (xml_dump != 0) { + printf("%s", xml_str); + retval = 0; + goto bailout; + } + + if (do_set != 0) { + if (param_name == NULL) + errx(1, "You must specify -p with -s"); + + retval = mt_set_param(mtfd, status_data, param_name, + param_value); + } else if (list != 0) + retval = mt_param_list(status_data, param_name, quiet); + +bailout: + free(param_name); + free(param_value); + return (retval); +} + +int +mt_print_density_entry(struct mt_status_entry *density_root, int indent) +{ + struct mt_status_entry *entry; + int retval = 0; + + STAILQ_FOREACH(entry, &density_root->child_entries, links) { + if (entry->var_type == MT_TYPE_NODE) { + retval = mt_print_density_entry(entry, indent + 2); + if (retval != 0) + break; + else + continue; + } + if ((strcmp(entry->entry_name, "primary_density_code") == 0) + || (strcmp(entry->entry_name, "secondary_density_code") == 0) + || (strcmp(entry->entry_name, "density_code") == 0)) { + + printf("%*s%s (%s): %s\n", indent, "", entry->desc ? + entry->desc : "", entry->entry_name, + denstostring(entry->value_unsigned)); + } else if (strcmp(entry->entry_name, "density_flags") == 0) { + printf("%*sMedium Access: ", indent, ""); + if (entry->value_unsigned & MT_DENS_WRITE_OK) { + printf("Read and Write\n"); + } else { + printf("Read Only\n"); + } + printf("%*sDefault Density: %s\n", indent, "", + (entry->value_unsigned & MT_DENS_DEFLT) ? "Yes" : + "No"); + printf("%*sDuplicate Density: %s\n", indent, "", + (entry->value_unsigned & MT_DENS_DUP) ? "Yes" : + "No"); + } else if (strcmp(entry->entry_name, "media_width") == 0) { + printf("%*s%s (%s): %.1f mm\n", indent, "", + entry->desc ? entry->desc : "", entry->entry_name, + (double)((double)entry->value_unsigned / 10)); + } else if (strcmp(entry->entry_name, "medium_length") == 0) { + printf("%*s%s (%s): %ju m\n", indent, "", + entry->desc ? entry->desc : "", entry->entry_name, + (uintmax_t)entry->value_unsigned); + } else if (strcmp(entry->entry_name, "capacity") == 0) { + printf("%*s%s (%s): %ju MB\n", indent, "", entry->desc ? + entry->desc : "", entry->entry_name, + (uintmax_t)entry->value_unsigned); + } else { + printf("%*s%s (%s): %s\n", indent, "", entry->desc ? + entry->desc : "", entry->entry_name, entry->value); + } + } + + return (retval); +} + +int +mt_print_density_report(struct mt_status_entry *report_root, int indent) +{ + struct mt_status_entry *mt_report, *media_report; + struct mt_status_entry *entry; + int retval = 0; + + mt_report = mt_entry_find(report_root, + __DECONST(char *, MT_MEDIUM_TYPE_REPORT_NAME)); + if (mt_report == NULL) + return (1); + + media_report = mt_entry_find(report_root, + __DECONST(char *, MT_MEDIA_REPORT_NAME)); + if (media_report == NULL) + return (1); + + if ((mt_report->value_signed == 0) + && (media_report->value_signed == 0)) { + printf("%*sThis tape drive supports the following " + "media densities:\n", indent, ""); + } else if ((mt_report->value_signed == 0) + && (media_report->value_signed != 0)) { + printf("%*sThe tape currently in this drive supports " + "the following media densities:\n", indent, ""); + } else if ((mt_report->value_signed != 0) + && (media_report->value_signed == 0)) { + printf("%*sThis tape drive supports the following " + "media types:\n", indent, ""); + } else { + printf("%*sThis tape currently in this drive supports " + "the following media types:\n", indent, ""); + } + + STAILQ_FOREACH(entry, &report_root->child_entries, links) { + struct mt_status_nv *nv; + + if (strcmp(entry->entry_name, MT_DENSITY_ENTRY_NAME) != 0) + continue; + + STAILQ_FOREACH(nv, &entry->nv_list, links) { + if (strcmp(nv->name, "num") != 0) + continue; + + break; + } + + indent += 2; + + printf("%*sDensity Entry", indent, ""); + if (nv != NULL) + printf(" %s", nv->value); + printf(":\n"); + + retval = mt_print_density_entry(entry, indent + 2); + + indent -= 2; + } + + return (retval); +} + +int +mt_print_density(struct mt_status_entry *density_root, int indent) +{ + struct mt_status_entry *entry; + int retval = 0; + + /* + * We should have this entry for every tape drive. This particular + * value is reported via the mode page block header, not the + * SCSI REPORT DENSITY SUPPORT command. + */ + entry = mt_entry_find(density_root, + __DECONST(char *, MT_MEDIA_DENSITY_NAME)); + if (entry == NULL) + errx(1, "Unable to find node %s", MT_MEDIA_DENSITY_NAME); + + printf("%*sCurrent density: %s\n", indent, "", + denstostring(entry->value_unsigned)); + + /* + * It isn't an error if we don't have any density reports. Tape + * drives that don't support the REPORT DENSITY SUPPORT command + * won't have any; they will only have the current density entry + * above. + */ + STAILQ_FOREACH(entry, &density_root->child_entries, links) { + if (strcmp(entry->entry_name, MT_DENSITY_REPORT_NAME) != 0) + continue; + + retval = mt_print_density_report(entry, indent); + } + + return (retval); +} + +int +mt_getdensity(int argc, char **argv, char *xml_str, + struct mt_status_data *status_data) +{ + int retval = 0; + int verbose = 0, xml_dump = 0; + struct mt_status_entry *density_root = NULL; + int c; + + while ((c = getopt(argc, argv, "vx")) != -1) { + switch (c) { + case 'v': + verbose = 1; + break; + case 'x': + xml_dump = 1; + break; + } + } + + if (xml_dump != 0) { + printf("%s", xml_str); + return (0); + } + + density_root = mt_status_entry_find(status_data, + __DECONST(char *, MT_DENSITY_ROOT_NAME)); + if (density_root == NULL) + errx(1, "Cannot find density root node %s", + MT_DENSITY_ROOT_NAME); + + retval = mt_print_density(density_root, 0); + + return (retval); } static void -- 2.42.0