1898b0535SWarner Losh /*- 28b8a9b1dSJustin T. Gibbs * Generic utility routines for the Common Access Method layer. 38b8a9b1dSJustin T. Gibbs * 4bec9534dSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5bec9534dSPedro F. Giffuni * 68b8a9b1dSJustin T. Gibbs * Copyright (c) 1997 Justin T. Gibbs. 78b8a9b1dSJustin T. Gibbs * All rights reserved. 88b8a9b1dSJustin T. Gibbs * 98b8a9b1dSJustin T. Gibbs * Redistribution and use in source and binary forms, with or without 108b8a9b1dSJustin T. Gibbs * modification, are permitted provided that the following conditions 118b8a9b1dSJustin T. Gibbs * are met: 128b8a9b1dSJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright 138b8a9b1dSJustin T. Gibbs * notice, this list of conditions, and the following disclaimer, 148b8a9b1dSJustin T. Gibbs * without modification, immediately at the beginning of the file. 158b8a9b1dSJustin T. Gibbs * 2. The name of the author may not be used to endorse or promote products 168b8a9b1dSJustin T. Gibbs * derived from this software without specific prior written permission. 178b8a9b1dSJustin T. Gibbs * 188b8a9b1dSJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 198b8a9b1dSJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 208b8a9b1dSJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 218b8a9b1dSJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 228b8a9b1dSJustin T. Gibbs * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 238b8a9b1dSJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 248b8a9b1dSJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 258b8a9b1dSJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 268b8a9b1dSJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 278b8a9b1dSJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 288b8a9b1dSJustin T. Gibbs * SUCH DAMAGE. 298b8a9b1dSJustin T. Gibbs */ 308b8a9b1dSJustin T. Gibbs 319c963d87SDavid E. O'Brien #include <sys/cdefs.h> 329c963d87SDavid E. O'Brien __FBSDID("$FreeBSD$"); 339c963d87SDavid E. O'Brien 349c963d87SDavid E. O'Brien #include <sys/param.h> 353393f8daSKenneth D. Merry #ifdef _KERNEL 363393f8daSKenneth D. Merry #include <sys/systm.h> 3765c38256SMike Smith #include <sys/kernel.h> 3865c38256SMike Smith #include <sys/sysctl.h> 393393f8daSKenneth D. Merry #else /* _KERNEL */ 403393f8daSKenneth D. Merry #include <stdlib.h> 413393f8daSKenneth D. Merry #include <stdio.h> 4206e79492SKenneth D. Merry #include <string.h> 43912e4916SEd Schouten #include <camlib.h> 443393f8daSKenneth D. Merry #endif /* _KERNEL */ 453393f8daSKenneth D. Merry 468b8a9b1dSJustin T. Gibbs #include <cam/cam.h> 473393f8daSKenneth D. Merry #include <cam/cam_ccb.h> 483393f8daSKenneth D. Merry #include <cam/scsi/scsi_all.h> 4906e79492SKenneth D. Merry #include <cam/scsi/smp_all.h> 503393f8daSKenneth D. Merry #include <sys/sbuf.h> 513393f8daSKenneth D. Merry 523393f8daSKenneth D. Merry #ifdef _KERNEL 533393f8daSKenneth D. Merry #include <sys/libkern.h> 5452c9ce25SScott Long #include <cam/cam_queue.h> 553393f8daSKenneth D. Merry #include <cam/cam_xpt.h> 56de5b1952SAlexander Leidinger 57de5b1952SAlexander Leidinger FEATURE(scbus, "SCSI devices support"); 58de5b1952SAlexander Leidinger 593393f8daSKenneth D. Merry #endif 603393f8daSKenneth D. Merry 613393f8daSKenneth D. Merry static int camstatusentrycomp(const void *key, const void *member); 623393f8daSKenneth D. Merry 633393f8daSKenneth D. Merry const struct cam_status_entry cam_status_table[] = { 643393f8daSKenneth D. Merry { CAM_REQ_INPROG, "CCB request is in progress" }, 653393f8daSKenneth D. Merry { CAM_REQ_CMP, "CCB request completed without error" }, 663393f8daSKenneth D. Merry { CAM_REQ_ABORTED, "CCB request aborted by the host" }, 673393f8daSKenneth D. Merry { CAM_UA_ABORT, "Unable to abort CCB request" }, 683393f8daSKenneth D. Merry { CAM_REQ_CMP_ERR, "CCB request completed with an error" }, 69104fad28SBenedict Reuschling { CAM_BUSY, "CAM subsystem is busy" }, 703393f8daSKenneth D. Merry { CAM_REQ_INVALID, "CCB request was invalid" }, 713393f8daSKenneth D. Merry { CAM_PATH_INVALID, "Supplied Path ID is invalid" }, 723393f8daSKenneth D. Merry { CAM_DEV_NOT_THERE, "Device Not Present" }, 733393f8daSKenneth D. Merry { CAM_UA_TERMIO, "Unable to terminate I/O CCB request" }, 743393f8daSKenneth D. Merry { CAM_SEL_TIMEOUT, "Selection Timeout" }, 753393f8daSKenneth D. Merry { CAM_CMD_TIMEOUT, "Command timeout" }, 763393f8daSKenneth D. Merry { CAM_SCSI_STATUS_ERROR, "SCSI Status Error" }, 773393f8daSKenneth D. Merry { CAM_MSG_REJECT_REC, "Message Reject Reveived" }, 783393f8daSKenneth D. Merry { CAM_SCSI_BUS_RESET, "SCSI Bus Reset Sent/Received" }, 793393f8daSKenneth D. Merry { CAM_UNCOR_PARITY, "Uncorrectable parity/CRC error" }, 803393f8daSKenneth D. Merry { CAM_AUTOSENSE_FAIL, "Auto-Sense Retrieval Failed" }, 813393f8daSKenneth D. Merry { CAM_NO_HBA, "No HBA Detected" }, 823393f8daSKenneth D. Merry { CAM_DATA_RUN_ERR, "Data Overrun error" }, 833393f8daSKenneth D. Merry { CAM_UNEXP_BUSFREE, "Unexpected Bus Free" }, 843393f8daSKenneth D. Merry { CAM_SEQUENCE_FAIL, "Target Bus Phase Sequence Failure" }, 853393f8daSKenneth D. Merry { CAM_CCB_LEN_ERR, "CCB length supplied is inadequate" }, 863393f8daSKenneth D. Merry { CAM_PROVIDE_FAIL, "Unable to provide requested capability" }, 873393f8daSKenneth D. Merry { CAM_BDR_SENT, "SCSI BDR Message Sent" }, 883393f8daSKenneth D. Merry { CAM_REQ_TERMIO, "CCB request terminated by the host" }, 893393f8daSKenneth D. Merry { CAM_UNREC_HBA_ERROR, "Unrecoverable Host Bus Adapter Error" }, 903393f8daSKenneth D. Merry { CAM_REQ_TOO_BIG, "The request was too large for this host" }, 913393f8daSKenneth D. Merry { CAM_REQUEUE_REQ, "Unconditionally Re-queue Request", }, 9252c9ce25SScott Long { CAM_ATA_STATUS_ERROR, "ATA Status Error" }, 9306e79492SKenneth D. Merry { CAM_SCSI_IT_NEXUS_LOST,"Initiator/Target Nexus Lost" }, 9406e79492SKenneth D. Merry { CAM_SMP_STATUS_ERROR, "SMP Status Error" }, 953393f8daSKenneth D. Merry { CAM_IDE, "Initiator Detected Error Message Received" }, 963393f8daSKenneth D. Merry { CAM_RESRC_UNAVAIL, "Resource Unavailable" }, 973393f8daSKenneth D. Merry { CAM_UNACKED_EVENT, "Unacknowledged Event by Host" }, 983393f8daSKenneth D. Merry { CAM_MESSAGE_RECV, "Message Received in Host Target Mode" }, 993393f8daSKenneth D. Merry { CAM_INVALID_CDB, "Invalid CDB received in Host Target Mode" }, 1003393f8daSKenneth D. Merry { CAM_LUN_INVALID, "Invalid Lun" }, 1013393f8daSKenneth D. Merry { CAM_TID_INVALID, "Invalid Target ID" }, 1023393f8daSKenneth D. Merry { CAM_FUNC_NOTAVAIL, "Function Not Available" }, 1033393f8daSKenneth D. Merry { CAM_NO_NEXUS, "Nexus Not Established" }, 1043393f8daSKenneth D. Merry { CAM_IID_INVALID, "Invalid Initiator ID" }, 1053393f8daSKenneth D. Merry { CAM_CDB_RECVD, "CDB Received" }, 1063393f8daSKenneth D. Merry { CAM_LUN_ALRDY_ENA, "LUN Already Enabled for Target Mode" }, 1073393f8daSKenneth D. Merry { CAM_SCSI_BUSY, "SCSI Bus Busy" }, 1083393f8daSKenneth D. Merry }; 1093393f8daSKenneth D. Merry 11065c38256SMike Smith #ifdef _KERNEL 1117029da5cSPawel Biernacki SYSCTL_NODE(_kern, OID_AUTO, cam, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 1127029da5cSPawel Biernacki "CAM Subsystem"); 1135f83aee5SSteven Hartland 1145f83aee5SSteven Hartland #ifndef CAM_DEFAULT_SORT_IO_QUEUES 1155f83aee5SSteven Hartland #define CAM_DEFAULT_SORT_IO_QUEUES 1 1165f83aee5SSteven Hartland #endif 1175f83aee5SSteven Hartland 1185f83aee5SSteven Hartland int cam_sort_io_queues = CAM_DEFAULT_SORT_IO_QUEUES; 1195f83aee5SSteven Hartland SYSCTL_INT(_kern_cam, OID_AUTO, sort_io_queues, CTLFLAG_RWTUN, 1205f83aee5SSteven Hartland &cam_sort_io_queues, 0, "Sort IO queues to try and optimise disk access patterns"); 12165c38256SMike Smith #endif 12265c38256SMike Smith 1238b8a9b1dSJustin T. Gibbs void 1248b8a9b1dSJustin T. Gibbs cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen) 1258b8a9b1dSJustin T. Gibbs { 126*3090d504SKenneth D. Merry cam_strvis_flag(dst, src, srclen, dstlen, 127*3090d504SKenneth D. Merry CAM_STRVIS_FLAG_NONASCII_ESC); 1288b8a9b1dSJustin T. Gibbs } 129*3090d504SKenneth D. Merry 130*3090d504SKenneth D. Merry void 131*3090d504SKenneth D. Merry cam_strvis_flag(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen, 132*3090d504SKenneth D. Merry uint32_t flags) 133*3090d504SKenneth D. Merry { 134*3090d504SKenneth D. Merry struct sbuf sb; 135*3090d504SKenneth D. Merry 136*3090d504SKenneth D. Merry sbuf_new(&sb, dst, dstlen, SBUF_FIXEDLEN); 137*3090d504SKenneth D. Merry cam_strvis_sbuf(&sb, src, srclen, flags); 138*3090d504SKenneth D. Merry sbuf_finish(&sb); 1398b8a9b1dSJustin T. Gibbs } 1408b8a9b1dSJustin T. Gibbs 1415672fac9SKenneth D. Merry void 1425672fac9SKenneth D. Merry cam_strvis_sbuf(struct sbuf *sb, const u_int8_t *src, int srclen, 1435672fac9SKenneth D. Merry uint32_t flags) 1445672fac9SKenneth D. Merry { 1455672fac9SKenneth D. Merry 1465672fac9SKenneth D. Merry /* Trim leading/trailing spaces, nulls. */ 1475672fac9SKenneth D. Merry while (srclen > 0 && src[0] == ' ') 1485672fac9SKenneth D. Merry src++, srclen--; 1495672fac9SKenneth D. Merry while (srclen > 0 1505672fac9SKenneth D. Merry && (src[srclen-1] == ' ' || src[srclen-1] == '\0')) 1515672fac9SKenneth D. Merry srclen--; 1525672fac9SKenneth D. Merry 1535672fac9SKenneth D. Merry while (srclen > 0) { 1545672fac9SKenneth D. Merry if (*src < 0x20 || *src >= 0x80) { 1555672fac9SKenneth D. Merry /* SCSI-II Specifies that these should never occur. */ 1565672fac9SKenneth D. Merry /* non-printable character */ 1575672fac9SKenneth D. Merry switch (flags & CAM_STRVIS_FLAG_NONASCII_MASK) { 1585672fac9SKenneth D. Merry case CAM_STRVIS_FLAG_NONASCII_ESC: 1595672fac9SKenneth D. Merry sbuf_printf(sb, "\\%c%c%c", 1605672fac9SKenneth D. Merry ((*src & 0300) >> 6) + '0', 1615672fac9SKenneth D. Merry ((*src & 0070) >> 3) + '0', 1625672fac9SKenneth D. Merry ((*src & 0007) >> 0) + '0'); 1635672fac9SKenneth D. Merry break; 1645672fac9SKenneth D. Merry case CAM_STRVIS_FLAG_NONASCII_RAW: 1655672fac9SKenneth D. Merry /* 1665672fac9SKenneth D. Merry * If we run into a NUL, just transform it 1675672fac9SKenneth D. Merry * into a space. 1685672fac9SKenneth D. Merry */ 1695672fac9SKenneth D. Merry if (*src != 0x00) 1705672fac9SKenneth D. Merry sbuf_putc(sb, *src); 1715672fac9SKenneth D. Merry else 1725672fac9SKenneth D. Merry sbuf_putc(sb, ' '); 1735672fac9SKenneth D. Merry break; 1745672fac9SKenneth D. Merry case CAM_STRVIS_FLAG_NONASCII_SPC: 1755672fac9SKenneth D. Merry sbuf_putc(sb, ' '); 1765672fac9SKenneth D. Merry break; 1775672fac9SKenneth D. Merry case CAM_STRVIS_FLAG_NONASCII_TRIM: 1785672fac9SKenneth D. Merry default: 1795672fac9SKenneth D. Merry break; 1805672fac9SKenneth D. Merry } 1815672fac9SKenneth D. Merry } else { 1825672fac9SKenneth D. Merry /* normal character */ 1835672fac9SKenneth D. Merry sbuf_putc(sb, *src); 1845672fac9SKenneth D. Merry } 1855672fac9SKenneth D. Merry src++; 1865672fac9SKenneth D. Merry srclen--; 1875672fac9SKenneth D. Merry } 1885672fac9SKenneth D. Merry } 1895672fac9SKenneth D. Merry 1908b8a9b1dSJustin T. Gibbs /* 1918b8a9b1dSJustin T. Gibbs * Compare string with pattern, returning 0 on match. 1928b8a9b1dSJustin T. Gibbs * Short pattern matches trailing blanks in name, 1933f04cc10SWarner Losh * Shell globbing rules apply: * matches 0 or more characters, 1943f04cc10SWarner Losh * ? matchces one character, [...] denotes a set to match one char, 1953f04cc10SWarner Losh * [^...] denotes a complimented set to match one character. 1963f04cc10SWarner Losh * Spaces in str used to match anything in the pattern string 1973f04cc10SWarner Losh * but was removed because it's a bug. No current patterns require 1983f04cc10SWarner Losh * it, as far as I know, but it's impossible to know what drives 1993f04cc10SWarner Losh * returned. 2003f04cc10SWarner Losh * 2013f04cc10SWarner Losh * Each '*' generates recursion, so keep the number of * in check. 2028b8a9b1dSJustin T. Gibbs */ 2038b8a9b1dSJustin T. Gibbs int 2048b8a9b1dSJustin T. Gibbs cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len) 2058b8a9b1dSJustin T. Gibbs { 2068b8a9b1dSJustin T. Gibbs 2078b8a9b1dSJustin T. Gibbs while (*pattern != '\0' && str_len > 0) { 2088b8a9b1dSJustin T. Gibbs if (*pattern == '*') { 2093f04cc10SWarner Losh pattern++; 2103f04cc10SWarner Losh if (*pattern == '\0') 2118b8a9b1dSJustin T. Gibbs return (0); 2123f04cc10SWarner Losh do { 2133f04cc10SWarner Losh if (cam_strmatch(str, pattern, str_len) == 0) 2143f04cc10SWarner Losh return (0); 2153f04cc10SWarner Losh str++; 2163f04cc10SWarner Losh str_len--; 2173f04cc10SWarner Losh } while (str_len > 0); 2188b8a9b1dSJustin T. Gibbs return (1); 2193f04cc10SWarner Losh } else if (*pattern == '[') { 2203f04cc10SWarner Losh int negate_range, ok; 221e7dda951SAlan Somers uint8_t pc = UCHAR_MAX; 222e7dda951SAlan Somers uint8_t sc; 2233f04cc10SWarner Losh 2243f04cc10SWarner Losh ok = 0; 2253f04cc10SWarner Losh sc = *str++; 2263f04cc10SWarner Losh str_len--; 2278a0a413eSAlan Somers pattern++; 2283f04cc10SWarner Losh if ((negate_range = (*pattern == '^')) != 0) 2293f04cc10SWarner Losh pattern++; 2308a0a413eSAlan Somers while ((*pattern != ']') && *pattern != '\0') { 2313f04cc10SWarner Losh if (*pattern == '-') { 2323f04cc10SWarner Losh if (pattern[1] == '\0') /* Bad pattern */ 2333f04cc10SWarner Losh return (1); 2343f04cc10SWarner Losh if (sc >= pc && sc <= pattern[1]) 2353f04cc10SWarner Losh ok = 1; 2368a0a413eSAlan Somers pattern++; 2378a0a413eSAlan Somers } else if (*pattern == sc) 2383f04cc10SWarner Losh ok = 1; 2398a0a413eSAlan Somers pc = *pattern; 2408a0a413eSAlan Somers pattern++; 2418b8a9b1dSJustin T. Gibbs } 2423f04cc10SWarner Losh if (ok == negate_range) 2433f04cc10SWarner Losh return (1); 2448a0a413eSAlan Somers pattern++; 2453f04cc10SWarner Losh } else if (*pattern == '?') { 2468a0a413eSAlan Somers /* 2478a0a413eSAlan Somers * NB: || *str == ' ' of the old code is a bug and was 2488a0a413eSAlan Somers * removed. If you add it back, keep this the last if 2498a0a413eSAlan Somers * before the naked else */ 2508b8a9b1dSJustin T. Gibbs pattern++; 2518b8a9b1dSJustin T. Gibbs str++; 2528b8a9b1dSJustin T. Gibbs str_len--; 2533f04cc10SWarner Losh } else { 2543f04cc10SWarner Losh if (*str != *pattern) 2553f04cc10SWarner Losh return (1); 2563f04cc10SWarner Losh pattern++; 2573f04cc10SWarner Losh str++; 2583f04cc10SWarner Losh str_len--; 2593f04cc10SWarner Losh } 2608b8a9b1dSJustin T. Gibbs } 2618a0a413eSAlan Somers 2628a0a413eSAlan Somers /* '*' is allowed to match nothing, so gobble it */ 2638a0a413eSAlan Somers while (*pattern == '*') 2648a0a413eSAlan Somers pattern++; 2658a0a413eSAlan Somers 2668a0a413eSAlan Somers if ( *pattern != '\0') { 2678a0a413eSAlan Somers /* Pattern not fully consumed. Not a match */ 2688a0a413eSAlan Somers return (1); 2698a0a413eSAlan Somers } 2708a0a413eSAlan Somers 2718a0a413eSAlan Somers /* Eat trailing spaces, which get added by SAT */ 27230a4094fSAlexander Motin while (str_len > 0 && *str == ' ') { 27330a4094fSAlexander Motin str++; 2748b8a9b1dSJustin T. Gibbs str_len--; 27530a4094fSAlexander Motin } 2768b8a9b1dSJustin T. Gibbs 2778b8a9b1dSJustin T. Gibbs return (str_len); 2788b8a9b1dSJustin T. Gibbs } 2798b8a9b1dSJustin T. Gibbs 2808b8a9b1dSJustin T. Gibbs caddr_t 2818b8a9b1dSJustin T. Gibbs cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries, 2828b8a9b1dSJustin T. Gibbs int entry_size, cam_quirkmatch_t *comp_func) 2838b8a9b1dSJustin T. Gibbs { 2848b8a9b1dSJustin T. Gibbs for (; num_entries > 0; num_entries--, quirk_table += entry_size) { 2858b8a9b1dSJustin T. Gibbs if ((*comp_func)(target, quirk_table) == 0) 2868b8a9b1dSJustin T. Gibbs return (quirk_table); 2878b8a9b1dSJustin T. Gibbs } 2888b8a9b1dSJustin T. Gibbs return (NULL); 2898b8a9b1dSJustin T. Gibbs } 2903393f8daSKenneth D. Merry 2913393f8daSKenneth D. Merry const struct cam_status_entry* 2923393f8daSKenneth D. Merry cam_fetch_status_entry(cam_status status) 2933393f8daSKenneth D. Merry { 2943393f8daSKenneth D. Merry status &= CAM_STATUS_MASK; 2953393f8daSKenneth D. Merry return (bsearch(&status, &cam_status_table, 2968dfea464SPedro F. Giffuni nitems(cam_status_table), 2973393f8daSKenneth D. Merry sizeof(*cam_status_table), 2983393f8daSKenneth D. Merry camstatusentrycomp)); 2993393f8daSKenneth D. Merry } 3003393f8daSKenneth D. Merry 3013393f8daSKenneth D. Merry static int 3023393f8daSKenneth D. Merry camstatusentrycomp(const void *key, const void *member) 3033393f8daSKenneth D. Merry { 3043393f8daSKenneth D. Merry cam_status status; 3053393f8daSKenneth D. Merry const struct cam_status_entry *table_entry; 3063393f8daSKenneth D. Merry 3073393f8daSKenneth D. Merry status = *(const cam_status *)key; 3083393f8daSKenneth D. Merry table_entry = (const struct cam_status_entry *)member; 3093393f8daSKenneth D. Merry 3103393f8daSKenneth D. Merry return (status - table_entry->status_code); 3113393f8daSKenneth D. Merry } 3123393f8daSKenneth D. Merry 3133393f8daSKenneth D. Merry #ifdef _KERNEL 3143393f8daSKenneth D. Merry char * 3153393f8daSKenneth D. Merry cam_error_string(union ccb *ccb, char *str, int str_len, 3163393f8daSKenneth D. Merry cam_error_string_flags flags, 3173393f8daSKenneth D. Merry cam_error_proto_flags proto_flags) 3183393f8daSKenneth D. Merry #else /* !_KERNEL */ 3193393f8daSKenneth D. Merry char * 3203393f8daSKenneth D. Merry cam_error_string(struct cam_device *device, union ccb *ccb, char *str, 3213393f8daSKenneth D. Merry int str_len, cam_error_string_flags flags, 3223393f8daSKenneth D. Merry cam_error_proto_flags proto_flags) 3233393f8daSKenneth D. Merry #endif /* _KERNEL/!_KERNEL */ 3243393f8daSKenneth D. Merry { 3253393f8daSKenneth D. Merry char path_str[64]; 3263393f8daSKenneth D. Merry struct sbuf sb; 3273393f8daSKenneth D. Merry 3283393f8daSKenneth D. Merry if ((ccb == NULL) 3293393f8daSKenneth D. Merry || (str == NULL) 3303393f8daSKenneth D. Merry || (str_len <= 0)) 3313393f8daSKenneth D. Merry return(NULL); 3323393f8daSKenneth D. Merry 3333393f8daSKenneth D. Merry if (flags == CAM_ESF_NONE) 3343393f8daSKenneth D. Merry return(NULL); 3353393f8daSKenneth D. Merry 3363393f8daSKenneth D. Merry switch (ccb->ccb_h.func_code) { 3378691755dSAlexander Motin case XPT_ATA_IO: 3388691755dSAlexander Motin switch (proto_flags & CAM_EPF_LEVEL_MASK) { 3398691755dSAlexander Motin case CAM_EPF_NONE: 3408691755dSAlexander Motin break; 3418691755dSAlexander Motin case CAM_EPF_ALL: 3428691755dSAlexander Motin case CAM_EPF_NORMAL: 3438691755dSAlexander Motin proto_flags |= CAM_EAF_PRINT_RESULT; 3448691755dSAlexander Motin /* FALLTHROUGH */ 3458691755dSAlexander Motin case CAM_EPF_MINIMAL: 3468691755dSAlexander Motin proto_flags |= CAM_EAF_PRINT_STATUS; 3478691755dSAlexander Motin /* FALLTHROUGH */ 3488691755dSAlexander Motin default: 3498691755dSAlexander Motin break; 3508691755dSAlexander Motin } 3518691755dSAlexander Motin break; 3523393f8daSKenneth D. Merry case XPT_SCSI_IO: 3533393f8daSKenneth D. Merry switch (proto_flags & CAM_EPF_LEVEL_MASK) { 3543393f8daSKenneth D. Merry case CAM_EPF_NONE: 3553393f8daSKenneth D. Merry break; 3563393f8daSKenneth D. Merry case CAM_EPF_ALL: 3573393f8daSKenneth D. Merry case CAM_EPF_NORMAL: 3583393f8daSKenneth D. Merry proto_flags |= CAM_ESF_PRINT_SENSE; 3593393f8daSKenneth D. Merry /* FALLTHROUGH */ 3603393f8daSKenneth D. Merry case CAM_EPF_MINIMAL: 3613393f8daSKenneth D. Merry proto_flags |= CAM_ESF_PRINT_STATUS; 36207c6eac9SPoul-Henning Kamp /* FALLTHROUGH */ 3633393f8daSKenneth D. Merry default: 3643393f8daSKenneth D. Merry break; 3653393f8daSKenneth D. Merry } 3663393f8daSKenneth D. Merry break; 36706e79492SKenneth D. Merry case XPT_SMP_IO: 36806e79492SKenneth D. Merry switch (proto_flags & CAM_EPF_LEVEL_MASK) { 36906e79492SKenneth D. Merry case CAM_EPF_NONE: 37006e79492SKenneth D. Merry break; 37106e79492SKenneth D. Merry case CAM_EPF_ALL: 37206e79492SKenneth D. Merry proto_flags |= CAM_ESMF_PRINT_FULL_CMD; 37306e79492SKenneth D. Merry /* FALLTHROUGH */ 37406e79492SKenneth D. Merry case CAM_EPF_NORMAL: 37506e79492SKenneth D. Merry case CAM_EPF_MINIMAL: 37606e79492SKenneth D. Merry proto_flags |= CAM_ESMF_PRINT_STATUS; 37706e79492SKenneth D. Merry /* FALLTHROUGH */ 37806e79492SKenneth D. Merry default: 37906e79492SKenneth D. Merry break; 38006e79492SKenneth D. Merry } 38106e79492SKenneth D. Merry break; 3823393f8daSKenneth D. Merry default: 3833393f8daSKenneth D. Merry break; 3843393f8daSKenneth D. Merry } 3853393f8daSKenneth D. Merry #ifdef _KERNEL 3863393f8daSKenneth D. Merry xpt_path_string(ccb->csio.ccb_h.path, path_str, sizeof(path_str)); 3873393f8daSKenneth D. Merry #else /* !_KERNEL */ 3883393f8daSKenneth D. Merry cam_path_string(device, path_str, sizeof(path_str)); 3893393f8daSKenneth D. Merry #endif /* _KERNEL/!_KERNEL */ 3903393f8daSKenneth D. Merry 3913393f8daSKenneth D. Merry sbuf_new(&sb, str, str_len, 0); 3923393f8daSKenneth D. Merry 3933393f8daSKenneth D. Merry if (flags & CAM_ESF_COMMAND) { 3943393f8daSKenneth D. Merry sbuf_cat(&sb, path_str); 3953393f8daSKenneth D. Merry switch (ccb->ccb_h.func_code) { 3968691755dSAlexander Motin case XPT_ATA_IO: 3978691755dSAlexander Motin ata_command_sbuf(&ccb->ataio, &sb); 3988691755dSAlexander Motin break; 3993393f8daSKenneth D. Merry case XPT_SCSI_IO: 4003393f8daSKenneth D. Merry #ifdef _KERNEL 4013393f8daSKenneth D. Merry scsi_command_string(&ccb->csio, &sb); 4023393f8daSKenneth D. Merry #else /* !_KERNEL */ 4033393f8daSKenneth D. Merry scsi_command_string(device, &ccb->csio, &sb); 4043393f8daSKenneth D. Merry #endif /* _KERNEL/!_KERNEL */ 4053393f8daSKenneth D. Merry break; 40606e79492SKenneth D. Merry case XPT_SMP_IO: 40706e79492SKenneth D. Merry smp_command_sbuf(&ccb->smpio, &sb, path_str, 79 - 40806e79492SKenneth D. Merry strlen(path_str), (proto_flags & 40906e79492SKenneth D. Merry CAM_ESMF_PRINT_FULL_CMD) ? 79 : 0); 410e40d8dbbSAlexander Motin break; 411e40d8dbbSAlexander Motin case XPT_NVME_IO: 412e40d8dbbSAlexander Motin case XPT_NVME_ADMIN: 413e40d8dbbSAlexander Motin nvme_command_sbuf(&ccb->nvmeio, &sb); 41406e79492SKenneth D. Merry break; 4153393f8daSKenneth D. Merry default: 416e40d8dbbSAlexander Motin sbuf_printf(&sb, "CAM func %#x", 417e40d8dbbSAlexander Motin ccb->ccb_h.func_code); 4183393f8daSKenneth D. Merry break; 4193393f8daSKenneth D. Merry } 420e40d8dbbSAlexander Motin sbuf_printf(&sb, "\n"); 4213393f8daSKenneth D. Merry } 4223393f8daSKenneth D. Merry 4233393f8daSKenneth D. Merry if (flags & CAM_ESF_CAM_STATUS) { 4243393f8daSKenneth D. Merry cam_status status; 4253393f8daSKenneth D. Merry const struct cam_status_entry *entry; 4263393f8daSKenneth D. Merry 4273393f8daSKenneth D. Merry sbuf_cat(&sb, path_str); 4283393f8daSKenneth D. Merry 4293393f8daSKenneth D. Merry status = ccb->ccb_h.status & CAM_STATUS_MASK; 4303393f8daSKenneth D. Merry 4313393f8daSKenneth D. Merry entry = cam_fetch_status_entry(status); 4323393f8daSKenneth D. Merry 4333393f8daSKenneth D. Merry if (entry == NULL) 43483c5d981SAlexander Motin sbuf_printf(&sb, "CAM status: Unknown (%#x)\n", 4353393f8daSKenneth D. Merry ccb->ccb_h.status); 4363393f8daSKenneth D. Merry else 43783c5d981SAlexander Motin sbuf_printf(&sb, "CAM status: %s\n", 4383393f8daSKenneth D. Merry entry->status_text); 4393393f8daSKenneth D. Merry } 4403393f8daSKenneth D. Merry 4413393f8daSKenneth D. Merry if (flags & CAM_ESF_PROTO_STATUS) { 4423393f8daSKenneth D. Merry 4433393f8daSKenneth D. Merry switch (ccb->ccb_h.func_code) { 4448691755dSAlexander Motin case XPT_ATA_IO: 4458691755dSAlexander Motin if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 4468691755dSAlexander Motin CAM_ATA_STATUS_ERROR) 4478691755dSAlexander Motin break; 4488691755dSAlexander Motin if (proto_flags & CAM_EAF_PRINT_STATUS) { 4498691755dSAlexander Motin sbuf_cat(&sb, path_str); 4508691755dSAlexander Motin ata_status_sbuf(&ccb->ataio, &sb); 4518691755dSAlexander Motin sbuf_printf(&sb, "\n"); 4528691755dSAlexander Motin } 4538691755dSAlexander Motin if (proto_flags & CAM_EAF_PRINT_RESULT) { 4548691755dSAlexander Motin sbuf_cat(&sb, path_str); 455c9767ca8SScott Long sbuf_printf(&sb, "RES: "); 456c9767ca8SScott Long ata_res_sbuf(&ccb->ataio.res, &sb); 4578691755dSAlexander Motin sbuf_printf(&sb, "\n"); 4588691755dSAlexander Motin } 4598691755dSAlexander Motin 4608691755dSAlexander Motin break; 4613393f8daSKenneth D. Merry case XPT_SCSI_IO: 4623393f8daSKenneth D. Merry if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 4633393f8daSKenneth D. Merry CAM_SCSI_STATUS_ERROR) 4643393f8daSKenneth D. Merry break; 4653393f8daSKenneth D. Merry 4663393f8daSKenneth D. Merry if (proto_flags & CAM_ESF_PRINT_STATUS) { 4673393f8daSKenneth D. Merry sbuf_cat(&sb, path_str); 46883c5d981SAlexander Motin sbuf_printf(&sb, "SCSI status: %s\n", 4693393f8daSKenneth D. Merry scsi_status_string(&ccb->csio)); 4703393f8daSKenneth D. Merry } 4713393f8daSKenneth D. Merry 4723393f8daSKenneth D. Merry if ((proto_flags & CAM_ESF_PRINT_SENSE) 4733393f8daSKenneth D. Merry && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND) 4743393f8daSKenneth D. Merry && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)) { 4753393f8daSKenneth D. Merry #ifdef _KERNEL 4763393f8daSKenneth D. Merry scsi_sense_sbuf(&ccb->csio, &sb, 4773393f8daSKenneth D. Merry SSS_FLAG_NONE); 4783393f8daSKenneth D. Merry #else /* !_KERNEL */ 4793393f8daSKenneth D. Merry scsi_sense_sbuf(device, &ccb->csio, &sb, 4803393f8daSKenneth D. Merry SSS_FLAG_NONE); 4813393f8daSKenneth D. Merry #endif /* _KERNEL/!_KERNEL */ 4823393f8daSKenneth D. Merry } 4833393f8daSKenneth D. Merry break; 48406e79492SKenneth D. Merry case XPT_SMP_IO: 48506e79492SKenneth D. Merry if ((ccb->ccb_h.status & CAM_STATUS_MASK) != 48606e79492SKenneth D. Merry CAM_SMP_STATUS_ERROR) 48706e79492SKenneth D. Merry break; 48806e79492SKenneth D. Merry 48906e79492SKenneth D. Merry if (proto_flags & CAM_ESF_PRINT_STATUS) { 49006e79492SKenneth D. Merry sbuf_cat(&sb, path_str); 49106e79492SKenneth D. Merry sbuf_printf(&sb, "SMP status: %s (%#x)\n", 49206e79492SKenneth D. Merry smp_error_desc(ccb->smpio.smp_response[2]), 49306e79492SKenneth D. Merry ccb->smpio.smp_response[2]); 49406e79492SKenneth D. Merry } 49506e79492SKenneth D. Merry /* There is no SMP equivalent to SCSI sense. */ 49606e79492SKenneth D. Merry break; 4973393f8daSKenneth D. Merry default: 4983393f8daSKenneth D. Merry break; 4993393f8daSKenneth D. Merry } 5003393f8daSKenneth D. Merry } 5013393f8daSKenneth D. Merry 5023393f8daSKenneth D. Merry sbuf_finish(&sb); 5033393f8daSKenneth D. Merry 5043393f8daSKenneth D. Merry return(sbuf_data(&sb)); 5053393f8daSKenneth D. Merry } 5063393f8daSKenneth D. Merry 5073393f8daSKenneth D. Merry #ifdef _KERNEL 5083393f8daSKenneth D. Merry 5093393f8daSKenneth D. Merry void 5103393f8daSKenneth D. Merry cam_error_print(union ccb *ccb, cam_error_string_flags flags, 5113393f8daSKenneth D. Merry cam_error_proto_flags proto_flags) 5123393f8daSKenneth D. Merry { 5133393f8daSKenneth D. Merry char str[512]; 5143393f8daSKenneth D. Merry 5153393f8daSKenneth D. Merry printf("%s", cam_error_string(ccb, str, sizeof(str), flags, 5163393f8daSKenneth D. Merry proto_flags)); 5173393f8daSKenneth D. Merry } 5183393f8daSKenneth D. Merry 5193393f8daSKenneth D. Merry #else /* !_KERNEL */ 5203393f8daSKenneth D. Merry 5213393f8daSKenneth D. Merry void 5223393f8daSKenneth D. Merry cam_error_print(struct cam_device *device, union ccb *ccb, 5233393f8daSKenneth D. Merry cam_error_string_flags flags, cam_error_proto_flags proto_flags, 5243393f8daSKenneth D. Merry FILE *ofile) 5253393f8daSKenneth D. Merry { 5263393f8daSKenneth D. Merry char str[512]; 5273393f8daSKenneth D. Merry 5283393f8daSKenneth D. Merry if ((device == NULL) || (ccb == NULL) || (ofile == NULL)) 5293393f8daSKenneth D. Merry return; 5303393f8daSKenneth D. Merry 5313393f8daSKenneth D. Merry fprintf(ofile, "%s", cam_error_string(device, ccb, str, sizeof(str), 5323393f8daSKenneth D. Merry flags, proto_flags)); 5333393f8daSKenneth D. Merry } 5343393f8daSKenneth D. Merry 5353393f8daSKenneth D. Merry #endif /* _KERNEL/!_KERNEL */ 536141bdcc1SNate Lawson 537141bdcc1SNate Lawson /* 538141bdcc1SNate Lawson * Common calculate geometry fuction 539141bdcc1SNate Lawson * 540141bdcc1SNate Lawson * Caller should set ccg->volume_size and block_size. 541141bdcc1SNate Lawson * The extended parameter should be zero if extended translation 542141bdcc1SNate Lawson * should not be used. 543141bdcc1SNate Lawson */ 544141bdcc1SNate Lawson void 545141bdcc1SNate Lawson cam_calc_geometry(struct ccb_calc_geometry *ccg, int extended) 546141bdcc1SNate Lawson { 547141bdcc1SNate Lawson uint32_t size_mb, secs_per_cylinder; 548141bdcc1SNate Lawson 549bae3cbf0SMatt Jacob if (ccg->block_size == 0) { 550bae3cbf0SMatt Jacob ccg->ccb_h.status = CAM_REQ_CMP_ERR; 551bae3cbf0SMatt Jacob return; 552bae3cbf0SMatt Jacob } 553bae3cbf0SMatt Jacob size_mb = (1024L * 1024L) / ccg->block_size; 554bae3cbf0SMatt Jacob if (size_mb == 0) { 555bae3cbf0SMatt Jacob ccg->ccb_h.status = CAM_REQ_CMP_ERR; 556bae3cbf0SMatt Jacob return; 557bae3cbf0SMatt Jacob } 558bae3cbf0SMatt Jacob size_mb = ccg->volume_size / size_mb; 559141bdcc1SNate Lawson if (size_mb > 1024 && extended) { 560141bdcc1SNate Lawson ccg->heads = 255; 561141bdcc1SNate Lawson ccg->secs_per_track = 63; 562141bdcc1SNate Lawson } else { 563141bdcc1SNate Lawson ccg->heads = 64; 564141bdcc1SNate Lawson ccg->secs_per_track = 32; 565141bdcc1SNate Lawson } 566141bdcc1SNate Lawson secs_per_cylinder = ccg->heads * ccg->secs_per_track; 567bae3cbf0SMatt Jacob if (secs_per_cylinder == 0) { 568bae3cbf0SMatt Jacob ccg->ccb_h.status = CAM_REQ_CMP_ERR; 569bae3cbf0SMatt Jacob return; 570bae3cbf0SMatt Jacob } 571141bdcc1SNate Lawson ccg->cylinders = ccg->volume_size / secs_per_cylinder; 572141bdcc1SNate Lawson ccg->ccb_h.status = CAM_REQ_CMP; 573141bdcc1SNate Lawson } 574