145039663SJohn Forte /*
245039663SJohn Forte  * CDDL HEADER START
345039663SJohn Forte  *
445039663SJohn Forte  * The contents of this file are subject to the terms of the
545039663SJohn Forte  * Common Development and Distribution License (the "License").
645039663SJohn Forte  * You may not use this file except in compliance with the License.
745039663SJohn Forte  *
845039663SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
945039663SJohn Forte  * or http://www.opensolaris.org/os/licensing.
1045039663SJohn Forte  * See the License for the specific language governing permissions
1145039663SJohn Forte  * and limitations under the License.
1245039663SJohn Forte  *
1345039663SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
1445039663SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1545039663SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
1645039663SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
1745039663SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
1845039663SJohn Forte  *
1945039663SJohn Forte  * CDDL HEADER END
2045039663SJohn Forte  */
2145039663SJohn Forte /*
22716c1805SNattuvetty Bhavyan  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
23*61dfa509SRick McNeal  * Copyright 2013, 2015 Nexenta Systems, Inc. All rights reserved.
2445039663SJohn Forte  */
2545039663SJohn Forte #ifndef _PPPT_H
2645039663SJohn Forte #define	_PPPT_H
2745039663SJohn Forte 
2845039663SJohn Forte #include <sys/pppt_ic_if.h>
2945039663SJohn Forte 
3045039663SJohn Forte #ifdef	__cplusplus
3145039663SJohn Forte extern "C" {
3245039663SJohn Forte #endif
3345039663SJohn Forte 
3445039663SJohn Forte #define	PPPT_GLOBAL_LOCK() mutex_enter(&pppt_global.global_lock)
3545039663SJohn Forte #define	PPPT_GLOBAL_UNLOCK() mutex_exit(&pppt_global.global_lock)
3645039663SJohn Forte 
3745039663SJohn Forte extern int pppt_logging;
3845039663SJohn Forte 
3945039663SJohn Forte #define	PPPT_LOG if (pppt_logging) cmn_err
4045039663SJohn Forte 
4145039663SJohn Forte #define	TGT_DEREG_RETRY_SECONDS	1
4245039663SJohn Forte 
4345039663SJohn Forte typedef enum {
4445039663SJohn Forte 	PPPT_STATUS_SUCCESS = 0,
4545039663SJohn Forte 	PPPT_STATUS_FAIL,
4645039663SJohn Forte 	PPPT_STATUS_ABORTED,
4745039663SJohn Forte 	PPPT_STATUS_DONE
4845039663SJohn Forte } pppt_status_t;
4945039663SJohn Forte 
5045039663SJohn Forte #define	PPPT_MODNAME "pppt"
5145039663SJohn Forte 
52*61dfa509SRick McNeal #define	TGT_STATE_LIST() \
53*61dfa509SRick McNeal 	item(TS_UNDEFINED) \
54*61dfa509SRick McNeal 	item(TS_CREATED) \
55*61dfa509SRick McNeal 	item(TS_ONLINING) \
56*61dfa509SRick McNeal 	item(TS_ONLINE) \
57*61dfa509SRick McNeal 	item(TS_STMF_ONLINE) \
58*61dfa509SRick McNeal 	item(TS_DELETING_NEED_OFFLINE) \
59*61dfa509SRick McNeal 	item(TS_OFFLINING) \
60*61dfa509SRick McNeal 	item(TS_OFFLINE) \
61*61dfa509SRick McNeal 	item(TS_STMF_OFFLINE) \
62*61dfa509SRick McNeal 	item(TS_DELETING_STMF_DEREG) \
63*61dfa509SRick McNeal 	item(TS_DELETING_STMF_DEREG_FAIL) \
64*61dfa509SRick McNeal 	item(TS_DELETING) \
65*61dfa509SRick McNeal 	item(TS_MAX_STATE)
66*61dfa509SRick McNeal 
6745039663SJohn Forte /* Target states and events, update pppt_ts_name table whenever modified */
6845039663SJohn Forte typedef enum {
69*61dfa509SRick McNeal #define	item(a) a,
70*61dfa509SRick McNeal 	TGT_STATE_LIST()
71*61dfa509SRick McNeal #undef	item
7245039663SJohn Forte } pppt_tgt_state_t;
7345039663SJohn Forte 
7445039663SJohn Forte #ifdef PPPT_TGT_SM_STRINGS
75*61dfa509SRick McNeal static const char *pppt_ts_name[TS_MAX_STATE + 1] = {
76*61dfa509SRick McNeal #define	item(a) #a,
77*61dfa509SRick McNeal 	TGT_STATE_LIST()
78*61dfa509SRick McNeal #undef	item
7945039663SJohn Forte };
8045039663SJohn Forte #endif
8145039663SJohn Forte 
82*61dfa509SRick McNeal #define	TGT_EVENT_LIST() \
83*61dfa509SRick McNeal 	item(TE_UNDEFINED) \
84*61dfa509SRick McNeal 	item(TE_STMF_ONLINE_REQ) \
85*61dfa509SRick McNeal 	item(TE_ONLINE_SUCCESS) \
86*61dfa509SRick McNeal 	item(TE_ONLINE_FAIL) \
87*61dfa509SRick McNeal 	item(TE_STMF_ONLINE_COMPLETE_ACK) \
88*61dfa509SRick McNeal 	item(TE_STMF_OFFLINE_REQ) \
89*61dfa509SRick McNeal 	item(TE_OFFLINE_COMPLETE) \
90*61dfa509SRick McNeal 	item(TE_STMF_OFFLINE_COMPLETE_ACK) \
91*61dfa509SRick McNeal 	item(TE_DELETE) \
92*61dfa509SRick McNeal 	item(TE_STMF_DEREG_SUCCESS) \
93*61dfa509SRick McNeal 	item(TE_STMF_DEREG_FAIL) \
94*61dfa509SRick McNeal 	item(TE_STMF_DEREG_RETRY) \
95*61dfa509SRick McNeal 	item(TE_WAIT_REF_COMPLETE) /* XXX */ \
96*61dfa509SRick McNeal 	item(TE_MAX_EVENT)
97*61dfa509SRick McNeal 
9845039663SJohn Forte typedef enum {
99*61dfa509SRick McNeal #define	item(a) a,
100*61dfa509SRick McNeal 	TGT_EVENT_LIST()
101*61dfa509SRick McNeal #undef	item
10245039663SJohn Forte } pppt_tgt_event_t;
10345039663SJohn Forte 
10445039663SJohn Forte #ifdef PPPT_TGT_SM_STRINGS
105*61dfa509SRick McNeal static const char *pppt_te_name[TE_MAX_EVENT + 1] = {
106*61dfa509SRick McNeal #define	item(a) #a,
107*61dfa509SRick McNeal 	TGT_EVENT_LIST()
108*61dfa509SRick McNeal #undef	item
10945039663SJohn Forte };
11045039663SJohn Forte #endif
11145039663SJohn Forte 
11245039663SJohn Forte typedef struct pppt_tgt_s {
11345039663SJohn Forte 	kmutex_t		target_mutex;
11445039663SJohn Forte 	kcondvar_t		target_cv;
11545039663SJohn Forte 	avl_node_t		target_global_ln;
11645039663SJohn Forte 	scsi_devid_desc_t	*target_devid;
11745039663SJohn Forte 	stmf_local_port_t	*target_stmf_lport;
11845039663SJohn Forte 	avl_tree_t		target_sess_list;
11945039663SJohn Forte 
12045039663SJohn Forte 	/* Target state */
12145039663SJohn Forte 	boolean_t		target_sm_busy;
12245039663SJohn Forte 	boolean_t		target_deleting;
12345039663SJohn Forte 	pppt_tgt_state_t	target_state;
12445039663SJohn Forte 	pppt_tgt_state_t	target_last_state;
12545039663SJohn Forte 	int			target_refcount;
12645039663SJohn Forte 	list_t			target_events;
12745039663SJohn Forte } pppt_tgt_t;
12845039663SJohn Forte 
12945039663SJohn Forte typedef struct {
13045039663SJohn Forte 	struct pppt_tgt_s	*ps_target;
13145039663SJohn Forte 	uint64_t		ps_session_id;
13245039663SJohn Forte 	int			ps_refcnt;
13345039663SJohn Forte 	kmutex_t		ps_mutex;
13445039663SJohn Forte 	kcondvar_t		ps_cv;
13545039663SJohn Forte 	boolean_t		ps_closed;
13645039663SJohn Forte 	avl_node_t		ps_global_ln;
13745039663SJohn Forte 	avl_node_t		ps_target_ln;
13845039663SJohn Forte 	avl_tree_t		ps_task_list;
13945039663SJohn Forte 	stmf_scsi_session_t	*ps_stmf_sess;
14045039663SJohn Forte } pppt_sess_t;
14145039663SJohn Forte 
14245039663SJohn Forte typedef struct {
14345039663SJohn Forte 	stmf_data_buf_t		*pbuf_stmf_buf;
14445039663SJohn Forte 	boolean_t		pbuf_is_immed;
14545039663SJohn Forte 	stmf_ic_msg_t		*pbuf_immed_msg;
14645039663SJohn Forte } pppt_buf_t;
14745039663SJohn Forte 
14845039663SJohn Forte typedef enum {
14945039663SJohn Forte 	PTS_INIT = 0,
15045039663SJohn Forte 	PTS_ACTIVE,
15145039663SJohn Forte 	PTS_DONE,
15245039663SJohn Forte 	PTS_SENT_STATUS,
15345039663SJohn Forte 	PTS_ABORTED
15445039663SJohn Forte } pppt_task_state_t;
15545039663SJohn Forte 
15645039663SJohn Forte typedef struct {
15745039663SJohn Forte 	pppt_sess_t		*pt_sess;
15845039663SJohn Forte 	avl_node_t		pt_sess_ln;
15945039663SJohn Forte 	int			pt_refcnt;
16045039663SJohn Forte 	kmutex_t		pt_mutex;
16145039663SJohn Forte 	stmf_ic_msgid_t		pt_task_id;
16245039663SJohn Forte 	uint8_t			pt_lun_id[16];
16345039663SJohn Forte 	pppt_task_state_t	pt_state;
16445039663SJohn Forte 	scsi_task_t		*pt_stmf_task;
16545039663SJohn Forte 	pppt_buf_t		*pt_immed_data;
16645039663SJohn Forte 	pppt_buf_t		*pt_read_buf;
16745039663SJohn Forte 	stmf_ic_msgid_t		pt_read_xfer_msgid;
16845039663SJohn Forte } pppt_task_t;
16945039663SJohn Forte 
17045039663SJohn Forte /*
17145039663SJohn Forte  * Error statistics
17245039663SJohn Forte  */
17345039663SJohn Forte typedef struct {
17445039663SJohn Forte 	uint64_t		es_tgt_reg_svc_disabled;
17545039663SJohn Forte 	uint64_t		es_tgt_reg_duplicate;
17645039663SJohn Forte 	uint64_t		es_tgt_reg_create_fail;
17745039663SJohn Forte 	uint64_t		es_tgt_dereg_svc_disabled;
17845039663SJohn Forte 	uint64_t		es_tgt_dereg_not_found;
17945039663SJohn Forte 	uint64_t		es_sess_destroy_no_session;
18045039663SJohn Forte 	uint64_t		es_sess_lookup_no_session;
18145039663SJohn Forte 	uint64_t		es_sess_lookup_ident_mismatch;
18245039663SJohn Forte 	uint64_t		es_sess_lookup_bad_tgt_state;
18345039663SJohn Forte 	uint64_t		es_scmd_ptask_alloc_fail;
18445039663SJohn Forte 	uint64_t		es_scmd_sess_create_fail;
18545039663SJohn Forte 	uint64_t		es_scmd_stask_alloc_fail;
18645039663SJohn Forte 	uint64_t		es_scmd_dup_task_count;
18745039663SJohn Forte } pppt_error_stats_t;
18845039663SJohn Forte 
18945039663SJohn Forte #define	PPPT_INC_STAT(stat_field) \
19045039663SJohn Forte 	atomic_inc_64(&pppt_global.global_error_stats.stat_field);
19145039663SJohn Forte 
19245039663SJohn Forte /*
19345039663SJohn Forte  * State values for the iscsit service
19445039663SJohn Forte  */
19545039663SJohn Forte typedef enum {
19645039663SJohn Forte 	PSS_UNDEFINED = 0,
19745039663SJohn Forte 	PSS_DETACHED,
19845039663SJohn Forte 	PSS_DISABLED,
19945039663SJohn Forte 	PSS_ENABLING,
20045039663SJohn Forte 	PSS_ENABLED,
20145039663SJohn Forte 	PSS_BUSY,
20245039663SJohn Forte 	PSS_DISABLING
20345039663SJohn Forte } pppt_service_state_t;
20445039663SJohn Forte 
20545039663SJohn Forte 
20645039663SJohn Forte typedef struct {
20745039663SJohn Forte 	pppt_service_state_t	global_svc_state;
20845039663SJohn Forte 	dev_info_t		*global_dip;
20945039663SJohn Forte 	stmf_port_provider_t	*global_pp;
21045039663SJohn Forte 	stmf_dbuf_store_t	*global_dbuf_store;
21145039663SJohn Forte 	taskq_t			*global_dispatch_taskq;
21245039663SJohn Forte 	taskq_t			*global_sess_taskq;
21345039663SJohn Forte 	avl_tree_t		global_sess_list;
21445039663SJohn Forte 	avl_tree_t		global_target_list;
21545039663SJohn Forte 	kmutex_t		global_lock;
21645039663SJohn Forte 	door_handle_t		global_door;
21745039663SJohn Forte 	kmutex_t		global_door_lock;
21845039663SJohn Forte 	pppt_error_stats_t	global_error_stats;
21945039663SJohn Forte } pppt_global_t;
22045039663SJohn Forte 
22145039663SJohn Forte extern pppt_global_t pppt_global;
22245039663SJohn Forte 
22345039663SJohn Forte stmf_status_t pppt_lport_xfer_data(scsi_task_t *task, stmf_data_buf_t *dbuf,
22445039663SJohn Forte     uint32_t ioflags);
22545039663SJohn Forte 
22645039663SJohn Forte void pppt_xfer_read_complete(pppt_task_t *pppt_task, stmf_status_t status);
22745039663SJohn Forte 
22845039663SJohn Forte stmf_status_t pppt_lport_send_status(scsi_task_t *task, uint32_t ioflags);
22945039663SJohn Forte 
23045039663SJohn Forte void pppt_lport_task_free(scsi_task_t *task);
23145039663SJohn Forte 
23245039663SJohn Forte stmf_status_t pppt_lport_abort(stmf_local_port_t *lport, int abort_cmd,
23345039663SJohn Forte     void *arg, uint32_t flags);
23445039663SJohn Forte 
23545039663SJohn Forte void pppt_lport_ctl(stmf_local_port_t *lport, int cmd, void *arg);
23645039663SJohn Forte 
23745039663SJohn Forte pppt_sess_t *pppt_sess_lookup_locked(uint64_t session_id,
23845039663SJohn Forte     scsi_devid_desc_t *lport_devid,
239716c1805SNattuvetty Bhavyan     stmf_remote_port_t *rport);
24045039663SJohn Forte 
24145039663SJohn Forte pppt_sess_t *pppt_sess_lookup_by_id_locked(uint64_t session_id);
24245039663SJohn Forte 
24345039663SJohn Forte pppt_sess_t *pppt_sess_lookup_create(scsi_devid_desc_t *lport_devid,
244716c1805SNattuvetty Bhavyan     scsi_devid_desc_t *rport_devid, stmf_remote_port_t *rport,
245716c1805SNattuvetty Bhavyan     uint64_t session_id, stmf_status_t *statusp);
24645039663SJohn Forte 
24745039663SJohn Forte void pppt_sess_rele(pppt_sess_t *sks);
24845039663SJohn Forte 
24945039663SJohn Forte void pppt_sess_rele_locked(pppt_sess_t *sks);
25045039663SJohn Forte 
25145039663SJohn Forte void pppt_sess_close_locked(pppt_sess_t *ps);
25245039663SJohn Forte 
25345039663SJohn Forte int pppt_sess_avl_compare_by_id(const void *void_sess1,
25445039663SJohn Forte     const void *void_sess2);
25545039663SJohn Forte 
25645039663SJohn Forte int pppt_sess_avl_compare_by_name(const void *void_sess1,
25745039663SJohn Forte     const void *void_sess2);
25845039663SJohn Forte 
25945039663SJohn Forte pppt_task_t *pppt_task_alloc(void);
26045039663SJohn Forte 
26145039663SJohn Forte void pppt_task_free(pppt_task_t *ptask);
26245039663SJohn Forte 
26345039663SJohn Forte pppt_status_t pppt_task_start(pppt_task_t *ptask);
26445039663SJohn Forte 
26545039663SJohn Forte pppt_status_t pppt_task_done(pppt_task_t *ptask);
26645039663SJohn Forte 
26745039663SJohn Forte pppt_task_t *pppt_task_lookup(stmf_ic_msgid_t msgid);
26845039663SJohn Forte 
26945039663SJohn Forte void pppt_msg_rx(stmf_ic_msg_t *msg);
27045039663SJohn Forte 
27145039663SJohn Forte void pppt_msg_tx_status(stmf_ic_msg_t *orig_msg, stmf_status_t status);
27245039663SJohn Forte 
27345039663SJohn Forte pppt_tgt_t *pppt_tgt_lookup(scsi_devid_desc_t *tgt_devid);
27445039663SJohn Forte 
27545039663SJohn Forte pppt_tgt_t *pppt_tgt_lookup_locked(scsi_devid_desc_t *tgt_devid);
27645039663SJohn Forte 
27745039663SJohn Forte pppt_tgt_t *pppt_tgt_create(stmf_ic_reg_port_msg_t *reg_port,
27845039663SJohn Forte     stmf_status_t *errcode);
27945039663SJohn Forte 
28045039663SJohn Forte void pppt_tgt_async_delete(pppt_tgt_t *tgt);
28145039663SJohn Forte 
28245039663SJohn Forte void pppt_tgt_destroy(pppt_tgt_t *tgt);
28345039663SJohn Forte 
28445039663SJohn Forte int pppt_tgt_avl_compare(const void *void_tgt1, const void *void_tgt2);
28545039663SJohn Forte 
28645039663SJohn Forte void pppt_tgt_sm_ctl(stmf_local_port_t *lport, int cmd, void *arg);
28745039663SJohn Forte 
288ef3b9e2fSDan McDonald pppt_status_t pppt_task_hold(pppt_task_t *);
289ef3b9e2fSDan McDonald 
29045039663SJohn Forte #ifdef	__cplusplus
29145039663SJohn Forte }
29245039663SJohn Forte #endif
29345039663SJohn Forte 
29445039663SJohn Forte #endif	/* _PPPT_H */
295