1 /*- 2 * Generic utility routines for the Common Access Method layer. 3 * 4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5 * 6 * Copyright (c) 1997 Justin T. Gibbs. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions, and the following disclaimer, 14 * without modification, immediately at the beginning of the file. 15 * 2. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #ifdef _KERNEL 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/sysctl.h> 39 #else /* _KERNEL */ 40 #include <stdlib.h> 41 #include <stdio.h> 42 #include <string.h> 43 #include <camlib.h> 44 #endif /* _KERNEL */ 45 46 #include <cam/cam.h> 47 #include <cam/cam_ccb.h> 48 #include <cam/scsi/scsi_all.h> 49 #include <cam/scsi/smp_all.h> 50 #include <sys/sbuf.h> 51 52 #ifdef _KERNEL 53 #include <sys/libkern.h> 54 #include <cam/cam_queue.h> 55 #include <cam/cam_xpt.h> 56 57 FEATURE(scbus, "SCSI devices support"); 58 59 #endif 60 61 static int camstatusentrycomp(const void *key, const void *member); 62 63 const struct cam_status_entry cam_status_table[] = { 64 { CAM_REQ_INPROG, "CCB request is in progress" }, 65 { CAM_REQ_CMP, "CCB request completed without error" }, 66 { CAM_REQ_ABORTED, "CCB request aborted by the host" }, 67 { CAM_UA_ABORT, "Unable to abort CCB request" }, 68 { CAM_REQ_CMP_ERR, "CCB request completed with an error" }, 69 { CAM_BUSY, "CAM subsystem is busy" }, 70 { CAM_REQ_INVALID, "CCB request was invalid" }, 71 { CAM_PATH_INVALID, "Supplied Path ID is invalid" }, 72 { CAM_DEV_NOT_THERE, "Device Not Present" }, 73 { CAM_UA_TERMIO, "Unable to terminate I/O CCB request" }, 74 { CAM_SEL_TIMEOUT, "Selection Timeout" }, 75 { CAM_CMD_TIMEOUT, "Command timeout" }, 76 { CAM_SCSI_STATUS_ERROR, "SCSI Status Error" }, 77 { CAM_MSG_REJECT_REC, "Message Reject Reveived" }, 78 { CAM_SCSI_BUS_RESET, "SCSI Bus Reset Sent/Received" }, 79 { CAM_UNCOR_PARITY, "Uncorrectable parity/CRC error" }, 80 { CAM_AUTOSENSE_FAIL, "Auto-Sense Retrieval Failed" }, 81 { CAM_NO_HBA, "No HBA Detected" }, 82 { CAM_DATA_RUN_ERR, "Data Overrun error" }, 83 { CAM_UNEXP_BUSFREE, "Unexpected Bus Free" }, 84 { CAM_SEQUENCE_FAIL, "Target Bus Phase Sequence Failure" }, 85 { CAM_CCB_LEN_ERR, "CCB length supplied is inadequate" }, 86 { CAM_PROVIDE_FAIL, "Unable to provide requested capability" }, 87 { CAM_BDR_SENT, "SCSI BDR Message Sent" }, 88 { CAM_REQ_TERMIO, "CCB request terminated by the host" }, 89 { CAM_UNREC_HBA_ERROR, "Unrecoverable Host Bus Adapter Error" }, 90 { CAM_REQ_TOO_BIG, "The request was too large for this host" }, 91 { CAM_REQUEUE_REQ, "Unconditionally Re-queue Request", }, 92 { CAM_ATA_STATUS_ERROR, "ATA Status Error" }, 93 { CAM_SCSI_IT_NEXUS_LOST,"Initiator/Target Nexus Lost" }, 94 { CAM_SMP_STATUS_ERROR, "SMP Status Error" }, 95 { CAM_IDE, "Initiator Detected Error Message Received" }, 96 { CAM_RESRC_UNAVAIL, "Resource Unavailable" }, 97 { CAM_UNACKED_EVENT, "Unacknowledged Event by Host" }, 98 { CAM_MESSAGE_RECV, "Message Received in Host Target Mode" }, 99 { CAM_INVALID_CDB, "Invalid CDB received in Host Target Mode" }, 100 { CAM_LUN_INVALID, "Invalid Lun" }, 101 { CAM_TID_INVALID, "Invalid Target ID" }, 102 { CAM_FUNC_NOTAVAIL, "Function Not Available" }, 103 { CAM_NO_NEXUS, "Nexus Not Established" }, 104 { CAM_IID_INVALID, "Invalid Initiator ID" }, 105 { CAM_CDB_RECVD, "CDB Received" }, 106 { CAM_LUN_ALRDY_ENA, "LUN Already Enabled for Target Mode" }, 107 { CAM_SCSI_BUSY, "SCSI Bus Busy" }, 108 }; 109 110 #ifdef _KERNEL 111 SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 112 "CAM Subsystem"); 113 114 #ifndef CAM_DEFAULT_SORT_IO_QUEUES 115 #define CAM_DEFAULT_SORT_IO_QUEUES 1 116 #endif 117 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"); 121 #endif 122 123 void 124 cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen) 125 { 126 cam_strvis_flag(dst, src, srclen, dstlen, 127 CAM_STRVIS_FLAG_NONASCII_ESC); 128 } 129 130 void 131 cam_strvis_flag(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen, 132 uint32_t flags) 133 { 134 struct sbuf sb; 135 136 sbuf_new(&sb, dst, dstlen, SBUF_FIXEDLEN); 137 cam_strvis_sbuf(&sb, src, srclen, flags); 138 sbuf_finish(&sb); 139 } 140 141 void 142 cam_strvis_sbuf(struct sbuf *sb, const u_int8_t *src, int srclen, 143 uint32_t flags) 144 { 145 146 /* Trim leading/trailing spaces, nulls. */ 147 while (srclen > 0 && src[0] == ' ') 148 src++, srclen--; 149 while (srclen > 0 150 && (src[srclen-1] == ' ' || src[srclen-1] == '\0')) 151 srclen--; 152 153 while (srclen > 0) { 154 if (*src < 0x20 || *src >= 0x80) { 155 /* SCSI-II Specifies that these should never occur. */ 156 /* non-printable character */ 157 switch (flags & CAM_STRVIS_FLAG_NONASCII_MASK) { 158 case CAM_STRVIS_FLAG_NONASCII_ESC: 159 sbuf_printf(sb, "\\%c%c%c", 160 ((*src & 0300) >> 6) + '0', 161 ((*src & 0070) >> 3) + '0', 162 ((*src & 0007) >> 0) + '0'); 163 break; 164 case CAM_STRVIS_FLAG_NONASCII_RAW: 165 /* 166 * If we run into a NUL, just transform it 167 * into a space. 168 */ 169 if (*src != 0x00) 170 sbuf_putc(sb, *src); 171 else 172 sbuf_putc(sb, ' '); 173 break; 174 case CAM_STRVIS_FLAG_NONASCII_SPC: 175 sbuf_putc(sb, ' '); 176 break; 177 case CAM_STRVIS_FLAG_NONASCII_TRIM: 178 default: 179 break; 180 } 181 } else { 182 /* normal character */ 183 sbuf_putc(sb, *src); 184 } 185 src++; 186 srclen--; 187 } 188 } 189 190 /* 191 * Compare string with pattern, returning 0 on match. 192 * Short pattern matches trailing blanks in name, 193 * Shell globbing rules apply: * matches 0 or more characters, 194 * ? matchces one character, [...] denotes a set to match one char, 195 * [^...] denotes a complimented set to match one character. 196 * Spaces in str used to match anything in the pattern string 197 * but was removed because it's a bug. No current patterns require 198 * it, as far as I know, but it's impossible to know what drives 199 * returned. 200 * 201 * Each '*' generates recursion, so keep the number of * in check. 202 */ 203 int 204 cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len) 205 { 206 207 while (*pattern != '\0' && str_len > 0) { 208 if (*pattern == '*') { 209 pattern++; 210 if (*pattern == '\0') 211 return (0); 212 do { 213 if (cam_strmatch(str, pattern, str_len) == 0) 214 return (0); 215 str++; 216 str_len--; 217 } while (str_len > 0); 218 return (1); 219 } else if (*pattern == '[') { 220 int negate_range, ok; 221 uint8_t pc = UCHAR_MAX; 222 uint8_t sc; 223 224 ok = 0; 225 sc = *str++; 226 str_len--; 227 pattern++; 228 if ((negate_range = (*pattern == '^')) != 0) 229 pattern++; 230 while ((*pattern != ']') && *pattern != '\0') { 231 if (*pattern == '-') { 232 if (pattern[1] == '\0') /* Bad pattern */ 233 return (1); 234 if (sc >= pc && sc <= pattern[1]) 235 ok = 1; 236 pattern++; 237 } else if (*pattern == sc) 238 ok = 1; 239 pc = *pattern; 240 pattern++; 241 } 242 if (ok == negate_range) 243 return (1); 244 pattern++; 245 } else if (*pattern == '?') { 246 /* 247 * NB: || *str == ' ' of the old code is a bug and was 248 * removed. If you add it back, keep this the last if 249 * before the naked else */ 250 pattern++; 251 str++; 252 str_len--; 253 } else { 254 if (*str != *pattern) 255 return (1); 256 pattern++; 257 str++; 258 str_len--; 259 } 260 } 261 262 /* '*' is allowed to match nothing, so gobble it */ 263 while (*pattern == '*') 264 pattern++; 265 266 if ( *pattern != '\0') { 267 /* Pattern not fully consumed. Not a match */ 268 return (1); 269 } 270 271 /* Eat trailing spaces, which get added by SAT */ 272 while (str_len > 0 && *str == ' ') { 273 str++; 274 str_len--; 275 } 276 277 return (str_len); 278 } 279 280 caddr_t 281 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries, 282 int entry_size, cam_quirkmatch_t *comp_func) 283 { 284 for (; num_entries > 0; num_entries--, quirk_table += entry_size) { 285 if ((*comp_func)(target, quirk_table) == 0) 286 return (quirk_table); 287 } 288 return (NULL); 289 } 290 291 const struct cam_status_entry* 292 cam_fetch_status_entry(cam_status status) 293 { 294 status &= CAM_STATUS_MASK; 295 return (bsearch(&status, &cam_status_table, 296 nitems(cam_status_table), 297 sizeof(*cam_status_table), 298 camstatusentrycomp)); 299 } 300 301 static int 302 camstatusentrycomp(const void *key, const void *member) 303 { 304 cam_status status; 305 const struct cam_status_entry *table_entry; 306 307 status = *(const cam_status *)key; 308 table_entry = (const struct cam_status_entry *)member; 309 310 return (status - table_entry->status_code); 311 } 312 313 #ifdef _KERNEL 314 char * 315 cam_error_string(union ccb *ccb, char *str, int str_len, 316 cam_error_string_flags flags, 317 cam_error_proto_flags proto_flags) 318 #else /* !_KERNEL */ 319 char * 320 cam_error_string(struct cam_device *device, union ccb *ccb, char *str, 321 int str_len, cam_error_string_flags flags, 322 cam_error_proto_flags proto_flags) 323 #endif /* _KERNEL/!_KERNEL */ 324 { 325 char path_str[64]; 326 struct sbuf sb; 327 328 if ((ccb == NULL) 329 || (str == NULL) 330 || (str_len <= 0)) 331 return(NULL); 332 333 if (flags == CAM_ESF_NONE) 334 return(NULL); 335 336 switch (ccb->ccb_h.func_code) { 337 case XPT_ATA_IO: 338 switch (proto_flags & CAM_EPF_LEVEL_MASK) { 339 case CAM_EPF_NONE: 340 break; 341 case CAM_EPF_ALL: 342 case CAM_EPF_NORMAL: 343 proto_flags |= CAM_EAF_PRINT_RESULT; 344 /* FALLTHROUGH */ 345 case CAM_EPF_MINIMAL: 346 proto_flags |= CAM_EAF_PRINT_STATUS; 347 /* FALLTHROUGH */ 348 default: 349 break; 350 } 351 break; 352 case XPT_SCSI_IO: 353 switch (proto_flags & CAM_EPF_LEVEL_MASK) { 354 case CAM_EPF_NONE: 355 break; 356 case CAM_EPF_ALL: 357 case CAM_EPF_NORMAL: 358 proto_flags |= CAM_ESF_PRINT_SENSE; 359 /* FALLTHROUGH */ 360 case CAM_EPF_MINIMAL: 361 proto_flags |= CAM_ESF_PRINT_STATUS; 362 /* FALLTHROUGH */ 363 default: 364 break; 365 } 366 break; 367 case XPT_SMP_IO: 368 switch (proto_flags & CAM_EPF_LEVEL_MASK) { 369 case CAM_EPF_NONE: 370 break; 371 case CAM_EPF_ALL: 372 proto_flags |= CAM_ESMF_PRINT_FULL_CMD; 373 /* FALLTHROUGH */ 374 case CAM_EPF_NORMAL: 375 case CAM_EPF_MINIMAL: 376 proto_flags |= CAM_ESMF_PRINT_STATUS; 377 /* FALLTHROUGH */ 378 default: 379 break; 380 } 381 break; 382 default: 383 break; 384 } 385 #ifdef _KERNEL 386 xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str)); 387 #else /* !_KERNEL */ 388 cam_path_string(device, path_str, sizeof(path_str)); 389 #endif /* _KERNEL/!_KERNEL */ 390 391 sbuf_new(&sb, str, str_len, 0); 392 393 if (flags & CAM_ESF_COMMAND) { 394 sbuf_cat(&sb, path_str); 395 switch (ccb->ccb_h.func_code) { 396 case XPT_ATA_IO: 397 ata_command_sbuf(&ccb->ataio, &sb); 398 break; 399 case XPT_SCSI_IO: 400 #ifdef _KERNEL 401 scsi_command_string(&ccb->csio, &sb); 402 #else /* !_KERNEL */ 403 scsi_command_string(device, &ccb->csio, &sb); 404 #endif /* _KERNEL/!_KERNEL */ 405 break; 406 case XPT_SMP_IO: 407 smp_command_sbuf(&ccb->smpio, &sb, path_str, 79 - 408 strlen(path_str), (proto_flags & 409 CAM_ESMF_PRINT_FULL_CMD) ? 79 : 0); 410 break; 411 case XPT_NVME_IO: 412 case XPT_NVME_ADMIN: 413 nvme_command_sbuf(&ccb->nvmeio, &sb); 414 break; 415 default: 416 sbuf_printf(&sb, "CAM func %#x", 417 ccb->ccb_h.func_code); 418 break; 419 } 420 sbuf_printf(&sb, "\n"); 421 } 422 423 if (flags & CAM_ESF_CAM_STATUS) { 424 cam_status status; 425 const struct cam_status_entry *entry; 426 427 sbuf_cat(&sb, path_str); 428 429 status = ccb->ccb_h.status & CAM_STATUS_MASK; 430 431 entry = cam_fetch_status_entry(status); 432 433 if (entry == NULL) 434 sbuf_printf(&sb, "CAM status: Unknown (%#x)\n", 435 ccb->ccb_h.status); 436 else 437 sbuf_printf(&sb, "CAM status: %s\n", 438 entry->status_text); 439 } 440 441 if (flags & CAM_ESF_PROTO_STATUS) { 442 443 switch (ccb->ccb_h.func_code) { 444 case XPT_ATA_IO: 445 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 446 CAM_ATA_STATUS_ERROR) 447 break; 448 if (proto_flags & CAM_EAF_PRINT_STATUS) { 449 sbuf_cat(&sb, path_str); 450 ata_status_sbuf(&ccb->ataio, &sb); 451 sbuf_printf(&sb, "\n"); 452 } 453 if (proto_flags & CAM_EAF_PRINT_RESULT) { 454 sbuf_cat(&sb, path_str); 455 sbuf_printf(&sb, "RES: "); 456 ata_res_sbuf(&ccb->ataio.res, &sb); 457 sbuf_printf(&sb, "\n"); 458 } 459 460 break; 461 case XPT_SCSI_IO: 462 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 463 CAM_SCSI_STATUS_ERROR) 464 break; 465 466 if (proto_flags & CAM_ESF_PRINT_STATUS) { 467 sbuf_cat(&sb, path_str); 468 sbuf_printf(&sb, "SCSI status: %s\n", 469 scsi_status_string(&ccb->csio)); 470 } 471 472 if ((proto_flags & CAM_ESF_PRINT_SENSE) 473 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 474 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) { 475 #ifdef _KERNEL 476 scsi_sense_sbuf(&ccb->csio, &sb, 477 SSS_FLAG_NONE); 478 #else /* !_KERNEL */ 479 scsi_sense_sbuf(device, &ccb->csio, &sb, 480 SSS_FLAG_NONE); 481 #endif /* _KERNEL/!_KERNEL */ 482 } 483 break; 484 case XPT_SMP_IO: 485 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 486 CAM_SMP_STATUS_ERROR) 487 break; 488 489 if (proto_flags & CAM_ESF_PRINT_STATUS) { 490 sbuf_cat(&sb, path_str); 491 sbuf_printf(&sb, "SMP status: %s (%#x)\n", 492 smp_error_desc(ccb->smpio.smp_response[2]), 493 ccb->smpio.smp_response[2]); 494 } 495 /* There is no SMP equivalent to SCSI sense. */ 496 break; 497 default: 498 break; 499 } 500 } 501 502 sbuf_finish(&sb); 503 504 return(sbuf_data(&sb)); 505 } 506 507 #ifdef _KERNEL 508 509 void 510 cam_error_print(union ccb *ccb, cam_error_string_flags flags, 511 cam_error_proto_flags proto_flags) 512 { 513 char str[512]; 514 515 printf("%s", cam_error_string(ccb, str, sizeof(str), flags, 516 proto_flags)); 517 } 518 519 #else /* !_KERNEL */ 520 521 void 522 cam_error_print(struct cam_device *device, union ccb *ccb, 523 cam_error_string_flags flags, cam_error_proto_flags proto_flags, 524 FILE *ofile) 525 { 526 char str[512]; 527 528 if ((device == NULL) || (ccb == NULL) || (ofile == NULL)) 529 return; 530 531 fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str), 532 flags, proto_flags)); 533 } 534 535 #endif /* _KERNEL/!_KERNEL */ 536 537 /* 538 * Common calculate geometry fuction 539 * 540 * Caller should set ccg->volume_size and block_size. 541 * The extended parameter should be zero if extended translation 542 * should not be used. 543 */ 544 void 545 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended) 546 { 547 uint32_t size_mb, secs_per_cylinder; 548 549 if (ccg->block_size == 0) { 550 ccg->ccb_h.status = CAM_REQ_CMP_ERR; 551 return; 552 } 553 size_mb = (1024L * 1024L) / ccg->block_size; 554 if (size_mb == 0) { 555 ccg->ccb_h.status = CAM_REQ_CMP_ERR; 556 return; 557 } 558 size_mb = ccg->volume_size / size_mb; 559 if (size_mb > 1024 && extended) { 560 ccg->heads = 255; 561 ccg->secs_per_track = 63; 562 } else { 563 ccg->heads = 64; 564 ccg->secs_per_track = 32; 565 } 566 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 567 if (secs_per_cylinder == 0) { 568 ccg->ccb_h.status = CAM_REQ_CMP_ERR; 569 return; 570 } 571 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 572 ccg->ccb_h.status = CAM_REQ_CMP; 573 } 574