scsi_target.h revision 107178
142433Sdfr/*
242433Sdfr * SCSI Target Emulator
342433Sdfr *
442433Sdfr * Copyright (c) 2002 Nate Lawson.
542433Sdfr * All rights reserved.
642433Sdfr *
742433Sdfr * Redistribution and use in source and binary forms, with or without
842433Sdfr * modification, are permitted provided that the following conditions
942433Sdfr * are met:
1042433Sdfr * 1. Redistributions of source code must retain the above copyright
1142433Sdfr *    notice, this list of conditions, and the following disclaimer,
1242433Sdfr *    without modification, immediately at the beginning of the file.
1342433Sdfr * 2. The name of the author may not be used to endorse or promote products
1442433Sdfr *    derived from this software without specific prior written permission.
1542433Sdfr *
1642433Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1742433Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1842433Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1942433Sdfr * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2042433Sdfr * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2142433Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2242433Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2342433Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2442433Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2542433Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2642433Sdfr * SUCH DAMAGE.
27116182Sobrien *
28116182Sobrien * $FreeBSD: head/share/examples/scsi_target/scsi_target.h 107178 2002-11-22 22:55:51Z njl $
29116182Sobrien */
3042433Sdfr
31209579Skib#ifndef _SCSI_TARGET_H
3292547Sarr#define _SCSI_TARGET_H
33183156Sjhb
3492547Sarr/*
35183156Sjhb * Maximum number of parallel commands to accept
36183156Sjhb * Set to 256 for Fibre Channel (SPI is 16)
37183156Sjhb */
38209579Skib#define MAX_INITIATORS		16
39209579Skib#define	SECTOR_SIZE		512
4042433Sdfr#define MAX_EVENTS		(MAX_INITIATORS + 5)
4142756Speter				/* kqueue for AIO, signals */
42183156Sjhb
43183156Sjhb/* Additional SCSI 3 defines for inquiry response */
44183156Sjhb#define SID_Addr16	0x0100
4542756Speter
46183156SjhbTAILQ_HEAD(io_queue, ccb_hdr);
4742433Sdfr
4883366Sjulian/* Offset into the private CCB area for storing our descriptor */
4942756Speter#define targ_descr	periph_priv.entries[1].ptr
50183156Sjhb
51183156Sjhb/* Descriptor attached to each ATIO */
5242756Speterstruct atio_descr {
5342756Speter	off_t	  base_off;	/* Base offset for ATIO */
5442756Speter	size_t	  total_len;	/* Total xfer len for this ATIO */
5583366Sjulian	size_t	  init_req;	/* Transfer count requested to/from init */
5669449Salfred	size_t	  init_ack;	/* Data transferred ok to/from init */
57183156Sjhb	size_t	  targ_req;	/* Transfer count requested to/from target */
58183156Sjhb	size_t	  targ_ack;	/* Data transferred ok to/from target */
5969449Salfred	int	  flags;	/* Flags for CTIOs */
6069449Salfred	u_int8_t *cdb;		/* Pointer to received CDB */
61209579Skib		  		/* List of completed AIO/CTIOs */
62209579Skib	struct	  io_queue cmplt_io;
63209579Skib};
64209579Skib
65209579Skibtypedef enum {
66209579Skib	ATIO_WORK,
67209579Skib	AIO_DONE,
68209579Skib	CTIO_DONE
69209579Skib} io_ops;
70209579Skib
71209579Skib/* Descriptor attached to each CTIO */
72209579Skibstruct ctio_descr {
73209579Skib	void	*buf;		/* Backing store */
74209579Skib	off_t	 offset;	/* Position in transfer (for file, */
75209579Skib				/* doesn't start at 0) */
76209579Skib	struct	 aiocb aiocb;	/* AIO descriptor for this CTIO */
7769449Salfred	struct	 ccb_accept_tio *atio;
78209579Skib				/* ATIO we are satisfying */
79209579Skib	io_ops	 event;		/* Event that queued this CTIO */
80209579Skib};
81209579Skib
82209579Skibtypedef enum {
83209579Skib        UA_NONE         = 0x00,
84209579Skib        UA_POWER_ON     = 0x01,
85209579Skib        UA_BUS_RESET    = 0x02,
86209579Skib        UA_BDR          = 0x04
87209579Skib} ua_types;
88209579Skib
89209579Skibtypedef enum {
90209579Skib        CA_NONE         = 0x00,
91209579Skib        CA_UNIT_ATTN    = 0x01,
92209579Skib        CA_CMD_SENSE    = 0x02
93209579Skib} ca_types;
94209579Skib
95209579Skibstruct initiator_state {
96209579Skib        ua_types   orig_ua;
97209579Skib        ca_types   orig_ca;
98209579Skib        ua_types   pending_ua;
99209579Skib        ca_types   pending_ca;
100209579Skib        struct     scsi_sense_data sense_data;
101209579Skib};
102209579Skib
103209579Skib/* Global functions */
104209579Skibextern cam_status	tcmd_init(u_int16_t req_inq_flags,
105209579Skib				  u_int16_t sim_inq_flags);
106209579Skibextern int		tcmd_handle(struct ccb_accept_tio *atio,
10742433Sdfr				    struct ccb_scsiio *ctio, io_ops event);
108183156Sjhbextern void		tcmd_sense(u_int init_id, struct ccb_scsiio *ctio,
10942433Sdfr				   u_int8_t flags,
110183156Sjhb				   u_int8_t asc, u_int8_t ascq);
11142433Sdfrextern void		tcmd_ua(u_int init_id, ua_types new_ua);
112183156Sjhbextern int		work_atio(struct ccb_accept_tio *atio);
113183156Sjhbextern void		send_ccb(union ccb *ccb, int priority);
114183156Sjhbextern void		free_ccb(union ccb *ccb);
115183156Sjhbstatic __inline u_int	min(u_int a, u_int b) { return (a < b ? a : b); }
116183156Sjhb
117183156Sjhb#endif /* _SCSI_TARGET_H */
118183156Sjhb