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