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