2 * Generic utility routines for the Common Access Method layer.
4 * Copyright (c) 1997 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
44 #include <cam/cam_ccb.h>
45 #include <cam/scsi/scsi_all.h>
49 #include <sys/libkern.h>
50 #include <cam/cam_xpt.h>
53 static int camstatusentrycomp(const void *key, const void *member);
55 const 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" },
99 const int num_cam_status_entries =
100 sizeof(cam_status_table)/sizeof(*cam_status_table);
103 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
107 cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen)
110 /* Trim leading/trailing spaces, nulls. */
111 while (srclen > 0 && src[0] == ' ')
114 && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
117 while (srclen > 0 && dstlen > 1) {
118 u_int8_t *cur_pos = dst;
120 if (*src < 0x20 || *src >= 0x80) {
121 /* SCSI-II Specifies that these should never occur. */
122 /* non-printable character */
125 *cur_pos++ = ((*src & 0300) >> 6) + '0';
126 *cur_pos++ = ((*src & 0070) >> 3) + '0';
127 *cur_pos++ = ((*src & 0007) >> 0) + '0';
132 /* normal character */
137 dstlen -= cur_pos - dst;
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.
150 cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len)
153 while (*pattern != '\0'&& str_len > 0) {
155 if (*pattern == '*') {
158 if ((*pattern != *str)
159 && (*pattern != '?' || *str == ' ')) {
166 while (str_len > 0 && *str++ == ' ')
173 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
174 int entry_size, cam_quirkmatch_t *comp_func)
176 for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
177 if ((*comp_func)(target, quirk_table) == 0)
178 return (quirk_table);
183 const struct cam_status_entry*
184 cam_fetch_status_entry(cam_status status)
186 status &= CAM_STATUS_MASK;
187 return (bsearch(&status, &cam_status_table,
188 num_cam_status_entries,
189 sizeof(*cam_status_table),
190 camstatusentrycomp));
194 camstatusentrycomp(const void *key, const void *member)
197 const struct cam_status_entry *table_entry;
199 status = *(const cam_status *)key;
200 table_entry = (const struct cam_status_entry *)member;
202 return (status - table_entry->status_code);
208 cam_error_string(union ccb *ccb, char *str, int str_len,
209 cam_error_string_flags flags,
210 cam_error_proto_flags proto_flags)
213 cam_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 */
226 if (flags == CAM_ESF_NONE)
229 switch (ccb->ccb_h.func_code) {
231 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
236 proto_flags |= CAM_ESF_PRINT_SENSE;
238 case CAM_EPF_MINIMAL:
239 proto_flags |= CAM_ESF_PRINT_STATUS;
249 xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
251 cam_path_string(device, path_str, sizeof(path_str));
252 #endif /* _KERNEL/!_KERNEL */
254 sbuf_new(&sb, str, str_len, 0);
256 if (flags & CAM_ESF_COMMAND) {
258 sbuf_cat(&sb, path_str);
260 switch (ccb->ccb_h.func_code) {
263 scsi_command_string(&ccb->csio, &sb);
265 scsi_command_string(device, &ccb->csio, &sb);
266 #endif /* _KERNEL/!_KERNEL */
267 sbuf_printf(&sb, "\n");
275 if (flags & CAM_ESF_CAM_STATUS) {
277 const struct cam_status_entry *entry;
279 sbuf_cat(&sb, path_str);
281 status = ccb->ccb_h.status & CAM_STATUS_MASK;
283 entry = cam_fetch_status_entry(status);
286 sbuf_printf(&sb, "CAM Status: Unknown (%#x)\n",
289 sbuf_printf(&sb, "CAM Status: %s\n",
293 if (flags & CAM_ESF_PROTO_STATUS) {
295 switch (ccb->ccb_h.func_code) {
297 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
298 CAM_SCSI_STATUS_ERROR)
301 if (proto_flags & CAM_ESF_PRINT_STATUS) {
302 sbuf_cat(&sb, path_str);
304 * Print out the SCSI status byte as long as
305 * the user wants some protocol output.
307 sbuf_printf(&sb, "SCSI Status: %s\n",
308 scsi_status_string(&ccb->csio));
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)) {
316 scsi_sense_sbuf(&ccb->csio, &sb,
319 scsi_sense_sbuf(device, &ccb->csio, &sb,
321 #endif /* _KERNEL/!_KERNEL */
331 return(sbuf_data(&sb));
337 cam_error_print(union ccb *ccb, cam_error_string_flags flags,
338 cam_error_proto_flags proto_flags)
342 printf("%s", cam_error_string(ccb, str, sizeof(str), flags,
349 cam_error_print(struct cam_device *device, union ccb *ccb,
350 cam_error_string_flags flags, cam_error_proto_flags proto_flags,
355 if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
358 fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
359 flags, proto_flags));
362 #endif /* _KERNEL/!_KERNEL */
365 * Common calculate geometry fuction
367 * Caller should set ccg->volume_size and block_size.
368 * The extended parameter should be zero if extended translation
369 * should not be used.
372 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
374 uint32_t size_mb, secs_per_cylinder;
376 if (ccg->block_size == 0) {
377 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
380 size_mb = (1024L * 1024L) / ccg->block_size;
382 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
385 size_mb = ccg->volume_size / size_mb;
386 if (size_mb > 1024 && extended) {
388 ccg->secs_per_track = 63;
391 ccg->secs_per_track = 32;
393 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
394 if (secs_per_cylinder == 0) {
395 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
398 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
399 ccg->ccb_h.status = CAM_REQ_CMP;