summaryrefslogtreecommitdiffstats
path: root/sys/cam/cam.c
blob: d575fd6751d75dfe98042b32338720c6366ac5bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Generic utility routines for the Common Access Method layer.
 *
 * Copyright (c) 1997 Justin T. Gibbs.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification, immediately at the beginning of the file.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */
#include <sys/param.h>

#ifdef _KERNEL
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#else /* _KERNEL */
#include <stdlib.h>
#include <stdio.h>
#endif /* _KERNEL */

#include <cam/cam.h>
#include <cam/cam_ccb.h>
#include <cam/scsi/scsi_all.h>
#include <sys/sbuf.h>

#ifdef _KERNEL
#include <sys/libkern.h>
#include <cam/cam_xpt.h>
#endif

static int	camstatusentrycomp(const void *key, const void *member);

const struct cam_status_entry cam_status_table[] = {
	{ CAM_REQ_INPROG,	 "CCB request is in progress"		     },
	{ CAM_REQ_CMP,		 "CCB request completed without error"	     },
	{ CAM_REQ_ABORTED,	 "CCB request aborted by the host"	     },
	{ CAM_UA_ABORT,		 "Unable to abort CCB request"		     },
	{ CAM_REQ_CMP_ERR,	 "CCB request completed with an error"	     },
	{ CAM_BUSY,		 "CAM subsytem is busy"			     },
	{ CAM_REQ_INVALID,	 "CCB request was invalid"		     },
	{ CAM_PATH_INVALID,	 "Supplied Path ID is invalid"		     },
	{ CAM_DEV_NOT_THERE,	 "Device Not Present"			     },
	{ CAM_UA_TERMIO,	 "Unable to terminate I/O CCB request"	     },
	{ CAM_SEL_TIMEOUT,	 "Selection Timeout"			     },
	{ CAM_CMD_TIMEOUT,	 "Command timeout"			     },
	{ CAM_SCSI_STATUS_ERROR, "SCSI Status Error"			     },
	{ CAM_MSG_REJECT_REC,	 "Message Reject Reveived"		     },
	{ CAM_SCSI_BUS_RESET,	 "SCSI Bus Reset Sent/Received"		     },
	{ CAM_UNCOR_PARITY,	 "Uncorrectable parity/CRC error"	     },
	{ CAM_AUTOSENSE_FAIL,	 "Auto-Sense Retrieval Failed"		     },
	{ CAM_NO_HBA,		 "No HBA Detected"			     },
	{ CAM_DATA_RUN_ERR,	 "Data Overrun error"			     },
	{ CAM_UNEXP_BUSFREE,	 "Unexpected Bus Free"			     },
	{ CAM_SEQUENCE_FAIL,	 "Target Bus Phase Sequence Failure"	     },
	{ CAM_CCB_LEN_ERR,	 "CCB length supplied is inadequate"	     },
	{ CAM_PROVIDE_FAIL,	 "Unable to provide requested capability"    },
	{ CAM_BDR_SENT,		 "SCSI BDR Message Sent"		     },
	{ CAM_REQ_TERMIO,	 "CCB request terminated by the host"	     },
	{ CAM_UNREC_HBA_ERROR,	 "Unrecoverable Host Bus Adapter Error"	     },
	{ CAM_REQ_TOO_BIG,	 "The request was too large for this host"   },
	{ CAM_REQUEUE_REQ,	 "Unconditionally Re-queue Request",	     },
	{ CAM_IDE,		 "Initiator Detected Error Message Received" },
	{ CAM_RESRC_UNAVAIL,	 "Resource Unavailable"			     },
	{ CAM_UNACKED_EVENT,	 "Unacknowledged Event by Host"		     },
	{ CAM_MESSAGE_RECV,	 "Message Received in Host Target Mode"	     },
	{ CAM_INVALID_CDB,	 "Invalid CDB received in Host Target Mode"  },
	{ CAM_LUN_INVALID,	 "Invalid Lun"				     },
	{ CAM_TID_INVALID,	 "Invalid Target ID"			     },
	{ CAM_FUNC_NOTAVAIL,	 "Function Not Available"		     },
	{ CAM_NO_NEXUS,		 "Nexus Not Established"		     },
	{ CAM_IID_INVALID,	 "Invalid Initiator ID"			     },
	{ CAM_CDB_RECVD,	 "CDB Received"				     },
	{ CAM_LUN_ALRDY_ENA,	 "LUN Already Enabled for Target Mode"	     },
	{ CAM_SCSI_BUSY,	 "SCSI Bus Busy"			     },
};

const int num_cam_status_entries =
    sizeof(cam_status_table)/sizeof(*cam_status_table);

#ifdef _KERNEL
SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
#endif

void
cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen)
{

	/* Trim leading/trailing spaces, nulls. */
	while (srclen > 0 && src[0] == ' ')
		src++, srclen--;
	while (srclen > 0
	    && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
		srclen--;

	while (srclen > 0 && dstlen > 1) {
		u_int8_t *cur_pos = dst;

		if (*src < 0x20 || *src >= 0x80) {
			/* SCSI-II Specifies that these should never occur. */
			/* non-printable character */
			if (dstlen > 4) {
				*cur_pos++ = '\\';
				*cur_pos++ = ((*src & 0300) >> 6) + '0';
				*cur_pos++ = ((*src & 0070) >> 3) + '0';
				*cur_pos++ = ((*src & 0007) >> 0) + '0';
			} else {
				*cur_pos++ = '?';
			}
		} else {
			/* normal character */
			*cur_pos++ = *src;
		}
		src++;
		srclen--;
		dstlen -= cur_pos - dst;
		dst = cur_pos;
	}
	*dst = '\0';
}

/*
 * Compare string with pattern, returning 0 on match.
 * Short pattern matches trailing blanks in name,
 * wildcard '*' in pattern matches rest of name,
 * wildcard '?' matches a single non-space character.
 */
int
cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len)
{

	while (*pattern != '\0'&& str_len > 0) {  

		if (*pattern == '*') {
			return (0);
		}
		if ((*pattern != *str)
		 && (*pattern != '?' || *str == ' ')) {
			return (1);
		}
		pattern++;
		str++;
		str_len--;
	}
	while (str_len > 0 && *str++ == ' ')
		str_len--;

	return (str_len);
}

caddr_t
cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
	       int entry_size, cam_quirkmatch_t *comp_func)
{
	for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
		if ((*comp_func)(target, quirk_table) == 0)
			return (quirk_table);
	}
	return (NULL);
}

const struct cam_status_entry*
cam_fetch_status_entry(cam_status status)
{
	status &= CAM_STATUS_MASK;
	return (bsearch(&status, &cam_status_table,
			num_cam_status_entries,
			sizeof(*cam_status_table),
			camstatusentrycomp));
}

static int
camstatusentrycomp(const void *key, const void *member)
{
	cam_status status;
	const struct cam_status_entry *table_entry;

	status = *(const cam_status *)key;
	table_entry = (const struct cam_status_entry *)member;

	return (status - table_entry->status_code);
}


#ifdef _KERNEL
char *
cam_error_string(union ccb *ccb, char *str, int str_len,
		 cam_error_string_flags flags,
		 cam_error_proto_flags proto_flags)
#else /* !_KERNEL */
char *
cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
		 int str_len, cam_error_string_flags flags,
		 cam_error_proto_flags proto_flags)
#endif /* _KERNEL/!_KERNEL */
{
	char path_str[64];
	struct sbuf sb;

	if ((ccb == NULL)
	 || (str == NULL)
	 || (str_len <= 0))
		return(NULL);

	if (flags == CAM_ESF_NONE)
		return(NULL);

	switch (ccb->ccb_h.func_code) {
		case XPT_SCSI_IO:
			switch (proto_flags & CAM_EPF_LEVEL_MASK) {
			case CAM_EPF_NONE:
				break;
			case CAM_EPF_ALL:
			case CAM_EPF_NORMAL:
				proto_flags |= CAM_ESF_PRINT_SENSE;
				/* FALLTHROUGH */
			case CAM_EPF_MINIMAL:
				proto_flags |= CAM_ESF_PRINT_STATUS;
			default:
				break;
			}
			break;
		default:
			break;
	}
#ifdef _KERNEL
	xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
#else /* !_KERNEL */
	cam_path_string(device, path_str, sizeof(path_str));
#endif /* _KERNEL/!_KERNEL */

	sbuf_new(&sb, str, str_len, 0);

	if (flags & CAM_ESF_COMMAND) {

		sbuf_cat(&sb, path_str);

		switch (ccb->ccb_h.func_code) {
		case XPT_SCSI_IO:
#ifdef _KERNEL
			scsi_command_string(&ccb->csio, &sb);
#else /* !_KERNEL */
			scsi_command_string(device, &ccb->csio, &sb);
#endif /* _KERNEL/!_KERNEL */
			sbuf_printf(&sb, "\n");
			
			break;
		default:
			break;
		}
	}

	if (flags & CAM_ESF_CAM_STATUS) {
		cam_status status;
		const struct cam_status_entry *entry;

		sbuf_cat(&sb, path_str);
  
		status = ccb->ccb_h.status & CAM_STATUS_MASK;

		entry = cam_fetch_status_entry(status);

		if (entry == NULL)
			sbuf_printf(&sb, "CAM Status: Unknown (%#x)\n",
				    ccb->ccb_h.status);
		else
			sbuf_printf(&sb, "CAM Status: %s\n",
				    entry->status_text);
	}

	if (flags & CAM_ESF_PROTO_STATUS) {
  
		switch (ccb->ccb_h.func_code) {
		case XPT_SCSI_IO:
			if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
			     CAM_SCSI_STATUS_ERROR)
				break;

			if (proto_flags & CAM_ESF_PRINT_STATUS) {
				sbuf_cat(&sb, path_str);
				/*
				 * Print out the SCSI status byte as long as
				 * the user wants some protocol output.
				 */
				sbuf_printf(&sb, "SCSI Status: %s\n",
					    scsi_status_string(&ccb->csio));
			}

			if ((proto_flags & CAM_ESF_PRINT_SENSE)
			 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
			 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) {

#ifdef _KERNEL
				scsi_sense_sbuf(&ccb->csio, &sb,
						SSS_FLAG_NONE);
#else /* !_KERNEL */
				scsi_sense_sbuf(device, &ccb->csio, &sb,
						SSS_FLAG_NONE);
#endif /* _KERNEL/!_KERNEL */
			}
			break;
		default:
			break;
		}
	}

	sbuf_finish(&sb);

	return(sbuf_data(&sb));
}

#ifdef _KERNEL

void
cam_error_print(union ccb *ccb, cam_error_string_flags flags,
		cam_error_proto_flags proto_flags)
{
	char str[512];

	printf("%s", cam_error_string(ccb, str, sizeof(str), flags,
	       proto_flags));
}

#else /* !_KERNEL */

void
cam_error_print(struct cam_device *device, union ccb *ccb,
		cam_error_string_flags flags, cam_error_proto_flags proto_flags,
		FILE *ofile)
{
	char str[512];

	if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
		return;

	fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
		flags, proto_flags));
}

#endif /* _KERNEL/!_KERNEL */
OpenPOWER on IntegriCloud