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