xref: /freebsd/share/examples/scsi_target/scsi_target.h (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * SCSI Target Emulator
5  *
6  * Copyright (c) 2002 Nate Lawson.
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 _SCSI_TARGET_H
32 #define _SCSI_TARGET_H
33 
34 /*
35  * Maximum number of parallel commands to accept,
36  * 1024 for Fibre Channel (SPI is 16).
37  */
38 #define MAX_INITIATORS		8
39 #define	SECTOR_SIZE		512
40 #define MAX_EVENTS		(MAX_INITIATORS + 5)
41 				/* kqueue for AIO, signals */
42 
43 /* Additional SCSI 3 defines for inquiry response */
44 #define SID_Addr16	0x0100
45 
46 TAILQ_HEAD(io_queue, ccb_hdr);
47 
48 /* Offset into the private CCB area for storing our descriptor */
49 #define targ_descr	periph_priv.entries[1].ptr
50 
51 /* Descriptor attached to each ATIO */
52 struct atio_descr {
53 	off_t	  base_off;	/* Base offset for ATIO */
54 	uint	  total_len;	/* Total xfer len for this ATIO */
55 	uint	  init_req;	/* Transfer count requested to/from init */
56 	uint	  init_ack;	/* Data transferred ok to/from init */
57 	uint	  targ_req;	/* Transfer count requested to/from target */
58 	uint	  targ_ack;	/* Data transferred ok to/from target */
59 	int	  flags;	/* Flags for CTIOs */
60 	u_int8_t *cdb;		/* Pointer to received CDB */
61 		  		/* List of completed AIO/CTIOs */
62 	struct	  io_queue cmplt_io;
63 };
64 
65 typedef enum {
66 	ATIO_WORK,
67 	AIO_DONE,
68 	CTIO_DONE
69 } io_ops;
70 
71 /* Descriptor attached to each CTIO */
72 struct ctio_descr {
73 	void	*buf;		/* Backing store */
74 	off_t	 offset;	/* Position in transfer (for file, */
75 				/* doesn't start at 0) */
76 	struct	 aiocb aiocb;	/* AIO descriptor for this CTIO */
77 	struct	 ccb_accept_tio *atio;
78 				/* ATIO we are satisfying */
79 	io_ops	 event;		/* Event that queued this CTIO */
80 };
81 
82 typedef enum {
83         UA_NONE         = 0x00,
84         UA_POWER_ON     = 0x01,
85         UA_BUS_RESET    = 0x02,
86         UA_BDR          = 0x04
87 } ua_types;
88 
89 typedef enum {
90         CA_NONE         = 0x00,
91         CA_UNIT_ATTN    = 0x01,
92         CA_CMD_SENSE    = 0x02
93 } ca_types;
94 
95 struct initiator_state {
96         ua_types   orig_ua;
97         ca_types   orig_ca;
98         ua_types   pending_ua;
99         ca_types   pending_ca;
100         struct     scsi_sense_data sense_data;
101 };
102 
103 /* Global functions */
104 extern cam_status	tcmd_init(u_int16_t req_inq_flags,
105 				  u_int16_t sim_inq_flags);
106 extern int		tcmd_handle(struct ccb_accept_tio *atio,
107 				    struct ccb_scsiio *ctio, io_ops event);
108 extern void		tcmd_sense(u_int init_id, struct ccb_scsiio *ctio,
109 				   u_int8_t flags,
110 				   u_int8_t asc, u_int8_t ascq);
111 extern void		tcmd_ua(u_int init_id, ua_types new_ua);
112 extern int		work_atio(struct ccb_accept_tio *atio);
113 extern void		send_ccb(union ccb *ccb, int priority);
114 extern void		free_ccb(union ccb *ccb);
115 static __inline u_int	min(u_int a, u_int b) { return (a < b ? a : b); }
116 
117 /* Global Data */
118 extern int notaio;
119 
120 /*
121  * Compat Defines
122  */
123 #if __FreeBSD_version >= 500000
124 #define	OFF_FMT	"%ju"
125 #else
126 #define	OFF_FMT "%llu"
127 #endif
128 
129 #endif /* _SCSI_TARGET_H */
130