1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 #ifndef _STMF_IMPL_H
26 #define	_STMF_IMPL_H
27 
28 #include <sys/stmf_defines.h>
29 #include <sys/stmf_ioctl.h>
30 
31 #ifdef	__cplusplus
32 extern "C" {
33 #endif
34 
35 typedef	uint32_t stmf_event_handle_t;
36 #define	STMF_MAX_NUM_EVENTS		(sizeof (stmf_event_handle_t) * 8)
37 #define	STMF_EVENT_ADD(h, e)		(atomic_or_32(&(h), \
38 						((uint32_t)1) << (e)))
39 #define	STMF_EVENT_REMOVE(h, e)		(atomic_and_32(&(h), \
40 						~(((uint32_t)1) << (e))))
41 #define	STMF_EVENT_ENABLED(h, e)	(((h) & ((uint32_t)1) << (e)) != 0)
42 #define	STMF_EVENT_CLEAR_ALL(h)		((h) = 0)
43 #define	STMF_EVENT_ALLOC_HANDLE(h)	((h) = 0)
44 #define	STMF_EVENT_FREE_HANDLE(h)	((h) = 0)
45 
46 #define	STMF_TGT_NAME_LEN		256
47 #define	STMF_GUID_INPUT			32
48 
49 #define	STMF_UPDATE_KSTAT_IO(kip, dbuf)					\
50 	if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {			\
51 		kip->reads++;						\
52 		kip->nread += dbuf->db_data_size;			\
53 	} else {							\
54 		kip->writes++;						\
55 		kip->nwritten += dbuf->db_data_size;			\
56 	}
57 
58 struct stmf_i_scsi_task;
59 struct stmf_itl_data;
60 
61 typedef struct stmf_i_lu_provider {
62 	stmf_lu_provider_t	*ilp_lp;
63 	uint32_t		ilp_alloc_size;
64 	uint32_t		ilp_nlus;	/* # LUNs being exported */
65 	uint32_t		ilp_cb_in_progress:1,
66 				ilp_rsvd:31;
67 	struct stmf_i_lu_provider *ilp_next;
68 	struct stmf_pp_data	*ilp_ppd;
69 } stmf_i_lu_provider_t;
70 
71 typedef struct stmf_i_lu {
72 	stmf_lu_t	*ilu_lu;
73 	uint32_t	ilu_alloc_size;
74 	uint32_t	ilu_flags;
75 	uint32_t	ilu_ref_cnt;
76 	uint8_t		ilu_state;
77 	uint8_t		ilu_prev_state;
78 	uint8_t		ilu_access;
79 	uint8_t		ilu_alua;
80 	stmf_event_handle_t ilu_event_hdl;
81 	struct stmf_i_lu *ilu_next;
82 	struct stmf_i_lu *ilu_prev;
83 	char		*ilu_alias;
84 	char		ilu_ascii_hex_guid[STMF_GUID_INPUT + 1];
85 	kmutex_t	ilu_task_lock;
86 	uint32_t	ilu_task_cntr1;
87 	uint32_t	ilu_task_cntr2;
88 	uint32_t	*ilu_cur_task_cntr;
89 	uint32_t	ilu_ntasks;	 /* # of tasks in the ilu_task list */
90 	uint32_t	ilu_ntasks_free;	/* # of tasks that are free */
91 	uint32_t	ilu_ntasks_min_free; /* # minimal free tasks */
92 	uint32_t	rsvd1;
93 	uint32_t	ilu_proxy_registered;
94 	uint64_t	ilu_reg_msgid;
95 	struct stmf_i_scsi_task	*ilu_tasks;
96 	struct stmf_i_scsi_task *ilu_free_tasks;
97 	struct stmf_itl_data	*ilu_itl_list;
98 	kstat_t		*ilu_kstat_info;
99 	kstat_t		*ilu_kstat_io;
100 	kmutex_t	ilu_kstat_lock;
101 
102 	/* point to the luid entry in stmf_state.stmf_luid_list */
103 	void		*ilu_luid;
104 } stmf_i_lu_t;
105 
106 /*
107  * ilu_flags
108  */
109 #define	ILU_STALL_DEREGISTER		0x0001
110 #define	ILU_RESET_ACTIVE		0x0002
111 
112 typedef struct stmf_i_port_provider {
113 	stmf_port_provider_t	*ipp_pp;
114 	uint32_t		ipp_alloc_size;
115 	uint32_t		ipp_npps;
116 	uint32_t		ipp_cb_in_progress:1,
117 				ipp_rsvd:31;
118 	struct stmf_i_port_provider *ipp_next;
119 	struct stmf_pp_data	*ipp_ppd;
120 } stmf_i_port_provider_t;
121 
122 typedef struct stmf_i_local_port {
123 	stmf_local_port_t	*ilport_lport;
124 	uint32_t		ilport_alloc_size;
125 	uint32_t		ilport_nsessions;
126 	struct stmf_i_scsi_session *ilport_ss_list;
127 	krwlock_t		ilport_lock;
128 	struct stmf_i_local_port *ilport_next;
129 	struct stmf_i_local_port *ilport_prev;
130 	uint8_t			ilport_state;
131 	uint8_t			ilport_prev_state;
132 	uint8_t			ilport_standby;
133 	uint8_t			ilport_alua;
134 	uint16_t		ilport_rtpid; /* relative tpid */
135 	uint16_t		ilport_proxy_registered;
136 	uint64_t		ilport_reg_msgid;
137 	uint8_t			ilport_no_standby_lu;
138 	stmf_event_handle_t	ilport_event_hdl;
139 	clock_t			ilport_last_online_clock;
140 	clock_t			ilport_avg_interval;
141 	uint32_t		ilport_online_times;
142 	uint32_t		ilport_flags;
143 	kstat_t			*ilport_kstat_info;
144 	kstat_t			*ilport_kstat_io;
145 	kmutex_t		ilport_kstat_lock;
146 	char			ilport_kstat_tgt_name[STMF_TGT_NAME_LEN];
147 	/* which target group this port belongs to in stmf_state.stmf_tg_list */
148 	void			*ilport_tg;
149 	/* XXX Need something to track all the remote ports also */
150 } stmf_i_local_port_t;
151 
152 #define	STMF_AVG_ONLINE_INTERVAL	(30 * drv_usectohz(1000000))
153 
154 /*
155  * ilport flags
156  */
157 #define	ILPORT_FORCED_OFFLINE		0x01
158 #define	ILPORT_SS_GOT_INITIAL_LUNS	0x02
159 
160 typedef struct stmf_i_scsi_session {
161 	stmf_scsi_session_t	*iss_ss;
162 	uint32_t		iss_alloc_size;
163 	uint32_t		iss_flags;
164 	struct stmf_i_scsi_session *iss_next;
165 	/*
166 	 * Ideally we should maintain 2 maps. One would indicate a new map
167 	 * which will become available only upon receipt of a REPORT LUN
168 	 * cmd.
169 	 */
170 	struct stmf_lun_map	*iss_sm;
171 	/*
172 	 * which host group the host of this session belongs to in
173 	 * stmf_state.stmf_hg_list
174 	 */
175 	void			*iss_hg;
176 	krwlock_t		*iss_lockp;
177 	time_t			iss_creation_time;
178 } stmf_i_scsi_session_t;
179 
180 /*
181  * iss flags
182  */
183 #define	ISS_LUN_INVENTORY_CHANGED		0x0001
184 #define	ISS_RESET_ACTIVE			0x0002
185 #define	ISS_BEING_CREATED			0x0004
186 #define	ISS_GOT_INITIAL_LUNS			0x0008
187 #define	ISS_EVENT_ACTIVE			0x0010
188 
189 #define	ITASK_MAX_NCMDS			14
190 #define	ITASK_DEFAULT_POLL_TIMEOUT	0
191 struct stmf_worker;
192 typedef struct stmf_i_scsi_task {
193 	scsi_task_t		*itask_task;
194 	uint32_t		itask_alloc_size;
195 	uint32_t		itask_flags;
196 	uint64_t		itask_proxy_msg_id;
197 	stmf_data_buf_t		*itask_proxy_dbuf;
198 	struct stmf_worker	*itask_worker;
199 	uint32_t		*itask_ilu_task_cntr;
200 	struct stmf_i_scsi_task	*itask_worker_next;
201 	struct stmf_i_scsi_task	*itask_lu_next;
202 	struct stmf_i_scsi_task	*itask_lu_prev;
203 	struct stmf_i_scsi_task	*itask_lu_free_next;
204 	struct stmf_i_scsi_task	*itask_abort_next;
205 	struct stmf_itl_data	*itask_itl_datap;
206 	clock_t			itask_start_time;	/* abort and normal */
207 	/* For now we only support 4 parallel buffers. Should be enough. */
208 	stmf_data_buf_t		*itask_dbufs[4];
209 	clock_t			itask_poll_timeout;
210 	uint8_t			itask_cmd_stack[ITASK_MAX_NCMDS];
211 	uint8_t			itask_ncmds;
212 	uint8_t			itask_allocated_buf_map;
213 	uint16_t		itask_cdb_buf_size;
214 } stmf_i_scsi_task_t;
215 
216 #define	ITASK_DEFAULT_ABORT_TIMEOUT	5
217 
218 /*
219  * itask_flags
220  */
221 #define	ITASK_IN_FREE_LIST		0x0001
222 #define	ITASK_IN_TRANSITION		0x0002
223 #define	ITASK_IN_WORKER_QUEUE		0x0004
224 #define	ITASK_BEING_ABORTED		0x0008
225 #define	ITASK_BEING_COMPLETED		0x0010
226 #define	ITASK_KNOWN_TO_TGT_PORT		0x0020
227 #define	ITASK_KNOWN_TO_LU		0x0040
228 #define	ITASK_LU_ABORT_CALLED		0x0080
229 #define	ITASK_TGT_PORT_ABORT_CALLED	0x0100
230 #define	ITASK_DEFAULT_HANDLING		0x0200
231 #define	ITASK_CAUSING_LU_RESET		0x0400
232 #define	ITASK_CAUSING_TARGET_RESET	0x0800
233 #define	ITASK_KSTAT_IN_RUNQ		0x1000
234 #define	ITASK_PROXY_TASK		0x2000
235 
236 /*
237  * itask cmds.
238  */
239 #define	ITASK_CMD_MASK			0x1F
240 #define	ITASK_CMD_BUF_NDX(cmd)		(((uint8_t)(cmd)) >> 5)
241 #define	ITASK_CMD_NEW_TASK		0x1
242 #define	ITASK_CMD_DATA_XFER_DONE	0x2
243 #define	ITASK_CMD_STATUS_DONE		0x3
244 #define	ITASK_CMD_ABORT			0x4
245 #define	ITASK_CMD_SEND_STATUS		0x5
246 #define	ITASK_CMD_POLL			0x10
247 #define	ITASK_CMD_POLL_LU		(ITASK_CMD_POLL | 1)
248 #define	ITASK_CMD_POLL_LPORT		(ITASK_CMD_POLL | 2)
249 
250 /*
251  * struct maintained on a per itl basis when the lu registers ITL handle.
252  */
253 typedef struct stmf_itl_data {
254 	uint32_t			itl_counter;
255 	uint8_t				itl_flags;
256 	uint8_t				itl_hdlrm_reason;
257 	uint16_t			itl_lun;
258 	void				*itl_handle;
259 	struct stmf_i_scsi_session	*itl_session;
260 	struct stmf_itl_data		*itl_next;
261 } stmf_itl_data_t;
262 
263 /*
264  * itl flags
265  */
266 #define	STMF_ITL_BEING_TERMINATED	0x01
267 
268 /*
269  * data structures to maintain provider private data.
270  */
271 typedef struct stmf_pp_data {
272 	struct stmf_pp_data	*ppd_next;
273 	void			*ppd_provider;
274 	nvlist_t		*ppd_nv;
275 	uint32_t		ppd_lu_provider:1,
276 				ppd_port_provider:1,
277 				ppd_rsvd:30;
278 	uint32_t		ppd_alloc_size;
279 	uint64_t		ppd_token;
280 	char			ppd_name[8];
281 } stmf_pp_data_t;
282 
283 typedef struct stmf_worker {
284 	kthread_t		*worker_tid;
285 	stmf_i_scsi_task_t	*worker_task_head;
286 	stmf_i_scsi_task_t	*worker_task_tail;
287 	stmf_i_scsi_task_t	*worker_wait_head;
288 	stmf_i_scsi_task_t	*worker_wait_tail;
289 	kmutex_t		worker_lock;
290 	kcondvar_t		worker_cv;
291 	uint32_t		worker_flags;
292 	uint32_t		worker_queue_depth;	/* ntasks cur queued */
293 	uint32_t		worker_max_qdepth_pu;	/* maxqd / unit time */
294 	uint32_t		worker_max_sys_qdepth_pu; /* for all workers */
295 	uint32_t		worker_ref_count;	/* # IOs referencing */
296 } stmf_worker_t;
297 
298 /*
299  * worker flags
300  */
301 #define	STMF_WORKER_STARTED		1
302 #define	STMF_WORKER_ACTIVE		2
303 #define	STMF_WORKER_TERMINATE		4
304 
305 /*
306  * data struct for managing transfers.
307  */
308 typedef struct stmf_xfer_data {
309 	uint32_t	alloc_size;	/* Including this struct */
310 	uint32_t	size_done;
311 	uint32_t	size_left;
312 	uint8_t		buf[4];
313 } stmf_xfer_data_t;
314 
315 /*
316  * Define frequently used macros
317  */
318 #define	TASK_TO_ITASK(x_task)	\
319 	((stmf_i_scsi_task_t *)(x_task)->task_stmf_private)
320 
321 void stmf_dlun_init();
322 stmf_status_t stmf_dlun_fini();
323 void stmf_worker_init();
324 stmf_status_t stmf_worker_fini();
325 void stmf_task_free(scsi_task_t *task);
326 void stmf_do_task_abort(scsi_task_t *task);
327 void stmf_do_itl_dereg(stmf_lu_t *lu, stmf_itl_data_t *itl,
328 		uint8_t hdlrm_reason);
329 void stmf_generate_lu_event(stmf_i_lu_t *ilu, int eventid,
330 				void *arg, uint32_t flags);
331 void stmf_generate_lport_event(stmf_i_local_port_t *ilport, int eventid,
332 						void *arg, uint32_t flags);
333 
334 #ifdef	__cplusplus
335 }
336 #endif
337 
338 #endif /* _STMF_IMPL_H */
339