FreeBSD kernel CAM code
ctl_scsi_all.c
Go to the documentation of this file.
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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
36__FBSDID("$FreeBSD$");
37
38#include <sys/types.h>
39#ifdef _KERNEL
40#include <sys/systm.h>
41#include <sys/libkern.h>
42#include <sys/kernel.h>
43#include <sys/sysctl.h>
44#else
45#include <errno.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
49#include <inttypes.h>
50#endif
51
52#include <cam/cam.h>
53#include <cam/cam_ccb.h>
54#include <cam/cam_queue.h>
55#include <cam/cam_xpt.h>
56#include <cam/scsi/scsi_all.h>
57
58#include <cam/ctl/ctl_io.h>
60#include <sys/sbuf.h>
61#ifndef _KERNEL
62#include <camlib.h>
63#endif
64
65const char *
67{
68 switch(ctsio->scsi_status) {
69 case SCSI_STATUS_OK:
70 return("OK");
72 return("Check Condition");
74 return("Busy");
76 return("Intermediate");
78 return("Intermediate-Condition Met");
80 return("Reservation Conflict");
82 return("Command Terminated");
84 return("Queue Full");
86 return("ACA Active");
88 return("Task Aborted");
89 default: {
90 static char unkstr[64];
91 snprintf(unkstr, sizeof(unkstr), "Unknown %#x",
92 ctsio->scsi_status);
93 return(unkstr);
94 }
95 }
96}
97
98/*
99 * scsi_command_string() returns 0 for success and -1 for failure.
100 */
101int
103 struct scsi_inquiry_data *inq_data, struct sbuf *sb)
104{
105 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
106
107 sbuf_printf(sb, "%s. CDB: %s",
108 scsi_op_desc(ctsio->cdb[0], inq_data),
109 scsi_cdb_string(ctsio->cdb, cdb_str, sizeof(cdb_str)));
110
111 return(0);
112}
113
114void
115ctl_scsi_path_string(union ctl_io *io, char *path_str, int len)
116{
117
118 snprintf(path_str, len, "(%u:%u:%u/%u): ",
121}
122
123/*
124 * ctl_scsi_sense_sbuf() returns 0 for success and -1 for failure.
125 */
126int
128 struct scsi_inquiry_data *inq_data, struct sbuf *sb,
130{
131 char path_str[64];
132
133 if ((ctsio == NULL) || (sb == NULL))
134 return(-1);
135
136 ctl_scsi_path_string((union ctl_io *)ctsio, path_str, sizeof(path_str));
137
138 if (flags & SSS_FLAG_PRINT_COMMAND) {
139 sbuf_cat(sb, path_str);
140
141 ctl_scsi_command_string(ctsio, inq_data, sb);
142
143 sbuf_printf(sb, "\n");
144 }
145
146 scsi_sense_only_sbuf(&ctsio->sense_data, ctsio->sense_len, sb,
147 path_str, inq_data, ctsio->cdb, ctsio->cdb_len);
148
149 return(0);
150}
151
152char *
154 struct scsi_inquiry_data *inq_data, char *str,
155 int str_len)
156{
157 struct sbuf sb;
158
159 sbuf_new(&sb, str, str_len, 0);
160
161 ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND);
162
163 sbuf_finish(&sb);
164
165 return(sbuf_data(&sb));
166}
167
168#ifdef _KERNEL
169void
171 struct scsi_inquiry_data *inq_data)
172{
173 struct sbuf sb;
174 char str[512];
175
176 sbuf_new(&sb, str, sizeof(str), 0);
177
178 ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND);
179
180 sbuf_finish(&sb);
181
182 printf("%s", sbuf_data(&sb));
183}
184
185#else /* _KERNEL */
186void
187ctl_scsi_sense_print(struct ctl_scsiio *ctsio,
188 struct scsi_inquiry_data *inq_data, FILE *ofile)
189{
190 struct sbuf sb;
191 char str[512];
192
193 if ((ctsio == NULL) || (ofile == NULL))
194 return;
195
196 sbuf_new(&sb, str, sizeof(str), 0);
197
198 ctl_scsi_sense_sbuf(ctsio, inq_data, &sb, SSS_FLAG_PRINT_COMMAND);
199
200 sbuf_finish(&sb);
201
202 fprintf(ofile, "%s", sbuf_data(&sb));
203}
204
205#endif /* _KERNEL */
int ctl_scsi_command_string(struct ctl_scsiio *ctsio, struct scsi_inquiry_data *inq_data, struct sbuf *sb)
Definition: ctl_scsi_all.c:102
const char * ctl_scsi_status_string(struct ctl_scsiio *ctsio)
Definition: ctl_scsi_all.c:66
__FBSDID("$FreeBSD$")
void ctl_scsi_sense_print(struct ctl_scsiio *ctsio, struct scsi_inquiry_data *inq_data)
Definition: ctl_scsi_all.c:170
void ctl_scsi_path_string(union ctl_io *io, char *path_str, int len)
Definition: ctl_scsi_all.c:115
int ctl_scsi_sense_sbuf(struct ctl_scsiio *ctsio, struct scsi_inquiry_data *inq_data, struct sbuf *sb, scsi_sense_string_flags flags)
Definition: ctl_scsi_all.c:127
char * ctl_scsi_sense_string(struct ctl_scsiio *ctsio, struct scsi_inquiry_data *inq_data, char *str, int str_len)
Definition: ctl_scsi_all.c:153
char * scsi_cdb_string(u_int8_t *cdb_ptr, char *cdb_string, size_t len)
Definition: scsi_all.c:3488
const char * scsi_op_desc(u_int16_t opcode, struct scsi_inquiry_data *inq_data)
Definition: scsi_all.c:628
void scsi_sense_only_sbuf(struct scsi_sense_data *sense, u_int sense_len, struct sbuf *sb, char *path_str, struct scsi_inquiry_data *inq_data, uint8_t *cdb, int cdb_len)
Definition: scsi_all.c:4858
#define SCSI_STATUS_BUSY
Definition: scsi_all.h:3694
#define SCSI_STATUS_CMD_TERMINATED
Definition: scsi_all.h:3698
#define SCSI_STATUS_CHECK_COND
Definition: scsi_all.h:3692
#define SCSI_STATUS_INTERMED
Definition: scsi_all.h:3695
#define SCSI_STATUS_OK
Definition: scsi_all.h:3691
scsi_sense_string_flags
Definition: scsi_all.h:3754
@ SSS_FLAG_PRINT_COMMAND
Definition: scsi_all.h:3756
#define SCSI_STATUS_TASK_ABORTED
Definition: scsi_all.h:3701
#define SCSI_STATUS_RESERV_CONFLICT
Definition: scsi_all.h:3697
#define SCSI_STATUS_ACA_ACTIVE
Definition: scsi_all.h:3700
#define SCSI_MAX_CDBLEN
Definition: scsi_all.h:55
#define SCSI_STATUS_INTERMED_COND_MET
Definition: scsi_all.h:3696
#define SCSI_STATUS_QUEUE_FULL
Definition: scsi_all.h:3699
struct ctl_nexus nexus
Definition: ctl_io.h:226
uint32_t targ_mapped_lun
Definition: ctl_io.h:199
uint32_t targ_lun
Definition: ctl_io.h:198
uint32_t targ_port
Definition: ctl_io.h:197
uint32_t initid
Definition: ctl_io.h:196
struct scsi_sense_data sense_data
Definition: ctl_io.h:326
uint8_t scsi_status
Definition: ctl_io.h:328
uint8_t cdb[CTL_MAX_CDBLEN]
Definition: ctl_io.h:335
uint8_t cdb_len
Definition: ctl_io.h:334
uint8_t sense_len
Definition: ctl_io.h:327
Definition: ctl_io.h:586
struct ctl_io_hdr io_hdr
Definition: ctl_io.h:587