ctladm.c revision 232604
1/*-
2 * Copyright (c) 2003, 2004 Silicon Graphics International Corp.
3 * Copyright (c) 1997-2007 Kenneth D. Merry
4 * Copyright (c) 2012 The FreeBSD Foundation
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions, and the following disclaimer,
15 *    without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 *    substantially similar to the "NO WARRANTY" disclaimer below
18 *    ("Disclaimer") and any redistribution must be conditioned upon
19 *    including a substantially similar Disclaimer requirement for further
20 *    binary redistribution.
21 *
22 * NO WARRANTY
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
34 *
35 * $Id: //depot/users/kenm/FreeBSD-test2/usr.sbin/ctladm/ctladm.c#4 $
36 */
37/*
38 * CAM Target Layer exercise program.
39 *
40 * Author: Ken Merry <ken@FreeBSD.org>
41 */
42
43#include <sys/cdefs.h>
44__FBSDID("$FreeBSD: head/usr.sbin/ctladm/ctladm.c 232604 2012-03-06 13:43:57Z trasz $");
45
46#include <sys/ioctl.h>
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <sys/param.h>
50#include <sys/queue.h>
51#include <sys/callout.h>
52#include <sys/sbuf.h>
53#include <stdint.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <unistd.h>
57#include <fcntl.h>
58#include <getopt.h>
59#include <string.h>
60#include <errno.h>
61#include <err.h>
62#include <ctype.h>
63#include <bsdxml.h>
64#include <cam/scsi/scsi_all.h>
65#include <cam/scsi/scsi_message.h>
66#include <cam/ctl/ctl.h>
67#include <cam/ctl/ctl_io.h>
68#include <cam/ctl/ctl_frontend_internal.h>
69#include <cam/ctl/ctl_backend.h>
70#include <cam/ctl/ctl_ioctl.h>
71#include <cam/ctl/ctl_backend_block.h>
72#include <cam/ctl/ctl_util.h>
73#include <cam/ctl/ctl_scsi_all.h>
74#include <camlib.h>
75#include <libutil.h>
76#include "ctladm.h"
77
78#ifdef min
79#undef min
80#endif
81#define min(x,y) (x < y) ? x : y
82
83typedef enum {
84	CTLADM_CMD_TUR,
85	CTLADM_CMD_INQUIRY,
86	CTLADM_CMD_REQ_SENSE,
87	CTLADM_CMD_ARRAYLIST,
88	CTLADM_CMD_REPORT_LUNS,
89	CTLADM_CMD_HELP,
90	CTLADM_CMD_DEVLIST,
91	CTLADM_CMD_ADDDEV,
92	CTLADM_CMD_RM,
93	CTLADM_CMD_CREATE,
94	CTLADM_CMD_READ,
95	CTLADM_CMD_WRITE,
96	CTLADM_CMD_PORT,
97	CTLADM_CMD_READCAPACITY,
98	CTLADM_CMD_MODESENSE,
99	CTLADM_CMD_DUMPOOA,
100	CTLADM_CMD_DUMPSTRUCTS,
101	CTLADM_CMD_START,
102	CTLADM_CMD_STOP,
103	CTLADM_CMD_SYNC_CACHE,
104	CTLADM_CMD_SHUTDOWN,
105	CTLADM_CMD_STARTUP,
106	CTLADM_CMD_LUNLIST,
107	CTLADM_CMD_HARDSTOP,
108	CTLADM_CMD_HARDSTART,
109	CTLADM_CMD_DELAY,
110	CTLADM_CMD_REALSYNC,
111	CTLADM_CMD_SETSYNC,
112	CTLADM_CMD_GETSYNC,
113	CTLADM_CMD_ERR_INJECT,
114	CTLADM_CMD_BBRREAD,
115	CTLADM_CMD_PRES_IN,
116	CTLADM_CMD_PRES_OUT,
117	CTLADM_CMD_INQ_VPD_DEVID,
118	CTLADM_CMD_RTPG,
119	CTLADM_CMD_MODIFY
120} ctladm_cmdfunction;
121
122typedef enum {
123	CTLADM_ARG_NONE		= 0x0000000,
124	CTLADM_ARG_AUTOSENSE	= 0x0000001,
125	CTLADM_ARG_DEVICE	= 0x0000002,
126	CTLADM_ARG_ARRAYSIZE	= 0x0000004,
127	CTLADM_ARG_BACKEND	= 0x0000008,
128	CTLADM_ARG_CDBSIZE	= 0x0000010,
129	CTLADM_ARG_DATALEN	= 0x0000020,
130	CTLADM_ARG_FILENAME	= 0x0000040,
131	CTLADM_ARG_LBA		= 0x0000080,
132	CTLADM_ARG_PC		= 0x0000100,
133	CTLADM_ARG_PAGE_CODE	= 0x0000200,
134	CTLADM_ARG_PAGE_LIST	= 0x0000400,
135	CTLADM_ARG_SUBPAGE	= 0x0000800,
136	CTLADM_ARG_PAGELIST	= 0x0001000,
137	CTLADM_ARG_DBD		= 0x0002000,
138	CTLADM_ARG_TARG_LUN	= 0x0004000,
139	CTLADM_ARG_BLOCKSIZE	= 0x0008000,
140	CTLADM_ARG_IMMED	= 0x0010000,
141	CTLADM_ARG_RELADR	= 0x0020000,
142	CTLADM_ARG_RETRIES	= 0x0040000,
143	CTLADM_ARG_ONOFFLINE	= 0x0080000,
144	CTLADM_ARG_ONESHOT	= 0x0100000,
145	CTLADM_ARG_TIMEOUT	= 0x0200000,
146	CTLADM_ARG_INITIATOR 	= 0x0400000,
147	CTLADM_ARG_NOCOPY	= 0x0800000,
148	CTLADM_ARG_NEED_TL	= 0x1000000
149} ctladm_cmdargs;
150
151struct ctladm_opts {
152	const char	*optname;
153	uint32_t	cmdnum;
154	ctladm_cmdargs	argnum;
155	const char	*subopt;
156};
157
158typedef enum {
159	CC_OR_NOT_FOUND,
160	CC_OR_AMBIGUOUS,
161	CC_OR_FOUND
162} ctladm_optret;
163
164static const char rw_opts[] = "Nb:c:d:f:l:";
165static const char startstop_opts[] = "io";
166
167struct ctladm_opts option_table[] = {
168	{"adddev", CTLADM_CMD_ADDDEV, CTLADM_ARG_NONE, NULL},
169	{"bbrread", CTLADM_CMD_BBRREAD, CTLADM_ARG_NEED_TL, "d:l:"},
170	{"create", CTLADM_CMD_CREATE, CTLADM_ARG_NONE, "b:B:d:l:o:s:S:t:"},
171	{"delay", CTLADM_CMD_DELAY, CTLADM_ARG_NEED_TL, "T:l:t:"},
172	{"devid", CTLADM_CMD_INQ_VPD_DEVID, CTLADM_ARG_NEED_TL, NULL},
173	{"devlist", CTLADM_CMD_DEVLIST, CTLADM_ARG_NONE, "b:vx"},
174	{"dumpooa", CTLADM_CMD_DUMPOOA, CTLADM_ARG_NONE, NULL},
175	{"dumpstructs", CTLADM_CMD_DUMPSTRUCTS, CTLADM_ARG_NONE, NULL},
176	{"getsync", CTLADM_CMD_GETSYNC, CTLADM_ARG_NEED_TL, NULL},
177	{"hardstart", CTLADM_CMD_HARDSTART, CTLADM_ARG_NONE, NULL},
178	{"hardstop", CTLADM_CMD_HARDSTOP, CTLADM_ARG_NONE, NULL},
179	{"help", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
180	{"inject", CTLADM_CMD_ERR_INJECT, CTLADM_ARG_NEED_TL, "cd:i:p:r:s:"},
181	{"inquiry", CTLADM_CMD_INQUIRY, CTLADM_ARG_NEED_TL, NULL},
182	{"lunlist", CTLADM_CMD_LUNLIST, CTLADM_ARG_NONE, NULL},
183	{"modesense", CTLADM_CMD_MODESENSE, CTLADM_ARG_NEED_TL, "P:S:dlm:c:"},
184	{"modify", CTLADM_CMD_MODIFY, CTLADM_ARG_NONE, "b:l:s:"},
185	{"port", CTLADM_CMD_PORT, CTLADM_ARG_NONE, "lo:p:qt:w:W:x"},
186	{"prin", CTLADM_CMD_PRES_IN, CTLADM_ARG_NEED_TL, "a:"},
187	{"prout", CTLADM_CMD_PRES_OUT, CTLADM_ARG_NEED_TL, "a:k:r:s:"},
188	{"read", CTLADM_CMD_READ, CTLADM_ARG_NEED_TL, rw_opts},
189	{"readcapacity", CTLADM_CMD_READCAPACITY, CTLADM_ARG_NEED_TL, "c:"},
190	{"realsync", CTLADM_CMD_REALSYNC, CTLADM_ARG_NONE, NULL},
191	{"remove", CTLADM_CMD_RM, CTLADM_ARG_NONE, "b:l:o:"},
192	{"reportluns", CTLADM_CMD_REPORT_LUNS, CTLADM_ARG_NEED_TL, NULL},
193	{"reqsense", CTLADM_CMD_REQ_SENSE, CTLADM_ARG_NEED_TL, NULL},
194	{"rtpg", CTLADM_CMD_RTPG, CTLADM_ARG_NEED_TL, NULL},
195	{"setsync", CTLADM_CMD_SETSYNC, CTLADM_ARG_NEED_TL, "i:"},
196	{"shutdown", CTLADM_CMD_SHUTDOWN, CTLADM_ARG_NONE, NULL},
197	{"start", CTLADM_CMD_START, CTLADM_ARG_NEED_TL, startstop_opts},
198	{"startup", CTLADM_CMD_STARTUP, CTLADM_ARG_NONE, NULL},
199	{"stop", CTLADM_CMD_STOP, CTLADM_ARG_NEED_TL, startstop_opts},
200	{"synccache", CTLADM_CMD_SYNC_CACHE, CTLADM_ARG_NEED_TL, "b:c:il:r"},
201	{"tur", CTLADM_CMD_TUR, CTLADM_ARG_NEED_TL, NULL},
202	{"write", CTLADM_CMD_WRITE, CTLADM_ARG_NEED_TL, rw_opts},
203	{"-?", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
204	{"-h", CTLADM_CMD_HELP, CTLADM_ARG_NONE, NULL},
205	{NULL, 0, 0, NULL}
206};
207
208
209ctladm_optret getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
210			ctladm_cmdargs *argnum, const char **subopt);
211static int cctl_parse_tl(char *str, int *target, int *lun);
212static int cctl_dump_ooa(int fd, int argc, char **argv);
213static int cctl_port_dump(int fd, int quiet, int xml, int32_t fe_num,
214			  ctl_port_type port_type);
215static int cctl_port(int fd, int argc, char **argv, char *combinedopt);
216static int cctl_do_io(int fd, int retries, union ctl_io *io, const char *func);
217static int cctl_delay(int fd, int target, int lun, int argc, char **argv,
218		      char *combinedopt);
219static int cctl_lunlist(int fd);
220static void cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len);
221static void cctl_cfi_bbr_statusstr(cfi_bbrread_status, char *str, int str_len);
222static int cctl_hardstopstart(int fd, ctladm_cmdfunction command);
223static int cctl_bbrread(int fd, int target, int lun, int iid, int argc,
224			char **argv, char *combinedopt);
225static int cctl_startup_shutdown(int fd, int target, int lun, int iid,
226				 ctladm_cmdfunction command);
227static int cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
228			   int argc, char **argv, char *combinedopt);
229static int cctl_start_stop(int fd, int target, int lun, int iid, int retries,
230			   int start, int argc, char **argv, char *combinedopt);
231static int cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
232			   int argc, char **argv, char *combinedopt);
233static int cctl_read_capacity(int fd, int target, int lun, int iid,
234			      int retries, int argc, char **argv,
235			      char *combinedopt);
236static int cctl_read_write(int fd, int target, int lun, int iid, int retries,
237			   int argc, char **argv, char *combinedopt,
238			   ctladm_cmdfunction command);
239static int cctl_get_luns(int fd, int target, int lun, int iid, int retries,
240			 struct scsi_report_luns_data **lun_data,
241			 uint32_t *num_luns);
242static int cctl_report_luns(int fd, int target, int lun, int iid, int retries);
243static int cctl_tur(int fd, int target, int lun, int iid, int retries);
244static int cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
245			    char *path_str, int path_len,
246			    struct scsi_inquiry_data *inq_data);
247static int cctl_inquiry(int fd, int target, int lun, int iid, int retries);
248static int cctl_req_sense(int fd, int target, int lun, int iid, int retries);
249static int cctl_persistent_reserve_in(int fd, int target, int lun,
250				      int initiator, int argc, char **argv,
251				      char *combinedopt, int retry_count);
252static int cctl_persistent_reserve_out(int fd, int target, int lun,
253				       int initiator, int argc, char **argv,
254				       char *combinedopt, int retry_count);
255static int cctl_create_lun(int fd, int argc, char **argv, char *combinedopt);
256static int cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator);
257static int cctl_report_target_port_group(int fd, int target, int lun,
258					 int initiator);
259static int cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt);
260
261ctladm_optret
262getoption(struct ctladm_opts *table, char *arg, uint32_t *cmdnum,
263	  ctladm_cmdargs *argnum, const char **subopt)
264{
265	struct ctladm_opts *opts;
266	int num_matches = 0;
267
268	for (opts = table; (opts != NULL) && (opts->optname != NULL);
269	     opts++) {
270		if (strncmp(opts->optname, arg, strlen(arg)) == 0) {
271			*cmdnum = opts->cmdnum;
272			*argnum = opts->argnum;
273			*subopt = opts->subopt;
274
275			if (strcmp(opts->optname, arg) == 0)
276				return (CC_OR_FOUND);
277
278			if (++num_matches > 1)
279				return(CC_OR_AMBIGUOUS);
280		}
281	}
282
283	if (num_matches > 0)
284		return(CC_OR_FOUND);
285	else
286		return(CC_OR_NOT_FOUND);
287}
288
289
290static int
291cctl_parse_tl(char *str, int *target, int *lun)
292{
293	char *tmpstr;
294	int retval;
295
296	retval = 0;
297
298	while (isspace(*str) && (*str != '\0'))
299		str++;
300
301	tmpstr = (char *)strtok(str, ":");
302	if ((tmpstr != NULL) && (*tmpstr != '\0')) {
303		*target = strtol(tmpstr, NULL, 0);
304		tmpstr = (char *)strtok(NULL, ":");
305		if ((tmpstr != NULL) && (*tmpstr != '\0')) {
306			*lun = strtol(tmpstr, NULL, 0);
307		} else
308			retval = -1;
309	} else
310		retval = -1;
311
312	return (retval);
313}
314
315static int
316cctl_dump_ooa(int fd, int argc, char **argv)
317{
318	struct ctl_ooa ooa;
319	long double cmd_latency;
320	int num_entries, len;
321	int target = -1, lun = -1;
322	int retval;
323	unsigned int i;
324
325	num_entries = 104;
326
327	if ((argc > 2)
328	 && (isdigit(argv[2][0]))) {
329		retval = cctl_parse_tl(argv[2], &target, &lun);
330		if (retval != 0)
331			warnx("invalid target:lun argument %s", argv[2]);
332	}
333retry:
334
335	len = num_entries * sizeof(struct ctl_ooa_entry);
336
337	bzero(&ooa, sizeof(ooa));
338
339	ooa.entries = malloc(len);
340
341	if (ooa.entries == NULL) {
342		warn("%s: error mallocing %d bytes", __func__, len);
343		return (1);
344	}
345
346	if (argc > 2) {
347		ooa.lun_num = lun;
348	} else
349		ooa.flags |= CTL_OOA_FLAG_ALL_LUNS;
350
351	ooa.alloc_len = len;
352	ooa.alloc_num = num_entries;
353	if (ioctl(fd, CTL_GET_OOA, &ooa) == -1) {
354		warn("%s: CTL_GET_OOA ioctl failed", __func__);
355		retval = 1;
356		goto bailout;
357	}
358
359	if (ooa.status == CTL_OOA_NEED_MORE_SPACE) {
360		num_entries = num_entries * 2;
361		free(ooa.entries);
362		ooa.entries = NULL;
363		goto retry;
364	}
365
366	if (ooa.status != CTL_OOA_OK) {
367		warnx("%s: CTL_GET_OOA ioctl returned error %d", __func__,
368		      ooa.status);
369		retval = 1;
370		goto bailout;
371	}
372
373	fprintf(stdout, "Dumping OOA queues\n");
374	for (i = 0; i < ooa.fill_num; i++) {
375		struct ctl_ooa_entry *entry;
376		char cdb_str[(SCSI_MAX_CDBLEN * 3) +1];
377		struct bintime delta_bt;
378		struct timespec ts;
379
380		entry = &ooa.entries[i];
381
382		delta_bt = ooa.cur_bt;
383		bintime_sub(&delta_bt, &entry->start_bt);
384		bintime2timespec(&delta_bt, &ts);
385		cmd_latency = ts.tv_sec * 1000;
386		if (ts.tv_nsec > 0)
387			cmd_latency += ts.tv_nsec / 1000000;
388
389		fprintf(stdout, "LUN %jd tag 0x%04x%s%s%s%s%s: %s. CDB: %s "
390			"(%0.0Lf ms)\n",
391			(intmax_t)entry->lun_num, entry->tag_num,
392			(entry->cmd_flags & CTL_OOACMD_FLAG_BLOCKED) ?
393			 " BLOCKED" : "",
394			(entry->cmd_flags & CTL_OOACMD_FLAG_DMA) ? " DMA" : "",
395			(entry->cmd_flags & CTL_OOACMD_FLAG_DMA_QUEUED) ?
396			 " DMAQUEUED" : "",
397			(entry->cmd_flags & CTL_OOACMD_FLAG_ABORT) ?
398			 " ABORT" : "",
399			(entry->cmd_flags & CTL_OOACMD_FLAG_RTR) ? " RTR" :"",
400			scsi_op_desc(entry->cdb[0], NULL),
401			scsi_cdb_string(entry->cdb, cdb_str, sizeof(cdb_str)),
402			cmd_latency);
403	}
404	fprintf(stdout, "OOA queues dump done\n");
405#if 0
406	if (ioctl(fd, CTL_DUMP_OOA) == -1) {
407		warn("%s: CTL_DUMP_OOA ioctl failed", __func__);
408		return (1);
409	}
410#endif
411
412bailout:
413	free(ooa.entries);
414
415	return (0);
416}
417
418static int
419cctl_dump_structs(int fd, ctladm_cmdargs cmdargs __unused)
420{
421	if (ioctl(fd, CTL_DUMP_STRUCTS) == -1) {
422		warn(__func__);
423		return (1);
424	}
425	return (0);
426}
427
428static int
429cctl_port_dump(int fd, int quiet, int xml, int32_t targ_port,
430	       ctl_port_type port_type)
431{
432	struct ctl_port_list port_list;
433	struct ctl_port_entry *entries;
434	struct sbuf *sb = NULL;
435	int num_entries;
436	int did_print = 0;
437	unsigned int i;
438
439	num_entries = 16;
440
441retry:
442
443	entries = malloc(sizeof(*entries) * num_entries);
444	bzero(&port_list, sizeof(port_list));
445	port_list.entries = entries;
446	port_list.alloc_num = num_entries;
447	port_list.alloc_len = num_entries * sizeof(*entries);
448	if (ioctl(fd, CTL_GET_PORT_LIST, &port_list) != 0) {
449		warn("%s: CTL_GET_PORT_LIST ioctl failed", __func__);
450		return (1);
451	}
452	if (port_list.status == CTL_PORT_LIST_NEED_MORE_SPACE) {
453		printf("%s: allocated %d, need %d, retrying\n", __func__,
454		       num_entries, port_list.fill_num + port_list.dropped_num);
455		free(entries);
456		num_entries = port_list.fill_num + port_list.dropped_num;
457		goto retry;
458	}
459
460	if ((quiet == 0)
461	 && (xml == 0))
462		printf("Port Online Type     Name         pp vp %-18s %-18s\n",
463		       "WWNN", "WWPN");
464
465	if (xml != 0) {
466		sb = sbuf_new_auto();
467		sbuf_printf(sb, "<ctlfelist>\n");
468	}
469	for (i = 0; i < port_list.fill_num; i++) {
470		struct ctl_port_entry *entry;
471		const char *type;
472
473		entry = &entries[i];
474
475		switch (entry->port_type) {
476		case CTL_PORT_FC:
477			type = "FC";
478			break;
479		case CTL_PORT_SCSI:
480			type = "SCSI";
481			break;
482		case CTL_PORT_IOCTL:
483			type = "IOCTL";
484			break;
485		case CTL_PORT_INTERNAL:
486			type = "INTERNAL";
487			break;
488		case CTL_PORT_ISC:
489			type = "ISC";
490			break;
491		default:
492			type = "UNKNOWN";
493			break;
494		}
495
496		/*
497		 * If the user specified a frontend number or a particular
498		 * frontend type, only print out that particular frontend
499		 * or frontend type.
500		 */
501		if ((targ_port != -1)
502		 && (targ_port != entry->targ_port))
503			continue;
504		else if ((port_type != CTL_PORT_NONE)
505		      && ((port_type & entry->port_type) == 0))
506			continue;
507
508		did_print = 1;
509
510#if 0
511		printf("Num: %ju Type: %s (%#x) Name: %s Physical Port: %d "
512		       "Virtual Port: %d\n", (uintmax_t)entry->fe_num, type,
513		       entry->port_type, entry->fe_name, entry->physical_port,
514		       entry->virtual_port);
515		printf("WWNN %#jx WWPN %#jx Online: %s\n",
516		       (uintmax_t)entry->wwnn, (uintmax_t)entry->wwpn,
517		       (entry->online) ? "YES" : "NO" );
518#endif
519		if (xml == 0) {
520			printf("%-4d %-6s %-8s %-12s %-2d %-2d %#-18jx "
521			       "%#-18jx\n",
522			       entry->targ_port, (entry->online) ? "YES" : "NO",
523			       type, entry->port_name, entry->physical_port,
524			       entry->virtual_port, (uintmax_t)entry->wwnn,
525			       (uintmax_t)entry->wwpn);
526		} else {
527			sbuf_printf(sb, "<targ_port id=\"%d\">\n",
528				    entry->targ_port);
529			sbuf_printf(sb, "<online>%s</online>\n",
530				    (entry->online) ? "YES" : "NO");
531			sbuf_printf(sb, "<port_type>%s</port_type>\n", type);
532			sbuf_printf(sb, "<port_name>%s</port_name>\n",
533				    entry->port_name);
534			sbuf_printf(sb, "<physical_port>%d</physical_port>\n",
535				    entry->physical_port);
536			sbuf_printf(sb, "<virtual_port>%d</virtual_port>\n",
537				    entry->virtual_port);
538			sbuf_printf(sb, "<wwnn>%#jx</wwnn>\n",
539				    (uintmax_t)entry->wwnn);
540			sbuf_printf(sb, "<wwpn>%#jx</wwpn>\n",
541				    (uintmax_t)entry->wwpn);
542			sbuf_printf(sb, "</targ_port>\n");
543		}
544
545	}
546	if (xml != 0) {
547		sbuf_printf(sb, "</ctlfelist>\n");
548		sbuf_finish(sb);
549		printf("%s", sbuf_data(sb));
550		sbuf_delete(sb);
551	}
552
553	/*
554	 * Give some indication that we didn't find the frontend or
555	 * frontend type requested by the user.  We could print something
556	 * out, but it would probably be better to hide that behind a
557	 * verbose flag.
558	 */
559	if ((did_print == 0)
560	 && ((targ_port != -1)
561	  || (port_type != CTL_PORT_NONE)))
562		return (1);
563	else
564		return (0);
565}
566
567typedef enum {
568	CCTL_PORT_MODE_NONE,
569	CCTL_PORT_MODE_LIST,
570	CCTL_PORT_MODE_SET,
571	CCTL_PORT_MODE_ON,
572	CCTL_PORT_MODE_OFF
573} cctl_port_mode;
574
575struct ctladm_opts cctl_fe_table[] = {
576	{"fc", CTL_PORT_FC, CTLADM_ARG_NONE, NULL},
577	{"scsi", CTL_PORT_SCSI, CTLADM_ARG_NONE, NULL},
578	{"internal", CTL_PORT_INTERNAL, CTLADM_ARG_NONE, NULL},
579	{"all", CTL_PORT_ALL, CTLADM_ARG_NONE, NULL},
580	{NULL, 0, 0, NULL}
581};
582
583static int
584cctl_port(int fd, int argc, char **argv, char *combinedopt)
585{
586	int c;
587	int32_t targ_port = -1;
588	int retval = 0;
589	int wwnn_set = 0, wwpn_set = 0;
590	uint64_t wwnn = 0, wwpn = 0;
591	cctl_port_mode port_mode = CCTL_PORT_MODE_NONE;
592	struct ctl_port_entry entry;
593	ctl_port_type port_type = CTL_PORT_NONE;
594	int quiet = 0, xml = 0;
595
596	while ((c = getopt(argc, argv, combinedopt)) != -1) {
597		switch (c) {
598		case 'l':
599			if (port_mode != CCTL_PORT_MODE_NONE)
600				goto bailout_badarg;
601
602			port_mode = CCTL_PORT_MODE_LIST;
603			break;
604		case 'o':
605			if (port_mode != CCTL_PORT_MODE_NONE)
606				goto bailout_badarg;
607
608			if (strcasecmp(optarg, "on") == 0)
609				port_mode = CCTL_PORT_MODE_ON;
610			else if (strcasecmp(optarg, "off") == 0)
611				port_mode = CCTL_PORT_MODE_OFF;
612			else {
613				warnx("Invalid -o argument %s, \"on\" or "
614				      "\"off\" are the only valid args",
615				      optarg);
616				retval = 1;
617				goto bailout;
618			}
619			break;
620		case 'p':
621			targ_port = strtol(optarg, NULL, 0);
622			break;
623		case 'q':
624			quiet = 1;
625			break;
626		case 't': {
627			ctladm_optret optret;
628			ctladm_cmdargs argnum;
629			const char *subopt;
630			ctl_port_type tmp_port_type;
631
632			optret = getoption(cctl_fe_table, optarg, &tmp_port_type,
633					   &argnum, &subopt);
634			if (optret == CC_OR_AMBIGUOUS) {
635				warnx("%s: ambiguous frontend type %s",
636				      __func__, optarg);
637				retval = 1;
638				goto bailout;
639			} else if (optret == CC_OR_NOT_FOUND) {
640				warnx("%s: invalid frontend type %s",
641				      __func__, optarg);
642				retval = 1;
643				goto bailout;
644			}
645
646			port_type |= tmp_port_type;
647			break;
648		}
649		case 'w':
650			if ((port_mode != CCTL_PORT_MODE_NONE)
651			 && (port_mode != CCTL_PORT_MODE_SET))
652				goto bailout_badarg;
653
654			port_mode = CCTL_PORT_MODE_SET;
655
656			wwnn = strtoull(optarg, NULL, 0);
657			wwnn_set = 1;
658			break;
659		case 'W':
660			if ((port_mode != CCTL_PORT_MODE_NONE)
661			 && (port_mode != CCTL_PORT_MODE_SET))
662				goto bailout_badarg;
663
664			port_mode = CCTL_PORT_MODE_SET;
665
666			wwpn = strtoull(optarg, NULL, 0);
667			wwpn_set = 1;
668			break;
669		case 'x':
670			xml = 1;
671			break;
672		}
673	}
674
675	/*
676	 * The user can specify either one or more frontend types (-t), or
677	 * a specific frontend, but not both.
678	 *
679	 * If the user didn't specify a frontend type or number, set it to
680	 * all.  This is primarily needed for the enable/disable ioctls.
681	 * This will be a no-op for the listing code.  For the set ioctl,
682	 * we'll throw an error, since that only works on one port at a time.
683	 */
684	if ((port_type != CTL_PORT_NONE) && (targ_port != -1)) {
685		warnx("%s: can only specify one of -t or -n", __func__);
686		retval = 1;
687		goto bailout;
688	} else if ((targ_port == -1) && (port_type == CTL_PORT_NONE))
689		port_type = CTL_PORT_ALL;
690
691	bzero(&entry, sizeof(&entry));
692
693	/*
694	 * These are needed for all but list/dump mode.
695	 */
696	entry.port_type = port_type;
697	entry.targ_port = targ_port;
698
699	switch (port_mode) {
700	case CCTL_PORT_MODE_LIST:
701		cctl_port_dump(fd, quiet, xml, targ_port, port_type);
702		break;
703	case CCTL_PORT_MODE_SET:
704		if (targ_port == -1) {
705			warnx("%s: -w and -W require -n", __func__);
706			retval = 1;
707			goto bailout;
708		}
709
710		if (wwnn_set) {
711			entry.flags |= CTL_PORT_WWNN_VALID;
712			entry.wwnn = wwnn;
713		}
714		if (wwpn_set) {
715			entry.flags |= CTL_PORT_WWPN_VALID;
716			entry.wwpn = wwpn;
717		}
718
719		if (ioctl(fd, CTL_SET_PORT_WWNS, &entry) == -1) {
720			warn("%s: CTL_SET_PORT_WWNS ioctl failed", __func__);
721			retval = 1;
722			goto bailout;
723		}
724		break;
725	case CCTL_PORT_MODE_ON:
726		if (ioctl(fd, CTL_ENABLE_PORT, &entry) == -1) {
727			warn("%s: CTL_ENABLE_PORT ioctl failed", __func__);
728			retval = 1;
729			goto bailout;
730		}
731		fprintf(stdout, "Front End Ports enabled\n");
732		break;
733	case CCTL_PORT_MODE_OFF:
734		if (ioctl(fd, CTL_DISABLE_PORT, &entry) == -1) {
735			warn("%s: CTL_DISABLE_PORT ioctl failed", __func__);
736			retval = 1;
737			goto bailout;
738		}
739		fprintf(stdout, "Front End Ports disabled\n");
740		break;
741	default:
742		warnx("%s: one of -l, -o or -w/-W must be specified", __func__);
743		retval = 1;
744		goto bailout;
745		break;
746	}
747
748bailout:
749
750	return (retval);
751
752bailout_badarg:
753	warnx("%s: only one of -l, -o or -w/-W may be specified", __func__);
754	return (1);
755}
756
757static int
758cctl_do_io(int fd, int retries, union ctl_io *io, const char *func)
759{
760	do {
761		if (ioctl(fd, CTL_IO, io) == -1) {
762			warn("%s: error sending CTL_IO ioctl", func);
763			return (-1);
764		}
765	} while (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
766	      && (retries-- > 0));
767
768	return (0);
769}
770
771static int
772cctl_delay(int fd, int target, int lun, int argc, char **argv,
773	   char *combinedopt)
774{
775	int datamove_delay;
776	struct ctl_io_delay_info delay_info;
777	char *delayloc = NULL;
778	char *delaytype = NULL;
779	int delaytime = -1;
780	int retval;
781	int c;
782
783	retval = 0;
784	datamove_delay = 0;
785
786	memset(&delay_info, 0, sizeof(delay_info));
787
788	while ((c = getopt(argc, argv, combinedopt)) != -1) {
789		switch (c) {
790		case 'T':
791			delaytype = strdup(optarg);
792			break;
793		case 'l':
794			delayloc = strdup(optarg);
795			break;
796		case 't':
797			delaytime = strtoul(optarg, NULL, 0);
798			break;
799		}
800	}
801
802	if (delaytime == -1) {
803		warnx("%s: you must specify the delaytime with -t", __func__);
804		retval = 1;
805		goto bailout;
806	}
807
808	if (strcasecmp(delayloc, "datamove") == 0)
809		delay_info.delay_loc = CTL_DELAY_LOC_DATAMOVE;
810	else if (strcasecmp(delayloc, "done") == 0)
811		delay_info.delay_loc = CTL_DELAY_LOC_DONE;
812	else {
813		warnx("%s: invalid delay location %s", __func__, delayloc);
814		retval = 1;
815		goto bailout;
816	}
817
818	if ((delaytype == NULL)
819	 || (strcmp(delaytype, "oneshot") == 0))
820		delay_info.delay_type = CTL_DELAY_TYPE_ONESHOT;
821	else if (strcmp(delaytype, "cont") == 0)
822		delay_info.delay_type = CTL_DELAY_TYPE_CONT;
823	else {
824		warnx("%s: invalid delay type %s", __func__, delaytype);
825		retval = 1;
826		goto bailout;
827	}
828
829	delay_info.target_id = target;
830	delay_info.lun_id = lun;
831	delay_info.delay_secs = delaytime;
832
833	if (ioctl(fd, CTL_DELAY_IO, &delay_info) == -1) {
834		warn("%s: CTL_DELAY_IO ioctl failed", __func__);
835		retval = 1;
836		goto bailout;
837	}
838	switch (delay_info.status) {
839	case CTL_DELAY_STATUS_NONE:
840		warnx("%s: no delay status??", __func__);
841		retval = 1;
842		break;
843	case CTL_DELAY_STATUS_OK:
844		break;
845	case CTL_DELAY_STATUS_INVALID_LUN:
846		warnx("%s: invalid lun %d", __func__, lun);
847		retval = 1;
848		break;
849	case CTL_DELAY_STATUS_INVALID_TYPE:
850		warnx("%s: invalid delay type %d", __func__,
851		      delay_info.delay_type);
852		retval = 1;
853		break;
854	case CTL_DELAY_STATUS_INVALID_LOC:
855		warnx("%s: delay location %s not implemented?", __func__,
856		      delayloc);
857		retval = 1;
858		break;
859	case CTL_DELAY_STATUS_NOT_IMPLEMENTED:
860		warnx("%s: delay not implemented in the kernel", __func__);
861		warnx("%s: recompile with the CTL_IO_DELAY flag set", __func__);
862		retval = 1;
863		break;
864	default:
865		warnx("%s: unknown delay return status %d", __func__,
866		      delay_info.status);
867		retval = 1;
868		break;
869	}
870bailout:
871
872	/* delayloc should never be NULL, but just in case...*/
873	if (delayloc != NULL)
874		free(delayloc);
875
876	return (retval);
877}
878
879static int
880cctl_realsync(int fd, int argc, char **argv)
881{
882	int syncstate;
883	int retval;
884	char *syncarg;
885
886	retval = 0;
887
888	if (argc != 3) {
889		warnx("%s %s takes exactly one argument", argv[0], argv[1]);
890		retval = 1;
891		goto bailout;
892	}
893
894	syncarg = argv[2];
895
896	if (strncasecmp(syncarg, "query", min(strlen(syncarg),
897			strlen("query"))) == 0) {
898		if (ioctl(fd, CTL_REALSYNC_GET, &syncstate) == -1) {
899			warn("%s: CTL_REALSYNC_GET ioctl failed", __func__);
900			retval = 1;
901			goto bailout;
902		}
903		fprintf(stdout, "SYNCHRONIZE CACHE support is: ");
904		switch (syncstate) {
905		case 0:
906			fprintf(stdout, "OFF\n");
907			break;
908		case 1:
909			fprintf(stdout, "ON\n");
910			break;
911		default:
912			fprintf(stdout, "unknown (%d)\n", syncstate);
913			break;
914		}
915		goto bailout;
916	} else if (strcasecmp(syncarg, "on") == 0) {
917		syncstate = 1;
918	} else if (strcasecmp(syncarg, "off") == 0) {
919		syncstate = 0;
920	} else {
921		warnx("%s: invalid realsync argument %s", __func__, syncarg);
922		retval = 1;
923		goto bailout;
924	}
925
926	if (ioctl(fd, CTL_REALSYNC_SET, &syncstate) == -1) {
927		warn("%s: CTL_REALSYNC_SET ioctl failed", __func__);
928		retval = 1;
929		goto bailout;
930	}
931bailout:
932	return (retval);
933}
934
935static int
936cctl_getsetsync(int fd, int target, int lun, ctladm_cmdfunction command,
937		int argc, char **argv, char *combinedopt)
938{
939	struct ctl_sync_info sync_info;
940	uint32_t ioctl_cmd;
941	int sync_interval = -1;
942	int retval;
943	int c;
944
945	retval = 0;
946
947	memset(&sync_info, 0, sizeof(sync_info));
948	sync_info.target_id = target;
949	sync_info.lun_id = lun;
950
951	while ((c = getopt(argc, argv, combinedopt)) != -1) {
952		switch (c) {
953		case 'i':
954			sync_interval = strtoul(optarg, NULL, 0);
955			break;
956		default:
957			break;
958		}
959	}
960
961	if (command == CTLADM_CMD_SETSYNC) {
962		if (sync_interval == -1) {
963			warnx("%s: you must specify the sync interval with -i",
964			      __func__);
965			retval = 1;
966			goto bailout;
967		}
968		sync_info.sync_interval = sync_interval;
969		ioctl_cmd = CTL_SETSYNC;
970	} else {
971		ioctl_cmd = CTL_GETSYNC;
972	}
973
974	if (ioctl(fd, ioctl_cmd, &sync_info) == -1) {
975		warn("%s: CTL_%sSYNC ioctl failed", __func__,
976		     (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET");
977		retval = 1;
978		goto bailout;
979	}
980
981	switch (sync_info.status) {
982	case CTL_GS_SYNC_OK:
983		if (command == CTLADM_CMD_GETSYNC) {
984			fprintf(stdout, "%d:%d: sync interval: %d\n",
985				target, lun, sync_info.sync_interval);
986		}
987		break;
988	case CTL_GS_SYNC_NO_LUN:
989		warnx("%s: unknown target:LUN %d:%d", __func__, target, lun);
990		retval = 1;
991		break;
992	case CTL_GS_SYNC_NONE:
993	default:
994		warnx("%s: unknown CTL_%sSYNC status %d", __func__,
995		      (command == CTLADM_CMD_SETSYNC) ? "SET" : "GET",
996		      sync_info.status);
997		retval = 1;
998		break;
999	}
1000bailout:
1001	return (retval);
1002}
1003
1004struct ctladm_opts cctl_err_types[] = {
1005	{"aborted", CTL_LUN_INJ_ABORTED, CTLADM_ARG_NONE, NULL},
1006	{"mediumerr", CTL_LUN_INJ_MEDIUM_ERR, CTLADM_ARG_NONE, NULL},
1007	{"ua", CTL_LUN_INJ_UA, CTLADM_ARG_NONE, NULL},
1008	{"custom", CTL_LUN_INJ_CUSTOM, CTLADM_ARG_NONE, NULL},
1009	{NULL, 0, 0, NULL}
1010
1011};
1012
1013struct ctladm_opts cctl_err_patterns[] = {
1014	{"read", CTL_LUN_PAT_READ, CTLADM_ARG_NONE, NULL},
1015	{"write", CTL_LUN_PAT_WRITE, CTLADM_ARG_NONE, NULL},
1016	{"rw", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1017	{"readwrite", CTL_LUN_PAT_READWRITE, CTLADM_ARG_NONE, NULL},
1018	{"readcap", CTL_LUN_PAT_READCAP, CTLADM_ARG_NONE, NULL},
1019	{"tur", CTL_LUN_PAT_TUR, CTLADM_ARG_NONE, NULL},
1020	{"any", CTL_LUN_PAT_ANY, CTLADM_ARG_NONE, NULL},
1021#if 0
1022	{"cmd", CTL_LUN_PAT_CMD,  CTLADM_ARG_NONE, NULL},
1023#endif
1024	{NULL, 0, 0, NULL}
1025};
1026
1027static int
1028cctl_error_inject(int fd, uint32_t target, uint32_t lun, int argc, char **argv,
1029		  char *combinedopt)
1030{
1031	int retval;
1032	struct ctl_error_desc err_desc;
1033	uint64_t lba = 0;
1034	uint32_t len = 0;
1035	uint64_t delete_id = 0;
1036	int delete_id_set = 0;
1037	int continuous = 0;
1038	int sense_len = 0;
1039	int fd_sense = 0;
1040	int c;
1041
1042	bzero(&err_desc, sizeof(err_desc));
1043	err_desc.target_id = target;
1044	err_desc.lun_id = lun;
1045
1046	while ((c = getopt(argc, argv, combinedopt)) != -1) {
1047		switch (c) {
1048		case 'c':
1049			continuous = 1;
1050			break;
1051		case 'd':
1052			delete_id = strtoull(optarg, NULL, 0);
1053			delete_id_set = 1;
1054			break;
1055		case 'i':
1056		case 'p': {
1057			ctladm_optret optret;
1058			ctladm_cmdargs argnum;
1059			const char *subopt;
1060
1061			if (c == 'i') {
1062				ctl_lun_error err_type;
1063
1064				if (err_desc.lun_error != CTL_LUN_INJ_NONE) {
1065					warnx("%s: can't specify multiple -i "
1066					      "arguments", __func__);
1067					retval = 1;
1068					goto bailout;
1069				}
1070				optret = getoption(cctl_err_types, optarg,
1071						   &err_type, &argnum, &subopt);
1072				err_desc.lun_error = err_type;
1073			} else {
1074				ctl_lun_error_pattern pattern;
1075
1076				optret = getoption(cctl_err_patterns, optarg,
1077						   &pattern, &argnum, &subopt);
1078				err_desc.error_pattern |= pattern;
1079			}
1080
1081			if (optret == CC_OR_AMBIGUOUS) {
1082				warnx("%s: ambiguous argument %s", __func__,
1083				      optarg);
1084				retval = 1;
1085				goto bailout;
1086			} else if (optret == CC_OR_NOT_FOUND) {
1087				warnx("%s: argument %s not found", __func__,
1088				      optarg);
1089				retval = 1;
1090				goto bailout;
1091			}
1092			break;
1093		}
1094		case 'r': {
1095			char *tmpstr, *tmpstr2;
1096
1097			tmpstr = strdup(optarg);
1098			if (tmpstr == NULL) {
1099				warn("%s: error duplicating string %s",
1100				     __func__, optarg);
1101				retval = 1;
1102				goto bailout;
1103			}
1104
1105			tmpstr2 = strsep(&tmpstr, ",");
1106			if (tmpstr2 == NULL) {
1107				warnx("%s: invalid -r argument %s", __func__,
1108				      optarg);
1109				retval = 1;
1110				free(tmpstr);
1111				goto bailout;
1112			}
1113			lba = strtoull(tmpstr2, NULL, 0);
1114			tmpstr2 = strsep(&tmpstr, ",");
1115			if (tmpstr2 == NULL) {
1116				warnx("%s: no len argument for -r lba,len, got"
1117				      " %s", __func__, optarg);
1118				retval = 1;
1119				free(tmpstr);
1120				goto bailout;
1121			}
1122			len = strtoul(tmpstr2, NULL, 0);
1123			free(tmpstr);
1124			break;
1125		}
1126		case 's': {
1127			struct get_hook hook;
1128			char *sensestr;
1129
1130			sense_len = strtol(optarg, NULL, 0);
1131			if (sense_len <= 0) {
1132				warnx("invalid number of sense bytes %d",
1133				      sense_len);
1134				retval = 1;
1135				goto bailout;
1136			}
1137
1138			sense_len = MIN(sense_len, SSD_FULL_SIZE);
1139
1140			hook.argc = argc - optind;
1141			hook.argv = argv + optind;
1142			hook.got = 0;
1143
1144			sensestr = cget(&hook, NULL);
1145			if ((sensestr != NULL)
1146			 && (sensestr[0] == '-')) {
1147				fd_sense = 1;
1148			} else {
1149				buff_encode_visit(
1150				    (uint8_t *)&err_desc.custom_sense,
1151				    sense_len, sensestr, iget, &hook);
1152			}
1153			optind += hook.got;
1154			break;
1155		}
1156		default:
1157			break;
1158		}
1159	}
1160
1161	if (delete_id_set != 0) {
1162		err_desc.serial = delete_id;
1163		if (ioctl(fd, CTL_ERROR_INJECT_DELETE, &err_desc) == -1) {
1164			warn("%s: error issuing CTL_ERROR_INJECT_DELETE ioctl",
1165			     __func__);
1166			retval = 1;
1167		}
1168		goto bailout;
1169	}
1170
1171	if (err_desc.lun_error == CTL_LUN_INJ_NONE) {
1172		warnx("%s: error injection command (-i) needed",
1173		      __func__);
1174		retval = 1;
1175		goto bailout;
1176	} else if ((err_desc.lun_error == CTL_LUN_INJ_CUSTOM)
1177		&& (sense_len == 0)) {
1178		warnx("%s: custom error requires -s", __func__);
1179		retval = 1;
1180		goto bailout;
1181	}
1182
1183	if (continuous != 0)
1184		err_desc.lun_error |= CTL_LUN_INJ_CONTINUOUS;
1185
1186	/*
1187	 * If fd_sense is set, we need to read the sense data the user
1188	 * wants returned from stdin.
1189	 */
1190        if (fd_sense == 1) {
1191		ssize_t amt_read;
1192		int amt_to_read = sense_len;
1193 		u_int8_t *buf_ptr = (uint8_t *)&err_desc.custom_sense;
1194
1195		for (amt_read = 0; amt_to_read > 0;
1196		     amt_read = read(STDIN_FILENO, buf_ptr, amt_to_read)) {
1197			if (amt_read == -1) {
1198         			warn("error reading sense data from stdin");
1199				retval = 1;
1200				goto bailout;
1201			}
1202			amt_to_read -= amt_read;
1203			buf_ptr += amt_read;
1204		}
1205	}
1206
1207	if (err_desc.error_pattern == CTL_LUN_PAT_NONE) {
1208		warnx("%s: command pattern (-p) needed", __func__);
1209		retval = 1;
1210		goto bailout;
1211	}
1212
1213	if (len != 0) {
1214		err_desc.error_pattern |= CTL_LUN_PAT_RANGE;
1215		/*
1216		 * We could check here to see whether it's a read/write
1217		 * command, but that will be pointless once we allow
1218		 * custom patterns.  At that point, the user could specify
1219		 * a READ(6) CDB type, and we wouldn't have an easy way here
1220		 * to verify whether range checking is possible there.  The
1221		 * user will just figure it out when his error never gets
1222		 * executed.
1223		 */
1224#if 0
1225		if ((err_desc.pattern & CTL_LUN_PAT_READWRITE) == 0) {
1226			warnx("%s: need read and/or write pattern if range "
1227			      "is specified", __func__);
1228			retval = 1;
1229			goto bailout;
1230		}
1231#endif
1232		err_desc.lba_range.lba = lba;
1233		err_desc.lba_range.len = len;
1234	}
1235
1236	if (ioctl(fd, CTL_ERROR_INJECT, &err_desc) == -1) {
1237		warn("%s: error issuing CTL_ERROR_INJECT ioctl", __func__);
1238		retval = 1;
1239	} else {
1240		printf("Error injection succeeded, serial number is %ju\n",
1241		       (uintmax_t)err_desc.serial);
1242	}
1243bailout:
1244
1245	return (retval);
1246}
1247
1248static int
1249cctl_lunlist(int fd)
1250{
1251	struct scsi_report_luns_data *lun_data;
1252	struct scsi_inquiry_data *inq_data;
1253	uint32_t num_luns;
1254	int target;
1255	int initid;
1256	unsigned int i;
1257	int retval;
1258
1259	retval = 0;
1260	inq_data = NULL;
1261
1262	target = 6;
1263	initid = 7;
1264
1265	/*
1266	 * XXX KDM assuming LUN 0 is fine, but we may need to change this
1267	 * if we ever acquire the ability to have multiple targets.
1268	 */
1269	if ((retval = cctl_get_luns(fd, target, /*lun*/ 0, initid,
1270				    /*retries*/ 2, &lun_data, &num_luns)) != 0)
1271		goto bailout;
1272
1273	inq_data = malloc(sizeof(*inq_data));
1274	if (inq_data == NULL) {
1275		warn("%s: couldn't allocate memory for inquiry data\n",
1276		     __func__);
1277		retval = 1;
1278		goto bailout;
1279	}
1280	for (i = 0; i < num_luns; i++) {
1281		char scsi_path[40];
1282		int lun_val;
1283
1284		switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1285		case RPL_LUNDATA_ATYP_PERIPH:
1286			lun_val = lun_data->luns[i].lundata[1];
1287			break;
1288		case RPL_LUNDATA_ATYP_FLAT:
1289			lun_val = (lun_data->luns[i].lundata[0] &
1290				RPL_LUNDATA_FLAT_LUN_MASK) |
1291				(lun_data->luns[i].lundata[1] <<
1292				RPL_LUNDATA_FLAT_LUN_BITS);
1293			break;
1294		case RPL_LUNDATA_ATYP_LUN:
1295		case RPL_LUNDATA_ATYP_EXTLUN:
1296		default:
1297			fprintf(stdout, "Unsupported LUN format %d\n",
1298				lun_data->luns[i].lundata[0] &
1299				RPL_LUNDATA_ATYP_MASK);
1300			lun_val = -1;
1301			break;
1302		}
1303		if (lun_val == -1)
1304			continue;
1305
1306		if ((retval = cctl_get_inquiry(fd, target, lun_val, initid,
1307					       /*retries*/ 2, scsi_path,
1308					       sizeof(scsi_path),
1309					       inq_data)) != 0) {
1310			goto bailout;
1311		}
1312		printf("%s", scsi_path);
1313		scsi_print_inquiry(inq_data);
1314	}
1315bailout:
1316
1317	if (lun_data != NULL)
1318		free(lun_data);
1319
1320	if (inq_data != NULL)
1321		free(inq_data);
1322
1323	return (retval);
1324}
1325
1326static void
1327cctl_cfi_mt_statusstr(cfi_mt_status status, char *str, int str_len)
1328{
1329	switch (status) {
1330	case CFI_MT_PORT_OFFLINE:
1331		snprintf(str, str_len, "Port Offline");
1332		break;
1333	case CFI_MT_ERROR:
1334		snprintf(str, str_len, "Error");
1335		break;
1336	case CFI_MT_SUCCESS:
1337		snprintf(str, str_len, "Success");
1338		break;
1339	case CFI_MT_NONE:
1340		snprintf(str, str_len, "None??");
1341		break;
1342	default:
1343		snprintf(str, str_len, "Unknown status: %d", status);
1344		break;
1345	}
1346}
1347
1348static void
1349cctl_cfi_bbr_statusstr(cfi_bbrread_status status, char *str, int str_len)
1350{
1351	switch (status) {
1352	case CFI_BBR_SUCCESS:
1353		snprintf(str, str_len, "Success");
1354		break;
1355	case CFI_BBR_LUN_UNCONFIG:
1356		snprintf(str, str_len, "LUN not configured");
1357		break;
1358	case CFI_BBR_NO_LUN:
1359		snprintf(str, str_len, "LUN does not exist");
1360		break;
1361	case CFI_BBR_NO_MEM:
1362		snprintf(str, str_len, "Memory allocation error");
1363		break;
1364	case CFI_BBR_BAD_LEN:
1365		snprintf(str, str_len, "Length is not a multiple of blocksize");
1366		break;
1367	case CFI_BBR_RESERV_CONFLICT:
1368		snprintf(str, str_len, "Reservation conflict");
1369		break;
1370	case CFI_BBR_LUN_STOPPED:
1371		snprintf(str, str_len, "LUN is powered off");
1372		break;
1373	case CFI_BBR_LUN_OFFLINE_CTL:
1374		snprintf(str, str_len, "LUN is offline");
1375		break;
1376	case CFI_BBR_LUN_OFFLINE_RC:
1377		snprintf(str, str_len, "RAIDCore array is offline (double "
1378			 "failure?)");
1379		break;
1380	case CFI_BBR_SCSI_ERROR:
1381		snprintf(str, str_len, "SCSI Error");
1382		break;
1383	case CFI_BBR_ERROR:
1384		snprintf(str, str_len, "Error");
1385		break;
1386	default:
1387		snprintf(str, str_len, "Unknown status: %d", status);
1388		break;
1389	}
1390}
1391
1392static int
1393cctl_hardstopstart(int fd, ctladm_cmdfunction command)
1394{
1395	struct ctl_hard_startstop_info hs_info;
1396	char error_str[256];
1397	int do_start;
1398	int retval;
1399
1400	retval = 0;
1401
1402	if (command == CTLADM_CMD_HARDSTART)
1403		do_start = 1;
1404	else
1405		do_start = 0;
1406
1407	if (ioctl(fd, (do_start == 1) ? CTL_HARD_START : CTL_HARD_STOP,
1408		  &hs_info) == -1) {
1409		warn("%s: CTL_HARD_%s ioctl failed", __func__,
1410		     (do_start == 1) ? "START" : "STOP");
1411		retval = 1;
1412		goto bailout;
1413	}
1414
1415	fprintf(stdout, "Hard %s Status: ", (command == CTLADM_CMD_HARDSTOP) ?
1416		"Stop" : "Start");
1417	cctl_cfi_mt_statusstr(hs_info.status, error_str, sizeof(error_str));
1418	fprintf(stdout, "%s\n", error_str);
1419	fprintf(stdout, "Total LUNs: %d\n", hs_info.total_luns);
1420	fprintf(stdout, "LUNs complete: %d\n", hs_info.luns_complete);
1421	fprintf(stdout, "LUNs failed: %d\n", hs_info.luns_failed);
1422
1423bailout:
1424	return (retval);
1425}
1426
1427static int
1428cctl_bbrread(int fd, int target __unused, int lun, int iid __unused,
1429	     int argc, char **argv, char *combinedopt)
1430{
1431	struct ctl_bbrread_info bbr_info;
1432	char error_str[256];
1433	int datalen = -1;
1434	uint64_t lba = 0;
1435	int lba_set = 0;
1436	int retval;
1437	int c;
1438
1439	retval = 0;
1440
1441	while ((c = getopt(argc, argv, combinedopt)) != -1) {
1442		switch (c) {
1443		case 'd':
1444			datalen = strtoul(optarg, NULL, 0);
1445			break;
1446		case 'l':
1447			lba = strtoull(optarg, NULL, 0);
1448			lba_set = 1;
1449			break;
1450		default:
1451			break;
1452		}
1453	}
1454
1455	if (lba_set == 0) {
1456		warnx("%s: you must specify an LBA with -l", __func__);
1457		retval = 1;
1458		goto bailout;
1459	}
1460
1461	if (datalen == -1) {
1462		warnx("%s: you must specify a length with -d", __func__);
1463		retval = 1;
1464		goto bailout;
1465	}
1466
1467	bbr_info.lun_num = lun;
1468	bbr_info.lba = lba;
1469	/*
1470	 * XXX KDM get the blocksize first??
1471	 */
1472	if ((datalen % 512) != 0) {
1473		warnx("%s: data length %d is not a multiple of 512 bytes",
1474		     __func__, datalen);
1475		retval = 1;
1476		goto bailout;
1477	}
1478	bbr_info.len = datalen;
1479
1480	if (ioctl(fd, CTL_BBRREAD, &bbr_info) == -1) {
1481		warn("%s: CTL_BBRREAD ioctl failed", __func__);
1482		retval = 1;
1483		goto bailout;
1484	}
1485	cctl_cfi_mt_statusstr(bbr_info.status, error_str, sizeof(error_str));
1486	fprintf(stdout, "BBR Read Overall Status: %s\n", error_str);
1487	cctl_cfi_bbr_statusstr(bbr_info.bbr_status, error_str,
1488			       sizeof(error_str));
1489	fprintf(stdout, "BBR Read Status: %s\n", error_str);
1490	/*
1491	 * XXX KDM should we bother printing out SCSI status if we get
1492	 * CFI_BBR_SCSI_ERROR back?
1493	 *
1494	 * Return non-zero if this fails?
1495	 */
1496bailout:
1497	return (retval);
1498}
1499
1500static int
1501cctl_startup_shutdown(int fd, int target, int lun, int iid,
1502		      ctladm_cmdfunction command)
1503{
1504	union ctl_io *io;
1505	struct ctl_id id;
1506	struct scsi_report_luns_data *lun_data;
1507	struct scsi_inquiry_data *inq_data;
1508	uint32_t num_luns;
1509	unsigned int i;
1510	int retval;
1511
1512	retval = 0;
1513	inq_data = NULL;
1514
1515	/*
1516	 * - report luns
1517	 * - step through each lun, do an inquiry
1518	 * - check OOA queue on direct access luns
1519	 * - send stop with offline bit to each direct access device with a
1520	 *   clear OOA queue
1521	 *   - if we get a reservation conflict, reset the LUN to clear it
1522	 *     and reissue the stop with the offline bit set
1523	 */
1524
1525	id.id = iid;
1526
1527	io = ctl_scsi_alloc_io(id);
1528	if (io == NULL) {
1529		warnx("%s: can't allocate memory", __func__);
1530		return (1);
1531	}
1532
1533	if ((retval = cctl_get_luns(fd, target, lun, iid, /*retries*/ 2,
1534				    &lun_data, &num_luns)) != 0)
1535		goto bailout;
1536
1537	inq_data = malloc(sizeof(*inq_data));
1538	if (inq_data == NULL) {
1539		warn("%s: couldn't allocate memory for inquiry data\n",
1540		     __func__);
1541		retval = 1;
1542		goto bailout;
1543	}
1544	for (i = 0; i < num_luns; i++) {
1545		char scsi_path[40];
1546		int lun_val;
1547
1548		/*
1549		 * XXX KDM figure out a way to share this code with
1550		 * cctl_lunlist()?
1551		 */
1552		switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
1553		case RPL_LUNDATA_ATYP_PERIPH:
1554			lun_val = lun_data->luns[i].lundata[1];
1555			break;
1556		case RPL_LUNDATA_ATYP_FLAT:
1557			lun_val = (lun_data->luns[i].lundata[0] &
1558				RPL_LUNDATA_FLAT_LUN_MASK) |
1559				(lun_data->luns[i].lundata[1] <<
1560				RPL_LUNDATA_FLAT_LUN_BITS);
1561			break;
1562		case RPL_LUNDATA_ATYP_LUN:
1563		case RPL_LUNDATA_ATYP_EXTLUN:
1564		default:
1565			fprintf(stdout, "Unsupported LUN format %d\n",
1566				lun_data->luns[i].lundata[0] &
1567				RPL_LUNDATA_ATYP_MASK);
1568			lun_val = -1;
1569			break;
1570		}
1571		if (lun_val == -1)
1572			continue;
1573
1574		if ((retval = cctl_get_inquiry(fd, target, lun_val, iid,
1575					       /*retries*/ 2, scsi_path,
1576					       sizeof(scsi_path),
1577					       inq_data)) != 0) {
1578			goto bailout;
1579		}
1580		printf("%s", scsi_path);
1581		scsi_print_inquiry(inq_data);
1582		/*
1583		 * We only want to shutdown direct access devices.
1584		 */
1585		if (SID_TYPE(inq_data) != T_DIRECT) {
1586			printf("%s LUN is not direct access, skipped\n",
1587			       scsi_path);
1588			continue;
1589		}
1590
1591		if (command == CTLADM_CMD_SHUTDOWN) {
1592			struct ctl_ooa_info ooa_info;
1593
1594			ooa_info.target_id = target;
1595			ooa_info.lun_id = lun_val;
1596
1597			if (ioctl(fd, CTL_CHECK_OOA, &ooa_info) == -1) {
1598				printf("%s CTL_CHECK_OOA ioctl failed\n",
1599				       scsi_path);
1600				continue;
1601			}
1602
1603			if (ooa_info.status != CTL_OOA_SUCCESS) {
1604				printf("%s CTL_CHECK_OOA returned status %d\n",
1605				       scsi_path, ooa_info.status);
1606				continue;
1607			}
1608			if (ooa_info.num_entries != 0) {
1609				printf("%s %d entr%s in the OOA queue, "
1610				       "skipping shutdown\n", scsi_path,
1611				       ooa_info.num_entries,
1612				       (ooa_info.num_entries > 1)?"ies" : "y" );
1613				continue;
1614			}
1615		}
1616
1617		ctl_scsi_start_stop(/*io*/ io,
1618				    /*start*/(command == CTLADM_CMD_STARTUP) ?
1619					      1 : 0,
1620				    /*load_eject*/ 0,
1621				    /*immediate*/ 0,
1622				    /*power_conditions*/ SSS_PC_START_VALID,
1623				    /*onoffline*/ 1,
1624				    /*ctl_tag_type*/
1625				    (command == CTLADM_CMD_STARTUP) ?
1626				    CTL_TAG_SIMPLE :CTL_TAG_ORDERED,
1627				    /*control*/ 0);
1628
1629		io->io_hdr.nexus.targ_target.id = target;
1630		io->io_hdr.nexus.targ_lun = lun_val;
1631		io->io_hdr.nexus.initid = id;
1632
1633		if (cctl_do_io(fd, /*retries*/ 3, io, __func__) != 0) {
1634			retval = 1;
1635			goto bailout;
1636		}
1637
1638		if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
1639			ctl_io_error_print(io, inq_data, stderr);
1640		else {
1641			printf("%s LUN is now %s\n", scsi_path,
1642			       (command == CTLADM_CMD_STARTUP) ? "online" :
1643			       "offline");
1644		}
1645	}
1646bailout:
1647	if (lun_data != NULL)
1648		free(lun_data);
1649
1650	if (inq_data != NULL)
1651		free(inq_data);
1652
1653	if (io != NULL)
1654		ctl_scsi_free_io(io);
1655
1656	return (retval);
1657}
1658
1659static int
1660cctl_sync_cache(int fd, int target, int lun, int iid, int retries,
1661		int argc, char **argv, char *combinedopt)
1662{
1663	union ctl_io *io;
1664	struct ctl_id id;
1665	int cdb_size = -1;
1666	int retval;
1667	uint64_t our_lba = 0;
1668	uint32_t our_block_count = 0;
1669	int reladr = 0, immed = 0;
1670	int c;
1671
1672	id.id = iid;
1673	retval = 0;
1674
1675	io = ctl_scsi_alloc_io(id);
1676	if (io == NULL) {
1677		warnx("%s: can't allocate memory", __func__);
1678		return (1);
1679	}
1680
1681	while ((c = getopt(argc, argv, combinedopt)) != -1) {
1682		switch (c) {
1683		case 'b':
1684			our_block_count = strtoul(optarg, NULL, 0);
1685			break;
1686		case 'c':
1687			cdb_size = strtol(optarg, NULL, 0);
1688			break;
1689		case 'i':
1690			immed = 1;
1691			break;
1692		case 'l':
1693			our_lba = strtoull(optarg, NULL, 0);
1694			break;
1695		case 'r':
1696			reladr = 1;
1697			break;
1698		default:
1699			break;
1700		}
1701	}
1702
1703	if (cdb_size != -1) {
1704		switch (cdb_size) {
1705		case 10:
1706		case 16:
1707			break;
1708		default:
1709			warnx("%s: invalid cdbsize %d, valid sizes are 10 "
1710			      "and 16", __func__, cdb_size);
1711			retval = 1;
1712			goto bailout;
1713			break; /* NOTREACHED */
1714		}
1715	} else
1716		cdb_size = 10;
1717
1718	ctl_scsi_sync_cache(/*io*/ io,
1719			    /*immed*/ immed,
1720			    /*reladr*/ reladr,
1721			    /*minimum_cdb_size*/ cdb_size,
1722			    /*starting_lba*/ our_lba,
1723			    /*block_count*/ our_block_count,
1724			    /*tag_type*/ CTL_TAG_SIMPLE,
1725			    /*control*/ 0);
1726
1727	io->io_hdr.nexus.targ_target.id = target;
1728	io->io_hdr.nexus.targ_lun = lun;
1729	io->io_hdr.nexus.initid = id;
1730
1731	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1732		retval = 1;
1733		goto bailout;
1734	}
1735
1736	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1737		fprintf(stdout, "Cache synchronized successfully\n");
1738	} else
1739		ctl_io_error_print(io, NULL, stderr);
1740bailout:
1741	ctl_scsi_free_io(io);
1742
1743	return (retval);
1744}
1745
1746static int
1747cctl_start_stop(int fd, int target, int lun, int iid, int retries, int start,
1748		int argc, char **argv, char *combinedopt)
1749{
1750	union ctl_io *io;
1751	struct ctl_id id;
1752	char scsi_path[40];
1753	int immed = 0, onoffline = 0;
1754	int retval, c;
1755
1756	id.id = iid;
1757	retval = 0;
1758
1759	io = ctl_scsi_alloc_io(id);
1760	if (io == NULL) {
1761		warnx("%s: can't allocate memory", __func__);
1762		return (1);
1763	}
1764
1765	while ((c = getopt(argc, argv, combinedopt)) != -1) {
1766		switch (c) {
1767		case 'i':
1768			immed = 1;
1769			break;
1770		case 'o':
1771			onoffline = 1;
1772			break;
1773		default:
1774			break;
1775		}
1776	}
1777	/*
1778	 * Use an ordered tag for the stop command, to guarantee that any
1779	 * pending I/O will finish before the stop command executes.  This
1780	 * would normally be the case anyway, since CTL will basically
1781	 * treat the start/stop command as an ordered command with respect
1782	 * to any other command except an INQUIRY.  (See ctl_ser_table.c.)
1783	 */
1784	ctl_scsi_start_stop(/*io*/ io,
1785			    /*start*/ start,
1786			    /*load_eject*/ 0,
1787			    /*immediate*/ immed,
1788			    /*power_conditions*/ SSS_PC_START_VALID,
1789			    /*onoffline*/ onoffline,
1790			    /*ctl_tag_type*/ start ? CTL_TAG_SIMPLE :
1791						     CTL_TAG_ORDERED,
1792			    /*control*/ 0);
1793
1794	io->io_hdr.nexus.targ_target.id = target;
1795	io->io_hdr.nexus.targ_lun = lun;
1796	io->io_hdr.nexus.initid = id;
1797
1798	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1799		retval = 1;
1800		goto bailout;
1801	}
1802
1803	ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
1804	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1805		fprintf(stdout, "%s LUN %s successfully\n", scsi_path,
1806			(start) ?  "started" : "stopped");
1807	} else
1808		ctl_io_error_print(io, NULL, stderr);
1809
1810bailout:
1811	ctl_scsi_free_io(io);
1812
1813	return (retval);
1814}
1815
1816static int
1817cctl_mode_sense(int fd, int target, int lun, int iid, int retries,
1818		int argc, char **argv, char *combinedopt)
1819{
1820	union ctl_io *io;
1821	struct ctl_id id;
1822	uint32_t datalen;
1823	uint8_t *dataptr;
1824	int pc = -1, cdbsize, retval, dbd = 0, subpage = -1;
1825	int list = 0;
1826	int page_code = -1;
1827	int c;
1828
1829	id.id = iid;
1830	cdbsize = 0;
1831	retval = 0;
1832	dataptr = NULL;
1833
1834	io = ctl_scsi_alloc_io(id);
1835	if (io == NULL) {
1836		warn("%s: can't allocate memory", __func__);
1837		return (1);
1838	}
1839
1840	while ((c = getopt(argc, argv, combinedopt)) != -1) {
1841		switch (c) {
1842		case 'P':
1843			pc = strtoul(optarg, NULL, 0);
1844			break;
1845		case 'S':
1846			subpage = strtoul(optarg, NULL, 0);
1847			break;
1848		case 'd':
1849			dbd = 1;
1850			break;
1851		case 'l':
1852			list = 1;
1853			break;
1854		case 'm':
1855			page_code = strtoul(optarg, NULL, 0);
1856			break;
1857		case 'c':
1858			cdbsize = strtol(optarg, NULL, 0);
1859			break;
1860		default:
1861			break;
1862		}
1863	}
1864
1865	if (((list == 0) && (page_code == -1))
1866	 || ((list != 0) && (page_code != -1))) {
1867		warnx("%s: you must specify either a page code (-m) or -l",
1868		      __func__);
1869		retval = 1;
1870		goto bailout;
1871	}
1872
1873	if ((page_code != -1)
1874	 && ((page_code > SMS_ALL_PAGES_PAGE)
1875	  || (page_code < 0))) {
1876		warnx("%s: page code %d is out of range", __func__,
1877		      page_code);
1878		retval = 1;
1879		goto bailout;
1880	}
1881
1882	if (list == 1) {
1883		page_code = SMS_ALL_PAGES_PAGE;
1884		if (pc != -1) {
1885			warnx("%s: arg -P makes no sense with -l",
1886			      __func__);
1887			retval = 1;
1888			goto bailout;
1889		}
1890		if (subpage != -1) {
1891			warnx("%s: arg -S makes no sense with -l", __func__);
1892			retval = 1;
1893			goto bailout;
1894		}
1895	}
1896
1897	if (pc == -1)
1898		pc = SMS_PAGE_CTRL_CURRENT;
1899	else {
1900		if ((pc > 3)
1901		 || (pc < 0)) {
1902			warnx("%s: page control value %d is out of range: 0-3",
1903			      __func__, pc);
1904			retval = 1;
1905			goto bailout;
1906		}
1907	}
1908
1909
1910	if ((subpage != -1)
1911	 && ((subpage > 255)
1912	  || (subpage < 0))) {
1913		warnx("%s: subpage code %d is out of range: 0-255", __func__,
1914		      subpage);
1915		retval = 1;
1916		goto bailout;
1917	}
1918	if (cdbsize != 0) {
1919		switch (cdbsize) {
1920		case 6:
1921		case 10:
1922			break;
1923		default:
1924			warnx("%s: invalid cdbsize %d, valid sizes are 6 "
1925			      "and 10", __func__, cdbsize);
1926			retval = 1;
1927			goto bailout;
1928			break;
1929		}
1930	} else
1931		cdbsize = 6;
1932
1933	if (subpage == -1)
1934		subpage = 0;
1935
1936	if (cdbsize == 6)
1937		datalen = 255;
1938	else
1939		datalen = 65535;
1940
1941	dataptr = (uint8_t *)malloc(datalen);
1942	if (dataptr == NULL) {
1943		warn("%s: can't allocate %d bytes", __func__, datalen);
1944		retval = 1;
1945		goto bailout;
1946	}
1947
1948	memset(dataptr, 0, datalen);
1949
1950	ctl_scsi_mode_sense(io,
1951			    /*data_ptr*/ dataptr,
1952			    /*data_len*/ datalen,
1953			    /*dbd*/ dbd,
1954			    /*llbaa*/ 0,
1955			    /*page_code*/ page_code,
1956			    /*pc*/ pc << 6,
1957			    /*subpage*/ subpage,
1958			    /*minimum_cdb_size*/ cdbsize,
1959			    /*tag_type*/ CTL_TAG_SIMPLE,
1960			    /*control*/ 0);
1961
1962	io->io_hdr.nexus.targ_target.id = target;
1963	io->io_hdr.nexus.targ_lun = lun;
1964	io->io_hdr.nexus.initid = id;
1965
1966	if (cctl_do_io(fd, retries, io, __func__) != 0) {
1967		retval = 1;
1968		goto bailout;
1969	}
1970
1971	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1972		int pages_len, used_len;
1973		uint32_t returned_len;
1974		uint8_t *ndataptr;
1975
1976		if (io->scsiio.cdb[0] == MODE_SENSE_6) {
1977			struct scsi_mode_hdr_6 *hdr6;
1978			int bdlen;
1979
1980			hdr6 = (struct scsi_mode_hdr_6 *)dataptr;
1981
1982			returned_len = hdr6->datalen + 1;
1983			bdlen = hdr6->block_descr_len;
1984
1985			ndataptr = (uint8_t *)((uint8_t *)&hdr6[1] + bdlen);
1986		} else {
1987			struct scsi_mode_hdr_10 *hdr10;
1988			int bdlen;
1989
1990			hdr10 = (struct scsi_mode_hdr_10 *)dataptr;
1991
1992			returned_len = scsi_2btoul(hdr10->datalen) + 2;
1993			bdlen = scsi_2btoul(hdr10->block_descr_len);
1994
1995			ndataptr = (uint8_t *)((uint8_t *)&hdr10[1] + bdlen);
1996		}
1997		/* just in case they can give us more than we allocated for */
1998		returned_len = min(returned_len, datalen);
1999		pages_len = returned_len - (ndataptr - dataptr);
2000#if 0
2001		fprintf(stdout, "returned_len = %d, pages_len = %d\n",
2002			returned_len, pages_len);
2003#endif
2004		if (list == 1) {
2005			fprintf(stdout, "Supported mode pages:\n");
2006			for (used_len = 0; used_len < pages_len;) {
2007				struct scsi_mode_page_header *header;
2008
2009				header = (struct scsi_mode_page_header *)
2010					&ndataptr[used_len];
2011				fprintf(stdout, "%d\n", header->page_code);
2012				used_len += header->page_length + 2;
2013			}
2014		} else {
2015			for (used_len = 0; used_len < pages_len; used_len++) {
2016				fprintf(stdout, "0x%x ", ndataptr[used_len]);
2017				if (((used_len+1) % 16) == 0)
2018					fprintf(stdout, "\n");
2019			}
2020			fprintf(stdout, "\n");
2021		}
2022	} else
2023		ctl_io_error_print(io, NULL, stderr);
2024bailout:
2025
2026	ctl_scsi_free_io(io);
2027
2028	if (dataptr != NULL)
2029		free(dataptr);
2030
2031	return (retval);
2032}
2033
2034static int
2035cctl_read_capacity(int fd, int target, int lun, int iid, int retries,
2036           	   int argc, char **argv, char *combinedopt)
2037{
2038	union ctl_io *io;
2039	struct ctl_id id;
2040	struct scsi_read_capacity_data *data;
2041	struct scsi_read_capacity_data_long *longdata;
2042	int cdbsize = -1, retval;
2043	uint8_t *dataptr;
2044	int c;
2045
2046	cdbsize = 10;
2047	dataptr = NULL;
2048	retval = 0;
2049	id.id = iid;
2050
2051	io = ctl_scsi_alloc_io(id);
2052	if (io == NULL) {
2053		warn("%s: can't allocate memory\n", __func__);
2054		return (1);
2055	}
2056
2057	while ((c = getopt(argc, argv, combinedopt)) != -1) {
2058		switch (c) {
2059		case 'c':
2060			cdbsize = strtol(optarg, NULL, 0);
2061			break;
2062		default:
2063			break;
2064		}
2065	}
2066	if (cdbsize != -1) {
2067		switch (cdbsize) {
2068		case 10:
2069		case 16:
2070			break;
2071		default:
2072			warnx("%s: invalid cdbsize %d, valid sizes are 10 "
2073			      "and 16", __func__, cdbsize);
2074			retval = 1;
2075			goto bailout;
2076			break; /* NOTREACHED */
2077		}
2078	} else
2079		cdbsize = 10;
2080
2081	dataptr = (uint8_t *)malloc(sizeof(*longdata));
2082	if (dataptr == NULL) {
2083		warn("%s: can't allocate %zd bytes\n", __func__,
2084		     sizeof(*longdata));
2085		retval = 1;
2086		goto bailout;
2087	}
2088	memset(dataptr, 0, sizeof(*longdata));
2089
2090retry:
2091
2092	switch (cdbsize) {
2093	case 10:
2094		ctl_scsi_read_capacity(io,
2095				       /*data_ptr*/ dataptr,
2096				       /*data_len*/ sizeof(*longdata),
2097				       /*addr*/ 0,
2098				       /*reladr*/ 0,
2099				       /*pmi*/ 0,
2100				       /*tag_type*/ CTL_TAG_SIMPLE,
2101				       /*control*/ 0);
2102		break;
2103	case 16:
2104		ctl_scsi_read_capacity_16(io,
2105					  /*data_ptr*/ dataptr,
2106					  /*data_len*/ sizeof(*longdata),
2107					  /*addr*/ 0,
2108					  /*reladr*/ 0,
2109					  /*pmi*/ 0,
2110					  /*tag_type*/ CTL_TAG_SIMPLE,
2111					  /*control*/ 0);
2112		break;
2113	}
2114
2115	io->io_hdr.nexus.initid = id;
2116	io->io_hdr.nexus.targ_target.id = target;
2117	io->io_hdr.nexus.targ_lun = lun;
2118
2119	if (cctl_do_io(fd, retries, io, __func__) != 0) {
2120		retval = 1;
2121		goto bailout;
2122	}
2123
2124	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2125		uint64_t maxlba;
2126		uint32_t blocksize;
2127
2128		if (cdbsize == 10) {
2129
2130			data = (struct scsi_read_capacity_data *)dataptr;
2131
2132			maxlba = scsi_4btoul(data->addr);
2133			blocksize = scsi_4btoul(data->length);
2134
2135			if (maxlba == 0xffffffff) {
2136				cdbsize = 16;
2137				goto retry;
2138			}
2139		} else {
2140			longdata=(struct scsi_read_capacity_data_long *)dataptr;
2141
2142			maxlba = scsi_8btou64(longdata->addr);
2143			blocksize = scsi_4btoul(longdata->length);
2144		}
2145
2146		fprintf(stdout, "Disk Capacity: %ju, Blocksize: %d\n",
2147			(uintmax_t)maxlba, blocksize);
2148	} else {
2149		ctl_io_error_print(io, NULL, stderr);
2150	}
2151bailout:
2152	ctl_scsi_free_io(io);
2153
2154	if (dataptr != NULL)
2155		free(dataptr);
2156
2157	return (retval);
2158}
2159
2160static int
2161cctl_read_write(int fd, int target, int lun, int iid, int retries,
2162		int argc, char **argv, char *combinedopt,
2163		ctladm_cmdfunction command)
2164{
2165	union ctl_io *io;
2166	struct ctl_id id;
2167	int file_fd, do_stdio;
2168	int cdbsize = -1, databytes;
2169	uint8_t *dataptr;
2170	char *filename = NULL;
2171	int datalen = -1, blocksize = -1;
2172	uint64_t lba = 0;
2173	int lba_set = 0;
2174	int retval;
2175	int c;
2176
2177	retval = 0;
2178	do_stdio = 0;
2179	dataptr = NULL;
2180	file_fd = -1;
2181	id.id = iid;
2182
2183	io = ctl_scsi_alloc_io(id);
2184	if (io == NULL) {
2185		warn("%s: can't allocate memory\n", __func__);
2186		return (1);
2187	}
2188
2189	while ((c = getopt(argc, argv, combinedopt)) != -1) {
2190		switch (c) {
2191		case 'N':
2192			io->io_hdr.flags |= CTL_FLAG_NO_DATAMOVE;
2193			break;
2194		case 'b':
2195			blocksize = strtoul(optarg, NULL, 0);
2196			break;
2197		case 'c':
2198			cdbsize = strtoul(optarg, NULL, 0);
2199			break;
2200		case 'd':
2201			datalen = strtoul(optarg, NULL, 0);
2202			break;
2203		case 'f':
2204			filename = strdup(optarg);
2205			break;
2206		case 'l':
2207			lba = strtoull(optarg, NULL, 0);
2208			lba_set = 1;
2209			break;
2210		default:
2211			break;
2212		}
2213	}
2214	if (filename == NULL) {
2215		warnx("%s: you must supply a filename using -f", __func__);
2216		retval = 1;
2217		goto bailout;
2218	}
2219
2220	if (datalen == -1) {
2221		warnx("%s: you must specify the data length with -d", __func__);
2222		retval = 1;
2223		goto bailout;
2224	}
2225
2226	if (lba_set == 0) {
2227		warnx("%s: you must specify the LBA with -l", __func__);
2228		retval = 1;
2229		goto bailout;
2230	}
2231
2232	if (blocksize == -1) {
2233		warnx("%s: you must specify the blocksize with -b", __func__);
2234		retval = 1;
2235		goto bailout;
2236	}
2237
2238	if (cdbsize != -1) {
2239		switch (cdbsize) {
2240		case 6:
2241		case 10:
2242		case 12:
2243		case 16:
2244			break;
2245		default:
2246			warnx("%s: invalid cdbsize %d, valid sizes are 6, "
2247			      "10, 12 or 16", __func__, cdbsize);
2248			retval = 1;
2249			goto bailout;
2250			break; /* NOTREACHED */
2251		}
2252	} else
2253		cdbsize = 6;
2254
2255	databytes = datalen * blocksize;
2256	dataptr = (uint8_t *)malloc(databytes);
2257
2258	if (dataptr == NULL) {
2259		warn("%s: can't allocate %d bytes\n", __func__, databytes);
2260		retval = 1;
2261		goto bailout;
2262	}
2263	if (strcmp(filename, "-") == 0) {
2264		if (command == CTLADM_CMD_READ)
2265			file_fd = STDOUT_FILENO;
2266		else
2267			file_fd = STDIN_FILENO;
2268		do_stdio = 1;
2269	} else {
2270		file_fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
2271		if (file_fd == -1) {
2272			warn("%s: can't open file %s", __func__, filename);
2273			retval = 1;
2274			goto bailout;
2275		}
2276	}
2277
2278	memset(dataptr, 0, databytes);
2279
2280	if (command == CTLADM_CMD_WRITE) {
2281		int bytes_read;
2282
2283		bytes_read = read(file_fd, dataptr, databytes);
2284		if (bytes_read == -1) {
2285			warn("%s: error reading file %s", __func__, filename);
2286			retval = 1;
2287			goto bailout;
2288		}
2289		if (bytes_read != databytes) {
2290			warnx("%s: only read %d bytes from file %s",
2291			      __func__, bytes_read, filename);
2292			retval = 1;
2293			goto bailout;
2294		}
2295	}
2296	ctl_scsi_read_write(io,
2297			    /*data_ptr*/ dataptr,
2298			    /*data_len*/ databytes,
2299			    /*read_op*/ (command == CTLADM_CMD_READ) ? 1 : 0,
2300			    /*byte2*/ 0,
2301			    /*minimum_cdb_size*/ cdbsize,
2302			    /*lba*/ lba,
2303			    /*num_blocks*/ datalen,
2304			    /*tag_type*/ CTL_TAG_SIMPLE,
2305			    /*control*/ 0);
2306
2307	io->io_hdr.nexus.targ_target.id = target;
2308	io->io_hdr.nexus.targ_lun = lun;
2309	io->io_hdr.nexus.initid = id;
2310
2311	if (cctl_do_io(fd, retries, io, __func__) != 0) {
2312		retval = 1;
2313		goto bailout;
2314	}
2315
2316	if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2317	 && (command == CTLADM_CMD_READ)) {
2318		int bytes_written;
2319
2320		bytes_written = write(file_fd, dataptr, databytes);
2321		if (bytes_written == -1) {
2322			warn("%s: can't write to %s", __func__, filename);
2323			goto bailout;
2324		}
2325	} else if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
2326		ctl_io_error_print(io, NULL, stderr);
2327
2328
2329bailout:
2330
2331	ctl_scsi_free_io(io);
2332
2333	if (dataptr != NULL)
2334		free(dataptr);
2335
2336	if ((do_stdio == 0)
2337	 && (file_fd != -1))
2338		close(file_fd);
2339
2340	return (retval);
2341}
2342
2343static int
2344cctl_get_luns(int fd, int target, int lun, int iid, int retries, struct
2345	      scsi_report_luns_data **lun_data, uint32_t *num_luns)
2346{
2347	union ctl_io *io;
2348	struct ctl_id id;
2349	uint32_t nluns;
2350	int lun_datalen;
2351	int retval;
2352
2353	retval = 0;
2354	id.id = iid;
2355
2356	io = ctl_scsi_alloc_io(id);
2357	if (io == NULL) {
2358		warnx("%s: can't allocate memory", __func__);
2359		return (1);
2360	}
2361
2362	/*
2363	 * lun_data includes space for 1 lun, allocate space for 4 initially.
2364	 * If that isn't enough, we'll allocate more.
2365	 */
2366	nluns = 4;
2367retry:
2368	lun_datalen = sizeof(*lun_data) +
2369		(nluns * sizeof(struct scsi_report_luns_lundata));
2370	*lun_data = malloc(lun_datalen);
2371
2372	if (*lun_data == NULL) {
2373		warnx("%s: can't allocate memory", __func__);
2374		ctl_scsi_free_io(io);
2375		return (1);
2376	}
2377
2378	ctl_scsi_report_luns(io,
2379			     /*data_ptr*/ (uint8_t *)*lun_data,
2380			     /*data_len*/ lun_datalen,
2381			     /*select_report*/ RPL_REPORT_ALL,
2382			     /*tag_type*/ CTL_TAG_SIMPLE,
2383			     /*control*/ 0);
2384
2385	io->io_hdr.nexus.initid = id;
2386	io->io_hdr.nexus.targ_target.id = target;
2387	io->io_hdr.nexus.targ_lun = lun;
2388
2389	if (cctl_do_io(fd, retries, io, __func__) != 0) {
2390		retval = 1;
2391		goto bailout;
2392	}
2393
2394	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2395		uint32_t returned_len, returned_luns;
2396
2397		returned_len = scsi_4btoul((*lun_data)->length);
2398		returned_luns = returned_len / 8;
2399		if (returned_luns > nluns) {
2400			nluns = returned_luns;
2401			free(*lun_data);
2402			goto retry;
2403		}
2404		/* These should be the same */
2405		*num_luns = MIN(returned_luns, nluns);
2406	} else {
2407		ctl_io_error_print(io, NULL, stderr);
2408		retval = 1;
2409	}
2410bailout:
2411	ctl_scsi_free_io(io);
2412
2413	return (retval);
2414}
2415
2416static int
2417cctl_report_luns(int fd, int target, int lun, int iid, int retries)
2418{
2419	struct scsi_report_luns_data *lun_data;
2420	uint32_t num_luns, i;
2421	int retval;
2422
2423	lun_data = NULL;
2424
2425	if ((retval = cctl_get_luns(fd, target, lun, iid, retries, &lun_data,
2426				   &num_luns)) != 0)
2427		goto bailout;
2428
2429	fprintf(stdout, "%u LUNs returned\n", num_luns);
2430	for (i = 0; i < num_luns; i++) {
2431		int lun_val;
2432
2433		/*
2434		 * XXX KDM figure out a way to share this code with
2435		 * cctl_lunlist()?
2436		 */
2437		switch (lun_data->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) {
2438		case RPL_LUNDATA_ATYP_PERIPH:
2439			lun_val = lun_data->luns[i].lundata[1];
2440			break;
2441		case RPL_LUNDATA_ATYP_FLAT:
2442			lun_val = (lun_data->luns[i].lundata[0] &
2443				RPL_LUNDATA_FLAT_LUN_MASK) |
2444				(lun_data->luns[i].lundata[1] <<
2445				RPL_LUNDATA_FLAT_LUN_BITS);
2446			break;
2447		case RPL_LUNDATA_ATYP_LUN:
2448		case RPL_LUNDATA_ATYP_EXTLUN:
2449		default:
2450			fprintf(stdout, "Unsupported LUN format %d\n",
2451				lun_data->luns[i].lundata[0] &
2452				RPL_LUNDATA_ATYP_MASK);
2453			lun_val = -1;
2454			break;
2455		}
2456		if (lun_val == -1)
2457			continue;
2458
2459		fprintf(stdout, "%d\n", lun_val);
2460	}
2461
2462bailout:
2463	if (lun_data != NULL)
2464		free(lun_data);
2465
2466	return (retval);
2467}
2468
2469static int
2470cctl_tur(int fd, int target, int lun, int iid, int retries)
2471{
2472	union ctl_io *io;
2473	struct ctl_id id;
2474
2475	id.id = iid;
2476
2477	io = ctl_scsi_alloc_io(id);
2478	if (io == NULL) {
2479		fprintf(stderr, "can't allocate memory\n");
2480		return (1);
2481	}
2482
2483	ctl_scsi_tur(io,
2484		     /* tag_type */ CTL_TAG_SIMPLE,
2485		     /* control */ 0);
2486
2487	io->io_hdr.nexus.targ_target.id = target;
2488	io->io_hdr.nexus.targ_lun = lun;
2489	io->io_hdr.nexus.initid = id;
2490
2491	if (cctl_do_io(fd, retries, io, __func__) != 0) {
2492		ctl_scsi_free_io(io);
2493		return (1);
2494	}
2495
2496	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
2497		fprintf(stdout, "Unit is ready\n");
2498	else
2499		ctl_io_error_print(io, NULL, stderr);
2500
2501	return (0);
2502}
2503
2504static int
2505cctl_get_inquiry(int fd, int target, int lun, int iid, int retries,
2506		 char *path_str, int path_len,
2507		 struct scsi_inquiry_data *inq_data)
2508{
2509	union ctl_io *io;
2510	struct ctl_id id;
2511	int retval;
2512
2513	retval = 0;
2514
2515	id.id = iid;
2516
2517	io = ctl_scsi_alloc_io(id);
2518	if (io == NULL) {
2519		warnx("cctl_inquiry: can't allocate memory\n");
2520		return (1);
2521	}
2522
2523	ctl_scsi_inquiry(/*io*/ io,
2524			 /*data_ptr*/ (uint8_t *)inq_data,
2525			 /*data_len*/ sizeof(*inq_data),
2526			 /*byte2*/ 0,
2527			 /*page_code*/ 0,
2528			 /*tag_type*/ CTL_TAG_SIMPLE,
2529			 /*control*/ 0);
2530
2531	io->io_hdr.nexus.targ_target.id = target;
2532	io->io_hdr.nexus.targ_lun = lun;
2533	io->io_hdr.nexus.initid = id;
2534
2535	if (cctl_do_io(fd, retries, io, __func__) != 0) {
2536		retval = 1;
2537		goto bailout;
2538	}
2539
2540	if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
2541		retval = 1;
2542		ctl_io_error_print(io, NULL, stderr);
2543	} else if (path_str != NULL)
2544		ctl_scsi_path_string(io, path_str, path_len);
2545
2546bailout:
2547	ctl_scsi_free_io(io);
2548
2549	return (retval);
2550}
2551
2552static int
2553cctl_inquiry(int fd, int target, int lun, int iid, int retries)
2554{
2555	struct scsi_inquiry_data *inq_data;
2556	char scsi_path[40];
2557	int retval;
2558
2559	retval = 0;
2560
2561	inq_data = malloc(sizeof(*inq_data));
2562	if (inq_data == NULL) {
2563		warnx("%s: can't allocate inquiry data", __func__);
2564		retval = 1;
2565		goto bailout;
2566	}
2567
2568	if ((retval = cctl_get_inquiry(fd, target, lun, iid, retries, scsi_path,
2569				       sizeof(scsi_path), inq_data)) != 0)
2570		goto bailout;
2571
2572	printf("%s", scsi_path);
2573	scsi_print_inquiry(inq_data);
2574
2575bailout:
2576	if (inq_data != NULL)
2577		free(inq_data);
2578
2579	return (retval);
2580}
2581
2582static int
2583cctl_req_sense(int fd, int target, int lun, int iid, int retries)
2584{
2585	union ctl_io *io;
2586	struct scsi_sense_data *sense_data;
2587	struct ctl_id id;
2588	int retval;
2589
2590	retval = 0;
2591
2592	id.id = iid;
2593
2594	io = ctl_scsi_alloc_io(id);
2595	if (io == NULL) {
2596		warnx("cctl_req_sense: can't allocate memory\n");
2597		return (1);
2598	}
2599	sense_data = malloc(sizeof(*sense_data));
2600	memset(sense_data, 0, sizeof(*sense_data));
2601
2602	ctl_scsi_request_sense(/*io*/ io,
2603			       /*data_ptr*/ (uint8_t *)sense_data,
2604			       /*data_len*/ sizeof(*sense_data),
2605			       /*byte2*/ 0,
2606			       /*tag_type*/ CTL_TAG_SIMPLE,
2607			       /*control*/ 0);
2608
2609	io->io_hdr.nexus.targ_target.id = target;
2610	io->io_hdr.nexus.targ_lun = lun;
2611	io->io_hdr.nexus.initid = id;
2612
2613	if (cctl_do_io(fd, retries, io, __func__) != 0) {
2614		retval = 1;
2615		goto bailout;
2616	}
2617
2618	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2619		bcopy(sense_data, &io->scsiio.sense_data, sizeof(*sense_data));
2620		io->scsiio.sense_len = sizeof(*sense_data);
2621		ctl_scsi_sense_print(&io->scsiio, NULL, stdout);
2622	} else
2623		ctl_io_error_print(io, NULL, stderr);
2624
2625bailout:
2626
2627	ctl_scsi_free_io(io);
2628	free(sense_data);
2629
2630	return (retval);
2631}
2632
2633static int
2634cctl_report_target_port_group(int fd, int target, int lun, int initiator)
2635{
2636	union ctl_io *io;
2637	struct ctl_id id;
2638	uint32_t datalen;
2639	uint8_t *dataptr;
2640	int retval;
2641
2642	id.id = initiator;
2643	dataptr = NULL;
2644	retval = 0;
2645
2646	io = ctl_scsi_alloc_io(id);
2647	if (io == NULL) {
2648		warn("%s: can't allocate memory", __func__);
2649		return (1);
2650	}
2651
2652	datalen = 64;
2653	dataptr = (uint8_t *)malloc(datalen);
2654	if (dataptr == NULL) {
2655		warn("%s: can't allocate %d bytes", __func__, datalen);
2656	    	retval = 1;
2657		goto bailout;
2658	}
2659
2660	memset(dataptr, 0, datalen);
2661
2662	ctl_scsi_maintenance_in(/*io*/ io,
2663				/*data_ptr*/ dataptr,
2664				/*data_len*/ datalen,
2665				/*action*/ SA_RPRT_TRGT_GRP,
2666				/*tag_type*/ CTL_TAG_SIMPLE,
2667				/*control*/ 0);
2668
2669	io->io_hdr.nexus.targ_target.id = target;
2670	io->io_hdr.nexus.targ_lun = lun;
2671	io->io_hdr.nexus.initid = id;
2672
2673	if (cctl_do_io(fd, 0, io, __func__) != 0) {
2674		retval = 1;
2675		goto bailout;
2676	}
2677
2678	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2679		int returned_len, used_len;
2680
2681		returned_len = scsi_4btoul(&dataptr[0]) + 4;
2682
2683		for (used_len = 0; used_len < returned_len; used_len++) {
2684			fprintf(stdout, "0x%02x ", dataptr[used_len]);
2685			if (((used_len+1) % 8) == 0)
2686				fprintf(stdout, "\n");
2687		}
2688		fprintf(stdout, "\n");
2689	} else
2690		ctl_io_error_print(io, NULL, stderr);
2691
2692bailout:
2693	ctl_scsi_free_io(io);
2694
2695	if (dataptr != NULL)
2696		free(dataptr);
2697
2698	return (retval);
2699}
2700
2701static int
2702cctl_inquiry_vpd_devid(int fd, int target, int lun, int initiator)
2703{
2704	union ctl_io *io;
2705	struct ctl_id id;
2706	uint32_t datalen;
2707	uint8_t *dataptr;
2708	int retval;
2709
2710	id.id = initiator;
2711	retval = 0;
2712	dataptr = NULL;
2713
2714	io = ctl_scsi_alloc_io(id);
2715	if (io == NULL) {
2716		warn("%s: can't allocate memory", __func__);
2717		return (1);
2718	}
2719
2720	datalen = 256;
2721	dataptr = (uint8_t *)malloc(datalen);
2722	if (dataptr == NULL) {
2723		warn("%s: can't allocate %d bytes", __func__, datalen);
2724	    	retval = 1;
2725		goto bailout;
2726	}
2727
2728	memset(dataptr, 0, datalen);
2729
2730	ctl_scsi_inquiry(/*io*/        io,
2731			 /*data_ptr*/  dataptr,
2732			 /*data_len*/  datalen,
2733			 /*byte2*/     SI_EVPD,
2734			 /*page_code*/ SVPD_DEVICE_ID,
2735			 /*tag_type*/  CTL_TAG_SIMPLE,
2736			 /*control*/   0);
2737
2738	io->io_hdr.nexus.targ_target.id = target;
2739	io->io_hdr.nexus.targ_lun = lun;
2740	io->io_hdr.nexus.initid = id;
2741
2742	if (cctl_do_io(fd, 0, io, __func__) != 0) {
2743		retval = 1;
2744		goto bailout;
2745	}
2746
2747	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2748		int returned_len, used_len;
2749
2750		returned_len = scsi_2btoul(&dataptr[2]) + 4;
2751
2752		for (used_len = 0; used_len < returned_len; used_len++) {
2753			fprintf(stdout, "0x%02x ", dataptr[used_len]);
2754			if (((used_len+1) % 8) == 0)
2755				fprintf(stdout, "\n");
2756		}
2757		fprintf(stdout, "\n");
2758	} else
2759		ctl_io_error_print(io, NULL, stderr);
2760
2761bailout:
2762	ctl_scsi_free_io(io);
2763
2764	if (dataptr != NULL)
2765		free(dataptr);
2766
2767	return (retval);
2768}
2769
2770static int
2771cctl_persistent_reserve_in(int fd, int target, int lun, int initiator,
2772                           int argc, char **argv, char *combinedopt,
2773			   int retry_count)
2774{
2775	union ctl_io *io;
2776	struct ctl_id id;
2777	uint32_t datalen;
2778	uint8_t *dataptr;
2779	int action = -1;
2780	int retval;
2781	int c;
2782
2783	id.id = initiator;
2784	retval = 0;
2785	dataptr = NULL;
2786
2787	io = ctl_scsi_alloc_io(id);
2788	if (io == NULL) {
2789		warn("%s: can't allocate memory", __func__);
2790		return (1);
2791	}
2792
2793	while ((c = getopt(argc, argv, combinedopt)) != -1) {
2794		switch (c) {
2795		case 'a':
2796			action = strtol(optarg, NULL, 0);
2797			break;
2798		default:
2799			break;
2800		}
2801	}
2802
2803	if (action < 0 || action > 2) {
2804		warn("action must be specified and in the range: 0-2");
2805		retval = 1;
2806		goto bailout;
2807	}
2808
2809
2810	datalen = 256;
2811	dataptr = (uint8_t *)malloc(datalen);
2812	if (dataptr == NULL) {
2813		warn("%s: can't allocate %d bytes", __func__, datalen);
2814	    	retval = 1;
2815		goto bailout;
2816	}
2817
2818	memset(dataptr, 0, datalen);
2819
2820	ctl_scsi_persistent_res_in(io,
2821				   /*data_ptr*/ dataptr,
2822				   /*data_len*/ datalen,
2823				   /*action*/   action,
2824				   /*tag_type*/ CTL_TAG_SIMPLE,
2825				   /*control*/  0);
2826
2827	io->io_hdr.nexus.targ_target.id = target;
2828	io->io_hdr.nexus.targ_lun = lun;
2829	io->io_hdr.nexus.initid = id;
2830
2831	if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2832		retval = 1;
2833		goto bailout;
2834	}
2835
2836	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2837		int returned_len, used_len;
2838
2839		returned_len = 0;
2840
2841		switch (action) {
2842		case 0:
2843			returned_len = scsi_4btoul(&dataptr[4]) + 8;
2844			returned_len = min(returned_len, 256);
2845			break;
2846		case 1:
2847			returned_len = scsi_4btoul(&dataptr[4]) + 8;
2848			break;
2849		case 2:
2850			returned_len = 8;
2851			break;
2852		default:
2853			warnx("%s: invalid action %d", __func__, action);
2854			goto bailout;
2855			break; /* NOTREACHED */
2856		}
2857
2858		for (used_len = 0; used_len < returned_len; used_len++) {
2859			fprintf(stdout, "0x%02x ", dataptr[used_len]);
2860			if (((used_len+1) % 8) == 0)
2861				fprintf(stdout, "\n");
2862		}
2863		fprintf(stdout, "\n");
2864	} else
2865		ctl_io_error_print(io, NULL, stderr);
2866
2867bailout:
2868	ctl_scsi_free_io(io);
2869
2870	if (dataptr != NULL)
2871		free(dataptr);
2872
2873	return (retval);
2874}
2875
2876static int
2877cctl_persistent_reserve_out(int fd, int target, int lun, int initiator,
2878			    int argc, char **argv, char *combinedopt,
2879			    int retry_count)
2880{
2881	union ctl_io *io;
2882	struct ctl_id id;
2883	uint32_t datalen;
2884	uint64_t key = 0, sa_key = 0;
2885	int action = -1, restype = -1;
2886	uint8_t *dataptr;
2887	int retval;
2888	int c;
2889
2890	id.id = initiator;
2891	retval = 0;
2892	dataptr = NULL;
2893
2894	io = ctl_scsi_alloc_io(id);
2895	if (io == NULL) {
2896		warn("%s: can't allocate memory", __func__);
2897		return (1);
2898	}
2899
2900	while ((c = getopt(argc, argv, combinedopt)) != -1) {
2901		switch (c) {
2902		case 'a':
2903			action = strtol(optarg, NULL, 0);
2904			break;
2905		case 'k':
2906			key = strtoull(optarg, NULL, 0);
2907			break;
2908		case 'r':
2909			restype = strtol(optarg, NULL, 0);
2910			break;
2911		case 's':
2912			sa_key = strtoull(optarg, NULL, 0);
2913			break;
2914		default:
2915			break;
2916		}
2917	}
2918	if (action < 0 || action > 5) {
2919		warn("action must be specified and in the range: 0-5");
2920		retval = 1;
2921		goto bailout;
2922	}
2923
2924	if (restype < 0 || restype > 5) {
2925		if (action != 0 && action != 5 && action != 3) {
2926			warn("'restype' must specified and in the range: 0-5");
2927			retval = 1;
2928			goto bailout;
2929		}
2930	}
2931
2932	datalen = 24;
2933	dataptr = (uint8_t *)malloc(datalen);
2934	if (dataptr == NULL) {
2935		warn("%s: can't allocate %d bytes", __func__, datalen);
2936		retval = 1;
2937		goto bailout;
2938	}
2939
2940	memset(dataptr, 0, datalen);
2941
2942	ctl_scsi_persistent_res_out(io,
2943				    /*data_ptr*/ dataptr,
2944				    /*data_len*/ datalen,
2945				    /*action*/   action,
2946				    /*type*/     restype,
2947				    /*key*/      key,
2948				    /*sa key*/   sa_key,
2949				    /*tag_type*/ CTL_TAG_SIMPLE,
2950				    /*control*/  0);
2951
2952	io->io_hdr.nexus.targ_target.id = target;
2953	io->io_hdr.nexus.targ_lun = lun;
2954	io->io_hdr.nexus.initid = id;
2955
2956	if (cctl_do_io(fd, retry_count, io, __func__) != 0) {
2957		retval = 1;
2958		goto bailout;
2959	}
2960	if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
2961		char scsi_path[40];
2962		ctl_scsi_path_string(io, scsi_path, sizeof(scsi_path));
2963		fprintf( stdout, "%sPERSISTENT RESERVE OUT executed "
2964			"successfully\n", scsi_path);
2965	} else
2966		ctl_io_error_print(io, NULL, stderr);
2967
2968bailout:
2969	ctl_scsi_free_io(io);
2970
2971	if (dataptr != NULL)
2972		free(dataptr);
2973
2974	return (retval);
2975}
2976
2977struct cctl_req_option {
2978	char			     *name;
2979	int			      namelen;
2980	char			     *value;
2981	int			      vallen;
2982	STAILQ_ENTRY(cctl_req_option) links;
2983};
2984
2985static int
2986cctl_create_lun(int fd, int argc, char **argv, char *combinedopt)
2987{
2988	struct ctl_lun_req req;
2989	int device_type = -1;
2990	uint64_t lun_size = 0;
2991	uint32_t blocksize = 0, req_lun_id = 0;
2992	char *serial_num = NULL;
2993	char *device_id = NULL;
2994	int lun_size_set = 0, blocksize_set = 0, lun_id_set = 0;
2995	char *backend_name = NULL;
2996	STAILQ_HEAD(, cctl_req_option) option_list;
2997	int num_options = 0;
2998	int retval = 0, c;
2999
3000	STAILQ_INIT(&option_list);
3001
3002	while ((c = getopt(argc, argv, combinedopt)) != -1) {
3003		switch (c) {
3004		case 'b':
3005			backend_name = strdup(optarg);
3006			break;
3007		case 'B':
3008			blocksize = strtoul(optarg, NULL, 0);
3009			blocksize_set = 1;
3010			break;
3011		case 'd':
3012			device_id = strdup(optarg);
3013			break;
3014		case 'l':
3015			req_lun_id = strtoul(optarg, NULL, 0);
3016			lun_id_set = 1;
3017			break;
3018		case 'o': {
3019			struct cctl_req_option *option;
3020			char *tmpstr;
3021			char *name, *value;
3022
3023			tmpstr = strdup(optarg);
3024			name = strsep(&tmpstr, "=");
3025			if (name == NULL) {
3026				warnx("%s: option -o takes \"name=value\""
3027				      "argument", __func__);
3028				retval = 1;
3029				goto bailout;
3030			}
3031			value = strsep(&tmpstr, "=");
3032			if (value == NULL) {
3033				warnx("%s: option -o takes \"name=value\""
3034				      "argument", __func__);
3035				retval = 1;
3036				goto bailout;
3037			}
3038			option = malloc(sizeof(*option));
3039			if (option == NULL) {
3040				warn("%s: error allocating %zd bytes",
3041				     __func__, sizeof(*option));
3042				retval = 1;
3043				goto bailout;
3044			}
3045			option->name = strdup(name);
3046			option->namelen = strlen(name) + 1;
3047			option->value = strdup(value);
3048			option->vallen = strlen(value) + 1;
3049			free(tmpstr);
3050
3051			STAILQ_INSERT_TAIL(&option_list, option, links);
3052			num_options++;
3053			break;
3054		}
3055		case 's':
3056			if (strcasecmp(optarg, "auto") != 0) {
3057				retval = expand_number(optarg, &lun_size);
3058				if (retval != 0) {
3059					warn("%s: invalid -s argument",
3060					    __func__);
3061					retval = 1;
3062					goto bailout;
3063				}
3064			}
3065			lun_size_set = 1;
3066			break;
3067		case 'S':
3068			serial_num = strdup(optarg);
3069			break;
3070		case 't':
3071			device_type = strtoul(optarg, NULL, 0);
3072			break;
3073		default:
3074			break;
3075		}
3076	}
3077
3078	if (backend_name == NULL) {
3079		warnx("%s: backend name (-b) must be specified", __func__);
3080		retval = 1;
3081		goto bailout;
3082	}
3083
3084	bzero(&req, sizeof(req));
3085
3086	strlcpy(req.backend, backend_name, sizeof(req.backend));
3087	req.reqtype = CTL_LUNREQ_CREATE;
3088
3089	if (blocksize_set != 0)
3090		req.reqdata.create.blocksize_bytes = blocksize;
3091
3092	if (lun_size_set != 0)
3093		req.reqdata.create.lun_size_bytes = lun_size;
3094
3095	if (lun_id_set != 0) {
3096		req.reqdata.create.flags |= CTL_LUN_FLAG_ID_REQ;
3097		req.reqdata.create.req_lun_id = req_lun_id;
3098	}
3099
3100	req.reqdata.create.flags |= CTL_LUN_FLAG_DEV_TYPE;
3101
3102	if (device_type != -1)
3103		req.reqdata.create.device_type = device_type;
3104	else
3105		req.reqdata.create.device_type = T_DIRECT;
3106
3107	if (serial_num != NULL) {
3108		strlcpy(req.reqdata.create.serial_num, serial_num,
3109			sizeof(req.reqdata.create.serial_num));
3110		req.reqdata.create.flags |= CTL_LUN_FLAG_SERIAL_NUM;
3111	}
3112
3113	if (device_id != NULL) {
3114		strlcpy(req.reqdata.create.device_id, device_id,
3115			sizeof(req.reqdata.create.device_id));
3116		req.reqdata.create.flags |= CTL_LUN_FLAG_DEVID;
3117	}
3118
3119	req.num_be_args = num_options;
3120	if (num_options > 0) {
3121		struct cctl_req_option *option, *next_option;
3122		int i;
3123
3124		req.be_args = malloc(num_options * sizeof(*req.be_args));
3125		if (req.be_args == NULL) {
3126			warn("%s: error allocating %zd bytes", __func__,
3127			     num_options * sizeof(*req.be_args));
3128			retval = 1;
3129			goto bailout;
3130		}
3131
3132		for (i = 0, option = STAILQ_FIRST(&option_list);
3133		     i < num_options; i++, option = next_option) {
3134			next_option = STAILQ_NEXT(option, links);
3135
3136			req.be_args[i].namelen = option->namelen;
3137			req.be_args[i].name = strdup(option->name);
3138			req.be_args[i].vallen = option->vallen;
3139			req.be_args[i].value = strdup(option->value);
3140			/*
3141			 * XXX KDM do we want a way to specify a writeable
3142			 * flag of some sort?  Do we want a way to specify
3143			 * binary data?
3144			 */
3145			req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3146
3147			STAILQ_REMOVE(&option_list, option, cctl_req_option,
3148				      links);
3149			free(option->name);
3150			free(option->value);
3151			free(option);
3152		}
3153	}
3154
3155	if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3156		warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3157		retval = 1;
3158		goto bailout;
3159	}
3160
3161	if (req.status == CTL_LUN_ERROR) {
3162		warnx("%s: error returned from LUN creation request:\n%s",
3163		      __func__, req.error_str);
3164		retval = 1;
3165		goto bailout;
3166	} else if (req.status != CTL_LUN_OK) {
3167		warnx("%s: unknown LUN creation request status %d",
3168		      __func__, req.status);
3169		retval = 1;
3170		goto bailout;
3171	}
3172
3173	fprintf(stdout, "LUN created successfully\n");
3174	fprintf(stdout, "backend:       %s\n", req.backend);
3175	fprintf(stdout, "device type:   %d\n",req.reqdata.create.device_type);
3176	fprintf(stdout, "LUN size:      %ju bytes\n",
3177		(uintmax_t)req.reqdata.create.lun_size_bytes);
3178	fprintf(stdout, "blocksize      %u bytes\n",
3179		req.reqdata.create.blocksize_bytes);
3180	fprintf(stdout, "LUN ID:        %d\n", req.reqdata.create.req_lun_id);
3181	fprintf(stdout, "Serial Number: %s\n", req.reqdata.create.serial_num);
3182	fprintf(stdout, "Device ID;     %s\n", req.reqdata.create.device_id);
3183
3184bailout:
3185	return (retval);
3186}
3187
3188static int
3189cctl_rm_lun(int fd, int argc, char **argv, char *combinedopt)
3190{
3191	struct ctl_lun_req req;
3192	uint32_t lun_id = 0;
3193	int lun_id_set = 0;
3194	char *backend_name = NULL;
3195	STAILQ_HEAD(, cctl_req_option) option_list;
3196	int num_options = 0;
3197	int retval = 0, c;
3198
3199	STAILQ_INIT(&option_list);
3200
3201	while ((c = getopt(argc, argv, combinedopt)) != -1) {
3202		switch (c) {
3203		case 'b':
3204			backend_name = strdup(optarg);
3205			break;
3206		case 'l':
3207			lun_id = strtoul(optarg, NULL, 0);
3208			lun_id_set = 1;
3209			break;
3210		case 'o': {
3211			struct cctl_req_option *option;
3212			char *tmpstr;
3213			char *name, *value;
3214
3215			tmpstr = strdup(optarg);
3216			name = strsep(&tmpstr, "=");
3217			if (name == NULL) {
3218				warnx("%s: option -o takes \"name=value\""
3219				      "argument", __func__);
3220				retval = 1;
3221				goto bailout;
3222			}
3223			value = strsep(&tmpstr, "=");
3224			if (value == NULL) {
3225				warnx("%s: option -o takes \"name=value\""
3226				      "argument", __func__);
3227				retval = 1;
3228				goto bailout;
3229			}
3230			option = malloc(sizeof(*option));
3231			if (option == NULL) {
3232				warn("%s: error allocating %zd bytes",
3233				     __func__, sizeof(*option));
3234				retval = 1;
3235				goto bailout;
3236			}
3237			option->name = strdup(name);
3238			option->namelen = strlen(name) + 1;
3239			option->value = strdup(value);
3240			option->vallen = strlen(value) + 1;
3241			free(tmpstr);
3242
3243			STAILQ_INSERT_TAIL(&option_list, option, links);
3244			num_options++;
3245			break;
3246		}
3247		default:
3248			break;
3249		}
3250	}
3251
3252	if (backend_name == NULL)
3253		errx(1, "%s: backend name (-b) must be specified", __func__);
3254
3255	if (lun_id_set == 0)
3256		errx(1, "%s: LUN id (-l) must be specified", __func__);
3257
3258	bzero(&req, sizeof(req));
3259
3260	strlcpy(req.backend, backend_name, sizeof(req.backend));
3261	req.reqtype = CTL_LUNREQ_RM;
3262
3263	req.reqdata.rm.lun_id = lun_id;
3264
3265	req.num_be_args = num_options;
3266	if (num_options > 0) {
3267		struct cctl_req_option *option, *next_option;
3268		int i;
3269
3270		req.be_args = malloc(num_options * sizeof(*req.be_args));
3271		if (req.be_args == NULL) {
3272			warn("%s: error allocating %zd bytes", __func__,
3273			     num_options * sizeof(*req.be_args));
3274			retval = 1;
3275			goto bailout;
3276		}
3277
3278		for (i = 0, option = STAILQ_FIRST(&option_list);
3279		     i < num_options; i++, option = next_option) {
3280			next_option = STAILQ_NEXT(option, links);
3281
3282			req.be_args[i].namelen = option->namelen;
3283			req.be_args[i].name = strdup(option->name);
3284			req.be_args[i].vallen = option->vallen;
3285			req.be_args[i].value = strdup(option->value);
3286			/*
3287			 * XXX KDM do we want a way to specify a writeable
3288			 * flag of some sort?  Do we want a way to specify
3289			 * binary data?
3290			 */
3291			req.be_args[i].flags = CTL_BEARG_ASCII | CTL_BEARG_RD;
3292
3293			STAILQ_REMOVE(&option_list, option, cctl_req_option,
3294				      links);
3295			free(option->name);
3296			free(option->value);
3297			free(option);
3298		}
3299	}
3300
3301	if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3302		warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3303		retval = 1;
3304		goto bailout;
3305	}
3306
3307	if (req.status == CTL_LUN_ERROR) {
3308		warnx("%s: error returned from LUN removal request:\n%s",
3309		      __func__, req.error_str);
3310		retval = 1;
3311		goto bailout;
3312	} else if (req.status != CTL_LUN_OK) {
3313		warnx("%s: unknown LUN removal request status %d",
3314		      __func__, req.status);
3315		retval = 1;
3316		goto bailout;
3317	}
3318
3319	printf("LUN %d deleted successfully\n", lun_id);
3320
3321bailout:
3322	return (retval);
3323}
3324
3325static int
3326cctl_modify_lun(int fd, int argc, char **argv, char *combinedopt)
3327{
3328	struct ctl_lun_req req;
3329	uint64_t lun_size = 0;
3330	uint32_t lun_id = 0;
3331	int lun_id_set = 0, lun_size_set = 0;
3332	char *backend_name = NULL;
3333	int retval = 0, c;
3334
3335	while ((c = getopt(argc, argv, combinedopt)) != -1) {
3336		switch (c) {
3337		case 'b':
3338			backend_name = strdup(optarg);
3339			break;
3340		case 'l':
3341			lun_id = strtoul(optarg, NULL, 0);
3342			lun_id_set = 1;
3343			break;
3344		case 's':
3345			if (strcasecmp(optarg, "auto") != 0) {
3346				retval = expand_number(optarg, &lun_size);
3347				if (retval != 0) {
3348					warn("%s: invalid -s argument",
3349					    __func__);
3350					retval = 1;
3351					goto bailout;
3352				}
3353			}
3354			lun_size_set = 1;
3355			break;
3356		default:
3357			break;
3358		}
3359	}
3360
3361	if (backend_name == NULL)
3362		errx(1, "%s: backend name (-b) must be specified", __func__);
3363
3364	if (lun_id_set == 0)
3365		errx(1, "%s: LUN id (-l) must be specified", __func__);
3366
3367	if (lun_size_set == 0)
3368		errx(1, "%s: size (-s) must be specified", __func__);
3369
3370	bzero(&req, sizeof(req));
3371
3372	strlcpy(req.backend, backend_name, sizeof(req.backend));
3373	req.reqtype = CTL_LUNREQ_MODIFY;
3374
3375	req.reqdata.modify.lun_id = lun_id;
3376	req.reqdata.modify.lun_size_bytes = lun_size;
3377
3378	if (ioctl(fd, CTL_LUN_REQ, &req) == -1) {
3379		warn("%s: error issuing CTL_LUN_REQ ioctl", __func__);
3380		retval = 1;
3381		goto bailout;
3382	}
3383
3384	if (req.status == CTL_LUN_ERROR) {
3385		warnx("%s: error returned from LUN modification request:\n%s",
3386		      __func__, req.error_str);
3387		retval = 1;
3388		goto bailout;
3389	} else if (req.status != CTL_LUN_OK) {
3390		warnx("%s: unknown LUN modification request status %d",
3391		      __func__, req.status);
3392		retval = 1;
3393		goto bailout;
3394	}
3395
3396	printf("LUN %d modified successfully\n", lun_id);
3397
3398bailout:
3399	return (retval);
3400}
3401
3402
3403/*
3404 * Name/value pair used for per-LUN attributes.
3405 */
3406struct cctl_lun_nv {
3407	char *name;
3408	char *value;
3409	STAILQ_ENTRY(cctl_lun_nv) links;
3410};
3411
3412/*
3413 * Backend LUN information.
3414 */
3415struct cctl_lun {
3416	uint64_t lun_id;
3417	char *backend_type;
3418	uint64_t size_blocks;
3419	uint32_t blocksize;
3420	char *serial_number;
3421	char *device_id;
3422	STAILQ_HEAD(,cctl_lun_nv) attr_list;
3423	STAILQ_ENTRY(cctl_lun) links;
3424};
3425
3426struct cctl_devlist_data {
3427	int num_luns;
3428	STAILQ_HEAD(,cctl_lun) lun_list;
3429	struct cctl_lun *cur_lun;
3430	int level;
3431	struct sbuf *cur_sb[32];
3432};
3433
3434static void
3435cctl_start_element(void *user_data, const char *name, const char **attr)
3436{
3437	int i;
3438	struct cctl_devlist_data *devlist;
3439	struct cctl_lun *cur_lun;
3440
3441	devlist = (struct cctl_devlist_data *)user_data;
3442	cur_lun = devlist->cur_lun;
3443	devlist->level++;
3444	if ((u_int)devlist->level > (sizeof(devlist->cur_sb) /
3445	    sizeof(devlist->cur_sb[0])))
3446		errx(1, "%s: too many nesting levels, %zd max", __func__,
3447		     sizeof(devlist->cur_sb) / sizeof(devlist->cur_sb[0]));
3448
3449	devlist->cur_sb[devlist->level] = sbuf_new_auto();
3450	if (devlist->cur_sb[devlist->level] == NULL)
3451		err(1, "%s: Unable to allocate sbuf", __func__);
3452
3453	if (strcmp(name, "lun") == 0) {
3454		if (cur_lun != NULL)
3455			errx(1, "%s: improper lun element nesting", __func__);
3456
3457		cur_lun = calloc(1, sizeof(*cur_lun));
3458		if (cur_lun == NULL)
3459			err(1, "%s: cannot allocate %zd bytes", __func__,
3460			    sizeof(*cur_lun));
3461
3462		devlist->num_luns++;
3463		devlist->cur_lun = cur_lun;
3464
3465		STAILQ_INIT(&cur_lun->attr_list);
3466		STAILQ_INSERT_TAIL(&devlist->lun_list, cur_lun, links);
3467
3468		for (i = 0; attr[i] != NULL; i += 2) {
3469			if (strcmp(attr[i], "id") == 0) {
3470				cur_lun->lun_id = strtoull(attr[i+1], NULL, 0);
3471			} else {
3472				errx(1, "%s: invalid LUN attribute %s = %s",
3473				     __func__, attr[i], attr[i+1]);
3474			}
3475		}
3476	}
3477}
3478
3479static void
3480cctl_end_element(void *user_data, const char *name)
3481{
3482	struct cctl_devlist_data *devlist;
3483	struct cctl_lun *cur_lun;
3484	char *str;
3485
3486	devlist = (struct cctl_devlist_data *)user_data;
3487	cur_lun = devlist->cur_lun;
3488
3489	if ((cur_lun == NULL)
3490	 && (strcmp(name, "ctllunlist") != 0))
3491		errx(1, "%s: cur_lun == NULL! (name = %s)", __func__, name);
3492
3493	if (devlist->cur_sb[devlist->level] == NULL)
3494		errx(1, "%s: no valid sbuf at level %d (name %s)", __func__,
3495		     devlist->level, name);
3496
3497	sbuf_finish(devlist->cur_sb[devlist->level]);
3498	str = strdup(sbuf_data(devlist->cur_sb[devlist->level]));
3499	if (str == NULL)
3500		err(1, "%s can't allocate %zd bytes for string", __func__,
3501		    sbuf_len(devlist->cur_sb[devlist->level]));
3502
3503	if (strlen(str) == 0) {
3504		free(str);
3505		str = NULL;
3506	}
3507
3508	sbuf_delete(devlist->cur_sb[devlist->level]);
3509	devlist->cur_sb[devlist->level] = NULL;
3510	devlist->level--;
3511
3512	if (strcmp(name, "backend_type") == 0) {
3513		cur_lun->backend_type = str;
3514		str = NULL;
3515	} else if (strcmp(name, "size") == 0) {
3516		cur_lun->size_blocks = strtoull(str, NULL, 0);
3517	} else if (strcmp(name, "blocksize") == 0) {
3518		cur_lun->blocksize = strtoul(str, NULL, 0);
3519	} else if (strcmp(name, "serial_number") == 0) {
3520		cur_lun->serial_number = str;
3521		str = NULL;
3522	} else if (strcmp(name, "device_id") == 0) {
3523		cur_lun->device_id = str;
3524		str = NULL;
3525	} else if (strcmp(name, "lun") == 0) {
3526		devlist->cur_lun = NULL;
3527	} else if (strcmp(name, "ctllunlist") == 0) {
3528
3529	} else {
3530		struct cctl_lun_nv *nv;
3531
3532		nv = calloc(1, sizeof(*nv));
3533		if (nv == NULL)
3534			err(1, "%s: can't allocate %zd bytes for nv pair",
3535			    __func__, sizeof(*nv));
3536
3537		nv->name = strdup(name);
3538		if (nv->name == NULL)
3539			err(1, "%s: can't allocated %zd bytes for string",
3540			    __func__, strlen(name));
3541
3542		nv->value = str;
3543		str = NULL;
3544		STAILQ_INSERT_TAIL(&cur_lun->attr_list, nv, links);
3545	}
3546
3547	free(str);
3548}
3549
3550static void
3551cctl_char_handler(void *user_data, const XML_Char *str, int len)
3552{
3553	struct cctl_devlist_data *devlist;
3554
3555	devlist = (struct cctl_devlist_data *)user_data;
3556
3557	sbuf_bcat(devlist->cur_sb[devlist->level], str, len);
3558}
3559
3560static int
3561cctl_devlist(int fd, int argc, char **argv, char *combinedopt)
3562{
3563	struct ctl_lun_list list;
3564	struct cctl_devlist_data devlist;
3565	struct cctl_lun *lun;
3566	XML_Parser parser;
3567	char *lun_str;
3568	int lun_len;
3569	int dump_xml = 0;
3570	int retval, c;
3571	char *backend = NULL;
3572	int verbose = 0;
3573
3574	retval = 0;
3575	lun_len = 4096;
3576
3577	bzero(&devlist, sizeof(devlist));
3578	STAILQ_INIT(&devlist.lun_list);
3579
3580	while ((c = getopt(argc, argv, combinedopt)) != -1) {
3581		switch (c) {
3582		case 'b':
3583			backend = strdup(optarg);
3584			break;
3585		case 'v':
3586			verbose++;
3587			break;
3588		case 'x':
3589			dump_xml = 1;
3590			break;
3591		default:
3592			break;
3593		}
3594	}
3595
3596retry:
3597	lun_str = malloc(lun_len);
3598
3599	bzero(&list, sizeof(list));
3600	list.alloc_len = lun_len;
3601	list.status = CTL_LUN_LIST_NONE;
3602	list.lun_xml = lun_str;
3603
3604	if (ioctl(fd, CTL_LUN_LIST, &list) == -1) {
3605		warn("%s: error issuing CTL_LUN_LIST ioctl", __func__);
3606		retval = 1;
3607		goto bailout;
3608	}
3609
3610	if (list.status == CTL_LUN_LIST_ERROR) {
3611		warnx("%s: error returned from CTL_LUN_LIST ioctl:\n%s",
3612		      __func__, list.error_str);
3613	} else if (list.status == CTL_LUN_LIST_NEED_MORE_SPACE) {
3614		lun_len = lun_len << 1;
3615		goto retry;
3616	}
3617
3618	if (dump_xml != 0) {
3619		printf("%s", lun_str);
3620		goto bailout;
3621	}
3622
3623	parser = XML_ParserCreate(NULL);
3624	if (parser == NULL) {
3625		warn("%s: Unable to create XML parser", __func__);
3626		retval = 1;
3627		goto bailout;
3628	}
3629
3630	XML_SetUserData(parser, &devlist);
3631	XML_SetElementHandler(parser, cctl_start_element, cctl_end_element);
3632	XML_SetCharacterDataHandler(parser, cctl_char_handler);
3633
3634	retval = XML_Parse(parser, lun_str, strlen(lun_str), 1);
3635	XML_ParserFree(parser);
3636	if (retval != 1) {
3637		retval = 1;
3638		goto bailout;
3639	}
3640
3641	printf("LUN Backend  %18s %4s %-16s %-16s\n", "Size (Blocks)", "BS",
3642	       "Serial Number", "Device ID");
3643	STAILQ_FOREACH(lun, &devlist.lun_list, links) {
3644		struct cctl_lun_nv *nv;
3645
3646		if ((backend != NULL)
3647		 && (strcmp(lun->backend_type, backend) != 0))
3648			continue;
3649
3650		printf("%3ju %-8s %18ju %4u %-16s %-16s\n",
3651		       (uintmax_t)lun->lun_id,
3652		       lun->backend_type, (uintmax_t)lun->size_blocks,
3653		       lun->blocksize, lun->serial_number, lun->device_id);
3654
3655		if (verbose == 0)
3656			continue;
3657
3658		STAILQ_FOREACH(nv, &lun->attr_list, links) {
3659			printf("      %s=%s\n", nv->name, nv->value);
3660		}
3661	}
3662bailout:
3663	free(lun_str);
3664
3665	return (retval);
3666}
3667
3668void
3669usage(int error)
3670{
3671	fprintf(error ? stderr : stdout,
3672"Usage:\n"
3673"Primary commands:\n"
3674"         ctladm tur         [dev_id][general options]\n"
3675"         ctladm inquiry     [dev_id][general options]\n"
3676"         ctladm devid       [dev_id][general options]\n"
3677"         ctladm reqsense    [dev_id][general options]\n"
3678"         ctladm reportluns  [dev_id][general options]\n"
3679"         ctladm read        [dev_id][general options] <-l lba> <-d len>\n"
3680"                            <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3681"         ctladm write       [dev_id][general options] <-l lba> <-d len>\n"
3682"                            <-f file|-> <-b blocksize> [-c cdbsize][-N]\n"
3683"         ctladm readcap     [dev_id][general options] [-c cdbsize]\n"
3684"         ctladm modesense   [dev_id][general options] <-m page|-l> [-P pc]\n"
3685"                            [-d] [-S subpage] [-c cdbsize]\n"
3686"         ctladm prin        [dev_id][general options] <-a action>\n"
3687"         ctladm prout       [dev_id][general options] <-a action>\n"
3688"                            <-r restype] [-k key] [-s sa_key]\n"
3689"         ctladm rtpg        [dev_id][general options]\n"
3690"         ctladm start       [dev_id][general options] [-i] [-o]\n"
3691"         ctladm stop        [dev_id][general options] [-i] [-o]\n"
3692"         ctladm synccache   [dev_id][general options] [-l lba]\n"
3693"                            [-b blockcount] [-r] [-i] [-c cdbsize]\n"
3694"         ctladm create      <-b backend> [-B blocksize] [-d device_id]\n"
3695"                            [-l lun_id] [-o name=value] [-s size_bytes]\n"
3696"                            [-S serial_num] [-t dev_type]\n"
3697"         ctladm remove      <-b backend> <-l lun_id> [-o name=value]\n"
3698"         ctladm modify      <-b backend> <-l lun_id> <-s size_bytes>\n"
3699"         ctladm devlist     [-b][-v][-x]\n"
3700"         ctladm shutdown\n"
3701"         ctladm startup\n"
3702"         ctladm hardstop\n"
3703"         ctladm hardstart\n"
3704"         ctladm lunlist\n"
3705"         ctladm bbrread     [dev_id] <-l lba> <-d datalen>\n"
3706"         ctladm delay       [dev_id] <-l datamove|done> [-T oneshot|cont]\n"
3707"                            [-t secs]\n"
3708"         ctladm realsync    <on|off|query>\n"
3709"         ctladm setsync     [dev_id] <-i interval>\n"
3710"         ctladm getsync     [dev_id]\n"
3711"         ctladm inject      [dev_id] <-i action> <-p pattern> [-r lba,len]\n"
3712"                            [-s len fmt [args]] [-c] [-d delete_id]\n"
3713"         ctladm port        <-l | -o <on|off> | [-w wwnn][-W wwpn]>\n"
3714"                            [-p targ_port] [-t port_type] [-q] [-x]\n"
3715"         ctladm dumpooa\n"
3716"         ctladm dumpstructs\n"
3717"         ctladm help\n"
3718"General Options:\n"
3719"-I intiator_id           : defaults to 7, used to change the initiator id\n"
3720"-C retries               : specify the number of times to retry this command\n"
3721"-D devicename            : specify the device to operate on\n"
3722"                         : (default is %s)\n"
3723"read/write options:\n"
3724"-l lba                   : logical block address\n"
3725"-d len                   : read/write length, in blocks\n"
3726"-f file|-                : write/read data to/from file or stdout/stdin\n"
3727"-b blocksize             : block size, in bytes\n"
3728"-c cdbsize               : specify minimum cdb size: 6, 10, 12 or 16\n"
3729"-N                       : do not copy data to/from userland\n"
3730"readcapacity options:\n"
3731"-c cdbsize               : specify minimum cdb size: 10 or 16\n"
3732"modesense options:\n"
3733"-m page                  : specify the mode page to view\n"
3734"-l                       : request a list of supported pages\n"
3735"-P pc                    : specify the page control value: 0-3 (current,\n"
3736"                           changeable, default, saved, respectively)\n"
3737"-d                       : disable block descriptors for mode sense\n"
3738"-S subpage               : specify a subpage\n"
3739"-c cdbsize               : specify minimum cdb size: 6 or 10\n"
3740"persistent reserve in options:\n"
3741"-a action                : specify the action value: 0-2 (read key, read\n"
3742"                           reservation, read capabilities, respectively)\n"
3743"persistent reserve out options:\n"
3744"-a action                : specify the action value: 0-5 (register, reserve,\n"
3745"                           release, clear, preempt, register and ignore)\n"
3746"-k key                   : key value\n"
3747"-s sa_key                : service action value\n"
3748"-r restype               : specify the reservation type: 0-5(wr ex, ex ac,\n"
3749"                           wr ex ro, ex ac ro, wr ex ar, ex ac ar)\n"
3750"start/stop options:\n"
3751"-i                       : set the immediate bit (CTL does not support this)\n"
3752"-o                       : set the on/offline bit\n"
3753"synccache options:\n"
3754"-l lba                   : set the starting LBA\n"
3755"-b blockcount            : set the length to sync in blocks\n"
3756"-r                       : set the relative addressing bit\n"
3757"-i                       : set the immediate bit\n"
3758"-c cdbsize               : specify minimum cdb size: 10 or 16\n"
3759"create options:\n"
3760"-b backend               : backend name (\"block\", \"ramdisk\", etc.)\n"
3761"-B blocksize             : LUN blocksize in bytes (some backends)\n"
3762"-d device_id             : SCSI VPD page 0x83 ID\n"
3763"-l lun_id                : requested LUN number\n"
3764"-o name=value            : backend-specific options, multiple allowed\n"
3765"-s size_bytes            : LUN size in bytes (some backends)\n"
3766"-S serial_num            : SCSI VPD page 0x80 serial number\n"
3767"-t dev_type              : SCSI device type (0=disk, 3=processor)\n"
3768"remove options:\n"
3769"-b backend               : backend name (\"block\", \"ramdisk\", etc.)\n"
3770"-l lun_id                : LUN number to delete\n"
3771"-o name=value            : backend-specific options, multiple allowed\n"
3772"devlist options:\n"
3773"-b backend               : list devices from specified backend only\n"
3774"-v                       : be verbose, show backend attributes\n"
3775"-x                       : dump raw XML\n"
3776"delay options:\n"
3777"-l datamove|done         : delay command at datamove or done phase\n"
3778"-T oneshot               : delay one command, then resume normal completion\n"
3779"-T cont                  : delay all commands\n"
3780"-t secs                  : number of seconds to delay\n"
3781"inject options:\n"
3782"-i error_action          : action to perform\n"
3783"-p pattern               : command pattern to look for\n"
3784"-r lba,len               : LBA range for pattern\n"
3785"-s len fmt [args]        : sense data for custom sense action\n"
3786"-c                       : continuous operation\n"
3787"-d delete_id             : error id to delete\n"
3788"port options:\n"
3789"-l                       : list frontend ports\n"
3790"-o on|off                : turn frontend ports on or off\n"
3791"-w wwnn                  : set WWNN for one frontend\n"
3792"-W wwpn                  : set WWPN for one frontend\n"
3793"-t port_type             : specify fc, scsi, ioctl, internal frontend type\n"
3794"-p targ_port             : specify target port number\n"
3795"-q                       : omit header in list output\n"
3796"-x                       : output port list in XML format\n"
3797"bbrread options:\n"
3798"-l lba                   : starting LBA\n"
3799"-d datalen               : length, in bytes, to read\n",
3800CTL_DEFAULT_DEV);
3801}
3802
3803int
3804main(int argc, char **argv)
3805{
3806	int option_index, c;
3807	ctladm_cmdfunction command;
3808	ctladm_cmdargs cmdargs;
3809	ctladm_optret optreturn;
3810	char *device;
3811	const char *mainopt = "C:D:I:";
3812	const char *subopt = NULL;
3813	char combinedopt[256];
3814	int target, lun;
3815	int optstart = 2;
3816	int retval, fd;
3817	int retries, timeout;
3818	int initid;
3819
3820	option_index = 0;
3821	retval = 0;
3822	cmdargs = CTLADM_ARG_NONE;
3823	command = CTLADM_CMD_HELP;
3824	device = NULL;
3825	fd = -1;
3826	retries = 0;
3827	target = 0;
3828	lun = 0;
3829	timeout = 0;
3830	initid = 7;
3831
3832	if (argc < 2) {
3833		usage(1);
3834		retval = 1;
3835		goto bailout;
3836	}
3837
3838	/*
3839	 * Get the base option.
3840	 */
3841	optreturn = getoption(option_table,argv[1], &command, &cmdargs,&subopt);
3842
3843	if (optreturn == CC_OR_AMBIGUOUS) {
3844		warnx("ambiguous option %s", argv[1]);
3845		usage(0);
3846		exit(1);
3847	} else if (optreturn == CC_OR_NOT_FOUND) {
3848		warnx("option %s not found", argv[1]);
3849		usage(0);
3850		exit(1);
3851	}
3852
3853	if (cmdargs & CTLADM_ARG_NEED_TL) {
3854	 	if ((argc < 3)
3855		 || (!isdigit(argv[2][0]))) {
3856			warnx("option %s requires a target:lun argument",
3857			      argv[1]);
3858			usage(0);
3859			exit(1);
3860		}
3861		retval = cctl_parse_tl(argv[2], &target, &lun);
3862		if (retval != 0)
3863			errx(1, "invalid target:lun argument %s", argv[2]);
3864
3865		cmdargs |= CTLADM_ARG_TARG_LUN;
3866		optstart++;
3867	}
3868
3869	/*
3870	 * Ahh, getopt(3) is a pain.
3871	 *
3872	 * This is a gross hack.  There really aren't many other good
3873	 * options (excuse the pun) for parsing options in a situation like
3874	 * this.  getopt is kinda braindead, so you end up having to run
3875	 * through the options twice, and give each invocation of getopt
3876	 * the option string for the other invocation.
3877	 *
3878	 * You would think that you could just have two groups of options.
3879	 * The first group would get parsed by the first invocation of
3880	 * getopt, and the second group would get parsed by the second
3881	 * invocation of getopt.  It doesn't quite work out that way.  When
3882	 * the first invocation of getopt finishes, it leaves optind pointing
3883	 * to the argument _after_ the first argument in the second group.
3884	 * So when the second invocation of getopt comes around, it doesn't
3885	 * recognize the first argument it gets and then bails out.
3886	 *
3887	 * A nice alternative would be to have a flag for getopt that says
3888	 * "just keep parsing arguments even when you encounter an unknown
3889	 * argument", but there isn't one.  So there's no real clean way to
3890	 * easily parse two sets of arguments without having one invocation
3891	 * of getopt know about the other.
3892	 *
3893	 * Without this hack, the first invocation of getopt would work as
3894	 * long as the generic arguments are first, but the second invocation
3895	 * (in the subfunction) would fail in one of two ways.  In the case
3896	 * where you don't set optreset, it would fail because optind may be
3897	 * pointing to the argument after the one it should be pointing at.
3898	 * In the case where you do set optreset, and reset optind, it would
3899	 * fail because getopt would run into the first set of options, which
3900	 * it doesn't understand.
3901	 *
3902	 * All of this would "sort of" work if you could somehow figure out
3903	 * whether optind had been incremented one option too far.  The
3904	 * mechanics of that, however, are more daunting than just giving
3905	 * both invocations all of the expect options for either invocation.
3906	 *
3907	 * Needless to say, I wouldn't mind if someone invented a better
3908	 * (non-GPL!) command line parsing interface than getopt.  I
3909	 * wouldn't mind if someone added more knobs to getopt to make it
3910	 * work better.  Who knows, I may talk myself into doing it someday,
3911	 * if the standards weenies let me.  As it is, it just leads to
3912	 * hackery like this and causes people to avoid it in some cases.
3913	 *
3914	 * KDM, September 8th, 1998
3915	 */
3916	if (subopt != NULL)
3917		sprintf(combinedopt, "%s%s", mainopt, subopt);
3918	else
3919		sprintf(combinedopt, "%s", mainopt);
3920
3921	/*
3922	 * Start getopt processing at argv[2/3], since we've already
3923	 * accepted argv[1..2] as the command name, and as a possible
3924	 * device name.
3925	 */
3926	optind = optstart;
3927
3928	/*
3929	 * Now we run through the argument list looking for generic
3930	 * options, and ignoring options that possibly belong to
3931	 * subfunctions.
3932	 */
3933	while ((c = getopt(argc, argv, combinedopt))!= -1){
3934		switch (c) {
3935		case 'C':
3936			cmdargs |= CTLADM_ARG_RETRIES;
3937			retries = strtol(optarg, NULL, 0);
3938			break;
3939		case 'D':
3940			device = strdup(optarg);
3941			cmdargs |= CTLADM_ARG_DEVICE;
3942			break;
3943		case 'I':
3944			cmdargs |= CTLADM_ARG_INITIATOR;
3945			initid = strtol(optarg, NULL, 0);
3946			break;
3947		default:
3948			break;
3949		}
3950	}
3951
3952	if ((cmdargs & CTLADM_ARG_INITIATOR) == 0)
3953		initid = 7;
3954
3955	optind = optstart;
3956	optreset = 1;
3957
3958	/*
3959	 * Default to opening the CTL device for now.
3960	 */
3961	if (((cmdargs & CTLADM_ARG_DEVICE) == 0)
3962	 && (command != CTLADM_CMD_HELP)) {
3963		device = strdup(CTL_DEFAULT_DEV);
3964		cmdargs |= CTLADM_ARG_DEVICE;
3965	}
3966
3967	if ((cmdargs & CTLADM_ARG_DEVICE)
3968	 && (command != CTLADM_CMD_HELP)) {
3969		fd = open(device, O_RDWR);
3970		if (fd == -1) {
3971			fprintf(stderr, "%s: error opening %s: %s\n",
3972				argv[0], device, strerror(errno));
3973			retval = 1;
3974			goto bailout;
3975		}
3976	} else if ((command != CTLADM_CMD_HELP)
3977		&& ((cmdargs & CTLADM_ARG_DEVICE) == 0)) {
3978		fprintf(stderr, "%s: you must specify a device with the "
3979			"--device argument for this command\n", argv[0]);
3980		command = CTLADM_CMD_HELP;
3981		retval = 1;
3982	}
3983
3984	switch (command) {
3985	case CTLADM_CMD_TUR:
3986		retval = cctl_tur(fd, target, lun, initid, retries);
3987		break;
3988	case CTLADM_CMD_INQUIRY:
3989		retval = cctl_inquiry(fd, target, lun, initid, retries);
3990		break;
3991	case CTLADM_CMD_REQ_SENSE:
3992		retval = cctl_req_sense(fd, target, lun, initid, retries);
3993		break;
3994	case CTLADM_CMD_REPORT_LUNS:
3995		retval = cctl_report_luns(fd, target, lun, initid, retries);
3996		break;
3997	case CTLADM_CMD_CREATE:
3998		retval = cctl_create_lun(fd, argc, argv, combinedopt);
3999		break;
4000	case CTLADM_CMD_RM:
4001		retval = cctl_rm_lun(fd, argc, argv, combinedopt);
4002		break;
4003	case CTLADM_CMD_DEVLIST:
4004		retval = cctl_devlist(fd, argc, argv, combinedopt);
4005		break;
4006	case CTLADM_CMD_READ:
4007	case CTLADM_CMD_WRITE:
4008		retval = cctl_read_write(fd, target, lun, initid, retries,
4009					 argc, argv, combinedopt, command);
4010		break;
4011	case CTLADM_CMD_PORT:
4012		retval = cctl_port(fd, argc, argv, combinedopt);
4013		break;
4014	case CTLADM_CMD_READCAPACITY:
4015		retval = cctl_read_capacity(fd, target, lun, initid, retries,
4016					    argc, argv, combinedopt);
4017		break;
4018	case CTLADM_CMD_MODESENSE:
4019		retval = cctl_mode_sense(fd, target, lun, initid, retries,
4020					 argc, argv, combinedopt);
4021		break;
4022	case CTLADM_CMD_START:
4023	case CTLADM_CMD_STOP:
4024		retval = cctl_start_stop(fd, target, lun, initid, retries,
4025					 (command == CTLADM_CMD_START) ? 1 : 0,
4026					 argc, argv, combinedopt);
4027		break;
4028	case CTLADM_CMD_SYNC_CACHE:
4029		retval = cctl_sync_cache(fd, target, lun, initid, retries,
4030					 argc, argv, combinedopt);
4031		break;
4032	case CTLADM_CMD_SHUTDOWN:
4033	case CTLADM_CMD_STARTUP:
4034		retval = cctl_startup_shutdown(fd, target, lun, initid,
4035					       command);
4036		break;
4037	case CTLADM_CMD_HARDSTOP:
4038	case CTLADM_CMD_HARDSTART:
4039		retval = cctl_hardstopstart(fd, command);
4040		break;
4041	case CTLADM_CMD_BBRREAD:
4042		retval = cctl_bbrread(fd, target, lun, initid, argc, argv,
4043				      combinedopt);
4044		break;
4045	case CTLADM_CMD_LUNLIST:
4046		retval = cctl_lunlist(fd);
4047		break;
4048	case CTLADM_CMD_DELAY:
4049		retval = cctl_delay(fd, target, lun, argc, argv, combinedopt);
4050		break;
4051	case CTLADM_CMD_REALSYNC:
4052		retval = cctl_realsync(fd, argc, argv);
4053		break;
4054	case CTLADM_CMD_SETSYNC:
4055	case CTLADM_CMD_GETSYNC:
4056		retval = cctl_getsetsync(fd, target, lun, command,
4057					 argc, argv, combinedopt);
4058		break;
4059	case CTLADM_CMD_ERR_INJECT:
4060		retval = cctl_error_inject(fd, target, lun, argc, argv,
4061					   combinedopt);
4062		break;
4063	case CTLADM_CMD_DUMPOOA:
4064		retval = cctl_dump_ooa(fd, argc, argv);
4065		break;
4066	case CTLADM_CMD_DUMPSTRUCTS:
4067		retval = cctl_dump_structs(fd, cmdargs);
4068		break;
4069	case CTLADM_CMD_PRES_IN:
4070		retval = cctl_persistent_reserve_in(fd, target, lun, initid,
4071		                                    argc, argv, combinedopt,
4072						    retries);
4073		break;
4074	case CTLADM_CMD_PRES_OUT:
4075		retval = cctl_persistent_reserve_out(fd, target, lun, initid,
4076						     argc, argv, combinedopt,
4077						     retries);
4078		break;
4079	case CTLADM_CMD_INQ_VPD_DEVID:
4080	        retval = cctl_inquiry_vpd_devid(fd, target, lun, initid);
4081		break;
4082	case CTLADM_CMD_RTPG:
4083	        retval = cctl_report_target_port_group(fd, target, lun, initid);
4084		break;
4085	case CTLADM_CMD_MODIFY:
4086	        retval = cctl_modify_lun(fd, argc, argv, combinedopt);
4087		break;
4088	case CTLADM_CMD_HELP:
4089	default:
4090		usage(retval);
4091		break;
4092	}
4093bailout:
4094
4095	if (fd != -1)
4096		close(fd);
4097
4098	exit (retval);
4099}
4100
4101/*
4102 * vim: ts=8
4103 */
4104