Home | History | Annotate | Download | only in iscsi
      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 2000 by Cisco Systems, Inc.  All rights reserved.
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 #ifndef _ISCSI_H
     28 #define	_ISCSI_H
     29 
     30 /*
     31  * Block comment which describes the contents of this file.
     32  */
     33 
     34 #ifdef __cplusplus
     35 extern "C" {
     36 #endif
     37 
     38 #include <sys/scsi/scsi.h>
     39 #include <sys/ddi.h>
     40 #include <sys/sunddi.h>
     41 #include <sys/socket.h>
     42 #include <sys/kstat.h>
     43 #include <sys/sunddi.h>
     44 #include <sys/sunmdi.h>
     45 #include <sys/mdi_impldefs.h>
     46 #include <sys/time.h>
     47 #include <sys/nvpair.h>
     48 #include <sys/sdt.h>
     49 
     50 #include <sys/iscsi_protocol.h>
     51 #include <sys/scsi/adapters/iscsi_if.h>
     52 #include <iscsiAuthClient.h>
     53 #include <iscsi_stats.h>
     54 #include <iscsi_thread.h>
     55 #include <sys/idm/idm.h>
     56 #include <sys/idm/idm_conn_sm.h>
     57 #include <nvfile.h>
     58 
     59 #ifndef MIN
     60 #define	MIN(a, b) ((a) < (b) ? (a) : (b))
     61 #endif
     62 
     63 #ifndef TRUE
     64 #define	TRUE 1
     65 #endif
     66 
     67 #ifndef FALSE
     68 #define	FALSE 0
     69 #endif
     70 
     71 #define	LOGIN_PDU_BUFFER_SIZE	(16 * 1024)	/* move somewhere else */
     72 
     73 extern boolean_t iscsi_conn_logging;
     74 extern boolean_t iscsi_io_logging;
     75 extern boolean_t iscsi_login_logging;
     76 extern boolean_t iscsi_logging;
     77 extern boolean_t iscsi_sess_logging;
     78 #define	ISCSI_CONN_LOG	if (iscsi_conn_logging) cmn_err
     79 #define	ISCSI_IO_LOG	if (iscsi_io_logging) cmn_err
     80 #define	ISCSI_LOGIN_LOG	if (iscsi_login_logging) cmn_err
     81 #define	ISCSI_LOG	if (iscsi_logging) cmn_err
     82 #define	ISCSI_SESS_LOG	if (iscsi_sess_logging) cmn_err
     83 
     84 /*
     85  * Name Format of the different Task Queues
     86  */
     87 #define	ISCSI_SESS_IOTH_NAME_FORMAT		"io_thrd_%d.%d"
     88 #define	ISCSI_SESS_WD_NAME_FORMAT		"wd_thrd_%d.%d"
     89 #define	ISCSI_SESS_LOGIN_TASKQ_NAME_FORMAT	"login_taskq_%d.%d"
     90 #define	ISCSI_CONN_CN_TASKQ_NAME_FORMAT		"conn_cn_taskq_%d.%d.%d"
     91 #define	ISCSI_CONN_RXTH_NAME_FORMAT		"rx_thrd_%d.%d.%d"
     92 #define	ISCSI_CONN_TXTH_NAME_FORMAT		"tx_thrd_%d.%d.%d"
     93 
     94 /*
     95  * The iSCSI driver will not build scatter/gather lists (iovec) longer
     96  * than the value defined here. Asserts have been include in the code
     97  * to check.
     98  */
     99 #define	ISCSI_MAX_IOVEC		5
    100 
    101 #define	ISCSI_DEFAULT_MAX_STORM_DELAY		32
    102 
    103 /*
    104  * The SNDBUF and RCVBUF size parameters for the sockets are just a
    105  * guess for the time being (I think it is the values used by CISCO
    106  * or UNH).  Testing will have to be done to figure * out the impact
    107  * of these values on performance.
    108  */
    109 #define	ISCSI_SOCKET_SNDBUF_SIZE		(256 * 1024)
    110 #define	ISCSI_SOCKET_RCVBUF_SIZE		(256 * 1024)
    111 #define	ISCSI_TCP_NODELAY_DEFAULT		0
    112 #define	ISCSI_TCP_CNOTIFY_THRESHOLD_DEFAULT	2000
    113 #define	ISCSI_TCP_CABORT_THRESHOLD_DEFAULT	10000
    114 #define	ISCSI_TCP_ABORT_THRESHOLD_DEFAULT	(30 * 1000) /* milliseconds */
    115 #define	ISNS_TCP_ABORT_THRESHOLD_DEFAULT	(3 * 1000) /* milliseconds */
    116 
    117 /* Default values for tunable parameters */
    118 #define	ISCSI_DEFAULT_RX_TIMEOUT_VALUE		60
    119 #define	ISCSI_DEFAULT_CONN_DEFAULT_LOGIN_MAX	180
    120 #define	ISCSI_DEFAULT_LOGIN_POLLING_DELAY	60
    121 
    122 /*
    123  * Convenient short hand defines
    124  */
    125 #define	TARGET_PROP	"target"
    126 #define	LUN_PROP	"lun"
    127 #define	MDI_GUID	"wwn"
    128 #define	NDI_GUID	"client-guid"
    129 
    130 #define	ISCSI_SIG_CMD	0x11111111
    131 #define	ISCSI_SIG_LUN	0x22222222
    132 #define	ISCSI_SIG_CONN	0x33333333
    133 #define	ISCSI_SIG_SESS	0x44444444
    134 #define	ISCSI_SIG_HBA	0x55555555
    135 
    136 #define	SENDTARGETS_DISCOVERY	"SENDTARGETS_DISCOVERY"
    137 
    138 #define	ISCSI_LUN_MASK_MSB	0x00003f00
    139 #define	ISCSI_LUN_MASK_LSB	0x000000ff
    140 #define	ISCSI_LUN_BYTE_COPY(lun, report_lun_data) \
    141 	lun[0] = (report_lun_data & ISCSI_LUN_MASK_MSB) >> 8; \
    142 	lun[1] = (report_lun_data & ISCSI_LUN_MASK_LSB);
    143 /*
    144  * Not defined by iSCSI, but used in the login code to
    145  * determine when to send the initial Login PDU
    146  */
    147 #define	ISCSI_INITIAL_LOGIN_STAGE	-1
    148 
    149 typedef enum iscsi_status {
    150 	/* Success */
    151 	ISCSI_STATUS_SUCCESS = 0,
    152 	/* Driver / Kernel / Code error */
    153 	ISCSI_STATUS_INTERNAL_ERROR,
    154 	/* ITT table is already full, unable to reserve slot */
    155 	ISCSI_STATUS_ITT_TABLE_FULL,
    156 	/* Login on connection failed */
    157 	ISCSI_STATUS_LOGIN_FAILED,
    158 	/* No connections are in the LOGGED_IN state */
    159 	ISCSI_STATUS_NO_CONN_LOGGED_IN,
    160 	/* TCP Transfer Error */
    161 	ISCSI_STATUS_TCP_TX_ERROR,
    162 	/* TCP Receive Error */
    163 	ISCSI_STATUS_TCP_RX_ERROR,
    164 	/* iSCSI packet RCV timeout */
    165 	ISCSI_STATUS_RX_TIMEOUT,
    166 	/* iSCSI Header Digest CRC error */
    167 	ISCSI_STATUS_HEADER_DIGEST_ERROR,
    168 	/* iSCSI Data Digest CRC error */
    169 	ISCSI_STATUS_DATA_DIGEST_ERROR,
    170 	/* kmem_alloc failure */
    171 	ISCSI_STATUS_ALLOC_FAILURE,
    172 	/* cmd (tran_abort/reset) failed */
    173 	ISCSI_STATUS_CMD_FAILED,
    174 	/* iSCSI protocol error */
    175 	ISCSI_STATUS_PROTOCOL_ERROR,
    176 	/* iSCSI protocol version mismatch */
    177 	ISCSI_STATUS_VERSION_MISMATCH,
    178 	/* iSCSI login negotiation failed */
    179 	ISCSI_STATUS_NEGO_FAIL,
    180 	/* iSCSI login authentication failed */
    181 	ISCSI_STATUS_AUTHENTICATION_FAILED,
    182 	/* iSCSI login redirection failed */
    183 	ISCSI_STATUS_REDIRECTION_FAILED,
    184 	/* iSCSI uscsi status failure */
    185 	ISCSI_STATUS_USCSI_FAILED,
    186 	/* data received would have overflowed given buffer */
    187 	ISCSI_STATUS_DATA_OVERFLOW,
    188 	/* session/connection needs to shutdown */
    189 	ISCSI_STATUS_SHUTDOWN,
    190 	/* logical unit in use */
    191 	ISCSI_STATUS_BUSY,
    192 	/* Login on connection failed, retries exceeded */
    193 	ISCSI_STATUS_LOGIN_TIMED_OUT
    194 } iscsi_status_t;
    195 #define	ISCSI_SUCCESS(status) (status == ISCSI_STATUS_SUCCESS)
    196 
    197 /* SNA32 check value used on increment of CmdSn values */
    198 #define	ISCSI_SNA32_CHECK 2147483648UL /* 2**31 */
    199 
    200 /*
    201  * This is the maximum number of commands that can be outstanding
    202  * on a iSCSI session at anyone point in time.
    203  */
    204 #define	ISCSI_CMD_TABLE_SIZE		1024
    205 
    206 /* Used on connections thread create of receiver thread */
    207 extern pri_t minclsyspri;
    208 
    209 /*
    210  * Callers of iscsid_config_one/all must hold this
    211  * semaphore across the calls.  Otherwise a ndi_devi_enter()
    212  * deadlock in the DDI layer may occur.
    213  */
    214 extern ksema_t iscsid_config_semaphore;
    215 
    216 extern kmutex_t iscsi_oid_mutex;
    217 extern uint32_t iscsi_oid;
    218 extern void *iscsi_state;
    219 
    220 /*
    221  * NOP delay is used to send a iSCSI NOP (ie. ping) across the
    222  * wire to see if the target is still alive.  NOPs are only
    223  * sent when the RX thread hasn't received anything for the
    224  * below amount of time.
    225  */
    226 #define	ISCSI_DEFAULT_NOP_DELAY			5 /* seconds */
    227 extern int	iscsi_nop_delay;
    228 /*
    229  * If we haven't received anything in a specified period of time
    230  * we will stop accepting IO via tran start.  This will enable
    231  * upper level drivers to see we might be having a problem and
    232  * in the case of scsi_vhci will start to route IO down a better
    233  * path.
    234  */
    235 #define	ISCSI_DEFAULT_RX_WINDOW			20 /* seconds */
    236 extern int	iscsi_rx_window;
    237 /*
    238  * If we haven't received anything in a specified period of time
    239  * we will stop accepting IO via tran start.  This the max limit
    240  * when encountered we will start returning a fatal error.
    241  */
    242 #define	ISCSI_DEFAULT_RX_MAX_WINDOW		180 /* seconds */
    243 extern int	iscsi_rx_max_window;
    244 
    245 /*
    246  * During iscsi boot, if the boot session has been created, the
    247  * initiator hasn't changed the boot lun to be online, we will wait
    248  * 180s here for lun online by default.
    249  */
    250 #define	ISCSI_BOOT_DEFAULT_MAX_DELAY		180 /* seconds */
    251 /*
    252  * +--------------------------------------------------------------------+
    253  * | iSCSI Driver Structures						|
    254  * +--------------------------------------------------------------------+
    255  */
    256 
    257 /*
    258  * iSCSI Auth Information
    259  */
    260 typedef struct iscsi_auth {
    261 	IscsiAuthStringBlock    auth_recv_string_block;
    262 	IscsiAuthStringBlock    auth_send_string_block;
    263 	IscsiAuthLargeBinary    auth_recv_binary_block;
    264 	IscsiAuthLargeBinary    auth_send_binary_block;
    265 	IscsiAuthClient		auth_client_block;
    266 	int			num_auth_buffers;
    267 	IscsiAuthBufferDesc	auth_buffers[5];
    268 
    269 	/*
    270 	 * To indicate if bi-directional authentication is enabled.
    271 	 * 0 means uni-directional authentication.
    272 	 * 1 means bi-directional authentication.
    273 	 */
    274 	int			bidirectional_auth;
    275 
    276 	/* Initiator's authentication information. */
    277 	char			username[iscsiAuthStringMaxLength];
    278 	uint8_t			password[iscsiAuthStringMaxLength];
    279 	int			password_length;
    280 
    281 	/* Target's authentication information. */
    282 	char			username_in[iscsiAuthStringMaxLength];
    283 	uint8_t			password_in[iscsiAuthStringMaxLength];
    284 	int			password_length_in;
    285 } iscsi_auth_t;
    286 
    287 /*
    288  * iSCSI Task
    289  */
    290 typedef struct iscsi_task {
    291 	void			*t_arg;
    292 	boolean_t		t_blocking;
    293 } iscsi_task_t;
    294 
    295 /*
    296  * These are all the iscsi_cmd types that we use to track our
    297  * commands between queues and actions.
    298  */
    299 typedef enum iscsi_cmd_type {
    300 	ISCSI_CMD_TYPE_SCSI = 1,	/* scsi cmd */
    301 	ISCSI_CMD_TYPE_NOP,		/* nop / ping */
    302 	ISCSI_CMD_TYPE_ABORT,		/* abort */
    303 	ISCSI_CMD_TYPE_RESET,		/* reset */
    304 	ISCSI_CMD_TYPE_LOGOUT,		/* logout */
    305 	ISCSI_CMD_TYPE_LOGIN,		/* login */
    306 	ISCSI_CMD_TYPE_TEXT		/* text */
    307 } iscsi_cmd_type_t;
    308 
    309 /*
    310  * iscsi_cmd_state - (reference iscsi_cmd.c for state diagram)
    311  */
    312 typedef enum iscsi_cmd_state {
    313 	ISCSI_CMD_STATE_FREE = 0,
    314 	ISCSI_CMD_STATE_PENDING,
    315 	ISCSI_CMD_STATE_ACTIVE,
    316 	ISCSI_CMD_STATE_ABORTING,
    317 	ISCSI_CMD_STATE_IDM_ABORTING,
    318 	ISCSI_CMD_STATE_COMPLETED,
    319 	ISCSI_CMD_STATE_MAX
    320 } iscsi_cmd_state_t;
    321 
    322 #ifdef ISCSI_CMD_SM_STRINGS
    323 static const char *iscsi_cmd_state_names[ISCSI_CMD_STATE_MAX+1] = {
    324 	"ISCSI_CMD_STATE_FREE",
    325 	"ISCSI_CMD_STATE_PENDING",
    326 	"ISCSI_CMD_STATE_ACTIVE",
    327 	"ISCSI_CMD_STATE_ABORTING",
    328 	"ISCSI_CMD_STATE_IDM_ABORTING",
    329 	"ISCSI_CMD_STATE_COMPLETED",
    330 	"ISCSI_CMD_STATE_MAX"
    331 };
    332 #endif
    333 
    334 /*
    335  * iscsi command events
    336  */
    337 typedef enum iscsi_cmd_event {
    338 	ISCSI_CMD_EVENT_E1 = 0,
    339 	ISCSI_CMD_EVENT_E2,
    340 	ISCSI_CMD_EVENT_E3,
    341 	ISCSI_CMD_EVENT_E4,
    342 	ISCSI_CMD_EVENT_E6,
    343 	ISCSI_CMD_EVENT_E7,
    344 	ISCSI_CMD_EVENT_E8,
    345 	ISCSI_CMD_EVENT_E9,
    346 	ISCSI_CMD_EVENT_E10,
    347 	ISCSI_CMD_EVENT_MAX
    348 } iscsi_cmd_event_t;
    349 
    350 #ifdef ISCSI_CMD_SM_STRINGS
    351 static const char *iscsi_cmd_event_names[ISCSI_CMD_EVENT_MAX+1] = {
    352 	"ISCSI_CMD_EVENT_E1",
    353 	"ISCSI_CMD_EVENT_E2",
    354 	"ISCSI_CMD_EVENT_E3",
    355 	"ISCSI_CMD_EVENT_E4",
    356 	"ISCSI_CMD_EVENT_E6",
    357 	"ISCSI_CMD_EVENT_E7",
    358 	"ISCSI_CMD_EVENT_E8",
    359 	"ISCSI_CMD_EVENT_E9",
    360 	"ISCSI_CMD_EVENT_E10",
    361 	"ISCSI_CMD_EVENT_MAX"
    362 };
    363 #endif
    364 
    365 /*
    366  * iscsi text command stages - these stages are used by iSCSI text
    367  * processing to manage long resonses.
    368  */
    369 typedef enum iscsi_cmd_text_stage {
    370 	ISCSI_CMD_TEXT_INITIAL_REQ = 0,
    371 	ISCSI_CMD_TEXT_CONTINUATION,
    372 	ISCSI_CMD_TEXT_FINAL_RSP
    373 } iscsi_cmd_text_stage_t;
    374 
    375 /*
    376  * iscsi cmd misc flags - bitwise applicable
    377  */
    378 #define	ISCSI_CMD_MISCFLAG_INTERNAL	0x1
    379 #define	ISCSI_CMD_MISCFLAG_FREE		0x2
    380 #define	ISCSI_CMD_MISCFLAG_STUCK	0x4
    381 #define	ISCSI_CMD_MISCFLAG_XARQ 	0x8
    382 #define	ISCSI_CMD_MISCFLAG_SENT		0x10
    383 #define	ISCSI_CMD_MISCFLAG_FLUSH	0x20
    384 
    385 /*
    386  * 1/2 of a 32 bit number, used for checking CmdSN
    387  * wrapped.
    388  */
    389 #define	ISCSI_CMD_SN_WRAP		0x80000000
    390 
    391 #define	ISCSI_CMD_PKT_STAT_INIT		0
    392 
    393 /*
    394  * iSCSI cmd/pkt Structure
    395  */
    396 typedef struct iscsi_cmd {
    397 	uint32_t		cmd_sig;
    398 	struct iscsi_cmd	*cmd_prev;
    399 	struct iscsi_cmd	*cmd_next;
    400 	struct iscsi_conn	*cmd_conn;
    401 
    402 	iscsi_cmd_type_t	cmd_type;
    403 	iscsi_cmd_state_t	cmd_state;
    404 	iscsi_cmd_state_t	cmd_prev_state;
    405 	clock_t			cmd_lbolt_pending;
    406 	clock_t			cmd_lbolt_active;
    407 	clock_t			cmd_lbolt_aborting;
    408 	clock_t			cmd_lbolt_idm_aborting;
    409 	clock_t			cmd_lbolt_timeout;
    410 	uint8_t			cmd_misc_flags;
    411 	idm_task_t		*cmd_itp;
    412 
    413 	union {
    414 		/* ISCSI_CMD_TYPE_SCSI */
    415 		struct {
    416 			idm_buf_t		*ibp_ibuf;
    417 			idm_buf_t		*ibp_obuf;
    418 			struct scsi_pkt		*pkt;
    419 			struct buf		*bp;
    420 			int			cmdlen;
    421 			int			statuslen;
    422 			size_t			data_transferred;
    423 
    424 			uint32_t		lun;
    425 
    426 			/*
    427 			 * If SCSI_CMD_TYPE is in ABORTING_STATE
    428 			 * then the abort_icmdp field will be a pointer
    429 			 * to the abort command chasing this one.
    430 			 */
    431 			struct iscsi_cmd	*abort_icmdp;
    432 			/*
    433 			 * pointer to the r2t associated with this
    434 			 * command (if any)
    435 			 */
    436 			struct iscsi_cmd	*r2t_icmdp;
    437 			/*
    438 			 * It will be true if this command has
    439 			 * another R2T to handle.
    440 			 */
    441 			boolean_t		r2t_more;
    442 			/*
    443 			 * It is used to record pkt_statistics temporarily.
    444 			 */
    445 			uint_t			pkt_stat;
    446 		} scsi;
    447 		/* ISCSI_CMD_TYPE_ABORT */
    448 		struct {
    449 			/* pointer to original iscsi_cmd, for abort */
    450 			struct iscsi_cmd	*icmdp;
    451 		} abort;
    452 		/* ISCSI_CMD_TYPE_RESET */
    453 		struct {
    454 			int			level;
    455 			uint8_t			response;
    456 		} reset;
    457 		/* ISCSI_CMD_TYPE_NOP */
    458 		struct {
    459 			int rsvd;
    460 		} nop;
    461 		/* ISCSI_CMD_TYPE_R2T */
    462 		struct {
    463 			struct iscsi_cmd	*icmdp;
    464 			uint32_t		offset;
    465 			uint32_t		length;
    466 		} r2t;
    467 		/* ISCSI_CMD_TYPE_LOGIN */
    468 		struct {
    469 			int rvsd;
    470 		} login;
    471 		/* ISCSI_CMD_TYPE_LOGOUT */
    472 		struct {
    473 			int rsvd;
    474 		} logout;
    475 		/* ISCSI_CMD_TYPE_TEXT */
    476 		struct {
    477 			char			*buf;
    478 			int			buf_len;
    479 			uint32_t		offset;
    480 			uint32_t		data_len;
    481 			uint32_t		total_rx_len;
    482 			uint32_t		ttt;
    483 			uint8_t			lun[8];
    484 			iscsi_cmd_text_stage_t	stage;
    485 		} text;
    486 	} cmd_un;
    487 
    488 	struct iscsi_lun	*cmd_lun; /* associated lun */
    489 
    490 	uint32_t		cmd_itt;
    491 	uint32_t		cmd_ttt;
    492 
    493 	/*
    494 	 * If a data digest error is seem on a data pdu.  This flag
    495 	 * will get set.  We don't abort the cmd immediately because
    496 	 * we want to read in all the data to get it out of the
    497 	 * stream.  Once the completion for the cmd is received we
    498 	 * we will abort the cmd and state no sense data was available.
    499 	 */
    500 	boolean_t		cmd_crc_error_seen;
    501 
    502 	/*
    503 	 * Used to block and wake up caller until action is completed.
    504 	 * This is for ABORT, RESET, and PASSTHRU cmds.
    505 	 */
    506 	int			cmd_result;
    507 	int			cmd_completed;
    508 	kmutex_t		cmd_mutex;
    509 	kcondvar_t		cmd_completion;
    510 
    511 	idm_pdu_t		cmd_pdu;
    512 
    513 	sm_audit_buf_t		cmd_state_audit;
    514 
    515 	uint32_t		cmd_sn;
    516 } iscsi_cmd_t;
    517 
    518 
    519 /*
    520  * iSCSI LUN Structure
    521  */
    522 typedef struct iscsi_lun {
    523 	uint32_t		lun_sig;
    524 	int			lun_state;
    525 
    526 	struct iscsi_lun	*lun_next;	/* next lun on this sess. */
    527 	struct iscsi_sess	*lun_sess;	/* parent sess. for lun */
    528 	dev_info_t		*lun_dip;
    529 	mdi_pathinfo_t		*lun_pip;
    530 
    531 	uint16_t		lun_num;	/* LUN */
    532 	uint8_t			lun_addr_type;	/* LUN addressing type */
    533 	uint32_t		lun_oid;	/* OID */
    534 	char			*lun_guid;	/* GUID */
    535 	int			lun_guid_size;	/* GUID allocation size */
    536 	char			*lun_addr;	/* sess,lun */
    537 	time_t			lun_time_online;
    538 
    539 	uchar_t			lun_cap;	/* bitmap of scsi caps */
    540 
    541 	uchar_t			lun_vid[ISCSI_INQ_VID_BUF_LEN];	/* Vendor ID */
    542 	uchar_t			lun_pid[ISCSI_INQ_PID_BUF_LEN];	/* Product ID */
    543 
    544 	uchar_t			lun_type;
    545 } iscsi_lun_t;
    546 
    547 #define	ISCSI_LUN_STATE_CLEAR	    0		/* used to clear all states */
    548 #define	ISCSI_LUN_STATE_OFFLINE	    1
    549 #define	ISCSI_LUN_STATE_ONLINE	    2
    550 #define	ISCSI_LUN_STATE_INVALID	    4		/* offline failed */
    551 #define	ISCSI_LUN_STATE_BUSY	    8		/* logic unit is in reset */
    552 
    553 #define	ISCSI_LUN_CAP_RESET	    0x01
    554 
    555 #define	ISCSI_SCSI_RESET_SENSE_CODE 0x29
    556 
    557 /*
    558  *
    559  *
    560  */
    561 typedef struct iscsi_queue {
    562 	iscsi_cmd_t	*head;
    563 	iscsi_cmd_t	*tail;
    564 	int		count;
    565 	kmutex_t	mutex;
    566 } iscsi_queue_t;
    567 
    568 #define	ISCSI_CONN_DEFAULT_LOGIN_MIN		0
    569 #define	ISCSI_CONN_DEFAULT_LOGIN_REDIRECT	10
    570 
    571 /* iSCSI tunable Parameters */
    572 typedef struct iscsi_tunable_params {
    573 	int		recv_login_rsp_timeout;	/* range: 0 - 60*60 */
    574 	int		conn_login_max;		/* range: 0 - 60*60 */
    575 	int		polling_login_delay;	/* range: 0 - 60*60 */
    576 } iscsi_tunable_params_t;
    577 
    578 typedef union iscsi_sockaddr {
    579 	struct sockaddr		sin;
    580 	struct sockaddr_in	sin4;
    581 	struct sockaddr_in6	sin6;
    582 } iscsi_sockaddr_t;
    583 
    584 #define	SIZEOF_SOCKADDR(so)	((so)->sa_family == AF_INET ? \
    585 	sizeof (struct sockaddr_in) : sizeof (struct sockaddr_in6))
    586 
    587 typedef enum {
    588 	LOGIN_START,
    589 	LOGIN_READY,
    590 	LOGIN_TX,
    591 	LOGIN_RX,
    592 	LOGIN_ERROR,
    593 	LOGIN_DONE,
    594 	LOGIN_FFP,
    595 	LOGIN_MAX
    596 } iscsi_login_state_t;
    597 
    598 #ifdef ISCSI_LOGIN_STATE_NAMES
    599 static const char *iscsi_login_state_names[LOGIN_MAX+1] = {
    600 	"LOGIN_START",
    601 	"LOGIN_READY",
    602 	"LOGIN_TX",
    603 	"LOGIN_RX",
    604 	"LOGIN_ERROR",
    605 	"LOGIN_DONE",
    606 	"LOGIN_FFP",
    607 	"LOGIN_MAX"
    608 };
    609 #endif
    610 
    611 /*
    612  * iscsi_conn_state
    613  */
    614 typedef enum iscsi_conn_state {
    615 	ISCSI_CONN_STATE_UNDEFINED = 0,
    616 	ISCSI_CONN_STATE_FREE,
    617 	ISCSI_CONN_STATE_IN_LOGIN,
    618 	ISCSI_CONN_STATE_LOGGED_IN,
    619 	ISCSI_CONN_STATE_IN_LOGOUT,
    620 	ISCSI_CONN_STATE_FAILED,
    621 	ISCSI_CONN_STATE_POLLING,
    622 	ISCSI_CONN_STATE_MAX
    623 } iscsi_conn_state_t;
    624 
    625 #ifdef ISCSI_ICS_NAMES
    626 static const char *iscsi_ics_name[ISCSI_CONN_STATE_MAX+1] = {
    627 	"ISCSI_CONN_STATE_UNDEFINED",
    628 	"ISCSI_CONN_STATE_FREE",
    629 	"ISCSI_CONN_STATE_IN_LOGIN",
    630 	"ISCSI_CONN_STATE_LOGGED_IN",
    631 	"ISCSI_CONN_STATE_IN_LOGOUT",
    632 	"ISCSI_CONN_STATE_FAILED",
    633 	"ISCSI_CONN_STATE_POLLING",
    634 	"ISCSI_CONN_STATE_MAX"
    635 };
    636 #endif
    637 
    638 #define	ISCSI_CONN_STATE_FULL_FEATURE(state) \
    639 	((state == ISCSI_CONN_STATE_LOGGED_IN) || \
    640 	(state == ISCSI_CONN_STATE_IN_LOGOUT))
    641 
    642 /*
    643  * iSCSI Connection Structure
    644  */
    645 typedef struct iscsi_conn {
    646 	uint32_t		conn_sig;
    647 	struct iscsi_conn	*conn_next;	/* next conn on this sess. */
    648 	struct iscsi_sess	*conn_sess;	/* parent sess. for conn. */
    649 
    650 	iscsi_conn_state_t	conn_state;	/* cur. conn. driver state */
    651 	iscsi_conn_state_t	conn_prev_state; /* prev. conn. driver state */
    652 	/* protects the session state and synchronizes the state machine */
    653 	kmutex_t		conn_state_mutex;
    654 	kcondvar_t		conn_state_change;
    655 	boolean_t		conn_state_destroy;
    656 	boolean_t		conn_state_ffp;
    657 	boolean_t		conn_state_idm_connected;
    658 	boolean_t		conn_async_logout;
    659 	ddi_taskq_t		*conn_cn_taskq;
    660 
    661 	idm_conn_t		*conn_ic;
    662 
    663 	/* base connection information, may have been redirected */
    664 	iscsi_sockaddr_t	conn_base_addr;
    665 
    666 	/* current connection information, may have been redirected */
    667 	iscsi_sockaddr_t	conn_curr_addr;
    668 
    669 	boolean_t		conn_bound;
    670 	iscsi_sockaddr_t	conn_bound_addr;
    671 
    672 	uint32_t		conn_cid;	/* CID */
    673 	uint32_t		conn_oid;	/* OID */
    674 
    675 	int			conn_current_stage;	/* iSCSI login stage */
    676 	int			conn_next_stage;	/* iSCSI login stage */
    677 	int			conn_partial_response;
    678 
    679 	/*
    680 	 * The active queue contains iscsi_cmds that have already
    681 	 * been sent on this connection.  Any future responses to
    682 	 * these cmds require alligence to this connection.  If there
    683 	 * are issues with these cmds the command may need aborted
    684 	 * depending on the command type, and must be put back into
    685 	 * the session's pending queue or aborted.
    686 	 */
    687 	iscsi_queue_t		conn_queue_active;
    688 	iscsi_queue_t		conn_queue_idm_aborting;
    689 
    690 	/* lbolt from the last receive, used for nop processing */
    691 	clock_t			conn_rx_lbolt;
    692 	clock_t			conn_nop_lbolt;
    693 
    694 	iscsi_thread_t		*conn_tx_thread;
    695 
    696 	/*
    697 	 * The expstatsn is the command status sn that is expected
    698 	 * next from the target.  Command status is carried on a number
    699 	 * of iSCSI PDUs (ex.  SCSI Cmd Response, SCSI Data IN with
    700 	 * S-Bit set, ...), not all PDUs.  If our expstatsn is different
    701 	 * than the received statsn.  Something got out of sync we need to
    702 	 * recover.
    703 	 */
    704 	uint32_t		conn_expstatsn;
    705 	uint32_t		conn_laststatsn;
    706 
    707 	/* active login parameters */
    708 	iscsi_login_params_t	conn_params;
    709 
    710 	/* Statistics */
    711 	struct {
    712 		kstat_t			*ks;
    713 		iscsi_conn_stats_t	ks_data;
    714 	} stats;
    715 
    716 	/*
    717 	 * These fields are used to coordinate the asynchronous IDM
    718 	 * PDU operations with the synchronous login code.
    719 	 */
    720 	kmutex_t		conn_login_mutex;
    721 	kcondvar_t		conn_login_cv;
    722 	iscsi_login_state_t	conn_login_state;
    723 	iscsi_status_t		conn_login_status;
    724 	iscsi_hdr_t		conn_login_resp_hdr;
    725 	char			*conn_login_data;
    726 	int			conn_login_datalen;
    727 	int			conn_login_max_data_length;
    728 
    729 	/*
    730 	 * login min and max identify the amount of time
    731 	 * in lbolt that iscsi_start_login() should attempt
    732 	 * to log into a target portal.  The login will
    733 	 * delay until the min lbolt has been reached and
    734 	 * will end once max time has been reached.  These
    735 	 * values are normally set to the default but can
    736 	 * are also altered by async commands received from
    737 	 * the targetlogin.
    738 	 */
    739 	clock_t			conn_login_min;
    740 	clock_t			conn_login_max;
    741 	sm_audit_buf_t		conn_state_audit;
    742 
    743 	/* active tunable parameters */
    744 	iscsi_tunable_params_t	conn_tunable_params;
    745 	boolean_t		conn_timeout;
    746 } iscsi_conn_t;
    747 
    748 
    749 /*
    750  * iscsi_sess_state - (reference iscsi_sess.c for state diagram)
    751  */
    752 typedef enum iscsi_sess_state {
    753 	ISCSI_SESS_STATE_FREE = 0,
    754 	ISCSI_SESS_STATE_LOGGED_IN,
    755 	ISCSI_SESS_STATE_FAILED,
    756 	ISCSI_SESS_STATE_IN_FLUSH,
    757 	ISCSI_SESS_STATE_FLUSHED,
    758 	ISCSI_SESS_STATE_MAX
    759 } iscsi_sess_state_t;
    760 
    761 #ifdef ISCSI_SESS_SM_STRINGS
    762 static const char *iscsi_sess_state_names[ISCSI_SESS_STATE_MAX+1] = {
    763 	"ISCSI_SESS_STATE_FREE",
    764 	"ISCSI_SESS_STATE_LOGGED_IN",
    765 	"ISCSI_SESS_STATE_FAILED",
    766 	"ISCSI_SESS_STATE_IN_FLUSH",
    767 	"ISCSI_SESS_STATE_FLUSHED",
    768 	"ISCSI_SESS_STATE_MAX"
    769 };
    770 #endif
    771 
    772 #define	ISCSI_SESS_STATE_FULL_FEATURE(state) \
    773 	((state == ISCSI_SESS_STATE_LOGGED_IN) || \
    774 	(state == ISCSI_SESS_STATE_IN_FLUSH))
    775 
    776 
    777 typedef enum iscsi_sess_event {
    778 	ISCSI_SESS_EVENT_N1 = 0,
    779 	ISCSI_SESS_EVENT_N3,
    780 	ISCSI_SESS_EVENT_N5,
    781 	ISCSI_SESS_EVENT_N6,
    782 	ISCSI_SESS_EVENT_N7,
    783 	ISCSI_SESS_EVENT_MAX
    784 } iscsi_sess_event_t;
    785 
    786 #ifdef ISCSI_SESS_SM_STRINGS
    787 static const char *iscsi_sess_event_names[ISCSI_SESS_EVENT_MAX+1] = {
    788 	"ISCSI_SESS_EVENT_N1",
    789 	"ISCSI_SESS_EVENT_N3",
    790 	"ISCSI_SESS_EVENT_N5",
    791 	"ISCSI_SESS_EVENT_N6",
    792 	"ISCSI_SESS_EVENT_N7",
    793 	"ISCSI_SESS_EVENT_MAX"
    794 };
    795 #endif
    796 
    797 typedef enum iscsi_sess_type {
    798 	ISCSI_SESS_TYPE_NORMAL = 0,
    799 	ISCSI_SESS_TYPE_DISCOVERY
    800 } iscsi_sess_type_t;
    801 
    802 #define	SESS_ABORT_TASK_MAX_THREADS	1
    803 
    804 /* Sun's initiator session ID */
    805 #define	ISCSI_SUN_ISID_0    0x40    /* ISID - EN format */
    806 #define	ISCSI_SUN_ISID_1    0x00    /* Sec B */
    807 #define	ISCSI_SUN_ISID_2    0x00    /* Sec B */
    808 #define	ISCSI_SUN_ISID_3    0x2A    /* Sec C - 42 = Sun's EN */
    809 /*
    810  * defines 4-5 are the reserved values.  These reserved values
    811  * are used as the ISID for an initiator-port in MP-API and used
    812  * for the send targets discovery sessions.  Byte 5 is overridden
    813  * for full feature sessions.  The default values of byte 5 for a
    814  * full feature session is 0.  When MS/T is enabled with more than
    815  * one session this byte 5 will increment > 0 up to
    816  * ISCSI_MAX_CONFIG_SESSIONS.
    817  */
    818 #define	ISCSI_SUN_ISID_4    0x00
    819 #define	ISCSI_SUN_ISID_5    0xFF
    820 
    821 #define	ISCSI_DEFAULT_SESS_BOUND	B_FALSE
    822 #define	ISCSI_DEFAULT_SESS_NUM		1
    823 
    824 /*
    825  * iSCSI Session(Target) Structure
    826  */
    827 typedef struct iscsi_sess {
    828 	uint32_t		sess_sig;
    829 
    830 	iscsi_sess_state_t	sess_state;
    831 	iscsi_sess_state_t	sess_prev_state;
    832 	clock_t			sess_state_lbolt;
    833 	/* protects the session state and synchronizes the state machine */
    834 	kmutex_t		sess_state_mutex;
    835 
    836 	/*
    837 	 * Associated target OID.
    838 	 */
    839 	uint32_t		sess_target_oid;
    840 
    841 	/*
    842 	 * Session OID.  Used by IMA, interfaces and exported as
    843 	 * TARGET_PROP which is checked by the NDI.  In addition
    844 	 * this is used in our tran_lun_init function.
    845 	 */
    846 	uint32_t		sess_oid;
    847 
    848 	struct iscsi_sess	*sess_next;
    849 	struct iscsi_hba	*sess_hba;
    850 
    851 	/* list of all luns relating to session */
    852 	struct iscsi_lun	*sess_lun_list;
    853 	krwlock_t		sess_lun_list_rwlock;
    854 
    855 	/* list of all connections relating to session */
    856 	struct iscsi_conn	*sess_conn_list;
    857 	struct iscsi_conn	*sess_conn_list_last_ptr;
    858 	/* pointer to active connection in session */
    859 	struct iscsi_conn	*sess_conn_act;
    860 	krwlock_t		sess_conn_list_rwlock;
    861 
    862 	/* Connection ID for next connection to be added to session */
    863 	uint32_t		sess_conn_next_cid;
    864 
    865 	/*
    866 	 * last time any connection on this session received
    867 	 * data from the target.
    868 	 */
    869 	clock_t			sess_rx_lbolt;
    870 
    871 	clock_t			sess_failure_lbolt;
    872 
    873 	int			sess_storm_delay;
    874 
    875 	/*
    876 	 * sess_cmdsn_mutex protects the cmdsn and itt table/values
    877 	 * Cmdsn isn't that big of a problem yet since we only have
    878 	 * one connection but in the future we will need to ensure
    879 	 * this locking is working so keep the sequence numbers in
    880 	 * sync on the wire.
    881 	 *
    882 	 * We also use this lock to protect the ITT table and it's
    883 	 * values.  We need to make sure someone doesn't assign
    884 	 * a duplicate ITT value or cell to a command.  Also we
    885 	 * need to make sure when someone is looking up an ITT
    886 	 * that the command is still in that correct queue location.
    887 	 */
    888 	kmutex_t		sess_cmdsn_mutex;
    889 
    890 	/*
    891 	 * iSCSI command sequencing / windowing.  The next
    892 	 * command to be sent via the pending queue will
    893 	 * get the sess_cmdsn.  If the maxcmdsn is less
    894 	 * than the next cmdsn then the iSCSI window is
    895 	 * closed and this command cannot be sent yet.
    896 	 * Most iscsi cmd responses from the target carry
    897 	 * a new maxcmdsn.  If this new maxcmdsn is greater
    898 	 * than the sess_maxcmdsn we will update it's value
    899 	 * and set a timer to fire in one tick and reprocess
    900 	 * the pending queue.
    901 	 *
    902 	 * The expcmdsn.   Is the value the target expects
    903 	 * to be sent for my next cmdsn.  If the expcmdsn
    904 	 * and the cmdsn get out of sync this could denote
    905 	 * a communication problem.
    906 	 */
    907 	uint32_t		sess_cmdsn;
    908 	uint32_t		sess_expcmdsn;
    909 	uint32_t		sess_maxcmdsn;
    910 
    911 	/* Next Initiator Task Tag (ITT) to use */
    912 	uint32_t		sess_itt;
    913 	/*
    914 	 * The session iscsi_cmd table is used to a fast performance
    915 	 * lookup of an ITT to a iscsi_cmd when we receive an iSCSI
    916 	 * PDU from the wire.  To reserve a location in the sess_cmd_table
    917 	 * we try the sess_itt % ISCSI_CMD_TABLE_SIZE if this cmd table
    918 	 * cell is already full.  Then increament the sess_itt and
    919 	 * try to get the cell position again, repeat until an empty
    920 	 * cell is found.  Once an empty cell is found place your
    921 	 * scsi_cmd point into the cell to reserve the location.  This
    922 	 * selection process should be done while holding the session's
    923 	 * mutex.
    924 	 */
    925 	struct iscsi_cmd	*sess_cmd_table[ISCSI_CMD_TABLE_SIZE];
    926 	int			sess_cmd_table_count;
    927 
    928 	/*
    929 	 * The pending queue contains all iscsi_cmds that require an
    930 	 * open MaxCmdSn window to be put on the wire and haven't
    931 	 * been placed on the wire.  Once placed on the wire they
    932 	 * will be moved to a connections specific active queue.
    933 	 */
    934 	iscsi_queue_t		sess_queue_pending;
    935 
    936 	iscsi_error_t		sess_last_err;
    937 
    938 	iscsi_queue_t		sess_queue_completion;
    939 	/* configured login parameters */
    940 	iscsi_login_params_t	sess_params;
    941 
    942 	/* general iSCSI protocol/session info */
    943 	uchar_t			sess_name[ISCSI_MAX_NAME_LEN];
    944 	int			sess_name_length;
    945 	char			sess_alias[ISCSI_MAX_NAME_LEN];
    946 	int			sess_alias_length;
    947 	iSCSIDiscoveryMethod_t	sess_discovered_by;
    948 	iscsi_sockaddr_t	sess_discovered_addr;
    949 	uchar_t			sess_isid[ISCSI_ISID_LEN]; /* Session ID */
    950 	uint16_t		sess_tsid; /* Target ID */
    951 	/*
    952 	 * If the target portal group tag(TPGT) is equal to ISCSI_DEFAULT_TPGT
    953 	 * then the initiator will accept a successful login with any TPGT
    954 	 * specified by the target.  If a none default TPGT is configured
    955 	 * then we will only successfully accept a login with that matching
    956 	 * TPGT value.
    957 	 */
    958 	int			sess_tpgt_conf;
    959 	/* This field records the negotiated TPGT value, preserved for dtrace */
    960 	int			sess_tpgt_nego;
    961 
    962 	/*
    963 	 * Authentication information.
    964 	 *
    965 	 * DCW: Again IMA seems to take a session view at this
    966 	 * information.
    967 	 */
    968 	iscsi_auth_t		sess_auth;
    969 
    970 	/* Statistics */
    971 	struct {
    972 		kstat_t			*ks;
    973 		iscsi_sess_stats_t	ks_data;
    974 		kstat_t			*ks_io;
    975 		kstat_io_t		ks_io_data;
    976 		kmutex_t		ks_io_lock;
    977 	} stats;
    978 
    979 	iscsi_thread_t		*sess_ic_thread;
    980 	boolean_t		sess_window_open;
    981 	boolean_t		sess_boot;
    982 	iscsi_sess_type_t	sess_type;
    983 
    984 	boolean_t		sess_enum_in_progress;
    985 
    986 	ddi_taskq_t		*sess_taskq;
    987 
    988 	iscsi_thread_t		*sess_wd_thread;
    989 
    990 	sm_audit_buf_t		sess_state_audit;
    991 
    992 	kmutex_t		sess_reset_mutex;
    993 
    994 	boolean_t		sess_reset_in_progress;
    995 } iscsi_sess_t;
    996 
    997 /*
    998  * This structure will be used to store sessions to be online
    999  * during normal login operation.
   1000  */
   1001 typedef struct iscsi_sess_list {
   1002 	iscsi_sess_t		*session;
   1003 	struct iscsi_sess_list	*next;
   1004 } iscsi_sess_list_t;
   1005 
   1006 /*
   1007  * iSCSI client notify task context for deferred IDM notifications processing
   1008  */
   1009 typedef struct iscsi_cn_task {
   1010 	idm_conn_t		*ct_ic;
   1011 	idm_client_notify_t	ct_icn;
   1012 	uintptr_t		ct_data;
   1013 } iscsi_cn_task_t;
   1014 
   1015 /*
   1016  * iscsi_network
   1017  */
   1018 typedef struct iscsi_network {
   1019 	void* (*socket)(int domain, int, int);
   1020 	int (*bind)(void *, struct sockaddr *, int, int, int);
   1021 	int (*connect)(void *, struct sockaddr *, int, int, int);
   1022 	int (*listen)(void *, int);
   1023 	void* (*accept)(void *, struct sockaddr *, int *);
   1024 	int (*getsockname)(void *, struct sockaddr *, socklen_t *);
   1025 	int (*getsockopt)(void *, int, int, void *, int *, int);
   1026 	int (*setsockopt)(void *, int, int, void *, int);
   1027 	int (*shutdown)(void *, int);
   1028 	void (*close)(void *);
   1029 
   1030 	size_t (*poll)(void *, clock_t);
   1031 	size_t (*sendmsg)(void *, struct msghdr *);
   1032 	size_t (*recvmsg)(void *, struct msghdr *, int);
   1033 
   1034 	iscsi_status_t (*sendpdu)(void *, iscsi_hdr_t *, char *, int);
   1035 	iscsi_status_t (*recvdata)(void *, iscsi_hdr_t *, char *,
   1036 	    int, int, int);
   1037 	iscsi_status_t (*recvhdr)(void *, iscsi_hdr_t *, int, int, int);
   1038 
   1039 	struct {
   1040 		int			sndbuf;
   1041 		int			rcvbuf;
   1042 		int			nodelay;
   1043 		int			conn_notify_threshold;
   1044 		int			conn_abort_threshold;
   1045 		int			abort_threshold;
   1046 	} tweaks;
   1047 } iscsi_network_t;
   1048 
   1049 #define	ISCSI_NET_HEADER_DIGEST	0x00000001
   1050 #define	ISCSI_NET_DATA_DIGEST	0x00000002
   1051 
   1052 extern iscsi_network_t *iscsi_net;
   1053 
   1054 /*
   1055  * If we get bus_config requests in less than 5 seconds
   1056  * apart skip the name services re-discovery and just
   1057  * complete the requested logins.  This protects against
   1058  * bus_config storms from stale /dev links.
   1059  */
   1060 #define	ISCSI_CONFIG_STORM_DELAY_DEFAULT    5
   1061 
   1062 /*
   1063  * iSCSI HBA Structure
   1064  */
   1065 typedef struct iscsi_hba {
   1066 	uint32_t		hba_sig;
   1067 	dev_info_t		*hba_dip;	/* dev info ptr */
   1068 	scsi_hba_tran_t		*hba_tran;	/* scsi tran ptr */
   1069 	ldi_ident_t		hba_li;
   1070 
   1071 	struct iscsi_sess	*hba_sess_list;	/* sess. list for hba */
   1072 	krwlock_t		hba_sess_list_rwlock; /* protect sess. list */
   1073 
   1074 	/* lbolt of the last time we received a config request */
   1075 	clock_t			hba_config_lbolt;
   1076 	/* current number of seconds to protect against bus config storms */
   1077 	int			hba_config_storm_delay;
   1078 
   1079 	/* general iSCSI protocol hba/initiator info */
   1080 	uchar_t			hba_name[ISCSI_MAX_NAME_LEN];
   1081 	int			hba_name_length;
   1082 	uchar_t			hba_alias[ISCSI_MAX_NAME_LEN];
   1083 	int			hba_alias_length;
   1084 
   1085 	/* Default SessionID for HBA */
   1086 	uchar_t			hba_isid[ISCSI_ISID_LEN];
   1087 
   1088 	/* Default HBA wide settings */
   1089 	iscsi_login_params_t	hba_params;
   1090 
   1091 	/*
   1092 	 * There's only one HBA and it's set to ISCSI_INITIATOR_OID
   1093 	 * (value of 1) at the beginning of time.
   1094 	 */
   1095 	uint32_t		hba_oid;
   1096 
   1097 	/*
   1098 	 * Keep track of which events have been sent. User daemons request
   1099 	 * this information so they don't wait for events which they won't
   1100 	 * see.
   1101 	 */
   1102 	kmutex_t		hba_discovery_events_mutex;
   1103 	iSCSIDiscoveryMethod_t  hba_discovery_events;
   1104 	boolean_t		hba_discovery_in_progress;
   1105 
   1106 	boolean_t		hba_mpxio_enabled; /* mpxio-enabled */
   1107 	/* if the persistent store is loaded */
   1108 	boolean_t		hba_persistent_loaded;
   1109 
   1110 	/*
   1111 	 * Ensures only one SendTargets operation occurs at a time
   1112 	 */
   1113 	ksema_t			hba_sendtgts_semaphore;
   1114 
   1115 	/*
   1116 	 * Statistics
   1117 	 */
   1118 	struct {
   1119 		kstat_t			*ks;
   1120 		iscsi_hba_stats_t	ks_data;
   1121 	} stats;
   1122 
   1123 	/*
   1124 	 * track/control the service status and client
   1125 	 *
   1126 	 * service- service online ensures the operational of cli
   1127 	 *	  - and the availability of iSCSI discovery/devices
   1128 	 *	  - so obviously offline means the unusable of cli
   1129 	 *	  - , disabling of all discovery methods and to offline
   1130 	 *	  - all discovered devices
   1131 	 *
   1132 	 * client - here the client actually means 'exclusive client'
   1133 	 *	  - for operations these clients take may conflict
   1134 	 *	  - with the changing of service status and therefore
   1135 	 *	  - need to be exclusive
   1136 	 *
   1137 	 * The service has three status:
   1138 	 * 	ISCSI_SERVICE_ENABLED	 -	client is permitted to
   1139 	 *				 -	request service
   1140 	 *
   1141 	 *	ISCSI_SERVICE_DISABLED	 -	client is not permitted to
   1142 	 *				 -	request service
   1143 	 *
   1144 	 *	ISCSI_SERVICE_TRANSITION -	client must wait for
   1145 	 *				 -	one of above two statuses
   1146 	 *
   1147 	 * The hba_service_client_count tracks the number of
   1148 	 * current clients, it increases with new clients and decreases
   1149 	 * with leaving clients. It stops to increase once the
   1150 	 * ISCSI_SERVICE_TRANSITION is set, and causes later clients be
   1151 	 * blocked there.
   1152 	 *
   1153 	 * The status of the service can only be changed when the number
   1154 	 * of current clients reaches zero.
   1155 	 *
   1156 	 * Clients include:
   1157 	 *	iscsi_ioctl
   1158 	 *	iscsi_tran_bus_config
   1159 	 *	iscsi_tran_bus_unconfig
   1160 	 *	isns_scn_callback
   1161 	 */
   1162 	kmutex_t		hba_service_lock;
   1163 	kcondvar_t		hba_service_cv;
   1164 	uint32_t		hba_service_status;
   1165 	uint32_t		hba_service_client_count;
   1166 
   1167 	/* Default HBA tunable settings */
   1168 	iscsi_tunable_params_t  hba_tunable_params;
   1169 	boolean_t		hba_service_status_overwrite;
   1170 } iscsi_hba_t;
   1171 
   1172 /*
   1173  * +--------------------------------------------------------------------+
   1174  * | iSCSI prototypes							|
   1175  * +--------------------------------------------------------------------+
   1176  */
   1177 
   1178 /* IDM client callback entry points */
   1179 idm_rx_pdu_cb_t iscsi_rx_scsi_rsp;
   1180 idm_rx_pdu_cb_t iscsi_rx_misc_pdu;
   1181 idm_rx_pdu_error_cb_t iscsi_rx_error_pdu;
   1182 idm_build_hdr_cb_t iscsi_build_hdr;
   1183 idm_task_cb_t iscsi_task_aborted;
   1184 idm_client_notify_cb_t iscsi_client_notify;
   1185 
   1186 /* iscsi_io.c */
   1187 int iscsi_sna_lte(uint32_t n1, uint32_t n2);
   1188 char *iscsi_get_next_text(char *data, int data_length, char *curr_text);
   1189 
   1190 void iscsi_ic_thread(iscsi_thread_t *thread, void *arg);
   1191 void iscsi_tx_thread(iscsi_thread_t *thread, void *arg);
   1192 void iscsi_wd_thread(iscsi_thread_t *thread, void *arg);
   1193 
   1194 iscsi_status_t iscsi_tx_cmd(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
   1195 
   1196 void iscsi_task_cleanup(int opcode, iscsi_cmd_t *icmdp);
   1197 
   1198 void iscsi_handle_abort(void *arg);
   1199 iscsi_status_t iscsi_handle_reset(iscsi_sess_t *isp, int level,
   1200     iscsi_lun_t *ilp);
   1201 iscsi_status_t iscsi_handle_logout(iscsi_conn_t *icp);
   1202 iscsi_status_t iscsi_handle_passthru(iscsi_sess_t *isp, uint16_t lun,
   1203     struct uscsi_cmd *ucmdp);
   1204 iscsi_status_t iscsi_handle_text(iscsi_conn_t *icp,
   1205     char *buf, uint32_t buf_len, uint32_t data_len, uint32_t *rx_data_len);
   1206 
   1207 void iscsi_iodone(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
   1208 
   1209 /* iscsi_crc.c */
   1210 uint32_t iscsi_crc32c(void *address, unsigned long length);
   1211 uint32_t iscsi_crc32c_continued(void *address, unsigned long length,
   1212     uint32_t crc);
   1213 
   1214 /* iscsi_queue.c */
   1215 void iscsi_init_queue(iscsi_queue_t *queue);
   1216 void iscsi_destroy_queue(iscsi_queue_t *queue);
   1217 void iscsi_enqueue_pending_cmd(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
   1218 void iscsi_dequeue_pending_cmd(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
   1219 void iscsi_enqueue_active_cmd(iscsi_conn_t *icp, iscsi_cmd_t *icmdp);
   1220 void iscsi_dequeue_active_cmd(iscsi_conn_t *icp, iscsi_cmd_t *icmdp);
   1221 void iscsi_enqueue_idm_aborting_cmd(iscsi_conn_t *icp, iscsi_cmd_t *icmdp);
   1222 void iscsi_dequeue_idm_aborting_cmd(iscsi_conn_t *icp, iscsi_cmd_t *icmdp);
   1223 void iscsi_enqueue_completed_cmd(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
   1224 iscsi_status_t iscsi_dequeue_cmd(iscsi_cmd_t **, iscsi_cmd_t **, iscsi_cmd_t *);
   1225 void iscsi_move_queue(iscsi_queue_t *src_queue, iscsi_queue_t *dst_queue);
   1226 void iscsi_enqueue_cmd_head(iscsi_cmd_t **, iscsi_cmd_t **,
   1227     iscsi_cmd_t *);
   1228 
   1229 /* iscsi_login.c */
   1230 iscsi_status_t iscsi_login_start(void *arg);
   1231 void iscsi_login_update_state(iscsi_conn_t *icp,
   1232     iscsi_login_state_t next_state);
   1233 void iscsi_login_update_state_locked(iscsi_conn_t *icp,
   1234     iscsi_login_state_t next_state);
   1235 
   1236 
   1237 /* iscsi_stats.c */
   1238 boolean_t iscsi_hba_kstat_init(struct iscsi_hba	*ihp);
   1239 boolean_t iscsi_hba_kstat_term(struct iscsi_hba	*ihp);
   1240 boolean_t iscsi_sess_kstat_init(struct iscsi_sess *isp);
   1241 boolean_t iscsi_sess_kstat_term(struct iscsi_sess *isp);
   1242 boolean_t iscsi_conn_kstat_init(struct iscsi_conn	*icp);
   1243 void iscsi_conn_kstat_term(struct iscsi_conn *icp);
   1244 
   1245 /* iscsi_net.c */
   1246 void iscsi_net_init();
   1247 void iscsi_net_fini();
   1248 iscsi_status_t iscsi_net_interface();
   1249 
   1250 /* iscsi_sess.c */
   1251 iscsi_sess_t *iscsi_sess_create(iscsi_hba_t *ihp,
   1252     iSCSIDiscoveryMethod_t method, struct sockaddr *addr_dsc,
   1253     char *target_name, int tpgt, uchar_t isid_lsb,
   1254     iscsi_sess_type_t type, uint32_t *oid);
   1255 void iscsi_sess_online(void *arg);
   1256 int iscsi_sess_get(uint32_t oid, iscsi_hba_t *ihp, iscsi_sess_t **ispp);
   1257 iscsi_status_t iscsi_sess_destroy(iscsi_sess_t *isp);
   1258 void iscsi_sess_state_machine(iscsi_sess_t *isp, iscsi_sess_event_t event);
   1259 char *iscsi_sess_state_str(iscsi_sess_state_t state);
   1260 boolean_t iscsi_sess_set_auth(iscsi_sess_t *isp);
   1261 iscsi_status_t iscsi_sess_reserve_scsi_itt(iscsi_cmd_t *icmdp);
   1262 void iscsi_sess_release_scsi_itt(iscsi_cmd_t *icmdp);
   1263 iscsi_status_t iscsi_sess_reserve_itt(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
   1264 void iscsi_sess_release_itt(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
   1265 void iscsi_sess_redrive_io(iscsi_sess_t *isp);
   1266 int iscsi_sess_get_by_target(uint32_t target_oid, iscsi_hba_t *ihp,
   1267 	iscsi_sess_t **ispp);
   1268 
   1269 
   1270 /* iscsi_conn.c */
   1271 iscsi_status_t iscsi_conn_create(struct sockaddr *addr, iscsi_sess_t *isp,
   1272     iscsi_conn_t **icpp);
   1273 iscsi_status_t iscsi_conn_online(iscsi_conn_t *icp);
   1274 iscsi_status_t iscsi_conn_offline(iscsi_conn_t *icp);
   1275 iscsi_status_t iscsi_conn_destroy(iscsi_conn_t *icp);
   1276 void iscsi_conn_set_login_min_max(iscsi_conn_t *icp, int min, int max);
   1277 iscsi_status_t iscsi_conn_sync_params(iscsi_conn_t *icp);
   1278 void iscsi_conn_retry(iscsi_sess_t *isp, iscsi_conn_t *icp);
   1279 void iscsi_conn_update_state(iscsi_conn_t *icp, iscsi_conn_state_t next_state);
   1280 void iscsi_conn_update_state_locked(iscsi_conn_t *icp,
   1281 			iscsi_conn_state_t next_state);
   1282 
   1283 /* iscsi_lun.c */
   1284 iscsi_status_t iscsi_lun_create(iscsi_sess_t *isp, uint16_t lun_num,
   1285     uint8_t lun_addr_type, struct scsi_inquiry *inq, char *guid);
   1286 iscsi_status_t iscsi_lun_destroy(iscsi_hba_t *ihp,
   1287     iscsi_lun_t *ilp);
   1288 void iscsi_lun_online(iscsi_hba_t *ihp,
   1289     iscsi_lun_t *ilp);
   1290 iscsi_status_t iscsi_lun_offline(iscsi_hba_t *ihp,
   1291     iscsi_lun_t *ilp, boolean_t lun_free);
   1292 
   1293 /* iscsi_cmd.c */
   1294 void iscsi_cmd_state_machine(iscsi_cmd_t *icmdp,
   1295     iscsi_cmd_event_t event, void *arg);
   1296 iscsi_cmd_t	*iscsi_cmd_alloc(iscsi_conn_t *icp, int km_flags);
   1297 void		iscsi_cmd_free(iscsi_cmd_t *icmdp);
   1298 
   1299 /* iscsi_ioctl.c */
   1300 void * iscsi_ioctl_copyin(caddr_t arg, int mode, size_t size);
   1301 int iscsi_ioctl_copyout(void *data, size_t size, caddr_t arg, int mode);
   1302 iscsi_conn_list_t *iscsi_ioctl_conn_oid_list_get_copyin(caddr_t, int);
   1303 int iscsi_ioctl_conn_oid_list_get_copyout(iscsi_conn_list_t *, caddr_t, int);
   1304 boolean_t iscsi_ioctl_conn_oid_list_get(iscsi_hba_t *ihp,
   1305     iscsi_conn_list_t *cl);
   1306 boolean_t iscsi_ioctl_conn_props_get(iscsi_hba_t *ihp, iscsi_conn_props_t *cp);
   1307 int iscsi_ioctl_sendtgts_get(iscsi_hba_t *ihp, iscsi_sendtgts_list_t *stl);
   1308 int iscsi_target_prop_mod(iscsi_hba_t *, iscsi_property_t *, int cmd);
   1309 int iscsi_set_params(iscsi_param_set_t *, iscsi_hba_t *, boolean_t);
   1310 int iscsi_get_persisted_param(uchar_t *, iscsi_param_get_t *,
   1311     iscsi_login_params_t *);
   1312 void iscsi_set_default_login_params(iscsi_login_params_t *params);
   1313 int iscsi_ioctl_get_config_sess(iscsi_hba_t *ihp,
   1314     iscsi_config_sess_t *ics);
   1315 int iscsi_ioctl_set_config_sess(iscsi_hba_t *ihp,
   1316     iscsi_config_sess_t *ics);
   1317 int iscsi_ioctl_set_tunable_param(iscsi_hba_t *ihp,
   1318     iscsi_tunable_object_t *tpss);
   1319 /* ioctls  prototypes */
   1320 int iscsi_get_param(iscsi_login_params_t *params,
   1321     boolean_t valid_flag,
   1322     iscsi_param_get_t *ipgp);
   1323 
   1324 /* iscsid.c */
   1325 boolean_t iscsid_init(iscsi_hba_t *ihp);
   1326 boolean_t iscsid_start(iscsi_hba_t *ihp);
   1327 boolean_t iscsid_stop(iscsi_hba_t *ihp);
   1328 void iscsid_fini();
   1329 void iscsid_props(iSCSIDiscoveryProperties_t *props);
   1330 boolean_t iscsid_enable_discovery(iscsi_hba_t *ihp,
   1331     iSCSIDiscoveryMethod_t idm, boolean_t poke);
   1332 boolean_t iscsid_disable_discovery(iscsi_hba_t *ihp,
   1333     iSCSIDiscoveryMethod_t idm);
   1334 void iscsid_poke_discovery(iscsi_hba_t *ihp, iSCSIDiscoveryMethod_t method);
   1335 void iscsid_do_sendtgts(entry_t *discovery_addr);
   1336 void iscsid_do_isns_query_one_server(
   1337     iscsi_hba_t *ihp, entry_t *isns_addr);
   1338 void iscsid_do_isns_query(iscsi_hba_t *ihp);
   1339 void iscsid_config_one(iscsi_hba_t *ihp,
   1340     char *name, boolean_t protect);
   1341 void iscsid_config_all(iscsi_hba_t *ihp, boolean_t protect);
   1342 void iscsid_unconfig_one(iscsi_hba_t *ihp, char *name);
   1343 void iscsid_unconfig_all(iscsi_hba_t *ihp);
   1344 void isns_scn_callback(void *arg);
   1345 boolean_t iscsid_del(iscsi_hba_t *ihp, char *target_name,
   1346     iSCSIDiscoveryMethod_t method, struct sockaddr *addr_dsc);
   1347 boolean_t iscsid_login_tgt(iscsi_hba_t *ihp, char *target_name,
   1348     iSCSIDiscoveryMethod_t method, struct sockaddr *addr_dsc);
   1349 void iscsid_addr_to_sockaddr(int src_insize, void *src_addr, int src_port,
   1350     struct sockaddr *dst_addr);
   1351 void iscsid_set_default_initiator_node_settings(iscsi_hba_t *ihp,
   1352     boolean_t minimal);
   1353 
   1354 void iscsi_send_sysevent(iscsi_hba_t *ihp, char *eventcalss,
   1355     char *subclass, nvlist_t *np);
   1356 boolean_t iscsi_reconfig_boot_sess(iscsi_hba_t *ihp);
   1357 boolean_t iscsi_chk_bootlun_mpxio(iscsi_hba_t *ihp);
   1358 boolean_t iscsi_cmp_boot_ini_name(char *name);
   1359 boolean_t iscsi_cmp_boot_tgt_name(char *name);
   1360 boolean_t iscsi_client_request_service(iscsi_hba_t *ihp);
   1361 void iscsi_client_release_service(iscsi_hba_t *ihp);
   1362 
   1363 extern void bcopy(const void *s1, void *s2, size_t n);
   1364 extern void bzero(void *s, size_t n);
   1365 /*
   1366  * Here we need a contract for inet_ntop() and inet_pton()
   1367  * in uts/common/inet/ip/inet_ntop.c
   1368  */
   1369 extern char *inet_ntop(int af, const void *addr, char *buf, int addrlen);
   1370 extern int inet_pton(int af, char *inp, void *outp);
   1371 
   1372 #ifdef __cplusplus
   1373 }
   1374 #endif
   1375 
   1376 #endif /* _ISCSI_H */
   1377