2 * Copyright (c) 2014 Spectra Logic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * substantially similar to the "NO WARRANTY" disclaimer below
13 * ("Disclaimer") and any redistribution must be conditioned upon
14 * including a substantially similar Disclaimer requirement for further
15 * binary redistribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
30 * Authors: Ken Merry (Spectra Logic Corporation)
33 * SCSI Read and Write Attribute support for camcontrol(8).
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include <sys/ioctl.h>
40 #include <sys/stdint.h>
41 #include <sys/types.h>
42 #include <sys/endian.h>
44 #include <sys/queue.h>
60 #include <cam/cam_debug.h>
61 #include <cam/cam_ccb.h>
62 #include <cam/scsi/scsi_all.h>
63 #include <cam/scsi/scsi_pass.h>
64 #include <cam/scsi/scsi_ch.h>
65 #include <cam/scsi/scsi_message.h>
67 #include "camcontrol.h"
70 struct scsi_attr_desc {
73 STAILQ_ENTRY(scsi_attr_desc) links;
77 static struct scsi_nv elem_type_map[] = {
78 { "all", ELEMENT_TYPE_ALL },
79 { "picker", ELEMENT_TYPE_MT },
80 { "slot", ELEMENT_TYPE_ST },
81 { "portal", ELEMENT_TYPE_IE },
82 { "drive", ELEMENT_TYPE_DT },
85 static struct scsi_nv sa_map[] = {
86 { "attr_values", SRA_SA_ATTR_VALUES },
87 { "attr_list", SRA_SA_ATTR_LIST },
88 { "lv_list", SRA_SA_LOG_VOL_LIST },
89 { "part_list", SRA_SA_PART_LIST },
90 { "supp_attr", SRA_SA_SUPPORTED_ATTRS }
93 static struct scsi_nv output_format_map[] = {
94 { "text_esc", SCSI_ATTR_OUTPUT_TEXT_ESC },
95 { "text_raw", SCSI_ATTR_OUTPUT_TEXT_RAW },
96 { "nonascii_esc", SCSI_ATTR_OUTPUT_NONASCII_ESC },
97 { "nonascii_trim", SCSI_ATTR_OUTPUT_NONASCII_TRIM },
98 { "nonascii_raw", SCSI_ATTR_OUTPUT_NONASCII_RAW },
99 { "field_all", SCSI_ATTR_OUTPUT_FIELD_ALL },
100 { "field_none", SCSI_ATTR_OUTPUT_FIELD_NONE },
101 { "field_desc", SCSI_ATTR_OUTPUT_FIELD_DESC },
102 { "field_num", SCSI_ATTR_OUTPUT_FIELD_NUM },
103 { "field_size", SCSI_ATTR_OUTPUT_FIELD_SIZE },
104 { "field_rw", SCSI_ATTR_OUTPUT_FIELD_RW },
108 scsiattrib(struct cam_device *device, int argc, char **argv, char *combinedopt,
109 int task_attr, int retry_count, int timeout, int verbosemode,
112 union ccb *ccb = NULL;
119 int read_service_action = -1;
120 int read_attr = 0, write_attr = 0;
121 int element_address = 0;
122 int element_type = ELEMENT_TYPE_ALL;
124 int logical_volume = 0;
126 uint8_t *data_buf = NULL;
127 uint32_t dxfer_len = UINT16_MAX - 1;
129 uint32_t output_format;
130 STAILQ_HEAD(, scsi_attr_desc) write_attr_list;
134 ccb = cam_getccb(device);
136 warnx("%s: error allocating CCB", __func__);
141 STAILQ_INIT(&write_attr_list);
144 * By default, when displaying attribute values, we trim out
145 * non-ASCII characters in ASCII fields. We display all fields
146 * (description, attribute number, attribute size, and readonly
147 * status). We default to displaying raw text.
149 * XXX KDM need to port this to stable/10 and newer FreeBSD
150 * versions that have iconv built in and can convert codesets.
152 output_format = SCSI_ATTR_OUTPUT_NONASCII_TRIM |
153 SCSI_ATTR_OUTPUT_FIELD_ALL |
154 SCSI_ATTR_OUTPUT_TEXT_RAW;
156 data_buf = malloc(dxfer_len);
157 if (data_buf == NULL) {
158 warn("%s: error allocating %u bytes", __func__, dxfer_len);
163 while ((c = getopt(argc, argv, combinedopt)) != -1) {
166 attr_num = strtol(optarg, &endptr, 0);
167 if (*endptr != '\0') {
168 warnx("%s: invalid attribute number %s",
173 start_attr = attr_num;
179 element_address = strtol(optarg, &endptr, 0);
180 if (*endptr != '\0') {
181 warnx("%s: invalid element address %s",
188 scsi_nv_status status;
189 scsi_attrib_output_flags new_outflags;
193 if (isdigit(optarg[0])) {
194 output_format = strtoul(optarg, &endptr, 0);
195 if (*endptr != '\0') {
196 warnx("%s: invalid numeric output "
197 "format argument %s", __func__,
204 new_outflags = SCSI_ATTR_OUTPUT_NONE;
206 while ((tmpstr = strsep(&optarg, ",")) != NULL) {
207 status = scsi_get_nv(output_format_map,
208 sizeof(output_format_map) /
209 sizeof(output_format_map[0]), tmpstr,
210 &entry_num, SCSI_NV_FLAG_IG_CASE);
212 if (status == SCSI_NV_FOUND)
214 output_format_map[entry_num].value;
216 warnx("%s: %s format option %s",
218 (status == SCSI_NV_AMBIGUOUS) ?
219 "ambiguous" : "invalid", tmpstr);
224 output_format = new_outflags;
228 partition = strtol(optarg, &endptr, 0);
229 if (*endptr != '\0') {
230 warnx("%s: invalid partition number %s",
237 scsi_nv_status status;
240 status = scsi_get_nv(sa_map, sizeof(sa_map) /
241 sizeof(sa_map[0]), optarg, &entry_num,
242 SCSI_NV_FLAG_IG_CASE);
243 if (status == SCSI_NV_FOUND)
244 read_service_action = sa_map[entry_num].value;
246 warnx("%s: %s %s option %s", __func__,
247 (status == SCSI_NV_AMBIGUOUS) ?
248 "ambiguous" : "invalid", "service action",
257 start_attr = strtol(optarg, &endptr, 0);
258 if (*endptr != '\0') {
259 warnx("%s: invalid starting attr argument %s",
266 scsi_nv_status status;
269 status = scsi_get_nv(elem_type_map,
270 sizeof(elem_type_map) / sizeof(elem_type_map[0]),
271 optarg, &entry_num, SCSI_NV_FLAG_IG_CASE);
272 if (status == SCSI_NV_FOUND)
273 element_type = elem_type_map[entry_num].value;
275 warnx("%s: %s %s option %s", __func__,
276 (status == SCSI_NV_AMBIGUOUS) ?
277 "ambiguous" : "invalid", "element type",
285 warnx("%s: writing attributes is not implemented yet",
291 logical_volume = strtol(optarg, &endptr, 0);
293 if (*endptr != '\0') {
294 warnx("%s: invalid logical volume argument %s",
306 * Default to reading attributes
308 if (((read_attr == 0) && (write_attr == 0))
309 || ((read_attr != 0) && (write_attr != 0))) {
310 warnx("%s: Must specify either -r or -w", __func__);
315 if (read_attr != 0) {
316 scsi_read_attribute(&ccb->csio,
317 /*retries*/ retry_count,
319 /*tag_action*/ task_attr,
320 /*service_action*/ read_service_action,
321 /*element*/ element_address,
322 /*elem_type*/ element_type,
323 /*logical_volume*/ logical_volume,
324 /*partition*/ partition,
325 /*first_attribute*/ start_attr,
326 /*cache*/ cached_attr,
327 /*data_ptr*/ data_buf,
328 /*length*/ dxfer_len,
329 /*sense_len*/ SSD_FULL_SIZE,
330 /*timeout*/ timeout ? timeout : 60000);
337 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
339 if (err_recover != 0)
340 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
342 if (cam_send_ccb(device, ccb) < 0) {
343 warn("error sending %s ATTRIBUTE", (read_attr != 0) ?
349 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
350 if (verbosemode != 0) {
351 cam_error_print(device, ccb, CAM_ESF_ALL,
352 CAM_EPF_ALL, stderr);
361 valid_len = dxfer_len - ccb->csio.resid;
363 switch (read_service_action) {
364 case SRA_SA_ATTR_VALUES: {
365 uint32_t len_left, hdr_len, cur_len;
366 struct scsi_read_attribute_values *hdr;
367 struct scsi_mam_attribute_header *cur_id;
372 hdr = (struct scsi_read_attribute_values *)data_buf;
374 if (valid_len < sizeof(*hdr)) {
375 fprintf(stdout, "No attributes returned.\n");
380 sb = sbuf_new_auto();
382 warn("%s: Unable to allocate sbuf", __func__);
387 * XXX KDM grab more data if it is available.
389 hdr_len = scsi_4btoul(hdr->length);
391 for (len_left = MIN(valid_len, hdr_len),
392 cur_pos = &hdr->attribute_0[0]; len_left > sizeof(*cur_id);
393 len_left -= cur_len, cur_pos += cur_len) {
395 cur_id = (struct scsi_mam_attribute_header *)cur_pos;
396 cur_len = scsi_2btoul(cur_id->length) + sizeof(*cur_id);
397 cur_attr_num = scsi_2btoul(cur_id->id);
400 && (cur_attr_num != attr_num))
403 error = scsi_attrib_sbuf(sb, cur_id, len_left,
404 /*user_table*/ NULL, /*num_user_entries*/ 0,
405 /*prefer_user_table*/ 0, output_format, error_str,
408 warnx("%s: %s", __func__, error_str);
418 fprintf(stdout, "%s", sbuf_data(sb));
422 case SRA_SA_SUPPORTED_ATTRS:
423 case SRA_SA_ATTR_LIST: {
424 uint32_t len_left, hdr_len;
425 struct scsi_attrib_list_header *hdr;
426 struct scsi_attrib_table_entry *entry = NULL;
427 const char *sa_name = "Supported Attributes";
428 const char *at_name = "Available Attributes";
432 hdr = (struct scsi_attrib_list_header *)data_buf;
433 if (valid_len < sizeof(*hdr)) {
434 fprintf(stdout, "No %s\n",
435 (read_service_action == SRA_SA_SUPPORTED_ATTRS)?
440 fprintf(stdout, "%s:\n",
441 (read_service_action == SRA_SA_SUPPORTED_ATTRS) ?
443 hdr_len = scsi_4btoul(hdr->length);
444 for (len_left = MIN(valid_len, hdr_len),
445 cur_id = &hdr->first_attr_0[0]; len_left > 1;
446 len_left -= sizeof(uint16_t), cur_id += sizeof(uint16_t)) {
447 attr_id = scsi_2btoul(cur_id);
450 && (attr_id != attr_num))
453 entry = scsi_get_attrib_entry(attr_id);
454 fprintf(stdout, "0x%.4x", attr_id);
456 fprintf(stdout, "\n");
458 fprintf(stdout, ": %s\n", entry->desc);
465 case SRA_SA_PART_LIST:
466 case SRA_SA_LOG_VOL_LIST: {
467 struct scsi_attrib_lv_list *lv_list;
468 const char *partition_name = "Partition";
469 const char *lv_name = "Logical Volume";
471 if (valid_len < sizeof(*lv_list)) {
472 fprintf(stdout, "No %s list returned\n",
473 (read_service_action == SRA_SA_PART_LIST) ?
474 partition_name : lv_name);
479 lv_list = (struct scsi_attrib_lv_list *)data_buf;
481 fprintf(stdout, "First %s: %d\n",
482 (read_service_action == SRA_SA_PART_LIST) ?
483 partition_name : lv_name,
484 lv_list->first_lv_number);
485 fprintf(stdout, "Number of %ss: %d\n",
486 (read_service_action == SRA_SA_PART_LIST) ?
487 partition_name : lv_name,
488 lv_list->num_logical_volumes);