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 127 /* Trim leading/trailing spaces, nulls. */ 128 while (srclen > 0 && src[0] == ' ') 129 src++, srclen--; 130 while (srclen > 0 131 && (src[srclen-1] == ' ' || src[srclen-1] == '\0')) 132 srclen--; 133 134 while (srclen > 0 && dstlen > 1) { 135 u_int8_t *cur_pos = dst; 136 137 if (*src < 0x20 || *src >= 0x80) { 138 /* SCSI-II Specifies that these should never occur. */ 139 /* non-printable character */ 140 if (dstlen > 4) { 141 *cur_pos++ = '\\'; 142 *cur_pos++ = ((*src & 0300) >> 6) + '0'; 143 *cur_pos++ = ((*src & 0070) >> 3) + '0'; 144 *cur_pos++ = ((*src & 0007) >> 0) + '0'; 145 } else { 146 *cur_pos++ = '?'; 147 } 148 } else { 149 /* normal character */ 150 *cur_pos++ = *src; 151 } 152 src++; 153 srclen--; 154 dstlen -= cur_pos - dst; 155 dst = cur_pos; 156 } 157 *dst = '\0'; 158 } 159 160 void 161 cam_strvis_sbuf(struct sbuf *sb, const u_int8_t *src, int srclen, 162 uint32_t flags) 163 { 164 165 /* Trim leading/trailing spaces, nulls. */ 166 while (srclen > 0 && src[0] == ' ') 167 src++, srclen--; 168 while (srclen > 0 169 && (src[srclen-1] == ' ' || src[srclen-1] == '\0')) 170 srclen--; 171 172 while (srclen > 0) { 173 if (*src < 0x20 || *src >= 0x80) { 174 /* SCSI-II Specifies that these should never occur. */ 175 /* non-printable character */ 176 switch (flags & CAM_STRVIS_FLAG_NONASCII_MASK) { 177 case CAM_STRVIS_FLAG_NONASCII_ESC: 178 sbuf_printf(sb, "\\%c%c%c", 179 ((*src & 0300) >> 6) + '0', 180 ((*src & 0070) >> 3) + '0', 181 ((*src & 0007) >> 0) + '0'); 182 break; 183 case CAM_STRVIS_FLAG_NONASCII_RAW: 184 /* 185 * If we run into a NUL, just transform it 186 * into a space. 187 */ 188 if (*src != 0x00) 189 sbuf_putc(sb, *src); 190 else 191 sbuf_putc(sb, ' '); 192 break; 193 case CAM_STRVIS_FLAG_NONASCII_SPC: 194 sbuf_putc(sb, ' '); 195 break; 196 case CAM_STRVIS_FLAG_NONASCII_TRIM: 197 default: 198 break; 199 } 200 } else { 201 /* normal character */ 202 sbuf_putc(sb, *src); 203 } 204 src++; 205 srclen--; 206 } 207 } 208 209 210 /* 211 * Compare string with pattern, returning 0 on match. 212 * Short pattern matches trailing blanks in name, 213 * Shell globbing rules apply: * matches 0 or more characters, 214 * ? matchces one character, [...] denotes a set to match one char, 215 * [^...] denotes a complimented set to match one character. 216 * Spaces in str used to match anything in the pattern string 217 * but was removed because it's a bug. No current patterns require 218 * it, as far as I know, but it's impossible to know what drives 219 * returned. 220 * 221 * Each '*' generates recursion, so keep the number of * in check. 222 */ 223 int 224 cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len) 225 { 226 227 while (*pattern != '\0' && str_len > 0) { 228 if (*pattern == '*') { 229 pattern++; 230 if (*pattern == '\0') 231 return (0); 232 do { 233 if (cam_strmatch(str, pattern, str_len) == 0) 234 return (0); 235 str++; 236 str_len--; 237 } while (str_len > 0); 238 return (1); 239 } else if (*pattern == '[') { 240 int negate_range, ok; 241 uint8_t pc = UCHAR_MAX; 242 uint8_t sc; 243 244 ok = 0; 245 sc = *str++; 246 str_len--; 247 pattern++; 248 if ((negate_range = (*pattern == '^')) != 0) 249 pattern++; 250 while ((*pattern != ']') && *pattern != '\0') { 251 if (*pattern == '-') { 252 if (pattern[1] == '\0') /* Bad pattern */ 253 return (1); 254 if (sc >= pc && sc <= pattern[1]) 255 ok = 1; 256 pattern++; 257 } else if (*pattern == sc) 258 ok = 1; 259 pc = *pattern; 260 pattern++; 261 } 262 if (ok == negate_range) 263 return (1); 264 pattern++; 265 } else if (*pattern == '?') { 266 /* 267 * NB: || *str == ' ' of the old code is a bug and was 268 * removed. If you add it back, keep this the last if 269 * before the naked else */ 270 pattern++; 271 str++; 272 str_len--; 273 } else { 274 if (*str != *pattern) 275 return (1); 276 pattern++; 277 str++; 278 str_len--; 279 } 280 } 281 282 /* '*' is allowed to match nothing, so gobble it */ 283 while (*pattern == '*') 284 pattern++; 285 286 if ( *pattern != '\0') { 287 /* Pattern not fully consumed. Not a match */ 288 return (1); 289 } 290 291 /* Eat trailing spaces, which get added by SAT */ 292 while (str_len > 0 && *str == ' ') { 293 str++; 294 str_len--; 295 } 296 297 return (str_len); 298 } 299 300 caddr_t 301 cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries, 302 int entry_size, cam_quirkmatch_t *comp_func) 303 { 304 for (; num_entries > 0; num_entries--, quirk_table += entry_size) { 305 if ((*comp_func)(target, quirk_table) == 0) 306 return (quirk_table); 307 } 308 return (NULL); 309 } 310 311 const struct cam_status_entry* 312 cam_fetch_status_entry(cam_status status) 313 { 314 status &= CAM_STATUS_MASK; 315 return (bsearch(&status, &cam_status_table, 316 nitems(cam_status_table), 317 sizeof(*cam_status_table), 318 camstatusentrycomp)); 319 } 320 321 static int 322 camstatusentrycomp(const void *key, const void *member) 323 { 324 cam_status status; 325 const struct cam_status_entry *table_entry; 326 327 status = *(const cam_status *)key; 328 table_entry = (const struct cam_status_entry *)member; 329 330 return (status - table_entry->status_code); 331 } 332 333 334 #ifdef _KERNEL 335 char * 336 cam_error_string(union ccb *ccb, char *str, int str_len, 337 cam_error_string_flags flags, 338 cam_error_proto_flags proto_flags) 339 #else /* !_KERNEL */ 340 char * 341 cam_error_string(struct cam_device *device, union ccb *ccb, char *str, 342 int str_len, cam_error_string_flags flags, 343 cam_error_proto_flags proto_flags) 344 #endif /* _KERNEL/!_KERNEL */ 345 { 346 char path_str[64]; 347 struct sbuf sb; 348 349 if ((ccb == NULL) 350 || (str == NULL) 351 || (str_len <= 0)) 352 return(NULL); 353 354 if (flags == CAM_ESF_NONE) 355 return(NULL); 356 357 switch (ccb->ccb_h.func_code) { 358 case XPT_ATA_IO: 359 switch (proto_flags & CAM_EPF_LEVEL_MASK) { 360 case CAM_EPF_NONE: 361 break; 362 case CAM_EPF_ALL: 363 case CAM_EPF_NORMAL: 364 proto_flags |= CAM_EAF_PRINT_RESULT; 365 /* FALLTHROUGH */ 366 case CAM_EPF_MINIMAL: 367 proto_flags |= CAM_EAF_PRINT_STATUS; 368 /* FALLTHROUGH */ 369 default: 370 break; 371 } 372 break; 373 case XPT_SCSI_IO: 374 switch (proto_flags & CAM_EPF_LEVEL_MASK) { 375 case CAM_EPF_NONE: 376 break; 377 case CAM_EPF_ALL: 378 case CAM_EPF_NORMAL: 379 proto_flags |= CAM_ESF_PRINT_SENSE; 380 /* FALLTHROUGH */ 381 case CAM_EPF_MINIMAL: 382 proto_flags |= CAM_ESF_PRINT_STATUS; 383 /* FALLTHROUGH */ 384 default: 385 break; 386 } 387 break; 388 case XPT_SMP_IO: 389 switch (proto_flags & CAM_EPF_LEVEL_MASK) { 390 case CAM_EPF_NONE: 391 break; 392 case CAM_EPF_ALL: 393 proto_flags |= CAM_ESMF_PRINT_FULL_CMD; 394 /* FALLTHROUGH */ 395 case CAM_EPF_NORMAL: 396 case CAM_EPF_MINIMAL: 397 proto_flags |= CAM_ESMF_PRINT_STATUS; 398 /* FALLTHROUGH */ 399 default: 400 break; 401 } 402 break; 403 default: 404 break; 405 } 406 #ifdef _KERNEL 407 xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str)); 408 #else /* !_KERNEL */ 409 cam_path_string(device, path_str, sizeof(path_str)); 410 #endif /* _KERNEL/!_KERNEL */ 411 412 sbuf_new(&sb, str, str_len, 0); 413 414 if (flags & CAM_ESF_COMMAND) { 415 sbuf_cat(&sb, path_str); 416 switch (ccb->ccb_h.func_code) { 417 case XPT_ATA_IO: 418 ata_command_sbuf(&ccb->ataio, &sb); 419 break; 420 case XPT_SCSI_IO: 421 #ifdef _KERNEL 422 scsi_command_string(&ccb->csio, &sb); 423 #else /* !_KERNEL */ 424 scsi_command_string(device, &ccb->csio, &sb); 425 #endif /* _KERNEL/!_KERNEL */ 426 break; 427 case XPT_SMP_IO: 428 smp_command_sbuf(&ccb->smpio, &sb, path_str, 79 - 429 strlen(path_str), (proto_flags & 430 CAM_ESMF_PRINT_FULL_CMD) ? 79 : 0); 431 break; 432 case XPT_NVME_IO: 433 case XPT_NVME_ADMIN: 434 nvme_command_sbuf(&ccb->nvmeio, &sb); 435 break; 436 default: 437 sbuf_printf(&sb, "CAM func %#x", 438 ccb->ccb_h.func_code); 439 break; 440 } 441 sbuf_printf(&sb, "\n"); 442 } 443 444 if (flags & CAM_ESF_CAM_STATUS) { 445 cam_status status; 446 const struct cam_status_entry *entry; 447 448 sbuf_cat(&sb, path_str); 449 450 status = ccb->ccb_h.status & CAM_STATUS_MASK; 451 452 entry = cam_fetch_status_entry(status); 453 454 if (entry == NULL) 455 sbuf_printf(&sb, "CAM status: Unknown (%#x)\n", 456 ccb->ccb_h.status); 457 else 458 sbuf_printf(&sb, "CAM status: %s\n", 459 entry->status_text); 460 } 461 462 if (flags & CAM_ESF_PROTO_STATUS) { 463 464 switch (ccb->ccb_h.func_code) { 465 case XPT_ATA_IO: 466 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 467 CAM_ATA_STATUS_ERROR) 468 break; 469 if (proto_flags & CAM_EAF_PRINT_STATUS) { 470 sbuf_cat(&sb, path_str); 471 ata_status_sbuf(&ccb->ataio, &sb); 472 sbuf_printf(&sb, "\n"); 473 } 474 if (proto_flags & CAM_EAF_PRINT_RESULT) { 475 sbuf_cat(&sb, path_str); 476 sbuf_printf(&sb, "RES: "); 477 ata_res_sbuf(&ccb->ataio.res, &sb); 478 sbuf_printf(&sb, "\n"); 479 } 480 481 break; 482 case XPT_SCSI_IO: 483 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 484 CAM_SCSI_STATUS_ERROR) 485 break; 486 487 if (proto_flags & CAM_ESF_PRINT_STATUS) { 488 sbuf_cat(&sb, path_str); 489 sbuf_printf(&sb, "SCSI status: %s\n", 490 scsi_status_string(&ccb->csio)); 491 } 492 493 if ((proto_flags & CAM_ESF_PRINT_SENSE) 494 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 495 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) { 496 497 #ifdef _KERNEL 498 scsi_sense_sbuf(&ccb->csio, &sb, 499 SSS_FLAG_NONE); 500 #else /* !_KERNEL */ 501 scsi_sense_sbuf(device, &ccb->csio, &sb, 502 SSS_FLAG_NONE); 503 #endif /* _KERNEL/!_KERNEL */ 504 } 505 break; 506 case XPT_SMP_IO: 507 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 508 CAM_SMP_STATUS_ERROR) 509 break; 510 511 if (proto_flags & CAM_ESF_PRINT_STATUS) { 512 sbuf_cat(&sb, path_str); 513 sbuf_printf(&sb, "SMP status: %s (%#x)\n", 514 smp_error_desc(ccb->smpio.smp_response[2]), 515 ccb->smpio.smp_response[2]); 516 } 517 /* There is no SMP equivalent to SCSI sense. */ 518 break; 519 default: 520 break; 521 } 522 } 523 524 sbuf_finish(&sb); 525 526 return(sbuf_data(&sb)); 527 } 528 529 #ifdef _KERNEL 530 531 void 532 cam_error_print(union ccb *ccb, cam_error_string_flags flags, 533 cam_error_proto_flags proto_flags) 534 { 535 char str[512]; 536 537 printf("%s", cam_error_string(ccb, str, sizeof(str), flags, 538 proto_flags)); 539 } 540 541 #else /* !_KERNEL */ 542 543 void 544 cam_error_print(struct cam_device *device, union ccb *ccb, 545 cam_error_string_flags flags, cam_error_proto_flags proto_flags, 546 FILE *ofile) 547 { 548 char str[512]; 549 550 if ((device == NULL) || (ccb == NULL) || (ofile == NULL)) 551 return; 552 553 fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str), 554 flags, proto_flags)); 555 } 556 557 #endif /* _KERNEL/!_KERNEL */ 558 559 /* 560 * Common calculate geometry fuction 561 * 562 * Caller should set ccg->volume_size and block_size. 563 * The extended parameter should be zero if extended translation 564 * should not be used. 565 */ 566 void 567 cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended) 568 { 569 uint32_t size_mb, secs_per_cylinder; 570 571 if (ccg->block_size == 0) { 572 ccg->ccb_h.status = CAM_REQ_CMP_ERR; 573 return; 574 } 575 size_mb = (1024L * 1024L) / ccg->block_size; 576 if (size_mb == 0) { 577 ccg->ccb_h.status = CAM_REQ_CMP_ERR; 578 return; 579 } 580 size_mb = ccg->volume_size / size_mb; 581 if (size_mb > 1024 && extended) { 582 ccg->heads = 255; 583 ccg->secs_per_track = 63; 584 } else { 585 ccg->heads = 64; 586 ccg->secs_per_track = 32; 587 } 588 secs_per_cylinder = ccg->heads * ccg->secs_per_track; 589 if (secs_per_cylinder == 0) { 590 ccg->ccb_h.status = CAM_REQ_CMP_ERR; 591 return; 592 } 593 ccg->cylinders = ccg->volume_size / secs_per_cylinder; 594 ccg->ccb_h.status = CAM_REQ_CMP; 595 } 596