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: src/sys/cam/cam.c,v 1.4 2001/03/27 05:45:09 ken Exp $ 29 * $DragonFly: src/sys/bus/cam/cam.c,v 1.8 2007/12/02 03:16:52 pavalos Exp $ 30 */ 31 #include <sys/param.h> 32 33 #ifdef _KERNEL 34 35 #include <sys/systm.h> 36 #include <sys/kernel.h> 37 #include <sys/sysctl.h> 38 39 #else 40 41 #include <stdlib.h> 42 #include <stdio.h> 43 44 #define kbsearch bsearch 45 46 #endif 47 48 #include "cam.h" 49 #include "cam_ccb.h" 50 #include "scsi/scsi_all.h" 51 #include <sys/sbuf.h> 52 53 #ifdef _KERNEL 54 #include <sys/libkern.h> 55 #include "cam_xpt.h" 56 #endif 57 58 static int camstatusentrycomp(const void *key, const void *member); 59 60 const struct cam_status_entry cam_status_table[] = { 61 { CAM_REQ_INPROG, "CCB request is in progress" }, 62 { CAM_REQ_CMP, "CCB request completed without error" }, 63 { CAM_REQ_ABORTED, "CCB request aborted by the host" }, 64 { CAM_UA_ABORT, "Unable to abort CCB request" }, 65 { CAM_REQ_CMP_ERR, "CCB request completed with an error" }, 66 { CAM_BUSY, "CAM subsytem is busy" }, 67 { CAM_REQ_INVALID, "CCB request was invalid" }, 68 { CAM_PATH_INVALID, "Supplied Path ID is invalid" }, 69 { CAM_DEV_NOT_THERE, "Device Not Present" }, 70 { CAM_UA_TERMIO, "Unable to terminate I/O CCB request" }, 71 { CAM_SEL_TIMEOUT, "Selection Timeout" }, 72 { CAM_CMD_TIMEOUT, "Command timeout" }, 73 { CAM_SCSI_STATUS_ERROR, "SCSI Status Error" }, 74 { CAM_MSG_REJECT_REC, "Message Reject Reveived" }, 75 { CAM_SCSI_BUS_RESET, "SCSI Bus Reset Sent/Received" }, 76 { CAM_UNCOR_PARITY, "Uncorrectable parity/CRC error" }, 77 { CAM_AUTOSENSE_FAIL, "Auto-Sense Retrieval Failed" }, 78 { CAM_NO_HBA, "No HBA Detected" }, 79 { CAM_DATA_RUN_ERR, "Data Overrun error" }, 80 { CAM_UNEXP_BUSFREE, "Unexpected Bus Free" }, 81 { CAM_SEQUENCE_FAIL, "Target Bus Phase Sequence Failure" }, 82 { CAM_CCB_LEN_ERR, "CCB length supplied is inadequate" }, 83 { CAM_PROVIDE_FAIL, "Unable to provide requested capability" }, 84 { CAM_BDR_SENT, "SCSI BDR Message Sent" }, 85 { CAM_REQ_TERMIO, "CCB request terminated by the host" }, 86 { CAM_UNREC_HBA_ERROR, "Unrecoverable Host Bus Adapter Error" }, 87 { CAM_REQ_TOO_BIG, "The request was too large for this host" }, 88 { CAM_REQUEUE_REQ, "Unconditionally Re-queue Request", }, 89 { CAM_IDE, "Initiator Detected Error Message Received" }, 90 { CAM_RESRC_UNAVAIL, "Resource Unavailable" }, 91 { CAM_UNACKED_EVENT, "Unacknowledged Event by Host" }, 92 { CAM_MESSAGE_RECV, "Message Received in Host Target Mode" }, 93 { CAM_INVALID_CDB, "Invalid CDB received in Host Target Mode" }, 94 { CAM_LUN_INVALID, "Invalid Lun" }, 95 { CAM_TID_INVALID, "Invalid Target ID" }, 96 { CAM_FUNC_NOTAVAIL, "Function Not Available" }, 97 { CAM_NO_NEXUS, "Nexus Not Established" }, 98 { CAM_IID_INVALID, "Invalid Initiator ID" }, 99 { CAM_CDB_RECVD, "CDB Received" }, 100 { CAM_LUN_ALRDY_ENA, "LUN Already Enabled for Target Mode" }, 101 { CAM_SCSI_BUSY, "SCSI Bus Busy" }, 102 }; 103 104 const int num_cam_status_entries = 105 sizeof(cam_status_table)/sizeof(*cam_status_table); 106 107 #ifdef _KERNEL 108 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD, 0, "CAM Subsystem"); 109 #endif 110 111 void 112 cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen) 113 { 114 115 /* Trim leading/trailing spaces, nulls. */ 116 while (srclen > 0 && src[0] == ' ') 117 src++, srclen--; 118 while (srclen > 0 119 && (src[srclen-1] == ' ' || src[srclen-1] == '\0')) 120 srclen--; 121 122 while (srclen > 0 && dstlen > 1) { 123 u_int8_t *cur_pos = dst; 124 125 if (*src < 0x20 || *src >= 0x80) { 126 /* SCSI-II Specifies that these should never occur. */ 127 /* non-printable character */ 128 if (dstlen > 4) { 129 *cur_pos++ = '\\'; 130 *cur_pos++ = ((*src & 0300) >> 6) + '0'; 131 *cur_pos++ = ((*src & 0070) >> 3) + '0'; 132 *cur_pos++ = ((*src & 0007) >> 0) + '0'; 133 } else { 134 *cur_pos++ = '?'; 135 } 136 } else { 137 /* normal character */ 138 *cur_pos++ = *src; 139 } 140 src++; 141 srclen--; 142 dstlen -= cur_pos - dst; 143 dst = cur_pos; 144 } 145 *dst = '\0'; 146 } 147 148 /* 149 * Compare string with pattern, returning 0 on match. 150 * Short pattern matches trailing blanks in name, 151 * wildcard '*' in pattern matches rest of name, 152 * wildcard '?' matches a single non-space character. 153 */ 154 int 155 cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len) 156 { 157 158 while (*pattern != '\0'&& str_len > 0) { 159 160 if (*pattern == '*') { 161 return (0); 162 } 163 if ((*pattern != *str) 164 && (*pattern != '?' || *str == ' ')) { 165 return (1); 166 } 167 pattern++; 168 str++; 169 str_len--; 170 } 171 while (str_len > 0 && *str++ == ' ') 172 str_len--; 173 174 return (str_len); 175 } 176 177 caddr_t 178 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries, 179 int entry_size, cam_quirkmatch_t *comp_func) 180 { 181 for (; num_entries > 0; num_entries--, quirk_table += entry_size) { 182 if ((*comp_func)(target, quirk_table) == 0) 183 return (quirk_table); 184 } 185 return (NULL); 186 } 187 188 const struct cam_status_entry* 189 cam_fetch_status_entry(cam_status status) 190 { 191 status &= CAM_STATUS_MASK; 192 return (kbsearch(&status, &cam_status_table, 193 num_cam_status_entries, 194 sizeof(*cam_status_table), 195 camstatusentrycomp)); 196 } 197 198 static int 199 camstatusentrycomp(const void *key, const void *member) 200 { 201 cam_status status; 202 const struct cam_status_entry *table_entry; 203 204 status = *(const cam_status *)key; 205 table_entry = (const struct cam_status_entry *)member; 206 207 return (status - table_entry->status_code); 208 } 209 210 211 #ifdef _KERNEL 212 char * 213 cam_error_string(union ccb *ccb, char *str, int str_len, 214 cam_error_string_flags flags, 215 cam_error_proto_flags proto_flags) 216 #else /* !_KERNEL */ 217 char * 218 cam_error_string(struct cam_device *device, union ccb *ccb, char *str, 219 int str_len, cam_error_string_flags flags, 220 cam_error_proto_flags proto_flags) 221 #endif /* _KERNEL/!_KERNEL */ 222 { 223 char path_str[64]; 224 struct sbuf sb; 225 226 if ((ccb == NULL) 227 || (str == NULL) 228 || (str_len <= 0)) 229 return(NULL); 230 231 if (flags == CAM_ESF_NONE) 232 return(NULL); 233 234 switch (ccb->ccb_h.func_code) { 235 case XPT_SCSI_IO: 236 switch (proto_flags & CAM_EPF_LEVEL_MASK) { 237 case CAM_EPF_NONE: 238 break; 239 case CAM_EPF_ALL: 240 case CAM_EPF_NORMAL: 241 proto_flags |= CAM_ESF_PRINT_SENSE; 242 /* FALLTHROUGH */ 243 case CAM_EPF_MINIMAL: 244 proto_flags |= CAM_ESF_PRINT_STATUS; 245 default: 246 break; 247 } 248 break; 249 default: 250 break; 251 } 252 #ifdef _KERNEL 253 xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str)); 254 #else /* !_KERNEL */ 255 cam_path_string(device, path_str, sizeof(path_str)); 256 #endif /* _KERNEL/!_KERNEL */ 257 258 sbuf_new(&sb, str, str_len, 0); 259 260 if (flags & CAM_ESF_COMMAND) { 261 262 sbuf_cat(&sb, path_str); 263 264 switch (ccb->ccb_h.func_code) { 265 case XPT_SCSI_IO: 266 #ifdef _KERNEL 267 scsi_command_string(&ccb->csio, &sb); 268 #else /* !_KERNEL */ 269 scsi_command_string(device, &ccb->csio, &sb); 270 #endif /* _KERNEL/!_KERNEL */ 271 sbuf_printf(&sb, "\n"); 272 273 break; 274 default: 275 break; 276 } 277 } 278 279 if (flags & CAM_ESF_CAM_STATUS) { 280 cam_status status; 281 const struct cam_status_entry *entry; 282 283 sbuf_cat(&sb, path_str); 284 285 status = ccb->ccb_h.status & CAM_STATUS_MASK; 286 287 entry = cam_fetch_status_entry(status); 288 289 if (entry == NULL) 290 sbuf_printf(&sb, "CAM Status: Unknown (%#x)\n", 291 ccb->ccb_h.status); 292 else 293 sbuf_printf(&sb, "CAM Status: %s\n", 294 entry->status_text); 295 } 296 297 if (flags & CAM_ESF_PROTO_STATUS) { 298 299 switch (ccb->ccb_h.func_code) { 300 case XPT_SCSI_IO: 301 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 302 CAM_SCSI_STATUS_ERROR) 303 break; 304 305 if (proto_flags & CAM_ESF_PRINT_STATUS) { 306 sbuf_cat(&sb, path_str); 307 /* 308 * Print out the SCSI status byte as long as 309 * the user wants some protocol output. 310 */ 311 sbuf_printf(&sb, "SCSI Status: %s\n", 312 scsi_status_string(&ccb->csio)); 313 } 314 315 if ((proto_flags & CAM_ESF_PRINT_SENSE) 316 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 317 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) { 318 319 #ifdef _KERNEL 320 scsi_sense_sbuf(&ccb->csio, &sb, 321 SSS_FLAG_NONE); 322 #else /* !_KERNEL */ 323 scsi_sense_sbuf(device, &ccb->csio, &sb, 324 SSS_FLAG_NONE); 325 #endif /* _KERNEL/!_KERNEL */ 326 } 327 break; 328 default: 329 break; 330 } 331 } 332 333 sbuf_finish(&sb); 334 335 return(sbuf_data(&sb)); 336 } 337 338 #ifdef _KERNEL 339 340 void 341 cam_error_print(union ccb *ccb, cam_error_string_flags flags, 342 cam_error_proto_flags proto_flags) 343 { 344 char str[512]; 345 346 kprintf("%s", cam_error_string(ccb, str, sizeof(str), flags, 347 proto_flags)); 348 } 349 350 #else /* !_KERNEL */ 351 352 void 353 cam_error_print(struct cam_device *device, union ccb *ccb, 354 cam_error_string_flags flags, cam_error_proto_flags proto_flags, 355 FILE *ofile) 356 { 357 char str[512]; 358 359 if ((device == NULL) || (ccb == NULL) || (ofile == NULL)) 360 return; 361 362 fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str), 363 flags, proto_flags)); 364 } 365 366 #endif /* _KERNEL/!_KERNEL */ 367 368 /* 369 * Common calculate geometry fuction 370 * 371 * Caller should set ccg->volume_size and block_size. 372 * The extended parameter should be zero if extended translation 373 * should not be used. 374 */ 375 void 376 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended) 377 { 378 uint32_t size_mb, secs_per_cylinder; 379 380 if (ccg->block_size == 0) { 381 ccg->ccb_h.status = CAM_REQ_CMP_ERR; 382 return; 383 } 384 size_mb = (1024L * 1024L) / ccg->block_size; 385 if (size_mb == 0) { 386 ccg->ccb_h.status = CAM_REQ_CMP_ERR; 387 return; 388 } 389 size_mb = ccg->volume_size / size_mb; 390 if (size_mb > 1024 && extended) { 391 ccg->heads = 255; 392 ccg->secs_per_track = 63; 393 } else { 394 ccg->heads = 64; 395 ccg->secs_per_track = 32; 396 } 397 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 398 if (secs_per_cylinder == 0) { 399 ccg->ccb_h.status = CAM_REQ_CMP_ERR; 400 return; 401 } 402 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 403 ccg->ccb_h.status = CAM_REQ_CMP; 404 } 405 406