xref: /freebsd/sys/cam/ctl/ctl_scsi_all.c (revision d184218c)
1 /*-
2  * Implementation of Utility functions for all SCSI device types.
3  *
4  * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5  * Copyright (c) 1997, 1998, 2003 Kenneth D. Merry.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_scsi_all.c#2 $
30  */
31 
32 #include <sys/param.h>
33 
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/types.h>
37 #ifdef _KERNEL
38 #include <sys/systm.h>
39 #include <sys/libkern.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
42 #else
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <inttypes.h>
48 #endif
49 
50 #include <cam/cam.h>
51 #include <cam/cam_ccb.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_xpt.h>
54 #include <cam/scsi/scsi_all.h>
55 
56 #include <cam/ctl/ctl_io.h>
57 #include <cam/ctl/ctl_scsi_all.h>
58 #include <sys/sbuf.h>
59 #ifndef _KERNEL
60 #include <camlib.h>
61 #endif
62 
63 const char *
64 ctl_scsi_status_string(struct ctl_scsiio *ctsio)
65 {
66 	switch(ctsio->scsi_status) {
67 	case SCSI_STATUS_OK:
68 		return("OK");
69 	case SCSI_STATUS_CHECK_COND:
70 		return("Check Condition");
71 	case SCSI_STATUS_BUSY:
72 		return("Busy");
73 	case SCSI_STATUS_INTERMED:
74 		return("Intermediate");
75 	case SCSI_STATUS_INTERMED_COND_MET:
76 		return("Intermediate-Condition Met");
77 	case SCSI_STATUS_RESERV_CONFLICT:
78 		return("Reservation Conflict");
79 	case SCSI_STATUS_CMD_TERMINATED:
80 		return("Command Terminated");
81 	case SCSI_STATUS_QUEUE_FULL:
82 		return("Queue Full");
83 	case SCSI_STATUS_ACA_ACTIVE:
84 		return("ACA Active");
85 	case SCSI_STATUS_TASK_ABORTED:
86 		return("Task Aborted");
87 	default: {
88 		static char unkstr[64];
89 		snprintf(unkstr, sizeof(unkstr), "Unknown %#x",
90 			 ctsio->scsi_status);
91 		return(unkstr);
92 	}
93 	}
94 }
95 
96 /*
97  * scsi_command_string() returns 0 for success and -1 for failure.
98  */
99 int
100 ctl_scsi_command_string(struct ctl_scsiio *ctsio,
101 			struct scsi_inquiry_data *inq_data, struct sbuf *sb)
102 {
103 	char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
104 
105 	sbuf_printf(sb, "%s. CDB: %s",
106 		    scsi_op_desc(ctsio->cdb[0], inq_data),
107 		    scsi_cdb_string(ctsio->cdb, cdb_str, sizeof(cdb_str)));
108 
109 	return(0);
110 }
111 
112 void
113 ctl_scsi_path_string(union ctl_io *io, char *path_str, int len)
114 {
115 	if (io->io_hdr.nexus.targ_target.wwid[0] == 0) {
116 		snprintf(path_str, len, "(%ju:%d:%ju:%d): ",
117 			 (uintmax_t)io->io_hdr.nexus.initid.id,
118 			 io->io_hdr.nexus.targ_port,
119 			 (uintmax_t)io->io_hdr.nexus.targ_target.id,
120 			 io->io_hdr.nexus.targ_lun);
121 	} else {
122 		/*
123 		 * XXX KDM find a better way to display FC WWIDs.
124 		 */
125 #ifdef _KERNEL
126 		snprintf(path_str, len, "(%ju:%d:%#jx,%#jx:%d): ",
127 			 (uintmax_t)io->io_hdr.nexus.initid.id,
128 			 io->io_hdr.nexus.targ_port,
129 			 (intmax_t)io->io_hdr.nexus.targ_target.wwid[0],
130 			 (intmax_t)io->io_hdr.nexus.targ_target.wwid[1],
131 			 io->io_hdr.nexus.targ_lun);
132 #else /* _KERNEL */
133 		snprintf(path_str, len, "(%ju:%d:%#jx,%#jx:%d): ",
134 			 (uintmax_t)io->io_hdr.nexus.initid.id,
135 			 io->io_hdr.nexus.targ_port,
136 			 (intmax_t)io->io_hdr.nexus.targ_target.wwid[0],
137 			 (intmax_t)io->io_hdr.nexus.targ_target.wwid[1],
138 			 io->io_hdr.nexus.targ_lun);
139 #endif /* _KERNEL */
140 	}
141 }
142 
143 /*
144  * ctl_scsi_sense_sbuf() returns 0 for success and -1 for failure.
145  */
146 int
147 ctl_scsi_sense_sbuf(struct ctl_scsiio *ctsio,
148 		    struct scsi_inquiry_data *inq_data, struct sbuf *sb,
149 		    scsi_sense_string_flags flags)
150 {
151 	char	  path_str[64];
152 
153 	if ((ctsio == NULL) || (sb == NULL))
154 		return(-1);
155 
156 	ctl_scsi_path_string((union ctl_io *)ctsio, path_str, sizeof(path_str));
157 
158 	if (flags & SSS_FLAG_PRINT_COMMAND) {
159 
160 		sbuf_cat(sb, path_str);
161 
162 		ctl_scsi_command_string(ctsio, inq_data, sb);
163 
164 		sbuf_printf(sb, "\n");
165 	}
166 
167 	scsi_sense_only_sbuf(&ctsio->sense_data, ctsio->sense_len, sb,
168 			     path_str, inq_data, ctsio->cdb, ctsio->cdb_len);
169 
170 	return(0);
171 }
172 
173 char *
174 ctl_scsi_sense_string(struct ctl_scsiio *ctsio,
175 		      struct scsi_inquiry_data *inq_data, char *str,
176 		      int str_len)
177 {
178 	struct sbuf sb;
179 
180 	sbuf_new(&sb, str, str_len, 0);
181 
182 	ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND);
183 
184 	sbuf_finish(&sb);
185 
186 	return(sbuf_data(&sb));
187 }
188 
189 #ifdef _KERNEL
190 void
191 ctl_scsi_sense_print(struct ctl_scsiio *ctsio,
192 		     struct scsi_inquiry_data *inq_data)
193 {
194 	struct sbuf sb;
195 	char str[512];
196 
197 	sbuf_new(&sb, str, sizeof(str), 0);
198 
199 	ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND);
200 
201 	sbuf_finish(&sb);
202 
203 	printf("%s", sbuf_data(&sb));
204 }
205 
206 #else /* _KERNEL */
207 void
208 ctl_scsi_sense_print(struct ctl_scsiio *ctsio,
209 		     struct scsi_inquiry_data *inq_data, FILE *ofile)
210 {
211 	struct sbuf sb;
212 	char str[512];
213 
214 	if ((ctsio == NULL) || (ofile == NULL))
215 		return;
216 
217 	sbuf_new(&sb, str, sizeof(str), 0);
218 
219 	ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND);
220 
221 	sbuf_finish(&sb);
222 
223 	fprintf(ofile, "%s", sbuf_data(&sb));
224 }
225 
226 #endif /* _KERNEL */
227 
228