1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2000 by Cisco Systems, Inc. All rights reserved. 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 * iSCSI Pseudo HBA Driver 27 */ 28 29 #include <sys/socket.h> /* networking stuff */ 30 #include <sys/t_kuser.h> /* networking stuff */ 31 #include <sys/tihdr.h> /* networking stuff */ 32 #include <sys/strsubr.h> /* networking stuff */ 33 #include <netinet/tcp.h> /* TCP_NODELAY */ 34 #include <sys/socketvar.h> /* _ALLOC_SLEEP */ 35 #include <sys/strsun.h> /* DB_TYPE() */ 36 #include <sys/scsi/generic/sense.h> 37 38 #include "iscsi.h" /* iscsi driver */ 39 #include <sys/iscsi_protocol.h> /* iscsi protocol */ 40 41 #define ISCSI_INI_TASK_TTT 0xffffffff 42 #define ISCSI_CONN_TIEMOUT_DETECT 20 43 44 boolean_t iscsi_io_logging = B_FALSE; 45 46 #define ISCSI_CHECK_SCSI_READ(ICHK_CMD, ICHK_HDR, ICHK_LEN, ICHK_TYPE) \ 47 if (idm_pattern_checking) { \ 48 struct scsi_pkt *pkt = (ICHK_CMD)->cmd_un.scsi.pkt; \ 49 if (((ICHK_HDR)->response == 0) && \ 50 ((ICHK_HDR)->cmd_status == 0) && \ 51 ((pkt->pkt_cdbp[0] == SCMD_READ_G1) || \ 52 (pkt->pkt_cdbp[0] == SCMD_READ_G4) || \ 53 (pkt->pkt_cdbp[0] == SCMD_READ) || \ 54 (pkt->pkt_cdbp[0] == SCMD_READ_G5))) { \ 55 idm_buf_t *idb = (ICHK_CMD)->cmd_un.scsi.ibp_ibuf; \ 56 IDM_BUFPAT_CHECK(idb, ICHK_LEN, ICHK_TYPE); \ 57 } \ 58 } 59 60 /* generic io helpers */ 61 static uint32_t n2h24(uchar_t *ptr); 62 static int iscsi_sna_lt(uint32_t n1, uint32_t n2); 63 void iscsi_update_flow_control(iscsi_sess_t *isp, 64 uint32_t max, uint32_t exp); 65 static iscsi_status_t iscsi_rx_process_scsi_itt_to_icmdp(iscsi_sess_t *isp, 66 idm_conn_t *ic, iscsi_scsi_rsp_hdr_t *ihp, iscsi_cmd_t **icmdp); 67 static iscsi_status_t iscsi_rx_process_itt_to_icmdp(iscsi_sess_t *isp, 68 iscsi_hdr_t *ihp, iscsi_cmd_t **icmdp); 69 static void iscsi_process_rsp_status(iscsi_sess_t *isp, iscsi_conn_t *icp, 70 idm_status_t status); 71 static void iscsi_drop_conn_cleanup(iscsi_conn_t *icp); 72 static boolean_t iscsi_nop_timeout_checks(iscsi_cmd_t *icmdp); 73 /* callbacks from idm */ 74 static idm_pdu_cb_t iscsi_tx_done; 75 76 /* receivers */ 77 static idm_status_t iscsi_rx_process_nop(idm_conn_t *ic, idm_pdu_t *pdu); 78 static idm_status_t iscsi_rx_process_data_rsp(idm_conn_t *ic, 79 idm_pdu_t *pdu); 80 static idm_status_t iscsi_rx_process_cmd_rsp(idm_conn_t *ic, idm_pdu_t *pdu); 81 static idm_status_t iscsi_rx_process_reject_rsp(idm_conn_t *ic, 82 idm_pdu_t *pdu); 83 84 static idm_status_t iscsi_rx_process_rejected_tsk_mgt(idm_conn_t *ic, 85 iscsi_hdr_t *old_ihp); 86 static idm_status_t iscsi_rx_process_task_mgt_rsp(idm_conn_t *ic, 87 idm_pdu_t *pdu); 88 static idm_status_t iscsi_rx_process_logout_rsp(idm_conn_t *ic, 89 idm_pdu_t *pdu); 90 static idm_status_t iscsi_rx_process_async_rsp(idm_conn_t *ic, 91 idm_pdu_t *pdu); 92 static idm_status_t iscsi_rx_process_text_rsp(idm_conn_t *ic, 93 idm_pdu_t *pdu); 94 95 /* senders */ 96 static iscsi_status_t iscsi_tx_scsi(iscsi_sess_t *isp, iscsi_cmd_t *icmdp); 97 static iscsi_status_t iscsi_tx_nop(iscsi_sess_t *isp, iscsi_cmd_t *icmdp); 98 static iscsi_status_t iscsi_tx_abort(iscsi_sess_t *isp, iscsi_cmd_t *icmdp); 99 static iscsi_status_t iscsi_tx_reset(iscsi_sess_t *isp, iscsi_cmd_t *icmdp); 100 static iscsi_status_t iscsi_tx_logout(iscsi_sess_t *isp, iscsi_cmd_t *icmdp); 101 static iscsi_status_t iscsi_tx_text(iscsi_sess_t *isp, iscsi_cmd_t *icmdp); 102 103 104 /* helpers */ 105 static void iscsi_logout_start(void *arg); 106 static void iscsi_handle_passthru_callback(struct scsi_pkt *pkt); 107 static void iscsi_handle_nop(iscsi_conn_t *icp, uint32_t itt, uint32_t ttt); 108 109 static void iscsi_timeout_checks(iscsi_sess_t *isp); 110 static void iscsi_nop_checks(iscsi_sess_t *isp); 111 static boolean_t iscsi_decode_sense(uint8_t *sense_data); 112 static void iscsi_flush_cmd_after_reset(uint32_t cmd_sn, uint16_t lun_num, 113 iscsi_conn_t *icp); 114 115 /* 116 * This file contains the main guts of the iSCSI protocol layer. 117 * It's broken into 5 sections; Basic helper functions, RX IO path, 118 * TX IO path, Completion (IC) IO path, and watchdog (WD) routines. 119 * 120 * The IO flow model is similiar to the below diagram. The 121 * iscsi session, connection and command state machines are used 122 * to drive IO through this flow diagram. Reference those files 123 * to get a detailed description of their respective state models 124 * prior to their xxx_state_machine_function(). 125 * 126 * tran_start() -> CMD_E1 TX_THREAD RX_THREAD 127 * | T T 128 * V T T 129 * PENDING_Q --CMD_E2--> ACTIVE_Q - --CMD_E3--+ 130 * T \ C T | 131 * T \M T | 132 * D T | 133 * WD_THREAD TT|TT T | 134 * /E T | 135 * / 6 T | 136 * ABORTING_Q<- --CMD_E3--+ 137 * T | 138 * T T | 139 * T | 140 * callback() <--CMD_E#-- COMPLETION_Q <------------+ 141 * T 142 * T 143 * IC_THREAD 144 * 145 * External and internal command are ran thru this same state 146 * machine. All commands enter the state machine by receiving an 147 * ISCSI_CMD_EVENT_E1. This event places the command into the 148 * PENDING_Q. Next when resources are available the TX_THREAD 149 * issues a E2 event on the command. This sends the command 150 * to the TCP stack and places the command on the ACTIVE_Q. While 151 * on the PENDIING_Q and ACTIVE_Q, the command is monitored via the 152 * WD_THREAD to ensure the pkt_time has not elapsed. If elapsed the 153 * command is issued an E6(timeout) event which moves either (if pending) 154 * completed the command or (if active) moves the command to the 155 * aborting queue and issues a SCSI TASK MANAGEMENT ABORT command 156 * to cancel the IO request. If the original command is completed 157 * or the TASK MANAGEMENT command completes the command is moved 158 * to the COMPLETION_Q via a E3 event. The IC_THREAD then processes 159 * the COMPLETION_Q and issues the scsi_pkt callback. This 160 * callback can not be processed directly from the RX_THREAD 161 * because the callback might call back into the iscsi driver 162 * causing a deadlock condition. 163 * 164 * For more details on the complete CMD state machine reference 165 * the state machine diagram in iscsi_cmd.c. The connection state 166 * machine is driven via IO events in this file. Then session 167 * events are driven by the connection events. For complete 168 * details on these state machines reference iscsi_sess.c and 169 * iscsi_conn.c 170 */ 171 172 173 /* 174 * +--------------------------------------------------------------------+ 175 * | io helper routines | 176 * +--------------------------------------------------------------------+ 177 */ 178 179 /* 180 * n2h24 - native to host 24 bit integer translation. 181 */ 182 static uint32_t 183 n2h24(uchar_t *ptr) 184 { 185 uint32_t idx; 186 bcopy(ptr, &idx, 3); 187 return (ntohl(idx) >> 8); 188 } 189 190 /* 191 * iscsi_sna_lt - Serial Number Arithmetic, 32 bits, less than, RFC1982 192 */ 193 static int 194 iscsi_sna_lt(uint32_t n1, uint32_t n2) 195 { 196 return ((n1 != n2) && 197 (((n1 < n2) && ((n2 - n1) < ISCSI_SNA32_CHECK)) || 198 ((n1 > n2) && ((n1 - n2) > ISCSI_SNA32_CHECK)))); 199 } 200 201 /* 202 * iscsi_sna_lte - Serial Number Arithmetic, 32 bits, less than or equal, 203 * RFC1982 204 */ 205 int 206 iscsi_sna_lte(uint32_t n1, uint32_t n2) 207 { 208 return ((n1 == n2) || 209 (((n1 < n2) && ((n2 - n1) < ISCSI_SNA32_CHECK)) || 210 ((n1 > n2) && ((n1 - n2) > ISCSI_SNA32_CHECK)))); 211 } 212 213 /* 214 * iscsi_update_flow_control - Update expcmdsn and maxcmdsn iSCSI 215 * flow control information for a session 216 */ 217 void 218 iscsi_update_flow_control(iscsi_sess_t *isp, uint32_t max, uint32_t exp) 219 { 220 ASSERT(isp != NULL); 221 ASSERT(mutex_owned(&isp->sess_cmdsn_mutex)); 222 223 if (!iscsi_sna_lt(max, (exp - 1))) { 224 225 if (!iscsi_sna_lte(exp, isp->sess_expcmdsn)) { 226 isp->sess_expcmdsn = exp; 227 } 228 229 if (!iscsi_sna_lte(max, isp->sess_maxcmdsn)) { 230 isp->sess_maxcmdsn = max; 231 if (iscsi_sna_lte(isp->sess_cmdsn, 232 isp->sess_maxcmdsn)) { 233 /* 234 * the window is open again - schedule 235 * to send any held tasks soon 236 */ 237 iscsi_sess_redrive_io(isp); 238 } 239 } 240 } 241 } 242 243 244 /* 245 * +--------------------------------------------------------------------+ 246 * | io receive and processing routines | 247 * +--------------------------------------------------------------------+ 248 */ 249 250 /* 251 * iscsi_rx_scsi_rsp - called from idm 252 * For each opcode type fan out the processing. 253 */ 254 void 255 iscsi_rx_scsi_rsp(idm_conn_t *ic, idm_pdu_t *pdu) 256 { 257 iscsi_conn_t *icp; 258 iscsi_sess_t *isp; 259 iscsi_hdr_t *ihp; 260 idm_status_t status; 261 262 ASSERT(ic != NULL); 263 ASSERT(pdu != NULL); 264 icp = ic->ic_handle; 265 ASSERT(icp != NULL); 266 ihp = (iscsi_hdr_t *)pdu->isp_hdr; 267 ASSERT(ihp != NULL); 268 isp = icp->conn_sess; 269 ASSERT(isp != NULL); 270 271 /* reset the session timer when we receive the response */ 272 isp->sess_rx_lbolt = icp->conn_rx_lbolt = ddi_get_lbolt(); 273 274 /* fan out the hdr processing */ 275 switch (ihp->opcode & ISCSI_OPCODE_MASK) { 276 case ISCSI_OP_SCSI_DATA_RSP: 277 status = iscsi_rx_process_data_rsp(ic, pdu); 278 break; 279 case ISCSI_OP_SCSI_RSP: 280 status = iscsi_rx_process_cmd_rsp(ic, pdu); 281 idm_pdu_complete(pdu, status); 282 break; 283 default: 284 cmn_err(CE_WARN, "iscsi connection(%u) protocol error - " 285 "received pdu with unsupported opcode 0x%02x", 286 icp->conn_oid, ihp->opcode); 287 status = IDM_STATUS_PROTOCOL_ERROR; 288 } 289 iscsi_process_rsp_status(isp, icp, status); 290 } 291 292 void 293 iscsi_task_cleanup(int opcode, iscsi_cmd_t *icmdp) 294 { 295 struct buf *bp; 296 idm_buf_t *ibp, *obp; 297 idm_task_t *itp; 298 299 itp = icmdp->cmd_itp; 300 ASSERT(itp != NULL); 301 ASSERT((opcode == ISCSI_OP_SCSI_DATA_RSP) || 302 (opcode == ISCSI_OP_SCSI_RSP)); 303 304 bp = icmdp->cmd_un.scsi.bp; 305 ibp = icmdp->cmd_un.scsi.ibp_ibuf; 306 obp = icmdp->cmd_un.scsi.ibp_obuf; 307 ISCSI_IO_LOG(CE_NOTE, "DEBUG: task_cleanup: itp: %p opcode: %d " 308 "icmdp: %p bp: %p ibp: %p", (void *)itp, opcode, 309 (void *)icmdp, (void *)bp, (void *)ibp); 310 if (bp && bp->b_bcount) { 311 if (ibp != NULL && bp->b_flags & B_READ) { 312 idm_buf_unbind_in(itp, ibp); 313 idm_buf_free(ibp); 314 icmdp->cmd_un.scsi.ibp_ibuf = NULL; 315 } else if (obp != NULL && !(bp->b_flags & B_READ)) { 316 idm_buf_unbind_out(itp, obp); 317 idm_buf_free(obp); 318 icmdp->cmd_un.scsi.ibp_obuf = NULL; 319 } 320 } 321 322 idm_task_done(itp); 323 } 324 325 idm_status_t 326 iscsi_rx_chk(iscsi_conn_t *icp, iscsi_sess_t *isp, 327 iscsi_scsi_rsp_hdr_t *irhp, iscsi_cmd_t **icmdp) 328 { 329 iscsi_status_t rval; 330 331 mutex_enter(&isp->sess_cmdsn_mutex); 332 333 if (icp->conn_expstatsn == ntohl(irhp->statsn)) { 334 icp->conn_expstatsn++; 335 } else { 336 cmn_err(CE_WARN, "iscsi connection(%u/%x) protocol error - " 337 "received status out of order itt:0x%x statsn:0x%x " 338 "expstatsn:0x%x", icp->conn_oid, irhp->opcode, 339 irhp->itt, ntohl(irhp->statsn), icp->conn_expstatsn); 340 mutex_exit(&isp->sess_cmdsn_mutex); 341 return (IDM_STATUS_PROTOCOL_ERROR); 342 } 343 344 /* get icmdp so we can cleanup on error */ 345 if ((irhp->opcode == ISCSI_OP_SCSI_DATA_RSP) || 346 (irhp->opcode == ISCSI_OP_SCSI_RSP)) { 347 rval = iscsi_rx_process_scsi_itt_to_icmdp(isp, icp->conn_ic, 348 irhp, icmdp); 349 } else { 350 rval = iscsi_rx_process_itt_to_icmdp(isp, 351 (iscsi_hdr_t *)irhp, icmdp); 352 } 353 354 if (!ISCSI_SUCCESS(rval)) { 355 mutex_exit(&isp->sess_cmdsn_mutex); 356 return (IDM_STATUS_PROTOCOL_ERROR); 357 } 358 359 /* update expcmdsn and maxcmdsn */ 360 iscsi_update_flow_control(isp, ntohl(irhp->maxcmdsn), 361 ntohl(irhp->expcmdsn)); 362 mutex_exit(&isp->sess_cmdsn_mutex); 363 return (IDM_STATUS_SUCCESS); 364 } 365 366 static void 367 iscsi_cmd_rsp_chk(iscsi_cmd_t *icmdp, iscsi_scsi_rsp_hdr_t *issrhp) 368 { 369 struct scsi_pkt *pkt; 370 size_t data_transferred; 371 372 pkt = icmdp->cmd_un.scsi.pkt; 373 pkt->pkt_resid = 0; 374 data_transferred = icmdp->cmd_un.scsi.data_transferred; 375 /* Check the residual count */ 376 if ((icmdp->cmd_un.scsi.bp) && 377 (data_transferred != icmdp->cmd_un.scsi.bp->b_bcount)) { 378 /* 379 * We didn't xfer the expected amount of data - 380 * the residual_count in the header is only 381 * valid if the underflow flag is set. 382 */ 383 if (issrhp->flags & ISCSI_FLAG_CMD_UNDERFLOW) { 384 pkt->pkt_resid = ntohl(issrhp->residual_count); 385 } else { 386 if (icmdp->cmd_un.scsi.bp->b_bcount > 387 data_transferred) { 388 /* 389 * Some data fell on the floor 390 * somehow - probably a CRC error 391 */ 392 pkt->pkt_resid = 393 icmdp->cmd_un.scsi.bp->b_bcount - 394 data_transferred; 395 } 396 } 397 ISCSI_IO_LOG(CE_NOTE, 398 "DEBUG: iscsi_rx_cmd_rsp_chk: itt: %u" 399 "data_trans != b_count data_transferred: %lu " 400 "b_count: %lu cmd_status: %d flags: %d resid: %lu", 401 issrhp->itt, data_transferred, 402 icmdp->cmd_un.scsi.bp->b_bcount, 403 issrhp->cmd_status & STATUS_MASK, 404 issrhp->flags, pkt->pkt_resid); 405 } 406 /* set flags that tell SCSA that the command is complete */ 407 if (icmdp->cmd_crc_error_seen == B_FALSE) { 408 /* Set successful completion */ 409 pkt->pkt_reason = CMD_CMPLT; 410 if (icmdp->cmd_un.scsi.bp) { 411 pkt->pkt_state |= (STATE_XFERRED_DATA | 412 STATE_GOT_STATUS); 413 } else { 414 pkt->pkt_state |= STATE_GOT_STATUS; 415 } 416 } else { 417 /* 418 * Some of the data was found to have an incorrect 419 * error at the protocol error. 420 */ 421 pkt->pkt_reason = CMD_PER_FAIL; 422 pkt->pkt_statistics |= STAT_PERR; 423 if (icmdp->cmd_un.scsi.bp) { 424 pkt->pkt_resid = 425 icmdp->cmd_un.scsi.bp->b_bcount; 426 } else { 427 pkt->pkt_resid = 0; 428 } 429 } 430 } 431 432 static boolean_t 433 iscsi_cmd_rsp_cmd_status(iscsi_cmd_t *icmdp, iscsi_scsi_rsp_hdr_t *issrhp, 434 uint8_t *data) 435 { 436 int32_t dlength = 0; 437 struct scsi_arq_status *arqstat = NULL; 438 size_t senselen = 0; 439 int32_t statuslen = 0; 440 int32_t senselen_to = 0; 441 struct scsi_pkt *pkt; 442 boolean_t affect = B_FALSE; 443 444 pkt = icmdp->cmd_un.scsi.pkt; 445 dlength = n2h24(issrhp->dlength); 446 447 /* 448 * Process iSCSI Cmd Response Status 449 * RFC 3720 Sectionn 10.4.2. 450 */ 451 switch (issrhp->cmd_status & STATUS_MASK) { 452 case STATUS_GOOD: 453 /* pass SCSI status up stack */ 454 if (pkt->pkt_scbp) { 455 pkt->pkt_scbp[0] = issrhp->cmd_status; 456 } 457 break; 458 case STATUS_CHECK: 459 /* 460 * Verify we received a sense buffer and 461 * that there is the correct amount of 462 * request sense space to copy it to. 463 */ 464 if ((dlength > 1) && 465 (pkt->pkt_scbp != NULL) && 466 (icmdp->cmd_un.scsi.statuslen >= 467 sizeof (struct scsi_arq_status))) { 468 /* 469 * If a bad command status is received we 470 * need to reset the pkt_resid to zero. 471 * The target driver compares its value 472 * before checking other error flags. 473 * (ex. check conditions) 474 */ 475 pkt->pkt_resid = 0; 476 477 /* get sense length from first 2 bytes */ 478 senselen = ((data[0] << 8) | data[1]) & 479 (size_t)0xFFFF; 480 ISCSI_IO_LOG(CE_NOTE, 481 "DEBUG: iscsi_rx_cmd_rsp_cmd_status status_check: " 482 "dlen: %d scbp: %p statuslen: %d arq: %d senselen:" 483 " %lu", dlength, (void *)pkt->pkt_scbp, 484 icmdp->cmd_un.scsi.statuslen, 485 (int)sizeof (struct scsi_arq_status), 486 senselen); 487 488 /* Sanity-check on the sense length */ 489 if ((senselen + 2) > dlength) { 490 senselen = dlength - 2; 491 } 492 493 /* 494 * If there was a Data Digest error then 495 * the sense data cannot be trusted. 496 */ 497 if (icmdp->cmd_crc_error_seen) { 498 senselen = 0; 499 } 500 501 /* automatic request sense */ 502 arqstat = 503 (struct scsi_arq_status *)pkt->pkt_scbp; 504 505 /* pass SCSI status up stack */ 506 *((uchar_t *)&arqstat->sts_status) = 507 issrhp->cmd_status; 508 509 /* 510 * Set the status for the automatic 511 * request sense command 512 */ 513 arqstat->sts_rqpkt_state = (STATE_GOT_BUS | 514 STATE_GOT_TARGET | STATE_SENT_CMD | 515 STATE_XFERRED_DATA | STATE_GOT_STATUS | 516 STATE_ARQ_DONE); 517 518 *((uchar_t *)&arqstat->sts_rqpkt_status) = 519 STATUS_GOOD; 520 521 arqstat->sts_rqpkt_reason = CMD_CMPLT; 522 statuslen = icmdp->cmd_un.scsi.statuslen; 523 524 if (senselen == 0) { 525 /* auto request sense failed */ 526 arqstat->sts_rqpkt_status.sts_chk = 1; 527 arqstat->sts_rqpkt_resid = statuslen; 528 } else if (senselen < statuslen) { 529 /* auto request sense short */ 530 arqstat->sts_rqpkt_resid = statuslen - senselen; 531 } else { 532 /* auto request sense complete */ 533 arqstat->sts_rqpkt_resid = 0; 534 } 535 arqstat->sts_rqpkt_statistics = 0; 536 pkt->pkt_state |= STATE_ARQ_DONE; 537 538 if (icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_XARQ) { 539 pkt->pkt_state |= STATE_XARQ_DONE; 540 } 541 542 senselen_to = pkt->pkt_scblen - 543 sizeof (struct scsi_arq_status) + 544 sizeof (struct scsi_extended_sense); 545 546 /* copy auto request sense */ 547 dlength = min(senselen, senselen_to); 548 if (dlength > 0) { 549 bcopy(&data[2], (uchar_t *)&arqstat-> 550 sts_sensedata, dlength); 551 552 affect = iscsi_decode_sense( 553 (uint8_t *)&arqstat->sts_sensedata); 554 } 555 break; 556 } 557 /* FALLTHRU */ 558 case STATUS_BUSY: 559 case STATUS_RESERVATION_CONFLICT: 560 case STATUS_QFULL: 561 case STATUS_ACA_ACTIVE: 562 default: 563 /* 564 * If a bad command status is received we need to 565 * reset the pkt_resid to zero. The target driver 566 * compares its value before checking other error 567 * flags. (ex. check conditions) 568 */ 569 ISCSI_IO_LOG(CE_NOTE, 570 "DEBUG: iscsi_rx_cmd_rsp_cmd_status: status: " 571 "%d cmd_status: %d dlen: %u scbp: %p statuslen: %d " 572 "arg_len: %d", issrhp->cmd_status & STATUS_MASK, 573 issrhp->cmd_status, dlength, (void *)pkt->pkt_scbp, 574 icmdp->cmd_un.scsi.statuslen, 575 (int)sizeof (struct scsi_arq_status)); 576 pkt->pkt_resid = 0; 577 /* pass SCSI status up stack */ 578 if (pkt->pkt_scbp) { 579 pkt->pkt_scbp[0] = issrhp->cmd_status; 580 } 581 } 582 583 return (affect); 584 } 585 586 /* 587 * iscsi_rx_process_login_pdup - Process login response PDU. This function 588 * copies the data into the connection context so that the login code can 589 * interpret it. 590 */ 591 592 idm_status_t 593 iscsi_rx_process_login_pdu(idm_conn_t *ic, idm_pdu_t *pdu) 594 { 595 iscsi_conn_t *icp; 596 597 icp = ic->ic_handle; 598 599 /* 600 * Copy header and data into connection structure so iscsi_login() 601 * can process it. 602 */ 603 mutex_enter(&icp->conn_login_mutex); 604 /* 605 * If conn_login_state != LOGIN_TX then we are not ready to handle 606 * this login response and we should just drop it. 607 */ 608 if (icp->conn_login_state == LOGIN_TX) { 609 icp->conn_login_datalen = pdu->isp_datalen; 610 bcopy(pdu->isp_hdr, &icp->conn_login_resp_hdr, 611 sizeof (iscsi_hdr_t)); 612 /* 613 * Login code is sloppy with it's NULL handling so make sure 614 * we don't leave any stale data in there. 615 */ 616 bzero(icp->conn_login_data, icp->conn_login_max_data_length); 617 bcopy(pdu->isp_data, icp->conn_login_data, 618 MIN(pdu->isp_datalen, icp->conn_login_max_data_length)); 619 iscsi_login_update_state_locked(icp, LOGIN_RX); 620 } 621 mutex_exit(&icp->conn_login_mutex); 622 623 return (IDM_STATUS_SUCCESS); 624 } 625 626 /* 627 * iscsi_rx_process_cmd_rsp - Process received scsi command response. This 628 * will contain sense data if the command was not successful. This data needs 629 * to be copied into the scsi_pkt. Otherwise we just complete the IO. 630 */ 631 static idm_status_t 632 iscsi_rx_process_cmd_rsp(idm_conn_t *ic, idm_pdu_t *pdu) 633 { 634 iscsi_conn_t *icp = ic->ic_handle; 635 iscsi_sess_t *isp = icp->conn_sess; 636 iscsi_scsi_rsp_hdr_t *issrhp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr; 637 uint8_t *data = pdu->isp_data; 638 iscsi_cmd_t *icmdp = NULL; 639 struct scsi_pkt *pkt = NULL; 640 idm_status_t rval; 641 struct buf *bp; 642 boolean_t flush = B_FALSE; 643 uint32_t cmd_sn = 0; 644 uint16_t lun_num = 0; 645 646 /* make sure we get status in order */ 647 mutex_enter(&icp->conn_queue_active.mutex); 648 649 if ((rval = iscsi_rx_chk(icp, isp, issrhp, 650 &icmdp)) != IDM_STATUS_SUCCESS) { 651 if (icmdp != NULL) { 652 iscsi_task_cleanup(issrhp->opcode, icmdp); 653 } 654 mutex_exit(&icp->conn_queue_active.mutex); 655 return (rval); 656 } 657 658 /* 659 * If we are in "idm aborting" state then we shouldn't continue 660 * to process this command. By definition this command is no longer 661 * on the active queue so we shouldn't try to remove it either. 662 */ 663 mutex_enter(&icmdp->cmd_mutex); 664 if (icmdp->cmd_state == ISCSI_CMD_STATE_IDM_ABORTING) { 665 mutex_exit(&icmdp->cmd_mutex); 666 mutex_exit(&icp->conn_queue_active.mutex); 667 return (IDM_STATUS_SUCCESS); 668 } 669 mutex_exit(&icmdp->cmd_mutex); 670 671 /* Get the IDM buffer and bytes transferred */ 672 bp = icmdp->cmd_un.scsi.bp; 673 if (ic->ic_conn_flags & IDM_CONN_USE_SCOREBOARD) { 674 /* Transport tracks bytes transferred so use those counts */ 675 if (bp && (bp->b_flags & B_READ)) { 676 icmdp->cmd_un.scsi.data_transferred += 677 icmdp->cmd_itp->idt_rx_bytes; 678 } else { 679 icmdp->cmd_un.scsi.data_transferred += 680 icmdp->cmd_itp->idt_tx_bytes; 681 } 682 } else { 683 /* 684 * Some transports cannot track the bytes transferred on 685 * the initiator side (like iSER) so we have to use the 686 * status info. If the response field indicates that 687 * the command actually completed then we will assume 688 * the data_transferred value represents the entire buffer 689 * unless the resid field says otherwise. This is a bit 690 * unintuitive but it's really impossible to know what 691 * has been transferred without detailed consideration 692 * of the SCSI status and sense key and that is outside 693 * the scope of the transport. Instead the target/class driver 694 * can consider these values along with the resid and figure 695 * it out. The data_transferred concept is just belt and 696 * suspenders anyway -- RFC 3720 actually explicitly rejects 697 * scoreboarding ("Initiators SHOULD NOT keep track of the 698 * data transferred to or from the target (scoreboarding)") 699 * perhaps for this very reason. 700 */ 701 if (issrhp->response != 0) { 702 icmdp->cmd_un.scsi.data_transferred = 0; 703 } else { 704 icmdp->cmd_un.scsi.data_transferred = 705 (bp == NULL) ? 0 : bp->b_bcount; 706 if (issrhp->flags & ISCSI_FLAG_CMD_UNDERFLOW) { 707 icmdp->cmd_un.scsi.data_transferred -= 708 ntohl(issrhp->residual_count); 709 } 710 } 711 } 712 713 ISCSI_CHECK_SCSI_READ(icmdp, issrhp, 714 icmdp->cmd_un.scsi.data_transferred, 715 BP_CHECK_THOROUGH); 716 717 ISCSI_IO_LOG(CE_NOTE, "DEBUG: rx_process_cmd_rsp: ic: %p pdu: %p itt:" 718 " %x expcmdsn: %x sess_cmd: %x sess_expcmdsn: %x data_transfered:" 719 " %lu ibp: %p obp: %p", (void *)ic, (void *)pdu, issrhp->itt, 720 issrhp->expcmdsn, isp->sess_cmdsn, isp->sess_expcmdsn, 721 icmdp->cmd_un.scsi.data_transferred, 722 (void *)icmdp->cmd_un.scsi.ibp_ibuf, 723 (void *)icmdp->cmd_un.scsi.ibp_obuf); 724 725 iscsi_task_cleanup(issrhp->opcode, icmdp); 726 727 if (issrhp->response) { 728 /* The target failed the command. */ 729 ISCSI_IO_LOG(CE_NOTE, "DEBUG: rx_process_cmd_rsp: ic: %p pdu:" 730 " %p response: %d bcount: %lu", (void *)ic, (void *)pdu, 731 issrhp->response, icmdp->cmd_un.scsi.bp->b_bcount); 732 pkt = icmdp->cmd_un.scsi.pkt; 733 pkt->pkt_reason = CMD_TRAN_ERR; 734 if (icmdp->cmd_un.scsi.bp) { 735 pkt->pkt_resid = icmdp->cmd_un.scsi.bp->b_bcount; 736 } else { 737 pkt->pkt_resid = 0; 738 } 739 } else { 740 /* success */ 741 iscsi_cmd_rsp_chk(icmdp, issrhp); 742 flush = iscsi_cmd_rsp_cmd_status(icmdp, issrhp, data); 743 if (flush == B_TRUE) { 744 cmd_sn = icmdp->cmd_sn; 745 ASSERT(icmdp->cmd_lun != NULL); 746 lun_num = icmdp->cmd_lun->lun_num; 747 } 748 } 749 750 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp); 751 if (flush == B_TRUE) { 752 iscsi_flush_cmd_after_reset(cmd_sn, lun_num, icp); 753 } 754 mutex_exit(&icp->conn_queue_active.mutex); 755 return (IDM_STATUS_SUCCESS); 756 } 757 758 static void 759 iscsi_data_rsp_pkt(iscsi_cmd_t *icmdp, iscsi_data_rsp_hdr_t *idrhp) 760 { 761 struct buf *bp = NULL; 762 size_t data_transferred; 763 struct scsi_pkt *pkt; 764 765 bp = icmdp->cmd_un.scsi.bp; 766 pkt = icmdp->cmd_un.scsi.pkt; 767 data_transferred = icmdp->cmd_un.scsi.data_transferred; 768 /* 769 * The command* must be completed now, since we won't get a command 770 * response PDU. The cmd_status and residual_count are 771 * not meaningful unless status_present is set. 772 */ 773 pkt->pkt_resid = 0; 774 /* Check the residual count */ 775 if (bp && (data_transferred != bp->b_bcount)) { 776 /* 777 * We didn't xfer the expected amount of data - 778 * the residual_count in the header is only valid 779 * if the underflow flag is set. 780 */ 781 if (idrhp->flags & ISCSI_FLAG_DATA_UNDERFLOW) { 782 pkt->pkt_resid = ntohl(idrhp->residual_count); 783 ISCSI_IO_LOG(CE_NOTE, "DEBUG: iscsi_data_rsp_pkt: " 784 "underflow: itt: %d " 785 "transferred: %lu count: %lu", idrhp->itt, 786 data_transferred, bp->b_bcount); 787 } else { 788 if (bp->b_bcount > data_transferred) { 789 /* Some data fell on the floor somehw */ 790 ISCSI_IO_LOG(CE_NOTE, "DEBUG: " 791 "iscsi_data_rsp_pkt: data fell: itt: %d " 792 "transferred: %lu count: %lu", idrhp->itt, 793 data_transferred, bp->b_bcount); 794 pkt->pkt_resid = 795 bp->b_bcount - data_transferred; 796 } 797 } 798 } 799 800 pkt->pkt_reason = CMD_CMPLT; 801 pkt->pkt_state |= (STATE_XFERRED_DATA | STATE_GOT_STATUS); 802 803 if (((idrhp->cmd_status & STATUS_MASK) != STATUS_GOOD) && 804 (icmdp->cmd_un.scsi.statuslen >= 805 sizeof (struct scsi_arq_status)) && pkt->pkt_scbp) { 806 807 /* 808 * Not supposed to get exception status here! 809 * We have no request sense data so just do the 810 * best we can 811 */ 812 struct scsi_arq_status *arqstat = 813 (struct scsi_arq_status *)pkt->pkt_scbp; 814 815 816 bzero(arqstat, sizeof (struct scsi_arq_status)); 817 818 *((uchar_t *)&arqstat->sts_status) = 819 idrhp->cmd_status; 820 821 arqstat->sts_rqpkt_resid = 822 sizeof (struct scsi_extended_sense); 823 ISCSI_IO_LOG(CE_NOTE, "DEBUG: iscsi_data_rsp_pkt: " 824 "exception status: itt: %d resid: %d", 825 idrhp->itt, arqstat->sts_rqpkt_resid); 826 827 } else if (pkt->pkt_scbp) { 828 /* just pass along the status we got */ 829 pkt->pkt_scbp[0] = idrhp->cmd_status; 830 } 831 } 832 833 /* 834 * iscsi_rx_process_data_rsp - 835 * This currently processes the final data sequence denoted by the data response 836 * PDU Status bit being set. We will not receive the SCSI response. 837 * This bit denotes that the PDU is the successful completion of the 838 * command. 839 */ 840 static idm_status_t 841 iscsi_rx_process_data_rsp(idm_conn_t *ic, idm_pdu_t *pdu) 842 { 843 iscsi_sess_t *isp = NULL; 844 iscsi_data_rsp_hdr_t *idrhp = (iscsi_data_rsp_hdr_t *)pdu->isp_hdr; 845 iscsi_cmd_t *icmdp = NULL; 846 struct buf *bp = NULL; 847 iscsi_conn_t *icp = ic->ic_handle; 848 idm_buf_t *ibp; 849 idm_status_t rval; 850 851 852 /* should only call this when the data rsp contains final rsp */ 853 ASSERT(idrhp->flags & ISCSI_FLAG_DATA_STATUS); 854 isp = icp->conn_sess; 855 856 mutex_enter(&icp->conn_queue_active.mutex); 857 if ((rval = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)idrhp, 858 &icmdp)) != IDM_STATUS_SUCCESS) { 859 if (icmdp != NULL) { 860 iscsi_task_cleanup(idrhp->opcode, icmdp); 861 } 862 mutex_exit(&icp->conn_queue_active.mutex); 863 return (rval); 864 } 865 866 /* 867 * If we are in "idm aborting" state then we shouldn't continue 868 * to process this command. By definition this command is no longer 869 * on the active queue so we shouldn't try to remove it either. 870 */ 871 mutex_enter(&icmdp->cmd_mutex); 872 if (icmdp->cmd_state == ISCSI_CMD_STATE_IDM_ABORTING) { 873 mutex_exit(&icmdp->cmd_mutex); 874 mutex_exit(&icp->conn_queue_active.mutex); 875 return (IDM_STATUS_SUCCESS); 876 } 877 mutex_exit(&icmdp->cmd_mutex); 878 879 /* 880 * Holding the pending/active queue locks across the 881 * iscsi_rx_data call later in this function may cause 882 * deadlock during fault injections. Instead remove 883 * the cmd from the active queue and release the locks. 884 * Then before returning or completing the command 885 * return the cmd to the active queue and reacquire 886 * the locks. 887 */ 888 iscsi_dequeue_active_cmd(icp, icmdp); 889 890 mutex_exit(&icp->conn_queue_active.mutex); 891 892 /* shorthand some values */ 893 bp = icmdp->cmd_un.scsi.bp; 894 895 /* 896 * some poorly behaved targets have been observed 897 * sending data-in pdu's during a write operation 898 */ 899 if (bp != NULL) { 900 if (!(bp->b_flags & B_READ)) { 901 cmn_err(CE_WARN, 902 "iscsi connection(%u) protocol error - " 903 "received data response during write operation " 904 "itt:0x%x", 905 icp->conn_oid, idrhp->itt); 906 mutex_enter(&icp->conn_queue_active.mutex); 907 iscsi_enqueue_active_cmd(icp, icmdp); 908 mutex_exit(&icp->conn_queue_active.mutex); 909 return (IDM_STATUS_PROTOCOL_ERROR); 910 } 911 } 912 913 ibp = icmdp->cmd_un.scsi.ibp_ibuf; 914 if (ibp == NULL) { 915 /* 916 * After the check of bp above we *should* have a corresponding 917 * idm_buf_t (ibp). It's possible that the original call 918 * to idm_buf_alloc failed due to a pending connection state 919 * transition in which case this value can be NULL. It's 920 * highly unlikely that the connection would be shutting down 921 * *and* we manage to process a data response and get to this 922 * point in the code but just in case we should check for it. 923 * This isn't really a protocol error -- we are almost certainly 924 * closing the connection anyway so just return a generic error. 925 */ 926 mutex_enter(&icp->conn_queue_active.mutex); 927 iscsi_enqueue_active_cmd(icp, icmdp); 928 mutex_exit(&icp->conn_queue_active.mutex); 929 return (IDM_STATUS_FAIL); 930 } 931 932 if (ic->ic_conn_flags & IDM_CONN_USE_SCOREBOARD) { 933 icmdp->cmd_un.scsi.data_transferred = 934 icmdp->cmd_itp->idt_rx_bytes; 935 } else { 936 icmdp->cmd_un.scsi.data_transferred = bp->b_bcount; 937 if (idrhp->flags & ISCSI_FLAG_CMD_UNDERFLOW) { 938 icmdp->cmd_un.scsi.data_transferred -= 939 ntohl(idrhp->residual_count); 940 } 941 } 942 943 ISCSI_IO_LOG(CE_NOTE, "DEBUG: rx_process_data_rsp: icp: %p pdu: %p " 944 "itt: %d ibp: %p icmdp: %p xfer_len: %lu transferred: %lu dlen: %u", 945 (void *)icp, (void *)pdu, idrhp->itt, (void *)bp, (void *)icmdp, 946 (ibp == NULL) ? 0 : ibp->idb_xfer_len, 947 icmdp->cmd_un.scsi.data_transferred, 948 n2h24(idrhp->dlength)); 949 950 iscsi_task_cleanup(idrhp->opcode, icmdp); 951 952 iscsi_data_rsp_pkt(icmdp, idrhp); 953 954 mutex_enter(&icp->conn_queue_active.mutex); 955 iscsi_enqueue_active_cmd(icp, icmdp); 956 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp); 957 mutex_exit(&icp->conn_queue_active.mutex); 958 959 return (IDM_STATUS_SUCCESS); 960 } 961 962 /* 963 * iscsi_rx_process_nop - Process a received nop. If nop is in response 964 * to a ping we sent update stats. If initiated by the target we need 965 * to response back to the target with a nop. Schedule the response. 966 */ 967 /* ARGSUSED */ 968 static idm_status_t 969 iscsi_rx_process_nop(idm_conn_t *ic, idm_pdu_t *pdu) 970 { 971 iscsi_sess_t *isp = NULL; 972 iscsi_nop_in_hdr_t *inihp = (iscsi_nop_in_hdr_t *)pdu->isp_hdr; 973 iscsi_cmd_t *icmdp = NULL; 974 iscsi_conn_t *icp = ic->ic_handle; 975 976 if (icp->conn_expstatsn != ntohl(inihp->statsn)) { 977 cmn_err(CE_WARN, "iscsi connection(%u/%x) protocol error - " 978 "received status out of order itt:0x%x statsn:0x%x " 979 "expstatsn:0x%x", icp->conn_oid, inihp->opcode, inihp->itt, 980 ntohl(inihp->statsn), icp->conn_expstatsn); 981 return (IDM_STATUS_PROTOCOL_ERROR); 982 } 983 isp = icp->conn_sess; 984 ASSERT(isp != NULL); 985 mutex_enter(&isp->sess_queue_pending.mutex); 986 mutex_enter(&icp->conn_queue_active.mutex); 987 mutex_enter(&isp->sess_cmdsn_mutex); 988 if (inihp->itt != ISCSI_RSVD_TASK_TAG) { 989 if (!ISCSI_SUCCESS(iscsi_rx_process_itt_to_icmdp( 990 isp, (iscsi_hdr_t *)inihp, &icmdp))) { 991 cmn_err(CE_WARN, "iscsi connection(%u) protocol error " 992 "- can not find cmd for itt:0x%x", 993 icp->conn_oid, inihp->itt); 994 mutex_exit(&isp->sess_cmdsn_mutex); 995 mutex_exit(&icp->conn_queue_active.mutex); 996 mutex_exit(&isp->sess_queue_pending.mutex); 997 return (IDM_STATUS_PROTOCOL_ERROR); 998 } 999 } 1000 1001 /* update expcmdsn and maxcmdsn */ 1002 iscsi_update_flow_control(isp, ntohl(inihp->maxcmdsn), 1003 ntohl(inihp->expcmdsn)); 1004 mutex_exit(&isp->sess_cmdsn_mutex); 1005 1006 if ((inihp->itt != ISCSI_RSVD_TASK_TAG) && 1007 (inihp->ttt == ISCSI_RSVD_TASK_TAG)) { 1008 /* This is the only type of nop that incs. the expstatsn */ 1009 icp->conn_expstatsn++; 1010 1011 /* 1012 * This is a targets response to our nop 1013 */ 1014 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp); 1015 } else if (inihp->ttt != ISCSI_RSVD_TASK_TAG) { 1016 /* 1017 * Target requested a nop. Send one. 1018 */ 1019 iscsi_handle_nop(icp, ISCSI_RSVD_TASK_TAG, inihp->ttt); 1020 } else { 1021 /* 1022 * This is a target-initiated ping that doesn't expect 1023 * a response; nothing to do except update our flow control 1024 * (which we do in all cases above). 1025 */ 1026 /* EMPTY */ 1027 } 1028 mutex_exit(&icp->conn_queue_active.mutex); 1029 mutex_exit(&isp->sess_queue_pending.mutex); 1030 1031 return (IDM_STATUS_SUCCESS); 1032 } 1033 1034 1035 /* 1036 * iscsi_rx_process_reject_rsp - The server rejected a PDU 1037 */ 1038 static idm_status_t 1039 iscsi_rx_process_reject_rsp(idm_conn_t *ic, idm_pdu_t *pdu) 1040 { 1041 iscsi_reject_rsp_hdr_t *irrhp = (iscsi_reject_rsp_hdr_t *)pdu->isp_hdr; 1042 iscsi_sess_t *isp = NULL; 1043 uint32_t dlength = 0; 1044 iscsi_hdr_t *old_ihp = NULL; 1045 iscsi_conn_t *icp = ic->ic_handle; 1046 uint8_t *data = pdu->isp_data; 1047 iscsi_hdr_t *ihp = (iscsi_hdr_t *)irrhp; 1048 idm_status_t status; 1049 iscsi_cmd_t *icmdp = NULL; 1050 1051 ASSERT(data != NULL); 1052 isp = icp->conn_sess; 1053 ASSERT(isp != NULL); 1054 1055 mutex_enter(&icp->conn_queue_active.mutex); 1056 if ((status = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)irrhp, 1057 &icmdp)) != IDM_STATUS_SUCCESS) { 1058 mutex_exit(&icp->conn_queue_active.mutex); 1059 return (status); 1060 } 1061 1062 /* If we don't have the rejected header we can't do anything */ 1063 dlength = n2h24(irrhp->dlength); 1064 if (dlength < sizeof (iscsi_hdr_t)) { 1065 return (IDM_STATUS_PROTOCOL_ERROR); 1066 } 1067 1068 /* map old ihp */ 1069 old_ihp = (iscsi_hdr_t *)data; 1070 1071 switch (irrhp->reason) { 1072 /* 1073 * ISCSI_REJECT_IMM_CMD_REJECT - Immediate Command Reject 1074 * too many immediate commands (original cmd can be resent) 1075 */ 1076 case ISCSI_REJECT_IMM_CMD_REJECT: 1077 /* 1078 * We have exceeded the server's capacity for outstanding 1079 * immediate commands. This must be a task management 1080 * command so try to find it in the abortingqueue and 1081 * complete it. 1082 */ 1083 if (!(old_ihp->opcode & ISCSI_OP_IMMEDIATE)) { 1084 /* Rejecting IMM but old old_hdr wasn't IMM */ 1085 return (IDM_STATUS_PROTOCOL_ERROR); 1086 } 1087 1088 /* 1089 * We only send NOP and TASK_MGT as IMM. All other 1090 * cases should be considered as a protocol error. 1091 */ 1092 switch (old_ihp->opcode & ISCSI_OPCODE_MASK) { 1093 case ISCSI_OP_NOOP_OUT: 1094 /* 1095 * A ping was rejected - treat this like 1096 * ping response. The down side is we 1097 * didn't get an updated MaxCmdSn. 1098 */ 1099 break; 1100 case ISCSI_OP_SCSI_TASK_MGT_MSG: 1101 (void) iscsi_rx_process_rejected_tsk_mgt(ic, old_ihp); 1102 break; 1103 default: 1104 cmn_err(CE_WARN, "iscsi connection(%u) protocol error " 1105 "- received a reject for a command(0x%02x) not " 1106 "sent as an immediate", icp->conn_oid, 1107 old_ihp->opcode); 1108 status = IDM_STATUS_PROTOCOL_ERROR; 1109 break; 1110 } 1111 break; 1112 1113 /* 1114 * For the rest of the reject cases just use the general 1115 * hammer of dis/reconnecting. This will resolve all 1116 * noted issues although could be more graceful. 1117 */ 1118 case ISCSI_REJECT_DATA_DIGEST_ERROR: 1119 case ISCSI_REJECT_CMD_BEFORE_LOGIN: 1120 case ISCSI_REJECT_SNACK_REJECT: 1121 case ISCSI_REJECT_PROTOCOL_ERROR: 1122 case ISCSI_REJECT_CMD_NOT_SUPPORTED: 1123 case ISCSI_REJECT_TASK_IN_PROGRESS: 1124 case ISCSI_REJECT_INVALID_DATA_ACK: 1125 case ISCSI_REJECT_INVALID_PDU_FIELD: 1126 case ISCSI_REJECT_LONG_OPERATION_REJECT: 1127 case ISCSI_REJECT_NEGOTIATION_RESET: 1128 default: 1129 cmn_err(CE_WARN, "iscsi connection(%u) closing connection - " 1130 "target requested itt:0x%x reason:0x%x", 1131 icp->conn_oid, ihp->itt, irrhp->reason); 1132 status = IDM_STATUS_PROTOCOL_ERROR; 1133 break; 1134 } 1135 1136 return (IDM_STATUS_SUCCESS); 1137 } 1138 1139 1140 /* 1141 * iscsi_rx_process_rejected_tsk_mgt - 1142 */ 1143 /* ARGSUSED */ 1144 static idm_status_t 1145 iscsi_rx_process_rejected_tsk_mgt(idm_conn_t *ic, iscsi_hdr_t *old_ihp) 1146 { 1147 iscsi_sess_t *isp = NULL; 1148 iscsi_cmd_t *icmdp = NULL; 1149 iscsi_conn_t *icp = NULL; 1150 1151 isp = icp->conn_sess; 1152 ASSERT(old_ihp != NULL); 1153 ASSERT(isp != NULL); 1154 1155 mutex_enter(&icp->conn_queue_active.mutex); 1156 mutex_enter(&isp->sess_cmdsn_mutex); 1157 if (!ISCSI_SUCCESS(iscsi_rx_process_itt_to_icmdp( 1158 isp, old_ihp, &icmdp))) { 1159 mutex_exit(&isp->sess_cmdsn_mutex); 1160 mutex_exit(&icp->conn_queue_active.mutex); 1161 return (IDM_STATUS_PROTOCOL_ERROR); 1162 } 1163 mutex_exit(&isp->sess_cmdsn_mutex); 1164 1165 switch (icmdp->cmd_type) { 1166 case ISCSI_CMD_TYPE_ABORT: 1167 case ISCSI_CMD_TYPE_RESET: 1168 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E4, 1169 icp->conn_sess); 1170 break; 1171 /* We don't send any other task mgr types */ 1172 default: 1173 ASSERT(B_FALSE); 1174 break; 1175 } 1176 mutex_exit(&icp->conn_queue_active.mutex); 1177 1178 return (IDM_STATUS_SUCCESS); 1179 } 1180 1181 1182 /* 1183 * iscsi_rx_process_task_mgt_rsp - 1184 */ 1185 /* ARGSUSED */ 1186 static idm_status_t 1187 iscsi_rx_process_task_mgt_rsp(idm_conn_t *ic, idm_pdu_t *pdu) 1188 { 1189 iscsi_sess_t *isp = NULL; 1190 iscsi_scsi_task_mgt_rsp_hdr_t *istmrhp = NULL; 1191 iscsi_cmd_t *icmdp = NULL; 1192 iscsi_conn_t *icp = ic->ic_handle; 1193 idm_status_t status = IDM_STATUS_SUCCESS; 1194 1195 isp = icp->conn_sess; 1196 istmrhp = (iscsi_scsi_task_mgt_rsp_hdr_t *)pdu->isp_hdr; 1197 1198 mutex_enter(&icp->conn_queue_active.mutex); 1199 if ((status = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)istmrhp, 1200 &icmdp)) != IDM_STATUS_SUCCESS) { 1201 mutex_exit(&icp->conn_queue_active.mutex); 1202 return (status); 1203 } 1204 1205 switch (icmdp->cmd_type) { 1206 case ISCSI_CMD_TYPE_ABORT: 1207 case ISCSI_CMD_TYPE_RESET: 1208 switch (istmrhp->response) { 1209 case SCSI_TCP_TM_RESP_COMPLETE: 1210 /* success */ 1211 iscsi_cmd_state_machine(icmdp, 1212 ISCSI_CMD_EVENT_E3, isp); 1213 break; 1214 case SCSI_TCP_TM_RESP_NO_TASK: 1215 /* 1216 * If the array no longer knows about 1217 * an ABORT RTT and we no longer have 1218 * a parent SCSI command it was just 1219 * completed, free this ABORT resource. 1220 * Otherwise FALLTHRU this will flag a 1221 * protocol problem. 1222 */ 1223 if ((icmdp->cmd_type == ISCSI_CMD_TYPE_ABORT) && 1224 (icmdp->cmd_un.abort.icmdp == NULL)) { 1225 iscsi_cmd_state_machine(icmdp, 1226 ISCSI_CMD_EVENT_E4, isp); 1227 break; 1228 } 1229 /* FALLTHRU */ 1230 case SCSI_TCP_TM_RESP_REJECTED: 1231 /* 1232 * If the target rejects our reset task, 1233 * we should record the response and complete 1234 * this command with the result. 1235 */ 1236 if (icmdp->cmd_type == ISCSI_CMD_TYPE_RESET) { 1237 icmdp->cmd_un.reset.response = 1238 istmrhp->response; 1239 iscsi_cmd_state_machine(icmdp, 1240 ISCSI_CMD_EVENT_E3, isp); 1241 break; 1242 } 1243 /* FALLTHRU */ 1244 case SCSI_TCP_TM_RESP_NO_LUN: 1245 case SCSI_TCP_TM_RESP_TASK_ALLEGIANT: 1246 case SCSI_TCP_TM_RESP_NO_FAILOVER: 1247 case SCSI_TCP_TM_RESP_IN_PRGRESS: 1248 default: 1249 /* 1250 * Something is out of sync. Flush 1251 * active queues and resync the 1252 * the connection to try and recover 1253 * to a known state. 1254 */ 1255 status = IDM_STATUS_PROTOCOL_ERROR; 1256 } 1257 break; 1258 1259 default: 1260 cmn_err(CE_WARN, "iscsi connection(%u) protocol error - " 1261 "received a task mgt response for a non-task mgt " 1262 "cmd itt:0x%x type:%d", icp->conn_oid, istmrhp->itt, 1263 icmdp->cmd_type); 1264 status = IDM_STATUS_PROTOCOL_ERROR; 1265 break; 1266 } 1267 1268 mutex_exit(&icp->conn_queue_active.mutex); 1269 return (status); 1270 } 1271 1272 1273 /* 1274 * iscsi_rx_process_logout_rsp - 1275 * 1276 */ 1277 /* ARGSUSED */ 1278 idm_status_t 1279 iscsi_rx_process_logout_rsp(idm_conn_t *ic, idm_pdu_t *pdu) 1280 { 1281 iscsi_conn_t *icp = ic->ic_handle; 1282 iscsi_logout_rsp_hdr_t *ilrhp = 1283 (iscsi_logout_rsp_hdr_t *)pdu->isp_hdr; 1284 iscsi_cmd_t *icmdp = NULL; 1285 iscsi_sess_t *isp; 1286 idm_status_t status = IDM_STATUS_SUCCESS; 1287 1288 isp = icp->conn_sess; 1289 1290 if (icp->conn_expstatsn != ntohl(ilrhp->statsn)) { 1291 cmn_err(CE_WARN, "iscsi connection(%u/%x) protocol error - " 1292 "received status out of order itt:0x%x statsn:0x%x " 1293 "expstatsn:0x%x", icp->conn_oid, ilrhp->opcode, ilrhp->itt, 1294 ntohl(ilrhp->statsn), icp->conn_expstatsn); 1295 return (IDM_STATUS_PROTOCOL_ERROR); 1296 } 1297 1298 mutex_enter(&icp->conn_queue_active.mutex); 1299 mutex_enter(&isp->sess_cmdsn_mutex); 1300 if (ilrhp->itt != ISCSI_RSVD_TASK_TAG) { 1301 if (!ISCSI_SUCCESS(iscsi_rx_process_itt_to_icmdp( 1302 isp, (iscsi_hdr_t *)ilrhp, &icmdp))) { 1303 mutex_exit(&isp->sess_cmdsn_mutex); 1304 mutex_exit(&icp->conn_queue_active.mutex); 1305 return (IDM_STATUS_PROTOCOL_ERROR); 1306 } 1307 } 1308 1309 /* update expcmdsn and maxcmdsn */ 1310 iscsi_update_flow_control(isp, ntohl(ilrhp->maxcmdsn), 1311 ntohl(ilrhp->expcmdsn)); 1312 mutex_exit(&isp->sess_cmdsn_mutex); 1313 1314 ISCSI_IO_LOG(CE_NOTE, 1315 "DEBUG: iscsi_rx_process_logout_rsp: response: %d", 1316 ilrhp->response); 1317 switch (ilrhp->response) { 1318 case ISCSI_LOGOUT_CID_NOT_FOUND: 1319 /* 1320 * If the target doesn't know about our connection 1321 * then we can consider our self disconnected. 1322 */ 1323 /* FALLTHRU */ 1324 case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED: 1325 /* 1326 * We don't support ErrorRecovery levels above 0 1327 * currently so consider this success. 1328 */ 1329 /* FALLTHRU */ 1330 case ISCSI_LOGOUT_CLEANUP_FAILED: 1331 /* 1332 * per spec. "cleanup failed for various reasons." 1333 * Although those various reasons are undefined. 1334 * Not sure what to do here. So fake success, 1335 * which will disconnect the connection. 1336 */ 1337 /* FALLTHRU */ 1338 case ISCSI_LOGOUT_SUCCESS: 1339 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp); 1340 mutex_exit(&icp->conn_queue_active.mutex); 1341 iscsi_drop_conn_cleanup(icp); 1342 break; 1343 default: 1344 mutex_exit(&icp->conn_queue_active.mutex); 1345 status = IDM_STATUS_PROTOCOL_ERROR; 1346 break; 1347 1348 } 1349 return (status); 1350 } 1351 1352 /* 1353 * iscsi_rx_process_async_rsp 1354 * 1355 */ 1356 /* ARGSUSED */ 1357 static idm_status_t 1358 iscsi_rx_process_async_rsp(idm_conn_t *ic, idm_pdu_t *pdu) 1359 { 1360 iscsi_conn_t *icp = ic->ic_handle; 1361 iscsi_sess_t *isp = icp->conn_sess; 1362 idm_status_t rval = IDM_STATUS_SUCCESS; 1363 iscsi_task_t *itp; 1364 iscsi_async_evt_hdr_t *iaehp = 1365 (iscsi_async_evt_hdr_t *)pdu->isp_hdr; 1366 1367 ASSERT(icp != NULL); 1368 ASSERT(pdu != NULL); 1369 ASSERT(isp != NULL); 1370 1371 mutex_enter(&isp->sess_cmdsn_mutex); 1372 if (icp->conn_expstatsn == ntohl(iaehp->statsn)) { 1373 icp->conn_expstatsn++; 1374 } else { 1375 cmn_err(CE_WARN, "iscsi connection(%u) protocol error - " 1376 "received status out of order statsn:0x%x " 1377 "expstatsn:0x%x", icp->conn_oid, 1378 ntohl(iaehp->statsn), icp->conn_expstatsn); 1379 mutex_exit(&isp->sess_cmdsn_mutex); 1380 return (IDM_STATUS_PROTOCOL_ERROR); 1381 } 1382 mutex_exit(&isp->sess_cmdsn_mutex); 1383 1384 switch (iaehp->async_event) { 1385 case ISCSI_ASYNC_EVENT_SCSI_EVENT: 1386 /* 1387 * SCSI asynchronous event is reported in 1388 * the sense data. Sense data that accompanies 1389 * the report in the data segment identifies the 1390 * condition. If the target supports SCSI 1391 * asynchronous events reporting (see [SAM2]) 1392 * as indicated in the stardard INQUIRY data 1393 * (see [SPC3]), its use may be enabled by 1394 * parameters in the SCSI control mode page 1395 * (see [SPC3]). 1396 * 1397 * T-10 has removed SCSI asunchronous events 1398 * from the standard. Although we have seen 1399 * a couple targets still spending these requests. 1400 * Those targets were specifically sending them 1401 * for notification of a LUN/Volume change 1402 * (ex. LUN addition/removal). Take a general 1403 * action to these events of dis/reconnecting. 1404 * Once reconnected we perform a reenumeration. 1405 */ 1406 idm_ini_conn_disconnect(ic); 1407 break; 1408 1409 case ISCSI_ASYNC_EVENT_REQUEST_LOGOUT: 1410 /* 1411 * We've been asked to logout by the target -- 1412 * we need to treat this differently from a normal logout 1413 * due to a discovery failure. Normal logouts result in 1414 * an N3 event to the session state machine and an offline 1415 * of the lun. In this case we want to put the connection 1416 * into "failed" state and generate N5 to the session state 1417 * machine since the initiator logged out at the target's 1418 * request. To track this we set a flag indicating we 1419 * received this async logout request from the tharget 1420 */ 1421 mutex_enter(&icp->conn_state_mutex); 1422 icp->conn_async_logout = B_TRUE; 1423 mutex_exit(&icp->conn_state_mutex); 1424 1425 /* Hold is released in iscsi_handle_logout. */ 1426 idm_conn_hold(ic); 1427 1428 /* Target has requested this connection to logout. */ 1429 itp = kmem_zalloc(sizeof (iscsi_task_t), KM_SLEEP); 1430 itp->t_arg = icp; 1431 itp->t_blocking = B_FALSE; 1432 if (ddi_taskq_dispatch(isp->sess_taskq, 1433 (void(*)())iscsi_logout_start, itp, DDI_SLEEP) != 1434 DDI_SUCCESS) { 1435 idm_conn_rele(ic); 1436 /* Disconnect if we couldn't dispatch the task */ 1437 idm_ini_conn_disconnect(ic); 1438 } 1439 break; 1440 1441 case ISCSI_ASYNC_EVENT_DROPPING_CONNECTION: 1442 /* 1443 * Target is going to drop our connection. 1444 * param1 - CID which will be dropped. 1445 * param2 - Min time to reconnect. 1446 * param3 - Max time to reconnect. 1447 * 1448 * For now just let fail as another disconnect. 1449 * 1450 * MC/S Once we support > 1 connections then 1451 * we need to check the CID and drop that 1452 * specific connection. 1453 */ 1454 iscsi_conn_set_login_min_max(icp, iaehp->param2, 1455 iaehp->param3); 1456 idm_ini_conn_disconnect(ic); 1457 break; 1458 1459 case ISCSI_ASYNC_EVENT_DROPPING_ALL_CONNECTIONS: 1460 /* 1461 * Target is going to drop ALL connections. 1462 * param2 - Min time to reconnect. 1463 * param3 - Max time to reconnect. 1464 * 1465 * For now just let fail as anyother disconnect. 1466 * 1467 * MC/S Once we support more than > 1 connections 1468 * then we need to drop all connections on the 1469 * session. 1470 */ 1471 iscsi_conn_set_login_min_max(icp, iaehp->param2, 1472 iaehp->param3); 1473 idm_ini_conn_disconnect(ic); 1474 break; 1475 1476 case ISCSI_ASYNC_EVENT_PARAM_NEGOTIATION: 1477 /* 1478 * Target requests parameter negotiation 1479 * on this connection. 1480 * 1481 * The initiator must honor this request. For 1482 * now we will request a logout. We can't 1483 * just ignore this or it might force corruption? 1484 */ 1485 1486 /* Hold is released in iscsi_handle_logout */ 1487 idm_conn_hold(ic); 1488 itp = kmem_zalloc(sizeof (iscsi_task_t), KM_SLEEP); 1489 itp->t_arg = icp; 1490 itp->t_blocking = B_FALSE; 1491 if (ddi_taskq_dispatch(isp->sess_taskq, 1492 (void(*)())iscsi_logout_start, itp, DDI_SLEEP) != 1493 DDI_SUCCESS) { 1494 /* Disconnect if we couldn't dispatch the task */ 1495 idm_conn_rele(ic); 1496 idm_ini_conn_disconnect(ic); 1497 } 1498 break; 1499 1500 case ISCSI_ASYNC_EVENT_VENDOR_SPECIFIC: 1501 /* 1502 * We currently don't handle any vendor 1503 * specific async events. So just ignore 1504 * the request. 1505 */ 1506 idm_ini_conn_disconnect(ic); 1507 break; 1508 default: 1509 rval = IDM_STATUS_PROTOCOL_ERROR; 1510 } 1511 1512 return (rval); 1513 } 1514 1515 /* 1516 * iscsi_rx_process_text_rsp - processes iSCSI text response. It sets 1517 * the cmd_result field of the command data structure with the actual 1518 * status value instead of returning the status value. The return value 1519 * is SUCCESS in order to let iscsi_handle_text control the operation of 1520 * a text request. 1521 * Text requests are a handled a little different than other types of 1522 * iSCSI commands because the initiator sends additional empty text requests 1523 * in order to obtain the remaining responses required to complete the 1524 * request. iscsi_handle_text controls the operation of text request, while 1525 * iscsi_rx_process_text_rsp just process the current response. 1526 */ 1527 static idm_status_t 1528 iscsi_rx_process_text_rsp(idm_conn_t *ic, idm_pdu_t *pdu) 1529 { 1530 iscsi_sess_t *isp = NULL; 1531 iscsi_text_rsp_hdr_t *ithp = 1532 (iscsi_text_rsp_hdr_t *)pdu->isp_hdr; 1533 iscsi_conn_t *icp = ic->ic_handle; 1534 iscsi_cmd_t *icmdp = NULL; 1535 boolean_t final = B_FALSE; 1536 uint32_t data_len; 1537 uint8_t *data = pdu->isp_data; 1538 idm_status_t rval; 1539 1540 isp = icp->conn_sess; 1541 1542 mutex_enter(&icp->conn_queue_active.mutex); 1543 if ((rval = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)ithp, 1544 &icmdp)) != IDM_STATUS_SUCCESS) { 1545 mutex_exit(&icp->conn_queue_active.mutex); 1546 return (rval); 1547 } 1548 1549 /* update local final response flag */ 1550 if (ithp->flags & ISCSI_FLAG_FINAL) { 1551 final = B_TRUE; 1552 } 1553 1554 /* 1555 * validate received TTT value. RFC3720 specifies the following: 1556 * - F bit set to 1 MUST have a reserved TTT value 0xffffffff 1557 * - F bit set to 0 MUST have a non-reserved TTT value !0xffffffff 1558 * In addition, the received TTT value must not change between 1559 * responses of a long text response 1560 */ 1561 if (((final == B_TRUE) && (ithp->ttt != ISCSI_RSVD_TASK_TAG)) || 1562 ((final == B_FALSE) && (ithp->ttt == ISCSI_RSVD_TASK_TAG))) { 1563 icmdp->cmd_result = ISCSI_STATUS_PROTOCOL_ERROR; 1564 icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_FINAL_RSP; 1565 mutex_exit(&icp->conn_queue_active.mutex); 1566 cmn_err(CE_WARN, "iscsi connection(%u) protocol error - " 1567 "received text response with invalid flags:0x%x or " 1568 "ttt:0x%x", icp->conn_oid, ithp->flags, ithp->itt); 1569 return (IDM_STATUS_PROTOCOL_ERROR); 1570 } 1571 1572 if ((icmdp->cmd_un.text.stage == ISCSI_CMD_TEXT_INITIAL_REQ) && 1573 (ithp->ttt == ISCSI_RSVD_TASK_TAG) && 1574 (final == B_FALSE)) { 1575 /* TTT should have matched reserved value */ 1576 icmdp->cmd_result = ISCSI_STATUS_PROTOCOL_ERROR; 1577 icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_FINAL_RSP; 1578 mutex_exit(&icp->conn_queue_active.mutex); 1579 cmn_err(CE_WARN, "iscsi connection(%u) protocol " 1580 "error - received text response with invalid " 1581 "ttt:0x%x", icp->conn_oid, ithp->ttt); 1582 return (IDM_STATUS_PROTOCOL_ERROR); 1583 } 1584 1585 /* 1586 * If this is first response, save away TTT value for later use 1587 * in a long text request/response sequence 1588 */ 1589 if (icmdp->cmd_un.text.stage == ISCSI_CMD_TEXT_INITIAL_REQ) { 1590 icmdp->cmd_un.text.ttt = ithp->ttt; 1591 } 1592 1593 data_len = ntoh24(ithp->dlength); 1594 1595 /* check whether enough buffer available to copy data */ 1596 if ((icmdp->cmd_un.text.total_rx_len + data_len) > 1597 icmdp->cmd_un.text.buf_len) { 1598 icmdp->cmd_un.text.total_rx_len += data_len; 1599 icmdp->cmd_result = ISCSI_STATUS_DATA_OVERFLOW; 1600 /* 1601 * DATA_OVERFLOW will result in a SUCCESS return so that 1602 * iscsi_handle_text can continue to obtain the remaining 1603 * text response if needed. 1604 */ 1605 } else { 1606 char *buf_data = (icmdp->cmd_un.text.buf + 1607 icmdp->cmd_un.text.offset); 1608 1609 bcopy(data, buf_data, data_len); 1610 icmdp->cmd_un.text.offset += data_len; 1611 icmdp->cmd_un.text.total_rx_len += data_len; 1612 icmdp->cmd_result = ISCSI_STATUS_SUCCESS; 1613 bcopy(ithp->rsvd4, icmdp->cmd_un.text.lun, 1614 sizeof (icmdp->cmd_un.text.lun)); 1615 } 1616 1617 /* update stage */ 1618 if (final == B_TRUE) { 1619 icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_FINAL_RSP; 1620 } else { 1621 icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_CONTINUATION; 1622 } 1623 1624 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp); 1625 mutex_exit(&icp->conn_queue_active.mutex); 1626 return (IDM_STATUS_SUCCESS); 1627 } 1628 1629 /* 1630 * iscsi_rx_process_scsi_itt_to_icmdp - Lookup itt using IDM to find matching 1631 * icmdp. Verify itt in hdr and icmdp are the same. 1632 */ 1633 static iscsi_status_t 1634 iscsi_rx_process_scsi_itt_to_icmdp(iscsi_sess_t *isp, idm_conn_t *ic, 1635 iscsi_scsi_rsp_hdr_t *ihp, iscsi_cmd_t **icmdp) 1636 { 1637 idm_task_t *itp; 1638 1639 ASSERT(isp != NULL); 1640 ASSERT(ihp != NULL); 1641 ASSERT(icmdp != NULL); 1642 ASSERT(mutex_owned(&isp->sess_cmdsn_mutex)); 1643 itp = idm_task_find_and_complete(ic, ihp->itt, ISCSI_INI_TASK_TTT); 1644 if (itp == NULL) { 1645 cmn_err(CE_WARN, "iscsi session(%u) protocol error - " 1646 "received unknown itt:0x%x - protocol error", 1647 isp->sess_oid, ihp->itt); 1648 return (ISCSI_STATUS_INTERNAL_ERROR); 1649 } 1650 *icmdp = itp->idt_private; 1651 1652 idm_task_rele(itp); 1653 1654 return (ISCSI_STATUS_SUCCESS); 1655 1656 } 1657 1658 /* 1659 * iscsi_rx_process_itt_to_icmdp - Lookup itt in the session's 1660 * cmd table to find matching icmdp. Verify itt in hdr and 1661 * icmdp are the same. 1662 */ 1663 static iscsi_status_t 1664 iscsi_rx_process_itt_to_icmdp(iscsi_sess_t *isp, iscsi_hdr_t *ihp, 1665 iscsi_cmd_t **icmdp) 1666 { 1667 int cmd_table_idx = 0; 1668 1669 ASSERT(isp != NULL); 1670 ASSERT(ihp != NULL); 1671 ASSERT(icmdp != NULL); 1672 ASSERT(mutex_owned(&isp->sess_cmdsn_mutex)); 1673 1674 /* try to find an associated iscsi_pkt */ 1675 cmd_table_idx = (ihp->itt - IDM_TASKIDS_MAX) % ISCSI_CMD_TABLE_SIZE; 1676 if (isp->sess_cmd_table[cmd_table_idx] == NULL) { 1677 cmn_err(CE_WARN, "iscsi session(%u) protocol error - " 1678 "received unknown itt:0x%x - protocol error", 1679 isp->sess_oid, ihp->itt); 1680 return (ISCSI_STATUS_INTERNAL_ERROR); 1681 } 1682 1683 /* verify itt */ 1684 if (isp->sess_cmd_table[cmd_table_idx]->cmd_itt != ihp->itt) { 1685 cmn_err(CE_WARN, "iscsi session(%u) received itt:0x%x " 1686 " which is out of sync with itt:0x%x", isp->sess_oid, 1687 ihp->itt, isp->sess_cmd_table[cmd_table_idx]->cmd_itt); 1688 return (ISCSI_STATUS_INTERNAL_ERROR); 1689 } 1690 1691 /* ensure that icmdp is still in Active state */ 1692 if (isp->sess_cmd_table[cmd_table_idx]->cmd_state != 1693 ISCSI_CMD_STATE_ACTIVE) { 1694 cmn_err(CE_WARN, "iscsi session(%u) received itt:0x%x " 1695 "but icmdp (%p) is not in active state", 1696 isp->sess_oid, ihp->itt, 1697 (void *)isp->sess_cmd_table[cmd_table_idx]); 1698 return (ISCSI_STATUS_INTERNAL_ERROR); 1699 } 1700 1701 /* make sure this is a SCSI cmd */ 1702 *icmdp = isp->sess_cmd_table[cmd_table_idx]; 1703 1704 return (ISCSI_STATUS_SUCCESS); 1705 } 1706 1707 /* 1708 * +--------------------------------------------------------------------+ 1709 * | End of protocol receive routines | 1710 * +--------------------------------------------------------------------+ 1711 */ 1712 1713 /* 1714 * +--------------------------------------------------------------------+ 1715 * | Beginning of protocol send routines | 1716 * +--------------------------------------------------------------------+ 1717 */ 1718 1719 1720 /* 1721 * iscsi_tx_thread - This thread is the driving point for all 1722 * iSCSI PDUs after login. No PDUs should call idm_pdu_tx() 1723 * directly they should be funneled through iscsi_tx_thread. 1724 */ 1725 void 1726 iscsi_tx_thread(iscsi_thread_t *thread, void *arg) 1727 { 1728 iscsi_conn_t *icp = (iscsi_conn_t *)arg; 1729 iscsi_sess_t *isp = NULL; 1730 iscsi_cmd_t *icmdp = NULL; 1731 clock_t tout; 1732 int ret = 1; 1733 1734 ASSERT(icp != NULL); 1735 isp = icp->conn_sess; 1736 ASSERT(isp != NULL); 1737 ASSERT(thread != NULL); 1738 ASSERT(thread->signature == SIG_ISCSI_THREAD); 1739 1740 tout = SEC_TO_TICK(1); 1741 /* 1742 * Transfer icmdps until shutdown by owning session. 1743 */ 1744 while (ret != 0) { 1745 1746 isp->sess_window_open = B_TRUE; 1747 /* 1748 * While the window is open, there are commands available 1749 * to send and the session state allows those commands to 1750 * be sent try to transfer them. 1751 */ 1752 mutex_enter(&isp->sess_queue_pending.mutex); 1753 while ((isp->sess_window_open == B_TRUE) && 1754 ((icmdp = isp->sess_queue_pending.head) != NULL) && 1755 (((icmdp->cmd_type != ISCSI_CMD_TYPE_SCSI) && 1756 (ISCSI_CONN_STATE_FULL_FEATURE(icp->conn_state))) || 1757 (icp->conn_state == ISCSI_CONN_STATE_LOGGED_IN))) { 1758 1759 /* update command with this connection info */ 1760 icmdp->cmd_conn = icp; 1761 /* attempt to send this command */ 1762 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E2, isp); 1763 1764 ASSERT(!mutex_owned(&isp->sess_queue_pending.mutex)); 1765 mutex_enter(&isp->sess_queue_pending.mutex); 1766 } 1767 mutex_exit(&isp->sess_queue_pending.mutex); 1768 1769 /* 1770 * Go to sleep until there is something new 1771 * to process (awoken via cv_boardcast). 1772 * Or the timer goes off. 1773 */ 1774 ret = iscsi_thread_wait(thread, tout); 1775 } 1776 1777 } 1778 1779 1780 /* 1781 * iscsi_tx_cmd - transfers icmdp across wire as iscsi pdu 1782 * 1783 * Just prior to sending the command to the networking layer the 1784 * pending queue lock will be dropped. At this point only local 1785 * resources will be used, not the icmdp. Holding the queue lock 1786 * across the networking call can lead to a hang. (This is due 1787 * to the the target driver and networking layers competing use 1788 * of the timeout() resources and the queue lock being held for 1789 * both sides.) Upon the completion of this command the lock 1790 * will have been re-acquired. 1791 */ 1792 iscsi_status_t 1793 iscsi_tx_cmd(iscsi_sess_t *isp, iscsi_cmd_t *icmdp) 1794 { 1795 iscsi_status_t rval = ISCSI_STATUS_INTERNAL_ERROR; 1796 1797 ASSERT(isp != NULL); 1798 ASSERT(icmdp != NULL); 1799 1800 /* transfer specific command type */ 1801 switch (icmdp->cmd_type) { 1802 case ISCSI_CMD_TYPE_SCSI: 1803 rval = iscsi_tx_scsi(isp, icmdp); 1804 break; 1805 case ISCSI_CMD_TYPE_NOP: 1806 rval = iscsi_tx_nop(isp, icmdp); 1807 break; 1808 case ISCSI_CMD_TYPE_ABORT: 1809 rval = iscsi_tx_abort(isp, icmdp); 1810 break; 1811 case ISCSI_CMD_TYPE_RESET: 1812 rval = iscsi_tx_reset(isp, icmdp); 1813 break; 1814 case ISCSI_CMD_TYPE_LOGOUT: 1815 rval = iscsi_tx_logout(isp, icmdp); 1816 break; 1817 case ISCSI_CMD_TYPE_TEXT: 1818 rval = iscsi_tx_text(isp, icmdp); 1819 break; 1820 default: 1821 cmn_err(CE_WARN, "iscsi_tx_cmd: invalid cmdtype: %d", 1822 icmdp->cmd_type); 1823 ASSERT(FALSE); 1824 } 1825 1826 ASSERT(!mutex_owned(&isp->sess_queue_pending.mutex)); 1827 return (rval); 1828 } 1829 1830 /* 1831 * a variable length cdb can be up to 16K, but we obviously don't want 1832 * to put that on the stack; go with 200 bytes; if we get something 1833 * bigger than that we will kmem_alloc a buffer 1834 */ 1835 #define DEF_CDB_LEN 200 1836 1837 /* 1838 * given the size of the cdb, return how many bytes the header takes, 1839 * which is the sizeof addl_hdr_t + the CDB size, minus the 16 bytes 1840 * stored in the basic header, minus sizeof (ahs_extscb) 1841 */ 1842 #define ADDLHDRSZ(x) (sizeof (iscsi_addl_hdr_t) + (x) - \ 1843 16 - 4) 1844 1845 static void 1846 iscsi_tx_init_hdr(iscsi_sess_t *isp, iscsi_conn_t *icp, 1847 iscsi_text_hdr_t *ihp, int opcode, iscsi_cmd_t *icmdp) 1848 { 1849 ihp->opcode = opcode; 1850 ihp->itt = icmdp->cmd_itt; 1851 mutex_enter(&isp->sess_cmdsn_mutex); 1852 icmdp->cmd_sn = isp->sess_cmdsn; 1853 ihp->cmdsn = htonl(isp->sess_cmdsn); 1854 isp->sess_cmdsn++; 1855 mutex_exit(&isp->sess_cmdsn_mutex); 1856 ihp->expstatsn = htonl(icp->conn_expstatsn); 1857 icp->conn_laststatsn = icp->conn_expstatsn; 1858 } 1859 1860 1861 static void 1862 iscsi_tx_scsi_data(iscsi_cmd_t *icmdp, iscsi_scsi_cmd_hdr_t *ihp, 1863 iscsi_conn_t *icp, idm_pdu_t *pdu) 1864 { 1865 struct buf *bp = NULL; 1866 size_t buflen = 0; 1867 uint32_t first_burst_length = 0; 1868 struct scsi_pkt *pkt; 1869 1870 pkt = icmdp->cmd_un.scsi.pkt; 1871 bp = icmdp->cmd_un.scsi.bp; 1872 if ((bp != NULL) && bp->b_bcount) { 1873 buflen = bp->b_bcount; 1874 first_burst_length = 1875 icp->conn_params.first_burst_length; 1876 1877 if (bp->b_flags & B_READ) { 1878 ihp->flags = ISCSI_FLAG_FINAL; 1879 /* 1880 * fix problem where OS sends bp (B_READ & 1881 * b_bcount!=0) for a TUR or START_STOP. 1882 * (comment came from cisco code.) 1883 */ 1884 if ((pkt->pkt_cdbp[0] != SCMD_TEST_UNIT_READY) && 1885 (pkt->pkt_cdbp[0] != SCMD_START_STOP)) { 1886 ihp->flags |= ISCSI_FLAG_CMD_READ; 1887 ihp->data_length = htonl(buflen); 1888 } 1889 } else { 1890 ihp->flags = ISCSI_FLAG_CMD_WRITE; 1891 /* 1892 * FinalBit on the the iSCSI PDU denotes this 1893 * is the last PDU in the sequence. 1894 * 1895 * initial_r2t = true means R2T is required 1896 * for additional PDU, so there will be no more 1897 * unsolicited PDUs following 1898 */ 1899 if (icp->conn_params.initial_r2t) { 1900 ihp->flags |= ISCSI_FLAG_FINAL; 1901 } 1902 1903 /* Check if we should send ImmediateData */ 1904 if (icp->conn_params.immediate_data) { 1905 pdu->isp_data = 1906 (uint8_t *)icmdp-> 1907 cmd_un.scsi.bp->b_un.b_addr; 1908 1909 pdu->isp_datalen = MIN(MIN(buflen, 1910 first_burst_length), 1911 icmdp->cmd_conn->conn_params. 1912 max_xmit_data_seg_len); 1913 1914 /* 1915 * if everything fits immediate, or 1916 * we can send all burst data immediate 1917 * (not unsol), set F 1918 */ 1919 /* 1920 * XXX This doesn't look right -- it's not 1921 * clear how we can handle transmitting 1922 * any unsolicited data. It looks like 1923 * we only support immediate data. So what 1924 * happens if we don't set ISCSI_FLAG_FINAL? 1925 * 1926 * Unless there's magic code somewhere that 1927 * is sending the remaining PDU's we should 1928 * simply set ISCSI_FLAG_FINAL and forget 1929 * about sending unsolicited data. The big 1930 * win is the immediate data anyway for small 1931 * PDU's. 1932 */ 1933 if ((pdu->isp_datalen == buflen) || 1934 (pdu->isp_datalen == first_burst_length)) { 1935 ihp->flags |= ISCSI_FLAG_FINAL; 1936 } 1937 1938 hton24(ihp->dlength, pdu->isp_datalen); 1939 } 1940 /* total data transfer length */ 1941 ihp->data_length = htonl(buflen); 1942 } 1943 } else { 1944 ihp->flags = ISCSI_FLAG_FINAL; 1945 } 1946 icmdp->cmd_un.scsi.data_transferred += pdu->isp_datalen; 1947 /* XXX How is this different from the code above? */ 1948 /* will idm send the next data command up to burst length? */ 1949 /* send the burstlen if we haven't sent immediate data */ 1950 /* CRM: should idm send difference min(buflen, first_burst) and imm? */ 1951 /* (MIN(first_burst_length, buflen) - imdata > 0) */ 1952 /* CRM_LATER: change this to generate unsolicited pdu */ 1953 if ((buflen > 0) && 1954 ((bp->b_flags & B_READ) == 0) && 1955 (icp->conn_params.initial_r2t == 0) && 1956 pdu->isp_datalen == 0) { 1957 1958 pdu->isp_datalen = MIN(first_burst_length, buflen); 1959 if ((pdu->isp_datalen == buflen) || 1960 (pdu->isp_datalen == first_burst_length)) { 1961 ihp->flags |= ISCSI_FLAG_FINAL; 1962 } 1963 pdu->isp_data = (uint8_t *)icmdp->cmd_un.scsi.bp->b_un.b_addr; 1964 hton24(ihp->dlength, pdu->isp_datalen); 1965 } 1966 } 1967 1968 static void 1969 iscsi_tx_scsi_init_pkt(iscsi_cmd_t *icmdp, iscsi_scsi_cmd_hdr_t *ihp) 1970 { 1971 struct scsi_pkt *pkt; 1972 1973 pkt = icmdp->cmd_un.scsi.pkt; 1974 pkt->pkt_state = (STATE_GOT_BUS | STATE_GOT_TARGET); 1975 pkt->pkt_reason = CMD_INCOMPLETE; 1976 1977 /* tagged queuing */ 1978 if (pkt->pkt_flags & FLAG_HTAG) { 1979 ihp->flags |= ISCSI_ATTR_HEAD_OF_QUEUE; 1980 } else if (pkt->pkt_flags & FLAG_OTAG) { 1981 ihp->flags |= ISCSI_ATTR_ORDERED; 1982 } else if (pkt->pkt_flags & FLAG_STAG) { 1983 ihp->flags |= ISCSI_ATTR_SIMPLE; 1984 } else { 1985 /* ihp->flags |= ISCSI_ATTR_UNTAGGED; */ 1986 /* EMPTY */ 1987 } 1988 1989 /* iscsi states lun is based on spc.2 */ 1990 ISCSI_LUN_BYTE_COPY(ihp->lun, icmdp->cmd_un.scsi.lun); 1991 1992 if (icmdp->cmd_un.scsi.cmdlen <= 16) { 1993 /* copy the SCSI Command Block into the PDU */ 1994 bcopy(pkt->pkt_cdbp, ihp->scb, 1995 icmdp->cmd_un.scsi.cmdlen); 1996 } else { 1997 iscsi_addl_hdr_t *iahp; 1998 1999 iahp = (iscsi_addl_hdr_t *)ihp; 2000 2001 ihp->hlength = (ADDLHDRSZ(icmdp->cmd_un.scsi.cmdlen) - 2002 sizeof (iscsi_scsi_cmd_hdr_t) + 3) / 4; 2003 iahp->ahs_hlen_hi = 0; 2004 iahp->ahs_hlen_lo = (icmdp->cmd_un.scsi.cmdlen - 15); 2005 iahp->ahs_key = 0x01; 2006 iahp->ahs_resv = 0; 2007 bcopy(pkt->pkt_cdbp, ihp->scb, 16); 2008 bcopy(((char *)pkt->pkt_cdbp) + 16, &iahp->ahs_extscb[0], 2009 icmdp->cmd_un.scsi.cmdlen); 2010 } 2011 2012 /* 2013 * Update all values before transfering. 2014 * We should never touch the icmdp after 2015 * transfering if there is no more data 2016 * to send. The only case the idm_pdu_tx() 2017 * will fail is a on a connection disconnect 2018 * in that case the command will be flushed. 2019 */ 2020 pkt->pkt_state |= STATE_SENT_CMD; 2021 } 2022 2023 static void 2024 iscsi_tx_scsi_init_task(iscsi_cmd_t *icmdp, iscsi_conn_t *icp, 2025 iscsi_scsi_cmd_hdr_t *ihp) 2026 { 2027 idm_task_t *itp; 2028 struct buf *bp = NULL; 2029 uint32_t data_length; 2030 2031 bp = icmdp->cmd_un.scsi.bp; 2032 2033 itp = icmdp->cmd_itp; 2034 ASSERT(itp != NULL); 2035 data_length = ntohl(ihp->data_length); 2036 ISCSI_IO_LOG(CE_NOTE, 2037 "DEBUG: iscsi_tx_init_task: task_start: %p idt_tt: %x cmdsn: %x " 2038 "sess_cmdsn: %x cmd: %p " 2039 "cmdtype: %d datalen: %u", 2040 (void *)itp, itp->idt_tt, ihp->cmdsn, icp->conn_sess->sess_cmdsn, 2041 (void *)icmdp, icmdp->cmd_type, data_length); 2042 if (data_length > 0) { 2043 if (bp->b_flags & B_READ) { 2044 icmdp->cmd_un.scsi.ibp_ibuf = 2045 idm_buf_alloc(icp->conn_ic, 2046 bp->b_un.b_addr, bp->b_bcount); 2047 if (icmdp->cmd_un.scsi.ibp_ibuf) 2048 idm_buf_bind_in(itp, 2049 icmdp->cmd_un.scsi.ibp_ibuf); 2050 } else { 2051 icmdp->cmd_un.scsi.ibp_obuf = 2052 idm_buf_alloc(icp->conn_ic, 2053 bp->b_un.b_addr, bp->b_bcount); 2054 if (icmdp->cmd_un.scsi.ibp_obuf) 2055 idm_buf_bind_out(itp, 2056 icmdp->cmd_un.scsi.ibp_obuf); 2057 } 2058 ISCSI_IO_LOG(CE_NOTE, 2059 "DEBUG: pdu_tx: task_start(%s): %p ic: %p idt_tt: %x " 2060 "cmdsn: %x sess_cmdsn: %x sess_expcmdsn: %x obuf: %p " 2061 "cmdp: %p cmdtype: %d " 2062 "buflen: %lu " "bpaddr: %p datalen: %u ", 2063 bp->b_flags & B_READ ? "B_READ" : "B_WRITE", 2064 (void *)itp, (void *)icp->conn_ic, 2065 itp->idt_tt, ihp->cmdsn, 2066 icp->conn_sess->sess_cmdsn, 2067 icp->conn_sess->sess_expcmdsn, 2068 (void *)icmdp->cmd_un.scsi.ibp_ibuf, 2069 (void *)icmdp, icmdp->cmd_type, bp->b_bcount, 2070 (void *)bp->b_un.b_addr, 2071 data_length); 2072 } 2073 2074 /* 2075 * Task is now active 2076 */ 2077 idm_task_start(itp, ISCSI_INI_TASK_TTT); 2078 } 2079 2080 /* 2081 * iscsi_tx_scsi - 2082 * 2083 */ 2084 static iscsi_status_t 2085 iscsi_tx_scsi(iscsi_sess_t *isp, iscsi_cmd_t *icmdp) 2086 { 2087 iscsi_status_t rval = ISCSI_STATUS_SUCCESS; 2088 iscsi_conn_t *icp = NULL; 2089 struct scsi_pkt *pkt = NULL; 2090 iscsi_scsi_cmd_hdr_t *ihp = NULL; 2091 int cdblen = 0; 2092 idm_pdu_t *pdu; 2093 int len; 2094 2095 ASSERT(isp != NULL); 2096 ASSERT(icmdp != NULL); 2097 2098 pdu = kmem_zalloc(sizeof (idm_pdu_t), KM_SLEEP); 2099 2100 pkt = icmdp->cmd_un.scsi.pkt; 2101 ASSERT(pkt != NULL); 2102 icp = icmdp->cmd_conn; 2103 ASSERT(icp != NULL); 2104 2105 /* Reset counts in case we are on a retry */ 2106 icmdp->cmd_un.scsi.data_transferred = 0; 2107 2108 if (icmdp->cmd_un.scsi.cmdlen > DEF_CDB_LEN) { 2109 cdblen = icmdp->cmd_un.scsi.cmdlen; 2110 ihp = kmem_zalloc(ADDLHDRSZ(cdblen), KM_SLEEP); 2111 len = ADDLHDRSZ(cdblen); 2112 } else { 2113 /* 2114 * only bzero the basic header; the additional header 2115 * will be set up correctly later, if needed 2116 */ 2117 ihp = kmem_zalloc(sizeof (iscsi_scsi_cmd_hdr_t), KM_SLEEP); 2118 len = sizeof (iscsi_scsi_cmd_hdr_t); 2119 } 2120 2121 iscsi_tx_init_hdr(isp, icp, (iscsi_text_hdr_t *)ihp, 2122 ISCSI_OP_SCSI_CMD, icmdp); 2123 2124 idm_pdu_init(pdu, icp->conn_ic, (void *)icmdp, &iscsi_tx_done); 2125 idm_pdu_init_hdr(pdu, (uint8_t *)ihp, len); 2126 pdu->isp_data = NULL; 2127 pdu->isp_datalen = 0; 2128 2129 /* 2130 * Sestion 12.11 of the iSCSI specification has a good table 2131 * describing when uncolicited data and/or immediate data 2132 * should be sent. 2133 */ 2134 2135 iscsi_tx_scsi_data(icmdp, ihp, icp, pdu); 2136 2137 iscsi_tx_scsi_init_pkt(icmdp, ihp); 2138 2139 /* Calls idm_task_start */ 2140 iscsi_tx_scsi_init_task(icmdp, icp, ihp); 2141 2142 mutex_exit(&isp->sess_queue_pending.mutex); 2143 2144 idm_pdu_tx(pdu); 2145 2146 icmdp->cmd_misc_flags |= ISCSI_CMD_MISCFLAG_SENT; 2147 2148 return (rval); 2149 } 2150 2151 2152 /* ARGSUSED */ 2153 static void 2154 iscsi_tx_done(idm_pdu_t *pdu, idm_status_t status) 2155 { 2156 kmem_free((iscsi_hdr_t *)pdu->isp_hdr, pdu->isp_hdrlen); 2157 kmem_free(pdu, sizeof (idm_pdu_t)); 2158 } 2159 2160 2161 static void 2162 iscsi_tx_pdu(iscsi_conn_t *icp, int opcode, void *hdr, int hdrlen, 2163 iscsi_cmd_t *icmdp) 2164 { 2165 idm_pdu_t *tx_pdu; 2166 iscsi_hdr_t *ihp = (iscsi_hdr_t *)hdr; 2167 2168 tx_pdu = kmem_zalloc(sizeof (idm_pdu_t), KM_SLEEP); 2169 ASSERT(tx_pdu != NULL); 2170 2171 idm_pdu_init(tx_pdu, icp->conn_ic, icmdp, &iscsi_tx_done); 2172 idm_pdu_init_hdr(tx_pdu, hdr, hdrlen); 2173 if (opcode == ISCSI_OP_TEXT_CMD) { 2174 idm_pdu_init_data(tx_pdu, 2175 (uint8_t *)icmdp->cmd_un.text.buf, 2176 ntoh24(ihp->dlength)); 2177 } 2178 2179 mutex_exit(&icp->conn_sess->sess_queue_pending.mutex); 2180 idm_pdu_tx(tx_pdu); 2181 icmdp->cmd_misc_flags |= ISCSI_CMD_MISCFLAG_SENT; 2182 } 2183 2184 2185 /* 2186 * iscsi_tx_nop - 2187 * 2188 */ 2189 static iscsi_status_t 2190 iscsi_tx_nop(iscsi_sess_t *isp, iscsi_cmd_t *icmdp) 2191 { 2192 iscsi_status_t rval = ISCSI_STATUS_SUCCESS; 2193 iscsi_conn_t *icp = NULL; 2194 iscsi_nop_out_hdr_t *inohp; 2195 2196 ASSERT(isp != NULL); 2197 ASSERT(icmdp != NULL); 2198 icp = icmdp->cmd_conn; 2199 ASSERT(icp != NULL); 2200 2201 inohp = kmem_zalloc(sizeof (iscsi_nop_out_hdr_t), KM_SLEEP); 2202 ASSERT(inohp != NULL); 2203 2204 inohp->opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE; 2205 inohp->flags = ISCSI_FLAG_FINAL; 2206 inohp->itt = icmdp->cmd_itt; 2207 inohp->ttt = icmdp->cmd_ttt; 2208 mutex_enter(&isp->sess_cmdsn_mutex); 2209 icmdp->cmd_sn = isp->sess_cmdsn; 2210 inohp->cmdsn = htonl(isp->sess_cmdsn); 2211 mutex_exit(&isp->sess_cmdsn_mutex); 2212 inohp->expstatsn = htonl(icp->conn_expstatsn); 2213 icp->conn_laststatsn = icp->conn_expstatsn; 2214 iscsi_tx_pdu(icp, ISCSI_OP_NOOP_OUT, inohp, 2215 sizeof (iscsi_nop_out_hdr_t), icmdp); 2216 return (rval); 2217 } 2218 2219 2220 /* 2221 * iscsi_tx_abort - 2222 * 2223 */ 2224 static iscsi_status_t 2225 iscsi_tx_abort(iscsi_sess_t *isp, iscsi_cmd_t *icmdp) 2226 { 2227 iscsi_status_t rval = ISCSI_STATUS_SUCCESS; 2228 iscsi_conn_t *icp = NULL; 2229 iscsi_scsi_task_mgt_hdr_t *istmh; 2230 2231 ASSERT(isp != NULL); 2232 ASSERT(icmdp != NULL); 2233 icp = icmdp->cmd_conn; 2234 ASSERT(icp != NULL); 2235 2236 istmh = kmem_zalloc(sizeof (iscsi_scsi_task_mgt_hdr_t), KM_SLEEP); 2237 ASSERT(istmh != NULL); 2238 mutex_enter(&isp->sess_cmdsn_mutex); 2239 icmdp->cmd_sn = isp->sess_cmdsn; 2240 istmh->cmdsn = htonl(isp->sess_cmdsn); 2241 mutex_exit(&isp->sess_cmdsn_mutex); 2242 istmh->expstatsn = htonl(icp->conn_expstatsn); 2243 icp->conn_laststatsn = icp->conn_expstatsn; 2244 istmh->itt = icmdp->cmd_itt; 2245 istmh->opcode = ISCSI_OP_SCSI_TASK_MGT_MSG | ISCSI_OP_IMMEDIATE; 2246 istmh->function = ISCSI_FLAG_FINAL | ISCSI_TM_FUNC_ABORT_TASK; 2247 ISCSI_LUN_BYTE_COPY(istmh->lun, 2248 icmdp->cmd_un.abort.icmdp->cmd_un.scsi.lun); 2249 istmh->rtt = icmdp->cmd_un.abort.icmdp->cmd_itt; 2250 iscsi_tx_pdu(icp, ISCSI_OP_SCSI_TASK_MGT_MSG, istmh, 2251 sizeof (iscsi_scsi_task_mgt_hdr_t), icmdp); 2252 2253 return (rval); 2254 } 2255 2256 2257 /* 2258 * iscsi_tx_reset - 2259 * 2260 */ 2261 static iscsi_status_t 2262 iscsi_tx_reset(iscsi_sess_t *isp, iscsi_cmd_t *icmdp) 2263 { 2264 iscsi_status_t rval = ISCSI_STATUS_SUCCESS; 2265 iscsi_conn_t *icp = NULL; 2266 iscsi_scsi_task_mgt_hdr_t *istmh; 2267 2268 ASSERT(isp != NULL); 2269 ASSERT(icmdp != NULL); 2270 icp = icmdp->cmd_conn; 2271 ASSERT(icp != NULL); 2272 2273 istmh = kmem_zalloc(sizeof (iscsi_scsi_task_mgt_hdr_t), KM_SLEEP); 2274 ASSERT(istmh != NULL); 2275 istmh->opcode = ISCSI_OP_SCSI_TASK_MGT_MSG | ISCSI_OP_IMMEDIATE; 2276 mutex_enter(&isp->sess_cmdsn_mutex); 2277 icmdp->cmd_sn = isp->sess_cmdsn; 2278 istmh->cmdsn = htonl(isp->sess_cmdsn); 2279 mutex_exit(&isp->sess_cmdsn_mutex); 2280 istmh->expstatsn = htonl(icp->conn_expstatsn); 2281 istmh->itt = icmdp->cmd_itt; 2282 2283 switch (icmdp->cmd_un.reset.level) { 2284 case RESET_LUN: 2285 istmh->function = ISCSI_FLAG_FINAL | 2286 ISCSI_TM_FUNC_LOGICAL_UNIT_RESET; 2287 ISCSI_LUN_BYTE_COPY(istmh->lun, icmdp->cmd_lun->lun_num); 2288 break; 2289 case RESET_TARGET: 2290 case RESET_BUS: 2291 istmh->function = ISCSI_FLAG_FINAL | 2292 ISCSI_TM_FUNC_TARGET_WARM_RESET; 2293 break; 2294 default: 2295 /* unsupported / unknown level */ 2296 ASSERT(FALSE); 2297 break; 2298 } 2299 2300 iscsi_tx_pdu(icp, ISCSI_OP_SCSI_TASK_MGT_MSG, istmh, 2301 sizeof (iscsi_scsi_task_mgt_hdr_t), icmdp); 2302 2303 return (rval); 2304 } 2305 2306 2307 /* 2308 * iscsi_tx_logout - 2309 * 2310 */ 2311 static iscsi_status_t 2312 iscsi_tx_logout(iscsi_sess_t *isp, iscsi_cmd_t *icmdp) 2313 { 2314 iscsi_status_t rval = ISCSI_STATUS_SUCCESS; 2315 iscsi_conn_t *icp = NULL; 2316 iscsi_logout_hdr_t *ilh; 2317 2318 ASSERT(isp != NULL); 2319 ASSERT(icmdp != NULL); 2320 icp = icmdp->cmd_conn; 2321 ASSERT(icp != NULL); 2322 2323 ilh = kmem_zalloc(sizeof (iscsi_logout_hdr_t), KM_SLEEP); 2324 ilh->opcode = ISCSI_OP_LOGOUT_CMD | ISCSI_OP_IMMEDIATE; 2325 ilh->flags = ISCSI_FLAG_FINAL | ISCSI_LOGOUT_REASON_CLOSE_SESSION; 2326 ilh->itt = icmdp->cmd_itt; 2327 ilh->cid = icp->conn_cid; 2328 mutex_enter(&isp->sess_cmdsn_mutex); 2329 icmdp->cmd_sn = isp->sess_cmdsn; 2330 ilh->cmdsn = htonl(isp->sess_cmdsn); 2331 mutex_exit(&isp->sess_cmdsn_mutex); 2332 ilh->expstatsn = htonl(icp->conn_expstatsn); 2333 iscsi_tx_pdu(icp, ISCSI_OP_LOGOUT_CMD, ilh, 2334 sizeof (iscsi_logout_hdr_t), icmdp); 2335 2336 return (rval); 2337 } 2338 2339 /* 2340 * iscsi_tx_text - setup iSCSI text request header and send PDU with 2341 * data given in the buffer attached to the command. For a single 2342 * text request, the target may need to send its response in multiple 2343 * text response. In this case, empty text requests are sent after 2344 * each received response to notify the target the initiator is ready 2345 * for more response. For the initial request, the data_len field in 2346 * the text specific portion of a command is set to the amount of data 2347 * the initiator wants to send as part of the request. If additional 2348 * empty text requests are required for long responses, the data_len 2349 * field is set to 0 by the iscsi_handle_text function. 2350 */ 2351 static iscsi_status_t 2352 iscsi_tx_text(iscsi_sess_t *isp, iscsi_cmd_t *icmdp) 2353 { 2354 iscsi_status_t rval = ISCSI_STATUS_SUCCESS; 2355 iscsi_conn_t *icp = NULL; 2356 iscsi_text_hdr_t *ith; 2357 2358 ASSERT(icmdp != NULL); 2359 icp = icmdp->cmd_conn; 2360 ASSERT(icp != NULL); 2361 2362 ith = kmem_zalloc(sizeof (iscsi_text_hdr_t), KM_SLEEP); 2363 ASSERT(ith != NULL); 2364 ith->flags = ISCSI_FLAG_FINAL; 2365 hton24(ith->dlength, icmdp->cmd_un.text.data_len); 2366 ith->ttt = icmdp->cmd_un.text.ttt; 2367 iscsi_tx_init_hdr(isp, icp, (iscsi_text_hdr_t *)ith, 2368 ISCSI_OP_TEXT_CMD, icmdp); 2369 bcopy(icmdp->cmd_un.text.lun, ith->rsvd4, sizeof (ith->rsvd4)); 2370 2371 iscsi_tx_pdu(icp, ISCSI_OP_TEXT_CMD, ith, sizeof (iscsi_text_hdr_t), 2372 icmdp); 2373 2374 return (rval); 2375 } 2376 2377 /* 2378 * +--------------------------------------------------------------------+ 2379 * | End of protocol send routines | 2380 * +--------------------------------------------------------------------+ 2381 */ 2382 2383 /* 2384 * iscsi_handle_abort - 2385 * 2386 */ 2387 void 2388 iscsi_handle_abort(void *arg) 2389 { 2390 iscsi_sess_t *isp = NULL; 2391 iscsi_cmd_t *icmdp = (iscsi_cmd_t *)arg; 2392 iscsi_cmd_t *new_icmdp; 2393 iscsi_conn_t *icp; 2394 2395 ASSERT(icmdp != NULL); 2396 icp = icmdp->cmd_conn; 2397 ASSERT(icp != NULL); 2398 isp = icp->conn_sess; 2399 ASSERT(isp != NULL); 2400 2401 /* there should only be one abort */ 2402 ASSERT(icmdp->cmd_un.scsi.abort_icmdp == NULL); 2403 2404 new_icmdp = iscsi_cmd_alloc(icp, KM_SLEEP); 2405 new_icmdp->cmd_type = ISCSI_CMD_TYPE_ABORT; 2406 new_icmdp->cmd_lun = icmdp->cmd_lun; 2407 new_icmdp->cmd_un.abort.icmdp = icmdp; 2408 new_icmdp->cmd_conn = icmdp->cmd_conn; 2409 icmdp->cmd_un.scsi.abort_icmdp = new_icmdp; 2410 2411 /* pending queue mutex is already held by timeout_checks */ 2412 iscsi_cmd_state_machine(new_icmdp, ISCSI_CMD_EVENT_E1, isp); 2413 } 2414 2415 /* 2416 * Callback from IDM indicating that the task has been suspended or aborted. 2417 */ 2418 void 2419 iscsi_task_aborted(idm_task_t *idt, idm_status_t status) 2420 { 2421 iscsi_cmd_t *icmdp = idt->idt_private; 2422 iscsi_conn_t *icp = icmdp->cmd_conn; 2423 iscsi_sess_t *isp = icp->conn_sess; 2424 2425 ASSERT(icmdp->cmd_conn != NULL); 2426 2427 switch (status) { 2428 case IDM_STATUS_SUSPENDED: 2429 /* 2430 * If the task is suspended, it may be aborted later, 2431 * so we can ignore this notification. 2432 */ 2433 break; 2434 2435 case IDM_STATUS_ABORTED: 2436 mutex_enter(&icp->conn_queue_active.mutex); 2437 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E9, isp); 2438 mutex_exit(&icp->conn_queue_active.mutex); 2439 break; 2440 2441 default: 2442 /* 2443 * Unexpected status. 2444 */ 2445 ASSERT(0); 2446 } 2447 2448 } 2449 2450 /* 2451 * iscsi_handle_nop - 2452 * 2453 */ 2454 static void 2455 iscsi_handle_nop(iscsi_conn_t *icp, uint32_t itt, uint32_t ttt) 2456 { 2457 iscsi_sess_t *isp = NULL; 2458 iscsi_cmd_t *icmdp = NULL; 2459 2460 ASSERT(icp != NULL); 2461 isp = icp->conn_sess; 2462 ASSERT(isp != NULL); 2463 2464 icmdp = iscsi_cmd_alloc(icp, KM_NOSLEEP); 2465 if (icmdp == NULL) { 2466 return; 2467 } 2468 2469 icmdp->cmd_type = ISCSI_CMD_TYPE_NOP; 2470 icmdp->cmd_itt = itt; 2471 icmdp->cmd_ttt = ttt; 2472 icmdp->cmd_lun = NULL; 2473 icp->conn_nop_lbolt = ddi_get_lbolt(); 2474 2475 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp); 2476 } 2477 2478 /* 2479 * iscsi_handle_reset - send reset request to the target 2480 * 2481 */ 2482 iscsi_status_t 2483 iscsi_handle_reset(iscsi_sess_t *isp, int level, iscsi_lun_t *ilp) 2484 { 2485 iscsi_status_t rval = ISCSI_STATUS_SUCCESS; 2486 iscsi_conn_t *icp; 2487 iscsi_cmd_t icmd; 2488 2489 ASSERT(isp != NULL); 2490 2491 if (level == RESET_LUN) { 2492 rw_enter(&isp->sess_lun_list_rwlock, RW_WRITER); 2493 ASSERT(ilp != NULL); 2494 if (ilp->lun_state & ISCSI_LUN_STATE_BUSY) { 2495 rw_exit(&isp->sess_lun_list_rwlock); 2496 return (ISCSI_STATUS_SUCCESS); 2497 } 2498 ilp->lun_state |= ISCSI_LUN_STATE_BUSY; 2499 rw_exit(&isp->sess_lun_list_rwlock); 2500 } else { 2501 mutex_enter(&isp->sess_reset_mutex); 2502 if (isp->sess_reset_in_progress == B_TRUE) { 2503 /* 2504 * If the reset is in progress, it is unnecessary 2505 * to send reset to the target redunantly. 2506 */ 2507 mutex_exit(&isp->sess_reset_mutex); 2508 return (ISCSI_STATUS_SUCCESS); 2509 } 2510 isp->sess_reset_in_progress = B_TRUE; 2511 mutex_exit(&isp->sess_reset_mutex); 2512 } 2513 2514 bzero(&icmd, sizeof (iscsi_cmd_t)); 2515 icmd.cmd_sig = ISCSI_SIG_CMD; 2516 icmd.cmd_state = ISCSI_CMD_STATE_FREE; 2517 icmd.cmd_type = ISCSI_CMD_TYPE_RESET; 2518 icmd.cmd_lun = ilp; 2519 icmd.cmd_un.reset.level = level; 2520 icmd.cmd_result = ISCSI_STATUS_SUCCESS; 2521 icmd.cmd_completed = B_FALSE; 2522 icmd.cmd_un.reset.response = SCSI_TCP_TM_RESP_COMPLETE; 2523 2524 mutex_init(&icmd.cmd_mutex, NULL, MUTEX_DRIVER, NULL); 2525 cv_init(&icmd.cmd_completion, NULL, CV_DRIVER, NULL); 2526 /* 2527 * If we received an IO and we are not in the 2528 * LOGGED_IN state we are in the process of 2529 * failing. Just respond that we are BUSY. 2530 */ 2531 mutex_enter(&isp->sess_state_mutex); 2532 if (!ISCSI_SESS_STATE_FULL_FEATURE(isp->sess_state)) { 2533 /* We aren't connected to the target fake success */ 2534 mutex_exit(&isp->sess_state_mutex); 2535 2536 if (level == RESET_LUN) { 2537 rw_enter(&isp->sess_lun_list_rwlock, RW_WRITER); 2538 ilp->lun_state &= ~ISCSI_LUN_STATE_BUSY; 2539 rw_exit(&isp->sess_lun_list_rwlock); 2540 } else { 2541 mutex_enter(&isp->sess_reset_mutex); 2542 isp->sess_reset_in_progress = B_FALSE; 2543 mutex_exit(&isp->sess_reset_mutex); 2544 } 2545 2546 return (ISCSI_STATUS_SUCCESS); 2547 } 2548 2549 mutex_enter(&isp->sess_queue_pending.mutex); 2550 iscsi_cmd_state_machine(&icmd, ISCSI_CMD_EVENT_E1, isp); 2551 mutex_exit(&isp->sess_queue_pending.mutex); 2552 mutex_exit(&isp->sess_state_mutex); 2553 2554 /* stall until completed */ 2555 mutex_enter(&icmd.cmd_mutex); 2556 while (icmd.cmd_completed == B_FALSE) { 2557 cv_wait(&icmd.cmd_completion, &icmd.cmd_mutex); 2558 } 2559 mutex_exit(&icmd.cmd_mutex); 2560 2561 /* copy rval */ 2562 rval = icmd.cmd_result; 2563 2564 if (rval == ISCSI_STATUS_SUCCESS) { 2565 /* 2566 * Reset was successful. We need to flush 2567 * all active IOs. 2568 */ 2569 rw_enter(&isp->sess_conn_list_rwlock, RW_READER); 2570 icp = isp->sess_conn_list; 2571 while (icp != NULL) { 2572 iscsi_cmd_t *t_icmdp = NULL; 2573 iscsi_cmd_t *next_icmdp = NULL; 2574 2575 mutex_enter(&icp->conn_queue_active.mutex); 2576 t_icmdp = icp->conn_queue_active.head; 2577 while (t_icmdp != NULL) { 2578 next_icmdp = t_icmdp->cmd_next; 2579 mutex_enter(&t_icmdp->cmd_mutex); 2580 if (!(t_icmdp->cmd_misc_flags & 2581 ISCSI_CMD_MISCFLAG_SENT)) { 2582 /* 2583 * Although this command is in the 2584 * active queue, it has not been sent. 2585 * Skip it. 2586 */ 2587 mutex_exit(&t_icmdp->cmd_mutex); 2588 t_icmdp = next_icmdp; 2589 continue; 2590 } 2591 if (level == RESET_LUN) { 2592 if (icmd.cmd_lun == NULL || 2593 t_icmdp->cmd_lun == NULL || 2594 (icmd.cmd_lun->lun_num != 2595 t_icmdp->cmd_lun->lun_num)) { 2596 mutex_exit(&t_icmdp->cmd_mutex); 2597 t_icmdp = next_icmdp; 2598 continue; 2599 } 2600 } 2601 2602 if (icmd.cmd_sn == t_icmdp->cmd_sn) { 2603 /* 2604 * This command may be replied with 2605 * UA sense key later. So currently 2606 * it is not a suitable time to flush 2607 * it. Mark its flag with FLUSH. There 2608 * is no harm to keep it for a while. 2609 */ 2610 t_icmdp->cmd_misc_flags |= 2611 ISCSI_CMD_MISCFLAG_FLUSH; 2612 if (t_icmdp->cmd_type == 2613 ISCSI_CMD_TYPE_SCSI) { 2614 t_icmdp->cmd_un.scsi.pkt_stat |= 2615 STAT_BUS_RESET; 2616 } 2617 mutex_exit(&t_icmdp->cmd_mutex); 2618 } else if ((icmd.cmd_sn > t_icmdp->cmd_sn) || 2619 ((t_icmdp->cmd_sn - icmd.cmd_sn) > 2620 ISCSI_CMD_SN_WRAP)) { 2621 /* 2622 * This reset request must act on all 2623 * the commnds from the same session 2624 * having a CmdSN lower than the task 2625 * mangement CmdSN. So flush these 2626 * commands here. 2627 */ 2628 if (t_icmdp->cmd_type == 2629 ISCSI_CMD_TYPE_SCSI) { 2630 t_icmdp->cmd_un.scsi.pkt_stat |= 2631 STAT_BUS_RESET; 2632 } 2633 mutex_exit(&t_icmdp->cmd_mutex); 2634 iscsi_cmd_state_machine(t_icmdp, 2635 ISCSI_CMD_EVENT_E7, isp); 2636 } else { 2637 mutex_exit(&t_icmdp->cmd_mutex); 2638 } 2639 2640 t_icmdp = next_icmdp; 2641 } 2642 2643 mutex_exit(&icp->conn_queue_active.mutex); 2644 icp = icp->conn_next; 2645 } 2646 rw_exit(&isp->sess_conn_list_rwlock); 2647 } 2648 2649 /* clean up */ 2650 cv_destroy(&icmd.cmd_completion); 2651 mutex_destroy(&icmd.cmd_mutex); 2652 2653 if (level == RESET_LUN) { 2654 rw_enter(&isp->sess_lun_list_rwlock, RW_WRITER); 2655 ilp->lun_state &= ~ISCSI_LUN_STATE_BUSY; 2656 rw_exit(&isp->sess_lun_list_rwlock); 2657 } else { 2658 mutex_enter(&isp->sess_reset_mutex); 2659 isp->sess_reset_in_progress = B_FALSE; 2660 mutex_exit(&isp->sess_reset_mutex); 2661 } 2662 2663 return (rval); 2664 } 2665 2666 /* 2667 * iscsi_logout_start - task handler for deferred logout 2668 * Acquire a hold before call, released in iscsi_handle_logout 2669 */ 2670 static void 2671 iscsi_logout_start(void *arg) 2672 { 2673 iscsi_task_t *itp = (iscsi_task_t *)arg; 2674 iscsi_conn_t *icp; 2675 2676 icp = (iscsi_conn_t *)itp->t_arg; 2677 2678 mutex_enter(&icp->conn_state_mutex); 2679 (void) iscsi_handle_logout(icp); 2680 mutex_exit(&icp->conn_state_mutex); 2681 } 2682 2683 /* 2684 * iscsi_handle_logout - This function will issue a logout for 2685 * the session from a specific connection. 2686 * Acquire idm_conn_hold before call. Released internally. 2687 */ 2688 iscsi_status_t 2689 iscsi_handle_logout(iscsi_conn_t *icp) 2690 { 2691 iscsi_sess_t *isp; 2692 idm_conn_t *ic; 2693 iscsi_cmd_t *icmdp; 2694 int rval; 2695 2696 ASSERT(icp != NULL); 2697 isp = icp->conn_sess; 2698 ic = icp->conn_ic; 2699 ASSERT(isp != NULL); 2700 ASSERT(isp->sess_hba != NULL); 2701 ASSERT(mutex_owned(&icp->conn_state_mutex)); 2702 2703 /* 2704 * If the connection has already gone down (e.g. if the transport 2705 * failed between when this LOGOUT was generated and now) then we 2706 * can and must skip sending the LOGOUT. Check the same condition 2707 * we use below to determine that connection has "settled". 2708 */ 2709 if ((icp->conn_state == ISCSI_CONN_STATE_FREE) || 2710 (icp->conn_state == ISCSI_CONN_STATE_FAILED) || 2711 (icp->conn_state == ISCSI_CONN_STATE_POLLING)) { 2712 idm_conn_rele(ic); 2713 return (0); 2714 } 2715 2716 icmdp = iscsi_cmd_alloc(icp, KM_SLEEP); 2717 ASSERT(icmdp != NULL); 2718 icmdp->cmd_type = ISCSI_CMD_TYPE_LOGOUT; 2719 icmdp->cmd_result = ISCSI_STATUS_SUCCESS; 2720 icmdp->cmd_completed = B_FALSE; 2721 2722 mutex_enter(&isp->sess_queue_pending.mutex); 2723 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp); 2724 mutex_exit(&isp->sess_queue_pending.mutex); 2725 2726 /* 2727 * release connection state mutex to avoid a deadlock. This 2728 * function is called from within the connection state 2729 * machine with the lock held. When the logout response is 2730 * received another call to the connection state machine 2731 * occurs which causes the deadlock 2732 */ 2733 mutex_exit(&icp->conn_state_mutex); 2734 2735 /* stall until completed */ 2736 mutex_enter(&icmdp->cmd_mutex); 2737 while (icmdp->cmd_completed == B_FALSE) { 2738 cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex); 2739 } 2740 mutex_exit(&icmdp->cmd_mutex); 2741 mutex_enter(&icp->conn_state_mutex); 2742 2743 /* copy rval */ 2744 rval = icmdp->cmd_result; 2745 2746 /* clean up */ 2747 iscsi_cmd_free(icmdp); 2748 2749 if (rval != 0) { 2750 /* If the logout failed then drop the connection */ 2751 idm_ini_conn_disconnect(icp->conn_ic); 2752 } 2753 2754 /* stall until connection settles */ 2755 while ((icp->conn_state != ISCSI_CONN_STATE_FREE) && 2756 (icp->conn_state != ISCSI_CONN_STATE_FAILED) && 2757 (icp->conn_state != ISCSI_CONN_STATE_POLLING)) { 2758 /* wait for transition */ 2759 cv_wait(&icp->conn_state_change, &icp->conn_state_mutex); 2760 } 2761 2762 idm_conn_rele(ic); 2763 2764 /* 2765 * Return value reflects whether the logout command completed -- 2766 * regardless of the return value the connection is closed and 2767 * ready for reconnection. 2768 */ 2769 return (rval); 2770 } 2771 2772 2773 /* 2774 * iscsi_handle_text - main control function for iSCSI text requests. This 2775 * function handles allocating the command, sending initial text request, and 2776 * handling long response sequence. 2777 * If a data overflow condition occurs, iscsi_handle_text continues to 2778 * receive responses until the all data has been recieved. This allows 2779 * the full data length to be returned to the caller. 2780 */ 2781 iscsi_status_t 2782 iscsi_handle_text(iscsi_conn_t *icp, char *buf, uint32_t buf_len, 2783 uint32_t data_len, uint32_t *rx_data_len) 2784 { 2785 iscsi_sess_t *isp; 2786 iscsi_cmd_t *icmdp; 2787 iscsi_status_t rval = ISCSI_STATUS_SUCCESS; 2788 2789 ASSERT(icp != NULL); 2790 ASSERT(buf != NULL); 2791 ASSERT(rx_data_len != NULL); 2792 2793 isp = icp->conn_sess; 2794 ASSERT(isp != NULL); 2795 2796 /* 2797 * Ensure data for text request command is not greater 2798 * than the negotiated maximum receive data seqment length. 2799 * 2800 * Although iSCSI allows for long text requests (multiple 2801 * pdus), this function places a restriction on text 2802 * requests to ensure it is handled by a single PDU. 2803 */ 2804 if (data_len > icp->conn_params.max_xmit_data_seg_len) { 2805 return (ISCSI_STATUS_CMD_FAILED); 2806 } 2807 2808 icmdp = iscsi_cmd_alloc(icp, KM_SLEEP); 2809 ASSERT(icmdp != NULL); 2810 2811 icmdp->cmd_type = ISCSI_CMD_TYPE_TEXT; 2812 icmdp->cmd_result = ISCSI_STATUS_SUCCESS; 2813 icmdp->cmd_misc_flags &= ~ISCSI_CMD_MISCFLAG_FREE; 2814 icmdp->cmd_completed = B_FALSE; 2815 2816 icmdp->cmd_un.text.buf = buf; 2817 icmdp->cmd_un.text.buf_len = buf_len; 2818 icmdp->cmd_un.text.offset = 0; 2819 icmdp->cmd_un.text.data_len = data_len; 2820 icmdp->cmd_un.text.total_rx_len = 0; 2821 icmdp->cmd_un.text.ttt = ISCSI_RSVD_TASK_TAG; 2822 icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_INITIAL_REQ; 2823 2824 long_text_response: 2825 mutex_enter(&isp->sess_state_mutex); 2826 if (!ISCSI_SESS_STATE_FULL_FEATURE(isp->sess_state)) { 2827 iscsi_cmd_free(icmdp); 2828 mutex_exit(&isp->sess_state_mutex); 2829 return (ISCSI_STATUS_NO_CONN_LOGGED_IN); 2830 } 2831 2832 mutex_enter(&isp->sess_queue_pending.mutex); 2833 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp); 2834 mutex_exit(&isp->sess_queue_pending.mutex); 2835 mutex_exit(&isp->sess_state_mutex); 2836 2837 /* stall until completed */ 2838 mutex_enter(&icmdp->cmd_mutex); 2839 while (icmdp->cmd_completed == B_FALSE) { 2840 cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex); 2841 } 2842 mutex_exit(&icmdp->cmd_mutex); 2843 2844 /* 2845 * check if error occured. If data overflow occured, continue on 2846 * to ensure we get all data so that the full data length can be 2847 * returned to the user 2848 */ 2849 if ((icmdp->cmd_result != ISCSI_STATUS_SUCCESS) && 2850 (icmdp->cmd_result != ISCSI_STATUS_DATA_OVERFLOW)) { 2851 cmn_err(CE_NOTE, "iscsi: SendTarget discovery failed (%d)", 2852 icmdp->cmd_result); 2853 rval = icmdp->cmd_result; 2854 iscsi_cmd_free(icmdp); 2855 return (rval); 2856 } 2857 2858 /* check if this was a partial text PDU */ 2859 if (icmdp->cmd_un.text.stage != ISCSI_CMD_TEXT_FINAL_RSP) { 2860 /* 2861 * If a paritial text rexponse received, send an empty 2862 * text request. This follows the behaviour specified 2863 * in RFC3720 regarding long text responses. 2864 */ 2865 icmdp->cmd_misc_flags &= ~ISCSI_CMD_MISCFLAG_FREE; 2866 icmdp->cmd_completed = B_FALSE; 2867 icmdp->cmd_un.text.data_len = 0; 2868 icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_CONTINUATION; 2869 goto long_text_response; 2870 } 2871 2872 /* 2873 * set total received data length. If data overflow this would be 2874 * amount of data that would have been received if buffer large 2875 * enough. 2876 */ 2877 *rx_data_len = icmdp->cmd_un.text.total_rx_len; 2878 2879 /* copy rval */ 2880 rval = icmdp->cmd_result; 2881 2882 /* clean up */ 2883 iscsi_cmd_free(icmdp); 2884 2885 return (rval); 2886 } 2887 2888 /* 2889 * iscsi_handle_passthru - This function is used to send a uscsi_cmd 2890 * to a specific target lun. This routine is used for internal purposes 2891 * during enumeration and via the ISCSI_USCSICMD IOCTL. We restrict 2892 * the CDBs that can be issued to a target/lun to INQUIRY, REPORT_LUNS, 2893 * and READ_CAPACITY for security purposes. 2894 * 2895 * The logic here is broken into three phases. 2896 * 1) Allocate and initialize a pkt/icmdp 2897 * 2) Send the pkt/icmdp 2898 * 3) cv_wait for completion 2899 */ 2900 iscsi_status_t 2901 iscsi_handle_passthru(iscsi_sess_t *isp, uint16_t lun, struct uscsi_cmd *ucmdp) 2902 { 2903 iscsi_status_t rval = ISCSI_STATUS_SUCCESS; 2904 iscsi_cmd_t *icmdp = NULL; 2905 struct scsi_pkt *pkt = NULL; 2906 struct buf *bp = NULL; 2907 struct scsi_arq_status *arqstat = NULL; 2908 int rqlen = SENSE_LENGTH; 2909 2910 ASSERT(isp != NULL); 2911 ASSERT(ucmdp != NULL); 2912 2913 /* 2914 * If the caller didn't provide a sense buffer we need 2915 * to allocation one to get the scsi status. 2916 */ 2917 if (ucmdp->uscsi_rqlen > SENSE_LENGTH) { 2918 rqlen = ucmdp->uscsi_rqlen; 2919 } 2920 2921 /* 2922 * Step 1. Setup structs - KM_SLEEP will always succeed 2923 */ 2924 bp = kmem_zalloc(sizeof (struct buf), KM_SLEEP); 2925 ASSERT(bp != NULL); 2926 pkt = kmem_zalloc(sizeof (struct scsi_pkt), KM_SLEEP); 2927 ASSERT(pkt != NULL); 2928 icmdp = iscsi_cmd_alloc(NULL, KM_SLEEP); 2929 ASSERT(icmdp != NULL); 2930 2931 /* setup bp structure */ 2932 bp->b_flags = B_READ; 2933 bp->b_bcount = ucmdp->uscsi_buflen; 2934 bp->b_un.b_addr = ucmdp->uscsi_bufaddr; 2935 2936 /* setup scsi_pkt structure */ 2937 pkt->pkt_ha_private = icmdp; 2938 pkt->pkt_scbp = kmem_zalloc(rqlen, KM_SLEEP); 2939 pkt->pkt_cdbp = kmem_zalloc(ucmdp->uscsi_cdblen, KM_SLEEP); 2940 /* callback routine for passthru, will wake cv_wait */ 2941 pkt->pkt_comp = iscsi_handle_passthru_callback; 2942 pkt->pkt_time = ucmdp->uscsi_timeout; 2943 2944 /* setup iscsi_cmd structure */ 2945 icmdp->cmd_lun = NULL; 2946 icmdp->cmd_type = ISCSI_CMD_TYPE_SCSI; 2947 icmdp->cmd_un.scsi.lun = lun; 2948 icmdp->cmd_un.scsi.pkt = pkt; 2949 icmdp->cmd_un.scsi.bp = bp; 2950 bcopy(ucmdp->uscsi_cdb, pkt->pkt_cdbp, ucmdp->uscsi_cdblen); 2951 icmdp->cmd_un.scsi.cmdlen = ucmdp->uscsi_cdblen; 2952 icmdp->cmd_un.scsi.statuslen = rqlen; 2953 icmdp->cmd_crc_error_seen = B_FALSE; 2954 icmdp->cmd_completed = B_FALSE; 2955 icmdp->cmd_result = ISCSI_STATUS_SUCCESS; 2956 2957 /* 2958 * Step 2. Push IO onto pending queue. If we aren't in 2959 * FULL_FEATURE we need to fail the IO. 2960 */ 2961 mutex_enter(&isp->sess_state_mutex); 2962 if (!ISCSI_SESS_STATE_FULL_FEATURE(isp->sess_state)) { 2963 mutex_exit(&isp->sess_state_mutex); 2964 2965 iscsi_cmd_free(icmdp); 2966 kmem_free(pkt->pkt_cdbp, ucmdp->uscsi_cdblen); 2967 kmem_free(pkt->pkt_scbp, rqlen); 2968 kmem_free(pkt, sizeof (struct scsi_pkt)); 2969 kmem_free(bp, sizeof (struct buf)); 2970 2971 return (ISCSI_STATUS_CMD_FAILED); 2972 } 2973 2974 mutex_enter(&isp->sess_queue_pending.mutex); 2975 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp); 2976 mutex_exit(&isp->sess_queue_pending.mutex); 2977 mutex_exit(&isp->sess_state_mutex); 2978 2979 /* 2980 * Step 3. Wait on cv_wait for completion routine 2981 */ 2982 mutex_enter(&icmdp->cmd_mutex); 2983 while (icmdp->cmd_completed == B_FALSE) { 2984 cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex); 2985 } 2986 mutex_exit(&icmdp->cmd_mutex); 2987 2988 /* copy rval */ 2989 rval = icmdp->cmd_result; 2990 2991 ucmdp->uscsi_resid = pkt->pkt_resid; 2992 2993 /* update scsi status */ 2994 arqstat = (struct scsi_arq_status *)pkt->pkt_scbp; 2995 ucmdp->uscsi_status = ((char *)&arqstat->sts_status)[0]; 2996 2997 /* copy request sense buffers if caller gave space */ 2998 if ((ucmdp->uscsi_rqlen > 0) && 2999 (ucmdp->uscsi_rqbuf != NULL)) { 3000 bcopy(arqstat, ucmdp->uscsi_rqbuf, 3001 MIN(sizeof (struct scsi_arq_status), rqlen)); 3002 } 3003 3004 /* clean up */ 3005 iscsi_cmd_free(icmdp); 3006 kmem_free(pkt->pkt_cdbp, ucmdp->uscsi_cdblen); 3007 kmem_free(pkt->pkt_scbp, rqlen); 3008 kmem_free(pkt, sizeof (struct scsi_pkt)); 3009 kmem_free(bp, sizeof (struct buf)); 3010 3011 return (rval); 3012 } 3013 3014 3015 /* 3016 * iscsi_handle_passthru_callback - 3017 * 3018 */ 3019 static void 3020 iscsi_handle_passthru_callback(struct scsi_pkt *pkt) 3021 { 3022 iscsi_cmd_t *icmdp = NULL; 3023 3024 ASSERT(pkt != NULL); 3025 icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private; 3026 ASSERT(icmdp != NULL); 3027 3028 mutex_enter(&icmdp->cmd_mutex); 3029 icmdp->cmd_completed = B_TRUE; 3030 icmdp->cmd_result = ISCSI_STATUS_SUCCESS; 3031 cv_broadcast(&icmdp->cmd_completion); 3032 mutex_exit(&icmdp->cmd_mutex); 3033 3034 } 3035 3036 /* 3037 * IDM callbacks 3038 */ 3039 void 3040 iscsi_build_hdr(idm_task_t *idm_task, idm_pdu_t *pdu, uint8_t opcode) 3041 { 3042 iscsi_cmd_t *icmdp = idm_task->idt_private; 3043 iscsi_conn_t *icp = icmdp->cmd_conn; 3044 iscsi_data_hdr_t *ihp = (iscsi_data_hdr_t *)pdu->isp_hdr; 3045 3046 mutex_enter(&icmdp->cmd_mutex); 3047 if (opcode == ISCSI_OP_SCSI_DATA) { 3048 uint32_t data_sn; 3049 uint32_t lun; 3050 icmdp = idm_task->idt_private; 3051 icp = icmdp->cmd_conn; 3052 ihp->opcode = opcode; 3053 ihp->itt = icmdp->cmd_itt; 3054 ihp->ttt = idm_task->idt_r2t_ttt; 3055 ihp->expstatsn = htonl(icp->conn_expstatsn); 3056 icp->conn_laststatsn = icp->conn_expstatsn; 3057 data_sn = ntohl(ihp->datasn); 3058 data_sn++; 3059 lun = icmdp->cmd_un.scsi.lun; 3060 ISCSI_LUN_BYTE_COPY(ihp->lun, lun); 3061 /* CRM: upate_flow_control */ 3062 ISCSI_IO_LOG(CE_NOTE, "DEBUG: iscsi_build_hdr" 3063 "(ISCSI_OP_SCSI_DATA): task: %p icp: %p ic: %p itt: %x " 3064 "exp: %d data_sn: %d", (void *)idm_task, (void *)icp, 3065 (void *)icp->conn_ic, ihp->itt, icp->conn_expstatsn, 3066 data_sn); 3067 } else { 3068 cmn_err(CE_WARN, "iscsi_build_hdr: unprocessed build " 3069 "header opcode: %x", opcode); 3070 } 3071 mutex_exit(&icmdp->cmd_mutex); 3072 } 3073 3074 static void 3075 iscsi_process_rsp_status(iscsi_sess_t *isp, iscsi_conn_t *icp, 3076 idm_status_t status) 3077 { 3078 switch (status) { 3079 case IDM_STATUS_SUCCESS: 3080 if ((isp->sess_state == ISCSI_SESS_STATE_IN_FLUSH) && 3081 (icp->conn_queue_active.count == 0)) { 3082 iscsi_drop_conn_cleanup(icp); 3083 } 3084 break; 3085 case IDM_STATUS_PROTOCOL_ERROR: 3086 KSTAT_INC_CONN_ERR_PROTOCOL(icp); 3087 iscsi_drop_conn_cleanup(icp); 3088 break; 3089 default: 3090 break; 3091 } 3092 } 3093 3094 static void 3095 iscsi_drop_conn_cleanup(iscsi_conn_t *icp) { 3096 mutex_enter(&icp->conn_state_mutex); 3097 idm_ini_conn_disconnect(icp->conn_ic); 3098 mutex_exit(&icp->conn_state_mutex); 3099 } 3100 3101 void 3102 iscsi_rx_error_pdu(idm_conn_t *ic, idm_pdu_t *pdu, idm_status_t status) 3103 { 3104 iscsi_conn_t *icp = (iscsi_conn_t *)ic->ic_handle; 3105 iscsi_sess_t *isp; 3106 3107 ASSERT(icp != NULL); 3108 isp = icp->conn_sess; 3109 ASSERT(isp != NULL); 3110 iscsi_process_rsp_status(isp, icp, status); 3111 idm_pdu_complete(pdu, status); 3112 } 3113 3114 void 3115 iscsi_rx_misc_pdu(idm_conn_t *ic, idm_pdu_t *pdu) 3116 { 3117 iscsi_conn_t *icp; 3118 iscsi_hdr_t *ihp = (iscsi_hdr_t *)pdu->isp_hdr; 3119 iscsi_sess_t *isp; 3120 idm_status_t status; 3121 3122 icp = ic->ic_handle; 3123 isp = icp->conn_sess; 3124 isp->sess_rx_lbolt = icp->conn_rx_lbolt = ddi_get_lbolt(); 3125 switch (ihp->opcode & ISCSI_OPCODE_MASK) { 3126 case ISCSI_OP_LOGIN_RSP: 3127 status = iscsi_rx_process_login_pdu(ic, pdu); 3128 idm_pdu_complete(pdu, status); 3129 break; 3130 case ISCSI_OP_LOGOUT_RSP: 3131 status = iscsi_rx_process_logout_rsp(ic, pdu); 3132 idm_pdu_complete(pdu, status); 3133 break; 3134 case ISCSI_OP_REJECT_MSG: 3135 status = iscsi_rx_process_reject_rsp(ic, pdu); 3136 break; 3137 case ISCSI_OP_SCSI_TASK_MGT_RSP: 3138 status = iscsi_rx_process_task_mgt_rsp(ic, pdu); 3139 idm_pdu_complete(pdu, status); 3140 break; 3141 case ISCSI_OP_NOOP_IN: 3142 status = iscsi_rx_process_nop(ic, pdu); 3143 idm_pdu_complete(pdu, status); 3144 break; 3145 case ISCSI_OP_ASYNC_EVENT: 3146 status = iscsi_rx_process_async_rsp(ic, pdu); 3147 break; 3148 case ISCSI_OP_TEXT_RSP: 3149 status = iscsi_rx_process_text_rsp(ic, pdu); 3150 idm_pdu_complete(pdu, status); 3151 break; 3152 default: 3153 cmn_err(CE_WARN, "iscsi connection(%u) protocol error " 3154 "- received misc unsupported opcode 0x%02x", 3155 icp->conn_oid, ihp->opcode); 3156 status = IDM_STATUS_PROTOCOL_ERROR; 3157 break; 3158 } 3159 iscsi_process_rsp_status(isp, icp, status); 3160 } 3161 3162 /* 3163 * +--------------------------------------------------------------------+ 3164 * | Beginning of completion routines | 3165 * +--------------------------------------------------------------------+ 3166 */ 3167 3168 /* 3169 * iscsi_ic_thread - 3170 */ 3171 void 3172 iscsi_ic_thread(iscsi_thread_t *thread, void *arg) 3173 { 3174 iscsi_sess_t *isp = (iscsi_sess_t *)arg; 3175 int ret; 3176 iscsi_queue_t q; 3177 iscsi_cmd_t *icmdp; 3178 iscsi_cmd_t *next_icmdp; 3179 3180 ASSERT(isp != NULL); 3181 ASSERT(thread != NULL); 3182 ASSERT(thread->signature == SIG_ISCSI_THREAD); 3183 3184 for (;;) { 3185 3186 /* 3187 * We wait till iodone or somebody else wakes us up. 3188 */ 3189 ret = iscsi_thread_wait(thread, -1); 3190 3191 /* 3192 * The value should never be negative since we never timeout. 3193 */ 3194 ASSERT(ret >= 0); 3195 3196 q.count = 0; 3197 q.head = NULL; 3198 q.tail = NULL; 3199 mutex_enter(&isp->sess_queue_completion.mutex); 3200 icmdp = isp->sess_queue_completion.head; 3201 while (icmdp != NULL) { 3202 next_icmdp = icmdp->cmd_next; 3203 mutex_enter(&icmdp->cmd_mutex); 3204 /* 3205 * check if the associated r2t/abort has finished 3206 * yet. If not, don't complete the command. 3207 */ 3208 if ((icmdp->cmd_un.scsi.r2t_icmdp == NULL) && 3209 (icmdp->cmd_un.scsi.abort_icmdp == NULL)) { 3210 mutex_exit(&icmdp->cmd_mutex); 3211 (void) iscsi_dequeue_cmd(&isp-> 3212 sess_queue_completion.head, 3213 &isp->sess_queue_completion.tail, 3214 icmdp); 3215 --isp->sess_queue_completion.count; 3216 iscsi_enqueue_cmd_head(&q.head, 3217 &q.tail, icmdp); 3218 } else { 3219 mutex_exit(&icmdp->cmd_mutex); 3220 } 3221 icmdp = next_icmdp; 3222 } 3223 mutex_exit(&isp->sess_queue_completion.mutex); 3224 icmdp = q.head; 3225 while (icmdp != NULL) { 3226 next_icmdp = icmdp->cmd_next; 3227 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E8, isp); 3228 icmdp = next_icmdp; 3229 } 3230 3231 if (ret > 0) 3232 /* Somebody woke us up to work */ 3233 continue; 3234 else 3235 /* 3236 * Somebody woke us up to kill ourselves. We will 3237 * make sure, however that the completion queue is 3238 * empty before leaving. After we've done that it 3239 * is the originator of the signal that has to make 3240 * sure no other SCSI command is posted. 3241 */ 3242 break; 3243 } 3244 3245 } 3246 3247 /* 3248 * iscsi_iodone - 3249 * 3250 */ 3251 void 3252 iscsi_iodone(iscsi_sess_t *isp, iscsi_cmd_t *icmdp) 3253 { 3254 struct scsi_pkt *pkt = NULL; 3255 struct buf *bp = icmdp->cmd_un.scsi.bp; 3256 3257 ASSERT(isp != NULL); 3258 ASSERT(icmdp != NULL); 3259 pkt = icmdp->cmd_un.scsi.pkt; 3260 ASSERT(pkt != NULL); 3261 3262 ASSERT(icmdp->cmd_un.scsi.abort_icmdp == NULL); 3263 ASSERT(icmdp->cmd_un.scsi.r2t_icmdp == NULL); 3264 if (pkt->pkt_reason == CMD_CMPLT) { 3265 if (bp) { 3266 if (bp->b_flags & B_READ) { 3267 KSTAT_SESS_RX_IO_DONE(isp, bp->b_bcount); 3268 } else { 3269 KSTAT_SESS_TX_IO_DONE(isp, bp->b_bcount); 3270 } 3271 } 3272 } 3273 3274 if (pkt->pkt_flags & FLAG_NOINTR) { 3275 cv_broadcast(&icmdp->cmd_completion); 3276 mutex_exit(&icmdp->cmd_mutex); 3277 } else { 3278 /* 3279 * Release mutex. As soon as callback is 3280 * issued the caller may destroy the command. 3281 */ 3282 mutex_exit(&icmdp->cmd_mutex); 3283 /* 3284 * We can't just directly call the pk_comp routine. In 3285 * many error cases the target driver will use the calling 3286 * thread to re-drive error handling (reset, retries...) 3287 * back into the hba driver (iscsi). If the target redrives 3288 * a reset back into the iscsi driver off this thead we have 3289 * a chance of deadlocking. So instead use the io completion 3290 * thread. 3291 */ 3292 (*icmdp->cmd_un.scsi.pkt->pkt_comp)(icmdp->cmd_un.scsi.pkt); 3293 } 3294 } 3295 3296 /* 3297 * +--------------------------------------------------------------------+ 3298 * | End of completion routines | 3299 * +--------------------------------------------------------------------+ 3300 */ 3301 3302 /* 3303 * +--------------------------------------------------------------------+ 3304 * | Beginning of watchdog routines | 3305 * +--------------------------------------------------------------------+ 3306 */ 3307 3308 /* 3309 * iscsi_watchdog_thread - 3310 * 3311 */ 3312 void 3313 iscsi_wd_thread(iscsi_thread_t *thread, void *arg) 3314 { 3315 iscsi_sess_t *isp = (iscsi_sess_t *)arg; 3316 int rc = 1; 3317 3318 ASSERT(isp != NULL); 3319 3320 while (rc != NULL) { 3321 3322 iscsi_timeout_checks(isp); 3323 iscsi_nop_checks(isp); 3324 3325 rc = iscsi_thread_wait(thread, SEC_TO_TICK(1)); 3326 } 3327 } 3328 3329 /* 3330 * iscsi_timeout_checks - 3331 * 3332 */ 3333 static void 3334 iscsi_timeout_checks(iscsi_sess_t *isp) 3335 { 3336 clock_t now = ddi_get_lbolt(); 3337 iscsi_conn_t *icp; 3338 iscsi_cmd_t *icmdp, *nicmdp; 3339 3340 ASSERT(isp != NULL); 3341 3342 /* PENDING */ 3343 mutex_enter(&isp->sess_state_mutex); 3344 mutex_enter(&isp->sess_queue_pending.mutex); 3345 for (icmdp = isp->sess_queue_pending.head; 3346 icmdp; icmdp = nicmdp) { 3347 nicmdp = icmdp->cmd_next; 3348 3349 /* Skip entries with no timeout */ 3350 if (icmdp->cmd_lbolt_timeout == 0) 3351 continue; 3352 3353 /* 3354 * Skip pending queue entries for cmd_type values that depend 3355 * on having an open cmdsn window for successfull transition 3356 * from pending to the active (i.e. ones that depend on 3357 * sess_cmdsn .vs. sess_maxcmdsn). For them, the timer starts 3358 * when they are successfully moved to the active queue by 3359 * iscsi_cmd_state_pending() code. 3360 */ 3361 /* 3362 * If the cmd is stuck, at least give it a chance 3363 * to timeout 3364 */ 3365 if (((icmdp->cmd_type == ISCSI_CMD_TYPE_SCSI) || 3366 (icmdp->cmd_type == ISCSI_CMD_TYPE_TEXT)) && 3367 !(icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_STUCK)) 3368 continue; 3369 3370 /* Skip if timeout still in the future */ 3371 if (now <= icmdp->cmd_lbolt_timeout) 3372 continue; 3373 3374 /* timeout */ 3375 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E6, isp); 3376 } 3377 mutex_exit(&isp->sess_queue_pending.mutex); 3378 mutex_exit(&isp->sess_state_mutex); 3379 3380 rw_enter(&isp->sess_conn_list_rwlock, RW_READER); 3381 icp = isp->sess_conn_list; 3382 while (icp != NULL) { 3383 3384 icp->conn_timeout = B_FALSE; 3385 /* ACTIVE */ 3386 mutex_enter(&icp->conn_state_mutex); 3387 mutex_enter(&isp->sess_queue_pending.mutex); 3388 mutex_enter(&icp->conn_queue_active.mutex); 3389 for (icmdp = icp->conn_queue_active.head; 3390 icmdp; icmdp = nicmdp) { 3391 nicmdp = icmdp->cmd_next; 3392 3393 if (iscsi_nop_timeout_checks(icmdp) == B_TRUE) { 3394 icp->conn_timeout = B_TRUE; 3395 } 3396 3397 /* Skip entries with no timeout */ 3398 if (icmdp->cmd_lbolt_timeout == 0) 3399 continue; 3400 3401 /* 3402 * Skip if command is not active or not needed 3403 * to flush. 3404 */ 3405 if (icmdp->cmd_state != ISCSI_CMD_STATE_ACTIVE && 3406 !(icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_FLUSH)) 3407 continue; 3408 3409 /* Skip if timeout still in the future */ 3410 if (now <= icmdp->cmd_lbolt_timeout) 3411 continue; 3412 3413 if (icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_FLUSH) { 3414 /* 3415 * This command is left during target reset, 3416 * we can flush it now. 3417 */ 3418 iscsi_cmd_state_machine(icmdp, 3419 ISCSI_CMD_EVENT_E7, isp); 3420 } else if (icmdp->cmd_state == ISCSI_CMD_STATE_ACTIVE) { 3421 /* timeout */ 3422 iscsi_cmd_state_machine(icmdp, 3423 ISCSI_CMD_EVENT_E6, isp); 3424 } 3425 3426 } 3427 mutex_exit(&icp->conn_queue_active.mutex); 3428 mutex_exit(&isp->sess_queue_pending.mutex); 3429 mutex_exit(&icp->conn_state_mutex); 3430 3431 icp = icp->conn_next; 3432 } 3433 3434 icp = isp->sess_conn_list; 3435 while (icp != NULL) { 3436 if (icp->conn_timeout == B_TRUE) { 3437 /* timeout on this connect detected */ 3438 idm_ini_conn_disconnect(icp->conn_ic); 3439 icp->conn_timeout = B_FALSE; 3440 } 3441 icp = icp->conn_next; 3442 } 3443 rw_exit(&isp->sess_conn_list_rwlock); 3444 } 3445 3446 /* 3447 * iscsi_nop_checks - sends a NOP on idle connections 3448 * 3449 * This function walks the connections on a session and 3450 * issues NOPs on those connections that are in FULL 3451 * FEATURE mode and have not received data for the 3452 * time period specified by iscsi_nop_delay (global). 3453 */ 3454 static void 3455 iscsi_nop_checks(iscsi_sess_t *isp) 3456 { 3457 iscsi_conn_t *icp; 3458 3459 ASSERT(isp != NULL); 3460 3461 if (isp->sess_type == ISCSI_SESS_TYPE_DISCOVERY) { 3462 return; 3463 } 3464 3465 rw_enter(&isp->sess_conn_list_rwlock, RW_READER); 3466 icp = isp->sess_conn_act; 3467 if (icp != NULL) { 3468 3469 mutex_enter(&icp->conn_state_mutex); 3470 if ((ISCSI_CONN_STATE_FULL_FEATURE(icp->conn_state)) && 3471 (ddi_get_lbolt() > isp->sess_conn_act->conn_rx_lbolt + 3472 SEC_TO_TICK(iscsi_nop_delay)) && (ddi_get_lbolt() > 3473 isp->sess_conn_act->conn_nop_lbolt + 3474 SEC_TO_TICK(iscsi_nop_delay))) { 3475 3476 /* 3477 * We haven't received anything from the 3478 * target is a defined period of time, 3479 * send NOP to see if the target is alive. 3480 */ 3481 mutex_enter(&isp->sess_queue_pending.mutex); 3482 iscsi_handle_nop(isp->sess_conn_act, 3483 0, ISCSI_RSVD_TASK_TAG); 3484 mutex_exit(&isp->sess_queue_pending.mutex); 3485 } 3486 mutex_exit(&icp->conn_state_mutex); 3487 3488 icp = icp->conn_next; 3489 } 3490 rw_exit(&isp->sess_conn_list_rwlock); 3491 } 3492 3493 static boolean_t 3494 iscsi_nop_timeout_checks(iscsi_cmd_t *icmdp) 3495 { 3496 if (icmdp->cmd_type == ISCSI_CMD_TYPE_NOP) { 3497 if ((ddi_get_lbolt() - icmdp->cmd_lbolt_active) > 3498 SEC_TO_TICK(ISCSI_CONN_TIEMOUT_DETECT)) { 3499 return (B_TRUE); 3500 } else { 3501 return (B_FALSE); 3502 } 3503 } 3504 return (B_FALSE); 3505 } 3506 /* 3507 * +--------------------------------------------------------------------+ 3508 * | End of wd routines | 3509 * +--------------------------------------------------------------------+ 3510 */ 3511 3512 /* 3513 * iscsi_flush_cmd_after_reset - flush commands after reset 3514 * 3515 * Here we will flush all the commands in the same connection whose cmdsn is 3516 * less than the one received with the Unit Attention. 3517 */ 3518 static void 3519 iscsi_flush_cmd_after_reset(uint32_t cmd_sn, uint16_t lun_num, 3520 iscsi_conn_t *icp) 3521 { 3522 iscsi_cmd_t *t_icmdp = NULL; 3523 iscsi_cmd_t *next_icmdp = NULL; 3524 3525 ASSERT(icp != NULL); 3526 3527 t_icmdp = icp->conn_queue_active.head; 3528 while (t_icmdp != NULL) { 3529 next_icmdp = t_icmdp->cmd_next; 3530 mutex_enter(&t_icmdp->cmd_mutex); 3531 /* 3532 * We will flush the commands whose cmdsn is less than the one 3533 * got Unit Attention. 3534 * Here we will check for wrap by subtracting and compare to 3535 * 1/2 of a 32 bit number, if greater then we wrapped. 3536 */ 3537 if ((t_icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_SENT) && 3538 ((cmd_sn > t_icmdp->cmd_sn) || 3539 ((t_icmdp->cmd_sn - cmd_sn) > 3540 ISCSI_CMD_SN_WRAP))) { 3541 if (t_icmdp->cmd_lun != NULL && 3542 t_icmdp->cmd_lun->lun_num == lun_num) { 3543 t_icmdp->cmd_misc_flags |= 3544 ISCSI_CMD_MISCFLAG_FLUSH; 3545 if (t_icmdp->cmd_type == ISCSI_CMD_TYPE_SCSI) { 3546 t_icmdp->cmd_un.scsi.pkt_stat |= 3547 STAT_BUS_RESET; 3548 } 3549 } 3550 } 3551 mutex_exit(&t_icmdp->cmd_mutex); 3552 t_icmdp = next_icmdp; 3553 } 3554 } 3555 3556 /* 3557 * iscsi_decode_sense - decode the sense data in the cmd response 3558 * 3559 * Here we only care about Unit Attention with 0x29. 3560 */ 3561 static boolean_t 3562 iscsi_decode_sense(uint8_t *sense_data) 3563 { 3564 uint8_t sense_key = 0; 3565 uint8_t asc = 0; 3566 boolean_t affect = B_FALSE; 3567 3568 ASSERT(sense_data != NULL); 3569 3570 sense_key = scsi_sense_key(sense_data); 3571 switch (sense_key) { 3572 case KEY_UNIT_ATTENTION: 3573 asc = scsi_sense_asc(sense_data); 3574 switch (asc) { 3575 case ISCSI_SCSI_RESET_SENSE_CODE: 3576 /* 3577 * POWER ON, RESET, OR BUS_DEVICE RESET 3578 * OCCURRED 3579 */ 3580 affect = B_TRUE; 3581 break; 3582 default: 3583 /* 3584 * Currently we don't care 3585 * about other sense key. 3586 */ 3587 break; 3588 } 3589 break; 3590 default: 3591 /* 3592 * Currently we don't care 3593 * about other sense key. 3594 */ 3595 break; 3596 } 3597 return (affect); 3598 } 3599