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