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/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
45 #include <cam/cam_ccb.h>
46 #include <cam/scsi/scsi_all.h>
47 #include <cam/scsi/smp_all.h>
51 #include <sys/libkern.h>
52 #include <cam/cam_queue.h>
53 #include <cam/cam_xpt.h>
55 FEATURE(scbus, "SCSI devices support");
59 static int camstatusentrycomp(const void *key, const void *member);
61 const struct cam_status_entry cam_status_table[] = {
62 { CAM_REQ_INPROG, "CCB request is in progress" },
63 { CAM_REQ_CMP, "CCB request completed without error" },
64 { CAM_REQ_ABORTED, "CCB request aborted by the host" },
65 { CAM_UA_ABORT, "Unable to abort CCB request" },
66 { CAM_REQ_CMP_ERR, "CCB request completed with an error" },
67 { CAM_BUSY, "CAM subsystem is busy" },
68 { CAM_REQ_INVALID, "CCB request was invalid" },
69 { CAM_PATH_INVALID, "Supplied Path ID is invalid" },
70 { CAM_DEV_NOT_THERE, "Device Not Present" },
71 { CAM_UA_TERMIO, "Unable to terminate I/O CCB request" },
72 { CAM_SEL_TIMEOUT, "Selection Timeout" },
73 { CAM_CMD_TIMEOUT, "Command timeout" },
74 { CAM_SCSI_STATUS_ERROR, "SCSI Status Error" },
75 { CAM_MSG_REJECT_REC, "Message Reject Reveived" },
76 { CAM_SCSI_BUS_RESET, "SCSI Bus Reset Sent/Received" },
77 { CAM_UNCOR_PARITY, "Uncorrectable parity/CRC error" },
78 { CAM_AUTOSENSE_FAIL, "Auto-Sense Retrieval Failed" },
79 { CAM_NO_HBA, "No HBA Detected" },
80 { CAM_DATA_RUN_ERR, "Data Overrun error" },
81 { CAM_UNEXP_BUSFREE, "Unexpected Bus Free" },
82 { CAM_SEQUENCE_FAIL, "Target Bus Phase Sequence Failure" },
83 { CAM_CCB_LEN_ERR, "CCB length supplied is inadequate" },
84 { CAM_PROVIDE_FAIL, "Unable to provide requested capability" },
85 { CAM_BDR_SENT, "SCSI BDR Message Sent" },
86 { CAM_REQ_TERMIO, "CCB request terminated by the host" },
87 { CAM_UNREC_HBA_ERROR, "Unrecoverable Host Bus Adapter Error" },
88 { CAM_REQ_TOO_BIG, "The request was too large for this host" },
89 { CAM_REQUEUE_REQ, "Unconditionally Re-queue Request", },
90 { CAM_ATA_STATUS_ERROR, "ATA Status Error" },
91 { CAM_SCSI_IT_NEXUS_LOST,"Initiator/Target Nexus Lost" },
92 { CAM_SMP_STATUS_ERROR, "SMP Status Error" },
93 { CAM_IDE, "Initiator Detected Error Message Received" },
94 { CAM_RESRC_UNAVAIL, "Resource Unavailable" },
95 { CAM_UNACKED_EVENT, "Unacknowledged Event by Host" },
96 { CAM_MESSAGE_RECV, "Message Received in Host Target Mode" },
97 { CAM_INVALID_CDB, "Invalid CDB received in Host Target Mode" },
98 { CAM_LUN_INVALID, "Invalid Lun" },
99 { CAM_TID_INVALID, "Invalid Target ID" },
100 { CAM_FUNC_NOTAVAIL, "Function Not Available" },
101 { CAM_NO_NEXUS, "Nexus Not Established" },
102 { CAM_IID_INVALID, "Invalid Initiator ID" },
103 { CAM_CDB_RECVD, "CDB Received" },
104 { CAM_LUN_ALRDY_ENA, "LUN Already Enabled for Target Mode" },
105 { CAM_SCSI_BUSY, "SCSI Bus Busy" },
108 const int num_cam_status_entries =
109 sizeof(cam_status_table)/sizeof(*cam_status_table);
112 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem");
114 #ifndef CAM_DEFAULT_SORT_IO_QUEUES
115 #define CAM_DEFAULT_SORT_IO_QUEUES 1
118 int cam_sort_io_queues = CAM_DEFAULT_SORT_IO_QUEUES;
119 SYSCTL_INT(_kern_cam, OID_AUTO, sort_io_queues, CTLFLAG_RWTUN,
120 &cam_sort_io_queues, 0, "Sort IO queues to try and optimise disk access patterns");
124 cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen)
127 /* Trim leading/trailing spaces, nulls. */
128 while (srclen > 0 && src[0] == ' ')
131 && (src[srclen-1] == ' ' || src[srclen-1] == '\0'))
134 while (srclen > 0 && dstlen > 1) {
135 u_int8_t *cur_pos = dst;
137 if (*src < 0x20 || *src >= 0x80) {
138 /* SCSI-II Specifies that these should never occur. */
139 /* non-printable character */
142 *cur_pos++ = ((*src & 0300) >> 6) + '0';
143 *cur_pos++ = ((*src & 0070) >> 3) + '0';
144 *cur_pos++ = ((*src & 0007) >> 0) + '0';
149 /* normal character */
154 dstlen -= cur_pos - dst;
161 * Compare string with pattern, returning 0 on match.
162 * Short pattern matches trailing blanks in name,
163 * wildcard '*' in pattern matches rest of name,
164 * wildcard '?' matches a single non-space character.
167 cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len)
170 while (*pattern != '\0'&& str_len > 0) {
172 if (*pattern == '*') {
175 if ((*pattern != *str)
176 && (*pattern != '?' || *str == ' ')) {
183 while (str_len > 0 && *str == ' ') {
187 if (str_len > 0 && *str == 0)
194 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
195 int entry_size, cam_quirkmatch_t *comp_func)
197 for (; num_entries > 0; num_entries--, quirk_table += entry_size) {
198 if ((*comp_func)(target, quirk_table) == 0)
199 return (quirk_table);
204 const struct cam_status_entry*
205 cam_fetch_status_entry(cam_status status)
207 status &= CAM_STATUS_MASK;
208 return (bsearch(&status, &cam_status_table,
209 num_cam_status_entries,
210 sizeof(*cam_status_table),
211 camstatusentrycomp));
215 camstatusentrycomp(const void *key, const void *member)
218 const struct cam_status_entry *table_entry;
220 status = *(const cam_status *)key;
221 table_entry = (const struct cam_status_entry *)member;
223 return (status - table_entry->status_code);
229 cam_error_string(union ccb *ccb, char *str, int str_len,
230 cam_error_string_flags flags,
231 cam_error_proto_flags proto_flags)
234 cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
235 int str_len, cam_error_string_flags flags,
236 cam_error_proto_flags proto_flags)
237 #endif /* _KERNEL/!_KERNEL */
247 if (flags == CAM_ESF_NONE)
250 switch (ccb->ccb_h.func_code) {
252 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
257 proto_flags |= CAM_EAF_PRINT_RESULT;
259 case CAM_EPF_MINIMAL:
260 proto_flags |= CAM_EAF_PRINT_STATUS;
267 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
272 proto_flags |= CAM_ESF_PRINT_SENSE;
274 case CAM_EPF_MINIMAL:
275 proto_flags |= CAM_ESF_PRINT_STATUS;
282 switch (proto_flags & CAM_EPF_LEVEL_MASK) {
286 proto_flags |= CAM_ESMF_PRINT_FULL_CMD;
289 case CAM_EPF_MINIMAL:
290 proto_flags |= CAM_ESMF_PRINT_STATUS;
300 xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str));
302 cam_path_string(device, path_str, sizeof(path_str));
303 #endif /* _KERNEL/!_KERNEL */
305 sbuf_new(&sb, str, str_len, 0);
307 if (flags & CAM_ESF_COMMAND) {
308 sbuf_cat(&sb, path_str);
309 switch (ccb->ccb_h.func_code) {
311 ata_command_sbuf(&ccb->ataio, &sb);
312 sbuf_printf(&sb, "\n");
316 scsi_command_string(&ccb->csio, &sb);
318 scsi_command_string(device, &ccb->csio, &sb);
319 #endif /* _KERNEL/!_KERNEL */
320 sbuf_printf(&sb, "\n");
323 smp_command_sbuf(&ccb->smpio, &sb, path_str, 79 -
324 strlen(path_str), (proto_flags &
325 CAM_ESMF_PRINT_FULL_CMD) ? 79 : 0);
326 sbuf_printf(&sb, "\n");
333 if (flags & CAM_ESF_CAM_STATUS) {
335 const struct cam_status_entry *entry;
337 sbuf_cat(&sb, path_str);
339 status = ccb->ccb_h.status & CAM_STATUS_MASK;
341 entry = cam_fetch_status_entry(status);
344 sbuf_printf(&sb, "CAM status: Unknown (%#x)\n",
347 sbuf_printf(&sb, "CAM status: %s\n",
351 if (flags & CAM_ESF_PROTO_STATUS) {
353 switch (ccb->ccb_h.func_code) {
355 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
356 CAM_ATA_STATUS_ERROR)
358 if (proto_flags & CAM_EAF_PRINT_STATUS) {
359 sbuf_cat(&sb, path_str);
360 ata_status_sbuf(&ccb->ataio, &sb);
361 sbuf_printf(&sb, "\n");
363 if (proto_flags & CAM_EAF_PRINT_RESULT) {
364 sbuf_cat(&sb, path_str);
365 ata_res_sbuf(&ccb->ataio, &sb);
366 sbuf_printf(&sb, "\n");
371 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
372 CAM_SCSI_STATUS_ERROR)
375 if (proto_flags & CAM_ESF_PRINT_STATUS) {
376 sbuf_cat(&sb, path_str);
377 sbuf_printf(&sb, "SCSI status: %s\n",
378 scsi_status_string(&ccb->csio));
381 if ((proto_flags & CAM_ESF_PRINT_SENSE)
382 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
383 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) {
386 scsi_sense_sbuf(&ccb->csio, &sb,
389 scsi_sense_sbuf(device, &ccb->csio, &sb,
391 #endif /* _KERNEL/!_KERNEL */
395 if ((ccb->ccb_h.status & CAM_STATUS_MASK) !=
396 CAM_SMP_STATUS_ERROR)
399 if (proto_flags & CAM_ESF_PRINT_STATUS) {
400 sbuf_cat(&sb, path_str);
401 sbuf_printf(&sb, "SMP status: %s (%#x)\n",
402 smp_error_desc(ccb->smpio.smp_response[2]),
403 ccb->smpio.smp_response[2]);
405 /* There is no SMP equivalent to SCSI sense. */
414 return(sbuf_data(&sb));
420 cam_error_print(union ccb *ccb, cam_error_string_flags flags,
421 cam_error_proto_flags proto_flags)
425 printf("%s", cam_error_string(ccb, str, sizeof(str), flags,
432 cam_error_print(struct cam_device *device, union ccb *ccb,
433 cam_error_string_flags flags, cam_error_proto_flags proto_flags,
438 if ((device == NULL) || (ccb == NULL) || (ofile == NULL))
441 fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str),
442 flags, proto_flags));
445 #endif /* _KERNEL/!_KERNEL */
448 * Common calculate geometry fuction
450 * Caller should set ccg->volume_size and block_size.
451 * The extended parameter should be zero if extended translation
452 * should not be used.
455 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
457 uint32_t size_mb, secs_per_cylinder;
459 if (ccg->block_size == 0) {
460 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
463 size_mb = (1024L * 1024L) / ccg->block_size;
465 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
468 size_mb = ccg->volume_size / size_mb;
469 if (size_mb > 1024 && extended) {
471 ccg->secs_per_track = 63;
474 ccg->secs_per_track = 32;
476 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
477 if (secs_per_cylinder == 0) {
478 ccg->ccb_h.status = CAM_REQ_CMP_ERR;
481 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
482 ccg->ccb_h.status = CAM_REQ_CMP;