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