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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 #ifndef _ISCSIT_H_
25 #define	_ISCSIT_H_
26 
27 #include <sys/iscsit/iscsi_if.h>
28 #include <iscsit_authclient.h>
29 #include <sys/iscsit/iscsit_common.h>
30 
31 /*
32  * For some reason iscsi_protocol.h lists the max version as "0x02" and the
33  * min version as "0x00".  RFC3720 clearly states that the current version
34  * number is 0x00 so that is what we will use.
35  */
36 #define	ISCSIT_MIN_VERSION			0x00
37 #define	ISCSIT_MAX_VERSION			0x00
38 #define	ISCSIT_MAX_CONNECTIONS			32 /* MC/S support  */
39 #define	ISCSIT_MAX_RECV_DATA_SEGMENT_LENGTH	(32*1024)
40 #define	ISCSIT_MAX_BURST_LENGTH			(512*1024)
41 #define	ISCSIT_MAX_FIRST_BURST_LENGTH		ISCSI_DEFAULT_FIRST_BURST_LENGTH
42 #define	ISCSIT_MAX_TIME2WAIT			ISCSI_DEFAULT_TIME_TO_WAIT
43 #define	ISCSIT_MAX_TIME2RETAIN			ISCSI_DEFAULT_TIME_TO_RETAIN
44 #define	ISCSIT_MAX_OUTSTANDING_R2T		ISCSI_DEFAULT_MAX_OUT_R2T
45 #define	ISCSIT_MAX_ERROR_RECOVERY_LEVEL		0
46 #define	ISCSIT_MAX_OUTSTANDING_UNEXPECTED_PDUS	0
47 
48 #define	ISCSIT_DEFAULT_TPG	"iscsit-default-tpg"
49 #define	ISCSIT_DEFAULT_TPGT	1
50 
51 #define	ISCSI_MAX_TSIH		0xffff
52 #define	ISCSI_UNSPEC_TSIH	0
53 
54 /* Max targets per system */
55 #define	ISCSIT_MAX_TARGETS	1024
56 
57 #define	ISCSIT_MAX_WINDOW	1024
58 #define	ISCSIT_RXPDU_QUEUE_LEN	2048
59 
60 #define	ISCSIT_CMDSN_LT_EXPCMDSN	-1
61 #define	ISCSIT_CMDSN_EQ_EXPCMDSN	1
62 #define	ISCSIT_CMDSN_GT_EXPCMDSN	0
63 /*
64  * MC/S: A timeout is maintained to recover from lost CmdSN (holes in the
65  * CmdSN ordering). When the timeout is reached, the ExpCmdSN is advanced
66  * past the hole to continue processing the queued commands. This value is
67  * system-tunable (volatile rxpdu_queue_threshold) and should be in the
68  * range from 5 to 30 seconds.
69  */
70 #define	ISCSIT_RXPDU_QUEUE_THRESHOLD		5	/* 5 seconds */
71 #define	ISCSIT_RXPDU_QUEUE_MONITOR_INTERVAL	5	/* 5 seconds */
72 
73 /* Time in seconds to wait between calls to stmf_deregister_local_port */
74 #define	TGT_DEREG_RETRY_SECONDS	1
75 
76 #define	ISCSIT_GLOBAL_LOCK(rw) rw_enter(&iscsit_global.global_rwlock, (rw))
77 #define	ISCSIT_GLOBAL_UNLOCK() rw_exit(&iscsit_global.global_rwlock)
78 
79 /* Circular buffer to hold the out-of-order PDUs in MC/S */
80 typedef struct {
81 	idm_pdu_t	*cb_buffer[ISCSIT_RXPDU_QUEUE_LEN];
82 	int		cb_num_elems;
83 } iscsit_cbuf_t;
84 
85 /*
86  * Used for serial number arithmetic (RFC 1982)
87  */
88 #define	ISCSIT_SNA32_CHECK	0x80000000
89 
90 typedef struct {
91 	char		tpg_name[MAX_TPG_NAMELEN];
92 	kmutex_t	tpg_mutex;
93 	idm_refcnt_t	tpg_refcnt;
94 	int		tpg_online;
95 	avl_tree_t	tpg_portal_list;
96 	avl_node_t	tpg_global_ln;
97 	list_node_t	tpg_delete_ln;
98 } iscsit_tpg_t;
99 
100 #define	IS_DEFAULT_TPGT(TPGT) \
101 	(((TPGT) != NULL) && \
102 	    ((TPGT)->tpgt_tpg == iscsit_global.global_default_tpg))
103 
104 typedef struct {
105 	iscsit_tpg_t	*tpgt_tpg;
106 	idm_refcnt_t	tpgt_refcnt;
107 	avl_node_t	tpgt_tgt_ln;
108 	list_node_t	tpgt_delete_ln;
109 	uint16_t	tpgt_tag;
110 	boolean_t	tpgt_needs_tpg_offline;
111 } iscsit_tpgt_t;
112 
113 typedef struct {
114 	struct sockaddr_storage portal_addr;
115 	int			portal_online;
116 	idm_refcnt_t		portal_refcnt;
117 	avl_node_t		portal_tpg_ln;
118 	iscsit_tpg_t		*portal_tpg;
119 	idm_svc_t		*portal_svc;
120 	boolean_t		portal_default;
121 	void			*portal_isns;
122 } iscsit_portal_t;
123 
124 
125 /* Target states and events, update iscsit_ts_name table whenever modified */
126 typedef enum {
127 	TS_UNDEFINED = 0,
128 	TS_CREATED,
129 	TS_ONLINING,
130 	TS_ONLINE,
131 	TS_STMF_ONLINE,
132 	TS_DELETING_NEED_OFFLINE,
133 	TS_OFFLINING,
134 	TS_OFFLINE,
135 	TS_STMF_OFFLINE,
136 	TS_DELETING_STMF_DEREG,
137 	TS_DELETING_STMF_DEREG_FAIL,
138 	TS_DELETING,
139 	TS_MAX_STATE
140 } iscsit_tgt_state_t;
141 
142 #ifdef ISCSIT_TGT_SM_STRINGS
143 static const char *iscsit_ts_name[TS_MAX_STATE+1] = {
144 	"TS_UNDEFINED",
145 	"TS_CREATED",
146 	"TS_ONLINING",
147 	"TS_ONLINE",
148 	"TS_STMF_ONLINE",
149 	"TS_DELETING_NEED_OFFLINE",
150 	"TS_OFFLINING",
151 	"TS_OFFLINE",
152 	"TS_STMF_OFFLINE",
153 	"TS_DELETING_STMF_DEREG",
154 	"TS_DELETING_STMF_DEREG_FAIL",
155 	"TS_DELETING",
156 	"TS_MAX_STATE"
157 };
158 #endif
159 
160 typedef enum {
161 	TE_UNDEFINED = 0,
162 	TE_STMF_ONLINE_REQ,
163 	TE_ONLINE_SUCCESS,
164 	TE_ONLINE_FAIL,
165 	TE_STMF_ONLINE_COMPLETE_ACK,
166 	TE_STMF_OFFLINE_REQ,
167 	TE_OFFLINE_COMPLETE,
168 	TE_STMF_OFFLINE_COMPLETE_ACK,
169 	TE_DELETE,
170 	TE_STMF_DEREG_SUCCESS,
171 	TE_STMF_DEREG_FAIL,
172 	TE_STMF_DEREG_RETRY,
173 	TE_WAIT_REF_COMPLETE,
174 	TE_MAX_EVENT
175 } iscsit_tgt_event_t;
176 
177 #ifdef ISCSIT_TGT_SM_STRINGS
178 static const char *iscsit_te_name[TE_MAX_EVENT+1] = {
179 	"TE_UNDEFINED",
180 	"TE_STMF_ONLINE_REQ",
181 	"TE_ONLINE_SUCCESS",
182 	"TE_ONLINE_FAIL",
183 	"TE_STMF_ONLINE_COMPLETE_ACK",
184 	"TE_STMF_OFFLINE_REQ",
185 	"TE_OFFLINE_COMPLETE",
186 	"TE_STMF_OFFLINE_COMPLETE_ACK",
187 	"TE_DELETE",
188 	"TE_STMF_DEREG_SUCCESS",
189 	"TE_STMF_DEREG_FAIL",
190 	"TE_STMF_DEREG_RETRY",
191 	"TE_WAIT_REF_COMPLETE",
192 	"TE_MAX_EVENT"
193 };
194 #endif
195 
196 typedef struct {
197 	char			*target_name;
198 	nvlist_t		*target_props;
199 	kmutex_t		target_mutex;
200 	idm_refcnt_t		target_refcnt;
201 	idm_refcnt_t		target_sess_refcnt;
202 	avl_tree_t		target_tpgt_list;
203 	avl_tree_t		target_sess_list;
204 	avl_node_t		target_global_ln;
205 	avl_node_t		target_global_deleted_ln;
206 	/* STMF lport == iSCSI target */
207 	scsi_devid_desc_t	*target_devid;
208 	stmf_local_port_t	*target_stmf_lport;
209 	uint8_t			target_stmf_lport_registered;
210 
211 	/* Target state */
212 	boolean_t		target_sm_busy;
213 	boolean_t		target_deleting;
214 	iscsit_tgt_state_t	target_state;
215 	iscsit_tgt_state_t	target_last_state;
216 	sm_audit_buf_t		target_state_audit;
217 	list_t			target_events;
218 	uint64_t		target_generation;
219 } iscsit_tgt_t;
220 
221 typedef struct {
222 	char			ini_name[MAX_ISCSI_NODENAMELEN];
223 	nvlist_t		*ini_props;
224 	avl_node_t		ini_global_ln;
225 } iscsit_ini_t;
226 
227 /*
228  * iSCSI Auth Information
229  */
230 typedef struct conn_auth {
231 	char			ca_tgt_chapuser[iscsitAuthStringMaxLength];
232 	uint8_t			ca_tgt_chapsecret[iscsitAuthStringMaxLength];
233 	int			ca_tgt_chapsecretlen;
234 
235 	char			ca_ini_chapuser[iscsitAuthStringMaxLength];
236 	uint8_t			ca_ini_chapsecret[iscsitAuthStringMaxLength];
237 	int			ca_ini_chapsecretlen;
238 
239 	/* RADIUS authentication information   	*/
240 	boolean_t		ca_use_radius;
241 	struct sockaddr_storage	ca_radius_server;
242 	uint8_t			ca_radius_secret[iscsitAuthStringMaxLength];
243 	int			ca_radius_secretlen;
244 
245 	/* authentication method list */
246 	iscsit_auth_method_t	ca_method_valid_list[iscsitAuthMethodMaxCount];
247 
248 	/* Target alias */
249 	char			ca_tgt_alias[MAX_ISCSI_NODENAMELEN];
250 } conn_auth_t;
251 
252 /*
253  * We have three state machines (so far) between the IDM connection state
254  * machine, the session state machine, and the login state machine.  All
255  * of these states have some concept of "full feature mode".  It's going
256  * to be obnoxious if we use a mixture of these "ffp" representations
257  * since it will be difficult to ensure the three state machines
258  * transition at exactly the same time.  We should drive decisions that
259  * depend on FFP from the IDM state machine which is actually snooping
260  * the iSCSI PDU's and will always transition at the correct time.
261  *
262  * A consequence of this approach is that there is a window just after
263  * login completes where we may get a SCSI request but the session
264  * or login state machine has not quite transitioned to "FFP".  Whether
265  * this is a problem depends on how we use those state machines.  This
266  * is what we should use them for:
267  *
268  * IDM Connection state machine - Decisions related to command processing
269  * including whether a connection is in FFP
270  *
271  * Session state machine - Summarize the state of all available connections
272  * for the purposes of ERL1, ERL2 and MC/S.  A session in LOGGED_IN state
273  * should always have at least one FFP connection but there may be a brief
274  * window where a session in ACTIVE might have one or more FFP connections
275  * even though ACTIVE is not strictly an FFP state according to the RFC.
276  *
277  * Login state machine -- drive the login process, collect negotiated
278  * parameters.  Another side effect of this approach is that we may get
279  * the "notify ffp" callback from the IDM connection state machine before
280  * the login state machine has actually transitioned to FFP state.
281  */
282 
283 struct iscsit_conn_s;
284 
285 /* Update iscsit_ss_name table whenever session states are modified */
286 typedef enum {
287 	SS_UNDEFINED = 0,
288 	SS_Q1_FREE,
289 	SS_Q2_ACTIVE,
290 	SS_Q3_LOGGED_IN,
291 	SS_Q4_FAILED,
292 	SS_Q5_CONTINUE,
293 	SS_Q6_DONE,
294 	SS_Q7_ERROR,
295 	/* Add new session states above SS_MAX_STATE */
296 	SS_MAX_STATE
297 } iscsit_session_state_t;
298 
299 #ifdef ISCSIT_SESS_SM_STRINGS
300 /* An array of state text values, for use in logging state transitions */
301 static const char *iscsit_ss_name[SS_MAX_STATE+1] = {
302 	"SS_UNDEFINED",
303 	"SS_Q1_FREE",
304 	"SS_Q2_ACTIVE",
305 	"SS_Q3_LOGGED_IN",
306 	"SS_Q4_FAILED",
307 	"SS_Q5_CONTINUE",
308 	"SS_Q6_DONE",
309 	"SS_Q7_ERROR",
310 	"SS_MAX_STATE"
311 };
312 #endif
313 
314 /* Update iscsit_se_name table whenever session events are modified */
315 typedef enum {
316 	SE_UNDEFINED = 0,
317 	SE_CONN_IN_LOGIN,	/* From login state machine */
318 	SE_CONN_LOGGED_IN,	/* FFP enabled client notification */
319 	SE_CONN_FFP_FAIL,	/* FFP disabled client notification */
320 	SE_CONN_FFP_DISABLE,	/* FFP disabled client notification */
321 	SE_CONN_FAIL,		/* Conn destroy client notification */
322 	SE_SESSION_CLOSE,	/* FFP disabled client notification */
323 	SE_SESSION_REINSTATE,	/* From login state machine */
324 	SE_SESSION_TIMEOUT,	/* Internal */
325 	SE_SESSION_CONTINUE,	/* From login state machine */
326 	SE_SESSION_CONTINUE_FAIL, /* From login state machine? */
327 	/* Add new events above SE_MAX_EVENT */
328 	SE_MAX_EVENT
329 } iscsit_session_event_t;
330 
331 #ifdef ISCSIT_SESS_SM_STRINGS
332 /* An array of event text values, for use in logging events */
333 static const char *iscsit_se_name[SE_MAX_EVENT+1] = {
334 	"SE_UNDEFINED",
335 	"SE_CONN_IN_LOGIN",
336 	"SE_CONN_LOGGED_IN",
337 	"SE_CONN_FFP_FAIL",
338 	"SE_CONN_FFP_DISABLE",
339 	"SE_CONN_FAIL",
340 	"SE_SESSION_CLOSE",
341 	"SE_SESSION_REINSTATE",
342 	"SE_SESSION_TIMEOUT",
343 	"SE_SESSION_CONTINUE",
344 	"SE_SESSION_CONTINUE_FAIL",
345 	"SE_MAX_EVENT"
346 };
347 #endif
348 
349 /*
350  * Set in ist_tgt after iscsit_tgt_unbind_sess to differentiate an unbound
351  * session from a discovery session.
352  */
353 #define	SESS_UNBOUND_FROM_TGT	-1
354 
355 typedef struct {
356 	stmf_scsi_session_t	*ist_stmf_sess;
357 	stmf_local_port_t	*ist_lport;
358 	iscsit_tgt_t		*ist_tgt;
359 	idm_refcnt_t		ist_refcnt;
360 	kmem_cache_t		*ist_task_cache;
361 	kmutex_t		ist_sn_mutex;
362 	kmutex_t		ist_mutex;
363 	kcondvar_t		ist_cv;
364 	iscsit_session_state_t	ist_state;
365 	iscsit_session_state_t	ist_last_state;
366 	sm_audit_buf_t		ist_state_audit;
367 	boolean_t		ist_sm_busy;
368 	boolean_t		ist_sm_complete;
369 	boolean_t		ist_admin_close;
370 	list_t			ist_events;
371 	int			ist_conn_count;
372 	int			ist_ffp_conn_count;
373 	struct iscsit_conn_s	*ist_failed_conn;
374 	timeout_id_t		ist_state_timeout;
375 	list_t			ist_conn_list;
376 	avl_node_t		ist_tgt_ln;
377 	char			*ist_initiator_name;
378 	char			*ist_initiator_alias;
379 	char			*ist_target_name;
380 	char			*ist_target_alias;
381 	uint8_t			ist_isid[ISCSI_ISID_LEN];
382 	uint16_t		ist_tsih;
383 	uint16_t		ist_tpgt_tag;
384 	uint32_t		ist_expcmdsn;
385 	uint32_t		ist_maxcmdsn;
386 	avl_tree_t		ist_task_list;
387 	iscsit_cbuf_t		*ist_rxpdu_queue;
388 } iscsit_sess_t;
389 
390 /* Update iscsit_ils_name table whenever login states are modified */
391 typedef enum {
392 	ILS_UNDEFINED = 0,
393 	ILS_LOGIN_INIT,
394 	ILS_LOGIN_WAITING,	/* Waiting for more login PDU's */
395 	ILS_LOGIN_PROCESSING,	/* Processing login request */
396 	ILS_LOGIN_RESPONDING,	/* Sending login response */
397 	ILS_LOGIN_RESPONDED,	/* Sent login response (no trans. to FFP) */
398 	ILS_LOGIN_FFP,		/* Sending last login PDU for final response */
399 	ILS_LOGIN_DONE,		/* Last login PDU sent (so we can free it) */
400 	ILS_LOGIN_ERROR,	/* Login error, login failed */
401 	/* Add new login states above ILS_MAX_STATE */
402 	ILS_MAX_STATE
403 } iscsit_login_state_t;
404 
405 #ifdef ISCSIT_LOGIN_SM_STRINGS
406 /* An array of login state text values, for use in logging login progress */
407 static const char *iscsit_ils_name[ILS_MAX_STATE+1] = {
408 	"ILS_UNDEFINED",
409 	"ILS_LOGIN_INIT",
410 	"ILS_LOGIN_WAITING",
411 	"ILS_LOGIN_PROCESSING",
412 	"ILS_LOGIN_RESPONDING",
413 	"ILS_LOGIN_RESPONDED",
414 	"ILS_LOGIN_FFP",
415 	"ILS_LOGIN_DONE",
416 	"ILS_LOGIN_ERROR",
417 	"ILS_MAX_STATE"
418 };
419 #endif
420 
421 /* Update iscsit_ile_name table whenever login events are modified */
422 typedef enum {
423 	ILE_UNDEFINED = 0,
424 	ILE_LOGIN_RCV,
425 	ILE_LOGIN_RESP_READY,
426 	ILE_LOGIN_FFP,
427 	ILE_LOGIN_RESP_COMPLETE,
428 	ILE_LOGIN_ERROR,
429 	ILE_LOGIN_CONN_ERROR,
430 	/* Add new login events above ILE_MAX_EVENT */
431 	ILE_MAX_EVENT
432 } iscsit_login_event_t;
433 
434 #ifdef ISCSIT_LOGIN_SM_STRINGS
435 /* An array of login event text values, for use in logging login events */
436 static const char *iscsit_ile_name[ILE_MAX_EVENT+1] = {
437 	"ILE_UNDEFINED",
438 	"ILE_LOGIN_RCV",
439 	"ILE_LOGIN_RESP_READY",
440 	"ILE_LOGIN_FFP",
441 	"ILE_LOGIN_RESP_COMPLETE",
442 	"ILE_LOGIN_ERROR",
443 	"ILE_LOGIN_CONN_ERROR",
444 	"ILE_MAX_EVENT"
445 };
446 #endif
447 
448 typedef struct {
449 	uint32_t		op_initial_params_set:1,
450 				op_discovery_session:1,
451 				op_initial_r2t:1,
452 				op_immed_data:1,
453 				op_data_pdu_in_order:1,
454 				op_data_sequence_in_order:1,
455 				op_declarative_params_set:1;
456 	uint64_t		op_max_connections;
457 	uint64_t		op_max_recv_data_segment_length;
458 	uint64_t		op_max_burst_length;
459 	uint64_t		op_first_burst_length;
460 	uint64_t		op_default_time_2_wait;
461 	uint64_t		op_default_time_2_retain;
462 	uint64_t		op_max_outstanding_r2t;
463 	uint64_t		op_error_recovery_level;
464 } iscsit_op_params_t;
465 
466 typedef struct {
467 	iscsit_login_state_t 	icl_login_state;
468 	iscsit_login_state_t 	icl_login_last_state;
469 	sm_audit_buf_t		icl_state_audit;
470 	boolean_t		icl_busy;
471 	boolean_t		icl_login_complete;
472 	kmutex_t		icl_mutex;
473 	uint32_t		icl_login_itt;
474 	uint8_t			icl_login_csg;
475 	uint8_t			icl_login_nsg;
476 	boolean_t		icl_login_transit;
477 	conn_auth_t		icl_auth;
478 	iscsit_auth_client_t	icl_auth_client;
479 	int			icl_auth_pass;
480 	list_t			icl_login_events;
481 	list_t			icl_pdu_list;
482 	uint16_t		icl_tsih;
483 	uint8_t			icl_isid[ISCSI_ISID_LEN];
484 	uint32_t		icl_cmdsn;
485 	uint16_t		icl_tpgt_tag;
486 	char			*icl_target_name;
487 	char			*icl_target_alias;
488 	char			*icl_initiator_name;
489 	char			*icl_login_resp_buf;
490 	void			*icl_login_resp_itb; /* mult-pdu idm buf */
491 	int			icl_login_resp_len; /* For kmem_free */
492 	int			icl_login_resp_valid_len;
493 	uint8_t			icl_login_resp_err_class;
494 	uint8_t			icl_login_resp_err_detail;
495 	iscsi_login_rsp_hdr_t	*icl_login_resp_tmpl;
496 	nvlist_t		*icl_request_nvlist;
497 	nvlist_t		*icl_response_nvlist;
498 	nvlist_t		*icl_negotiated_values;
499 } iscsit_conn_login_t;
500 
501 #define	SET_LOGIN_ERROR(SLE_ICT, SLE_CLASS, SLE_DETAIL) \
502 	(SLE_ICT)->ict_login_sm.icl_login_resp_err_class = (SLE_CLASS); \
503 	(SLE_ICT)->ict_login_sm.icl_login_resp_err_detail = (SLE_DETAIL);
504 
505 typedef struct iscsit_conn_s {
506 	idm_conn_t		*ict_ic;
507 	iscsit_sess_t		*ict_sess;
508 	kmutex_t		ict_mutex;
509 	idm_refcnt_t		ict_refcnt;
510 	idm_refcnt_t		ict_dispatch_refcnt;
511 	list_node_t		ict_sess_ln;
512 	iscsit_conn_login_t	ict_login_sm;
513 	iscsit_op_params_t	ict_op;
514 	uint16_t		ict_cid;
515 	uint32_t		ict_statsn;
516 	kmutex_t		ict_statsn_mutex;
517 	uint32_t		ict_keepalive_ttt;
518 	struct iscsit_conn_s	*ict_reinstate_conn;
519 	uint32_t		ict_reinstating:1,
520 				ict_lost:1,
521 				ict_destroyed:1;
522 	/*
523 	 * Parameters for processing text commands
524 	 */
525 	char			*ict_text_rsp_buf;
526 	uint32_t		ict_text_rsp_len;
527 	uint32_t		ict_text_rsp_valid_len;
528 	uint32_t		ict_text_rsp_off;
529 	uint32_t		ict_text_req_itt;	/* from initiator */
530 	uint32_t		ict_text_rsp_ttt;
531 } iscsit_conn_t;
532 
533 #define	ICT_FLAGS_DISCOVERY	0x00000001
534 
535 typedef struct {
536 	idm_buf_t		*ibuf_idm_buf;
537 	stmf_data_buf_t		*ibuf_stmf_buf;
538 	idm_pdu_t		*ibuf_immed_data_pdu;
539 	boolean_t		ibuf_is_immed;
540 } iscsit_buf_t;
541 
542 typedef struct {
543 	scsi_task_t		*it_stmf_task;
544 	idm_task_t		*it_idm_task;
545 	iscsit_buf_t		*it_immed_data;
546 	iscsit_conn_t		*it_ict;
547 	kmutex_t		it_mutex;
548 	idm_pdu_t		*it_tm_pdu;
549 	uint32_t		it_stmf_abort:1,
550 				it_aborted:1,
551 				it_active:1,
552 				it_tm_task:1,
553 				it_tm_responded:1;
554 	uint32_t		it_cmdsn;
555 	uint32_t		it_itt;
556 	uint32_t		it_ttt;
557 	avl_node_t		it_sess_ln;
558 } iscsit_task_t;
559 
560 typedef struct iscsit_isns_cfg {
561 	kmutex_t		isns_mutex;
562 	boolean_t		isns_state;
563 	list_t			isns_svrs;
564 } iscsit_isns_cfg_t;
565 
566 /*
567  * State values for the iscsit service
568  */
569 typedef enum {
570 	ISE_UNDEFINED = 0,
571 	ISE_DETACHED,
572 	ISE_DISABLED,
573 	ISE_ENABLING,
574 	ISE_ENABLED,
575 	ISE_BUSY,
576 	ISE_DISABLING
577 } iscsit_service_enabled_t;
578 
579 
580 typedef struct {
581 	iscsit_service_enabled_t	global_svc_state;
582 	dev_info_t			*global_dip;
583 	ldi_ident_t			global_li;
584 	nvlist_t			*global_props;
585 	stmf_port_provider_t		*global_pp;
586 	stmf_dbuf_store_t		*global_dbuf_store;
587 	taskq_t				*global_dispatch_taskq;
588 	idm_refcnt_t			global_refcnt;
589 	avl_tree_t			global_discovery_sessions;
590 	avl_tree_t			global_target_list;
591 	list_t				global_deleted_target_list;
592 	avl_tree_t			global_tpg_list;
593 	avl_tree_t			global_ini_list;
594 	iscsit_tpg_t			*global_default_tpg;
595 	vmem_t				*global_tsih_pool;
596 	iscsit_isns_cfg_t		global_isns_cfg;
597 	iscsi_radius_props_t		global_radius_server;
598 	krwlock_t			global_rwlock;
599 	kmutex_t			global_state_mutex;
600 } iscsit_global_t;
601 
602 extern iscsit_global_t iscsit_global;
603 
604 void
605 iscsit_global_hold();
606 
607 void
608 iscsit_global_rele();
609 
610 void
611 iscsit_global_wait_ref();
612 
613 idm_status_t
614 iscsit_login_sm_init(iscsit_conn_t *ict);
615 
616 void
617 iscsit_login_sm_fini(iscsit_conn_t *ict);
618 
619 void
620 iscsit_login_sm_event(iscsit_conn_t *ic, iscsit_login_event_t event,
621     idm_pdu_t *pdu);
622 
623 void
624 iscsit_login_sm_event_locked(iscsit_conn_t *ic, iscsit_login_event_t event,
625     idm_pdu_t *pdu);
626 
627 void
628 iscsit_send_async_event(iscsit_conn_t *ict, uint8_t async_event);
629 
630 void
631 iscsit_pdu_tx(idm_pdu_t *pdu);
632 
633 void
634 iscsit_send_reject(iscsit_conn_t *ict, idm_pdu_t *rejected_pdu, uint8_t reason);
635 
636 void
637 iscsit_text_cmd_fini(iscsit_conn_t *ict);
638 
639 /*
640  * IDM conn ops
641  */
642 
643 idm_rx_pdu_cb_t		iscsit_op_scsi_cmd;
644 idm_rx_pdu_cb_t		iscsit_rx_pdu;
645 idm_rx_pdu_error_cb_t	iscsit_rx_pdu_error;
646 idm_task_cb_t		iscsit_task_aborted;
647 idm_client_notify_cb_t	iscsit_client_notify;
648 idm_build_hdr_cb_t	iscsit_build_hdr;
649 idm_update_statsn_cb_t	iscsit_update_statsn;
650 idm_keepalive_cb_t	iscsit_keepalive;
651 
652 /*
653  * lport entry points
654  */
655 stmf_status_t
656 iscsit_xfer_scsi_data(scsi_task_t *task, stmf_data_buf_t *dbuf,
657     uint32_t ioflags);
658 
659 stmf_status_t
660 iscsit_send_scsi_status(scsi_task_t *task, uint32_t ioflags);
661 
662 void
663 iscsit_lport_task_free(scsi_task_t *task);
664 
665 stmf_status_t
666 iscsit_abort(stmf_local_port_t *lport, int abort_cmd, void *arg,
667     uint32_t flags);
668 
669 void
670 iscsit_ctl(stmf_local_port_t *lport, int cmd, void *arg);
671 
672 /*
673  * Connection functions
674  */
675 idm_status_t
676 iscsit_conn_reinstate(iscsit_conn_t *existing_ict, iscsit_conn_t *ict);
677 
678 void
679 iscsit_conn_destroy_done(iscsit_conn_t *ict);
680 
681 void
682 iscsit_conn_set_auth(iscsit_conn_t *ict);
683 
684 void
685 iscsit_conn_hold(iscsit_conn_t *ict);
686 
687 void
688 iscsit_conn_rele(iscsit_conn_t *ict);
689 
690 /*
691  * Session functions
692  */
693 int
694 iscsit_sess_avl_compare(const void *void_sess1, const void *void_sess2);
695 
696 iscsit_sess_t *
697 iscsit_sess_create(iscsit_tgt_t *tgt, iscsit_conn_t *ict,
698     uint32_t cmdsn, uint8_t *isid, uint16_t tag,
699     char *initiator_name, char *target_name,
700     uint8_t *error_class, uint8_t *error_detail);
701 
702 void
703 iscsit_sess_destroy(iscsit_sess_t *ist);
704 
705 void
706 iscsit_sess_hold(iscsit_sess_t *ist);
707 
708 void
709 iscsit_sess_rele(iscsit_sess_t *ist);
710 
711 iscsit_conn_t *
712 iscsit_sess_lookup_conn(iscsit_sess_t *ist, uint16_t cid);
713 
714 void
715 iscsit_sess_bind_conn(iscsit_sess_t *ist, iscsit_conn_t *ict);
716 
717 void
718 iscsit_sess_unbind_conn(iscsit_sess_t *ist, iscsit_conn_t *ict);
719 
720 void
721 iscsit_sess_close(iscsit_sess_t *ist);
722 
723 iscsit_sess_t *
724 iscsit_sess_reinstate(iscsit_tgt_t *tgt, iscsit_sess_t *ist, iscsit_conn_t *ict,
725     uint8_t *error_class, uint8_t *error_detail);
726 
727 void
728 iscsit_sess_sm_event(iscsit_sess_t *ist, iscsit_session_event_t event,
729     iscsit_conn_t *ict);
730 
731 /*
732  * Target, TPGT, TPGT and portal functions
733  */
734 
735 void
736 iscsit_tgt_sm_event(iscsit_tgt_t *tgt, iscsit_tgt_event_t event);
737 
738 void
739 tgt_sm_event_locked(iscsit_tgt_t *tgt, iscsit_tgt_event_t event);
740 
741 it_cfg_status_t
742 iscsit_config_merge_tgt(it_config_t *cfg);
743 
744 void
745 iscsit_config_destroy_tgts(list_t *tgt_del_list);
746 
747 void
748 iscsit_config_destroy_tpgts(list_t *tpgt_del_list);
749 
750 iscsit_tgt_t *
751 iscsit_tgt_lookup(char *target_name);
752 
753 iscsit_tgt_t *
754 iscsit_tgt_lookup_locked(char *target_name);
755 
756 int
757 iscsit_tgt_avl_compare(const void *void_tgt1, const void *void_tgt2);
758 
759 int
760 iscsit_tpgt_avl_compare(const void *void_tpgt1, const void *void_tpgt2);
761 
762 void
763 iscsit_tgt_hold(iscsit_tgt_t *tgt);
764 
765 void
766 iscsit_tgt_rele(iscsit_tgt_t *tgt);
767 
768 iscsit_tpgt_t *
769 iscsit_tgt_lookup_tpgt(iscsit_tgt_t *tgt, uint16_t tag);
770 
771 void
772 iscsit_tpgt_hold(iscsit_tpgt_t *tpgt);
773 
774 void
775 iscsit_tpgt_rele(iscsit_tpgt_t *tpgt);
776 
777 iscsit_portal_t *
778 iscsit_tgt_lookup_portal(iscsit_tgt_t *tgt, struct sockaddr_storage *sa,
779     iscsit_tpgt_t **output_tpgt);
780 
781 iscsit_sess_t *
782 iscsit_tgt_lookup_sess(iscsit_tgt_t *tgt, char *initiator_name,
783     uint8_t *isid, uint16_t tsih, uint16_t tag);
784 
785 void
786 iscsit_tgt_bind_sess(iscsit_tgt_t *tgt, iscsit_sess_t *sess);
787 
788 void
789 iscsit_tgt_unbind_sess(iscsit_tgt_t *tgt, iscsit_sess_t *sess);
790 
791 it_cfg_status_t
792 iscsit_config_merge_tpg(it_config_t *cfg, list_t *tpg_del_list);
793 
794 void
795 iscsit_config_destroy_tpgs(list_t *tpg_del_list);
796 
797 iscsit_tpg_t *
798 iscsit_tpg_lookup(char *tpg_name);
799 
800 int
801 iscsit_tpg_avl_compare(const void *void_tpg1, const void *void_tpg2);
802 
803 void
804 iscsit_tpg_hold(iscsit_tpg_t *tpg);
805 
806 void
807 iscsit_tpg_rele(iscsit_tpg_t *tpg);
808 
809 iscsit_tpg_t *
810 iscsit_tpg_createdefault();
811 
812 void
813 iscsit_tpg_destroydefault(iscsit_tpg_t *tpg);
814 
815 idm_status_t
816 iscsit_tpg_online(iscsit_tpg_t *tpg);
817 
818 void
819 iscsit_tpg_offline(iscsit_tpg_t *tpg);
820 
821 iscsit_portal_t *
822 iscsit_tpg_portal_lookup(iscsit_tpg_t *tpg, struct sockaddr_storage *sa);
823 
824 void
825 iscsit_portal_hold(iscsit_portal_t *portal);
826 
827 void
828 iscsit_portal_rele(iscsit_portal_t *portal);
829 
830 it_cfg_status_t
831 iscsit_config_merge_ini(it_config_t *cfg);
832 
833 int
834 iscsit_ini_avl_compare(const void *void_ini1, const void *void_ini2);
835 
836 iscsit_ini_t *
837 iscsit_ini_lookup_locked(char *ini_name);
838 
839 int
840 iscsit_portal_avl_compare(const void *void_portal1, const void *void_portal2);
841 
842 int
843 iscsit_verify_chap_resp(iscsit_conn_login_t *lsm,
844     unsigned int chap_i, uchar_t *chap_c, unsigned int challenge_len,
845     uchar_t *chap_r, unsigned int resp_len);
846 
847 void
848 iscsit_rxpdu_queue_monitor_start(void);
849 
850 void
851 iscsit_rxpdu_queue_monitor_stop(void);
852 
853 #endif /* _ISCSIT_H_ */
854