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