cam.h revision 198849
1218887Sdim/*-
2218887Sdim * Data structures and definitions for the CAM system.
3218887Sdim *
4218887Sdim * Copyright (c) 1997 Justin T. Gibbs.
5218887Sdim * All rights reserved.
6218887Sdim *
7218887Sdim * Redistribution and use in source and binary forms, with or without
8218887Sdim * modification, are permitted provided that the following conditions
9218887Sdim * are met:
10218887Sdim * 1. Redistributions of source code must retain the above copyright
11218887Sdim *    notice, this list of conditions, and the following disclaimer,
12218887Sdim *    without modification, immediately at the beginning of the file.
13218887Sdim * 2. The name of the author may not be used to endorse or promote products
14218887Sdim *    derived from this software without specific prior written permission.
15218887Sdim *
16218887Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17218887Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18218887Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19245431Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20218887Sdim * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21218887Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22245431Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23218887Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24218887Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25218887Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26218887Sdim * SUCH DAMAGE.
27218887Sdim *
28218887Sdim * $FreeBSD: head/sys/cam/cam.h 198849 2009-11-03 11:19:05Z mav $
29218887Sdim */
30235633Sdim
31235633Sdim#ifndef _CAM_CAM_H
32218887Sdim#define _CAM_CAM_H 1
33218887Sdim
34226890Sdim#ifdef _KERNEL
35235633Sdim#include <opt_cam.h>
36245431Sdim#endif
37218887Sdim
38218887Sdim#include <sys/cdefs.h>
39218887Sdim
40218887Sdimtypedef u_int path_id_t;
41218887Sdimtypedef u_int target_id_t;
42218887Sdimtypedef u_int lun_id_t;
43218887Sdim
44218887Sdim#define	CAM_XPT_PATH_ID	((path_id_t)~0)
45245431Sdim#define	CAM_BUS_WILDCARD ((path_id_t)~0)
46245431Sdim#define	CAM_TARGET_WILDCARD ((target_id_t)~0)
47245431Sdim#define	CAM_LUN_WILDCARD ((lun_id_t)~0)
48245431Sdim
49245431Sdim/*
50218887Sdim * Maximum length for a CAM CDB.
51218887Sdim */
52218887Sdim#define CAM_MAX_CDBLEN 16
53245431Sdim
54218887Sdim/*
55245431Sdim * Definition of a CAM peripheral driver entry.  Peripheral drivers instantiate
56218887Sdim * one of these for each device they wish to communicate with and pass it into
57218887Sdim * the xpt layer when they wish to schedule work on that device via the
58218887Sdim * xpt_schedule API.
59218887Sdim */
60218887Sdimstruct cam_periph;
61235633Sdim
62218887Sdim/*
63218887Sdim * Priority information for a CAM structure.  The generation number is
64218887Sdim * incremented everytime a new entry is entered into the queue giving round
65218887Sdim * robin per priority level scheduling.
66218887Sdim */
67218887Sdimtypedef struct {
68263509Sdim	u_int32_t priority;
69263509Sdim#define CAM_PRIORITY_BUS	0
70263509Sdim#define CAM_PRIORITY_DEV	0
71263509Sdim#define CAM_PRIORITY_NORMAL	1
72218887Sdim#define CAM_PRIORITY_NONE	(u_int32_t)-1
73218887Sdim	u_int32_t generation;
74218887Sdim	int       index;
75218887Sdim#define CAM_UNQUEUED_INDEX	-1
76218887Sdim#define CAM_ACTIVE_INDEX	-2
77218887Sdim#define CAM_DONEQ_INDEX		-3
78218887Sdim} cam_pinfo;
79218887Sdim
80218887Sdim/*
81218887Sdim * Macro to compare two generation numbers.  It is used like this:
82218887Sdim *
83218887Sdim *	if (GENERATIONCMP(a, >=, b))
84218887Sdim *		...;
85218887Sdim *
86218887Sdim * GERERATIONCMP uses modular arithmetic to guard against wraps
87226890Sdim * wraps in the generation number.
88218887Sdim */
89218887Sdim#define GENERATIONCMP(x, op, y) ((int32_t)((x) - (y)) op 0)
90218887Sdim
91235633Sdim/* CAM flags XXX Move to cam_periph.h ??? */
92235633Sdimtypedef enum {
93218887Sdim	CAM_FLAG_NONE		= 0x00,
94218887Sdim	CAM_EXPECT_INQ_CHANGE	= 0x01,
95245431Sdim	CAM_RETRY_SELTO		= 0x02 /* Retry Selection Timeouts */
96245431Sdim} cam_flags;
97218887Sdim
98218887Sdim/* CAM  Status field values */
99245431Sdimtypedef enum {
100218887Sdim	CAM_REQ_INPROG,		/* CCB request is in progress */
101218887Sdim	CAM_REQ_CMP,		/* CCB request completed without error */
102245431Sdim	CAM_REQ_ABORTED,	/* CCB request aborted by the host */
103245431Sdim	CAM_UA_ABORT,		/* Unable to abort CCB request */
104218887Sdim	CAM_REQ_CMP_ERR,	/* CCB request completed with an error */
105218887Sdim	CAM_BUSY,		/* CAM subsystem is busy */
106245431Sdim	CAM_REQ_INVALID,	/* CCB request was invalid */
107252723Sdim	CAM_PATH_INVALID,	/* Supplied Path ID is invalid */
108245431Sdim	CAM_DEV_NOT_THERE,	/* SCSI Device Not Installed/there */
109218887Sdim	CAM_UA_TERMIO,		/* Unable to terminate I/O CCB request */
110218887Sdim	CAM_SEL_TIMEOUT,	/* Target Selection Timeout */
111218887Sdim	CAM_CMD_TIMEOUT,	/* Command timeout */
112218887Sdim	CAM_SCSI_STATUS_ERROR,	/* SCSI error, look at error code in CCB */
113218887Sdim	CAM_MSG_REJECT_REC,	/* Message Reject Received */
114226890Sdim	CAM_SCSI_BUS_RESET,	/* SCSI Bus Reset Sent/Received */
115226890Sdim	CAM_UNCOR_PARITY,	/* Uncorrectable parity error occurred */
116226890Sdim	CAM_AUTOSENSE_FAIL = 0x10,/* Autosense: request sense cmd fail */
117218887Sdim	CAM_NO_HBA,		/* No HBA Detected error */
118218887Sdim	CAM_DATA_RUN_ERR,	/* Data Overrun error */
119218887Sdim	CAM_UNEXP_BUSFREE,	/* Unexpected Bus Free */
120218887Sdim	CAM_SEQUENCE_FAIL,	/* Target Bus Phase Sequence Failure */
121218887Sdim	CAM_CCB_LEN_ERR,	/* CCB length supplied is inadequate */
122218887Sdim	CAM_PROVIDE_FAIL,	/* Unable to provide requested capability */
123235633Sdim	CAM_BDR_SENT,		/* A SCSI BDR msg was sent to target */
124218887Sdim	CAM_REQ_TERMIO,		/* CCB request terminated by the host */
125218887Sdim	CAM_UNREC_HBA_ERROR,	/* Unrecoverable Host Bus Adapter Error */
126218887Sdim	CAM_REQ_TOO_BIG,	/* The request was too large for this host */
127218887Sdim	CAM_REQUEUE_REQ,	/*
128235633Sdim				 * This request should be requeued to preserve
129218887Sdim				 * transaction ordering.  This typically occurs
130218887Sdim				 * when the SIM recognizes an error that should
131218887Sdim				 * freeze the queue and must place additional
132218887Sdim				 * requests for the target at the sim level
133				 * back into the XPT queue.
134				 */
135	CAM_ATA_STATUS_ERROR,	/* ATA error, look at error code in CCB */
136	CAM_SCSI_IT_NEXUS_LOST,	/* Initiator/Target Nexus lost. */
137	CAM_IDE = 0x33,		/* Initiator Detected Error */
138	CAM_RESRC_UNAVAIL,	/* Resource Unavailable */
139	CAM_UNACKED_EVENT,	/* Unacknowledged Event by Host */
140	CAM_MESSAGE_RECV,	/* Message Received in Host Target Mode */
141	CAM_INVALID_CDB,	/* Invalid CDB received in Host Target Mode */
142	CAM_LUN_INVALID,	/* Lun supplied is invalid */
143	CAM_TID_INVALID,	/* Target ID supplied is invalid */
144	CAM_FUNC_NOTAVAIL,	/* The requested function is not available */
145	CAM_NO_NEXUS,		/* Nexus is not established */
146	CAM_IID_INVALID,	/* The initiator ID is invalid */
147	CAM_CDB_RECVD,		/* The SCSI CDB has been received */
148	CAM_LUN_ALRDY_ENA,	/* The LUN is already enabled for target mode */
149	CAM_SCSI_BUSY,		/* SCSI Bus Busy */
150
151	CAM_DEV_QFRZN = 0x40,	/* The DEV queue is frozen w/this err */
152
153				/* Autosense data valid for target */
154	CAM_AUTOSNS_VALID = 0x80,
155	CAM_RELEASE_SIMQ = 0x100,/* SIM ready to take more commands */
156	CAM_SIM_QUEUED   = 0x200,/* SIM has this command in it's queue */
157
158	CAM_STATUS_MASK = 0x3F,	/* Mask bits for just the status # */
159
160				/* Target Specific Adjunct Status */
161	CAM_SENT_SENSE = 0x40000000	/* sent sense with status */
162} cam_status;
163
164typedef enum {
165	CAM_ESF_NONE		= 0x00,
166	CAM_ESF_COMMAND		= 0x01,
167	CAM_ESF_CAM_STATUS	= 0x02,
168	CAM_ESF_PROTO_STATUS	= 0x04,
169	CAM_ESF_ALL		= 0xff
170} cam_error_string_flags;
171
172typedef enum {
173	CAM_EPF_NONE		= 0x00,
174	CAM_EPF_MINIMAL		= 0x01,
175	CAM_EPF_NORMAL		= 0x02,
176	CAM_EPF_ALL		= 0x03,
177	CAM_EPF_LEVEL_MASK	= 0x0f
178	/* All bits above bit 3 are protocol-specific */
179} cam_error_proto_flags;
180
181typedef enum {
182	CAM_ESF_PRINT_NONE	= 0x00,
183	CAM_ESF_PRINT_STATUS	= 0x10,
184	CAM_ESF_PRINT_SENSE	= 0x20
185} cam_error_scsi_flags;
186
187typedef enum {
188	CAM_EAF_PRINT_NONE	= 0x00,
189	CAM_EAF_PRINT_STATUS	= 0x10,
190	CAM_EAF_PRINT_RESULT	= 0x20
191} cam_error_ata_flags;
192
193struct cam_status_entry
194{
195	cam_status  status_code;
196	const char *status_text;
197};
198
199extern const struct cam_status_entry cam_status_table[];
200extern const int num_cam_status_entries;
201union ccb;
202
203#ifdef SYSCTL_DECL	/* from sysctl.h */
204SYSCTL_DECL(_kern_cam);
205#endif
206
207__BEGIN_DECLS
208typedef int (cam_quirkmatch_t)(caddr_t, caddr_t);
209
210caddr_t	cam_quirkmatch(caddr_t target, caddr_t quirk_table, int num_entries,
211		       int entry_size, cam_quirkmatch_t *comp_func);
212
213void	cam_strvis(u_int8_t *dst, const u_int8_t *src, int srclen, int dstlen);
214
215int	cam_strmatch(const u_int8_t *str, const u_int8_t *pattern, int str_len);
216const struct cam_status_entry*
217	cam_fetch_status_entry(cam_status status);
218#ifdef _KERNEL
219char *	cam_error_string(union ccb *ccb, char *str, int str_len,
220			 cam_error_string_flags flags,
221			 cam_error_proto_flags proto_flags);
222void	cam_error_print(union ccb *ccb, cam_error_string_flags flags,
223			cam_error_proto_flags proto_flags);
224#else /* _KERNEL */
225struct cam_device;
226
227char *	cam_error_string(struct cam_device *device, union ccb *ccb, char *str,
228			 int str_len, cam_error_string_flags flags,
229			 cam_error_proto_flags proto_flags);
230void	cam_error_print(struct cam_device *device, union ccb *ccb,
231			cam_error_string_flags flags,
232			cam_error_proto_flags proto_flags, FILE *ofile);
233#endif /* _KERNEL */
234__END_DECLS
235
236#ifdef _KERNEL
237static __inline void cam_init_pinfo(cam_pinfo *pinfo);
238
239static __inline void cam_init_pinfo(cam_pinfo *pinfo)
240{
241	pinfo->priority = CAM_PRIORITY_NONE;
242	pinfo->index = CAM_UNQUEUED_INDEX;
243}
244#endif
245
246#endif /* _CAM_CAM_H */
247