1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * RFC 3720 (iSCSI) protocol data types
5 * Copyright (C) 2005 Dmitry Yusupov
6 * Copyright (C) 2005 Alex Aizman
7 * maintained by open-iscsi@googlegroups.com
13 #include <linux/types.h>
14 #include <scsi/scsi.h>
16 #define ISCSI_DRAFT20_VERSION 0x00
18 /* default iSCSI listen port for incoming connections */
19 #define ISCSI_LISTEN_PORT 3260
21 /* iSCSI header length */
22 #define ISCSI_HDR_LEN 48
24 /* iSCSI CRC32C length */
25 #define ISCSI_CRC_LEN 4
27 /* Padding word length */
28 #define ISCSI_PAD_LEN 4
31 * Serial Number Arithmetic, 32 bits, RFC1982
34 static inline int iscsi_sna_lt(u32 n1, u32 n2)
36 return (s32)(n1 - n2) < 0;
39 static inline int iscsi_sna_lte(u32 n1, u32 n2)
41 return (s32)(n1 - n2) <= 0;
44 static inline int iscsi_sna_gt(u32 n1, u32 n2)
46 return (s32)(n1 - n2) > 0;
49 static inline int iscsi_sna_gte(u32 n1, u32 n2)
51 return (s32)(n1 - n2) >= 0;
55 * useful common(control and data paths) macro
57 #define ntoh24(p) (((p)[0] << 16) | ((p)[1] << 8) | ((p)[2]))
58 #define hton24(p, v) { \
59 p[0] = (((v) >> 16) & 0xFF); \
60 p[1] = (((v) >> 8) & 0xFF); \
61 p[2] = ((v) & 0xFF); \
63 #define zero_data(p) {p[0]=0;p[1]=0;p[2]=0;}
65 /* initiator tags; opaque for target */
66 typedef uint32_t __bitwise itt_t;
67 /* below makes sense only for initiator that created this tag */
68 #define build_itt(itt, age) ((__force itt_t)\
69 ((itt) | ((age) << ISCSI_AGE_SHIFT)))
70 #define get_itt(itt) ((__force uint32_t)(itt_t)(itt) & ISCSI_ITT_MASK)
71 #define RESERVED_ITT ((__force itt_t)0xffffffff)
74 * iSCSI Template Message Header
78 uint8_t flags; /* Final bit */
80 uint8_t hlength; /* AHSs total length */
81 uint8_t dlength[3]; /* Data length */
83 itt_t itt; /* Initiator Task Tag, opaque for target */
84 __be32 ttt; /* Target Task Tag */
91 /************************* RFC 3720 Begin *****************************/
93 #define ISCSI_RESERVED_TAG 0xffffffff
95 /* Opcode encoding bits */
96 #define ISCSI_OP_RETRY 0x80
97 #define ISCSI_OP_IMMEDIATE 0x40
98 #define ISCSI_OPCODE_MASK 0x3F
100 /* Initiator Opcode values */
101 #define ISCSI_OP_NOOP_OUT 0x00
102 #define ISCSI_OP_SCSI_CMD 0x01
103 #define ISCSI_OP_SCSI_TMFUNC 0x02
104 #define ISCSI_OP_LOGIN 0x03
105 #define ISCSI_OP_TEXT 0x04
106 #define ISCSI_OP_SCSI_DATA_OUT 0x05
107 #define ISCSI_OP_LOGOUT 0x06
108 #define ISCSI_OP_SNACK 0x10
110 #define ISCSI_OP_VENDOR1_CMD 0x1c
111 #define ISCSI_OP_VENDOR2_CMD 0x1d
112 #define ISCSI_OP_VENDOR3_CMD 0x1e
113 #define ISCSI_OP_VENDOR4_CMD 0x1f
115 /* Target Opcode values */
116 #define ISCSI_OP_NOOP_IN 0x20
117 #define ISCSI_OP_SCSI_CMD_RSP 0x21
118 #define ISCSI_OP_SCSI_TMFUNC_RSP 0x22
119 #define ISCSI_OP_LOGIN_RSP 0x23
120 #define ISCSI_OP_TEXT_RSP 0x24
121 #define ISCSI_OP_SCSI_DATA_IN 0x25
122 #define ISCSI_OP_LOGOUT_RSP 0x26
123 #define ISCSI_OP_R2T 0x31
124 #define ISCSI_OP_ASYNC_EVENT 0x32
125 #define ISCSI_OP_REJECT 0x3f
127 struct iscsi_ahs_hdr {
133 #define ISCSI_AHSTYPE_CDB 1
134 #define ISCSI_AHSTYPE_RLENGTH 2
135 #define ISCSI_CDB_SIZE 16
137 /* iSCSI PDU Header */
138 struct iscsi_scsi_req {
145 itt_t itt; /* Initiator Task Tag */
149 uint8_t cdb[ISCSI_CDB_SIZE]; /* SCSI Command Block */
150 /* Additional Data (Command Dependent) */
153 /* Command PDU flags */
154 #define ISCSI_FLAG_CMD_FINAL 0x80
155 #define ISCSI_FLAG_CMD_READ 0x40
156 #define ISCSI_FLAG_CMD_WRITE 0x20
157 #define ISCSI_FLAG_CMD_ATTR_MASK 0x07 /* 3 bits */
159 /* SCSI Command Attribute values */
160 #define ISCSI_ATTR_UNTAGGED 0
161 #define ISCSI_ATTR_SIMPLE 1
162 #define ISCSI_ATTR_ORDERED 2
163 #define ISCSI_ATTR_HEAD_OF_QUEUE 3
164 #define ISCSI_ATTR_ACA 4
166 struct iscsi_rlength_ahdr {
173 /* Extended CDB AHS */
174 struct iscsi_ecdb_ahdr {
175 __be16 ahslength; /* CDB length - 15, including reserved byte */
178 /* 4-byte aligned extended CDB spillover */
179 uint8_t ecdb[SCSI_MAX_VARLEN_CDB_SIZE - ISCSI_CDB_SIZE];
182 /* SCSI Response Header */
183 struct iscsi_scsi_rsp {
191 itt_t itt; /* Initiator Task Tag */
197 __be32 bi_residual_count;
198 __be32 residual_count;
199 /* Response or Sense Data (optional) */
202 /* Command Response PDU flags */
203 #define ISCSI_FLAG_CMD_BIDI_OVERFLOW 0x10
204 #define ISCSI_FLAG_CMD_BIDI_UNDERFLOW 0x08
205 #define ISCSI_FLAG_CMD_OVERFLOW 0x04
206 #define ISCSI_FLAG_CMD_UNDERFLOW 0x02
208 /* iSCSI Status values. Valid if Rsp Selector bit is not set */
209 #define ISCSI_STATUS_CMD_COMPLETED 0
210 #define ISCSI_STATUS_TARGET_FAILURE 1
211 #define ISCSI_STATUS_SUBSYS_FAILURE 2
213 /* Asynchronous Event Header */
233 /* iSCSI Event Codes */
234 #define ISCSI_ASYNC_MSG_SCSI_EVENT 0
235 #define ISCSI_ASYNC_MSG_REQUEST_LOGOUT 1
236 #define ISCSI_ASYNC_MSG_DROPPING_CONNECTION 2
237 #define ISCSI_ASYNC_MSG_DROPPING_ALL_CONNECTIONS 3
238 #define ISCSI_ASYNC_MSG_PARAM_NEGOTIATION 4
239 #define ISCSI_ASYNC_MSG_VENDOR_SPECIFIC 255
241 /* NOP-Out Message */
242 struct iscsi_nopout {
249 itt_t itt; /* Initiator Task Tag */
250 __be32 ttt; /* Target Transfer Tag */
264 itt_t itt; /* Initiator Task Tag */
265 __be32 ttt; /* Target Transfer Tag */
272 /* SCSI Task Management Message Header */
280 itt_t itt; /* Initiator Task Tag */
281 itt_t rtt; /* Reference Task Tag */
289 #define ISCSI_FLAG_TM_FUNC_MASK 0x7F
291 /* Function values */
292 #define ISCSI_TM_FUNC_ABORT_TASK 1
293 #define ISCSI_TM_FUNC_ABORT_TASK_SET 2
294 #define ISCSI_TM_FUNC_CLEAR_ACA 3
295 #define ISCSI_TM_FUNC_CLEAR_TASK_SET 4
296 #define ISCSI_TM_FUNC_LOGICAL_UNIT_RESET 5
297 #define ISCSI_TM_FUNC_TARGET_WARM_RESET 6
298 #define ISCSI_TM_FUNC_TARGET_COLD_RESET 7
299 #define ISCSI_TM_FUNC_TASK_REASSIGN 8
301 #define ISCSI_TM_FUNC_VALUE(hdr) ((hdr)->flags & ISCSI_FLAG_TM_FUNC_MASK)
303 /* SCSI Task Management Response Header */
304 struct iscsi_tm_rsp {
307 uint8_t response; /* see Response values below */
312 itt_t itt; /* Initiator Task Tag */
313 itt_t rtt; /* Reference Task Tag */
320 /* Response values */
321 #define ISCSI_TMF_RSP_COMPLETE 0x00
322 #define ISCSI_TMF_RSP_NO_TASK 0x01
323 #define ISCSI_TMF_RSP_NO_LUN 0x02
324 #define ISCSI_TMF_RSP_TASK_ALLEGIANT 0x03
325 #define ISCSI_TMF_RSP_NO_FAILOVER 0x04
326 #define ISCSI_TMF_RSP_NOT_SUPPORTED 0x05
327 #define ISCSI_TMF_RSP_AUTH_FAILED 0x06
328 #define ISCSI_TMF_RSP_REJECTED 0xff
330 /* Ready To Transfer Header */
331 struct iscsi_r2t_rsp {
338 itt_t itt; /* Initiator Task Tag */
339 __be32 ttt; /* Target Transfer Tag */
367 /* SCSI Data Response Hdr */
368 struct iscsi_data_rsp {
383 __be32 residual_count;
386 /* Data Response PDU flags */
387 #define ISCSI_FLAG_DATA_ACK 0x40
388 #define ISCSI_FLAG_DATA_OVERFLOW 0x04
389 #define ISCSI_FLAG_DATA_UNDERFLOW 0x02
390 #define ISCSI_FLAG_DATA_STATUS 0x01
405 /* Text - key=value pairs */
408 #define ISCSI_FLAG_TEXT_CONTINUE 0x40
410 /* Text Response Header */
411 struct iscsi_text_rsp {
424 /* Text Response - key:value pairs */
428 struct iscsi_login_req {
431 uint8_t max_version; /* Max. version supported */
432 uint8_t min_version; /* Min. version supported */
435 uint8_t isid[6]; /* Initiator Session ID */
436 __be16 tsih; /* Target Session Handle */
437 itt_t itt; /* Initiator Task Tag */
445 /* Login PDU flags */
446 #define ISCSI_FLAG_LOGIN_TRANSIT 0x80
447 #define ISCSI_FLAG_LOGIN_CONTINUE 0x40
448 #define ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK 0x0C /* 2 bits */
449 #define ISCSI_FLAG_LOGIN_CURRENT_STAGE1 0x04
450 #define ISCSI_FLAG_LOGIN_CURRENT_STAGE2 0x08
451 #define ISCSI_FLAG_LOGIN_CURRENT_STAGE3 0x0C
452 #define ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK 0x03 /* 2 bits */
453 #define ISCSI_FLAG_LOGIN_NEXT_STAGE1 0x01
454 #define ISCSI_FLAG_LOGIN_NEXT_STAGE2 0x02
455 #define ISCSI_FLAG_LOGIN_NEXT_STAGE3 0x03
457 #define ISCSI_LOGIN_CURRENT_STAGE(flags) \
458 ((flags & ISCSI_FLAG_LOGIN_CURRENT_STAGE_MASK) >> 2)
459 #define ISCSI_LOGIN_NEXT_STAGE(flags) \
460 (flags & ISCSI_FLAG_LOGIN_NEXT_STAGE_MASK)
462 /* Login Response Header */
463 struct iscsi_login_rsp {
466 uint8_t max_version; /* Max. version supported */
467 uint8_t active_version; /* Active version */
470 uint8_t isid[6]; /* Initiator Session ID */
471 __be16 tsih; /* Target Session Handle */
472 itt_t itt; /* Initiator Task Tag */
477 uint8_t status_class; /* see Login RSP ststus classes below */
478 uint8_t status_detail; /* see Login RSP Status details below */
482 /* Login stage (phase) codes for CSG, NSG */
483 #define ISCSI_INITIAL_LOGIN_STAGE -1
484 #define ISCSI_SECURITY_NEGOTIATION_STAGE 0
485 #define ISCSI_OP_PARMS_NEGOTIATION_STAGE 1
486 #define ISCSI_FULL_FEATURE_PHASE 3
488 /* Login Status response classes */
489 #define ISCSI_STATUS_CLS_SUCCESS 0x00
490 #define ISCSI_STATUS_CLS_REDIRECT 0x01
491 #define ISCSI_STATUS_CLS_INITIATOR_ERR 0x02
492 #define ISCSI_STATUS_CLS_TARGET_ERR 0x03
494 /* Login Status response detail codes */
495 /* Class-0 (Success) */
496 #define ISCSI_LOGIN_STATUS_ACCEPT 0x00
498 /* Class-1 (Redirection) */
499 #define ISCSI_LOGIN_STATUS_TGT_MOVED_TEMP 0x01
500 #define ISCSI_LOGIN_STATUS_TGT_MOVED_PERM 0x02
502 /* Class-2 (Initiator Error) */
503 #define ISCSI_LOGIN_STATUS_INIT_ERR 0x00
504 #define ISCSI_LOGIN_STATUS_AUTH_FAILED 0x01
505 #define ISCSI_LOGIN_STATUS_TGT_FORBIDDEN 0x02
506 #define ISCSI_LOGIN_STATUS_TGT_NOT_FOUND 0x03
507 #define ISCSI_LOGIN_STATUS_TGT_REMOVED 0x04
508 #define ISCSI_LOGIN_STATUS_NO_VERSION 0x05
509 #define ISCSI_LOGIN_STATUS_ISID_ERROR 0x06
510 #define ISCSI_LOGIN_STATUS_MISSING_FIELDS 0x07
511 #define ISCSI_LOGIN_STATUS_CONN_ADD_FAILED 0x08
512 #define ISCSI_LOGIN_STATUS_NO_SESSION_TYPE 0x09
513 #define ISCSI_LOGIN_STATUS_NO_SESSION 0x0a
514 #define ISCSI_LOGIN_STATUS_INVALID_REQUEST 0x0b
516 /* Class-3 (Target Error) */
517 #define ISCSI_LOGIN_STATUS_TARGET_ERROR 0x00
518 #define ISCSI_LOGIN_STATUS_SVC_UNAVAILABLE 0x01
519 #define ISCSI_LOGIN_STATUS_NO_RESOURCES 0x02
522 struct iscsi_logout {
529 itt_t itt; /* Initiator Task Tag */
537 /* Logout PDU flags */
538 #define ISCSI_FLAG_LOGOUT_REASON_MASK 0x7F
540 /* logout reason_code values */
542 #define ISCSI_LOGOUT_REASON_CLOSE_SESSION 0
543 #define ISCSI_LOGOUT_REASON_CLOSE_CONNECTION 1
544 #define ISCSI_LOGOUT_REASON_RECOVERY 2
545 #define ISCSI_LOGOUT_REASON_AEN_REQUEST 3
547 /* Logout Response Header */
548 struct iscsi_logout_rsp {
551 uint8_t response; /* see Logout response values below */
556 itt_t itt; /* Initiator Task Tag */
567 /* logout response status values */
569 #define ISCSI_LOGOUT_SUCCESS 0
570 #define ISCSI_LOGOUT_CID_NOT_FOUND 1
571 #define ISCSI_LOGOUT_RECOVERY_UNSUPPORTED 2
572 #define ISCSI_LOGOUT_CLEANUP_FAILED 3
591 /* SNACK PDU flags */
592 #define ISCSI_FLAG_SNACK_TYPE_DATA 0
593 #define ISCSI_FLAG_SNACK_TYPE_R2T 0
594 #define ISCSI_FLAG_SNACK_TYPE_STATUS 1
595 #define ISCSI_FLAG_SNACK_TYPE_DATA_ACK 2
596 #define ISCSI_FLAG_SNACK_TYPE_RDATA 3
597 #define ISCSI_FLAG_SNACK_TYPE_MASK 0x0F /* 4 bits */
599 /* Reject Message Header */
600 struct iscsi_reject {
615 /* Text - Rejected hdr */
618 /* Reason for Reject */
619 #define ISCSI_REASON_CMD_BEFORE_LOGIN 1
620 #define ISCSI_REASON_DATA_DIGEST_ERROR 2
621 #define ISCSI_REASON_DATA_SNACK_REJECT 3
622 #define ISCSI_REASON_PROTOCOL_ERROR 4
623 #define ISCSI_REASON_CMD_NOT_SUPPORTED 5
624 #define ISCSI_REASON_IMM_CMD_REJECT 6
625 #define ISCSI_REASON_TASK_IN_PROGRESS 7
626 #define ISCSI_REASON_INVALID_SNACK 8
627 #define ISCSI_REASON_BOOKMARK_INVALID 9
628 #define ISCSI_REASON_BOOKMARK_NO_RESOURCES 10
629 #define ISCSI_REASON_NEGOTIATION_RESET 11
631 /* Max. number of Key=Value pairs in a text message */
632 #define MAX_KEY_VALUE_PAIRS 8192
634 /* maximum length for text keys/values */
635 #define KEY_MAXLEN 64
636 #define VALUE_MAXLEN 255
637 #define TARGET_NAME_MAXLEN VALUE_MAXLEN
639 #define ISCSI_DEF_MAX_RECV_SEG_LEN 8192
640 #define ISCSI_MIN_MAX_RECV_SEG_LEN 512
641 #define ISCSI_MAX_MAX_RECV_SEG_LEN 16777215
643 #define ISCSI_DEF_FIRST_BURST_LEN 65536
644 #define ISCSI_MIN_FIRST_BURST_LEN 512
645 #define ISCSI_MAX_FIRST_BURST_LEN 16777215
647 #define ISCSI_DEF_MAX_BURST_LEN 262144
648 #define ISCSI_MIN_MAX_BURST_LEN 512
649 #define ISCSI_MAX_MAX_BURST_LEN 16777215
651 #define ISCSI_DEF_TIME2WAIT 2
653 #define ISCSI_NAME_LEN 224
655 /************************* RFC 3720 End *****************************/
657 #endif /* ISCSI_PROTO_H */