xref: /netbsd-src/sys/dev/scsipi/scsipiconf.h (revision b19e9f6776dfba359eaedbfd67d0367bca2f732e)
1 /*	$NetBSD: scsipiconf.h,v 1.52 2001/05/21 15:50:46 mjacob Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 1999, 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum; by Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Originally written by Julian Elischer (julian@tfs.com)
42  * for TRW Financial Systems for use under the MACH(2.5) operating system.
43  *
44  * TRW Financial Systems, in accordance with their agreement with Carnegie
45  * Mellon University, makes this software available to CMU to distribute
46  * or use in any manner that they see fit as long as this message is kept with
47  * the software. For this reason TFS also grants any other persons or
48  * organisations permission to use or modify this software.
49  *
50  * TFS supplies this software to be publicly redistributed
51  * on the understanding that TFS is not responsible for the correct
52  * functioning of this software in any circumstances.
53  *
54  * Ported to run under 386BSD by Julian Elischer (julian@tfs.com) Sept 1992
55  */
56 
57 #ifndef _DEV_SCSIPI_SCSIPICONF_H_
58 #define _DEV_SCSIPI_SCSIPICONF_H_
59 
60 typedef	int	boolean;
61 
62 #include <sys/callout.h>
63 #include <sys/queue.h>
64 #include <dev/scsipi/scsipi_debug.h>
65 
66 struct buf;
67 struct proc;
68 struct device;
69 struct scsipi_channel;
70 struct scsipi_periph;
71 struct scsipi_xfer;
72 
73 /*
74  * The following defines the scsipi_xfer queue.
75  */
76 TAILQ_HEAD(scsipi_xfer_queue, scsipi_xfer);
77 
78 struct scsipi_generic {
79 	u_int8_t opcode;
80 	u_int8_t bytes[15];
81 };
82 
83 
84 /*
85  * scsipi_async_event_t:
86  *
87  *	Asynchronous events from the adapter to the mid-layer and
88  *	peripherial.
89  *
90  *	Arguments:
91  *
92  *	ASYNC_EVENT_MAX_OPENINGS	scsipi_max_openings * -- max
93  *					openings, device specified in
94  *					parameters
95  *
96  *	ASYNC_EVENT_XFER_MODE		scsipi_xfer_mode * -- xfer mode
97  *					parameters changed for I_T Nexus
98  *	ASYNC_EVENT_RESET		NULL - channel has been reset
99  */
100 typedef enum {
101 	ASYNC_EVENT_MAX_OPENINGS,	/* set max openings on periph */
102 	ASYNC_EVENT_XFER_MODE,		/* xfer mode update for I_T */
103 	ASYNC_EVENT_RESET		/* channel reset */
104 } scsipi_async_event_t;
105 
106 /*
107  * scsipi_max_openings:
108  *
109  *	Argument for an ASYNC_EVENT_MAX_OPENINGS event.
110  */
111 struct scsipi_max_openings {
112 	int	mo_target;		/* openings are for this target... */
113 	int	mo_lun;			/* ...and this lun */
114 	int	mo_openings;		/* openings value */
115 };
116 
117 /*
118  * scsipi_xfer_mode:
119  *
120  *	Argument for an ASYNC_EVENT_XFER_MODE event.
121  */
122 struct scsipi_xfer_mode {
123 	int	xm_target;		/* target, for I_T Nexus */
124 	int	xm_mode;		/* PERIPH_CAP* bits */
125 	int	xm_period;		/* sync period */
126 	int	xm_offset;		/* sync offset */
127 };
128 
129 
130 /*
131  * scsipi_adapter_req_t:
132  *
133  *	Requests that can be made of an adapter.
134  *
135  *	Arguments:
136  *
137  *	ADAPTER_REQ_RUN_XFER		scsipi_xfer * -- the xfer which
138  *					is to be run
139  *
140  *	ADAPTER_REQ_GROW_RESOURCES	no argument
141  *
142  *	ADAPTER_REQ_SET_XFER_MODE	scsipi_xfer_mode * -- set the xfer
143  *					mode for the I_T Nexus according to
144  *					this
145  */
146 typedef enum {
147 	ADAPTER_REQ_RUN_XFER,		/* run a scsipi_xfer */
148 	ADAPTER_REQ_GROW_RESOURCES,	/* grow xfer execution resources */
149 	ADAPTER_REQ_SET_XFER_MODE	/* set xfer mode */
150 } scsipi_adapter_req_t;
151 
152 
153 /*
154  * scsipi_periphsw:
155  *
156  *	Callbacks into periph driver from midlayer.
157  *
158  *	psw_error	Called by the bustype's interpret-sense routine
159  *			to do periph-specific sense handling.
160  *
161  *	psw_start	Called by midlayer to restart a device once
162  *			more command openings become available.
163  *
164  *	psw_async	Called by midlayer when an asynchronous event
165  *			from the adapter occurs.
166  *
167  *	psw_done	Called by the midlayer when an xfer has completed.
168  */
169 struct scsipi_periphsw {
170 	int	(*psw_error) __P((struct scsipi_xfer *));
171 	void	(*psw_start) __P((struct scsipi_periph *));
172 	int	(*psw_async) __P((struct scsipi_periph *,
173 		    scsipi_async_event_t, void *));
174 	void	(*psw_done) __P((struct scsipi_xfer *));
175 };
176 
177 struct disk_parms;
178 struct scsipi_inquiry_pattern;
179 
180 /*
181  * scsipi_adapter:
182  *
183  *	This structure describes an instance of a SCSIPI adapter.
184  *
185  *	Note that `adapt_openings' is used by (the common case of) adapters
186  *	which have per-adapter resources.  If an adapter's command resources
187  *	are associated with a channel, the the 	`chan_openings' below will
188  *	be used instead.
189  *
190  *	Note that all adapter entry points take a pointer to a channel,
191  *	as an adapter may have more than one channel, and the channel
192  *	structure contains the channel number.
193  */
194 struct scsipi_adapter {
195 	struct device *adapt_dev;	/* pointer to adapter's device */
196 	int	adapt_nchannels;	/* numnber of adapter channels */
197 	int	adapt_refcnt;		/* adapter's reference count */
198 	int	adapt_openings;		/* total # of command openings */
199 	int	adapt_max_periph;	/* max openings per periph */
200 
201 	void	(*adapt_request) __P((struct scsipi_channel *,
202 		    scsipi_adapter_req_t, void *));
203 	void	(*adapt_minphys) __P((struct buf *));
204 	int	(*adapt_ioctl) __P((struct scsipi_channel *, u_long,
205 		    caddr_t, int, struct proc *));
206 	int	(*adapt_enable) __P((struct device *, int));
207 	int	(*adapt_getgeom) __P((struct scsipi_periph *,
208 			struct disk_parms *, u_long));
209 	int	(*adapt_accesschk) __P((struct scsipi_periph *,
210 			struct scsipi_inquiry_pattern *));
211 };
212 
213 #define	scsipi_adapter_minphys(chan, bp)				\
214 	(*(chan)->chan_adapter->adapt_minphys)((bp))
215 
216 #define	scsipi_adapter_request(chan, req, arg)				\
217 	(*(chan)->chan_adapter->adapt_request)((chan), (req), (arg))
218 
219 #define	scsipi_adapter_ioctl(chan, cmd, data, flag, p)			\
220 	(*(chan)->chan_adapter->adapt_ioctl)((chan), (cmd), (data), (flag), (p))
221 
222 #define	scsipi_adapter_enable(chan, enable)				\
223 	(*(chan)->chan_adapt->adapt_enable)((chan), (enable))
224 
225 
226 /*
227  * scsipi_bustype:
228  *
229  *	This structure describes a SCSIPI bus type.
230  */
231 struct scsipi_bustype {
232 	int	bustype_type;		/* symbolic name of type */
233 
234 	int	(*bustype_cmd) __P((struct scsipi_periph *,
235 		    struct scsipi_generic *, int, void *, size_t, int,
236 		    int, struct buf *, int));
237 	int	(*bustype_interpret_sense) __P((struct scsipi_xfer *));
238 	void	(*bustype_printaddr) __P((struct scsipi_periph *));
239 	void	(*bustype_kill_pending) __P((struct scsipi_periph *));
240 };
241 
242 /* bustype_type */
243 #define	SCSIPI_BUSTYPE_SCSI	0
244 #define	SCSIPI_BUSTYPE_ATAPI	1
245 
246 
247 /*
248  * scsipi_channel:
249  *
250  *	This structure describes a single channel of a SCSIPI adapter.
251  *	An adapter may have one or more channels.  See the comment above
252  *	regarding the resource counter.
253  */
254 struct scsipi_channel {
255 	u_int8_t type; /* XXX will die, compat with ata_atapi_attach for umass */
256 #define BUS_SCSI                0
257 #define BUS_ATAPI               1
258 /*define BUS_ATA                2*/
259 
260 	struct scsipi_adapter *chan_adapter; /* pointer to our adapter */
261 
262 	const struct scsipi_bustype *chan_bustype; /* channel's bus type */
263 
264 	/*
265 	 * Periphs for this channel.  2-dimensional array is dynamically
266 	 * allocated.
267 	 *
268 	 * XXX Consider a different data structure to save space.
269 	 */
270 	struct scsipi_periph ***chan_periphs;
271 
272 	int	chan_channel;		/* channel number */
273 	int	chan_flags;		/* channel flags */
274 	int	chan_openings;		/* number of command openings */
275 	int	chan_max_periph;	/* max openings per periph */
276 
277 	int	chan_ntargets;		/* number of targets */
278 	int	chan_nluns;		/* number of luns */
279 	int	chan_id;		/* adapter's ID for this channel */
280 
281 	int	chan_defquirks;		/* default device's quirks */
282 
283 	struct proc *chan_thread;	/* completion thread */
284 
285 	int	chan_qfreeze;		/* freeze count for queue */
286 
287 	/* Job queue for this channel. */
288 	struct scsipi_xfer_queue chan_queue;
289 
290 	/* Completed (async) jobs. */
291 	struct scsipi_xfer_queue chan_complete;
292 };
293 
294 /* chan_flags */
295 #define	SCSIPI_CHAN_SHUTDOWN	0x01	/* channel is shutting down */
296 #define	SCSIPI_CHAN_OPENINGS	0x02	/* use chan_openings */
297 #define	SCSIPI_CHAN_CANGROW	0x04	/* channel can grow resources */
298 #define	SCSIPI_CHAN_NOSETTLE	0x08	/* don't wait for devices to settle */
299 
300 #define	SCSIPI_CHAN_MAX_PERIPH(chan)					\
301 	(((chan)->chan_flags & SCSIPI_CHAN_OPENINGS) ?			\
302 	 (chan)->chan_max_periph : (chan)->chan_adapter->adapt_max_periph)
303 
304 
305 #define	scsipi_printaddr(periph)					\
306 	(*(periph)->periph_channel->chan_bustype->bustype_printaddr)((periph))
307 
308 #define	scsipi_periph_bustype(periph)					\
309 	(periph)->periph_channel->chan_bustype->bustype_type
310 
311 
312 /*
313  * Number of tag words in a periph structure:
314  *
315  *	n_tag_words = ((256 / NBBY) / sizeof(u_int32_t))
316  */
317 #define	PERIPH_NTAGWORDS	((256 / 8) / sizeof(u_int32_t))
318 
319 
320 /*
321  * scsipi_periph:
322  *
323  *	This structure describes the path between a peripherial device
324  *	and an adapter.  It contains a pointer to the adapter channel
325  *	which in turn contains a pointer to the adapter.
326  *
327  * XXX Given the way NetBSD's autoconfiguration works, this is ...
328  * XXX nasty.
329  *
330  *	Well, it's a lot nicer than it used to be, but there could
331  *	still be an improvement.
332  */
333 struct scsipi_periph {
334 	struct device *periph_dev;	/* pointer to peripherial's device */
335 	struct scsipi_channel *periph_channel; /* channel we're connected to */
336 
337 	const struct scsipi_periphsw *periph_switch; /* peripherial's entry
338 							points */
339 	int	periph_openings;	/* max # of outstanding commands */
340 	int	periph_active;		/* current # of outstanding commands */
341 	int	periph_sent;		/* current # of commands sent to adapt*/
342 
343 	int	periph_mode;		/* operation modes, CAP bits */
344 	int	periph_period;		/* sync period (factor) */
345 	int	periph_offset;		/* sync offset */
346 
347 	/*
348 	 * Information gleaned from the inquiry data.
349 	 */
350 	u_int8_t periph_type;		/* basic device type */
351 	int	periph_cap;		/* capabilities */
352 	int	periph_quirks;		/* device's quirks */
353 
354 	int	periph_flags;		/* misc. flags */
355 	int	periph_dbflags;		/* debugging flags */
356 
357 	int	periph_target;		/* target ID (drive # on ATAPI) */
358 	int	periph_lun;		/* LUN (not used on ATAPI) */
359 
360 	int	periph_version;		/* ANSI SCSI version */
361 
362 	int	periph_qfreeze;		/* queue freeze count */
363 
364 	/* Bitmap of free command tags. */
365 	u_int32_t periph_freetags[PERIPH_NTAGWORDS];
366 
367 	/* Pending scsipi_xfers on this peripherial. */
368 	struct scsipi_xfer_queue periph_xferq;
369 
370 	struct callout periph_callout;
371 
372 	/* xfer which has a pending CHECK_CONDITION */
373 	struct scsipi_xfer *periph_xscheck;
374 
375 };
376 
377 /*
378  * Macro to return the current xfer mode of a periph.
379  */
380 #define	PERIPH_XFER_MODE(periph)					\
381 	(((periph)->periph_flags & PERIPH_MODE_VALID) ?			\
382 	 (periph)->periph_mode : 0)
383 
384 /* periph_cap */
385 #define	PERIPH_CAP_ANEC		0x0001	/* async event notification */
386 #define	PERIPH_CAP_TERMIOP	0x0002	/* terminate i/o proc. messages */
387 #define	PERIPH_CAP_RELADR	0x0004	/* relative addressing */
388 #define	PERIPH_CAP_WIDE32	0x0008	/* wide-32 transfers */
389 #define	PERIPH_CAP_WIDE16	0x0010	/* wide-16 transfers */
390 		/*	XXX	0x0020	   reserved for ATAPI_CFG_DRQ_MASK */
391 		/*	XXX	0x0040	   reserved for ATAPI_CFG_DRQ_MASK */
392 #define	PERIPH_CAP_SYNC		0x0080	/* synchronous transfers */
393 #define	PERIPH_CAP_LINKCMDS	0x0100	/* linked commands */
394 #define	PERIPH_CAP_TQING	0x0200	/* tagged queueing */
395 #define	PERIPH_CAP_SFTRESET	0x0400	/* soft RESET condition response */
396 #define	PERIPH_CAP_CMD16	0x0800	/* 16 byte commands (ATAPI) */
397 
398 /* periph_flags */
399 #define	PERIPH_REMOVABLE	0x0001	/* media is removable */
400 #define	PERIPH_MEDIA_LOADED	0x0002	/* media is loaded */
401 #define	PERIPH_WAITING		0x0004	/* process waiting for opening */
402 #define	PERIPH_OPEN		0x0008	/* device is open */
403 #define	PERIPH_WAITDRAIN	0x0010	/* waiting for pending xfers to drain */
404 #define	PERIPH_GROW_OPENINGS	0x0020	/* allow openings to grow */
405 #define	PERIPH_MODE_VALID	0x0040	/* periph_mode is valid */
406 #define	PERIPH_RECOVERING	0x0080	/* periph is recovering */
407 #define	PERIPH_RECOVERY_ACTIVE	0x0100	/* a recovery command is active */
408 #define PERIPH_KEEP_LABEL	0x0200	/* retain label after 'full' close */
409 #define	PERIPH_SENSE		0x0400	/* periph has sense pending */
410 #define PERIPH_UNTAG		0x0800	/* untagged command running */
411 
412 /* periph_quirks */
413 #define	PQUIRK_AUTOSAVE		0x00000001	/* do implicit SAVE POINTERS */
414 #define	PQUIRK_NOSYNC		0x00000002	/* does not grok SDTR */
415 #define	PQUIRK_NOWIDE		0x00000004	/* does not grok WDTR */
416 #define	PQUIRK_NOTAG		0x00000008	/* does not grok tagged cmds */
417 #define	PQUIRK_NOLUNS		0x00000010	/* DTWT with LUNs */
418 #define	PQUIRK_FORCELUNS	0x00000020	/* prehistoric device groks
419 						   LUNs */
420 #define	PQUIRK_NOMODESENSE	0x00000040	/* device doesn't do MODE SENSE
421 						   properly */
422 #define	PQUIRK_NOSTARTUNIT	0x00000080	/* do not issue START UNIT */
423 #define	PQUIRK_NOSYNCCACHE	0x00000100	/* do not issue SYNC CACHE */
424 #define	PQUIRK_CDROM		0x00000200	/* device is a CD-ROM, no
425 						   matter what else it claims */
426 #define	PQUIRK_LITTLETOC	0x00000400	/* audio TOC is little-endian */
427 #define	PQUIRK_NOCAPACITY	0x00000800	/* no READ CD CAPACITY */
428 #define	PQUIRK_NOTUR		0x00001000	/* no TEST UNIT READY */
429 #define	PQUIRK_NODOORLOCK	0x00002000	/* can't lock door */
430 #define	PQUIRK_NOSENSE		0x00004000	/* can't REQUEST SENSE */
431 #define PQUIRK_ONLYBIG		0x00008000	/* only use SCSI_{R,W}_BIG */
432 #define PQUIRK_BYTE5_ZERO	0x00010000	/* byte5 in capacity is wrong */
433 #define PQUIRK_NO_FLEX_PAGE	0x00020000	/* does not support flex geom page */
434 
435 
436 /*
437  * Error values an adapter driver may return
438  */
439 typedef enum {
440 	XS_NOERROR,		/* there is no error, (sense is invalid)  */
441 	XS_SENSE,		/* Check the returned sense for the error */
442 	XS_SHORTSENSE,		/* Check the ATAPI sense for the error	  */
443 	XS_DRIVER_STUFFUP,	/* Driver failed to perform operation     */
444 	XS_RESOURCE_SHORTAGE,	/* adapter resource shortage		  */
445 	XS_SELTIMEOUT,		/* The device timed out.. turned off?     */
446 	XS_TIMEOUT,		/* The Timeout reported was caught by SW  */
447 	XS_BUSY,		/* The device busy, try again later?      */
448 	XS_RESET,		/* bus was reset; possible retry command  */
449 	XS_REQUEUE		/* requeue this command */
450 } scsipi_xfer_result_t;
451 
452 /*
453  * Each scsipi transaction is fully described by one of these structures
454  * It includes information about the source of the command and also the
455  * device and adapter for which the command is destined.
456  *
457  * Before the HBA is given this transaction, channel_q is the linkage on
458  * the related channel's chan_queue.
459  *
460  * When the this transaction is taken off the channel's chan_queue and
461  * the HBA's request entry point is called with this transaction, the
462  * HBA can use the channel_q tag for whatever it likes until it calls
463  * scsipi_done for this transaction, at which time it has to stop
464  * using channel_q.
465  *
466  * After scsipi_done is called with this transaction and if there was an
467  * error on it, channel_q then becomes the linkage on the related channel's
468  * chan_complete cqueue.
469  *
470  * The device_q member is maintained by the scsipi middle layer.  When
471  * a device issues a command, the xfer is placed on that device's
472  * pending commands queue.  When an xfer is done and freed, it is taken
473  * off the device's queue.  This allows for a device to wait for all of
474  * its pending commands to complete.
475  */
476 struct scsipi_xfer {
477 	TAILQ_ENTRY(scsipi_xfer) channel_q; /* entry on channel queue */
478 	TAILQ_ENTRY(scsipi_xfer) device_q;  /* device's pending xfers */
479 	struct callout xs_callout;	/* callout for adapter use */
480 	int	xs_control;		/* control flags */
481 	__volatile int xs_status;	/* status flags */
482 	struct scsipi_periph *xs_periph;/* peripherial doing the xfer */
483 	int	xs_retries;		/* the number of times to retry */
484 	int	xs_requeuecnt;		/* number of requeues */
485 	int	timeout;		/* in milliseconds */
486 	struct	scsipi_generic *cmd;	/* The scsipi command to execute */
487 	int	cmdlen;			/* how long it is */
488 	u_char	*data;			/* dma address OR a uio address */
489 	int	datalen;		/* data len (blank if uio) */
490 	int	resid;			/* how much buffer was not touched */
491 	scsipi_xfer_result_t error;	/* an error value */
492 	struct	buf *bp;		/* If we need to associate with */
493 					/* a buf */
494 	union {
495 		struct  scsipi_sense_data scsi_sense; /* 32 bytes */
496 		u_int32_t atapi_sense;
497 	} sense;
498 
499 	struct scsipi_xfer *xs_sensefor;/* we are requesting sense for this */
500 					/* xfer */
501 
502 	u_int8_t status;		/* SCSI status */
503 
504 	/*
505 	 * Info for tagged command queueing.  This may or may not
506 	 * be used by a given adapter driver.  These are the same
507 	 * as the bytes in the tag message.
508 	 */
509 	u_int8_t xs_tag_type;		/* tag type */
510 	u_int8_t xs_tag_id;		/* tag ID */
511 
512 	struct	scsipi_generic cmdstore
513 	    __attribute__ ((aligned (4)));/* stash the command in here */
514 };
515 
516 /*
517  * scsipi_xfer control flags
518  *
519  * To do:
520  *
521  *	- figure out what to do with XS_CTL_ESCAPE
522  *
523  *	- replace XS_CTL_URGENT with an `xs_priority' field?
524  */
525 #define	XS_CTL_NOSLEEP		0x00000001	/* don't sleep */
526 #define	XS_CTL_POLL		0x00000002	/* poll for completion */
527 #define	XS_CTL_DISCOVERY	0x00000004	/* doing device discovery */
528 #define	XS_CTL_ASYNC		0x00000008	/* command completes
529 						   asynchronously */
530 #define	XS_CTL_USERCMD		0x00000010	/* user issued command */
531 #define	XS_CTL_SILENT		0x00000020	/* don't print sense info */
532 #define	XS_CTL_IGNORE_NOT_READY	0x00000040	/* ignore NOT READY */
533 #define	XS_CTL_IGNORE_MEDIA_CHANGE 					\
534 				0x00000080	/* ignore media change */
535 #define	XS_CTL_IGNORE_ILLEGAL_REQUEST					\
536 				0x00000100	/* ignore ILLEGAL REQUEST */
537 #define	XS_CTL_RESET		0x00000200	/* reset the device */
538 #define	XS_CTL_DATA_UIO		0x00000400	/* xs_data points to uio */
539 #define	XS_CTL_DATA_IN		0x00000800	/* data coming into memory */
540 #define	XS_CTL_DATA_OUT		0x00001000	/* data going out of memory */
541 #define	XS_CTL_TARGET		0x00002000	/* target mode operation */
542 #define	XS_CTL_ESCAPE		0x00004000	/* escape operation */
543 #define	XS_CTL_URGENT		0x00008000	/* urgent (recovery)
544 						   operation */
545 #define	XS_CTL_SIMPLE_TAG	0x00010000	/* use a Simple Tag */
546 #define	XS_CTL_ORDERED_TAG	0x00020000	/* use an Ordered Tag */
547 #define	XS_CTL_HEAD_TAG		0x00040000	/* use a Head of Queue Tag */
548 #define	XS_CTL_THAW_PERIPH	0x00080000	/* thaw periph once enqueued */
549 #define	XS_CTL_FREEZE_PERIPH	0x00100000	/* freeze periph when done */
550 #define XS_CTL_DATA_ONSTACK	0x00200000	/* data is alloc'ed on stack */
551 #define XS_CTL_REQSENSE		0x00400000	/* xfer is a request sense */
552 
553 #define	XS_CTL_TAGMASK	(XS_CTL_SIMPLE_TAG|XS_CTL_ORDERED_TAG|XS_CTL_HEAD_TAG)
554 
555 #define	XS_CTL_TAGTYPE(xs)	((xs)->xs_control & XS_CTL_TAGMASK)
556 
557 /*
558  * scsipi_xfer status flags
559  */
560 #define	XS_STS_DONE		0x00000001	/* scsipi_xfer is done */
561 #define	XS_STS_PRIVATE		0xf0000000	/* reserved for HBA's use */
562 
563 /*
564  * This describes matching information for scsipi_inqmatch().  The more things
565  * match, the higher the configuration priority.
566  */
567 struct scsipi_inquiry_pattern {
568 	u_int8_t type;
569 	boolean removable;
570 	char *vendor;
571 	char *product;
572 	char *revision;
573 };
574 
575 /*
576  * This is used to pass information from the high-level configuration code
577  * to the device-specific drivers.
578  */
579 struct scsipibus_attach_args {
580 	struct scsipi_periph *sa_periph;
581 	struct scsipi_inquiry_pattern sa_inqbuf;
582 	struct scsipi_inquiry_data *sa_inqptr;
583 	union {				/* bus-type specific infos */
584 		u_int8_t scsi_version;	/* SCSI version */
585 	} scsipi_info;
586 };
587 
588 /*
589  * this describes a quirk entry
590  */
591 struct scsi_quirk_inquiry_pattern {
592 	struct scsipi_inquiry_pattern pattern;
593 	int quirks;
594 };
595 
596 /*
597  * Default number of retries, used for generic routines.
598  */
599 #define SCSIPIRETRIES 4
600 
601 
602 #ifdef _KERNEL
603 void	scsipi_init __P((void));
604 int	scsipi_command __P((struct scsipi_periph *,
605 	    struct scsipi_generic *, int, u_char *, int,
606 	    int, int, struct buf *, int));
607 void	scsipi_create_completion_thread __P((void *));
608 caddr_t	scsipi_inqmatch __P((struct scsipi_inquiry_pattern *, caddr_t,
609 	    int, int, int *));
610 char	*scsipi_dtype __P((int));
611 void	scsipi_strvis __P((u_char *, int, u_char *, int));
612 int	scsipi_execute_xs __P((struct scsipi_xfer *));
613 u_long	scsipi_size __P((struct scsipi_periph *, int));
614 int	scsipi_test_unit_ready __P((struct scsipi_periph *, int));
615 int	scsipi_prevent __P((struct scsipi_periph *, int, int));
616 int	scsipi_inquire __P((struct scsipi_periph *,
617 	    struct scsipi_inquiry_data *, int));
618 int	scsipi_mode_select __P((struct scsipi_periph *, int,
619 	    struct scsipi_mode_header *, int, int, int, int));
620 int	scsipi_mode_select_big __P((struct scsipi_periph *, int,
621 	    struct scsipi_mode_header_big *, int, int, int, int));
622 int	scsipi_mode_sense __P((struct scsipi_periph *, int, int,
623 	    struct scsipi_mode_header *, int, int, int, int));
624 int	scsipi_mode_sense_big __P((struct scsipi_periph *, int, int,
625 	    struct scsipi_mode_header_big *, int, int, int, int));
626 int	scsipi_start __P((struct scsipi_periph *, int, int));
627 void	scsipi_done __P((struct scsipi_xfer *));
628 void	scsipi_user_done __P((struct scsipi_xfer *));
629 int	scsipi_interpret_sense __P((struct scsipi_xfer *));
630 void	scsipi_wait_drain __P((struct scsipi_periph *));
631 void	scsipi_kill_pending __P((struct scsipi_periph *));
632 struct scsipi_periph *scsipi_alloc_periph __P((int));
633 #ifdef SCSIVERBOSE
634 void	scsipi_print_sense __P((struct scsipi_xfer *, int));
635 void	scsipi_print_sense_data __P((struct scsipi_sense_data *, int));
636 char   *scsipi_decode_sense __P((void *, int));
637 #endif
638 void	scsipi_async_event __P((struct scsipi_channel *,
639 	    scsipi_async_event_t, void *));
640 int	scsipi_do_ioctl __P((struct scsipi_periph *, dev_t, u_long, caddr_t,
641 	    int, struct proc *));
642 
643 void	scsipi_print_xfer_mode __P((struct scsipi_periph *));
644 void	scsipi_set_xfer_mode __P((struct scsipi_channel *, int, int));
645 
646 int	scsipi_channel_init __P((struct scsipi_channel *));
647 void	scsipi_channel_shutdown __P((struct scsipi_channel *));
648 
649 void	scsipi_insert_periph __P((struct scsipi_channel *,
650 	    struct scsipi_periph *));
651 void	scsipi_remove_periph __P((struct scsipi_channel *,
652 	    struct scsipi_periph *));
653 struct scsipi_periph *scsipi_lookup_periph __P((struct scsipi_channel *,
654 	    int, int));
655 
656 int	scsipi_adapter_addref __P((struct scsipi_adapter *));
657 void	scsipi_adapter_delref __P((struct scsipi_adapter *));
658 
659 void	scsipi_channel_freeze __P((struct scsipi_channel *, int));
660 void	scsipi_channel_thaw __P((struct scsipi_channel *, int));
661 void	scsipi_channel_timed_thaw __P((void *));
662 
663 void	scsipi_periph_freeze __P((struct scsipi_periph *, int));
664 void	scsipi_periph_thaw __P((struct scsipi_periph *, int));
665 void	scsipi_periph_timed_thaw __P((void *));
666 
667 int	scsipi_sync_period_to_factor __P((int));
668 int	scsipi_sync_factor_to_period __P((int));
669 int	scsipi_sync_factor_to_freq __P((int));
670 
671 void	show_scsipi_xs __P((struct scsipi_xfer *));
672 void	show_scsipi_cmd __P((struct scsipi_xfer *));
673 void	show_mem __P((u_char *, int));
674 #endif /* _KERNEL */
675 
676 static __inline void _lto2b __P((u_int32_t val, u_int8_t *bytes))
677 	__attribute__ ((unused));
678 static __inline void _lto3b __P((u_int32_t val, u_int8_t *bytes))
679 	__attribute__ ((unused));
680 static __inline void _lto4b __P((u_int32_t val, u_int8_t *bytes))
681 	__attribute__ ((unused));
682 static __inline u_int32_t _2btol __P((const u_int8_t *bytes))
683 	__attribute__ ((unused));
684 static __inline u_int32_t _3btol __P((const u_int8_t *bytes))
685 	__attribute__ ((unused));
686 static __inline u_int32_t _4btol __P((const u_int8_t *bytes))
687 	__attribute__ ((unused));
688 
689 static __inline void _lto2l __P((u_int32_t val, u_int8_t *bytes))
690 	__attribute__ ((unused));
691 static __inline void _lto3l __P((u_int32_t val, u_int8_t *bytes))
692 	__attribute__ ((unused));
693 static __inline void _lto4l __P((u_int32_t val, u_int8_t *bytes))
694 	__attribute__ ((unused));
695 static __inline u_int32_t _2ltol __P((const u_int8_t *bytes))
696 	__attribute__ ((unused));
697 static __inline u_int32_t _3ltol __P((const u_int8_t *bytes))
698 	__attribute__ ((unused));
699 static __inline u_int32_t _4ltol __P((const u_int8_t *bytes))
700 	__attribute__ ((unused));
701 static __inline void bswap __P((char *, int))
702 	__attribute__ ((unused));
703 
704 static __inline void
705 _lto2b(val, bytes)
706 	u_int32_t val;
707 	u_int8_t *bytes;
708 {
709 
710 	bytes[0] = (val >> 8) & 0xff;
711 	bytes[1] = val & 0xff;
712 }
713 
714 static __inline void
715 _lto3b(val, bytes)
716 	u_int32_t val;
717 	u_int8_t *bytes;
718 {
719 
720 	bytes[0] = (val >> 16) & 0xff;
721 	bytes[1] = (val >> 8) & 0xff;
722 	bytes[2] = val & 0xff;
723 }
724 
725 static __inline void
726 _lto4b(val, bytes)
727 	u_int32_t val;
728 	u_int8_t *bytes;
729 {
730 
731 	bytes[0] = (val >> 24) & 0xff;
732 	bytes[1] = (val >> 16) & 0xff;
733 	bytes[2] = (val >> 8) & 0xff;
734 	bytes[3] = val & 0xff;
735 }
736 
737 static __inline u_int32_t
738 _2btol(bytes)
739 	const u_int8_t *bytes;
740 {
741 	u_int32_t rv;
742 
743 	rv = (bytes[0] << 8) |
744 	     bytes[1];
745 	return (rv);
746 }
747 
748 static __inline u_int32_t
749 _3btol(bytes)
750 	const u_int8_t *bytes;
751 {
752 	u_int32_t rv;
753 
754 	rv = (bytes[0] << 16) |
755 	     (bytes[1] << 8) |
756 	     bytes[2];
757 	return (rv);
758 }
759 
760 static __inline u_int32_t
761 _4btol(bytes)
762 	const u_int8_t *bytes;
763 {
764 	u_int32_t rv;
765 
766 	rv = (bytes[0] << 24) |
767 	     (bytes[1] << 16) |
768 	     (bytes[2] << 8) |
769 	     bytes[3];
770 	return (rv);
771 }
772 
773 static __inline void
774 _lto2l(val, bytes)
775 	u_int32_t val;
776 	u_int8_t *bytes;
777 {
778 
779 	bytes[0] = val & 0xff;
780 	bytes[1] = (val >> 8) & 0xff;
781 }
782 
783 static __inline void
784 _lto3l(val, bytes)
785 	u_int32_t val;
786 	u_int8_t *bytes;
787 {
788 
789 	bytes[0] = val & 0xff;
790 	bytes[1] = (val >> 8) & 0xff;
791 	bytes[2] = (val >> 16) & 0xff;
792 }
793 
794 static __inline void
795 _lto4l(val, bytes)
796 	u_int32_t val;
797 	u_int8_t *bytes;
798 {
799 
800 	bytes[0] = val & 0xff;
801 	bytes[1] = (val >> 8) & 0xff;
802 	bytes[2] = (val >> 16) & 0xff;
803 	bytes[3] = (val >> 24) & 0xff;
804 }
805 
806 static __inline u_int32_t
807 _2ltol(bytes)
808 	const u_int8_t *bytes;
809 {
810 	u_int32_t rv;
811 
812 	rv = bytes[0] |
813 	     (bytes[1] << 8);
814 	return (rv);
815 }
816 
817 static __inline u_int32_t
818 _3ltol(bytes)
819 	const u_int8_t *bytes;
820 {
821 	u_int32_t rv;
822 
823 	rv = bytes[0] |
824 	     (bytes[1] << 8) |
825 	     (bytes[2] << 16);
826 	return (rv);
827 }
828 
829 static __inline u_int32_t
830 _4ltol(bytes)
831 	const u_int8_t *bytes;
832 {
833 	u_int32_t rv;
834 
835 	rv = bytes[0] |
836 	     (bytes[1] << 8) |
837 	     (bytes[2] << 16) |
838 	     (bytes[3] << 24);
839 	return (rv);
840 }
841 
842 static __inline void
843 bswap (buf, len)
844 	char *buf;
845 	int len;
846 {
847 	u_int16_t *p = (u_int16_t *)(buf + len);
848 
849 	while (--p >= (u_int16_t *)buf)
850 		*p = (*p & 0xff) << 8 | (*p >> 8 & 0xff);
851 }
852 
853 #endif /* _DEV_SCSIPI_SCSIPICONF_H_ */
854