diff options
author | ken <ken@FreeBSD.org> | 2015-06-16 02:31:11 +0000 |
---|---|---|
committer | ken <ken@FreeBSD.org> | 2015-06-16 02:31:11 +0000 |
commit | 1d2632e58b4f9b2c6256c2da8cefdd3574d81cde (patch) | |
tree | baa2cc1d59134334453890933ea6c280e7f34eef | |
parent | 9e32ce59f0b80ab53877e923cc05f059e9075435 (diff) | |
download | FreeBSD-src-1d2632e58b4f9b2c6256c2da8cefdd3574d81cde.zip FreeBSD-src-1d2632e58b4f9b2c6256c2da8cefdd3574d81cde.tar.gz |
MFC, r284192:
------------------------------------------------------------------------
r284192 | ken | 2015-06-09 15:39:38 -0600 (Tue, 09 Jun 2015) | 102 lines
Add support for reading MAM attributes to camcontrol(8) and libcam(3).
MAM is Medium Auxiliary Memory and is most commonly found as flash
chips on tapes.
This includes support for reading attributes and decoding most
known attributes, but does not yet include support for writing
attributes or reporting attributes in XML format.
libsbuf/Makefile:
Add subr_prf.c for the new sbuf_hexdump() function. This
function is essentially the same function.
libsbuf/Symbol.map:
Add a new shared library minor version, and include the
sbuf_hexdump() function.
libsbuf/Version.def:
Add version 1.4 of the libsbuf library.
libutil/hexdump.3:
Document sbuf_hexdump() alongside hexdump(3), since it is
essentially the same function.
camcontrol/Makefile:
Add attrib.c.
camcontrol/attrib.c:
Implementation of READ ATTRIBUTE support for camcontrol(8).
camcontrol/camcontrol.8:
Document the new 'camcontrol attrib' subcommand.
camcontrol/camcontrol.c:
Add the new 'camcontrol attrib' subcommand.
camcontrol/camcontrol.h:
Add a function prototype for scsiattrib().
share/man/man9/sbuf.9:
Document the existence of sbuf_hexdump() and point users to
the hexdump(3) man page for more details.
sys/cam/scsi/scsi_all.c:
Add a table of known attributes, text descriptions and
handler functions.
Add a new scsi_attrib_sbuf() function along with a number
of other related functions that help decode attributes.
scsi_attrib_ascii_sbuf() decodes ASCII format attributes.
scsi_attrib_int_sbuf() decodes binary format attributes, and
will pass them off to scsi_attrib_hexdump_sbuf() if they're
bigger than 8 bytes.
scsi_attrib_vendser_sbuf() decodes the vendor and drive
serial number attribute.
scsi_attrib_volcoh_sbuf() decodes the Volume Coherency
Information attribute that LTFS writes out.
sys/cam/scsi/scsi_all.h:
Add a number of attribute-related structure definitions and
other defines.
Add function prototypes for all of the functions added in
scsi_all.c.
sys/kern/subr_prf.c:
Add a new function, sbuf_hexdump(). This is the same as
the existing hexdump(9) function, except that it puts the
result in an sbuf.
This also changes subr_prf.c so that it can be compiled in
userland for includsion in libsbuf.
We should work to change this so that the kernel hexdump
implementation is a wrapper around sbuf_hexdump() with a
statically allocated sbuf with a drain. That will require
a drain function that goes to the kernel printf() buffer
that can take a non-NUL terminated string as input.
That is because an sbuf isn't NUL-terminated until it is
finished, and we don't want to finish it while we're still
using it.
We should also work to consolidate the userland hexdump and
kernel hexdump implemenatations, which are currently
separate. This would also mean making applications that
currently link in libutil link in libsbuf.
sys/sys/sbuf.h:
Add the prototype for sbuf_hexdump(), and add another copy
of the hexdump flag values if they aren't already defined.
Ideally the flags should be defined in one place but the
implemenation makes it difficult to do properly. (See
above.)
Sponsored by: Spectra Logic Corporation
------------------------------------------------------------------------
-rw-r--r-- | lib/libsbuf/Makefile | 2 | ||||
-rw-r--r-- | lib/libsbuf/Symbol.map | 4 | ||||
-rw-r--r-- | lib/libsbuf/Version.def | 3 | ||||
-rw-r--r-- | lib/libutil/hexdump.3 | 19 | ||||
-rw-r--r-- | sbin/camcontrol/Makefile | 2 | ||||
-rw-r--r-- | sbin/camcontrol/attrib.c | 509 | ||||
-rw-r--r-- | sbin/camcontrol/camcontrol.8 | 145 | ||||
-rw-r--r-- | sbin/camcontrol/camcontrol.c | 27 | ||||
-rw-r--r-- | sbin/camcontrol/camcontrol.h | 3 | ||||
-rw-r--r-- | share/man/man9/sbuf.9 | 20 | ||||
-rw-r--r-- | sys/cam/cam.c | 50 | ||||
-rw-r--r-- | sys/cam/cam.h | 12 | ||||
-rw-r--r-- | sys/cam/scsi/scsi_all.c | 892 | ||||
-rw-r--r-- | sys/cam/scsi/scsi_all.h | 283 | ||||
-rw-r--r-- | sys/kern/subr_prf.c | 63 | ||||
-rw-r--r-- | sys/sys/sbuf.h | 10 |
16 files changed, 2036 insertions, 8 deletions
diff --git a/lib/libsbuf/Makefile b/lib/libsbuf/Makefile index 79d3fe9..98ceeb6 100644 --- a/lib/libsbuf/Makefile +++ b/lib/libsbuf/Makefile @@ -2,7 +2,7 @@ LIB= sbuf SHLIBDIR?= /lib -SRCS= subr_sbuf.c +SRCS= subr_prf.c subr_sbuf.c SHLIB_MAJOR = 6 diff --git a/lib/libsbuf/Symbol.map b/lib/libsbuf/Symbol.map index cf2508f..164b8f3 100644 --- a/lib/libsbuf/Symbol.map +++ b/lib/libsbuf/Symbol.map @@ -27,3 +27,7 @@ FBSD_1.3 { sbuf_start_section; sbuf_end_section; }; + +FBSD_1.4 { + sbuf_hexdump; +}; diff --git a/lib/libsbuf/Version.def b/lib/libsbuf/Version.def index fb0b3f6..fd5c6be 100644 --- a/lib/libsbuf/Version.def +++ b/lib/libsbuf/Version.def @@ -5,3 +5,6 @@ FBSD_1.2 { FBSD_1.3 { } FBSD_1.2; + +FBSD_1.4 { +} FBSD_1.3; diff --git a/lib/libutil/hexdump.3 b/lib/libutil/hexdump.3 index 8e997ac..9fa96f8 100644 --- a/lib/libutil/hexdump.3 +++ b/lib/libutil/hexdump.3 @@ -28,7 +28,7 @@ .\" .\" $FreeBSD$ .\" -.Dd July 1, 2008 +.Dd May 8, 2014 .Dt HEXDUMP 3 .Os .Sh NAME @@ -36,8 +36,17 @@ .Nd "dump a block of bytes to standard out in hexadecimal form" .Sh SYNOPSIS .In libutil.h +.In sys/sbuf.h .Ft void .Fn hexdump "void *ptr" "int length" "const char *hdr" "int flags" +.Ft void +.Fo sbuf_hexdump +.Fa "struct sbuf *sb" +.Fa "void *ptr" +.Fa "int length" +.Fa "const char *hdr" +.Fa "int flags" +.Fc .Sh DESCRIPTION The .Fn hexdump @@ -50,6 +59,11 @@ output will start with an offset count, followed by 16 hexadecimal values, followed by 16 .Tn ASCII characters. +.Pp +The +.Fn sbuf_hexdump +function prints the hexdump to the supplied +.Xr sbuf 9 . .Bl -tag -width indent .It Fa ptr Pointer to the array of bytes to print. @@ -88,7 +102,8 @@ Do not print the character values on each line. .El .El .Sh SEE ALSO -.Xr ascii 7 +.Xr ascii 7 , +.Xr sbuf 9 .Sh AUTHORS This manual page was written by .An Scott Long . diff --git a/sbin/camcontrol/Makefile b/sbin/camcontrol/Makefile index 4f11f19..519ecbf 100644 --- a/sbin/camcontrol/Makefile +++ b/sbin/camcontrol/Makefile @@ -3,7 +3,7 @@ PROG= camcontrol SRCS= camcontrol.c util.c .if !defined(RELEASE_CRUNCH) -SRCS+= fwdownload.c modeedit.c persist.c progress.c +SRCS+= attrib.c fwdownload.c modeedit.c persist.c progress.c .else CFLAGS+= -DMINIMALISTIC .endif diff --git a/sbin/camcontrol/attrib.c b/sbin/camcontrol/attrib.c new file mode 100644 index 0000000..62d4b1e --- /dev/null +++ b/sbin/camcontrol/attrib.c @@ -0,0 +1,509 @@ +/*- + * Copyright (c) 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) + */ +/* + * SCSI Read and Write Attribute support for camcontrol(8). + */ + +#include <sys/cdefs.h> +__FBSDID("$FreeBSD$"); + +#include <sys/ioctl.h> +#include <sys/stdint.h> +#include <sys/types.h> +#include <sys/endian.h> +#include <sys/sbuf.h> +#include <sys/queue.h> +#include <sys/chio.h> + +#include <stdio.h> +#include <stdlib.h> +#include <inttypes.h> +#include <unistd.h> +#include <string.h> +#include <strings.h> +#include <fcntl.h> +#include <ctype.h> +#include <limits.h> +#include <err.h> +#include <locale.h> + +#include <cam/cam.h> +#include <cam/cam_debug.h> +#include <cam/cam_ccb.h> +#include <cam/scsi/scsi_all.h> +#include <cam/scsi/scsi_pass.h> +#include <cam/scsi/scsi_ch.h> +#include <cam/scsi/scsi_message.h> +#include <camlib.h> +#include "camcontrol.h" + +#if 0 +struct scsi_attr_desc { + int attr_id; + + STAILQ_ENTRY(scsi_attr_desc) links; +}; +#endif + +static struct scsi_nv elem_type_map[] = { + { "all", ELEMENT_TYPE_ALL }, + { "picker", ELEMENT_TYPE_MT }, + { "slot", ELEMENT_TYPE_ST }, + { "portal", ELEMENT_TYPE_IE }, + { "drive", ELEMENT_TYPE_DT }, +}; + +static struct scsi_nv sa_map[] = { + { "attr_values", SRA_SA_ATTR_VALUES }, + { "attr_list", SRA_SA_ATTR_LIST }, + { "lv_list", SRA_SA_LOG_VOL_LIST }, + { "part_list", SRA_SA_PART_LIST }, + { "supp_attr", SRA_SA_SUPPORTED_ATTRS } +}; + +static struct scsi_nv output_format_map[] = { + { "text_esc", SCSI_ATTR_OUTPUT_TEXT_ESC }, + { "text_raw", SCSI_ATTR_OUTPUT_TEXT_RAW }, + { "nonascii_esc", SCSI_ATTR_OUTPUT_NONASCII_ESC }, + { "nonascii_trim", SCSI_ATTR_OUTPUT_NONASCII_TRIM }, + { "nonascii_raw", SCSI_ATTR_OUTPUT_NONASCII_RAW }, + { "field_all", SCSI_ATTR_OUTPUT_FIELD_ALL }, + { "field_none", SCSI_ATTR_OUTPUT_FIELD_NONE }, + { "field_desc", SCSI_ATTR_OUTPUT_FIELD_DESC }, + { "field_num", SCSI_ATTR_OUTPUT_FIELD_NUM }, + { "field_size", SCSI_ATTR_OUTPUT_FIELD_SIZE }, + { "field_rw", SCSI_ATTR_OUTPUT_FIELD_RW }, +}; + +int +scsiattrib(struct cam_device *device, int argc, char **argv, char *combinedopt, + int retry_count, int timeout, int verbosemode, int err_recover) +{ + union ccb *ccb = NULL; + int attr_num = -1; +#if 0 + int num_attrs = 0; +#endif + int start_attr = 0; + int cached_attr = 0; + int read_service_action = -1; + int read_attr = 0, write_attr = 0; + int element_address = 0; + int element_type = ELEMENT_TYPE_ALL; + int partition = 0; + int logical_volume = 0; + char *endptr; + uint8_t *data_buf = NULL; + uint32_t dxfer_len = UINT16_MAX - 1; + uint32_t valid_len; + uint32_t output_format; + STAILQ_HEAD(, scsi_attr_desc) write_attr_list; + int error = 0; + int c; + + ccb = cam_getccb(device); + if (ccb == NULL) { + warnx("%s: error allocating CCB", __func__); + error = 1; + goto bailout; + } + + bzero(&(&ccb->ccb_h)[1], + sizeof(union ccb) - sizeof(struct ccb_hdr)); + + STAILQ_INIT(&write_attr_list); + + /* + * By default, when displaying attribute values, we trim out + * non-ASCII characters in ASCII fields. We display all fields + * (description, attribute number, attribute size, and readonly + * status). We default to displaying raw text. + * + * XXX KDM need to port this to stable/10 and newer FreeBSD + * versions that have iconv built in and can convert codesets. + */ + output_format = SCSI_ATTR_OUTPUT_NONASCII_TRIM | + SCSI_ATTR_OUTPUT_FIELD_ALL | + SCSI_ATTR_OUTPUT_TEXT_RAW; + + data_buf = malloc(dxfer_len); + if (data_buf == NULL) { + warn("%s: error allocating %u bytes", __func__, dxfer_len); + error = 1; + goto bailout; + } + + while ((c = getopt(argc, argv, combinedopt)) != -1) { + switch (c) { + case 'a': + attr_num = strtol(optarg, &endptr, 0); + if (*endptr != '\0') { + warnx("%s: invalid attribute number %s", + __func__, optarg); + error = 1; + goto bailout; + } + start_attr = attr_num; + break; + case 'c': + cached_attr = 1; + break; + case 'e': + element_address = strtol(optarg, &endptr, 0); + if (*endptr != '\0') { + warnx("%s: invalid element address %s", + __func__, optarg); + error = 1; + goto bailout; + } + break; + case 'F': { + scsi_nv_status status; + scsi_attrib_output_flags new_outflags; + int entry_num = 0; + char *tmpstr; + + if (isdigit(optarg[0])) { + output_format = strtoul(optarg, &endptr, 0); + if (*endptr != '\0') { + warnx("%s: invalid numeric output " + "format argument %s", __func__, + optarg); + error = 1; + goto bailout; + } + break; + } + new_outflags = SCSI_ATTR_OUTPUT_NONE; + + while ((tmpstr = strsep(&optarg, ",")) != NULL) { + status = scsi_get_nv(output_format_map, + sizeof(output_format_map) / + sizeof(output_format_map[0]), tmpstr, + &entry_num, SCSI_NV_FLAG_IG_CASE); + + if (status == SCSI_NV_FOUND) + new_outflags |= + output_format_map[entry_num].value; + else { + warnx("%s: %s format option %s", + __func__, + (status == SCSI_NV_AMBIGUOUS) ? + "ambiguous" : "invalid", tmpstr); + error = 1; + goto bailout; + } + } + output_format = new_outflags; + break; + } + case 'p': + partition = strtol(optarg, &endptr, 0); + if (*endptr != '\0') { + warnx("%s: invalid partition number %s", + __func__, optarg); + error = 1; + goto bailout; + } + break; + case 'r': { + scsi_nv_status status; + int entry_num = 0; + + status = scsi_get_nv(sa_map, sizeof(sa_map) / + sizeof(sa_map[0]), optarg, &entry_num, + SCSI_NV_FLAG_IG_CASE); + if (status == SCSI_NV_FOUND) + read_service_action = sa_map[entry_num].value; + else { + warnx("%s: %s %s option %s", __func__, + (status == SCSI_NV_AMBIGUOUS) ? + "ambiguous" : "invalid", "service action", + optarg); + error = 1; + goto bailout; + } + read_attr = 1; + break; + } + case 's': + start_attr = strtol(optarg, &endptr, 0); + if (*endptr != '\0') { + warnx("%s: invalid starting attr argument %s", + __func__, optarg); + error = 1; + goto bailout; + } + break; + case 'T': { + scsi_nv_status status; + int entry_num = 0; + + status = scsi_get_nv(elem_type_map, + sizeof(elem_type_map) / sizeof(elem_type_map[0]), + optarg, &entry_num, SCSI_NV_FLAG_IG_CASE); + if (status == SCSI_NV_FOUND) + element_type = elem_type_map[entry_num].value; + else { + warnx("%s: %s %s option %s", __func__, + (status == SCSI_NV_AMBIGUOUS) ? + "ambiguous" : "invalid", "element type", + optarg); + error = 1; + goto bailout; + } + break; + } + case 'w': + warnx("%s: writing attributes is not implemented yet", + __func__); + error = 1; + goto bailout; + break; + case 'V': + logical_volume = strtol(optarg, &endptr, 0); + + if (*endptr != '\0') { + warnx("%s: invalid logical volume argument %s", + __func__, optarg); + error = 1; + goto bailout; + } + break; + default: + break; + } + } + + /* + * Default to reading attributes + */ + if (((read_attr == 0) && (write_attr == 0)) + || ((read_attr != 0) && (write_attr != 0))) { + warnx("%s: Must specify either -r or -w", __func__); + error = 1; + goto bailout; + } + + if (read_attr != 0) { + scsi_read_attribute(&ccb->csio, + /*retries*/ retry_count, + /*cbfcnp*/ NULL, + /*tag_action*/ MSG_SIMPLE_Q_TAG, + /*service_action*/ read_service_action, + /*element*/ element_address, + /*elem_type*/ element_type, + /*logical_volume*/ logical_volume, + /*partition*/ partition, + /*first_attribute*/ start_attr, + /*cache*/ cached_attr, + /*data_ptr*/ data_buf, + /*length*/ dxfer_len, + /*sense_len*/ SSD_FULL_SIZE, + /*timeout*/ timeout ? timeout : 60000); +#if 0 + } else { +#endif + + } + + ccb->ccb_h.flags |= CAM_DEV_QFRZDIS; + + if (err_recover != 0) + ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER; + + if (cam_send_ccb(device, ccb) < 0) { + warn("error sending %s ATTRIBUTE", (read_attr != 0) ? + "READ" : "WRITE"); + + if (verbosemode != 0) { + cam_error_print(device, ccb, CAM_ESF_ALL, + CAM_EPF_ALL, stderr); + } + + error = 1; + goto bailout; + } + + if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) { + if (verbosemode != 0) { + cam_error_print(device, ccb, CAM_ESF_ALL, + CAM_EPF_ALL, stderr); + } + error = 1; + goto bailout; + } + + if (read_attr == 0) + goto bailout; + + valid_len = dxfer_len - ccb->csio.resid; + + switch (read_service_action) { + case SRA_SA_ATTR_VALUES: { + uint32_t len_left, hdr_len, cur_len; + struct scsi_read_attribute_values *hdr; + struct scsi_mam_attribute_header *cur_id; + char error_str[512]; + uint8_t *cur_pos; + struct sbuf *sb; + + hdr = (struct scsi_read_attribute_values *)data_buf; + + if (valid_len < sizeof(*hdr)) { + fprintf(stdout, "No attributes returned.\n"); + error = 0; + goto bailout; + } + + sb = sbuf_new_auto(); + if (sb == NULL) { + warn("%s: Unable to allocate sbuf", __func__); + error = 1; + goto bailout; + } + /* + * XXX KDM grab more data if it is available. + */ + hdr_len = scsi_4btoul(hdr->length); + + for (len_left = MIN(valid_len, hdr_len), + cur_pos = &hdr->attribute_0[0]; len_left > sizeof(*cur_id); + len_left -= cur_len, cur_pos += cur_len) { + int cur_attr_num; + cur_id = (struct scsi_mam_attribute_header *)cur_pos; + cur_len = scsi_2btoul(cur_id->length) + sizeof(*cur_id); + cur_attr_num = scsi_2btoul(cur_id->id); + + if ((attr_num != -1) + && (cur_attr_num != attr_num)) + continue; + + error = scsi_attrib_sbuf(sb, cur_id, len_left, + /*user_table*/ NULL, /*num_user_entries*/ 0, + /*prefer_user_table*/ 0, output_format, error_str, + sizeof(error_str)); + if (error != 0) { + warnx("%s: %s", __func__, error_str); + sbuf_delete(sb); + error = 1; + goto bailout; + } + if (attr_num != -1) + break; + } + + sbuf_finish(sb); + fprintf(stdout, "%s", sbuf_data(sb)); + sbuf_delete(sb); + break; + } + case SRA_SA_SUPPORTED_ATTRS: + case SRA_SA_ATTR_LIST: { + uint32_t len_left, hdr_len; + struct scsi_attrib_list_header *hdr; + struct scsi_attrib_table_entry *entry = NULL; + const char *sa_name = "Supported Attributes"; + const char *at_name = "Available Attributes"; + int attr_id; + uint8_t *cur_id; + + hdr = (struct scsi_attrib_list_header *)data_buf; + if (valid_len < sizeof(*hdr)) { + fprintf(stdout, "No %s\n", + (read_service_action == SRA_SA_SUPPORTED_ATTRS)? + sa_name : at_name); + error = 0; + goto bailout; + } + fprintf(stdout, "%s:\n", + (read_service_action == SRA_SA_SUPPORTED_ATTRS) ? + sa_name : at_name); + hdr_len = scsi_4btoul(hdr->length); + for (len_left = MIN(valid_len, hdr_len), + cur_id = &hdr->first_attr_0[0]; len_left > 1; + len_left -= sizeof(uint16_t), cur_id += sizeof(uint16_t)) { + attr_id = scsi_2btoul(cur_id); + + if ((attr_num != -1) + && (attr_id != attr_num)) + continue; + + entry = scsi_get_attrib_entry(attr_id); + fprintf(stdout, "0x%.4x", attr_id); + if (entry == NULL) + fprintf(stdout, "\n"); + else + fprintf(stdout, ": %s\n", entry->desc); + + if (attr_num != -1) + break; + } + break; + } + case SRA_SA_PART_LIST: + case SRA_SA_LOG_VOL_LIST: { + struct scsi_attrib_lv_list *lv_list; + const char *partition_name = "Partition"; + const char *lv_name = "Logical Volume"; + + if (valid_len < sizeof(*lv_list)) { + fprintf(stdout, "No %s list returned\n", + (read_service_action == SRA_SA_PART_LIST) ? + partition_name : lv_name); + error = 0; + goto bailout; + } + + lv_list = (struct scsi_attrib_lv_list *)data_buf; + + fprintf(stdout, "First %s: %d\n", + (read_service_action == SRA_SA_PART_LIST) ? + partition_name : lv_name, + lv_list->first_lv_number); + fprintf(stdout, "Number of %ss: %d\n", + (read_service_action == SRA_SA_PART_LIST) ? + partition_name : lv_name, + lv_list->num_logical_volumes); + break; + } + default: + break; + } +bailout: + if (ccb != NULL) + cam_freeccb(ccb); + + free(data_buf); + + return (error); +} diff --git a/sbin/camcontrol/camcontrol.8 b/sbin/camcontrol/camcontrol.8 index c3c6b1c..4ee52d1 100644 --- a/sbin/camcontrol/camcontrol.8 +++ b/sbin/camcontrol/camcontrol.8 @@ -298,6 +298,19 @@ .Op Fl T Ar res_type .Op Fl U .Nm +.Ic attrib +.Op device id +.Op generic args +.Aq Fl r Ar action | Fl w Ar attrib +.Op Fl a Ar attr_num +.Op Fl c +.Op Fl e Ar elem_addr +.Op Fl F Ar form1,form2 +.Op Fl p Ar part +.Op Fl s Ar start_addr +.Op Fl T Ar elem_type +.Op Fl V Ar lv_num +.Nm .Ic help .Sh DESCRIPTION The @@ -1823,6 +1836,129 @@ Register and Move request. This option only applies to the Register and Move service action of the Persistent Reserve Out command. .El +.It Ic attrib +Issue the +.Tn SCSI +READ or WRITE ATTRIBUTE commands. +These commands are used to read and write attributes in Medium Auxiliary +Memory (MAM). +The most common place Medium Auxiliary Memory is found is small flash chips +included tape cartriges. +For instance, +.Tn LTO +tapes have MAM. +Either the +.Fl r +option or the +.Fl w +option must be specified. +.Bl -tag -width 14n +.It Fl r Ar action +Specify the READ ATTRIBUTE service action. +.Bl -tag -width 11n +.It attr_values +Issue the ATTRIBUTE VALUES service action. +Read and decode the available attributes and their values. +.It attr_list +Issue the ATTRIBUTE LIST service action. +List the attributes that are available to read and write. +.It lv_list +Issue the LOGICAL VOLUME LIST service action. +List the available logical volumes in the MAM. +.It part_list +Issue the PARTITION LIST service action. +List the available partitions in the MAM. +.It supp_attr +Issue the SUPPORTED ATTRIBUTES service action. +List attributes that are supported for reading or writing. +These attributes may or may not be currently present in the MAM. +.El +.It Fl w Ar attr +Specify an attribute to write to the MAM. +This option is not yet implemented. +.It Fl a Ar num +Specify the attribute number to display. +This option only works with the attr_values, attr_list and supp_attr +arguments to +.Fl r . +.It Fl c +Display cached attributes. +If the device supports this flag, it allows displaying attributes for the +last piece of media loaded in the drive. +.It Fl e Ar num +Specify the element address. +This is used for specifying which element number in a medium changer to +access when reading attributes. +The element number could be for a picker, portal, slot or drive. +.It Fl F Ar form1,form2 +Specify the output format for the attribute values (attr_val) display as a +comma separated list of options. +The default output is currently set to field_all,nonascii_trim,text_raw. +Once this code is ported to FreeBSD 10, any text fields will be converted +from their codeset to the user's native codeset with +.Xr iconv 3 . +.Pp +The text options are mutually exclusive; if you specify more than one, you +will get unpredictable results. +The nonascii options are also mutually exclusive. +Most of the field options may be logically ORed together. +.Bl -tag -width 12n +.It text_esc +Print text fields with non-ASCII characters escaped. +.It text_raw +Print text fields natively, with no codeset conversion. +.It nonascii_esc +If any non-ASCII characters occur in fields that are supposed to be ASCII, +escape the non-ASCII characters. +.It nonascii_trim +If any non-ASCII characters occur in fields that are supposed to be ASCII, +omit the non-ASCII characters. +.It nonascii_raw +If any non-ASCII characters occur in fields that are supposed to be ASCII, +print them as they are. +.It field_all +Print all of the prefix fields: description, attribute number, attribute +size, and the attribute's readonly status. +If field_all is specified, specifying any other field options will not have +an effect. +.It field_none +Print none of the prefix fields, and only print out the attribute value. +If field_none is specified, specifying any other field options will result +in those fields being printed. +.It field_desc +Print out the attribute description. +.It field_num +Print out the attribute number. +.It field_size +Print out the attribute size. +.It field_rw +Print out the attribute's readonly status. +.El +.It Fl p Ar part +Specify the partition. +When the media has multiple partitions, specifying different partition +numbers allows seeing the values for each individual partition. +.It Fl s Ar start_num +Specify the starting attribute number. +This requests that the target device return attribute information starting +at the given number. +.It Fl T Ar elem_type +Specify the element type. +For medium changer devices, this allows specifying the type the element +referenced in the element address ( +.Fl e ) . +Valid types are: +.Dq all , +.Dq picker , +.Dq slot , +.Dq portal , +and +.Dq drive . +.El +.It Fl V Ar vol_num +Specify the number of the logical volume to operate on. +If the media has multiple logical volumes, this will allow displaying +or writing attributes on the given logical volume. .It Ic help Print out verbose usage information. .El @@ -2049,6 +2185,15 @@ current initiator will be unregistered from the target. The reservation will be moved to relative target port 2 on the target device. The registration will persist across power losses. +.Pp +.Bd -literal -offset indent +camcontrol attrib sa0 -v -i attr_values -p 1 +.Ed +.Pp +This will read and decode the attribute values from partition 1 on the tape +in tape drive sa0, and will display any +.Tn SCSI +errors that result. .Sh SEE ALSO .Xr cam 3 , .Xr cam_cdbparse 3 , diff --git a/sbin/camcontrol/camcontrol.c b/sbin/camcontrol/camcontrol.c index c0a1344..072492d 100644 --- a/sbin/camcontrol/camcontrol.c +++ b/sbin/camcontrol/camcontrol.c @@ -98,7 +98,8 @@ typedef enum { CAM_CMD_SANITIZE = 0x0000001f, CAM_CMD_PERSIST = 0x00000020, CAM_CMD_APM = 0x00000021, - CAM_CMD_AAM = 0x00000022 + CAM_CMD_AAM = 0x00000022, + CAM_CMD_ATTRIB = 0x00000023 } cam_cmdmask; typedef enum { @@ -224,6 +225,7 @@ static struct camcontrol_opts option_table[] = { {"security", CAM_CMD_SECURITY, CAM_ARG_NONE, "d:e:fh:k:l:qs:T:U:y"}, {"hpa", CAM_CMD_HPA, CAM_ARG_NONE, "Pflp:qs:U:y"}, {"persist", CAM_CMD_PERSIST, CAM_ARG_NONE, "ai:I:k:K:o:ps:ST:U"}, + {"attrib", CAM_CMD_ATTRIB, CAM_ARG_NONE, "a:ce:F:p:r:s:T:w:V:"}, #endif /* MINIMALISTIC */ {"help", CAM_CMD_USAGE, CAM_ARG_NONE, NULL}, {"-?", CAM_CMD_USAGE, CAM_ARG_NONE, NULL}, @@ -8118,6 +8120,9 @@ usage(int printlong) " camcontrol persist [dev_id][generic args] <-i action|-o action>\n" " [-a][-I tid][-k key][-K sa_key][-p][-R rtp]\n" " [-s scope][-S][-T type][-U]\n" +" camcontrol attrib [dev_id][generic args] <-r action|-w attr>\n" +" [-a attr_num][-c][-e elem][-F form1,form1]\n" +" [-p part][-s start][-T type][-V vol]\n" #endif /* MINIMALISTIC */ " camcontrol help\n"); if (!printlong) @@ -8157,6 +8162,7 @@ usage(int printlong) "fwdownload program firmware of the named device with the given image\n" "security report or send ATA security commands to the named device\n" "persist send the SCSI PERSISTENT RESERVE IN or OUT commands\n" +"attrib send the SCSI READ or WRITE ATTRIBUTE commands\n" "help this message\n" "Device Identifiers:\n" "bus:target specify the bus and target, lun defaults to 0\n" @@ -8307,6 +8313,20 @@ usage(int printlong) "-T res_type specify the reservation type: read_shared, wr_ex, rd_ex,\n" " ex_ac, wr_ex_ro, ex_ac_ro, wr_ex_ar, ex_ac_ar\n" "-U unregister the current initiator for register_move\n" +"attrib arguments:\n" +"-r action specify attr_values, attr_list, lv_list, part_list, or\n" +" supp_attr\n" +"-w attr specify an attribute to write, one -w argument per attr\n" +"-a attr_num only display this attribute number\n" +"-c get cached attributes\n" +"-e elem_addr request attributes for the given element in a changer\n" +"-F form1,form2 output format, comma separated list: text_esc, text_raw,\n" +" nonascii_esc, nonascii_trim, nonascii_raw, field_all,\n" +" field_none, field_desc, field_num, field_size, field_rw\n" +"-p partition request attributes for the given partition\n" +"-s start_attr request attributes starting at the given number\n" +"-T elem_type specify the element type (used with -e)\n" +"-V logical_vol specify the logical volume ID\n" ); #endif /* MINIMALISTIC */ } @@ -8651,6 +8671,11 @@ main(int argc, char **argv) retry_count, timeout, arglist & CAM_ARG_VERBOSE, arglist & CAM_ARG_ERR_RECOVER); break; + case CAM_CMD_ATTRIB: + error = scsiattrib(cam_dev, argc, argv, combinedopt, + retry_count, timeout, arglist & CAM_ARG_VERBOSE, + arglist & CAM_ARG_ERR_RECOVER); + break; #endif /* MINIMALISTIC */ case CAM_CMD_USAGE: usage(1); diff --git a/sbin/camcontrol/camcontrol.h b/sbin/camcontrol/camcontrol.h index 7c249bf..582b6a3 100644 --- a/sbin/camcontrol/camcontrol.h +++ b/sbin/camcontrol/camcontrol.h @@ -66,6 +66,9 @@ int scsidoinquiry(struct cam_device *device, int argc, char **argv, int scsipersist(struct cam_device *device, int argc, char **argv, char *combinedopt, int retry_count, int timeout, int verbose, int err_recover); +int scsiattrib(struct cam_device *device, int argc, char **argv, + char *combinedopt, int retry_count, int timeout, int verbose, + int err_recover); char *cget(void *hook, char *name); int iget(void *hook, char *name); void arg_put(void *hook, int letter, void *arg, int count, char *name); diff --git a/share/man/man9/sbuf.9 b/share/man/man9/sbuf.9 index d8acbaf..1cd39dc 100644 --- a/share/man/man9/sbuf.9 +++ b/share/man/man9/sbuf.9 @@ -53,7 +53,8 @@ .Nm sbuf_done , .Nm sbuf_delete , .Nm sbuf_start_section , -.Nm sbuf_end_section +.Nm sbuf_end_section , +.Nm sbuf_hexdump .Nd safe string composition .Sh SYNOPSIS .In sys/types.h @@ -106,6 +107,14 @@ .Fn sbuf_start_section "struct sbuf *s" "ssize_t *old_lenp" .Ft ssize_t .Fn sbuf_end_section "struct sbuf *s" "ssize_t old_len" "size_t pad" "int c" +.Ft void +.Fo sbuf_hexdump +.Fa "struct sbuf *sb" +.Fa "void *ptr" +.Fa "int length" +.Fa "const char *hdr" +.Fa "int flags" +.Fc .In sys/sysctl.h .Ft struct sbuf * .Fn sbuf_new_for_sysctl "struct sbuf *s" "char *buf" "int length" "struct sysctl_req *req" @@ -432,6 +441,14 @@ and \-1 can be specified for and .Fa old_len respectively. +.Pp +The +.Fn sbuf_hexdump +function prints an array of bytes to the supplied sbuf, along with an ASCII +representation of the bytes if possible. +See the +.Xr hexdump 3 +man page for more details on the interface. .Sh NOTES If an operation caused an .Fa sbuf @@ -535,6 +552,7 @@ transmit_msg(sbuf_data(sb), sbuf_len(sb)); sbuf_delete(sb); .Ed .Sh SEE ALSO +.Xr hexdump 3 , .Xr printf 3 , .Xr strcat 3 , .Xr strcpy 3 , diff --git a/sys/cam/cam.c b/sys/cam/cam.c index f608d6f..c7dc2dd 100644 --- a/sys/cam/cam.c +++ b/sys/cam/cam.c @@ -158,6 +158,56 @@ cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen) *dst = '\0'; } +void +cam_strvis_sbuf(struct sbuf *sb, const u_int8_t *src, int srclen, + uint32_t flags) +{ + + /* Trim leading/trailing spaces, nulls. */ + while (srclen > 0 && src[0] == ' ') + src++, srclen--; + while (srclen > 0 + && (src[srclen-1] == ' ' || src[srclen-1] == '\0')) + srclen--; + + while (srclen > 0) { + if (*src < 0x20 || *src >= 0x80) { + /* SCSI-II Specifies that these should never occur. */ + /* non-printable character */ + switch (flags & CAM_STRVIS_FLAG_NONASCII_MASK) { + case CAM_STRVIS_FLAG_NONASCII_ESC: + sbuf_printf(sb, "\\%c%c%c", + ((*src & 0300) >> 6) + '0', + ((*src & 0070) >> 3) + '0', + ((*src & 0007) >> 0) + '0'); + break; + case CAM_STRVIS_FLAG_NONASCII_RAW: + /* + * If we run into a NUL, just transform it + * into a space. + */ + if (*src != 0x00) + sbuf_putc(sb, *src); + else + sbuf_putc(sb, ' '); + break; + case CAM_STRVIS_FLAG_NONASCII_SPC: + sbuf_putc(sb, ' '); + break; + case CAM_STRVIS_FLAG_NONASCII_TRIM: + default: + break; + } + } else { + /* normal character */ + sbuf_putc(sb, *src); + } + src++; + srclen--; + } +} + + /* * Compare string with pattern, returning 0 on match. * Short pattern matches trailing blanks in name, diff --git a/sys/cam/cam.h b/sys/cam/cam.h index c226e3e..5b8a5e0 100644 --- a/sys/cam/cam.h +++ b/sys/cam/cam.h @@ -346,6 +346,15 @@ typedef enum { CAM_EAF_PRINT_RESULT = 0x20 } cam_error_ata_flags; +typedef enum { + CAM_STRVIS_FLAG_NONE = 0x00, + CAM_STRVIS_FLAG_NONASCII_MASK = 0x03, + CAM_STRVIS_FLAG_NONASCII_TRIM = 0x00, + CAM_STRVIS_FLAG_NONASCII_RAW = 0x01, + CAM_STRVIS_FLAG_NONASCII_SPC = 0x02, + CAM_STRVIS_FLAG_NONASCII_ESC = 0x03 +} cam_strvis_flags; + struct cam_status_entry { cam_status status_code; @@ -358,6 +367,7 @@ extern const int num_cam_status_entries; extern int cam_sort_io_queues; #endif union ccb; +struct sbuf; #ifdef SYSCTL_DECL /* from sysctl.h */ SYSCTL_DECL(_kern_cam); @@ -370,6 +380,8 @@ caddr_t cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries, int entry_size, cam_quirkmatch_t *comp_func); void cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen); +void cam_strvis_sbuf(struct sbuf *sb, const u_int8_t *src, int srclen, + uint32_t flags); int cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len); const struct cam_status_entry* diff --git a/sys/cam/scsi/scsi_all.c b/sys/cam/scsi/scsi_all.c index 6267693..25c5451 100644 --- a/sys/cam/scsi/scsi_all.c +++ b/sys/cam/scsi/scsi_all.c @@ -6454,6 +6454,830 @@ bailout: return (retval); } +struct scsi_attrib_table_entry scsi_mam_attr_table[] = { + { SMA_ATTR_REM_CAP_PARTITION, SCSI_ATTR_FLAG_NONE, + "Remaining Capacity in Partition", + /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf,/*parse_str*/ NULL }, + { SMA_ATTR_MAX_CAP_PARTITION, SCSI_ATTR_FLAG_NONE, + "Maximum Capacity in Partition", + /*suffix*/"MB", /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL }, + { SMA_ATTR_TAPEALERT_FLAGS, SCSI_ATTR_FLAG_HEX, + "TapeAlert Flags", + /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL }, + { SMA_ATTR_LOAD_COUNT, SCSI_ATTR_FLAG_NONE, + "Load Count", + /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL }, + { SMA_ATTR_MAM_SPACE_REMAINING, SCSI_ATTR_FLAG_NONE, + "MAM Space Remaining", + /*suffix*/"bytes", /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_DEV_ASSIGNING_ORG, SCSI_ATTR_FLAG_NONE, + "Assigning Organization", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_FORMAT_DENSITY_CODE, SCSI_ATTR_FLAG_HEX, + "Format Density Code", + /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL }, + { SMA_ATTR_INITIALIZATION_COUNT, SCSI_ATTR_FLAG_NONE, + "Initialization Count", + /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, /*parse_str*/ NULL }, + { SMA_ATTR_VOLUME_ID, SCSI_ATTR_FLAG_NONE, + "Volume Identifier", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_VOLUME_CHANGE_REF, SCSI_ATTR_FLAG_HEX, + "Volume Change Reference", + /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_DEV_SERIAL_LAST_LOAD, SCSI_ATTR_FLAG_NONE, + "Device Vendor/Serial at Last Load", + /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_DEV_SERIAL_LAST_LOAD_1, SCSI_ATTR_FLAG_NONE, + "Device Vendor/Serial at Last Load - 1", + /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_DEV_SERIAL_LAST_LOAD_2, SCSI_ATTR_FLAG_NONE, + "Device Vendor/Serial at Last Load - 2", + /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_DEV_SERIAL_LAST_LOAD_3, SCSI_ATTR_FLAG_NONE, + "Device Vendor/Serial at Last Load - 3", + /*suffix*/NULL, /*to_str*/ scsi_attrib_vendser_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_TOTAL_MB_WRITTEN_LT, SCSI_ATTR_FLAG_NONE, + "Total MB Written in Medium Life", + /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_TOTAL_MB_READ_LT, SCSI_ATTR_FLAG_NONE, + "Total MB Read in Medium Life", + /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_TOTAL_MB_WRITTEN_CUR, SCSI_ATTR_FLAG_NONE, + "Total MB Written in Current/Last Load", + /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_TOTAL_MB_READ_CUR, SCSI_ATTR_FLAG_NONE, + "Total MB Read in Current/Last Load", + /*suffix*/ "MB", /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_FIRST_ENC_BLOCK, SCSI_ATTR_FLAG_NONE, + "Logical Position of First Encrypted Block", + /*suffix*/ NULL, /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_NEXT_UNENC_BLOCK, SCSI_ATTR_FLAG_NONE, + "Logical Position of First Unencrypted Block after First " + "Encrypted Block", + /*suffix*/ NULL, /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MEDIUM_USAGE_HIST, SCSI_ATTR_FLAG_NONE, + "Medium Usage History", + /*suffix*/ NULL, /*to_str*/ NULL, + /*parse_str*/ NULL }, + { SMA_ATTR_PART_USAGE_HIST, SCSI_ATTR_FLAG_NONE, + "Partition Usage History", + /*suffix*/ NULL, /*to_str*/ NULL, + /*parse_str*/ NULL }, + { SMA_ATTR_MED_MANUF, SCSI_ATTR_FLAG_NONE, + "Medium Manufacturer", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MED_SERIAL, SCSI_ATTR_FLAG_NONE, + "Medium Serial Number", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MED_LENGTH, SCSI_ATTR_FLAG_NONE, + "Medium Length", + /*suffix*/"m", /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MED_WIDTH, SCSI_ATTR_FLAG_FP | SCSI_ATTR_FLAG_DIV_10 | + SCSI_ATTR_FLAG_FP_1DIGIT, + "Medium Width", + /*suffix*/"mm", /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MED_ASSIGNING_ORG, SCSI_ATTR_FLAG_NONE, + "Assigning Organization", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MED_DENSITY_CODE, SCSI_ATTR_FLAG_HEX, + "Medium Density Code", + /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MED_MANUF_DATE, SCSI_ATTR_FLAG_NONE, + "Medium Manufacture Date", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MAM_CAPACITY, SCSI_ATTR_FLAG_NONE, + "MAM Capacity", + /*suffix*/"bytes", /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MED_TYPE, SCSI_ATTR_FLAG_HEX, + "Medium Type", + /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MED_TYPE_INFO, SCSI_ATTR_FLAG_HEX, + "Medium Type Information", + /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MED_SERIAL_NUM, SCSI_ATTR_FLAG_NONE, + "Medium Serial Number", + /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_APP_VENDOR, SCSI_ATTR_FLAG_NONE, + "Application Vendor", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_APP_NAME, SCSI_ATTR_FLAG_NONE, + "Application Name", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_APP_VERSION, SCSI_ATTR_FLAG_NONE, + "Application Version", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_USER_MED_TEXT_LABEL, SCSI_ATTR_FLAG_NONE, + "User Medium Text Label", + /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_LAST_WRITTEN_TIME, SCSI_ATTR_FLAG_NONE, + "Date and Time Last Written", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_TEXT_LOCAL_ID, SCSI_ATTR_FLAG_HEX, + "Text Localization Identifier", + /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_BARCODE, SCSI_ATTR_FLAG_NONE, + "Barcode", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_HOST_OWNER_NAME, SCSI_ATTR_FLAG_NONE, + "Owning Host Textual Name", + /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_MEDIA_POOL, SCSI_ATTR_FLAG_NONE, + "Media Pool", + /*suffix*/NULL, /*to_str*/ scsi_attrib_text_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_PART_USER_LABEL, SCSI_ATTR_FLAG_NONE, + "Partition User Text Label", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_LOAD_UNLOAD_AT_PART, SCSI_ATTR_FLAG_NONE, + "Load/Unload at Partition", + /*suffix*/NULL, /*to_str*/ scsi_attrib_int_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_APP_FORMAT_VERSION, SCSI_ATTR_FLAG_NONE, + "Application Format Version", + /*suffix*/NULL, /*to_str*/ scsi_attrib_ascii_sbuf, + /*parse_str*/ NULL }, + { SMA_ATTR_VOL_COHERENCY_INFO, SCSI_ATTR_FLAG_NONE, + "Volume Coherency Information", + /*suffix*/NULL, /*to_str*/ scsi_attrib_volcoh_sbuf, + /*parse_str*/ NULL }, + { 0x0ff1, SCSI_ATTR_FLAG_NONE, + "Spectra MLM Creation", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, + { 0x0ff2, SCSI_ATTR_FLAG_NONE, + "Spectra MLM C3", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, + { 0x0ff3, SCSI_ATTR_FLAG_NONE, + "Spectra MLM RW", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, + { 0x0ff4, SCSI_ATTR_FLAG_NONE, + "Spectra MLM SDC List", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, + { 0x0ff7, SCSI_ATTR_FLAG_NONE, + "Spectra MLM Post Scan", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, + { 0x0ffe, SCSI_ATTR_FLAG_NONE, + "Spectra MLM Checksum", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, + { 0x17f1, SCSI_ATTR_FLAG_NONE, + "Spectra MLM Creation", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, + { 0x17f2, SCSI_ATTR_FLAG_NONE, + "Spectra MLM C3", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, + { 0x17f3, SCSI_ATTR_FLAG_NONE, + "Spectra MLM RW", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, + { 0x17f4, SCSI_ATTR_FLAG_NONE, + "Spectra MLM SDC List", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, + { 0x17f7, SCSI_ATTR_FLAG_NONE, + "Spectra MLM Post Scan", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, + { 0x17ff, SCSI_ATTR_FLAG_NONE, + "Spectra MLM Checksum", + /*suffix*/NULL, /*to_str*/ scsi_attrib_hexdump_sbuf, + /*parse_str*/ NULL }, +}; + +/* + * Print out Volume Coherency Information (Attribute 0x080c). + * This field has two variable length members, including one at the + * beginning, so it isn't practical to have a fixed structure definition. + * This is current as of SSC4r03 (see section 4.2.21.3), dated March 25, + * 2013. + */ +int +scsi_attrib_volcoh_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len) +{ + size_t avail_len; + uint32_t field_size; + uint64_t tmp_val; + uint8_t *cur_ptr; + int retval; + int vcr_len, as_len; + + retval = 0; + tmp_val = 0; + + field_size = scsi_2btoul(hdr->length); + avail_len = valid_len - sizeof(*hdr); + if (field_size > avail_len) { + if (error_str != NULL) { + snprintf(error_str, error_str_len, "Available " + "length of attribute ID 0x%.4x %zu < field " + "length %u", scsi_2btoul(hdr->id), avail_len, + field_size); + } + retval = 1; + goto bailout; + } else if (field_size == 0) { + /* + * It isn't clear from the spec whether a field length of + * 0 is invalid here. It probably is, but be lenient here + * to avoid inconveniencing the user. + */ + goto bailout; + } + cur_ptr = hdr->attribute; + vcr_len = *cur_ptr; + cur_ptr++; + + sbuf_printf(sb, "\n\tVolume Change Reference Value:"); + + switch (vcr_len) { + case 0: + if (error_str != NULL) { + snprintf(error_str, error_str_len, "Volume Change " + "Reference value has length of 0"); + } + retval = 1; + goto bailout; + break; /*NOTREACHED*/ + case 1: + tmp_val = *cur_ptr; + break; + case 2: + tmp_val = scsi_2btoul(cur_ptr); + break; + case 3: + tmp_val = scsi_3btoul(cur_ptr); + break; + case 4: + tmp_val = scsi_4btoul(cur_ptr); + break; + case 8: + tmp_val = scsi_8btou64(cur_ptr); + break; + default: + sbuf_printf(sb, "\n"); + sbuf_hexdump(sb, cur_ptr, vcr_len, NULL, 0); + break; + } + if (vcr_len <= 8) + sbuf_printf(sb, " 0x%jx\n", (uintmax_t)tmp_val); + + cur_ptr += vcr_len; + tmp_val = scsi_8btou64(cur_ptr); + sbuf_printf(sb, "\tVolume Coherency Count: %ju\n", (uintmax_t)tmp_val); + + cur_ptr += sizeof(tmp_val); + tmp_val = scsi_8btou64(cur_ptr); + sbuf_printf(sb, "\tVolume Coherency Set Identifier: 0x%jx\n", + (uintmax_t)tmp_val); + + /* + * Figure out how long the Application Client Specific Information + * is and produce a hexdump. + */ + cur_ptr += sizeof(tmp_val); + as_len = scsi_2btoul(cur_ptr); + cur_ptr += sizeof(uint16_t); + sbuf_printf(sb, "\tApplication Client Specific Information: "); + if (((as_len == SCSI_LTFS_VER0_LEN) + || (as_len == SCSI_LTFS_VER1_LEN)) + && (strncmp(cur_ptr, SCSI_LTFS_STR_NAME, SCSI_LTFS_STR_LEN) == 0)) { + sbuf_printf(sb, "LTFS\n"); + cur_ptr += SCSI_LTFS_STR_LEN + 1; + if (cur_ptr[SCSI_LTFS_UUID_LEN] != '\0') + cur_ptr[SCSI_LTFS_UUID_LEN] = '\0'; + sbuf_printf(sb, "\tLTFS UUID: %s\n", cur_ptr); + cur_ptr += SCSI_LTFS_UUID_LEN + 1; + /* XXX KDM check the length */ + sbuf_printf(sb, "\tLTFS Version: %d\n", *cur_ptr); + } else { + sbuf_printf(sb, "Unknown\n"); + sbuf_hexdump(sb, cur_ptr, as_len, NULL, 0); + } + +bailout: + return (retval); +} + +int +scsi_attrib_vendser_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len) +{ + size_t avail_len; + uint32_t field_size; + struct scsi_attrib_vendser *vendser; + cam_strvis_flags strvis_flags; + int retval = 0; + + field_size = scsi_2btoul(hdr->length); + avail_len = valid_len - sizeof(*hdr); + if (field_size > avail_len) { + if (error_str != NULL) { + snprintf(error_str, error_str_len, "Available " + "length of attribute ID 0x%.4x %zu < field " + "length %u", scsi_2btoul(hdr->id), avail_len, + field_size); + } + retval = 1; + goto bailout; + } else if (field_size == 0) { + /* + * A field size of 0 doesn't make sense here. The device + * can at least give you the vendor ID, even if it can't + * give you the serial number. + */ + if (error_str != NULL) { + snprintf(error_str, error_str_len, "The length of " + "attribute ID 0x%.4x is 0", + scsi_2btoul(hdr->id)); + } + retval = 1; + goto bailout; + } + vendser = (struct scsi_attrib_vendser *)hdr->attribute; + + switch (output_flags & SCSI_ATTR_OUTPUT_NONASCII_MASK) { + case SCSI_ATTR_OUTPUT_NONASCII_TRIM: + strvis_flags = CAM_STRVIS_FLAG_NONASCII_TRIM; + break; + case SCSI_ATTR_OUTPUT_NONASCII_RAW: + strvis_flags = CAM_STRVIS_FLAG_NONASCII_RAW; + break; + case SCSI_ATTR_OUTPUT_NONASCII_ESC: + default: + strvis_flags = CAM_STRVIS_FLAG_NONASCII_ESC; + break;; + } + cam_strvis_sbuf(sb, vendser->vendor, sizeof(vendser->vendor), + strvis_flags); + sbuf_putc(sb, ' '); + cam_strvis_sbuf(sb, vendser->serial_num, sizeof(vendser->serial_num), + strvis_flags); +bailout: + return (retval); +} + +int +scsi_attrib_hexdump_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len) +{ + uint32_t field_size; + ssize_t avail_len; + uint32_t print_len; + uint8_t *num_ptr; + int retval = 0; + + field_size = scsi_2btoul(hdr->length); + avail_len = valid_len - sizeof(*hdr); + print_len = MIN(avail_len, field_size); + num_ptr = hdr->attribute; + + if (print_len > 0) { + sbuf_printf(sb, "\n"); + sbuf_hexdump(sb, num_ptr, print_len, NULL, 0); + } + + return (retval); +} + +int +scsi_attrib_int_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len) +{ + uint64_t print_number; + size_t avail_len; + uint32_t number_size; + int retval = 0; + + number_size = scsi_2btoul(hdr->length); + + avail_len = valid_len - sizeof(*hdr); + if (avail_len < number_size) { + if (error_str != NULL) { + snprintf(error_str, error_str_len, "Available " + "length of attribute ID 0x%.4x %zu < field " + "length %u", scsi_2btoul(hdr->id), avail_len, + number_size); + } + retval = 1; + goto bailout; + } + + switch (number_size) { + case 0: + /* + * We don't treat this as an error, since there may be + * scenarios where a device reports a field but then gives + * a length of 0. See the note in scsi_attrib_ascii_sbuf(). + */ + goto bailout; + break; /*NOTREACHED*/ + case 1: + print_number = hdr->attribute[0]; + break; + case 2: + print_number = scsi_2btoul(hdr->attribute); + break; + case 3: + print_number = scsi_3btoul(hdr->attribute); + break; + case 4: + print_number = scsi_4btoul(hdr->attribute); + break; + case 8: + print_number = scsi_8btou64(hdr->attribute); + break; + default: + /* + * If we wind up here, the number is too big to print + * normally, so just do a hexdump. + */ + retval = scsi_attrib_hexdump_sbuf(sb, hdr, valid_len, + flags, output_flags, + error_str, error_str_len); + goto bailout; + break; + } + + if (flags & SCSI_ATTR_FLAG_FP) { +#ifndef _KERNEL + long double num_float; + + num_float = (long double)print_number; + + if (flags & SCSI_ATTR_FLAG_DIV_10) + num_float /= 10; + + sbuf_printf(sb, "%.*Lf", (flags & SCSI_ATTR_FLAG_FP_1DIGIT) ? + 1 : 0, num_float); +#else /* _KERNEL */ + sbuf_printf(sb, "%ju", (flags & SCSI_ATTR_FLAG_DIV_10) ? + (print_number / 10) : print_number); +#endif /* _KERNEL */ + } else if (flags & SCSI_ATTR_FLAG_HEX) { + sbuf_printf(sb, "0x%jx", (uintmax_t)print_number); + } else + sbuf_printf(sb, "%ju", (uintmax_t)print_number); + +bailout: + return (retval); +} + +int +scsi_attrib_ascii_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len) +{ + size_t avail_len; + uint32_t field_size, print_size; + int retval = 0; + + avail_len = valid_len - sizeof(*hdr); + field_size = scsi_2btoul(hdr->length); + print_size = MIN(avail_len, field_size); + + if (print_size > 0) { + cam_strvis_flags strvis_flags; + + switch (output_flags & SCSI_ATTR_OUTPUT_NONASCII_MASK) { + case SCSI_ATTR_OUTPUT_NONASCII_TRIM: + strvis_flags = CAM_STRVIS_FLAG_NONASCII_TRIM; + break; + case SCSI_ATTR_OUTPUT_NONASCII_RAW: + strvis_flags = CAM_STRVIS_FLAG_NONASCII_RAW; + break; + case SCSI_ATTR_OUTPUT_NONASCII_ESC: + default: + strvis_flags = CAM_STRVIS_FLAG_NONASCII_ESC; + break; + } + cam_strvis_sbuf(sb, hdr->attribute, print_size, strvis_flags); + } else if (avail_len < field_size) { + /* + * We only report an error if the user didn't allocate + * enough space to hold the full value of this field. If + * the field length is 0, that is allowed by the spec. + * e.g. in SPC-4r37, section 7.4.2.2.5, VOLUME IDENTIFIER + * "This attribute indicates the current volume identifier + * (see SMC-3) of the medium. If the device server supports + * this attribute but does not have access to the volume + * identifier, the device server shall report this attribute + * with an attribute length value of zero." + */ + if (error_str != NULL) { + snprintf(error_str, error_str_len, "Available " + "length of attribute ID 0x%.4x %zu < field " + "length %u", scsi_2btoul(hdr->id), avail_len, + field_size); + } + retval = 1; + } + + return (retval); +} + +int +scsi_attrib_text_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len) +{ + size_t avail_len; + uint32_t field_size, print_size; + int retval = 0; + int esc_text = 1; + + avail_len = valid_len - sizeof(*hdr); + field_size = scsi_2btoul(hdr->length); + print_size = MIN(avail_len, field_size); + + if ((output_flags & SCSI_ATTR_OUTPUT_TEXT_MASK) == + SCSI_ATTR_OUTPUT_TEXT_RAW) + esc_text = 0; + + if (print_size > 0) { + uint32_t i; + + for (i = 0; i < print_size; i++) { + if (hdr->attribute[i] == '\0') + continue; + else if (((unsigned char)hdr->attribute[i] < 0x80) + || (esc_text == 0)) + sbuf_putc(sb, hdr->attribute[i]); + else + sbuf_printf(sb, "%%%02x", + (unsigned char)hdr->attribute[i]); + } + } else if (avail_len < field_size) { + /* + * We only report an error if the user didn't allocate + * enough space to hold the full value of this field. + */ + if (error_str != NULL) { + snprintf(error_str, error_str_len, "Available " + "length of attribute ID 0x%.4x %zu < field " + "length %u", scsi_2btoul(hdr->id), avail_len, + field_size); + } + retval = 1; + } + + return (retval); +} + +struct scsi_attrib_table_entry * +scsi_find_attrib_entry(struct scsi_attrib_table_entry *table, + size_t num_table_entries, uint32_t id) +{ + uint32_t i; + + for (i = 0; i < num_table_entries; i++) { + if (table[i].id == id) + return (&table[i]); + } + + return (NULL); +} + +struct scsi_attrib_table_entry * +scsi_get_attrib_entry(uint32_t id) +{ + return (scsi_find_attrib_entry(scsi_mam_attr_table, + sizeof(scsi_mam_attr_table) / sizeof(scsi_mam_attr_table[0]), + id)); +} + +int +scsi_attrib_value_sbuf(struct sbuf *sb, uint32_t valid_len, + struct scsi_mam_attribute_header *hdr, uint32_t output_flags, + char *error_str, size_t error_str_len) +{ + int retval; + + switch (hdr->byte2 & SMA_FORMAT_MASK) { + case SMA_FORMAT_ASCII: + retval = scsi_attrib_ascii_sbuf(sb, hdr, valid_len, + SCSI_ATTR_FLAG_NONE, output_flags, error_str,error_str_len); + break; + case SMA_FORMAT_BINARY: + if (scsi_2btoul(hdr->length) <= 8) + retval = scsi_attrib_int_sbuf(sb, hdr, valid_len, + SCSI_ATTR_FLAG_NONE, output_flags, error_str, + error_str_len); + else + retval = scsi_attrib_hexdump_sbuf(sb, hdr, valid_len, + SCSI_ATTR_FLAG_NONE, output_flags, error_str, + error_str_len); + break; + case SMA_FORMAT_TEXT: + retval = scsi_attrib_text_sbuf(sb, hdr, valid_len, + SCSI_ATTR_FLAG_NONE, output_flags, error_str, + error_str_len); + break; + default: + if (error_str != NULL) { + snprintf(error_str, error_str_len, "Unknown attribute " + "format 0x%x", hdr->byte2 & SMA_FORMAT_MASK); + } + retval = 1; + goto bailout; + break; /*NOTREACHED*/ + } + + sbuf_trim(sb); + +bailout: + + return (retval); +} + +void +scsi_attrib_prefix_sbuf(struct sbuf *sb, uint32_t output_flags, + struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, const char *desc) +{ + int need_space = 0; + uint32_t len; + uint32_t id; + + /* + * We can't do anything if we don't have enough valid data for the + * header. + */ + if (valid_len < sizeof(*hdr)) + return; + + id = scsi_2btoul(hdr->id); + /* + * Note that we print out the value of the attribute listed in the + * header, regardless of whether we actually got that many bytes + * back from the device through the controller. A truncated result + * could be the result of a failure to ask for enough data; the + * header indicates how many bytes are allocated for this attribute + * in the MAM. + */ + len = scsi_2btoul(hdr->length); + + if ((output_flags & SCSI_ATTR_OUTPUT_FIELD_MASK) == + SCSI_ATTR_OUTPUT_FIELD_NONE) + return; + + if ((output_flags & SCSI_ATTR_OUTPUT_FIELD_DESC) + && (desc != NULL)) { + sbuf_printf(sb, "%s", desc); + need_space = 1; + } + + if (output_flags & SCSI_ATTR_OUTPUT_FIELD_NUM) { + sbuf_printf(sb, "%s(0x%.4x)", (need_space) ? " " : "", id); + need_space = 0; + } + + if (output_flags & SCSI_ATTR_OUTPUT_FIELD_SIZE) { + sbuf_printf(sb, "%s[%d]", (need_space) ? " " : "", len); + need_space = 0; + } + if (output_flags & SCSI_ATTR_OUTPUT_FIELD_RW) { + sbuf_printf(sb, "%s(%s)", (need_space) ? " " : "", + (hdr->byte2 & SMA_READ_ONLY) ? "RO" : "RW"); + } + sbuf_printf(sb, ": "); +} + +int +scsi_attrib_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, struct scsi_attrib_table_entry *user_table, + size_t num_user_entries, int prefer_user_table, + uint32_t output_flags, char *error_str, int error_str_len) +{ + int retval; + struct scsi_attrib_table_entry *table1 = NULL, *table2 = NULL; + struct scsi_attrib_table_entry *entry = NULL; + size_t table1_size = 0, table2_size = 0; + uint32_t id; + + retval = 0; + + if (valid_len < sizeof(*hdr)) { + retval = 1; + goto bailout; + } + + id = scsi_2btoul(hdr->id); + + if (user_table != NULL) { + if (prefer_user_table != 0) { + table1 = user_table; + table1_size = num_user_entries; + table2 = scsi_mam_attr_table; + table2_size = sizeof(scsi_mam_attr_table) / + sizeof(scsi_mam_attr_table[0]); + } else { + table1 = scsi_mam_attr_table; + table1_size = sizeof(scsi_mam_attr_table) / + sizeof(scsi_mam_attr_table[0]); + table2 = user_table; + table2_size = num_user_entries; + } + } else { + table1 = scsi_mam_attr_table; + table1_size = sizeof(scsi_mam_attr_table) / + sizeof(scsi_mam_attr_table[0]); + } + + entry = scsi_find_attrib_entry(table1, table1_size, id); + if (entry != NULL) { + scsi_attrib_prefix_sbuf(sb, output_flags, hdr, valid_len, + entry->desc); + if (entry->to_str == NULL) + goto print_default; + retval = entry->to_str(sb, hdr, valid_len, entry->flags, + output_flags, error_str, error_str_len); + goto bailout; + } + if (table2 != NULL) { + entry = scsi_find_attrib_entry(table2, table2_size, id); + if (entry != NULL) { + if (entry->to_str == NULL) + goto print_default; + + scsi_attrib_prefix_sbuf(sb, output_flags, hdr, + valid_len, entry->desc); + retval = entry->to_str(sb, hdr, valid_len, entry->flags, + output_flags, error_str, + error_str_len); + goto bailout; + } + } + + scsi_attrib_prefix_sbuf(sb, output_flags, hdr, valid_len, NULL); + +print_default: + retval = scsi_attrib_value_sbuf(sb, valid_len, hdr, output_flags, + error_str, error_str_len); +bailout: + if (retval == 0) { + if ((entry != NULL) + && (entry->suffix != NULL)) + sbuf_printf(sb, " %s", entry->suffix); + + sbuf_trim(sb); + sbuf_printf(sb, "\n"); + } + + return (retval); +} + void scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), @@ -7404,6 +8228,74 @@ scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries, timeout); } +void +scsi_read_attribute(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, u_int8_t service_action, + uint32_t element, u_int8_t elem_type, int logical_volume, + int partition, u_int32_t first_attribute, int cache, + u_int8_t *data_ptr, u_int32_t length, int sense_len, + u_int32_t timeout) +{ + struct scsi_read_attribute *scsi_cmd; + + scsi_cmd = (struct scsi_read_attribute *)&csio->cdb_io.cdb_bytes; + bzero(scsi_cmd, sizeof(*scsi_cmd)); + + scsi_cmd->opcode = READ_ATTRIBUTE; + scsi_cmd->service_action = service_action, + scsi_ulto2b(element, scsi_cmd->element); + scsi_cmd->elem_type = elem_type; + scsi_cmd->logical_volume = logical_volume; + scsi_cmd->partition = partition; + scsi_ulto2b(first_attribute, scsi_cmd->first_attribute); + scsi_ulto4b(length, scsi_cmd->length); + if (cache != 0) + scsi_cmd->cache |= SRA_CACHE; + + 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_write_attribute(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, uint32_t element, int logical_volume, + int partition, int wtc, u_int8_t *data_ptr, + u_int32_t length, int sense_len, u_int32_t timeout) +{ + struct scsi_write_attribute *scsi_cmd; + + scsi_cmd = (struct scsi_write_attribute *)&csio->cdb_io.cdb_bytes; + bzero(scsi_cmd, sizeof(*scsi_cmd)); + + scsi_cmd->opcode = WRITE_ATTRIBUTE; + if (wtc != 0) + scsi_cmd->byte2 = SWA_WTC; + scsi_ulto3b(element, scsi_cmd->element); + scsi_cmd->logical_volume = logical_volume; + scsi_cmd->partition = partition; + scsi_ulto4b(length, scsi_cmd->length); + + cam_fill_csio(csio, + retries, + cbfcnp, + /*flags*/CAM_DIR_OUT, + tag_action, + /*data_ptr*/data_ptr, + /*dxfer_len*/length, + sense_len, + sizeof(*scsi_cmd), + timeout); +} void scsi_persistent_reserve_in(struct ccb_scsiio *csio, uint32_t retries, diff --git a/sys/cam/scsi/scsi_all.h b/sys/cam/scsi/scsi_all.h index f70b094..e446f6c 100644 --- a/sys/cam/scsi/scsi_all.h +++ b/sys/cam/scsi/scsi_all.h @@ -996,6 +996,216 @@ struct scsi_write_buffer u_int8_t control; }; +struct scsi_read_attribute +{ + u_int8_t opcode; + u_int8_t service_action; +#define SRA_SA_ATTR_VALUES 0x00 +#define SRA_SA_ATTR_LIST 0x01 +#define SRA_SA_LOG_VOL_LIST 0x02 +#define SRA_SA_PART_LIST 0x03 +#define SRA_SA_RESTRICTED 0x04 +#define SRA_SA_SUPPORTED_ATTRS 0x05 +#define SRA_SA_MASK 0x1f + u_int8_t element[2]; + u_int8_t elem_type; + u_int8_t logical_volume; + u_int8_t reserved1; + u_int8_t partition; + u_int8_t first_attribute[2]; + u_int8_t length[4]; + u_int8_t cache; +#define SRA_CACHE 0x01 + u_int8_t control; +}; + +struct scsi_write_attribute +{ + u_int8_t opcode; + u_int8_t byte2; +#define SWA_WTC 0x01 + u_int8_t element[3]; + u_int8_t logical_volume; + u_int8_t reserved1; + u_int8_t partition; + u_int8_t reserved2[2]; + u_int8_t length[4]; + u_int8_t reserved3; + u_int8_t control; +}; + + +struct scsi_read_attribute_values +{ + u_int8_t length[4]; + u_int8_t attribute_0[0]; +}; + +struct scsi_mam_attribute_header +{ + u_int8_t id[2]; + /* + * Attributes obtained from SPC-4r36g (section 7.4.2.2) and + * SSC-4r03 (section 4.2.21). + */ +#define SMA_ATTR_ID_DEVICE_MIN 0x0000 + +#define SMA_ATTR_REM_CAP_PARTITION 0x0000 +#define SMA_ATTR_MAX_CAP_PARTITION 0x0001 +#define SMA_ATTR_TAPEALERT_FLAGS 0x0002 +#define SMA_ATTR_LOAD_COUNT 0x0003 +#define SMA_ATTR_MAM_SPACE_REMAINING 0x0004 + +#define SMA_ATTR_DEV_ASSIGNING_ORG 0x0005 +#define SMA_ATTR_FORMAT_DENSITY_CODE 0x0006 +#define SMA_ATTR_INITIALIZATION_COUNT 0x0007 +#define SMA_ATTR_VOLUME_ID 0x0008 +#define SMA_ATTR_VOLUME_CHANGE_REF 0x0009 + +#define SMA_ATTR_DEV_SERIAL_LAST_LOAD 0x020a +#define SMA_ATTR_DEV_SERIAL_LAST_LOAD_1 0x020b +#define SMA_ATTR_DEV_SERIAL_LAST_LOAD_2 0x020c +#define SMA_ATTR_DEV_SERIAL_LAST_LOAD_3 0x020d + +#define SMA_ATTR_TOTAL_MB_WRITTEN_LT 0x0220 +#define SMA_ATTR_TOTAL_MB_READ_LT 0x0221 +#define SMA_ATTR_TOTAL_MB_WRITTEN_CUR 0x0222 +#define SMA_ATTR_TOTAL_MB_READ_CUR 0x0223 +#define SMA_ATTR_FIRST_ENC_BLOCK 0x0224 +#define SMA_ATTR_NEXT_UNENC_BLOCK 0x0225 + +#define SMA_ATTR_MEDIUM_USAGE_HIST 0x0340 +#define SMA_ATTR_PART_USAGE_HIST 0x0341 + +#define SMA_ATTR_ID_DEVICE_MAX 0x03ff + +#define SMA_ATTR_ID_MEDIUM_MIN 0x0400 + +#define SMA_ATTR_MED_MANUF 0x0400 +#define SMA_ATTR_MED_SERIAL 0x0401 + +#define SMA_ATTR_MED_LENGTH 0x0402 +#define SMA_ATTR_MED_WIDTH 0x0403 +#define SMA_ATTR_MED_ASSIGNING_ORG 0x0404 +#define SMA_ATTR_MED_DENSITY_CODE 0x0405 + +#define SMA_ATTR_MED_MANUF_DATE 0x0406 +#define SMA_ATTR_MAM_CAPACITY 0x0407 +#define SMA_ATTR_MED_TYPE 0x0408 +#define SMA_ATTR_MED_TYPE_INFO 0x0409 +#define SMA_ATTR_MED_SERIAL_NUM 0x040a + +#define SMA_ATTR_ID_MEDIUM_MAX 0x07ff + +#define SMA_ATTR_ID_HOST_MIN 0x0800 + +#define SMA_ATTR_APP_VENDOR 0x0800 +#define SMA_ATTR_APP_NAME 0x0801 +#define SMA_ATTR_APP_VERSION 0x0802 +#define SMA_ATTR_USER_MED_TEXT_LABEL 0x0803 +#define SMA_ATTR_LAST_WRITTEN_TIME 0x0804 +#define SMA_ATTR_TEXT_LOCAL_ID 0x0805 +#define SMA_ATTR_BARCODE 0x0806 +#define SMA_ATTR_HOST_OWNER_NAME 0x0807 +#define SMA_ATTR_MEDIA_POOL 0x0808 +#define SMA_ATTR_PART_USER_LABEL 0x0809 +#define SMA_ATTR_LOAD_UNLOAD_AT_PART 0x080a +#define SMA_ATTR_APP_FORMAT_VERSION 0x080b +#define SMA_ATTR_VOL_COHERENCY_INFO 0x080c + +#define SMA_ATTR_ID_HOST_MAX 0x0bff + +#define SMA_ATTR_VENDOR_DEVICE_MIN 0x0c00 +#define SMA_ATTR_VENDOR_DEVICE_MAX 0x0fff +#define SMA_ATTR_VENDOR_MEDIUM_MIN 0x1000 +#define SMA_ATTR_VENDOR_MEDIUM_MAX 0x13ff +#define SMA_ATTR_VENDOR_HOST_MIN 0x1400 +#define SMA_ATTR_VENDOR_HOST_MAX 0x17ff + u_int8_t byte2; +#define SMA_FORMAT_BINARY 0x00 +#define SMA_FORMAT_ASCII 0x01 +#define SMA_FORMAT_TEXT 0x02 +#define SMA_FORMAT_MASK 0x03 +#define SMA_READ_ONLY 0x80 + u_int8_t length[2]; + u_int8_t attribute[0]; +}; + +struct scsi_attrib_list_header { + u_int8_t length[4]; + u_int8_t first_attr_0[0]; +}; + +struct scsi_attrib_lv_list { + u_int8_t length[2]; + u_int8_t first_lv_number; + u_int8_t num_logical_volumes; +}; + +struct scsi_attrib_vendser { + uint8_t vendor[8]; + uint8_t serial_num[32]; +}; + +/* + * These values are used to decode the Volume Coherency Information + * Attribute (0x080c) for LTFS-format coherency information. + * Although the Application Client Specific lengths are different for + * Version 0 and Version 1, the data is in fact the same. The length + * difference was due to a code bug. + */ +#define SCSI_LTFS_VER0_LEN 42 +#define SCSI_LTFS_VER1_LEN 43 +#define SCSI_LTFS_UUID_LEN 36 +#define SCSI_LTFS_STR_NAME "LTFS" +#define SCSI_LTFS_STR_LEN 4 + +typedef enum { + SCSI_ATTR_FLAG_NONE = 0x00, + SCSI_ATTR_FLAG_HEX = 0x01, + SCSI_ATTR_FLAG_FP = 0x02, + SCSI_ATTR_FLAG_DIV_10 = 0x04, + SCSI_ATTR_FLAG_FP_1DIGIT = 0x08 +} scsi_attrib_flags; + +typedef enum { + SCSI_ATTR_OUTPUT_NONE = 0x00, + SCSI_ATTR_OUTPUT_TEXT_MASK = 0x03, + SCSI_ATTR_OUTPUT_TEXT_RAW = 0x00, + SCSI_ATTR_OUTPUT_TEXT_ESC = 0x01, + SCSI_ATTR_OUTPUT_TEXT_RSV1 = 0x02, + SCSI_ATTR_OUTPUT_TEXT_RSV2 = 0x03, + SCSI_ATTR_OUTPUT_NONASCII_MASK = 0x0c, + SCSI_ATTR_OUTPUT_NONASCII_TRIM = 0x00, + SCSI_ATTR_OUTPUT_NONASCII_ESC = 0x04, + SCSI_ATTR_OUTPUT_NONASCII_RAW = 0x08, + SCSI_ATTR_OUTPUT_NONASCII_RSV1 = 0x0c, + SCSI_ATTR_OUTPUT_FIELD_MASK = 0xf0, + SCSI_ATTR_OUTPUT_FIELD_ALL = 0xf0, + SCSI_ATTR_OUTPUT_FIELD_NONE = 0x00, + SCSI_ATTR_OUTPUT_FIELD_DESC = 0x10, + SCSI_ATTR_OUTPUT_FIELD_NUM = 0x20, + SCSI_ATTR_OUTPUT_FIELD_SIZE = 0x40, + SCSI_ATTR_OUTPUT_FIELD_RW = 0x80 +} scsi_attrib_output_flags; + +struct sbuf; + +struct scsi_attrib_table_entry +{ + u_int32_t id; + u_int32_t flags; + const char *desc; + const char *suffix; + int (*to_str)(struct sbuf *sb, struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len); + int (*parse_str)(char *str, struct scsi_mam_attribute_header *hdr, + uint32_t alloc_len, uint32_t flags, char *error_str, + int error_str_len); +}; + struct scsi_rw_6 { u_int8_t opcode; @@ -1750,6 +1960,8 @@ struct ata_pass_16 { #define READ_16 0x88 #define COMPARE_AND_WRITE 0x89 #define WRITE_16 0x8A +#define READ_ATTRIBUTE 0x8C +#define WRITE_ATTRIBUTE 0x8D #define WRITE_VERIFY_16 0x8E #define VERIFY_16 0x8F #define SYNCHRONIZE_CACHE_16 0x91 @@ -3272,8 +3484,6 @@ struct cam_device; extern const char *scsi_sense_key_text[]; -struct sbuf; - __BEGIN_DECLS void scsi_sense_desc(int sense_key, int asc, int ascq, struct scsi_inquiry_data *inq_data, @@ -3465,6 +3675,63 @@ int scsi_parse_transportid(char *transportid_str, #endif char *error_str, int error_str_len); + +int scsi_attrib_volcoh_sbuf(struct sbuf *sb, + struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len); + +int scsi_attrib_vendser_sbuf(struct sbuf *sb, + struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len); + +int scsi_attrib_hexdump_sbuf(struct sbuf *sb, + struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len); + +int scsi_attrib_int_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len); + +int scsi_attrib_ascii_sbuf(struct sbuf *sb, + struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len); + +int scsi_attrib_text_sbuf(struct sbuf *sb, + struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, uint32_t flags, + uint32_t output_flags, char *error_str, + int error_str_len); + +struct scsi_attrib_table_entry *scsi_find_attrib_entry( + struct scsi_attrib_table_entry *table, + size_t num_table_entries, uint32_t id); + +struct scsi_attrib_table_entry *scsi_get_attrib_entry(uint32_t id); + +int scsi_attrib_value_sbuf(struct sbuf *sb, uint32_t valid_len, + struct scsi_mam_attribute_header *hdr, + uint32_t output_flags, char *error_str, + size_t error_str_len); + +void scsi_attrib_prefix_sbuf(struct sbuf *sb, uint32_t output_flags, + struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, const char *desc); + +int scsi_attrib_sbuf(struct sbuf *sb, struct scsi_mam_attribute_header *hdr, + uint32_t valid_len, + struct scsi_attrib_table_entry *user_table, + size_t num_user_entries, int prefer_user_table, + uint32_t output_flags, char *error_str, int error_str_len); + void scsi_test_unit_ready(struct ccb_scsiio *csio, u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), @@ -3659,6 +3926,18 @@ void scsi_start_stop(struct ccb_scsiio *csio, u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), u_int8_t tag_action, int start, int load_eject, int immediate, u_int8_t sense_len, u_int32_t timeout); +void scsi_read_attribute(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, u_int8_t service_action, + uint32_t element, u_int8_t elem_type, + int logical_volume, int partition, + u_int32_t first_attribute, int cache, u_int8_t *data_ptr, + u_int32_t length, int sense_len, u_int32_t timeout); +void scsi_write_attribute(struct ccb_scsiio *csio, u_int32_t retries, + void (*cbfcnp)(struct cam_periph *, union ccb *), + u_int8_t tag_action, uint32_t element, + int logical_volume, int partition, int wtc, u_int8_t *data_ptr, + u_int32_t length, int 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 *), diff --git a/sys/kern/subr_prf.c b/sys/kern/subr_prf.c index 28e33a4..ebc08e7 100644 --- a/sys/kern/subr_prf.c +++ b/sys/kern/subr_prf.c @@ -37,10 +37,13 @@ #include <sys/cdefs.h> __FBSDID("$FreeBSD$"); +#ifdef _KERNEL #include "opt_ddb.h" #include "opt_printf.h" +#endif /* _KERNEL */ #include <sys/param.h> +#ifdef _KERNEL #include <sys/systm.h> #include <sys/lock.h> #include <sys/kdb.h> @@ -57,7 +60,9 @@ __FBSDID("$FreeBSD$"); #include <sys/syslog.h> #include <sys/cons.h> #include <sys/uio.h> +#endif #include <sys/ctype.h> +#include <sys/sbuf.h> #ifdef DDB #include <ddb/ddb.h> @@ -69,6 +74,8 @@ __FBSDID("$FreeBSD$"); */ #include <machine/stdarg.h> +#ifdef _KERNEL + #define TOCONS 0x01 #define TOTTY 0x02 #define TOLOG 0x04 @@ -1122,3 +1129,59 @@ hexdump(const void *ptr, int length, const char *hdr, int flags) printf("\n"); } } +#endif /* _KERNEL */ + +void +sbuf_hexdump(struct sbuf *sb, const void *ptr, int length, const char *hdr, + int flags) +{ + int i, j, k; + int cols; + const unsigned char *cp; + char delim; + + if ((flags & HD_DELIM_MASK) != 0) + delim = (flags & HD_DELIM_MASK) >> 8; + else + delim = ' '; + + if ((flags & HD_COLUMN_MASK) != 0) + cols = flags & HD_COLUMN_MASK; + else + cols = 16; + + cp = ptr; + for (i = 0; i < length; i+= cols) { + if (hdr != NULL) + sbuf_printf(sb, "%s", hdr); + + if ((flags & HD_OMIT_COUNT) == 0) + sbuf_printf(sb, "%04x ", i); + + if ((flags & HD_OMIT_HEX) == 0) { + for (j = 0; j < cols; j++) { + k = i + j; + if (k < length) + sbuf_printf(sb, "%c%02x", delim, cp[k]); + else + sbuf_printf(sb, " "); + } + } + + if ((flags & HD_OMIT_CHARS) == 0) { + sbuf_printf(sb, " |"); + for (j = 0; j < cols; j++) { + k = i + j; + if (k >= length) + sbuf_printf(sb, " "); + else if (cp[k] >= ' ' && cp[k] <= '~') + sbuf_printf(sb, "%c", cp[k]); + else + sbuf_printf(sb, "."); + } + sbuf_printf(sb, "|"); + } + sbuf_printf(sb, "\n"); + } +} + diff --git a/sys/sys/sbuf.h b/sys/sys/sbuf.h index 9816a4c..dbe2f68 100644 --- a/sys/sys/sbuf.h +++ b/sys/sys/sbuf.h @@ -57,6 +57,14 @@ struct sbuf { ssize_t s_sect_len; /* current length of section */ }; +#ifndef HD_COLUMN_MASK +#define HD_COLUMN_MASK 0xff +#define HD_DELIM_MASK 0xff00 +#define HD_OMIT_COUNT (1 << 16) +#define HD_OMIT_HEX (1 << 17) +#define HD_OMIT_CHARS (1 << 18) +#endif /* HD_COLUMN_MASK */ + __BEGIN_DECLS /* * API functions @@ -85,6 +93,8 @@ int sbuf_done(const struct sbuf *); void sbuf_delete(struct sbuf *); void sbuf_start_section(struct sbuf *, ssize_t *); ssize_t sbuf_end_section(struct sbuf *, ssize_t, size_t, int); +void sbuf_hexdump(struct sbuf *, const void *, int, const char *, + int); #ifdef _KERNEL struct uio; |