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