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 retry_count, int timeout, int verbosemode, int err_recover)
111 union ccb *ccb = NULL;
118 int read_service_action = -1;
119 int read_attr = 0, write_attr = 0;
120 int element_address = 0;
121 int element_type = ELEMENT_TYPE_ALL;
123 int logical_volume = 0;
125 uint8_t *data_buf = NULL;
126 uint32_t dxfer_len = UINT16_MAX - 1;
128 uint32_t output_format;
129 STAILQ_HEAD(, scsi_attr_desc) write_attr_list;
133 ccb = cam_getccb(device);
135 warnx("%s: error allocating CCB", __func__);
140 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
142 STAILQ_INIT(&write_attr_list);
145 * By default, when displaying attribute values, we trim out
146 * non-ASCII characters in ASCII fields. We display all fields
147 * (description, attribute number, attribute size, and readonly
148 * status). We default to displaying raw text.
150 * XXX KDM need to port this to stable/10 and newer FreeBSD
151 * versions that have iconv built in and can convert codesets.
153 output_format = SCSI_ATTR_OUTPUT_NONASCII_TRIM |
154 SCSI_ATTR_OUTPUT_FIELD_ALL |
155 SCSI_ATTR_OUTPUT_TEXT_RAW;
157 data_buf = malloc(dxfer_len);
158 if (data_buf == NULL) {
159 warn("%s: error allocating %u bytes", __func__, dxfer_len);
164 while ((c = getopt(argc, argv, combinedopt)) != -1) {
167 attr_num = strtol(optarg, &endptr, 0);
168 if (*endptr != '\0') {
169 warnx("%s: invalid attribute number %s",
174 start_attr = attr_num;
180 element_address = strtol(optarg, &endptr, 0);
181 if (*endptr != '\0') {
182 warnx("%s: invalid element address %s",
189 scsi_nv_status status;
190 scsi_attrib_output_flags new_outflags;
194 if (isdigit(optarg[0])) {
195 output_format = strtoul(optarg, &endptr, 0);
196 if (*endptr != '\0') {
197 warnx("%s: invalid numeric output "
198 "format argument %s", __func__,
205 new_outflags = SCSI_ATTR_OUTPUT_NONE;
207 while ((tmpstr = strsep(&optarg, ",")) != NULL) {
208 status = scsi_get_nv(output_format_map,
209 sizeof(output_format_map) /
210 sizeof(output_format_map[0]), tmpstr,
211 &entry_num, SCSI_NV_FLAG_IG_CASE);
213 if (status == SCSI_NV_FOUND)
215 output_format_map[entry_num].value;
217 warnx("%s: %s format option %s",
219 (status == SCSI_NV_AMBIGUOUS) ?
220 "ambiguous" : "invalid", tmpstr);
225 output_format = new_outflags;
229 partition = strtol(optarg, &endptr, 0);
230 if (*endptr != '\0') {
231 warnx("%s: invalid partition number %s",
238 scsi_nv_status status;
241 status = scsi_get_nv(sa_map, sizeof(sa_map) /
242 sizeof(sa_map[0]), optarg, &entry_num,
243 SCSI_NV_FLAG_IG_CASE);
244 if (status == SCSI_NV_FOUND)
245 read_service_action = sa_map[entry_num].value;
247 warnx("%s: %s %s option %s", __func__,
248 (status == SCSI_NV_AMBIGUOUS) ?
249 "ambiguous" : "invalid", "service action",
258 start_attr = strtol(optarg, &endptr, 0);
259 if (*endptr != '\0') {
260 warnx("%s: invalid starting attr argument %s",
267 scsi_nv_status status;
270 status = scsi_get_nv(elem_type_map,
271 sizeof(elem_type_map) / sizeof(elem_type_map[0]),
272 optarg, &entry_num, SCSI_NV_FLAG_IG_CASE);
273 if (status == SCSI_NV_FOUND)
274 element_type = elem_type_map[entry_num].value;
276 warnx("%s: %s %s option %s", __func__,
277 (status == SCSI_NV_AMBIGUOUS) ?
278 "ambiguous" : "invalid", "element type",
286 warnx("%s: writing attributes is not implemented yet",
292 logical_volume = strtol(optarg, &endptr, 0);
294 if (*endptr != '\0') {
295 warnx("%s: invalid logical volume argument %s",
307 * Default to reading attributes
309 if (((read_attr == 0) && (write_attr == 0))
310 || ((read_attr != 0) && (write_attr != 0))) {
311 warnx("%s: Must specify either -r or -w", __func__);
316 if (read_attr != 0) {
317 scsi_read_attribute(&ccb->csio,
318 /*retries*/ retry_count,
320 /*tag_action*/ MSG_SIMPLE_Q_TAG,
321 /*service_action*/ read_service_action,
322 /*element*/ element_address,
323 /*elem_type*/ element_type,
324 /*logical_volume*/ logical_volume,
325 /*partition*/ partition,
326 /*first_attribute*/ start_attr,
327 /*cache*/ cached_attr,
328 /*data_ptr*/ data_buf,
329 /*length*/ dxfer_len,
330 /*sense_len*/ SSD_FULL_SIZE,
331 /*timeout*/ timeout ? timeout : 60000);
338 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
340 if (err_recover != 0)
341 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
343 if (cam_send_ccb(device, ccb) < 0) {
344 warn("error sending %s ATTRIBUTE", (read_attr != 0) ?
347 if (verbosemode != 0) {
348 cam_error_print(device, ccb, CAM_ESF_ALL,
349 CAM_EPF_ALL, stderr);
356 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
357 if (verbosemode != 0) {
358 cam_error_print(device, ccb, CAM_ESF_ALL,
359 CAM_EPF_ALL, stderr);
368 valid_len = dxfer_len - ccb->csio.resid;
370 switch (read_service_action) {
371 case SRA_SA_ATTR_VALUES: {
372 uint32_t len_left, hdr_len, cur_len;
373 struct scsi_read_attribute_values *hdr;
374 struct scsi_mam_attribute_header *cur_id;
379 hdr = (struct scsi_read_attribute_values *)data_buf;
381 if (valid_len < sizeof(*hdr)) {
382 fprintf(stdout, "No attributes returned.\n");
387 sb = sbuf_new_auto();
389 warn("%s: Unable to allocate sbuf", __func__);
394 * XXX KDM grab more data if it is available.
396 hdr_len = scsi_4btoul(hdr->length);
398 for (len_left = MIN(valid_len, hdr_len),
399 cur_pos = &hdr->attribute_0[0]; len_left > sizeof(*cur_id);
400 len_left -= cur_len, cur_pos += cur_len) {
402 cur_id = (struct scsi_mam_attribute_header *)cur_pos;
403 cur_len = scsi_2btoul(cur_id->length) + sizeof(*cur_id);
404 cur_attr_num = scsi_2btoul(cur_id->id);
407 && (cur_attr_num != attr_num))
410 error = scsi_attrib_sbuf(sb, cur_id, len_left,
411 /*user_table*/ NULL, /*num_user_entries*/ 0,
412 /*prefer_user_table*/ 0, output_format, error_str,
415 warnx("%s: %s", __func__, error_str);
425 fprintf(stdout, "%s", sbuf_data(sb));
429 case SRA_SA_SUPPORTED_ATTRS:
430 case SRA_SA_ATTR_LIST: {
431 uint32_t len_left, hdr_len;
432 struct scsi_attrib_list_header *hdr;
433 struct scsi_attrib_table_entry *entry = NULL;
434 const char *sa_name = "Supported Attributes";
435 const char *at_name = "Available Attributes";
439 hdr = (struct scsi_attrib_list_header *)data_buf;
440 if (valid_len < sizeof(*hdr)) {
441 fprintf(stdout, "No %s\n",
442 (read_service_action == SRA_SA_SUPPORTED_ATTRS)?
447 fprintf(stdout, "%s:\n",
448 (read_service_action == SRA_SA_SUPPORTED_ATTRS) ?
450 hdr_len = scsi_4btoul(hdr->length);
451 for (len_left = MIN(valid_len, hdr_len),
452 cur_id = &hdr->first_attr_0[0]; len_left > 1;
453 len_left -= sizeof(uint16_t), cur_id += sizeof(uint16_t)) {
454 attr_id = scsi_2btoul(cur_id);
457 && (attr_id != attr_num))
460 entry = scsi_get_attrib_entry(attr_id);
461 fprintf(stdout, "0x%.4x", attr_id);
463 fprintf(stdout, "\n");
465 fprintf(stdout, ": %s\n", entry->desc);
472 case SRA_SA_PART_LIST:
473 case SRA_SA_LOG_VOL_LIST: {
474 struct scsi_attrib_lv_list *lv_list;
475 const char *partition_name = "Partition";
476 const char *lv_name = "Logical Volume";
478 if (valid_len < sizeof(*lv_list)) {
479 fprintf(stdout, "No %s list returned\n",
480 (read_service_action == SRA_SA_PART_LIST) ?
481 partition_name : lv_name);
486 lv_list = (struct scsi_attrib_lv_list *)data_buf;
488 fprintf(stdout, "First %s: %d\n",
489 (read_service_action == SRA_SA_PART_LIST) ?
490 partition_name : lv_name,
491 lv_list->first_lv_number);
492 fprintf(stdout, "Number of %ss: %d\n",
493 (read_service_action == SRA_SA_PART_LIST) ?
494 partition_name : lv_name,
495 lv_list->num_logical_volumes);