1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Implementation of Utility functions for all SCSI device types. 5 * 6 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs. 7 * Copyright (c) 1997, 1998, 2003 Kenneth D. Merry. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification, immediately at the beginning of the file. 16 * 2. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_scsi_all.c#2 $ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/types.h> 36 #ifdef _KERNEL 37 #include <sys/systm.h> 38 #include <sys/libkern.h> 39 #include <sys/kernel.h> 40 #include <sys/sysctl.h> 41 #else 42 #include <errno.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <inttypes.h> 47 #endif 48 49 #include <cam/cam.h> 50 #include <cam/cam_ccb.h> 51 #include <cam/cam_queue.h> 52 #include <cam/cam_xpt.h> 53 #include <cam/scsi/scsi_all.h> 54 55 #include <cam/ctl/ctl_io.h> 56 #include <cam/ctl/ctl_scsi_all.h> 57 #include <sys/sbuf.h> 58 #ifndef _KERNEL 59 #include <camlib.h> 60 #endif 61 62 const char * 63 ctl_scsi_status_string(struct ctl_scsiio *ctsio) 64 { 65 switch(ctsio->scsi_status) { 66 case SCSI_STATUS_OK: 67 return("OK"); 68 case SCSI_STATUS_CHECK_COND: 69 return("Check Condition"); 70 case SCSI_STATUS_BUSY: 71 return("Busy"); 72 case SCSI_STATUS_INTERMED: 73 return("Intermediate"); 74 case SCSI_STATUS_INTERMED_COND_MET: 75 return("Intermediate-Condition Met"); 76 case SCSI_STATUS_RESERV_CONFLICT: 77 return("Reservation Conflict"); 78 case SCSI_STATUS_CMD_TERMINATED: 79 return("Command Terminated"); 80 case SCSI_STATUS_QUEUE_FULL: 81 return("Queue Full"); 82 case SCSI_STATUS_ACA_ACTIVE: 83 return("ACA Active"); 84 case SCSI_STATUS_TASK_ABORTED: 85 return("Task Aborted"); 86 default: { 87 static char unkstr[64]; 88 snprintf(unkstr, sizeof(unkstr), "Unknown %#x", 89 ctsio->scsi_status); 90 return(unkstr); 91 } 92 } 93 } 94 95 /* 96 * scsi_command_string() returns 0 for success and -1 for failure. 97 */ 98 int 99 ctl_scsi_command_string(struct ctl_scsiio *ctsio, 100 struct scsi_inquiry_data *inq_data, struct sbuf *sb) 101 { 102 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1]; 103 104 sbuf_printf(sb, "%s. CDB: %s", 105 scsi_op_desc(ctsio->cdb[0], inq_data), 106 scsi_cdb_string(ctsio->cdb, cdb_str, sizeof(cdb_str))); 107 108 return(0); 109 } 110 111 void 112 ctl_scsi_path_string(union ctl_io *io, char *path_str, int len) 113 { 114 115 snprintf(path_str, len, "(%u:%u:%u/%u): ", 116 io->io_hdr.nexus.initid, io->io_hdr.nexus.targ_port, 117 io->io_hdr.nexus.targ_lun, io->io_hdr.nexus.targ_mapped_lun); 118 } 119 120 /* 121 * ctl_scsi_sense_sbuf() returns 0 for success and -1 for failure. 122 */ 123 int 124 ctl_scsi_sense_sbuf(struct ctl_scsiio *ctsio, 125 struct scsi_inquiry_data *inq_data, struct sbuf *sb, 126 scsi_sense_string_flags flags) 127 { 128 char path_str[64]; 129 130 if ((ctsio == NULL) || (sb == NULL)) 131 return(-1); 132 133 ctl_scsi_path_string((union ctl_io *)ctsio, path_str, sizeof(path_str)); 134 135 if (flags & SSS_FLAG_PRINT_COMMAND) { 136 sbuf_cat(sb, path_str); 137 138 ctl_scsi_command_string(ctsio, inq_data, sb); 139 140 sbuf_putc(sb, '\n'); 141 } 142 143 scsi_sense_only_sbuf(&ctsio->sense_data, ctsio->sense_len, sb, 144 path_str, inq_data, ctsio->cdb, ctsio->cdb_len); 145 146 return(0); 147 } 148 149 char * 150 ctl_scsi_sense_string(struct ctl_scsiio *ctsio, 151 struct scsi_inquiry_data *inq_data, char *str, 152 int str_len) 153 { 154 struct sbuf sb; 155 156 sbuf_new(&sb, str, str_len, 0); 157 158 ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND); 159 160 sbuf_finish(&sb); 161 162 return(sbuf_data(&sb)); 163 } 164 165 #ifdef _KERNEL 166 void 167 ctl_scsi_sense_print(struct ctl_scsiio *ctsio, 168 struct scsi_inquiry_data *inq_data) 169 { 170 struct sbuf sb; 171 char str[512]; 172 173 sbuf_new(&sb, str, sizeof(str), 0); 174 175 ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND); 176 177 sbuf_finish(&sb); 178 179 printf("%s", sbuf_data(&sb)); 180 } 181 182 #else /* _KERNEL */ 183 void 184 ctl_scsi_sense_print(struct ctl_scsiio *ctsio, 185 struct scsi_inquiry_data *inq_data, FILE *ofile) 186 { 187 struct sbuf sb; 188 char str[512]; 189 190 if ((ctsio == NULL) || (ofile == NULL)) 191 return; 192 193 sbuf_new(&sb, str, sizeof(str), 0); 194 195 ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND); 196 197 sbuf_finish(&sb); 198 199 fprintf(ofile, "%s", sbuf_data(&sb)); 200 } 201 202 #endif /* _KERNEL */ 203