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