1 #ifndef ISCSI_TARGET_CORE_H
2 #define ISCSI_TARGET_CORE_H
3 
4 #include <linux/in.h>
5 #include <linux/configfs.h>
6 #include <net/sock.h>
7 #include <net/tcp.h>
8 #include <scsi/scsi_cmnd.h>
9 #include <scsi/iscsi_proto.h>
10 #include <target/target_core_base.h>
11 
12 #define ISCSIT_VERSION			"v4.1.0"
13 #define ISCSI_MAX_DATASN_MISSING_COUNT	16
14 #define ISCSI_TX_THREAD_TCP_TIMEOUT	2
15 #define ISCSI_RX_THREAD_TCP_TIMEOUT	2
16 #define SECONDS_FOR_ASYNC_LOGOUT	10
17 #define SECONDS_FOR_ASYNC_TEXT		10
18 #define SECONDS_FOR_LOGOUT_COMP		15
19 #define WHITE_SPACE			" \t\v\f\n\r"
20 #define ISCSIT_MIN_TAGS			16
21 #define ISCSIT_EXTRA_TAGS		8
22 #define ISCSIT_TCP_BACKLOG		256
23 #define ISCSI_RX_THREAD_NAME		"iscsi_trx"
24 #define ISCSI_TX_THREAD_NAME		"iscsi_ttx"
25 
26 /* struct iscsi_node_attrib sanity values */
27 #define NA_DATAOUT_TIMEOUT		3
28 #define NA_DATAOUT_TIMEOUT_MAX		60
29 #define NA_DATAOUT_TIMEOUT_MIX		2
30 #define NA_DATAOUT_TIMEOUT_RETRIES	5
31 #define NA_DATAOUT_TIMEOUT_RETRIES_MAX	15
32 #define NA_DATAOUT_TIMEOUT_RETRIES_MIN	1
33 #define NA_NOPIN_TIMEOUT		15
34 #define NA_NOPIN_TIMEOUT_MAX		60
35 #define NA_NOPIN_TIMEOUT_MIN		3
36 #define NA_NOPIN_RESPONSE_TIMEOUT	30
37 #define NA_NOPIN_RESPONSE_TIMEOUT_MAX	60
38 #define NA_NOPIN_RESPONSE_TIMEOUT_MIN	3
39 #define NA_RANDOM_DATAIN_PDU_OFFSETS	0
40 #define NA_RANDOM_DATAIN_SEQ_OFFSETS	0
41 #define NA_RANDOM_R2T_OFFSETS		0
42 
43 /* struct iscsi_tpg_attrib sanity values */
44 #define TA_AUTHENTICATION		1
45 #define TA_LOGIN_TIMEOUT		15
46 #define TA_LOGIN_TIMEOUT_MAX		30
47 #define TA_LOGIN_TIMEOUT_MIN		5
48 #define TA_NETIF_TIMEOUT		2
49 #define TA_NETIF_TIMEOUT_MAX		15
50 #define TA_NETIF_TIMEOUT_MIN		2
51 #define TA_GENERATE_NODE_ACLS		0
52 #define TA_DEFAULT_CMDSN_DEPTH		64
53 #define TA_DEFAULT_CMDSN_DEPTH_MAX	512
54 #define TA_DEFAULT_CMDSN_DEPTH_MIN	1
55 #define TA_CACHE_DYNAMIC_ACLS		0
56 /* Enabled by default in demo mode (generic_node_acls=1) */
57 #define TA_DEMO_MODE_WRITE_PROTECT	1
58 /* Disabled by default in production mode w/ explict ACLs */
59 #define TA_PROD_MODE_WRITE_PROTECT	0
60 #define TA_DEMO_MODE_DISCOVERY		1
61 #define TA_DEFAULT_ERL			0
62 #define TA_CACHE_CORE_NPS		0
63 /* T10 protection information disabled by default */
64 #define TA_DEFAULT_T10_PI		0
65 #define TA_DEFAULT_FABRIC_PROT_TYPE	0
66 
67 #define ISCSI_IOV_DATA_BUFFER		5
68 
69 enum iscsit_transport_type {
70 	ISCSI_TCP				= 0,
71 	ISCSI_SCTP_TCP				= 1,
72 	ISCSI_SCTP_UDP				= 2,
73 	ISCSI_IWARP_TCP				= 3,
74 	ISCSI_IWARP_SCTP			= 4,
75 	ISCSI_INFINIBAND			= 5,
76 };
77 
78 /* RFC-3720 7.1.4  Standard Connection State Diagram for a Target */
79 enum target_conn_state_table {
80 	TARG_CONN_STATE_FREE			= 0x1,
81 	TARG_CONN_STATE_XPT_UP			= 0x3,
82 	TARG_CONN_STATE_IN_LOGIN		= 0x4,
83 	TARG_CONN_STATE_LOGGED_IN		= 0x5,
84 	TARG_CONN_STATE_IN_LOGOUT		= 0x6,
85 	TARG_CONN_STATE_LOGOUT_REQUESTED	= 0x7,
86 	TARG_CONN_STATE_CLEANUP_WAIT		= 0x8,
87 };
88 
89 /* RFC-3720 7.3.2  Session State Diagram for a Target */
90 enum target_sess_state_table {
91 	TARG_SESS_STATE_FREE			= 0x1,
92 	TARG_SESS_STATE_ACTIVE			= 0x2,
93 	TARG_SESS_STATE_LOGGED_IN		= 0x3,
94 	TARG_SESS_STATE_FAILED			= 0x4,
95 	TARG_SESS_STATE_IN_CONTINUE		= 0x5,
96 };
97 
98 /* struct iscsi_data_count->type */
99 enum data_count_type {
100 	ISCSI_RX_DATA	= 1,
101 	ISCSI_TX_DATA	= 2,
102 };
103 
104 /* struct iscsi_datain_req->dr_complete */
105 enum datain_req_comp_table {
106 	DATAIN_COMPLETE_NORMAL			= 1,
107 	DATAIN_COMPLETE_WITHIN_COMMAND_RECOVERY = 2,
108 	DATAIN_COMPLETE_CONNECTION_RECOVERY	= 3,
109 };
110 
111 /* struct iscsi_datain_req->recovery */
112 enum datain_req_rec_table {
113 	DATAIN_WITHIN_COMMAND_RECOVERY		= 1,
114 	DATAIN_CONNECTION_RECOVERY		= 2,
115 };
116 
117 /* struct iscsi_portal_group->state */
118 enum tpg_state_table {
119 	TPG_STATE_FREE				= 0,
120 	TPG_STATE_ACTIVE			= 1,
121 	TPG_STATE_INACTIVE			= 2,
122 	TPG_STATE_COLD_RESET			= 3,
123 };
124 
125 /* struct iscsi_tiqn->tiqn_state */
126 enum tiqn_state_table {
127 	TIQN_STATE_ACTIVE			= 1,
128 	TIQN_STATE_SHUTDOWN			= 2,
129 };
130 
131 /* struct iscsi_cmd->cmd_flags */
132 enum cmd_flags_table {
133 	ICF_GOT_LAST_DATAOUT			= 0x00000001,
134 	ICF_GOT_DATACK_SNACK			= 0x00000002,
135 	ICF_NON_IMMEDIATE_UNSOLICITED_DATA	= 0x00000004,
136 	ICF_SENT_LAST_R2T			= 0x00000008,
137 	ICF_WITHIN_COMMAND_RECOVERY		= 0x00000010,
138 	ICF_CONTIG_MEMORY			= 0x00000020,
139 	ICF_ATTACHED_TO_RQUEUE			= 0x00000040,
140 	ICF_OOO_CMDSN				= 0x00000080,
141 	ICF_SENDTARGETS_ALL			= 0x00000100,
142 	ICF_SENDTARGETS_SINGLE			= 0x00000200,
143 };
144 
145 /* struct iscsi_cmd->i_state */
146 enum cmd_i_state_table {
147 	ISTATE_NO_STATE			= 0,
148 	ISTATE_NEW_CMD			= 1,
149 	ISTATE_DEFERRED_CMD		= 2,
150 	ISTATE_UNSOLICITED_DATA		= 3,
151 	ISTATE_RECEIVE_DATAOUT		= 4,
152 	ISTATE_RECEIVE_DATAOUT_RECOVERY	= 5,
153 	ISTATE_RECEIVED_LAST_DATAOUT	= 6,
154 	ISTATE_WITHIN_DATAOUT_RECOVERY	= 7,
155 	ISTATE_IN_CONNECTION_RECOVERY	= 8,
156 	ISTATE_RECEIVED_TASKMGT		= 9,
157 	ISTATE_SEND_ASYNCMSG		= 10,
158 	ISTATE_SENT_ASYNCMSG		= 11,
159 	ISTATE_SEND_DATAIN		= 12,
160 	ISTATE_SEND_LAST_DATAIN		= 13,
161 	ISTATE_SENT_LAST_DATAIN		= 14,
162 	ISTATE_SEND_LOGOUTRSP		= 15,
163 	ISTATE_SENT_LOGOUTRSP		= 16,
164 	ISTATE_SEND_NOPIN		= 17,
165 	ISTATE_SENT_NOPIN		= 18,
166 	ISTATE_SEND_REJECT		= 19,
167 	ISTATE_SENT_REJECT		= 20,
168 	ISTATE_SEND_R2T			= 21,
169 	ISTATE_SENT_R2T			= 22,
170 	ISTATE_SEND_R2T_RECOVERY	= 23,
171 	ISTATE_SENT_R2T_RECOVERY	= 24,
172 	ISTATE_SEND_LAST_R2T		= 25,
173 	ISTATE_SENT_LAST_R2T		= 26,
174 	ISTATE_SEND_LAST_R2T_RECOVERY	= 27,
175 	ISTATE_SENT_LAST_R2T_RECOVERY	= 28,
176 	ISTATE_SEND_STATUS		= 29,
177 	ISTATE_SEND_STATUS_BROKEN_PC	= 30,
178 	ISTATE_SENT_STATUS		= 31,
179 	ISTATE_SEND_STATUS_RECOVERY	= 32,
180 	ISTATE_SENT_STATUS_RECOVERY	= 33,
181 	ISTATE_SEND_TASKMGTRSP		= 34,
182 	ISTATE_SENT_TASKMGTRSP		= 35,
183 	ISTATE_SEND_TEXTRSP		= 36,
184 	ISTATE_SENT_TEXTRSP		= 37,
185 	ISTATE_SEND_NOPIN_WANT_RESPONSE	= 38,
186 	ISTATE_SENT_NOPIN_WANT_RESPONSE	= 39,
187 	ISTATE_SEND_NOPIN_NO_RESPONSE	= 40,
188 	ISTATE_REMOVE			= 41,
189 	ISTATE_FREE			= 42,
190 };
191 
192 /* Used for iscsi_recover_cmdsn() return values */
193 enum recover_cmdsn_ret_table {
194 	CMDSN_ERROR_CANNOT_RECOVER	= -1,
195 	CMDSN_NORMAL_OPERATION		= 0,
196 	CMDSN_LOWER_THAN_EXP		= 1,
197 	CMDSN_HIGHER_THAN_EXP		= 2,
198 	CMDSN_MAXCMDSN_OVERRUN		= 3,
199 };
200 
201 /* Used for iscsi_handle_immediate_data() return values */
202 enum immedate_data_ret_table {
203 	IMMEDIATE_DATA_CANNOT_RECOVER	= -1,
204 	IMMEDIATE_DATA_NORMAL_OPERATION = 0,
205 	IMMEDIATE_DATA_ERL1_CRC_FAILURE = 1,
206 };
207 
208 /* Used for iscsi_decide_dataout_action() return values */
209 enum dataout_action_ret_table {
210 	DATAOUT_CANNOT_RECOVER		= -1,
211 	DATAOUT_NORMAL			= 0,
212 	DATAOUT_SEND_R2T		= 1,
213 	DATAOUT_SEND_TO_TRANSPORT	= 2,
214 	DATAOUT_WITHIN_COMMAND_RECOVERY = 3,
215 };
216 
217 /* Used for struct iscsi_node_auth->naf_flags */
218 enum naf_flags_table {
219 	NAF_USERID_SET			= 0x01,
220 	NAF_PASSWORD_SET		= 0x02,
221 	NAF_USERID_IN_SET		= 0x04,
222 	NAF_PASSWORD_IN_SET		= 0x08,
223 };
224 
225 /* Used by various struct timer_list to manage iSCSI specific state */
226 enum iscsi_timer_flags_table {
227 	ISCSI_TF_RUNNING		= 0x01,
228 	ISCSI_TF_STOP			= 0x02,
229 	ISCSI_TF_EXPIRED		= 0x04,
230 };
231 
232 /* Used for struct iscsi_np->np_flags */
233 enum np_flags_table {
234 	NPF_IP_NETWORK		= 0x00,
235 };
236 
237 /* Used for struct iscsi_np->np_thread_state */
238 enum np_thread_state_table {
239 	ISCSI_NP_THREAD_ACTIVE		= 1,
240 	ISCSI_NP_THREAD_INACTIVE	= 2,
241 	ISCSI_NP_THREAD_RESET		= 3,
242 	ISCSI_NP_THREAD_SHUTDOWN	= 4,
243 	ISCSI_NP_THREAD_EXIT		= 5,
244 };
245 
246 struct iscsi_conn_ops {
247 	u8	HeaderDigest;			/* [0,1] == [None,CRC32C] */
248 	u8	DataDigest;			/* [0,1] == [None,CRC32C] */
249 	u32	MaxRecvDataSegmentLength;	/* [512..2**24-1] */
250 	u32	MaxXmitDataSegmentLength;	/* [512..2**24-1] */
251 	u8	OFMarker;			/* [0,1] == [No,Yes] */
252 	u8	IFMarker;			/* [0,1] == [No,Yes] */
253 	u32	OFMarkInt;			/* [1..65535] */
254 	u32	IFMarkInt;			/* [1..65535] */
255 	/*
256 	 * iSER specific connection parameters
257 	 */
258 	u32	InitiatorRecvDataSegmentLength;	/* [512..2**24-1] */
259 	u32	TargetRecvDataSegmentLength;	/* [512..2**24-1] */
260 };
261 
262 struct iscsi_sess_ops {
263 	char	InitiatorName[224];
264 	char	InitiatorAlias[256];
265 	char	TargetName[224];
266 	char	TargetAlias[256];
267 	char	TargetAddress[256];
268 	u16	TargetPortalGroupTag;		/* [0..65535] */
269 	u16	MaxConnections;			/* [1..65535] */
270 	u8	InitialR2T;			/* [0,1] == [No,Yes] */
271 	u8	ImmediateData;			/* [0,1] == [No,Yes] */
272 	u32	MaxBurstLength;			/* [512..2**24-1] */
273 	u32	FirstBurstLength;		/* [512..2**24-1] */
274 	u16	DefaultTime2Wait;		/* [0..3600] */
275 	u16	DefaultTime2Retain;		/* [0..3600] */
276 	u16	MaxOutstandingR2T;		/* [1..65535] */
277 	u8	DataPDUInOrder;			/* [0,1] == [No,Yes] */
278 	u8	DataSequenceInOrder;		/* [0,1] == [No,Yes] */
279 	u8	ErrorRecoveryLevel;		/* [0..2] */
280 	u8	SessionType;			/* [0,1] == [Normal,Discovery]*/
281 	/*
282 	 * iSER specific session parameters
283 	 */
284 	u8	RDMAExtensions;			/* [0,1] == [No,Yes] */
285 };
286 
287 struct iscsi_queue_req {
288 	int			state;
289 	struct iscsi_cmd	*cmd;
290 	struct list_head	qr_list;
291 };
292 
293 struct iscsi_data_count {
294 	int			data_length;
295 	int			sync_and_steering;
296 	enum data_count_type	type;
297 	u32			iov_count;
298 	u32			ss_iov_count;
299 	u32			ss_marker_count;
300 	struct kvec		*iov;
301 };
302 
303 struct iscsi_param_list {
304 	bool			iser;
305 	struct list_head	param_list;
306 	struct list_head	extra_response_list;
307 };
308 
309 struct iscsi_datain_req {
310 	enum datain_req_comp_table dr_complete;
311 	int			generate_recovery_values;
312 	enum datain_req_rec_table recovery;
313 	u32			begrun;
314 	u32			runlength;
315 	u32			data_length;
316 	u32			data_offset;
317 	u32			data_sn;
318 	u32			next_burst_len;
319 	u32			read_data_done;
320 	u32			seq_send_order;
321 	struct list_head	cmd_datain_node;
322 } ____cacheline_aligned;
323 
324 struct iscsi_ooo_cmdsn {
325 	u16			cid;
326 	u32			batch_count;
327 	u32			cmdsn;
328 	u32			exp_cmdsn;
329 	struct iscsi_cmd	*cmd;
330 	struct list_head	ooo_list;
331 } ____cacheline_aligned;
332 
333 struct iscsi_datain {
334 	u8			flags;
335 	u32			data_sn;
336 	u32			length;
337 	u32			offset;
338 } ____cacheline_aligned;
339 
340 struct iscsi_r2t {
341 	int			seq_complete;
342 	int			recovery_r2t;
343 	int			sent_r2t;
344 	u32			r2t_sn;
345 	u32			offset;
346 	u32			targ_xfer_tag;
347 	u32			xfer_len;
348 	struct list_head	r2t_list;
349 } ____cacheline_aligned;
350 
351 struct iscsi_cmd {
352 	enum iscsi_timer_flags_table dataout_timer_flags;
353 	/* DataOUT timeout retries */
354 	u8			dataout_timeout_retries;
355 	/* Within command recovery count */
356 	u8			error_recovery_count;
357 	/* iSCSI dependent state for out or order CmdSNs */
358 	enum cmd_i_state_table	deferred_i_state;
359 	/* iSCSI dependent state */
360 	enum cmd_i_state_table	i_state;
361 	/* Command is an immediate command (ISCSI_OP_IMMEDIATE set) */
362 	u8			immediate_cmd;
363 	/* Immediate data present */
364 	u8			immediate_data;
365 	/* iSCSI Opcode */
366 	u8			iscsi_opcode;
367 	/* iSCSI Response Code */
368 	u8			iscsi_response;
369 	/* Logout reason when iscsi_opcode == ISCSI_INIT_LOGOUT_CMND */
370 	u8			logout_reason;
371 	/* Logout response code when iscsi_opcode == ISCSI_INIT_LOGOUT_CMND */
372 	u8			logout_response;
373 	/* MaxCmdSN has been incremented */
374 	u8			maxcmdsn_inc;
375 	/* Immediate Unsolicited Dataout */
376 	u8			unsolicited_data;
377 	/* Reject reason code */
378 	u8			reject_reason;
379 	/* CID contained in logout PDU when opcode == ISCSI_INIT_LOGOUT_CMND */
380 	u16			logout_cid;
381 	/* Command flags */
382 	enum cmd_flags_table	cmd_flags;
383 	/* Initiator Task Tag assigned from Initiator */
384 	itt_t			init_task_tag;
385 	/* Target Transfer Tag assigned from Target */
386 	u32			targ_xfer_tag;
387 	/* CmdSN assigned from Initiator */
388 	u32			cmd_sn;
389 	/* ExpStatSN assigned from Initiator */
390 	u32			exp_stat_sn;
391 	/* StatSN assigned to this ITT */
392 	u32			stat_sn;
393 	/* DataSN Counter */
394 	u32			data_sn;
395 	/* R2TSN Counter */
396 	u32			r2t_sn;
397 	/* Last DataSN acknowledged via DataAck SNACK */
398 	u32			acked_data_sn;
399 	/* Used for echoing NOPOUT ping data */
400 	u32			buf_ptr_size;
401 	/* Used to store DataDigest */
402 	u32			data_crc;
403 	/* Counter for MaxOutstandingR2T */
404 	u32			outstanding_r2ts;
405 	/* Next R2T Offset when DataSequenceInOrder=Yes */
406 	u32			r2t_offset;
407 	/* Iovec current and orig count for iscsi_cmd->iov_data */
408 	u32			iov_data_count;
409 	u32			orig_iov_data_count;
410 	/* Number of miscellaneous iovecs used for IP stack calls */
411 	u32			iov_misc_count;
412 	/* Number of struct iscsi_pdu in struct iscsi_cmd->pdu_list */
413 	u32			pdu_count;
414 	/* Next struct iscsi_pdu to send in struct iscsi_cmd->pdu_list */
415 	u32			pdu_send_order;
416 	/* Current struct iscsi_pdu in struct iscsi_cmd->pdu_list */
417 	u32			pdu_start;
418 	/* Next struct iscsi_seq to send in struct iscsi_cmd->seq_list */
419 	u32			seq_send_order;
420 	/* Number of struct iscsi_seq in struct iscsi_cmd->seq_list */
421 	u32			seq_count;
422 	/* Current struct iscsi_seq in struct iscsi_cmd->seq_list */
423 	u32			seq_no;
424 	/* Lowest offset in current DataOUT sequence */
425 	u32			seq_start_offset;
426 	/* Highest offset in current DataOUT sequence */
427 	u32			seq_end_offset;
428 	/* Total size in bytes received so far of READ data */
429 	u32			read_data_done;
430 	/* Total size in bytes received so far of WRITE data */
431 	u32			write_data_done;
432 	/* Counter for FirstBurstLength key */
433 	u32			first_burst_len;
434 	/* Counter for MaxBurstLength key */
435 	u32			next_burst_len;
436 	/* Transfer size used for IP stack calls */
437 	u32			tx_size;
438 	/* Buffer used for various purposes */
439 	void			*buf_ptr;
440 	/* Used by SendTargets=[iqn.,eui.] discovery */
441 	void			*text_in_ptr;
442 	/* See include/linux/dma-mapping.h */
443 	enum dma_data_direction	data_direction;
444 	/* iSCSI PDU Header + CRC */
445 	unsigned char		pdu[ISCSI_HDR_LEN + ISCSI_CRC_LEN];
446 	/* Number of times struct iscsi_cmd is present in immediate queue */
447 	atomic_t		immed_queue_count;
448 	atomic_t		response_queue_count;
449 	spinlock_t		datain_lock;
450 	spinlock_t		dataout_timeout_lock;
451 	/* spinlock for protecting struct iscsi_cmd->i_state */
452 	spinlock_t		istate_lock;
453 	/* spinlock for adding within command recovery entries */
454 	spinlock_t		error_lock;
455 	/* spinlock for adding R2Ts */
456 	spinlock_t		r2t_lock;
457 	/* DataIN List */
458 	struct list_head	datain_list;
459 	/* R2T List */
460 	struct list_head	cmd_r2t_list;
461 	/* Timer for DataOUT */
462 	struct timer_list	dataout_timer;
463 	/* Iovecs for SCSI data payload RX/TX w/ kernel level sockets */
464 	struct kvec		*iov_data;
465 	/* Iovecs for miscellaneous purposes */
466 #define ISCSI_MISC_IOVECS			5
467 	struct kvec		iov_misc[ISCSI_MISC_IOVECS];
468 	/* Array of struct iscsi_pdu used for DataPDUInOrder=No */
469 	struct iscsi_pdu	*pdu_list;
470 	/* Current struct iscsi_pdu used for DataPDUInOrder=No */
471 	struct iscsi_pdu	*pdu_ptr;
472 	/* Array of struct iscsi_seq used for DataSequenceInOrder=No */
473 	struct iscsi_seq	*seq_list;
474 	/* Current struct iscsi_seq used for DataSequenceInOrder=No */
475 	struct iscsi_seq	*seq_ptr;
476 	/* TMR Request when iscsi_opcode == ISCSI_OP_SCSI_TMFUNC */
477 	struct iscsi_tmr_req	*tmr_req;
478 	/* Connection this command is alligient to */
479 	struct iscsi_conn	*conn;
480 	/* Pointer to connection recovery entry */
481 	struct iscsi_conn_recovery *cr;
482 	/* Session the command is part of,  used for connection recovery */
483 	struct iscsi_session	*sess;
484 	/* list_head for connection list */
485 	struct list_head	i_conn_node;
486 	/* The TCM I/O descriptor that is accessed via container_of() */
487 	struct se_cmd		se_cmd;
488 	/* Sense buffer that will be mapped into outgoing status */
489 #define ISCSI_SENSE_BUFFER_LEN          (TRANSPORT_SENSE_BUFFER + 2)
490 	unsigned char		sense_buffer[ISCSI_SENSE_BUFFER_LEN];
491 
492 	u32			padding;
493 	u8			pad_bytes[4];
494 
495 	struct scatterlist	*first_data_sg;
496 	u32			first_data_sg_off;
497 	u32			kmapped_nents;
498 	sense_reason_t		sense_reason;
499 }  ____cacheline_aligned;
500 
501 struct iscsi_tmr_req {
502 	bool			task_reassign:1;
503 	u32			exp_data_sn;
504 	struct iscsi_cmd	*ref_cmd;
505 	struct iscsi_conn_recovery *conn_recovery;
506 	struct se_tmr_req	*se_tmr_req;
507 };
508 
509 struct iscsi_conn {
510 	wait_queue_head_t	queues_wq;
511 	/* Authentication Successful for this connection */
512 	u8			auth_complete;
513 	/* State connection is currently in */
514 	u8			conn_state;
515 	u8			conn_logout_reason;
516 	u8			network_transport;
517 	enum iscsi_timer_flags_table nopin_timer_flags;
518 	enum iscsi_timer_flags_table nopin_response_timer_flags;
519 	/* Used to know what thread encountered a transport failure */
520 	u8			which_thread;
521 	/* connection id assigned by the Initiator */
522 	u16			cid;
523 	/* Remote TCP Port */
524 	u16			login_port;
525 	u16			local_port;
526 	int			net_size;
527 	int			login_family;
528 	u32			auth_id;
529 	u32			conn_flags;
530 	/* Used for iscsi_tx_login_rsp() */
531 	itt_t			login_itt;
532 	u32			exp_statsn;
533 	/* Per connection status sequence number */
534 	u32			stat_sn;
535 	/* IFMarkInt's Current Value */
536 	u32			if_marker;
537 	/* OFMarkInt's Current Value */
538 	u32			of_marker;
539 	/* Used for calculating OFMarker offset to next PDU */
540 	u32			of_marker_offset;
541 #define IPV6_ADDRESS_SPACE				48
542 	unsigned char		login_ip[IPV6_ADDRESS_SPACE];
543 	unsigned char		local_ip[IPV6_ADDRESS_SPACE];
544 	int			conn_usage_count;
545 	int			conn_waiting_on_uc;
546 	atomic_t		check_immediate_queue;
547 	atomic_t		conn_logout_remove;
548 	atomic_t		connection_exit;
549 	atomic_t		connection_recovery;
550 	atomic_t		connection_reinstatement;
551 	atomic_t		connection_wait_rcfr;
552 	atomic_t		sleep_on_conn_wait_comp;
553 	atomic_t		transport_failed;
554 	struct completion	conn_post_wait_comp;
555 	struct completion	conn_wait_comp;
556 	struct completion	conn_wait_rcfr_comp;
557 	struct completion	conn_waiting_on_uc_comp;
558 	struct completion	conn_logout_comp;
559 	struct completion	tx_half_close_comp;
560 	struct completion	rx_half_close_comp;
561 	/* socket used by this connection */
562 	struct socket		*sock;
563 	void			(*orig_data_ready)(struct sock *);
564 	void			(*orig_state_change)(struct sock *);
565 #define LOGIN_FLAGS_READ_ACTIVE		1
566 #define LOGIN_FLAGS_CLOSED		2
567 #define LOGIN_FLAGS_READY		4
568 	unsigned long		login_flags;
569 	struct delayed_work	login_work;
570 	struct delayed_work	login_cleanup_work;
571 	struct iscsi_login	*login;
572 	struct timer_list	nopin_timer;
573 	struct timer_list	nopin_response_timer;
574 	struct timer_list	transport_timer;
575 	struct task_struct	*login_kworker;
576 	/* Spinlock used for add/deleting cmd's from conn_cmd_list */
577 	spinlock_t		cmd_lock;
578 	spinlock_t		conn_usage_lock;
579 	spinlock_t		immed_queue_lock;
580 	spinlock_t		nopin_timer_lock;
581 	spinlock_t		response_queue_lock;
582 	spinlock_t		state_lock;
583 	/* libcrypto RX and TX contexts for crc32c */
584 	struct hash_desc	conn_rx_hash;
585 	struct hash_desc	conn_tx_hash;
586 	/* Used for scheduling TX and RX connection kthreads */
587 	cpumask_var_t		conn_cpumask;
588 	unsigned int		conn_rx_reset_cpumask:1;
589 	unsigned int		conn_tx_reset_cpumask:1;
590 	/* list_head of struct iscsi_cmd for this connection */
591 	struct list_head	conn_cmd_list;
592 	struct list_head	immed_queue_list;
593 	struct list_head	response_queue_list;
594 	struct iscsi_conn_ops	*conn_ops;
595 	struct iscsi_login	*conn_login;
596 	struct iscsit_transport *conn_transport;
597 	struct iscsi_param_list	*param_list;
598 	/* Used for per connection auth state machine */
599 	void			*auth_protocol;
600 	void			*context;
601 	struct iscsi_login_thread_s *login_thread;
602 	struct iscsi_portal_group *tpg;
603 	struct iscsi_tpg_np	*tpg_np;
604 	/* Pointer to parent session */
605 	struct iscsi_session	*sess;
606 	int			bitmap_id;
607 	int			rx_thread_active;
608 	struct task_struct	*rx_thread;
609 	int			tx_thread_active;
610 	struct task_struct	*tx_thread;
611 	/* list_head for session connection list */
612 	struct list_head	conn_list;
613 } ____cacheline_aligned;
614 
615 struct iscsi_conn_recovery {
616 	u16			cid;
617 	u32			cmd_count;
618 	u32			maxrecvdatasegmentlength;
619 	u32			maxxmitdatasegmentlength;
620 	int			ready_for_reallegiance;
621 	struct list_head	conn_recovery_cmd_list;
622 	spinlock_t		conn_recovery_cmd_lock;
623 	struct timer_list	time2retain_timer;
624 	struct iscsi_session	*sess;
625 	struct list_head	cr_list;
626 }  ____cacheline_aligned;
627 
628 struct iscsi_session {
629 	u8			initiator_vendor;
630 	u8			isid[6];
631 	enum iscsi_timer_flags_table time2retain_timer_flags;
632 	u8			version_active;
633 	u16			cid_called;
634 	u16			conn_recovery_count;
635 	u16			tsih;
636 	/* state session is currently in */
637 	u32			session_state;
638 	/* session wide counter: initiator assigned task tag */
639 	itt_t			init_task_tag;
640 	/* session wide counter: target assigned task tag */
641 	u32			targ_xfer_tag;
642 	u32			cmdsn_window;
643 
644 	/* protects cmdsn values */
645 	struct mutex		cmdsn_mutex;
646 	/* session wide counter: expected command sequence number */
647 	u32			exp_cmd_sn;
648 	/* session wide counter: maximum allowed command sequence number */
649 	u32			max_cmd_sn;
650 	struct list_head	sess_ooo_cmdsn_list;
651 
652 	/* LIO specific session ID */
653 	u32			sid;
654 	char			auth_type[8];
655 	/* unique within the target */
656 	int			session_index;
657 	/* Used for session reference counting */
658 	int			session_usage_count;
659 	int			session_waiting_on_uc;
660 	atomic_long_t		cmd_pdus;
661 	atomic_long_t		rsp_pdus;
662 	atomic_long_t		tx_data_octets;
663 	atomic_long_t		rx_data_octets;
664 	atomic_long_t		conn_digest_errors;
665 	atomic_long_t		conn_timeout_errors;
666 	u64			creation_time;
667 	/* Number of active connections */
668 	atomic_t		nconn;
669 	atomic_t		session_continuation;
670 	atomic_t		session_fall_back_to_erl0;
671 	atomic_t		session_logout;
672 	atomic_t		session_reinstatement;
673 	atomic_t		session_stop_active;
674 	atomic_t		sleep_on_sess_wait_comp;
675 	/* connection list */
676 	struct list_head	sess_conn_list;
677 	struct list_head	cr_active_list;
678 	struct list_head	cr_inactive_list;
679 	spinlock_t		conn_lock;
680 	spinlock_t		cr_a_lock;
681 	spinlock_t		cr_i_lock;
682 	spinlock_t		session_usage_lock;
683 	spinlock_t		ttt_lock;
684 	struct completion	async_msg_comp;
685 	struct completion	reinstatement_comp;
686 	struct completion	session_wait_comp;
687 	struct completion	session_waiting_on_uc_comp;
688 	struct timer_list	time2retain_timer;
689 	struct iscsi_sess_ops	*sess_ops;
690 	struct se_session	*se_sess;
691 	struct iscsi_portal_group *tpg;
692 } ____cacheline_aligned;
693 
694 struct iscsi_login {
695 	u8 auth_complete;
696 	u8 checked_for_existing;
697 	u8 current_stage;
698 	u8 leading_connection;
699 	u8 first_request;
700 	u8 version_min;
701 	u8 version_max;
702 	u8 login_complete;
703 	u8 login_failed;
704 	bool zero_tsih;
705 	char isid[6];
706 	u32 cmd_sn;
707 	itt_t init_task_tag;
708 	u32 initial_exp_statsn;
709 	u32 rsp_length;
710 	u16 cid;
711 	u16 tsih;
712 	char req[ISCSI_HDR_LEN];
713 	char rsp[ISCSI_HDR_LEN];
714 	char *req_buf;
715 	char *rsp_buf;
716 	struct iscsi_conn *conn;
717 	struct iscsi_np *np;
718 } ____cacheline_aligned;
719 
720 struct iscsi_node_attrib {
721 	u32			dataout_timeout;
722 	u32			dataout_timeout_retries;
723 	u32			default_erl;
724 	u32			nopin_timeout;
725 	u32			nopin_response_timeout;
726 	u32			random_datain_pdu_offsets;
727 	u32			random_datain_seq_offsets;
728 	u32			random_r2t_offsets;
729 	u32			tmr_cold_reset;
730 	u32			tmr_warm_reset;
731 	struct iscsi_node_acl *nacl;
732 };
733 
734 struct se_dev_entry_s;
735 
736 struct iscsi_node_auth {
737 	enum naf_flags_table	naf_flags;
738 	int			authenticate_target;
739 	/* Used for iscsit_global->discovery_auth,
740 	 * set to zero (auth disabled) by default */
741 	int			enforce_discovery_auth;
742 #define MAX_USER_LEN				256
743 #define MAX_PASS_LEN				256
744 	char			userid[MAX_USER_LEN];
745 	char			password[MAX_PASS_LEN];
746 	char			userid_mutual[MAX_USER_LEN];
747 	char			password_mutual[MAX_PASS_LEN];
748 };
749 
750 #include "iscsi_target_stat.h"
751 
752 struct iscsi_node_stat_grps {
753 	struct config_group	iscsi_sess_stats_group;
754 	struct config_group	iscsi_conn_stats_group;
755 };
756 
757 struct iscsi_node_acl {
758 	struct iscsi_node_attrib node_attrib;
759 	struct iscsi_node_auth	node_auth;
760 	struct iscsi_node_stat_grps node_stat_grps;
761 	struct se_node_acl	se_node_acl;
762 };
763 
764 struct iscsi_tpg_attrib {
765 	u32			authentication;
766 	u32			login_timeout;
767 	u32			netif_timeout;
768 	u32			generate_node_acls;
769 	u32			cache_dynamic_acls;
770 	u32			default_cmdsn_depth;
771 	u32			demo_mode_write_protect;
772 	u32			prod_mode_write_protect;
773 	u32			demo_mode_discovery;
774 	u32			default_erl;
775 	u8			t10_pi;
776 	u32			fabric_prot_type;
777 	struct iscsi_portal_group *tpg;
778 };
779 
780 struct iscsi_np {
781 	int			np_network_transport;
782 	int			np_ip_proto;
783 	int			np_sock_type;
784 	enum np_thread_state_table np_thread_state;
785 	bool                    enabled;
786 	enum iscsi_timer_flags_table np_login_timer_flags;
787 	u32			np_exports;
788 	enum np_flags_table	np_flags;
789 	unsigned char		np_ip[IPV6_ADDRESS_SPACE];
790 	u16			np_port;
791 	spinlock_t		np_thread_lock;
792 	struct completion	np_restart_comp;
793 	struct socket		*np_socket;
794 	struct __kernel_sockaddr_storage np_sockaddr;
795 	struct task_struct	*np_thread;
796 	struct timer_list	np_login_timer;
797 	void			*np_context;
798 	struct iscsit_transport *np_transport;
799 	struct list_head	np_list;
800 } ____cacheline_aligned;
801 
802 struct iscsi_tpg_np {
803 	struct iscsi_np		*tpg_np;
804 	struct iscsi_portal_group *tpg;
805 	struct iscsi_tpg_np	*tpg_np_parent;
806 	struct list_head	tpg_np_list;
807 	struct list_head	tpg_np_child_list;
808 	struct list_head	tpg_np_parent_list;
809 	struct se_tpg_np	se_tpg_np;
810 	spinlock_t		tpg_np_parent_lock;
811 	struct completion	tpg_np_comp;
812 	struct kref		tpg_np_kref;
813 };
814 
815 struct iscsi_portal_group {
816 	unsigned char		tpg_chap_id;
817 	/* TPG State */
818 	enum tpg_state_table	tpg_state;
819 	/* Target Portal Group Tag */
820 	u16			tpgt;
821 	/* Id assigned to target sessions */
822 	u16			ntsih;
823 	/* Number of active sessions */
824 	u32			nsessions;
825 	/* Number of Network Portals available for this TPG */
826 	u32			num_tpg_nps;
827 	/* Per TPG LIO specific session ID. */
828 	u32			sid;
829 	/* Spinlock for adding/removing Network Portals */
830 	spinlock_t		tpg_np_lock;
831 	spinlock_t		tpg_state_lock;
832 	struct se_portal_group tpg_se_tpg;
833 	struct mutex		tpg_access_lock;
834 	struct semaphore	np_login_sem;
835 	struct iscsi_tpg_attrib	tpg_attrib;
836 	struct iscsi_node_auth	tpg_demo_auth;
837 	/* Pointer to default list of iSCSI parameters for TPG */
838 	struct iscsi_param_list	*param_list;
839 	struct iscsi_tiqn	*tpg_tiqn;
840 	struct list_head	tpg_gnp_list;
841 	struct list_head	tpg_list;
842 } ____cacheline_aligned;
843 
844 struct iscsi_wwn_stat_grps {
845 	struct config_group	iscsi_stat_group;
846 	struct config_group	iscsi_instance_group;
847 	struct config_group	iscsi_sess_err_group;
848 	struct config_group	iscsi_tgt_attr_group;
849 	struct config_group	iscsi_login_stats_group;
850 	struct config_group	iscsi_logout_stats_group;
851 };
852 
853 struct iscsi_tiqn {
854 #define ISCSI_IQN_LEN				224
855 	unsigned char		tiqn[ISCSI_IQN_LEN];
856 	enum tiqn_state_table	tiqn_state;
857 	int			tiqn_access_count;
858 	u32			tiqn_active_tpgs;
859 	u32			tiqn_ntpgs;
860 	u32			tiqn_num_tpg_nps;
861 	u32			tiqn_nsessions;
862 	struct list_head	tiqn_list;
863 	struct list_head	tiqn_tpg_list;
864 	spinlock_t		tiqn_state_lock;
865 	spinlock_t		tiqn_tpg_lock;
866 	struct se_wwn		tiqn_wwn;
867 	struct iscsi_wwn_stat_grps tiqn_stat_grps;
868 	int			tiqn_index;
869 	struct iscsi_sess_err_stats  sess_err_stats;
870 	struct iscsi_login_stats     login_stats;
871 	struct iscsi_logout_stats    logout_stats;
872 } ____cacheline_aligned;
873 
874 struct iscsit_global {
875 	/* In core shutdown */
876 	u32			in_shutdown;
877 	u32			active_ts;
878 	/* Unique identifier used for the authentication daemon */
879 	u32			auth_id;
880 	u32			inactive_ts;
881 #define ISCSIT_BITMAP_BITS	262144
882 	/* Thread Set bitmap pointer */
883 	unsigned long		*ts_bitmap;
884 	spinlock_t		ts_bitmap_lock;
885 	/* Used for iSCSI discovery session authentication */
886 	struct iscsi_node_acl	discovery_acl;
887 	struct iscsi_portal_group	*discovery_tpg;
888 };
889 
890 static inline u32 session_get_next_ttt(struct iscsi_session *session)
891 {
892 	u32 ttt;
893 
894 	spin_lock_bh(&session->ttt_lock);
895 	ttt = session->targ_xfer_tag++;
896 	if (ttt == 0xFFFFFFFF)
897 		ttt = session->targ_xfer_tag++;
898 	spin_unlock_bh(&session->ttt_lock);
899 
900 	return ttt;
901 }
902 
903 extern struct iscsi_cmd *iscsit_find_cmd_from_itt(struct iscsi_conn *, itt_t);
904 #endif /* ISCSI_TARGET_CORE_H */
905