1 /*- 2 * Copyright (c) 2012 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Edward Tomasz Napierala under sponsorship 6 * from the FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #ifndef ISCSI_PROTO_H 33 #define ISCSI_PROTO_H 34 35 #ifndef CTASSERT 36 #define CTASSERT(x) _CTASSERT(x, __LINE__) 37 #define _CTASSERT(x, y) __CTASSERT(x, y) 38 #define __CTASSERT(x, y) typedef char __assert_ ## y [(x) ? 1 : -1] 39 #endif 40 41 #define ISCSI_SNGT(x, y) ((int32_t)(x) - (int32_t)(y) > 0) 42 #define ISCSI_SNLT(x, y) ((int32_t)(x) - (int32_t)(y) < 0) 43 44 #define ISCSI_BHS_SIZE 48 45 #define ISCSI_HEADER_DIGEST_SIZE 4 46 #define ISCSI_DATA_DIGEST_SIZE 4 47 48 #define ISCSI_BHS_OPCODE_IMMEDIATE 0x40 49 50 #define ISCSI_BHS_OPCODE_NOP_OUT 0x00 51 #define ISCSI_BHS_OPCODE_SCSI_COMMAND 0x01 52 #define ISCSI_BHS_OPCODE_TASK_REQUEST 0x02 53 #define ISCSI_BHS_OPCODE_LOGIN_REQUEST 0x03 54 #define ISCSI_BHS_OPCODE_TEXT_REQUEST 0x04 55 #define ISCSI_BHS_OPCODE_SCSI_DATA_OUT 0x05 56 #define ISCSI_BHS_OPCODE_LOGOUT_REQUEST 0x06 57 58 #define ISCSI_BHS_OPCODE_NOP_IN 0x20 59 #define ISCSI_BHS_OPCODE_SCSI_RESPONSE 0x21 60 #define ISCSI_BHS_OPCODE_TASK_RESPONSE 0x22 61 #define ISCSI_BHS_OPCODE_LOGIN_RESPONSE 0x23 62 #define ISCSI_BHS_OPCODE_TEXT_RESPONSE 0x24 63 #define ISCSI_BHS_OPCODE_SCSI_DATA_IN 0x25 64 #define ISCSI_BHS_OPCODE_LOGOUT_RESPONSE 0x26 65 #define ISCSI_BHS_OPCODE_R2T 0x31 66 #define ISCSI_BHS_OPCODE_ASYNC_MESSAGE 0x32 67 #define ISCSI_BHS_OPCODE_REJECT 0x3f 68 69 struct iscsi_bhs { 70 uint8_t bhs_opcode; 71 uint8_t bhs_opcode_specific1[3]; 72 uint8_t bhs_total_ahs_len; 73 uint8_t bhs_data_segment_len[3]; 74 uint64_t bhs_lun; 75 uint8_t bhs_inititator_task_tag[4]; 76 uint8_t bhs_opcode_specific4[28]; 77 }; 78 CTASSERT(sizeof(struct iscsi_bhs) == ISCSI_BHS_SIZE); 79 80 #define BHSSC_FLAGS_F 0x80 81 #define BHSSC_FLAGS_R 0x40 82 #define BHSSC_FLAGS_W 0x20 83 #define BHSSC_FLAGS_ATTR 0x07 84 85 #define BHSSC_FLAGS_ATTR_UNTAGGED 0 86 #define BHSSC_FLAGS_ATTR_SIMPLE 1 87 #define BHSSC_FLAGS_ATTR_ORDERED 2 88 #define BHSSC_FLAGS_ATTR_HOQ 3 89 #define BHSSC_FLAGS_ATTR_ACA 4 90 91 struct iscsi_bhs_scsi_command { 92 uint8_t bhssc_opcode; 93 uint8_t bhssc_flags; 94 uint8_t bhssc_reserved[2]; 95 uint8_t bhssc_total_ahs_len; 96 uint8_t bhssc_data_segment_len[3]; 97 uint64_t bhssc_lun; 98 uint32_t bhssc_initiator_task_tag; 99 uint32_t bhssc_expected_data_transfer_length; 100 uint32_t bhssc_cmdsn; 101 uint32_t bhssc_expstatsn; 102 uint8_t bhssc_cdb[16]; 103 }; 104 CTASSERT(sizeof(struct iscsi_bhs_scsi_command) == ISCSI_BHS_SIZE); 105 106 #define BHSSR_FLAGS_RESIDUAL_UNDERFLOW 0x02 107 #define BHSSR_FLAGS_RESIDUAL_OVERFLOW 0x04 108 109 #define BHSSR_RESPONSE_COMMAND_COMPLETED 0x00 110 111 struct iscsi_bhs_scsi_response { 112 uint8_t bhssr_opcode; 113 uint8_t bhssr_flags; 114 uint8_t bhssr_response; 115 uint8_t bhssr_status; 116 uint8_t bhssr_total_ahs_len; 117 uint8_t bhssr_data_segment_len[3]; 118 uint16_t bhssr_status_qualifier; 119 uint16_t bhssr_reserved; 120 uint32_t bhssr_reserved2; 121 uint32_t bhssr_initiator_task_tag; 122 uint32_t bhssr_snack_tag; 123 uint32_t bhssr_statsn; 124 uint32_t bhssr_expcmdsn; 125 uint32_t bhssr_maxcmdsn; 126 uint32_t bhssr_expdatasn; 127 uint32_t bhssr_bidirectional_read_residual_count; 128 uint32_t bhssr_residual_count; 129 }; 130 CTASSERT(sizeof(struct iscsi_bhs_scsi_response) == ISCSI_BHS_SIZE); 131 132 #define BHSTMR_FUNCTION_ABORT_TASK 1 133 #define BHSTMR_FUNCTION_ABORT_TASK_SET 2 134 #define BHSTMR_FUNCTION_CLEAR_ACA 3 135 #define BHSTMR_FUNCTION_CLEAR_TASK_SET 4 136 #define BHSTMR_FUNCTION_LOGICAL_UNIT_RESET 5 137 #define BHSTMR_FUNCTION_TARGET_WARM_RESET 6 138 #define BHSTMR_FUNCTION_TARGET_COLD_RESET 7 139 #define BHSTMR_FUNCTION_TASK_REASSIGN 8 140 #define BHSTMR_FUNCTION_QUERY_TASK 9 141 #define BHSTMR_FUNCTION_QUERY_TASK_SET 10 142 #define BHSTMR_FUNCTION_I_T_NEXUS_RESET 11 143 #define BHSTMR_FUNCTION_QUERY_ASYNC_EVENT 12 144 145 struct iscsi_bhs_task_management_request { 146 uint8_t bhstmr_opcode; 147 uint8_t bhstmr_function; 148 uint8_t bhstmr_reserved[2]; 149 uint8_t bhstmr_total_ahs_len; 150 uint8_t bhstmr_data_segment_len[3]; 151 uint64_t bhstmr_lun; 152 uint32_t bhstmr_initiator_task_tag; 153 uint32_t bhstmr_referenced_task_tag; 154 uint32_t bhstmr_cmdsn; 155 uint32_t bhstmr_expstatsn; 156 uint32_t bhstmr_refcmdsn; 157 uint32_t bhstmr_expdatasn; 158 uint64_t bhstmr_reserved2; 159 }; 160 CTASSERT(sizeof(struct iscsi_bhs_task_management_request) == ISCSI_BHS_SIZE); 161 162 #define BHSTMR_RESPONSE_FUNCTION_COMPLETE 0 163 #define BHSTMR_RESPONSE_TASK_DOES_NOT_EXIST 1 164 #define BHSTMR_RESPONSE_LUN_DOES_NOT_EXIST 2 165 #define BHSTMR_RESPONSE_TASK_STILL_ALLEGIANT 3 166 #define BHSTMR_RESPONSE_TASK_ALL_REASS_NOT_SUPP 4 167 #define BHSTMR_RESPONSE_FUNCTION_NOT_SUPPORTED 5 168 #define BHSTMR_RESPONSE_FUNCTION_AUTH_FAIL 6 169 #define BHSTMR_RESPONSE_FUNCTION_SUCCEEDED 7 170 #define BHSTMR_RESPONSE_FUNCTION_REJECTED 255 171 172 struct iscsi_bhs_task_management_response { 173 uint8_t bhstmr_opcode; 174 uint8_t bhstmr_flags; 175 uint8_t bhstmr_response; 176 uint8_t bhstmr_reserved; 177 uint8_t bhstmr_total_ahs_len; 178 uint8_t bhstmr_data_segment_len[3]; 179 uint64_t bhstmr_reserved2; 180 uint32_t bhstmr_initiator_task_tag; 181 uint32_t bhstmr_reserved3; 182 uint32_t bhstmr_statsn; 183 uint32_t bhstmr_expcmdsn; 184 uint32_t bhstmr_maxcmdsn; 185 uint8_t bhstmr_reserved4[12]; 186 }; 187 CTASSERT(sizeof(struct iscsi_bhs_task_management_response) == ISCSI_BHS_SIZE); 188 189 #define BHSLR_FLAGS_TRANSIT 0x80 190 #define BHSLR_FLAGS_CONTINUE 0x40 191 192 #define BHSLR_STAGE_SECURITY_NEGOTIATION 0 193 #define BHSLR_STAGE_OPERATIONAL_NEGOTIATION 1 194 #define BHSLR_STAGE_FULL_FEATURE_PHASE 3 /* Yes, 3. */ 195 196 struct iscsi_bhs_login_request { 197 uint8_t bhslr_opcode; 198 uint8_t bhslr_flags; 199 uint8_t bhslr_version_max; 200 uint8_t bhslr_version_min; 201 uint8_t bhslr_total_ahs_len; 202 uint8_t bhslr_data_segment_len[3]; 203 uint8_t bhslr_isid[6]; 204 uint16_t bhslr_tsih; 205 uint32_t bhslr_initiator_task_tag; 206 uint16_t bhslr_cid; 207 uint16_t bhslr_reserved; 208 uint32_t bhslr_cmdsn; 209 uint32_t bhslr_expstatsn; 210 uint8_t bhslr_reserved2[16]; 211 }; 212 CTASSERT(sizeof(struct iscsi_bhs_login_request) == ISCSI_BHS_SIZE); 213 214 struct iscsi_bhs_login_response { 215 uint8_t bhslr_opcode; 216 uint8_t bhslr_flags; 217 uint8_t bhslr_version_max; 218 uint8_t bhslr_version_active; 219 uint8_t bhslr_total_ahs_len; 220 uint8_t bhslr_data_segment_len[3]; 221 uint8_t bhslr_isid[6]; 222 uint16_t bhslr_tsih; 223 uint32_t bhslr_initiator_task_tag; 224 uint32_t bhslr_reserved; 225 uint32_t bhslr_statsn; 226 uint32_t bhslr_expcmdsn; 227 uint32_t bhslr_maxcmdsn; 228 uint8_t bhslr_status_class; 229 uint8_t bhslr_status_detail; 230 uint16_t bhslr_reserved2; 231 uint8_t bhslr_reserved3[8]; 232 }; 233 CTASSERT(sizeof(struct iscsi_bhs_login_response) == ISCSI_BHS_SIZE); 234 235 #define BHSTR_FLAGS_FINAL 0x80 236 #define BHSTR_FLAGS_CONTINUE 0x40 237 238 struct iscsi_bhs_text_request { 239 uint8_t bhstr_opcode; 240 uint8_t bhstr_flags; 241 uint16_t bhstr_reserved; 242 uint8_t bhstr_total_ahs_len; 243 uint8_t bhstr_data_segment_len[3]; 244 uint64_t bhstr_lun; 245 uint32_t bhstr_initiator_task_tag; 246 uint32_t bhstr_target_transfer_tag; 247 uint32_t bhstr_cmdsn; 248 uint32_t bhstr_expstatsn; 249 uint8_t bhstr_reserved2[16]; 250 }; 251 CTASSERT(sizeof(struct iscsi_bhs_text_request) == ISCSI_BHS_SIZE); 252 253 struct iscsi_bhs_text_response { 254 uint8_t bhstr_opcode; 255 uint8_t bhstr_flags; 256 uint16_t bhstr_reserved; 257 uint8_t bhstr_total_ahs_len; 258 uint8_t bhstr_data_segment_len[3]; 259 uint64_t bhstr_lun; 260 uint32_t bhstr_initiator_task_tag; 261 uint32_t bhstr_target_transfer_tag; 262 uint32_t bhstr_statsn; 263 uint32_t bhstr_expcmdsn; 264 uint32_t bhstr_maxcmdsn; 265 uint8_t bhstr_reserved2[12]; 266 }; 267 CTASSERT(sizeof(struct iscsi_bhs_text_response) == ISCSI_BHS_SIZE); 268 269 #define BHSDO_FLAGS_F 0x80 270 271 struct iscsi_bhs_data_out { 272 uint8_t bhsdo_opcode; 273 uint8_t bhsdo_flags; 274 uint8_t bhsdo_reserved[2]; 275 uint8_t bhsdo_total_ahs_len; 276 uint8_t bhsdo_data_segment_len[3]; 277 uint64_t bhsdo_lun; 278 uint32_t bhsdo_initiator_task_tag; 279 uint32_t bhsdo_target_transfer_tag; 280 uint32_t bhsdo_reserved2; 281 uint32_t bhsdo_expstatsn; 282 uint32_t bhsdo_reserved3; 283 uint32_t bhsdo_datasn; 284 uint32_t bhsdo_buffer_offset; 285 uint32_t bhsdo_reserved4; 286 }; 287 CTASSERT(sizeof(struct iscsi_bhs_data_out) == ISCSI_BHS_SIZE); 288 289 #define BHSDI_FLAGS_F 0x80 290 #define BHSDI_FLAGS_A 0x40 291 #define BHSDI_FLAGS_O 0x04 292 #define BHSDI_FLAGS_U 0x02 293 #define BHSDI_FLAGS_S 0x01 294 295 struct iscsi_bhs_data_in { 296 uint8_t bhsdi_opcode; 297 uint8_t bhsdi_flags; 298 uint8_t bhsdi_reserved; 299 uint8_t bhsdi_status; 300 uint8_t bhsdi_total_ahs_len; 301 uint8_t bhsdi_data_segment_len[3]; 302 uint64_t bhsdi_lun; 303 uint32_t bhsdi_initiator_task_tag; 304 uint32_t bhsdi_target_transfer_tag; 305 uint32_t bhsdi_statsn; 306 uint32_t bhsdi_expcmdsn; 307 uint32_t bhsdi_maxcmdsn; 308 uint32_t bhsdi_datasn; 309 uint32_t bhsdi_buffer_offset; 310 uint32_t bhsdi_residual_count; 311 }; 312 CTASSERT(sizeof(struct iscsi_bhs_data_in) == ISCSI_BHS_SIZE); 313 314 struct iscsi_bhs_r2t { 315 uint8_t bhsr2t_opcode; 316 uint8_t bhsr2t_flags; 317 uint16_t bhsr2t_reserved; 318 uint8_t bhsr2t_total_ahs_len; 319 uint8_t bhsr2t_data_segment_len[3]; 320 uint64_t bhsr2t_lun; 321 uint32_t bhsr2t_initiator_task_tag; 322 uint32_t bhsr2t_target_transfer_tag; 323 uint32_t bhsr2t_statsn; 324 uint32_t bhsr2t_expcmdsn; 325 uint32_t bhsr2t_maxcmdsn; 326 uint32_t bhsr2t_r2tsn; 327 uint32_t bhsr2t_buffer_offset; 328 uint32_t bhsr2t_desired_data_transfer_length; 329 }; 330 CTASSERT(sizeof(struct iscsi_bhs_r2t) == ISCSI_BHS_SIZE); 331 332 struct iscsi_bhs_nop_out { 333 uint8_t bhsno_opcode; 334 uint8_t bhsno_flags; 335 uint16_t bhsno_reserved; 336 uint8_t bhsno_total_ahs_len; 337 uint8_t bhsno_data_segment_len[3]; 338 uint64_t bhsno_lun; 339 uint32_t bhsno_initiator_task_tag; 340 uint32_t bhsno_target_transfer_tag; 341 uint32_t bhsno_cmdsn; 342 uint32_t bhsno_expstatsn; 343 uint8_t bhsno_reserved2[16]; 344 }; 345 CTASSERT(sizeof(struct iscsi_bhs_nop_out) == ISCSI_BHS_SIZE); 346 347 struct iscsi_bhs_nop_in { 348 uint8_t bhsni_opcode; 349 uint8_t bhsni_flags; 350 uint16_t bhsni_reserved; 351 uint8_t bhsni_total_ahs_len; 352 uint8_t bhsni_data_segment_len[3]; 353 uint64_t bhsni_lun; 354 uint32_t bhsni_initiator_task_tag; 355 uint32_t bhsni_target_transfer_tag; 356 uint32_t bhsni_statsn; 357 uint32_t bhsni_expcmdsn; 358 uint32_t bhsni_maxcmdsn; 359 uint8_t bhsno_reserved2[12]; 360 }; 361 CTASSERT(sizeof(struct iscsi_bhs_nop_in) == ISCSI_BHS_SIZE); 362 363 #define BHSLR_REASON_CLOSE_SESSION 0 364 #define BHSLR_REASON_CLOSE_CONNECTION 1 365 #define BHSLR_REASON_REMOVE_FOR_RECOVERY 2 366 367 struct iscsi_bhs_logout_request { 368 uint8_t bhslr_opcode; 369 uint8_t bhslr_reason; 370 uint16_t bhslr_reserved; 371 uint8_t bhslr_total_ahs_len; 372 uint8_t bhslr_data_segment_len[3]; 373 uint64_t bhslr_reserved2; 374 uint32_t bhslr_initiator_task_tag; 375 uint16_t bhslr_cid; 376 uint16_t bhslr_reserved3; 377 uint32_t bhslr_cmdsn; 378 uint32_t bhslr_expstatsn; 379 uint8_t bhslr_reserved4[16]; 380 }; 381 CTASSERT(sizeof(struct iscsi_bhs_logout_request) == ISCSI_BHS_SIZE); 382 383 #define BHSLR_RESPONSE_CLOSED_SUCCESSFULLY 0 384 #define BHSLR_RESPONSE_RECOVERY_NOT_SUPPORTED 2 385 386 struct iscsi_bhs_logout_response { 387 uint8_t bhslr_opcode; 388 uint8_t bhslr_flags; 389 uint8_t bhslr_response; 390 uint8_t bhslr_reserved; 391 uint8_t bhslr_total_ahs_len; 392 uint8_t bhslr_data_segment_len[3]; 393 uint64_t bhslr_reserved2; 394 uint32_t bhslr_initiator_task_tag; 395 uint32_t bhslr_reserved3; 396 uint32_t bhslr_statsn; 397 uint32_t bhslr_expcmdsn; 398 uint32_t bhslr_maxcmdsn; 399 uint32_t bhslr_reserved4; 400 uint16_t bhslr_time2wait; 401 uint16_t bhslr_time2retain; 402 uint32_t bhslr_reserved5; 403 }; 404 CTASSERT(sizeof(struct iscsi_bhs_logout_response) == ISCSI_BHS_SIZE); 405 406 #define BHSAM_EVENT_TARGET_REQUESTS_LOGOUT 1 407 #define BHSAM_EVENT_TARGET_TERMINATES_CONNECTION 2 408 #define BHSAM_EVENT_TARGET_TERMINATES_SESSION 3 409 410 struct iscsi_bhs_asynchronous_message { 411 uint8_t bhsam_opcode; 412 uint8_t bhsam_flags; 413 uint16_t bhsam_reserved; 414 uint8_t bhsam_total_ahs_len; 415 uint8_t bhsam_data_segment_len[3]; 416 uint64_t bhsam_lun; 417 uint32_t bhsam_0xffffffff; 418 uint32_t bhsam_reserved2; 419 uint32_t bhsam_statsn; 420 uint32_t bhsam_expcmdsn; 421 uint32_t bhsam_maxcmdsn; 422 uint8_t bhsam_async_event; 423 uint8_t bhsam_async_vcode; 424 uint16_t bhsam_parameter1; 425 uint16_t bhsam_parameter2; 426 uint16_t bhsam_parameter3; 427 uint32_t bhsam_reserved3; 428 }; 429 CTASSERT(sizeof(struct iscsi_bhs_asynchronous_message) == ISCSI_BHS_SIZE); 430 431 #define BHSSR_REASON_DATA_DIGEST_ERROR 0x02 432 #define BHSSR_PROTOCOL_ERROR 0x04 433 #define BHSSR_COMMAND_NOT_SUPPORTED 0x05 434 #define BHSSR_INVALID_PDU_FIELD 0x09 435 436 struct iscsi_bhs_reject { 437 uint8_t bhsr_opcode; 438 uint8_t bhsr_flags; 439 uint8_t bhsr_reason; 440 uint8_t bhsr_reserved; 441 uint8_t bhsr_total_ahs_len; 442 uint8_t bhsr_data_segment_len[3]; 443 uint64_t bhsr_reserved2; 444 uint32_t bhsr_0xffffffff; 445 uint32_t bhsr_reserved3; 446 uint32_t bhsr_statsn; 447 uint32_t bhsr_expcmdsn; 448 uint32_t bhsr_maxcmdsn; 449 uint32_t bhsr_datasn_r2tsn; 450 uint32_t bhsr_reserved4; 451 uint32_t bhsr_reserved5; 452 }; 453 CTASSERT(sizeof(struct iscsi_bhs_reject) == ISCSI_BHS_SIZE); 454 455 #endif /* !ISCSI_PROTO_H */ 456