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