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