Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 9 Jun 2015 21:39:38 +0000 (UTC)
From:      "Kenneth D. Merry" <ken@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r284192 - in head: lib/libsbuf lib/libutil sbin/camcontrol share/man/man9 sys/cam sys/cam/scsi sys/kern sys/sys
Message-ID:  <201506092139.t59Ldcdp036361@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ken
Date: Tue Jun  9 21:39:38 2015
New Revision: 284192
URL: https://svnweb.freebsd.org/changeset/base/284192

Log:
  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
  MFC after:	1 week

Added:
  head/sbin/camcontrol/attrib.c   (contents, props changed)
Modified:
  head/lib/libsbuf/Makefile
  head/lib/libsbuf/Symbol.map
  head/lib/libsbuf/Version.def
  head/lib/libutil/hexdump.3
  head/sbin/camcontrol/Makefile
  head/sbin/camcontrol/camcontrol.8
  head/sbin/camcontrol/camcontrol.c
  head/sbin/camcontrol/camcontrol.h
  head/share/man/man9/sbuf.9
  head/sys/cam/cam.c
  head/sys/cam/cam.h
  head/sys/cam/scsi/scsi_all.c
  head/sys/cam/scsi/scsi_all.h
  head/sys/kern/subr_prf.c
  head/sys/sys/sbuf.h

Modified: head/lib/libsbuf/Makefile
==============================================================================
--- head/lib/libsbuf/Makefile	Tue Jun  9 19:41:16 2015	(r284191)
+++ head/lib/libsbuf/Makefile	Tue Jun  9 21:39:38 2015	(r284192)
@@ -2,7 +2,7 @@
 
 LIB=		sbuf
 SHLIBDIR?= 	/lib
-SRCS=		subr_sbuf.c 
+SRCS=		subr_prf.c subr_sbuf.c
 
 SHLIB_MAJOR	= 6
 

Modified: head/lib/libsbuf/Symbol.map
==============================================================================
--- head/lib/libsbuf/Symbol.map	Tue Jun  9 19:41:16 2015	(r284191)
+++ head/lib/libsbuf/Symbol.map	Tue Jun  9 21:39:38 2015	(r284192)
@@ -27,3 +27,7 @@ FBSD_1.3 {
 	sbuf_start_section;
 	sbuf_end_section;
 };
+
+FBSD_1.4 {
+	sbuf_hexdump;
+};

Modified: head/lib/libsbuf/Version.def
==============================================================================
--- head/lib/libsbuf/Version.def	Tue Jun  9 19:41:16 2015	(r284191)
+++ head/lib/libsbuf/Version.def	Tue Jun  9 21:39:38 2015	(r284192)
@@ -5,3 +5,6 @@ FBSD_1.2 {
 
 FBSD_1.3 {
 } FBSD_1.2;
+
+FBSD_1.4 {
+} FBSD_1.3;

Modified: head/lib/libutil/hexdump.3
==============================================================================
--- head/lib/libutil/hexdump.3	Tue Jun  9 19:41:16 2015	(r284191)
+++ head/lib/libutil/hexdump.3	Tue Jun  9 21:39:38 2015	(r284192)
@@ -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
 .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 eac
 .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 .

Modified: head/sbin/camcontrol/Makefile
==============================================================================
--- head/sbin/camcontrol/Makefile	Tue Jun  9 19:41:16 2015	(r284191)
+++ head/sbin/camcontrol/Makefile	Tue Jun  9 21:39:38 2015	(r284192)
@@ -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

Added: head/sbin/camcontrol/attrib.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/sbin/camcontrol/attrib.c	Tue Jun  9 21:39:38 2015	(r284192)
@@ -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);
+}

Modified: head/sbin/camcontrol/camcontrol.8
==============================================================================
--- head/sbin/camcontrol/camcontrol.8	Tue Jun  9 19:41:16 2015	(r284191)
+++ head/sbin/camcontrol/camcontrol.8	Tue Jun  9 21:39:38 2015	(r284192)
@@ -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
@@ -1822,6 +1835,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
@@ -2047,6 +2183,15 @@ current initiator will be unregistered f
 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 ,

Modified: head/sbin/camcontrol/camcontrol.c
==============================================================================
--- head/sbin/camcontrol/camcontrol.c	Tue Jun  9 19:41:16 2015	(r284191)
+++ head/sbin/camcontrol/camcontrol.c	Tue Jun  9 21:39:38 2015	(r284192)
@@ -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_tab
 	{"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},
@@ -8117,6 +8119,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)
@@ -8156,6 +8161,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"
@@ -8306,6 +8312,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 */
 }
@@ -8650,6 +8670,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);

Modified: head/sbin/camcontrol/camcontrol.h
==============================================================================
--- head/sbin/camcontrol/camcontrol.h	Tue Jun  9 19:41:16 2015	(r284191)
+++ head/sbin/camcontrol/camcontrol.h	Tue Jun  9 21:39:38 2015	(r284192)
@@ -66,6 +66,9 @@ int scsidoinquiry(struct cam_device *dev
 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);

Modified: head/share/man/man9/sbuf.9
==============================================================================
--- head/share/man/man9/sbuf.9	Tue Jun  9 19:41:16 2015	(r284191)
+++ head/share/man/man9/sbuf.9	Tue Jun  9 21:39:38 2015	(r284192)
@@ -56,7 +56,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
@@ -115,6 +116,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"
@@ -455,6 +464,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
@@ -558,6 +575,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 ,

Modified: head/sys/cam/cam.c
==============================================================================
--- head/sys/cam/cam.c	Tue Jun  9 19:41:16 2015	(r284191)
+++ head/sys/cam/cam.c	Tue Jun  9 21:39:38 2015	(r284192)
@@ -157,6 +157,56 @@ cam_strvis(u_int8_t *dst, const u_int8_t
 	*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,

Modified: head/sys/cam/cam.h
==============================================================================
--- head/sys/cam/cam.h	Tue Jun  9 19:41:16 2015	(r284191)
+++ head/sys/cam/cam.h	Tue Jun  9 21:39:38 2015	(r284192)
@@ -342,6 +342,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;
@@ -354,6 +363,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);
@@ -366,6 +376,8 @@ caddr_t	cam_quirkmatch(caddr_t target, c
 		       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*

Modified: head/sys/cam/scsi/scsi_all.c
==============================================================================
--- head/sys/cam/scsi/scsi_all.c	Tue Jun  9 19:41:16 2015	(r284191)

*** DIFF OUTPUT TRUNCATED AT 1000 LINES ***



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201506092139.t59Ldcdp036361>