xref: /freebsd/sys/cam/cam.h (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1 /*-
2  * Data structures and definitions for the CAM system.
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
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  * $FreeBSD$
31  */
32 
33 #ifndef _CAM_CAM_H
34 #define _CAM_CAM_H 1
35 
36 #ifdef _KERNEL
37 #include "opt_cam.h"
38 #endif
39 
40 #include <sys/cdefs.h>
41 #ifndef _KERNEL
42 #include <stdbool.h>
43 #endif
44 
45 typedef u_int path_id_t;
46 typedef u_int target_id_t;
47 typedef uint64_t lun_id_t;
48 
49 #define	CAM_XPT_PATH_ID	((path_id_t)~0)
50 #define	CAM_BUS_WILDCARD ((path_id_t)~0)
51 #define	CAM_TARGET_WILDCARD ((target_id_t)~0)
52 #define	CAM_LUN_WILDCARD (~(u_int)0)
53 
54 #define CAM_EXTLUN_BYTE_SWIZZLE(lun) (	\
55 	((((uint64_t)lun) & 0xffff000000000000L) >> 48) | \
56 	((((uint64_t)lun) & 0x0000ffff00000000L) >> 16) | \
57 	((((uint64_t)lun) & 0x00000000ffff0000L) << 16) | \
58 	((((uint64_t)lun) & 0x000000000000ffffL) << 48))
59 
60 /*
61  * Maximum length for a CAM CDB.
62  */
63 #define CAM_MAX_CDBLEN 16
64 
65 /*
66  * Definition of a CAM peripheral driver entry.  Peripheral drivers instantiate
67  * one of these for each device they wish to communicate with and pass it into
68  * the xpt layer when they wish to schedule work on that device via the
69  * xpt_schedule API.
70  */
71 struct cam_periph;
72 
73 /*
74  * Priority information for a CAM structure.
75  */
76 typedef enum {
77     CAM_RL_HOST,
78     CAM_RL_BUS,
79     CAM_RL_XPT,
80     CAM_RL_DEV,
81     CAM_RL_NORMAL,
82     CAM_RL_VALUES
83 } cam_rl;
84 /*
85  * The generation number is incremented every time a new entry is entered into
86  * the queue giving round robin per priority level scheduling.
87  */
88 typedef struct {
89 	uint32_t priority;
90 #define CAM_PRIORITY_HOST	((CAM_RL_HOST << 8) + 0x80)
91 #define CAM_PRIORITY_BUS	((CAM_RL_BUS << 8) + 0x80)
92 #define CAM_PRIORITY_XPT	((CAM_RL_XPT << 8) + 0x80)
93 #define CAM_PRIORITY_DEV	((CAM_RL_DEV << 8) + 0x80)
94 #define CAM_PRIORITY_OOB	(CAM_RL_DEV << 8)
95 #define CAM_PRIORITY_NORMAL	((CAM_RL_NORMAL << 8) + 0x80)
96 #define CAM_PRIORITY_NONE	(uint32_t)-1
97 	uint32_t generation;
98 	int       index;
99 #define CAM_UNQUEUED_INDEX	-1
100 #define CAM_ACTIVE_INDEX	-2
101 #define CAM_DONEQ_INDEX		-3
102 #define CAM_ASYNC_INDEX		-4
103 #define CAM_EXTRAQ_INDEX	INT_MAX
104 } cam_pinfo;
105 
106 /*
107  * Macro to compare two generation numbers.  It is used like this:
108  *
109  *	if (GENERATIONCMP(a, >=, b))
110  *		...;
111  *
112  * GERERATIONCMP uses modular arithmetic to guard against wraps
113  * wraps in the generation number.
114  */
115 #define GENERATIONCMP(x, op, y) ((int32_t)((x) - (y)) op 0)
116 
117 /* CAM flags XXX Move to cam_periph.h ??? */
118 typedef enum {
119 	CAM_FLAG_NONE		= 0x00,
120 	CAM_EXPECT_INQ_CHANGE	= 0x01,
121 	CAM_RETRY_SELTO		= 0x02 /* Retry Selection Timeouts */
122 } cam_flags;
123 
124 enum {
125 	SF_RETRY_UA		= 0x01,	/* Retry UNIT ATTENTION conditions. */
126 	SF_NO_PRINT		= 0x02,	/* Never print error status. */
127 	SF_QUIET_IR		= 0x04,	/* Be quiet about Illegal Request responses */
128 	SF_PRINT_ALWAYS		= 0x08,	/* Always print error status. */
129 	SF_NO_RECOVERY		= 0x10,	/* Don't do active error recovery. */
130 	SF_NO_RETRY		= 0x20,	/* Don't do any retries. */
131 	SF_RETRY_BUSY		= 0x40	/* Retry BUSY status. */
132 };
133 
134 /* CAM  Status field values */
135 typedef enum {
136 	/* CCB request is in progress */
137 	CAM_REQ_INPROG		= 0x00,
138 
139 	/* CCB request completed without error */
140 	CAM_REQ_CMP		= 0x01,
141 
142 	/* CCB request aborted by the host */
143 	CAM_REQ_ABORTED		= 0x02,
144 
145 	/* Unable to abort CCB request */
146 	CAM_UA_ABORT		= 0x03,
147 
148 	/* CCB request completed with an error */
149 	CAM_REQ_CMP_ERR		= 0x04,
150 
151 	/* CAM subsystem is busy */
152 	CAM_BUSY		= 0x05,
153 
154 	/* CCB request was invalid */
155 	CAM_REQ_INVALID		= 0x06,
156 
157 	/* Supplied Path ID is invalid */
158 	CAM_PATH_INVALID	= 0x07,
159 
160 	/* SCSI Device Not Installed/there */
161 	CAM_DEV_NOT_THERE	= 0x08,
162 
163 	/* Unable to terminate I/O CCB request */
164 	CAM_UA_TERMIO		= 0x09,
165 
166 	/* Target Selection Timeout */
167 	CAM_SEL_TIMEOUT		= 0x0a,
168 
169 	/* Command timeout */
170 	CAM_CMD_TIMEOUT		= 0x0b,
171 
172 	/* SCSI error, look at error code in CCB */
173 	CAM_SCSI_STATUS_ERROR	= 0x0c,
174 
175 	/* Message Reject Received */
176 	CAM_MSG_REJECT_REC	= 0x0d,
177 
178 	/* SCSI Bus Reset Sent/Received */
179 	CAM_SCSI_BUS_RESET	= 0x0e,
180 
181 	/* Uncorrectable parity error occurred */
182 	CAM_UNCOR_PARITY	= 0x0f,
183 
184 	/* Autosense: request sense cmd fail */
185 	CAM_AUTOSENSE_FAIL	= 0x10,
186 
187 	/* No HBA Detected error */
188 	CAM_NO_HBA		= 0x11,
189 
190 	/* Data Overrun error */
191 	CAM_DATA_RUN_ERR	= 0x12,
192 
193 	/* Unexpected Bus Free */
194 	CAM_UNEXP_BUSFREE	= 0x13,
195 
196 	/* Target Bus Phase Sequence Failure */
197 	CAM_SEQUENCE_FAIL	= 0x14,
198 
199 	/* CCB length supplied is inadequate */
200 	CAM_CCB_LEN_ERR		= 0x15,
201 
202 	/* Unable to provide requested capability*/
203 	CAM_PROVIDE_FAIL	= 0x16,
204 
205 	/* A SCSI BDR msg was sent to target */
206 	CAM_BDR_SENT		= 0x17,
207 
208 	/* CCB request terminated by the host */
209 	CAM_REQ_TERMIO		= 0x18,
210 
211 	/* Unrecoverable Host Bus Adapter Error */
212 	CAM_UNREC_HBA_ERROR	= 0x19,
213 
214 	/* Request was too large for this host */
215 	CAM_REQ_TOO_BIG		= 0x1a,
216 
217 	/*
218 	 * This request should be requeued to preserve
219 	 * transaction ordering.  This typically occurs
220 	 * when the SIM recognizes an error that should
221 	 * freeze the queue and must place additional
222 	 * requests for the target at the sim level
223 	 * back into the XPT queue.
224 	 */
225 	CAM_REQUEUE_REQ		= 0x1b,
226 
227 	/* ATA error, look at error code in CCB */
228 	CAM_ATA_STATUS_ERROR	= 0x1c,
229 
230 	/* Initiator/Target Nexus lost. */
231 	CAM_SCSI_IT_NEXUS_LOST	= 0x1d,
232 
233 	/* SMP error, look at error code in CCB */
234 	CAM_SMP_STATUS_ERROR	= 0x1e,
235 
236 	/*
237 	 * Command completed without error but  exceeded the soft
238 	 * timeout threshold.
239 	 */
240 	CAM_REQ_SOFTTIMEOUT	= 0x1f,
241 
242 	/*
243 	 * NVME error, look at errro code in CCB
244 	 */
245 	CAM_NVME_STATUS_ERROR	= 0x20,
246 
247 	/*
248 	 * 0x21 - 0x32 are unassigned
249 	 */
250 
251 	/* Initiator Detected Error */
252 	CAM_IDE			= 0x33,
253 
254 	/* Resource Unavailable */
255 	CAM_RESRC_UNAVAIL	= 0x34,
256 
257 	/* Unacknowledged Event by Host */
258 	CAM_UNACKED_EVENT	= 0x35,
259 
260 	/* Message Received in Host Target Mode */
261 	CAM_MESSAGE_RECV	= 0x36,
262 
263 	/* Invalid CDB received in Host Target Mode */
264 	CAM_INVALID_CDB		= 0x37,
265 
266 	/* Lun supplied is invalid */
267 	CAM_LUN_INVALID		= 0x38,
268 
269 	/* Target ID supplied is invalid */
270 	CAM_TID_INVALID		= 0x39,
271 
272 	/* The requested function is not available */
273 	CAM_FUNC_NOTAVAIL	= 0x3a,
274 
275 	/* Nexus is not established */
276 	CAM_NO_NEXUS		= 0x3b,
277 
278 	/* The initiator ID is invalid */
279 	CAM_IID_INVALID		= 0x3c,
280 
281 	/* The SCSI CDB has been received */
282 	CAM_CDB_RECVD		= 0x3d,
283 
284 	/* The LUN is already enabled for target mode */
285 	CAM_LUN_ALRDY_ENA	= 0x3e,
286 
287 	/* SCSI Bus Busy */
288 	CAM_SCSI_BUSY		= 0x3f,
289 
290 	/*
291 	 * Flags
292 	 */
293 
294 	/* The DEV queue is frozen w/this err */
295 	CAM_DEV_QFRZN		= 0x40,
296 
297 	/* Autosense data valid for target */
298 	CAM_AUTOSNS_VALID	= 0x80,
299 
300 	/* SIM ready to take more commands */
301 	CAM_RELEASE_SIMQ	= 0x100,
302 
303 	/* SIM has this command in its queue */
304 	CAM_SIM_QUEUED		= 0x200,
305 
306 	/* Quality of service data is valid */
307 	CAM_QOS_VALID		= 0x400,
308 
309 	/* Mask bits for just the status # */
310 	CAM_STATUS_MASK = 0x3F,
311 
312 	/*
313 	 * Target Specific Adjunct Status
314 	 */
315 
316 	/* sent sense with status */
317 	CAM_SENT_SENSE		= 0x40000000
318 } cam_status;
319 
320 typedef enum {
321 	CAM_ESF_NONE		= 0x00,
322 	CAM_ESF_COMMAND		= 0x01,
323 	CAM_ESF_CAM_STATUS	= 0x02,
324 	CAM_ESF_PROTO_STATUS	= 0x04,
325 	CAM_ESF_ALL		= 0xff
326 } cam_error_string_flags;
327 
328 typedef enum {
329 	CAM_EPF_NONE		= 0x00,
330 	CAM_EPF_MINIMAL		= 0x01,
331 	CAM_EPF_NORMAL		= 0x02,
332 	CAM_EPF_ALL		= 0x03,
333 	CAM_EPF_LEVEL_MASK	= 0x0f
334 	/* All bits above bit 3 are protocol-specific */
335 } cam_error_proto_flags;
336 
337 typedef enum {
338 	CAM_ESF_PRINT_NONE	= 0x00,
339 	CAM_ESF_PRINT_STATUS	= 0x10,
340 	CAM_ESF_PRINT_SENSE	= 0x20
341 } cam_error_scsi_flags;
342 
343 typedef enum {
344 	CAM_ESMF_PRINT_NONE	= 0x00,
345 	CAM_ESMF_PRINT_STATUS	= 0x10,
346 	CAM_ESMF_PRINT_FULL_CMD	= 0x20,
347 } cam_error_smp_flags;
348 
349 typedef enum {
350 	CAM_EAF_PRINT_NONE	= 0x00,
351 	CAM_EAF_PRINT_STATUS	= 0x10,
352 	CAM_EAF_PRINT_RESULT	= 0x20
353 } cam_error_ata_flags;
354 
355 typedef enum {
356 	CAM_STRVIS_FLAG_NONE		= 0x00,
357 	CAM_STRVIS_FLAG_NONASCII_MASK	= 0x03,
358 	CAM_STRVIS_FLAG_NONASCII_TRIM	= 0x00,
359 	CAM_STRVIS_FLAG_NONASCII_RAW	= 0x01,
360 	CAM_STRVIS_FLAG_NONASCII_SPC	= 0x02,
361 	CAM_STRVIS_FLAG_NONASCII_ESC	= 0x03
362 } cam_strvis_flags;
363 
364 struct cam_status_entry
365 {
366 	cam_status  status_code;
367 	const char *status_text;
368 };
369 
370 extern const struct cam_status_entry cam_status_table[];
371 extern const int num_cam_status_entries;
372 #ifdef _KERNEL
373 extern int cam_sort_io_queues;
374 #endif
375 union ccb;
376 struct sbuf;
377 
378 #ifdef SYSCTL_DECL	/* from sysctl.h */
379 SYSCTL_DECL(_kern_cam);
380 #endif
381 
382 __BEGIN_DECLS
383 typedef int (cam_quirkmatch_t)(caddr_t, caddr_t);
384 
385 caddr_t	cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
386 		       int entry_size, cam_quirkmatch_t *comp_func);
387 
388 void	cam_strvis(uint8_t *dst, const uint8_t *src, int srclen, int dstlen);
389 void	cam_strvis_flag(uint8_t *dst, const uint8_t *src, int srclen,
390 			int dstlen, uint32_t flags);
391 void	cam_strvis_sbuf(struct sbuf *sb, const uint8_t *src, int srclen,
392 			uint32_t flags);
393 
394 int	cam_strmatch(const uint8_t *str, const uint8_t *pattern, int str_len);
395 const struct cam_status_entry*
396 	cam_fetch_status_entry(cam_status status);
397 #ifdef _KERNEL
398 char *	cam_error_string(union ccb *ccb, char *str, int str_len,
399 			 cam_error_string_flags flags,
400 			 cam_error_proto_flags proto_flags);
401 void	cam_error_print(union ccb *ccb, cam_error_string_flags flags,
402 			cam_error_proto_flags proto_flags);
403 #else /* _KERNEL */
404 struct cam_device;
405 
406 char *	cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
407 			 int str_len, cam_error_string_flags flags,
408 			 cam_error_proto_flags proto_flags);
409 void	cam_error_print(struct cam_device *device, union ccb *ccb,
410 			cam_error_string_flags flags,
411 			cam_error_proto_flags proto_flags, FILE *ofile);
412 #endif /* _KERNEL */
413 __END_DECLS
414 
415 #ifdef _KERNEL
416 static __inline void cam_init_pinfo(cam_pinfo *pinfo)
417 {
418 	pinfo->priority = CAM_PRIORITY_NONE;
419 	pinfo->index = CAM_UNQUEUED_INDEX;
420 }
421 #endif
422 
423 #endif /* _CAM_CAM_H */
424