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