Deleted Added
full compact
cam.c (115562) cam.c (116161)
1/*
2 * Generic utility routines for the Common Access Method layer.
3 *
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
1/*
2 * Generic utility routines for the Common Access Method layer.
3 *
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/sys/cam/cam.c 115562 2003-05-31 20:46:21Z phk $
29 */
27 */
30#include <sys/param.h>
31
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/cam/cam.c 116161 2003-06-10 17:50:20Z obrien $");
31
32#include <sys/param.h>
32#ifdef _KERNEL
33#include <sys/param.h>
34#include <sys/systm.h>
35#include <sys/kernel.h>
36#include <sys/sysctl.h>
37#else /* _KERNEL */
38#include <stdlib.h>
39#include <stdio.h>
40#endif /* _KERNEL */
41
42#include <cam/cam.h>
43#include <cam/cam_ccb.h>
44#include <cam/scsi/scsi_all.h>
45#include <sys/sbuf.h>
46
47#ifdef _KERNEL
48#include <sys/libkern.h>
49#include <cam/cam_xpt.h>
50#endif
51
52static int camstatusentrycomp(const void *key, const void *member);
53
54const struct cam_status_entry cam_status_table[] = {
55 { CAM_REQ_INPROG, "CCB request is in progress" },
56 { CAM_REQ_CMP, "CCB request completed without error" },
57 { CAM_REQ_ABORTED, "CCB request aborted by the host" },
58 { CAM_UA_ABORT, "Unable to abort CCB request" },
59 { CAM_REQ_CMP_ERR, "CCB request completed with an error" },
60 { CAM_BUSY, "CAM subsytem is busy" },
61 { CAM_REQ_INVALID, "CCB request was invalid" },
62 { CAM_PATH_INVALID, "Supplied Path ID is invalid" },
63 { CAM_DEV_NOT_THERE, "Device Not Present" },
64 { CAM_UA_TERMIO, "Unable to terminate I/O CCB request" },
65 { CAM_SEL_TIMEOUT, "Selection Timeout" },
66 { CAM_CMD_TIMEOUT, "Command timeout" },
67 { CAM_SCSI_STATUS_ERROR, "SCSI Status Error" },
68 { CAM_MSG_REJECT_REC, "Message Reject Reveived" },
69 { CAM_SCSI_BUS_RESET, "SCSI Bus Reset Sent/Received" },
70 { CAM_UNCOR_PARITY, "Uncorrectable parity/CRC error" },
71 { CAM_AUTOSENSE_FAIL, "Auto-Sense Retrieval Failed" },
72 { CAM_NO_HBA, "No HBA Detected" },
73 { CAM_DATA_RUN_ERR, "Data Overrun error" },
74 { CAM_UNEXP_BUSFREE, "Unexpected Bus Free" },
75 { CAM_SEQUENCE_FAIL, "Target Bus Phase Sequence Failure" },
76 { CAM_CCB_LEN_ERR, "CCB length supplied is inadequate" },
77 { CAM_PROVIDE_FAIL, "Unable to provide requested capability" },
78 { CAM_BDR_SENT, "SCSI BDR Message Sent" },
79 { CAM_REQ_TERMIO, "CCB request terminated by the host" },
80 { CAM_UNREC_HBA_ERROR, "Unrecoverable Host Bus Adapter Error" },
81 { CAM_REQ_TOO_BIG, "The request was too large for this host" },
82 { CAM_REQUEUE_REQ, "Unconditionally Re-queue Request", },
83 { CAM_IDE, "Initiator Detected Error Message Received" },
84 { CAM_RESRC_UNAVAIL, "Resource Unavailable" },
85 { CAM_UNACKED_EVENT, "Unacknowledged Event by Host" },
86 { CAM_MESSAGE_RECV, "Message Received in Host Target Mode" },
87 { CAM_INVALID_CDB, "Invalid CDB received in Host Target Mode" },
88 { CAM_LUN_INVALID, "Invalid Lun" },
89 { CAM_TID_INVALID, "Invalid Target ID" },
90 { CAM_FUNC_NOTAVAIL, "Function Not Available" },
91 { CAM_NO_NEXUS, "Nexus Not Established" },
92 { CAM_IID_INVALID, "Invalid Initiator ID" },
93 { CAM_CDB_RECVD, "CDB Received" },
94 { CAM_LUN_ALRDY_ENA, "LUN Already Enabled for Target Mode" },
95 { CAM_SCSI_BUSY, "SCSI Bus Busy" },
96};
97
98const int num_cam_status_entries =
99 sizeof(cam_status_table)/sizeof(*cam_status_table);
100
101#ifdef _KERNEL
102SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
103#endif
104
105void
106cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen)
107{
108
109 /* Trim leading/trailing spaces, nulls. */
110 while (srclen > 0 && src[0] == ' ')
111 src++, srclen--;
112 while (srclen > 0
113 && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
114 srclen--;
115
116 while (srclen > 0 && dstlen > 1) {
117 u_int8_t *cur_pos = dst;
118
119 if (*src < 0x20 || *src >= 0x80) {
120 /* SCSI-II Specifies that these should never occur. */
121 /* non-printable character */
122 if (dstlen > 4) {
123 *cur_pos++ = '\\';
124 *cur_pos++ = ((*src & 0300) >> 6) + '0';
125 *cur_pos++ = ((*src & 0070) >> 3) + '0';
126 *cur_pos++ = ((*src & 0007) >> 0) + '0';
127 } else {
128 *cur_pos++ = '?';
129 }
130 } else {
131 /* normal character */
132 *cur_pos++ = *src;
133 }
134 src++;
135 srclen--;
136 dstlen -= cur_pos - dst;
137 dst = cur_pos;
138 }
139 *dst = '\0';
140}
141
142/*
143 * Compare string with pattern, returning 0 on match.
144 * Short pattern matches trailing blanks in name,
145 * wildcard '*' in pattern matches rest of name,
146 * wildcard '?' matches a single non-space character.
147 */
148int
149cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len)
150{
151
152 while (*pattern != '\0'&& str_len > 0) {
153
154 if (*pattern == '*') {
155 return (0);
156 }
157 if ((*pattern != *str)
158 && (*pattern != '?' || *str == ' ')) {
159 return (1);
160 }
161 pattern++;
162 str++;
163 str_len--;
164 }
165 while (str_len > 0 && *str++ == ' ')
166 str_len--;
167
168 return (str_len);
169}
170
171caddr_t
172cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
173 int entry_size, cam_quirkmatch_t *comp_func)
174{
175 for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
176 if ((*comp_func)(target, quirk_table) == 0)
177 return (quirk_table);
178 }
179 return (NULL);
180}
181
182const struct cam_status_entry*
183cam_fetch_status_entry(cam_status status)
184{
185 status &= CAM_STATUS_MASK;
186 return (bsearch(&status, &cam_status_table,
187 num_cam_status_entries,
188 sizeof(*cam_status_table),
189 camstatusentrycomp));
190}
191
192static int
193camstatusentrycomp(const void *key, const void *member)
194{
195 cam_status status;
196 const struct cam_status_entry *table_entry;
197
198 status = *(const cam_status *)key;
199 table_entry = (const struct cam_status_entry *)member;
200
201 return (status - table_entry->status_code);
202}
203
204
205#ifdef _KERNEL
206char *
207cam_error_string(union ccb *ccb, char *str, int str_len,
208 cam_error_string_flags flags,
209 cam_error_proto_flags proto_flags)
210#else /* !_KERNEL */
211char *
212cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
213 int str_len, cam_error_string_flags flags,
214 cam_error_proto_flags proto_flags)
215#endif /* _KERNEL/!_KERNEL */
216{
217 char path_str[64];
218 struct sbuf sb;
219
220 if ((ccb == NULL)
221 || (str == NULL)
222 || (str_len <= 0))
223 return(NULL);
224
225 if (flags == CAM_ESF_NONE)
226 return(NULL);
227
228 switch (ccb->ccb_h.func_code) {
229 case XPT_SCSI_IO:
230 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
231 case CAM_EPF_NONE:
232 break;
233 case CAM_EPF_ALL:
234 case CAM_EPF_NORMAL:
235 proto_flags |= CAM_ESF_PRINT_SENSE;
236 /* FALLTHROUGH */
237 case CAM_EPF_MINIMAL:
238 proto_flags |= CAM_ESF_PRINT_STATUS;
239 /* FALLTHROUGH */
240 default:
241 break;
242 }
243 break;
244 default:
245 break;
246 }
247#ifdef _KERNEL
248 xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
249#else /* !_KERNEL */
250 cam_path_string(device, path_str, sizeof(path_str));
251#endif /* _KERNEL/!_KERNEL */
252
253 sbuf_new(&sb, str, str_len, 0);
254
255 if (flags & CAM_ESF_COMMAND) {
256
257 sbuf_cat(&sb, path_str);
258
259 switch (ccb->ccb_h.func_code) {
260 case XPT_SCSI_IO:
261#ifdef _KERNEL
262 scsi_command_string(&ccb->csio, &sb);
263#else /* !_KERNEL */
264 scsi_command_string(device, &ccb->csio, &sb);
265#endif /* _KERNEL/!_KERNEL */
266 sbuf_printf(&sb, "\n");
267
268 break;
269 default:
270 break;
271 }
272 }
273
274 if (flags & CAM_ESF_CAM_STATUS) {
275 cam_status status;
276 const struct cam_status_entry *entry;
277
278 sbuf_cat(&sb, path_str);
279
280 status = ccb->ccb_h.status & CAM_STATUS_MASK;
281
282 entry = cam_fetch_status_entry(status);
283
284 if (entry == NULL)
285 sbuf_printf(&sb, "CAM Status: Unknown (%#x)\n",
286 ccb->ccb_h.status);
287 else
288 sbuf_printf(&sb, "CAM Status: %s\n",
289 entry->status_text);
290 }
291
292 if (flags & CAM_ESF_PROTO_STATUS) {
293
294 switch (ccb->ccb_h.func_code) {
295 case XPT_SCSI_IO:
296 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
297 CAM_SCSI_STATUS_ERROR)
298 break;
299
300 if (proto_flags & CAM_ESF_PRINT_STATUS) {
301 sbuf_cat(&sb, path_str);
302 /*
303 * Print out the SCSI status byte as long as
304 * the user wants some protocol output.
305 */
306 sbuf_printf(&sb, "SCSI Status: %s\n",
307 scsi_status_string(&ccb->csio));
308 }
309
310 if ((proto_flags & CAM_ESF_PRINT_SENSE)
311 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
312 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) {
313
314#ifdef _KERNEL
315 scsi_sense_sbuf(&ccb->csio, &sb,
316 SSS_FLAG_NONE);
317#else /* !_KERNEL */
318 scsi_sense_sbuf(device, &ccb->csio, &sb,
319 SSS_FLAG_NONE);
320#endif /* _KERNEL/!_KERNEL */
321 }
322 break;
323 default:
324 break;
325 }
326 }
327
328 sbuf_finish(&sb);
329
330 return(sbuf_data(&sb));
331}
332
333#ifdef _KERNEL
334
335void
336cam_error_print(union ccb *ccb, cam_error_string_flags flags,
337 cam_error_proto_flags proto_flags)
338{
339 char str[512];
340
341 printf("%s", cam_error_string(ccb, str, sizeof(str), flags,
342 proto_flags));
343}
344
345#else /* !_KERNEL */
346
347void
348cam_error_print(struct cam_device *device, union ccb *ccb,
349 cam_error_string_flags flags, cam_error_proto_flags proto_flags,
350 FILE *ofile)
351{
352 char str[512];
353
354 if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
355 return;
356
357 fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
358 flags, proto_flags));
359}
360
361#endif /* _KERNEL/!_KERNEL */
33#ifdef _KERNEL
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/sysctl.h>
38#else /* _KERNEL */
39#include <stdlib.h>
40#include <stdio.h>
41#endif /* _KERNEL */
42
43#include <cam/cam.h>
44#include <cam/cam_ccb.h>
45#include <cam/scsi/scsi_all.h>
46#include <sys/sbuf.h>
47
48#ifdef _KERNEL
49#include <sys/libkern.h>
50#include <cam/cam_xpt.h>
51#endif
52
53static int camstatusentrycomp(const void *key, const void *member);
54
55const struct cam_status_entry cam_status_table[] = {
56 { CAM_REQ_INPROG, "CCB request is in progress" },
57 { CAM_REQ_CMP, "CCB request completed without error" },
58 { CAM_REQ_ABORTED, "CCB request aborted by the host" },
59 { CAM_UA_ABORT, "Unable to abort CCB request" },
60 { CAM_REQ_CMP_ERR, "CCB request completed with an error" },
61 { CAM_BUSY, "CAM subsytem is busy" },
62 { CAM_REQ_INVALID, "CCB request was invalid" },
63 { CAM_PATH_INVALID, "Supplied Path ID is invalid" },
64 { CAM_DEV_NOT_THERE, "Device Not Present" },
65 { CAM_UA_TERMIO, "Unable to terminate I/O CCB request" },
66 { CAM_SEL_TIMEOUT, "Selection Timeout" },
67 { CAM_CMD_TIMEOUT, "Command timeout" },
68 { CAM_SCSI_STATUS_ERROR, "SCSI Status Error" },
69 { CAM_MSG_REJECT_REC, "Message Reject Reveived" },
70 { CAM_SCSI_BUS_RESET, "SCSI Bus Reset Sent/Received" },
71 { CAM_UNCOR_PARITY, "Uncorrectable parity/CRC error" },
72 { CAM_AUTOSENSE_FAIL, "Auto-Sense Retrieval Failed" },
73 { CAM_NO_HBA, "No HBA Detected" },
74 { CAM_DATA_RUN_ERR, "Data Overrun error" },
75 { CAM_UNEXP_BUSFREE, "Unexpected Bus Free" },
76 { CAM_SEQUENCE_FAIL, "Target Bus Phase Sequence Failure" },
77 { CAM_CCB_LEN_ERR, "CCB length supplied is inadequate" },
78 { CAM_PROVIDE_FAIL, "Unable to provide requested capability" },
79 { CAM_BDR_SENT, "SCSI BDR Message Sent" },
80 { CAM_REQ_TERMIO, "CCB request terminated by the host" },
81 { CAM_UNREC_HBA_ERROR, "Unrecoverable Host Bus Adapter Error" },
82 { CAM_REQ_TOO_BIG, "The request was too large for this host" },
83 { CAM_REQUEUE_REQ, "Unconditionally Re-queue Request", },
84 { CAM_IDE, "Initiator Detected Error Message Received" },
85 { CAM_RESRC_UNAVAIL, "Resource Unavailable" },
86 { CAM_UNACKED_EVENT, "Unacknowledged Event by Host" },
87 { CAM_MESSAGE_RECV, "Message Received in Host Target Mode" },
88 { CAM_INVALID_CDB, "Invalid CDB received in Host Target Mode" },
89 { CAM_LUN_INVALID, "Invalid Lun" },
90 { CAM_TID_INVALID, "Invalid Target ID" },
91 { CAM_FUNC_NOTAVAIL, "Function Not Available" },
92 { CAM_NO_NEXUS, "Nexus Not Established" },
93 { CAM_IID_INVALID, "Invalid Initiator ID" },
94 { CAM_CDB_RECVD, "CDB Received" },
95 { CAM_LUN_ALRDY_ENA, "LUN Already Enabled for Target Mode" },
96 { CAM_SCSI_BUSY, "SCSI Bus Busy" },
97};
98
99const int num_cam_status_entries =
100 sizeof(cam_status_table)/sizeof(*cam_status_table);
101
102#ifdef _KERNEL
103SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
104#endif
105
106void
107cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen)
108{
109
110 /* Trim leading/trailing spaces, nulls. */
111 while (srclen > 0 && src[0] == ' ')
112 src++, srclen--;
113 while (srclen > 0
114 && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
115 srclen--;
116
117 while (srclen > 0 && dstlen > 1) {
118 u_int8_t *cur_pos = dst;
119
120 if (*src < 0x20 || *src >= 0x80) {
121 /* SCSI-II Specifies that these should never occur. */
122 /* non-printable character */
123 if (dstlen > 4) {
124 *cur_pos++ = '\\';
125 *cur_pos++ = ((*src & 0300) >> 6) + '0';
126 *cur_pos++ = ((*src & 0070) >> 3) + '0';
127 *cur_pos++ = ((*src & 0007) >> 0) + '0';
128 } else {
129 *cur_pos++ = '?';
130 }
131 } else {
132 /* normal character */
133 *cur_pos++ = *src;
134 }
135 src++;
136 srclen--;
137 dstlen -= cur_pos - dst;
138 dst = cur_pos;
139 }
140 *dst = '\0';
141}
142
143/*
144 * Compare string with pattern, returning 0 on match.
145 * Short pattern matches trailing blanks in name,
146 * wildcard '*' in pattern matches rest of name,
147 * wildcard '?' matches a single non-space character.
148 */
149int
150cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len)
151{
152
153 while (*pattern != '\0'&& str_len > 0) {
154
155 if (*pattern == '*') {
156 return (0);
157 }
158 if ((*pattern != *str)
159 && (*pattern != '?' || *str == ' ')) {
160 return (1);
161 }
162 pattern++;
163 str++;
164 str_len--;
165 }
166 while (str_len > 0 && *str++ == ' ')
167 str_len--;
168
169 return (str_len);
170}
171
172caddr_t
173cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
174 int entry_size, cam_quirkmatch_t *comp_func)
175{
176 for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
177 if ((*comp_func)(target, quirk_table) == 0)
178 return (quirk_table);
179 }
180 return (NULL);
181}
182
183const struct cam_status_entry*
184cam_fetch_status_entry(cam_status status)
185{
186 status &= CAM_STATUS_MASK;
187 return (bsearch(&status, &cam_status_table,
188 num_cam_status_entries,
189 sizeof(*cam_status_table),
190 camstatusentrycomp));
191}
192
193static int
194camstatusentrycomp(const void *key, const void *member)
195{
196 cam_status status;
197 const struct cam_status_entry *table_entry;
198
199 status = *(const cam_status *)key;
200 table_entry = (const struct cam_status_entry *)member;
201
202 return (status - table_entry->status_code);
203}
204
205
206#ifdef _KERNEL
207char *
208cam_error_string(union ccb *ccb, char *str, int str_len,
209 cam_error_string_flags flags,
210 cam_error_proto_flags proto_flags)
211#else /* !_KERNEL */
212char *
213cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
214 int str_len, cam_error_string_flags flags,
215 cam_error_proto_flags proto_flags)
216#endif /* _KERNEL/!_KERNEL */
217{
218 char path_str[64];
219 struct sbuf sb;
220
221 if ((ccb == NULL)
222 || (str == NULL)
223 || (str_len <= 0))
224 return(NULL);
225
226 if (flags == CAM_ESF_NONE)
227 return(NULL);
228
229 switch (ccb->ccb_h.func_code) {
230 case XPT_SCSI_IO:
231 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
232 case CAM_EPF_NONE:
233 break;
234 case CAM_EPF_ALL:
235 case CAM_EPF_NORMAL:
236 proto_flags |= CAM_ESF_PRINT_SENSE;
237 /* FALLTHROUGH */
238 case CAM_EPF_MINIMAL:
239 proto_flags |= CAM_ESF_PRINT_STATUS;
240 /* FALLTHROUGH */
241 default:
242 break;
243 }
244 break;
245 default:
246 break;
247 }
248#ifdef _KERNEL
249 xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
250#else /* !_KERNEL */
251 cam_path_string(device, path_str, sizeof(path_str));
252#endif /* _KERNEL/!_KERNEL */
253
254 sbuf_new(&sb, str, str_len, 0);
255
256 if (flags & CAM_ESF_COMMAND) {
257
258 sbuf_cat(&sb, path_str);
259
260 switch (ccb->ccb_h.func_code) {
261 case XPT_SCSI_IO:
262#ifdef _KERNEL
263 scsi_command_string(&ccb->csio, &sb);
264#else /* !_KERNEL */
265 scsi_command_string(device, &ccb->csio, &sb);
266#endif /* _KERNEL/!_KERNEL */
267 sbuf_printf(&sb, "\n");
268
269 break;
270 default:
271 break;
272 }
273 }
274
275 if (flags & CAM_ESF_CAM_STATUS) {
276 cam_status status;
277 const struct cam_status_entry *entry;
278
279 sbuf_cat(&sb, path_str);
280
281 status = ccb->ccb_h.status & CAM_STATUS_MASK;
282
283 entry = cam_fetch_status_entry(status);
284
285 if (entry == NULL)
286 sbuf_printf(&sb, "CAM Status: Unknown (%#x)\n",
287 ccb->ccb_h.status);
288 else
289 sbuf_printf(&sb, "CAM Status: %s\n",
290 entry->status_text);
291 }
292
293 if (flags & CAM_ESF_PROTO_STATUS) {
294
295 switch (ccb->ccb_h.func_code) {
296 case XPT_SCSI_IO:
297 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
298 CAM_SCSI_STATUS_ERROR)
299 break;
300
301 if (proto_flags & CAM_ESF_PRINT_STATUS) {
302 sbuf_cat(&sb, path_str);
303 /*
304 * Print out the SCSI status byte as long as
305 * the user wants some protocol output.
306 */
307 sbuf_printf(&sb, "SCSI Status: %s\n",
308 scsi_status_string(&ccb->csio));
309 }
310
311 if ((proto_flags & CAM_ESF_PRINT_SENSE)
312 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
313 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) {
314
315#ifdef _KERNEL
316 scsi_sense_sbuf(&ccb->csio, &sb,
317 SSS_FLAG_NONE);
318#else /* !_KERNEL */
319 scsi_sense_sbuf(device, &ccb->csio, &sb,
320 SSS_FLAG_NONE);
321#endif /* _KERNEL/!_KERNEL */
322 }
323 break;
324 default:
325 break;
326 }
327 }
328
329 sbuf_finish(&sb);
330
331 return(sbuf_data(&sb));
332}
333
334#ifdef _KERNEL
335
336void
337cam_error_print(union ccb *ccb, cam_error_string_flags flags,
338 cam_error_proto_flags proto_flags)
339{
340 char str[512];
341
342 printf("%s", cam_error_string(ccb, str, sizeof(str), flags,
343 proto_flags));
344}
345
346#else /* !_KERNEL */
347
348void
349cam_error_print(struct cam_device *device, union ccb *ccb,
350 cam_error_string_flags flags, cam_error_proto_flags proto_flags,
351 FILE *ofile)
352{
353 char str[512];
354
355 if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
356 return;
357
358 fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
359 flags, proto_flags));
360}
361
362#endif /* _KERNEL/!_KERNEL */