1fcf3ce44SJohn Forte /*
2fcf3ce44SJohn Forte  * CDDL HEADER START
3fcf3ce44SJohn Forte  *
4fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7fcf3ce44SJohn Forte  *
8fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11fcf3ce44SJohn Forte  * and limitations under the License.
12fcf3ce44SJohn Forte  *
13fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce44SJohn Forte  *
19fcf3ce44SJohn Forte  * CDDL HEADER END
20fcf3ce44SJohn Forte  */
21fcf3ce44SJohn Forte /*
22fcf3ce44SJohn Forte  * Copyright 2000 by Cisco Systems, Inc.  All rights reserved.
231a1a84a3SPeter Dunlap  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24fcf3ce44SJohn Forte  * Use is subject to license terms.
25fcf3ce44SJohn Forte  *
26fcf3ce44SJohn Forte  * iSCSI Pseudo HBA Driver
27fcf3ce44SJohn Forte  */
28fcf3ce44SJohn Forte 
29fcf3ce44SJohn Forte #include <sys/socket.h>		/* networking stuff */
30fcf3ce44SJohn Forte #include <sys/t_kuser.h>	/* networking stuff */
31fcf3ce44SJohn Forte #include <sys/tihdr.h>		/* networking stuff */
32fcf3ce44SJohn Forte #include <sys/strsubr.h>	/* networking stuff */
33fcf3ce44SJohn Forte #include <netinet/tcp.h>	/* TCP_NODELAY */
34fcf3ce44SJohn Forte #include <sys/socketvar.h>	/* _ALLOC_SLEEP */
35fcf3ce44SJohn Forte #include <sys/strsun.h>		/* DB_TYPE() */
362b79d384Sbing zhao - Sun Microsystems - Beijing China #include <sys/scsi/generic/sense.h>
37fcf3ce44SJohn Forte 
38fcf3ce44SJohn Forte #include "iscsi.h"		/* iscsi driver */
391a1a84a3SPeter Dunlap #include <sys/iscsi_protocol.h>	/* iscsi protocol */
40fcf3ce44SJohn Forte 
4130e7468fSPeter Dunlap #define	ISCSI_INI_TASK_TTT	0xffffffff
42*aff4bce5Syi zhang - Sun Microsystems - Beijing China #define	ISCSI_CONN_TIEMOUT_DETECT	20
4330e7468fSPeter Dunlap 
4430e7468fSPeter Dunlap boolean_t iscsi_io_logging = B_FALSE;
4530e7468fSPeter Dunlap 
4630e7468fSPeter Dunlap #define	ISCSI_CHECK_SCSI_READ(ICHK_CMD, ICHK_HDR, ICHK_LEN, ICHK_TYPE)	\
4730e7468fSPeter Dunlap 	if (idm_pattern_checking)  {					\
4830e7468fSPeter Dunlap 		struct scsi_pkt *pkt = (ICHK_CMD)->cmd_un.scsi.pkt;	\
4930e7468fSPeter Dunlap 		if (((ICHK_HDR)->response == 0) && 			\
5030e7468fSPeter Dunlap 		    ((ICHK_HDR)->cmd_status == 0) &&			\
5130e7468fSPeter Dunlap 		    ((pkt->pkt_cdbp[0] == SCMD_READ_G1) ||		\
5230e7468fSPeter Dunlap 		    (pkt->pkt_cdbp[0] == SCMD_READ_G4) || 		\
5330e7468fSPeter Dunlap 		    (pkt->pkt_cdbp[0] == SCMD_READ) || 			\
5430e7468fSPeter Dunlap 		    (pkt->pkt_cdbp[0] == SCMD_READ_G5))) {		\
5530e7468fSPeter Dunlap 			idm_buf_t *idb = (ICHK_CMD)->cmd_un.scsi.ibp_ibuf; \
5630e7468fSPeter Dunlap 			IDM_BUFPAT_CHECK(idb, ICHK_LEN, ICHK_TYPE); \
5730e7468fSPeter Dunlap 		}						\
5830e7468fSPeter Dunlap 	}
5930e7468fSPeter Dunlap 
60fcf3ce44SJohn Forte /* generic io helpers */
61fcf3ce44SJohn Forte static uint32_t n2h24(uchar_t *ptr);
62fcf3ce44SJohn Forte static int iscsi_sna_lt(uint32_t n1, uint32_t n2);
6330e7468fSPeter Dunlap void iscsi_update_flow_control(iscsi_sess_t *isp,
64fcf3ce44SJohn Forte     uint32_t max, uint32_t exp);
6530e7468fSPeter Dunlap static iscsi_status_t iscsi_rx_process_scsi_itt_to_icmdp(iscsi_sess_t *isp,
6630e7468fSPeter Dunlap     idm_conn_t *ic, iscsi_scsi_rsp_hdr_t *ihp, iscsi_cmd_t **icmdp);
67fcf3ce44SJohn Forte static iscsi_status_t iscsi_rx_process_itt_to_icmdp(iscsi_sess_t *isp,
68fcf3ce44SJohn Forte     iscsi_hdr_t *ihp, iscsi_cmd_t **icmdp);
6930e7468fSPeter Dunlap static void iscsi_process_rsp_status(iscsi_sess_t *isp, iscsi_conn_t *icp,
7030e7468fSPeter Dunlap     idm_status_t status);
7130e7468fSPeter Dunlap static void iscsi_drop_conn_cleanup(iscsi_conn_t *icp);
72*aff4bce5Syi zhang - Sun Microsystems - Beijing China static boolean_t iscsi_nop_timeout_checks(iscsi_cmd_t *icmdp);
7330e7468fSPeter Dunlap /* callbacks from idm */
7430e7468fSPeter Dunlap static idm_pdu_cb_t iscsi_tx_done;
7530e7468fSPeter Dunlap 
7630e7468fSPeter Dunlap /* receivers */
7730e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_nop(idm_conn_t *ic, idm_pdu_t *pdu);
7830e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_data_rsp(idm_conn_t *ic,
7930e7468fSPeter Dunlap     idm_pdu_t *pdu);
8030e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_cmd_rsp(idm_conn_t *ic, idm_pdu_t *pdu);
8130e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_reject_rsp(idm_conn_t *ic,
8230e7468fSPeter Dunlap     idm_pdu_t *pdu);
8330e7468fSPeter Dunlap 
8430e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_rejected_tsk_mgt(idm_conn_t *ic,
8530e7468fSPeter Dunlap     iscsi_hdr_t *old_ihp);
8630e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_task_mgt_rsp(idm_conn_t *ic,
8730e7468fSPeter Dunlap     idm_pdu_t *pdu);
8830e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_logout_rsp(idm_conn_t *ic,
8930e7468fSPeter Dunlap     idm_pdu_t *pdu);
9030e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_async_rsp(idm_conn_t *ic,
9130e7468fSPeter Dunlap     idm_pdu_t *pdu);
9230e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_text_rsp(idm_conn_t *ic,
9330e7468fSPeter Dunlap     idm_pdu_t *pdu);
94fcf3ce44SJohn Forte 
95fcf3ce44SJohn Forte /* senders */
96fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_scsi(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
97fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_nop(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
98fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_abort(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
99fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_reset(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
100fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_logout(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
101fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_text(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
102fcf3ce44SJohn Forte 
103fcf3ce44SJohn Forte 
104fcf3ce44SJohn Forte /* helpers */
10530e7468fSPeter Dunlap static void iscsi_logout_start(void *arg);
106fcf3ce44SJohn Forte static void iscsi_handle_passthru_callback(struct scsi_pkt *pkt);
107fcf3ce44SJohn Forte static void iscsi_handle_nop(iscsi_conn_t *icp, uint32_t itt, uint32_t ttt);
108fcf3ce44SJohn Forte 
109fcf3ce44SJohn Forte static void iscsi_timeout_checks(iscsi_sess_t *isp);
110fcf3ce44SJohn Forte static void iscsi_nop_checks(iscsi_sess_t *isp);
1112b79d384Sbing zhao - Sun Microsystems - Beijing China static boolean_t iscsi_decode_sense(uint8_t *sense_data);
1122b79d384Sbing zhao - Sun Microsystems - Beijing China static void iscsi_flush_cmd_after_reset(uint32_t cmd_sn, uint16_t lun_num,
1132b79d384Sbing zhao - Sun Microsystems - Beijing China     iscsi_conn_t *icp);
114fcf3ce44SJohn Forte 
115fcf3ce44SJohn Forte /*
116fcf3ce44SJohn Forte  * This file contains the main guts of the iSCSI protocol layer.
117fcf3ce44SJohn Forte  * It's broken into 5 sections; Basic helper functions, RX IO path,
118fcf3ce44SJohn Forte  * TX IO path, Completion (IC) IO path, and watchdog (WD) routines.
119fcf3ce44SJohn Forte  *
120fcf3ce44SJohn Forte  * The IO flow model is similiar to the below diagram.  The
121fcf3ce44SJohn Forte  * iscsi session, connection and command state machines are used
122fcf3ce44SJohn Forte  * to drive IO through this flow diagram.  Reference those files
123fcf3ce44SJohn Forte  * to get a detailed description of their respective state models
124fcf3ce44SJohn Forte  * prior to their xxx_state_machine_function().
125fcf3ce44SJohn Forte  *
126fcf3ce44SJohn Forte  * tran_start() -> CMD_E1     TX_THREAD                   RX_THREAD
127fcf3ce44SJohn Forte  *                   |            T                           T
128fcf3ce44SJohn Forte  *                   V            T                           T
129fcf3ce44SJohn Forte  *                PENDING_Q  --CMD_E2--> ACTIVE_Q -      --CMD_E3--+
130fcf3ce44SJohn Forte  *                                T                \ C        T    |
131fcf3ce44SJohn Forte  *                                T                 \M        T    |
132fcf3ce44SJohn Forte  *                                                   D        T    |
133fcf3ce44SJohn Forte  *                                       WD_THREAD TT|TT      T    |
134fcf3ce44SJohn Forte  *                                                  /E        T    |
135fcf3ce44SJohn Forte  *                                                 / 6        T    |
136fcf3ce44SJohn Forte  *                                     ABORTING_Q<-      --CMD_E3--+
137fcf3ce44SJohn Forte  *                                                            T    |
138fcf3ce44SJohn Forte  *                                T                           T    |
139fcf3ce44SJohn Forte  *                                T                                |
140fcf3ce44SJohn Forte  *               callback()  <--CMD_E#-- COMPLETION_Q <------------+
141fcf3ce44SJohn Forte  *                                T
142fcf3ce44SJohn Forte  *                                T
143fcf3ce44SJohn Forte  *                            IC_THREAD
144fcf3ce44SJohn Forte  *
145fcf3ce44SJohn Forte  * External and internal command are ran thru this same state
146fcf3ce44SJohn Forte  * machine.  All commands enter the state machine by receiving an
147fcf3ce44SJohn Forte  * ISCSI_CMD_EVENT_E1.  This event places the command into the
148fcf3ce44SJohn Forte  * PENDING_Q.  Next when resources are available the TX_THREAD
149fcf3ce44SJohn Forte  * issues a E2 event on the command.  This sends the command
150fcf3ce44SJohn Forte  * to the TCP stack and places the command on the ACTIVE_Q.  While
151fcf3ce44SJohn Forte  * on the PENDIING_Q and ACTIVE_Q, the command is monitored via the
152fcf3ce44SJohn Forte  * WD_THREAD to ensure the pkt_time has not elapsed.  If elapsed the
153fcf3ce44SJohn Forte  * command is issued an E6(timeout) event which moves either (if pending)
154fcf3ce44SJohn Forte  * completed the command or (if active) moves the command to the
155fcf3ce44SJohn Forte  * aborting queue and issues a SCSI TASK MANAGEMENT ABORT command
156fcf3ce44SJohn Forte  * to cancel the IO request.  If the original command is completed
157fcf3ce44SJohn Forte  * or the TASK MANAGEMENT command completes the command is moved
158fcf3ce44SJohn Forte  * to the COMPLETION_Q via a E3 event.  The IC_THREAD then processes
159fcf3ce44SJohn Forte  * the COMPLETION_Q and issues the scsi_pkt callback.  This
160fcf3ce44SJohn Forte  * callback can not be processed directly from the RX_THREAD
161fcf3ce44SJohn Forte  * because the callback might call back into the iscsi driver
162fcf3ce44SJohn Forte  * causing a deadlock condition.
163fcf3ce44SJohn Forte  *
164fcf3ce44SJohn Forte  * For more details on the complete CMD state machine reference
165fcf3ce44SJohn Forte  * the state machine diagram in iscsi_cmd.c.  The connection state
166fcf3ce44SJohn Forte  * machine is driven via IO events in this file.  Then session
167fcf3ce44SJohn Forte  * events are driven by the connection events.  For complete
168fcf3ce44SJohn Forte  * details on these state machines reference iscsi_sess.c and
169fcf3ce44SJohn Forte  * iscsi_conn.c
170fcf3ce44SJohn Forte  */
171fcf3ce44SJohn Forte 
172fcf3ce44SJohn Forte 
173fcf3ce44SJohn Forte /*
174fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
175fcf3ce44SJohn Forte  * | io helper routines							|
176fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
177fcf3ce44SJohn Forte  */
178fcf3ce44SJohn Forte 
179fcf3ce44SJohn Forte /*
180fcf3ce44SJohn Forte  * n2h24 - native to host 24 bit integer translation.
181fcf3ce44SJohn Forte  */
182fcf3ce44SJohn Forte static uint32_t
183fcf3ce44SJohn Forte n2h24(uchar_t *ptr)
184fcf3ce44SJohn Forte {
185fcf3ce44SJohn Forte 	uint32_t idx;
186fcf3ce44SJohn Forte 	bcopy(ptr, &idx, 3);
187fcf3ce44SJohn Forte 	return (ntohl(idx) >> 8);
188fcf3ce44SJohn Forte }
189fcf3ce44SJohn Forte 
190fcf3ce44SJohn Forte /*
191fcf3ce44SJohn Forte  * iscsi_sna_lt - Serial Number Arithmetic, 32 bits, less than, RFC1982
192fcf3ce44SJohn Forte  */
193fcf3ce44SJohn Forte static int
194fcf3ce44SJohn Forte iscsi_sna_lt(uint32_t n1, uint32_t n2)
195fcf3ce44SJohn Forte {
196fcf3ce44SJohn Forte 	return ((n1 != n2) &&
197fcf3ce44SJohn Forte 	    (((n1 < n2) && ((n2 - n1) < ISCSI_SNA32_CHECK)) ||
198fcf3ce44SJohn Forte 	    ((n1 > n2) && ((n1 - n2) > ISCSI_SNA32_CHECK))));
199fcf3ce44SJohn Forte }
200fcf3ce44SJohn Forte 
201fcf3ce44SJohn Forte /*
202fcf3ce44SJohn Forte  * iscsi_sna_lte - Serial Number Arithmetic, 32 bits, less than or equal,
203fcf3ce44SJohn Forte  * RFC1982
204fcf3ce44SJohn Forte  */
205fcf3ce44SJohn Forte int
206fcf3ce44SJohn Forte iscsi_sna_lte(uint32_t n1, uint32_t n2)
207fcf3ce44SJohn Forte {
208fcf3ce44SJohn Forte 	return ((n1 == n2) ||
209fcf3ce44SJohn Forte 	    (((n1 < n2) && ((n2 - n1) < ISCSI_SNA32_CHECK)) ||
210fcf3ce44SJohn Forte 	    ((n1 > n2) && ((n1 - n2) > ISCSI_SNA32_CHECK))));
211fcf3ce44SJohn Forte }
212fcf3ce44SJohn Forte 
213fcf3ce44SJohn Forte /*
214fcf3ce44SJohn Forte  * iscsi_update_flow_control - Update expcmdsn and maxcmdsn iSCSI
215fcf3ce44SJohn Forte  * flow control information for a session
216fcf3ce44SJohn Forte  */
21730e7468fSPeter Dunlap void
218fcf3ce44SJohn Forte iscsi_update_flow_control(iscsi_sess_t *isp, uint32_t max, uint32_t exp)
219fcf3ce44SJohn Forte {
220fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
221fcf3ce44SJohn Forte 	ASSERT(mutex_owned(&isp->sess_cmdsn_mutex));
222fcf3ce44SJohn Forte 
223fcf3ce44SJohn Forte 	if (!iscsi_sna_lt(max, (exp - 1))) {
224fcf3ce44SJohn Forte 
225fcf3ce44SJohn Forte 		if (!iscsi_sna_lte(exp, isp->sess_expcmdsn)) {
226fcf3ce44SJohn Forte 			isp->sess_expcmdsn = exp;
227fcf3ce44SJohn Forte 		}
228fcf3ce44SJohn Forte 
229fcf3ce44SJohn Forte 		if (!iscsi_sna_lte(max, isp->sess_maxcmdsn)) {
230fcf3ce44SJohn Forte 			isp->sess_maxcmdsn = max;
231fcf3ce44SJohn Forte 			if (iscsi_sna_lte(isp->sess_cmdsn,
232fcf3ce44SJohn Forte 			    isp->sess_maxcmdsn)) {
233fcf3ce44SJohn Forte 				/*
234fcf3ce44SJohn Forte 				 * the window is open again - schedule
235fcf3ce44SJohn Forte 				 * to send any held tasks soon
236fcf3ce44SJohn Forte 				 */
237fcf3ce44SJohn Forte 				iscsi_sess_redrive_io(isp);
238fcf3ce44SJohn Forte 			}
239fcf3ce44SJohn Forte 		}
240fcf3ce44SJohn Forte 	}
241fcf3ce44SJohn Forte }
242fcf3ce44SJohn Forte 
243fcf3ce44SJohn Forte 
244fcf3ce44SJohn Forte /*
245fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
246fcf3ce44SJohn Forte  * | io receive and processing routines					|
247fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
248fcf3ce44SJohn Forte  */
249fcf3ce44SJohn Forte 
250fcf3ce44SJohn Forte /*
25130e7468fSPeter Dunlap  * iscsi_rx_scsi_rsp - called from idm
25230e7468fSPeter Dunlap  * For each opcode type fan out the processing.
253fcf3ce44SJohn Forte  */
254fcf3ce44SJohn Forte void
25530e7468fSPeter Dunlap iscsi_rx_scsi_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
256fcf3ce44SJohn Forte {
25730e7468fSPeter Dunlap 	iscsi_conn_t	*icp;
25830e7468fSPeter Dunlap 	iscsi_sess_t	*isp;
25930e7468fSPeter Dunlap 	iscsi_hdr_t	*ihp;
26030e7468fSPeter Dunlap 	idm_status_t	status;
261fcf3ce44SJohn Forte 
26230e7468fSPeter Dunlap 	ASSERT(ic != NULL);
26330e7468fSPeter Dunlap 	ASSERT(pdu != NULL);
26430e7468fSPeter Dunlap 	icp		= ic->ic_handle;
265fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
26630e7468fSPeter Dunlap 	ihp		= (iscsi_hdr_t *)pdu->isp_hdr;
267fcf3ce44SJohn Forte 	ASSERT(ihp != NULL);
26830e7468fSPeter Dunlap 	isp		= icp->conn_sess;
269fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
270fcf3ce44SJohn Forte 
27130e7468fSPeter Dunlap 	/* reset the session timer when we receive the response */
27230e7468fSPeter Dunlap 	isp->sess_rx_lbolt = icp->conn_rx_lbolt = ddi_get_lbolt();
273fcf3ce44SJohn Forte 
274fcf3ce44SJohn Forte 	/* fan out the hdr processing */
275fcf3ce44SJohn Forte 	switch (ihp->opcode & ISCSI_OPCODE_MASK) {
276fcf3ce44SJohn Forte 	case ISCSI_OP_SCSI_DATA_RSP:
27730e7468fSPeter Dunlap 		status = iscsi_rx_process_data_rsp(ic, pdu);
278fcf3ce44SJohn Forte 		break;
279fcf3ce44SJohn Forte 	case ISCSI_OP_SCSI_RSP:
28030e7468fSPeter Dunlap 		status = iscsi_rx_process_cmd_rsp(ic, pdu);
28130e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
282fcf3ce44SJohn Forte 		break;
283fcf3ce44SJohn Forte 	default:
284fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) protocol error - "
28530e7468fSPeter Dunlap 		    "received pdu with unsupported opcode 0x%02x",
286fcf3ce44SJohn Forte 		    icp->conn_oid, ihp->opcode);
28730e7468fSPeter Dunlap 		status = IDM_STATUS_PROTOCOL_ERROR;
288fcf3ce44SJohn Forte 	}
28930e7468fSPeter Dunlap 	iscsi_process_rsp_status(isp, icp, status);
290fcf3ce44SJohn Forte }
291fcf3ce44SJohn Forte 
29230e7468fSPeter Dunlap void
29330e7468fSPeter Dunlap iscsi_task_cleanup(int opcode, iscsi_cmd_t *icmdp)
294fcf3ce44SJohn Forte {
29530e7468fSPeter Dunlap 	struct buf 	*bp;
29630e7468fSPeter Dunlap 	idm_buf_t	*ibp, *obp;
29730e7468fSPeter Dunlap 	idm_task_t	*itp;
298fcf3ce44SJohn Forte 
29930e7468fSPeter Dunlap 	itp = icmdp->cmd_itp;
30030e7468fSPeter Dunlap 	ASSERT(itp != NULL);
30130e7468fSPeter Dunlap 	ASSERT((opcode == ISCSI_OP_SCSI_DATA_RSP) ||
30230e7468fSPeter Dunlap 	    (opcode == ISCSI_OP_SCSI_RSP));
303fcf3ce44SJohn Forte 
30430e7468fSPeter Dunlap 	bp = icmdp->cmd_un.scsi.bp;
30530e7468fSPeter Dunlap 	ibp = icmdp->cmd_un.scsi.ibp_ibuf;
30630e7468fSPeter Dunlap 	obp = icmdp->cmd_un.scsi.ibp_obuf;
30730e7468fSPeter Dunlap 	ISCSI_IO_LOG(CE_NOTE, "DEBUG: task_cleanup: itp: %p opcode: %d "
30830e7468fSPeter Dunlap 	    "icmdp: %p bp: %p ibp: %p", (void *)itp, opcode,
30930e7468fSPeter Dunlap 	    (void *)icmdp, (void *)bp, (void *)ibp);
31030e7468fSPeter Dunlap 	if (bp && bp->b_bcount) {
31130e7468fSPeter Dunlap 		if (ibp != NULL && bp->b_flags & B_READ) {
31230e7468fSPeter Dunlap 			idm_buf_unbind_in(itp, ibp);
31330e7468fSPeter Dunlap 			idm_buf_free(ibp);
31430e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.ibp_ibuf = NULL;
31530e7468fSPeter Dunlap 		} else if (obp != NULL && !(bp->b_flags & B_READ)) {
31630e7468fSPeter Dunlap 			idm_buf_unbind_out(itp, obp);
31730e7468fSPeter Dunlap 			idm_buf_free(obp);
31830e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.ibp_obuf = NULL;
319fcf3ce44SJohn Forte 		}
320fcf3ce44SJohn Forte 	}
321fcf3ce44SJohn Forte 
32230e7468fSPeter Dunlap 	idm_task_done(itp);
32330e7468fSPeter Dunlap }
32430e7468fSPeter Dunlap 
32530e7468fSPeter Dunlap idm_status_t
32630e7468fSPeter Dunlap iscsi_rx_chk(iscsi_conn_t *icp, iscsi_sess_t *isp,
32730e7468fSPeter Dunlap     iscsi_scsi_rsp_hdr_t *irhp, iscsi_cmd_t **icmdp)
32830e7468fSPeter Dunlap {
32930e7468fSPeter Dunlap 	iscsi_status_t rval;
33030e7468fSPeter Dunlap 
331fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
33230e7468fSPeter Dunlap 
33330e7468fSPeter Dunlap 	if (icp->conn_expstatsn == ntohl(irhp->statsn)) {
33430e7468fSPeter Dunlap 		icp->conn_expstatsn++;
33530e7468fSPeter Dunlap 	} else {
33630e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi connection(%u/%x) protocol error - "
33730e7468fSPeter Dunlap 		    "received status out of order itt:0x%x statsn:0x%x "
33830e7468fSPeter Dunlap 		    "expstatsn:0x%x", icp->conn_oid, irhp->opcode,
33930e7468fSPeter Dunlap 		    irhp->itt, ntohl(irhp->statsn), icp->conn_expstatsn);
340fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_cmdsn_mutex);
34130e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
34230e7468fSPeter Dunlap 	}
34330e7468fSPeter Dunlap 
34430e7468fSPeter Dunlap 	/* get icmdp so we can cleanup on error */
34530e7468fSPeter Dunlap 	if ((irhp->opcode == ISCSI_OP_SCSI_DATA_RSP) ||
34630e7468fSPeter Dunlap 	    (irhp->opcode == ISCSI_OP_SCSI_RSP)) {
34730e7468fSPeter Dunlap 		rval = iscsi_rx_process_scsi_itt_to_icmdp(isp, icp->conn_ic,
34830e7468fSPeter Dunlap 		    irhp, icmdp);
34930e7468fSPeter Dunlap 	} else {
35030e7468fSPeter Dunlap 		rval = iscsi_rx_process_itt_to_icmdp(isp,
35130e7468fSPeter Dunlap 		    (iscsi_hdr_t *)irhp, icmdp);
35230e7468fSPeter Dunlap 	}
35330e7468fSPeter Dunlap 
35430e7468fSPeter Dunlap 	if (!ISCSI_SUCCESS(rval)) {
35530e7468fSPeter Dunlap 		mutex_exit(&isp->sess_cmdsn_mutex);
35630e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
357fcf3ce44SJohn Forte 	}
358fcf3ce44SJohn Forte 
359fcf3ce44SJohn Forte 	/* update expcmdsn and maxcmdsn */
36030e7468fSPeter Dunlap 	iscsi_update_flow_control(isp, ntohl(irhp->maxcmdsn),
36130e7468fSPeter Dunlap 	    ntohl(irhp->expcmdsn));
362fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
36330e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
36430e7468fSPeter Dunlap }
365fcf3ce44SJohn Forte 
36630e7468fSPeter Dunlap static void
36730e7468fSPeter Dunlap iscsi_cmd_rsp_chk(iscsi_cmd_t *icmdp, iscsi_scsi_rsp_hdr_t *issrhp)
36830e7468fSPeter Dunlap {
36930e7468fSPeter Dunlap 	struct scsi_pkt *pkt;
37030e7468fSPeter Dunlap 	size_t data_transferred;
371fcf3ce44SJohn Forte 
37230e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
37330e7468fSPeter Dunlap 	pkt->pkt_resid = 0;
37430e7468fSPeter Dunlap 	data_transferred = icmdp->cmd_un.scsi.data_transferred;
37530e7468fSPeter Dunlap 	/* Check the residual count */
37630e7468fSPeter Dunlap 	if ((icmdp->cmd_un.scsi.bp) &&
37730e7468fSPeter Dunlap 	    (data_transferred != icmdp->cmd_un.scsi.bp->b_bcount)) {
378fcf3ce44SJohn Forte 		/*
37930e7468fSPeter Dunlap 		 * We didn't xfer the expected amount of data -
38030e7468fSPeter Dunlap 		 * the residual_count in the header is only
38130e7468fSPeter Dunlap 		 * valid if the underflow flag is set.
382fcf3ce44SJohn Forte 		 */
38330e7468fSPeter Dunlap 		if (issrhp->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
38430e7468fSPeter Dunlap 			pkt->pkt_resid = ntohl(issrhp->residual_count);
38530e7468fSPeter Dunlap 		} else {
38630e7468fSPeter Dunlap 			if (icmdp->cmd_un.scsi.bp->b_bcount >
38730e7468fSPeter Dunlap 			    data_transferred) {
38830e7468fSPeter Dunlap 				/*
38930e7468fSPeter Dunlap 				 * Some data fell on the floor
39030e7468fSPeter Dunlap 				 * somehow - probably a CRC error
39130e7468fSPeter Dunlap 				 */
39230e7468fSPeter Dunlap 				pkt->pkt_resid =
39330e7468fSPeter Dunlap 				    icmdp->cmd_un.scsi.bp->b_bcount -
39430e7468fSPeter Dunlap 				    data_transferred;
39530e7468fSPeter Dunlap 			}
396fcf3ce44SJohn Forte 		}
39730e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE,
39830e7468fSPeter Dunlap 		    "DEBUG: iscsi_rx_cmd_rsp_chk: itt: %u"
39930e7468fSPeter Dunlap 		    "data_trans != b_count data_transferred: %lu "
40030e7468fSPeter Dunlap 		    "b_count: %lu cmd_status: %d flags: %d resid: %lu",
40130e7468fSPeter Dunlap 		    issrhp->itt, data_transferred,
40230e7468fSPeter Dunlap 		    icmdp->cmd_un.scsi.bp->b_bcount,
40330e7468fSPeter Dunlap 		    issrhp->cmd_status & STATUS_MASK,
40430e7468fSPeter Dunlap 		    issrhp->flags, pkt->pkt_resid);
40530e7468fSPeter Dunlap 	}
40630e7468fSPeter Dunlap 	/* set flags that tell SCSA that the command is complete */
40730e7468fSPeter Dunlap 	if (icmdp->cmd_crc_error_seen == B_FALSE) {
40830e7468fSPeter Dunlap 		/* Set successful completion */
40930e7468fSPeter Dunlap 		pkt->pkt_reason = CMD_CMPLT;
41030e7468fSPeter Dunlap 		if (icmdp->cmd_un.scsi.bp) {
41130e7468fSPeter Dunlap 			pkt->pkt_state |= (STATE_XFERRED_DATA |
41230e7468fSPeter Dunlap 			    STATE_GOT_STATUS);
41330e7468fSPeter Dunlap 		} else {
41430e7468fSPeter Dunlap 			pkt->pkt_state |= STATE_GOT_STATUS;
41530e7468fSPeter Dunlap 		}
41630e7468fSPeter Dunlap 	} else {
417fcf3ce44SJohn Forte 		/*
41830e7468fSPeter Dunlap 		 * Some of the data was found to have an incorrect
41930e7468fSPeter Dunlap 		 * error at the protocol error.
420fcf3ce44SJohn Forte 		 */
42130e7468fSPeter Dunlap 		pkt->pkt_reason = CMD_PER_FAIL;
42230e7468fSPeter Dunlap 		pkt->pkt_statistics |= STAT_PERR;
42330e7468fSPeter Dunlap 		if (icmdp->cmd_un.scsi.bp) {
42430e7468fSPeter Dunlap 			pkt->pkt_resid =
42530e7468fSPeter Dunlap 			    icmdp->cmd_un.scsi.bp->b_bcount;
426fcf3ce44SJohn Forte 		} else {
42730e7468fSPeter Dunlap 			pkt->pkt_resid = 0;
428fcf3ce44SJohn Forte 		}
429fcf3ce44SJohn Forte 	}
43030e7468fSPeter Dunlap }
43130e7468fSPeter Dunlap 
4322b79d384Sbing zhao - Sun Microsystems - Beijing China static boolean_t
43330e7468fSPeter Dunlap iscsi_cmd_rsp_cmd_status(iscsi_cmd_t *icmdp, iscsi_scsi_rsp_hdr_t *issrhp,
43430e7468fSPeter Dunlap     uint8_t *data)
43530e7468fSPeter Dunlap {
4365279807dSJack Meng 	int32_t			dlength		= 0;
43730e7468fSPeter Dunlap 	struct scsi_arq_status	*arqstat	= NULL;
43830e7468fSPeter Dunlap 	size_t			senselen	= 0;
4395279807dSJack Meng 	int32_t			statuslen	= 0;
4405279807dSJack Meng 	int32_t			senselen_to	= 0;
44130e7468fSPeter Dunlap 	struct scsi_pkt		*pkt;
4422b79d384Sbing zhao - Sun Microsystems - Beijing China 	boolean_t		affect		= B_FALSE;
44330e7468fSPeter Dunlap 
44430e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
44530e7468fSPeter Dunlap 	dlength = n2h24(issrhp->dlength);
446fcf3ce44SJohn Forte 
447fcf3ce44SJohn Forte 	/*
44830e7468fSPeter Dunlap 	 * Process iSCSI Cmd Response Status
44930e7468fSPeter Dunlap 	 * RFC 3720 Sectionn 10.4.2.
450fcf3ce44SJohn Forte 	 */
45130e7468fSPeter Dunlap 	switch (issrhp->cmd_status & STATUS_MASK) {
45230e7468fSPeter Dunlap 	case STATUS_GOOD:
45330e7468fSPeter Dunlap 		/* pass SCSI status up stack */
45430e7468fSPeter Dunlap 		if (pkt->pkt_scbp) {
45530e7468fSPeter Dunlap 			pkt->pkt_scbp[0] = issrhp->cmd_status;
45630e7468fSPeter Dunlap 		}
45730e7468fSPeter Dunlap 		break;
45830e7468fSPeter Dunlap 	case STATUS_CHECK:
45930e7468fSPeter Dunlap 		/*
46030e7468fSPeter Dunlap 		 * Verify we received a sense buffer and
46130e7468fSPeter Dunlap 		 * that there is the correct amount of
46230e7468fSPeter Dunlap 		 * request sense space to copy it to.
46330e7468fSPeter Dunlap 		 */
46430e7468fSPeter Dunlap 		if ((dlength > 1) &&
46530e7468fSPeter Dunlap 		    (pkt->pkt_scbp != NULL) &&
46630e7468fSPeter Dunlap 		    (icmdp->cmd_un.scsi.statuslen >=
46730e7468fSPeter Dunlap 		    sizeof (struct scsi_arq_status))) {
468fcf3ce44SJohn Forte 			/*
46930e7468fSPeter Dunlap 			 * If a bad command status is received we
47030e7468fSPeter Dunlap 			 * need to reset the pkt_resid to zero.
47130e7468fSPeter Dunlap 			 * The target driver compares its value
47230e7468fSPeter Dunlap 			 * before checking other error flags.
47330e7468fSPeter Dunlap 			 * (ex. check conditions)
474fcf3ce44SJohn Forte 			 */
47530e7468fSPeter Dunlap 			pkt->pkt_resid = 0;
47630e7468fSPeter Dunlap 
47730e7468fSPeter Dunlap 			/* get sense length from first 2 bytes */
47830e7468fSPeter Dunlap 			senselen = ((data[0] << 8) | data[1]) &
47930e7468fSPeter Dunlap 			    (size_t)0xFFFF;
48030e7468fSPeter Dunlap 			ISCSI_IO_LOG(CE_NOTE,
48130e7468fSPeter Dunlap 			    "DEBUG: iscsi_rx_cmd_rsp_cmd_status status_check: "
48230e7468fSPeter Dunlap 			    "dlen: %d scbp: %p statuslen: %d arq: %d senselen:"
48330e7468fSPeter Dunlap 			    " %lu", dlength, (void *)pkt->pkt_scbp,
48430e7468fSPeter Dunlap 			    icmdp->cmd_un.scsi.statuslen,
48530e7468fSPeter Dunlap 			    (int)sizeof (struct scsi_arq_status),
48630e7468fSPeter Dunlap 			    senselen);
48730e7468fSPeter Dunlap 
48830e7468fSPeter Dunlap 			/* Sanity-check on the sense length */
48930e7468fSPeter Dunlap 			if ((senselen + 2) > dlength) {
49030e7468fSPeter Dunlap 				senselen = dlength - 2;
491fcf3ce44SJohn Forte 			}
492fcf3ce44SJohn Forte 
49330e7468fSPeter Dunlap 			/*
49430e7468fSPeter Dunlap 			 * If there was a Data Digest error then
49530e7468fSPeter Dunlap 			 * the sense data cannot be trusted.
49630e7468fSPeter Dunlap 			 */
49730e7468fSPeter Dunlap 			if (icmdp->cmd_crc_error_seen) {
49830e7468fSPeter Dunlap 				senselen = 0;
49930e7468fSPeter Dunlap 			}
500fcf3ce44SJohn Forte 
50130e7468fSPeter Dunlap 			/* automatic request sense */
50230e7468fSPeter Dunlap 			arqstat =
50330e7468fSPeter Dunlap 			    (struct scsi_arq_status *)pkt->pkt_scbp;
50430e7468fSPeter Dunlap 
50530e7468fSPeter Dunlap 			/* pass SCSI status up stack */
50630e7468fSPeter Dunlap 			*((uchar_t *)&arqstat->sts_status) =
50730e7468fSPeter Dunlap 			    issrhp->cmd_status;
508fcf3ce44SJohn Forte 
509fcf3ce44SJohn Forte 			/*
51030e7468fSPeter Dunlap 			 * Set the status for the automatic
51130e7468fSPeter Dunlap 			 * request sense command
512fcf3ce44SJohn Forte 			 */
51330e7468fSPeter Dunlap 			arqstat->sts_rqpkt_state = (STATE_GOT_BUS |
51430e7468fSPeter Dunlap 			    STATE_GOT_TARGET | STATE_SENT_CMD |
51530e7468fSPeter Dunlap 			    STATE_XFERRED_DATA | STATE_GOT_STATUS |
51630e7468fSPeter Dunlap 			    STATE_ARQ_DONE);
517fcf3ce44SJohn Forte 
51830e7468fSPeter Dunlap 			*((uchar_t *)&arqstat->sts_rqpkt_status) =
51930e7468fSPeter Dunlap 			    STATUS_GOOD;
520fcf3ce44SJohn Forte 
52130e7468fSPeter Dunlap 			arqstat->sts_rqpkt_reason = CMD_CMPLT;
52230e7468fSPeter Dunlap 			statuslen = icmdp->cmd_un.scsi.statuslen;
523fcf3ce44SJohn Forte 
52430e7468fSPeter Dunlap 			if (senselen == 0) {
52530e7468fSPeter Dunlap 				/* auto request sense failed */
52630e7468fSPeter Dunlap 				arqstat->sts_rqpkt_status.sts_chk = 1;
52730e7468fSPeter Dunlap 				arqstat->sts_rqpkt_resid = statuslen;
52830e7468fSPeter Dunlap 			} else if (senselen < statuslen) {
52930e7468fSPeter Dunlap 				/* auto request sense short */
53030e7468fSPeter Dunlap 				arqstat->sts_rqpkt_resid = statuslen - senselen;
53130e7468fSPeter Dunlap 			} else {
53230e7468fSPeter Dunlap 				/* auto request sense complete */
53330e7468fSPeter Dunlap 				arqstat->sts_rqpkt_resid = 0;
53430e7468fSPeter Dunlap 			}
53530e7468fSPeter Dunlap 			arqstat->sts_rqpkt_statistics = 0;
53630e7468fSPeter Dunlap 			pkt->pkt_state |= STATE_ARQ_DONE;
537fcf3ce44SJohn Forte 
53830e7468fSPeter Dunlap 			if (icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_XARQ) {
53930e7468fSPeter Dunlap 				pkt->pkt_state |= STATE_XARQ_DONE;
54030e7468fSPeter Dunlap 			}
54130e7468fSPeter Dunlap 
5425279807dSJack Meng 			senselen_to =  pkt->pkt_scblen -
5435279807dSJack Meng 			    sizeof (struct scsi_arq_status) +
5445279807dSJack Meng 			    sizeof (struct scsi_extended_sense);
5455279807dSJack Meng 
54630e7468fSPeter Dunlap 			/* copy auto request sense */
5475279807dSJack Meng 			dlength = min(senselen, senselen_to);
5485279807dSJack Meng 			if (dlength > 0) {
54930e7468fSPeter Dunlap 				bcopy(&data[2], (uchar_t *)&arqstat->
55030e7468fSPeter Dunlap 				    sts_sensedata, dlength);
5512b79d384Sbing zhao - Sun Microsystems - Beijing China 
5522b79d384Sbing zhao - Sun Microsystems - Beijing China 				affect = iscsi_decode_sense(
5532b79d384Sbing zhao - Sun Microsystems - Beijing China 				    (uint8_t *)&arqstat->sts_sensedata);
55430e7468fSPeter Dunlap 			}
55530e7468fSPeter Dunlap 			break;
556fcf3ce44SJohn Forte 		}
55730e7468fSPeter Dunlap 		/* FALLTHRU */
55830e7468fSPeter Dunlap 	case STATUS_BUSY:
55930e7468fSPeter Dunlap 	case STATUS_RESERVATION_CONFLICT:
56030e7468fSPeter Dunlap 	case STATUS_QFULL:
56130e7468fSPeter Dunlap 	case STATUS_ACA_ACTIVE:
56230e7468fSPeter Dunlap 	default:
56330e7468fSPeter Dunlap 		/*
56430e7468fSPeter Dunlap 		 * If a bad command status is received we need to
56530e7468fSPeter Dunlap 		 * reset the pkt_resid to zero.  The target driver
56630e7468fSPeter Dunlap 		 * compares its value before checking other error
56730e7468fSPeter Dunlap 		 * flags. (ex. check conditions)
56830e7468fSPeter Dunlap 		 */
56930e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE,
57030e7468fSPeter Dunlap 		    "DEBUG: iscsi_rx_cmd_rsp_cmd_status: status: "
57130e7468fSPeter Dunlap 		    "%d cmd_status: %d dlen: %u scbp: %p statuslen: %d "
57230e7468fSPeter Dunlap 		    "arg_len: %d", issrhp->cmd_status & STATUS_MASK,
57330e7468fSPeter Dunlap 		    issrhp->cmd_status, dlength, (void *)pkt->pkt_scbp,
57430e7468fSPeter Dunlap 		    icmdp->cmd_un.scsi.statuslen,
57530e7468fSPeter Dunlap 		    (int)sizeof (struct scsi_arq_status));
57630e7468fSPeter Dunlap 		pkt->pkt_resid = 0;
57730e7468fSPeter Dunlap 		/* pass SCSI status up stack */
57830e7468fSPeter Dunlap 		if (pkt->pkt_scbp) {
57930e7468fSPeter Dunlap 			pkt->pkt_scbp[0] = issrhp->cmd_status;
58030e7468fSPeter Dunlap 		}
58130e7468fSPeter Dunlap 	}
5822b79d384Sbing zhao - Sun Microsystems - Beijing China 
5832b79d384Sbing zhao - Sun Microsystems - Beijing China 	return (affect);
58430e7468fSPeter Dunlap }
585fcf3ce44SJohn Forte 
58630e7468fSPeter Dunlap /*
58730e7468fSPeter Dunlap  * iscsi_rx_process_login_pdup - Process login response PDU.  This function
58830e7468fSPeter Dunlap  * copies the data into the connection context so that the login code can
58930e7468fSPeter Dunlap  * interpret it.
59030e7468fSPeter Dunlap  */
59130e7468fSPeter Dunlap 
59230e7468fSPeter Dunlap idm_status_t
59330e7468fSPeter Dunlap iscsi_rx_process_login_pdu(idm_conn_t *ic, idm_pdu_t *pdu)
59430e7468fSPeter Dunlap {
59530e7468fSPeter Dunlap 	iscsi_conn_t 		*icp;
59630e7468fSPeter Dunlap 
59730e7468fSPeter Dunlap 	icp = ic->ic_handle;
59830e7468fSPeter Dunlap 
59930e7468fSPeter Dunlap 	/*
60030e7468fSPeter Dunlap 	 * Copy header and data into connection structure so iscsi_login()
60130e7468fSPeter Dunlap 	 * can process it.
60230e7468fSPeter Dunlap 	 */
60330e7468fSPeter Dunlap 	mutex_enter(&icp->conn_login_mutex);
60430e7468fSPeter Dunlap 	/*
60530e7468fSPeter Dunlap 	 * If conn_login_state != LOGIN_TX then we are not ready to handle
60630e7468fSPeter Dunlap 	 * this login response and we should just  drop it.
60730e7468fSPeter Dunlap 	 */
60830e7468fSPeter Dunlap 	if (icp->conn_login_state == LOGIN_TX) {
60930e7468fSPeter Dunlap 		icp->conn_login_datalen = pdu->isp_datalen;
61030e7468fSPeter Dunlap 		bcopy(pdu->isp_hdr, &icp->conn_login_resp_hdr,
61130e7468fSPeter Dunlap 		    sizeof (iscsi_hdr_t));
61230e7468fSPeter Dunlap 		/*
61330e7468fSPeter Dunlap 		 * Login code is sloppy with it's NULL handling so make sure
61430e7468fSPeter Dunlap 		 * we don't leave any stale data in there.
61530e7468fSPeter Dunlap 		 */
61630e7468fSPeter Dunlap 		bzero(icp->conn_login_data, icp->conn_login_max_data_length);
61730e7468fSPeter Dunlap 		bcopy(pdu->isp_data, icp->conn_login_data,
61830e7468fSPeter Dunlap 		    MIN(pdu->isp_datalen, icp->conn_login_max_data_length));
61930e7468fSPeter Dunlap 		iscsi_login_update_state_locked(icp, LOGIN_RX);
620fcf3ce44SJohn Forte 	}
62130e7468fSPeter Dunlap 	mutex_exit(&icp->conn_login_mutex);
622fcf3ce44SJohn Forte 
62330e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
624fcf3ce44SJohn Forte }
625fcf3ce44SJohn Forte 
626fcf3ce44SJohn Forte /*
627fcf3ce44SJohn Forte  * iscsi_rx_process_cmd_rsp - Process received scsi command response.  This
628fcf3ce44SJohn Forte  * will contain sense data if the command was not successful.  This data needs
629fcf3ce44SJohn Forte  * to be copied into the scsi_pkt.  Otherwise we just complete the IO.
630fcf3ce44SJohn Forte  */
63130e7468fSPeter Dunlap static idm_status_t
63230e7468fSPeter Dunlap iscsi_rx_process_cmd_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
633fcf3ce44SJohn Forte {
63430e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
63530e7468fSPeter Dunlap 	iscsi_sess_t		*isp	= icp->conn_sess;
63630e7468fSPeter Dunlap 	iscsi_scsi_rsp_hdr_t	*issrhp	= (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
63730e7468fSPeter Dunlap 	uint8_t			*data	= pdu->isp_data;
63830e7468fSPeter Dunlap 	iscsi_cmd_t		*icmdp	= NULL;
63930e7468fSPeter Dunlap 	struct scsi_pkt		*pkt	= NULL;
64030e7468fSPeter Dunlap 	idm_status_t		rval;
64130e7468fSPeter Dunlap 	struct buf		*bp;
6422b79d384Sbing zhao - Sun Microsystems - Beijing China 	boolean_t		flush	= B_FALSE;
6432b79d384Sbing zhao - Sun Microsystems - Beijing China 	uint32_t		cmd_sn	= 0;
6442b79d384Sbing zhao - Sun Microsystems - Beijing China 	uint16_t		lun_num = 0;
645fcf3ce44SJohn Forte 
646fcf3ce44SJohn Forte 	/* make sure we get status in order */
64730e7468fSPeter Dunlap 	mutex_enter(&icp->conn_queue_active.mutex);
64830e7468fSPeter Dunlap 
64930e7468fSPeter Dunlap 	if ((rval = iscsi_rx_chk(icp, isp, issrhp,
65030e7468fSPeter Dunlap 	    &icmdp)) != IDM_STATUS_SUCCESS) {
65130e7468fSPeter Dunlap 		if (icmdp != NULL) {
65230e7468fSPeter Dunlap 			iscsi_task_cleanup(issrhp->opcode, icmdp);
65330e7468fSPeter Dunlap 		}
65430e7468fSPeter Dunlap 		mutex_exit(&icp->conn_queue_active.mutex);
65530e7468fSPeter Dunlap 		return (rval);
656fcf3ce44SJohn Forte 	}
657fcf3ce44SJohn Forte 
65830e7468fSPeter Dunlap 	/*
65930e7468fSPeter Dunlap 	 * If we are in "idm aborting" state then we shouldn't continue
66030e7468fSPeter Dunlap 	 * to process this command.  By definition this command is no longer
66130e7468fSPeter Dunlap 	 * on the active queue so we shouldn't try to remove it either.
66230e7468fSPeter Dunlap 	 */
66330e7468fSPeter Dunlap 	mutex_enter(&icmdp->cmd_mutex);
66430e7468fSPeter Dunlap 	if (icmdp->cmd_state == ISCSI_CMD_STATE_IDM_ABORTING) {
66530e7468fSPeter Dunlap 		mutex_exit(&icmdp->cmd_mutex);
666fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
66730e7468fSPeter Dunlap 		return (IDM_STATUS_SUCCESS);
668fcf3ce44SJohn Forte 	}
66930e7468fSPeter Dunlap 	mutex_exit(&icmdp->cmd_mutex);
670fcf3ce44SJohn Forte 
67130e7468fSPeter Dunlap 	/* Get the IDM buffer and bytes transferred */
67230e7468fSPeter Dunlap 	bp = icmdp->cmd_un.scsi.bp;
67330e7468fSPeter Dunlap 	if (ic->ic_conn_flags & IDM_CONN_USE_SCOREBOARD) {
67430e7468fSPeter Dunlap 		/* Transport tracks bytes transferred so use those counts */
67530e7468fSPeter Dunlap 		if (bp && (bp->b_flags & B_READ)) {
67630e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.data_transferred +=
67730e7468fSPeter Dunlap 			    icmdp->cmd_itp->idt_rx_bytes;
67830e7468fSPeter Dunlap 		} else {
67930e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.data_transferred +=
68030e7468fSPeter Dunlap 			    icmdp->cmd_itp->idt_tx_bytes;
68130e7468fSPeter Dunlap 		}
68230e7468fSPeter Dunlap 	} else {
68330e7468fSPeter Dunlap 		/*
68430e7468fSPeter Dunlap 		 * Some transports cannot track the bytes transferred on
68530e7468fSPeter Dunlap 		 * the initiator side (like iSER) so we have to use the
68630e7468fSPeter Dunlap 		 * status info.  If the response field indicates that
68730e7468fSPeter Dunlap 		 * the command actually completed then we will assume
68830e7468fSPeter Dunlap 		 * the data_transferred value represents the entire buffer
68930e7468fSPeter Dunlap 		 * unless the resid field says otherwise.  This is a bit
69030e7468fSPeter Dunlap 		 * unintuitive but it's really impossible to know what
69130e7468fSPeter Dunlap 		 * has been transferred without detailed consideration
69230e7468fSPeter Dunlap 		 * of the SCSI status and sense key and that is outside
69330e7468fSPeter Dunlap 		 * the scope of the transport.  Instead the target/class driver
69430e7468fSPeter Dunlap 		 * can consider these values along with the resid and figure
69530e7468fSPeter Dunlap 		 * it out.  The data_transferred concept is just belt and
69630e7468fSPeter Dunlap 		 * suspenders anyway -- RFC 3720 actually explicitly rejects
69730e7468fSPeter Dunlap 		 * scoreboarding ("Initiators SHOULD NOT keep track of the
69830e7468fSPeter Dunlap 		 * data transferred to or from the target (scoreboarding)")
69930e7468fSPeter Dunlap 		 * perhaps for this very reason.
70030e7468fSPeter Dunlap 		 */
70130e7468fSPeter Dunlap 		if (issrhp->response != 0) {
70230e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.data_transferred = 0;
70330e7468fSPeter Dunlap 		} else {
70430e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.data_transferred =
70530e7468fSPeter Dunlap 			    (bp == NULL) ? 0 : bp->b_bcount;
70630e7468fSPeter Dunlap 			if (issrhp->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
70730e7468fSPeter Dunlap 				icmdp->cmd_un.scsi.data_transferred -=
70830e7468fSPeter Dunlap 				    ntohl(issrhp->residual_count);
70930e7468fSPeter Dunlap 			}
71030e7468fSPeter Dunlap 		}
71130e7468fSPeter Dunlap 	}
712fcf3ce44SJohn Forte 
71330e7468fSPeter Dunlap 	ISCSI_CHECK_SCSI_READ(icmdp, issrhp,
71430e7468fSPeter Dunlap 	    icmdp->cmd_un.scsi.data_transferred,
71530e7468fSPeter Dunlap 	    BP_CHECK_THOROUGH);
71630e7468fSPeter Dunlap 
71730e7468fSPeter Dunlap 	ISCSI_IO_LOG(CE_NOTE, "DEBUG: rx_process_cmd_rsp: ic: %p pdu: %p itt:"
71830e7468fSPeter Dunlap 	    " %x expcmdsn: %x sess_cmd: %x sess_expcmdsn: %x data_transfered:"
71930e7468fSPeter Dunlap 	    " %lu ibp: %p obp: %p", (void *)ic, (void *)pdu, issrhp->itt,
72030e7468fSPeter Dunlap 	    issrhp->expcmdsn, isp->sess_cmdsn, isp->sess_expcmdsn,
72130e7468fSPeter Dunlap 	    icmdp->cmd_un.scsi.data_transferred,
72230e7468fSPeter Dunlap 	    (void *)icmdp->cmd_un.scsi.ibp_ibuf,
72330e7468fSPeter Dunlap 	    (void *)icmdp->cmd_un.scsi.ibp_obuf);
72430e7468fSPeter Dunlap 
72530e7468fSPeter Dunlap 	iscsi_task_cleanup(issrhp->opcode, icmdp);
726fcf3ce44SJohn Forte 
727fcf3ce44SJohn Forte 	if (issrhp->response) {
728fcf3ce44SJohn Forte 		/* The target failed the command. */
72930e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE, "DEBUG: rx_process_cmd_rsp: ic: %p pdu:"
73030e7468fSPeter Dunlap 		    " %p response: %d bcount: %lu", (void *)ic, (void *)pdu,
73130e7468fSPeter Dunlap 		    issrhp->response, icmdp->cmd_un.scsi.bp->b_bcount);
73230e7468fSPeter Dunlap 		pkt = icmdp->cmd_un.scsi.pkt;
733fcf3ce44SJohn Forte 		pkt->pkt_reason = CMD_TRAN_ERR;
734fcf3ce44SJohn Forte 		if (icmdp->cmd_un.scsi.bp) {
735fcf3ce44SJohn Forte 			pkt->pkt_resid = icmdp->cmd_un.scsi.bp->b_bcount;
736fcf3ce44SJohn Forte 		} else {
737fcf3ce44SJohn Forte 			pkt->pkt_resid = 0;
738fcf3ce44SJohn Forte 		}
739fcf3ce44SJohn Forte 	} else {
740fcf3ce44SJohn Forte 		/* success */
74130e7468fSPeter Dunlap 		iscsi_cmd_rsp_chk(icmdp, issrhp);
7422b79d384Sbing zhao - Sun Microsystems - Beijing China 		flush = iscsi_cmd_rsp_cmd_status(icmdp, issrhp, data);
7432b79d384Sbing zhao - Sun Microsystems - Beijing China 		if (flush == B_TRUE) {
7442b79d384Sbing zhao - Sun Microsystems - Beijing China 			cmd_sn = icmdp->cmd_sn;
7452b79d384Sbing zhao - Sun Microsystems - Beijing China 			ASSERT(icmdp->cmd_lun != NULL);
7462b79d384Sbing zhao - Sun Microsystems - Beijing China 			lun_num = icmdp->cmd_lun->lun_num;
7472b79d384Sbing zhao - Sun Microsystems - Beijing China 		}
74830e7468fSPeter Dunlap 	}
749fcf3ce44SJohn Forte 
75030e7468fSPeter Dunlap 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp);
7512b79d384Sbing zhao - Sun Microsystems - Beijing China 	if (flush == B_TRUE) {
7522b79d384Sbing zhao - Sun Microsystems - Beijing China 		iscsi_flush_cmd_after_reset(cmd_sn, lun_num, icp);
7532b79d384Sbing zhao - Sun Microsystems - Beijing China 	}
75430e7468fSPeter Dunlap 	mutex_exit(&icp->conn_queue_active.mutex);
75530e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
75630e7468fSPeter Dunlap }
757fcf3ce44SJohn Forte 
75830e7468fSPeter Dunlap static void
75930e7468fSPeter Dunlap iscsi_data_rsp_pkt(iscsi_cmd_t *icmdp, iscsi_data_rsp_hdr_t *idrhp)
76030e7468fSPeter Dunlap {
76130e7468fSPeter Dunlap 	struct buf		*bp	= NULL;
76230e7468fSPeter Dunlap 	size_t			data_transferred;
76330e7468fSPeter Dunlap 	struct scsi_pkt		*pkt;
764fcf3ce44SJohn Forte 
76530e7468fSPeter Dunlap 	bp = icmdp->cmd_un.scsi.bp;
76630e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
76730e7468fSPeter Dunlap 	data_transferred = icmdp->cmd_un.scsi.data_transferred;
76830e7468fSPeter Dunlap 	/*
76930e7468fSPeter Dunlap 	 * The command* must be completed now, since we won't get a command
77030e7468fSPeter Dunlap 	 * response PDU. The cmd_status and residual_count are
77130e7468fSPeter Dunlap 	 * not meaningful unless status_present is set.
77230e7468fSPeter Dunlap 	 */
77330e7468fSPeter Dunlap 	pkt->pkt_resid = 0;
77430e7468fSPeter Dunlap 	/* Check the residual count */
77530e7468fSPeter Dunlap 	if (bp && (data_transferred != bp->b_bcount)) {
776fcf3ce44SJohn Forte 		/*
77730e7468fSPeter Dunlap 		 * We didn't xfer the expected amount of data -
77830e7468fSPeter Dunlap 		 * the residual_count in the header is only valid
77930e7468fSPeter Dunlap 		 * if the underflow flag is set.
780fcf3ce44SJohn Forte 		 */
78130e7468fSPeter Dunlap 		if (idrhp->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
78230e7468fSPeter Dunlap 			pkt->pkt_resid = ntohl(idrhp->residual_count);
78330e7468fSPeter Dunlap 			ISCSI_IO_LOG(CE_NOTE, "DEBUG: iscsi_data_rsp_pkt: "
78430e7468fSPeter Dunlap 			    "underflow: itt: %d "
78530e7468fSPeter Dunlap 			    "transferred: %lu count: %lu", idrhp->itt,
78630e7468fSPeter Dunlap 			    data_transferred, bp->b_bcount);
78730e7468fSPeter Dunlap 		} else {
78830e7468fSPeter Dunlap 			if (bp->b_bcount > data_transferred) {
78930e7468fSPeter Dunlap 				/* Some data fell on the floor somehw */
79030e7468fSPeter Dunlap 				ISCSI_IO_LOG(CE_NOTE, "DEBUG: "
79130e7468fSPeter Dunlap 				    "iscsi_data_rsp_pkt: data fell: itt: %d "
79230e7468fSPeter Dunlap 				    "transferred: %lu count: %lu", idrhp->itt,
79330e7468fSPeter Dunlap 				    data_transferred, bp->b_bcount);
79430e7468fSPeter Dunlap 				pkt->pkt_resid =
79530e7468fSPeter Dunlap 				    bp->b_bcount - data_transferred;
796fcf3ce44SJohn Forte 			}
79730e7468fSPeter Dunlap 		}
79830e7468fSPeter Dunlap 	}
799fcf3ce44SJohn Forte 
80030e7468fSPeter Dunlap 	pkt->pkt_reason = CMD_CMPLT;
80130e7468fSPeter Dunlap 	pkt->pkt_state |= (STATE_XFERRED_DATA | STATE_GOT_STATUS);
802fcf3ce44SJohn Forte 
80330e7468fSPeter Dunlap 	if (((idrhp->cmd_status & STATUS_MASK) != STATUS_GOOD) &&
80430e7468fSPeter Dunlap 	    (icmdp->cmd_un.scsi.statuslen >=
80530e7468fSPeter Dunlap 	    sizeof (struct scsi_arq_status)) && pkt->pkt_scbp) {
806fcf3ce44SJohn Forte 
80730e7468fSPeter Dunlap 		/*
80830e7468fSPeter Dunlap 		 * Not supposed to get exception status here!
80930e7468fSPeter Dunlap 		 * We have no request sense data so just do the
81030e7468fSPeter Dunlap 		 * best we can
81130e7468fSPeter Dunlap 		 */
81230e7468fSPeter Dunlap 		struct scsi_arq_status *arqstat =
81330e7468fSPeter Dunlap 		    (struct scsi_arq_status *)pkt->pkt_scbp;
814fcf3ce44SJohn Forte 
815fcf3ce44SJohn Forte 
81630e7468fSPeter Dunlap 		bzero(arqstat, sizeof (struct scsi_arq_status));
817fcf3ce44SJohn Forte 
81830e7468fSPeter Dunlap 		*((uchar_t *)&arqstat->sts_status) =
81930e7468fSPeter Dunlap 		    idrhp->cmd_status;
820fcf3ce44SJohn Forte 
82130e7468fSPeter Dunlap 		arqstat->sts_rqpkt_resid =
82230e7468fSPeter Dunlap 		    sizeof (struct scsi_extended_sense);
82330e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE, "DEBUG: iscsi_data_rsp_pkt: "
82430e7468fSPeter Dunlap 		    "exception status: itt: %d resid: %d",
82530e7468fSPeter Dunlap 		    idrhp->itt, arqstat->sts_rqpkt_resid);
826b4243054SSheng-Liang Eric Zhang 
82730e7468fSPeter Dunlap 	} else if (pkt->pkt_scbp) {
82830e7468fSPeter Dunlap 		/* just pass along the status we got */
82930e7468fSPeter Dunlap 		pkt->pkt_scbp[0] = idrhp->cmd_status;
830fcf3ce44SJohn Forte 	}
831fcf3ce44SJohn Forte }
832fcf3ce44SJohn Forte 
833fcf3ce44SJohn Forte /*
83430e7468fSPeter Dunlap  * iscsi_rx_process_data_rsp -
83530e7468fSPeter Dunlap  * This currently processes the final data sequence denoted by the data response
83630e7468fSPeter Dunlap  * PDU Status bit being set.  We will not receive the SCSI response.
83730e7468fSPeter Dunlap  * This bit denotes that the PDU is the successful completion of the
83830e7468fSPeter Dunlap  * command.
839fcf3ce44SJohn Forte  */
84030e7468fSPeter Dunlap static idm_status_t
84130e7468fSPeter Dunlap iscsi_rx_process_data_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
842fcf3ce44SJohn Forte {
84330e7468fSPeter Dunlap 	iscsi_sess_t		*isp	= NULL;
84430e7468fSPeter Dunlap 	iscsi_data_rsp_hdr_t	*idrhp	= (iscsi_data_rsp_hdr_t *)pdu->isp_hdr;
84530e7468fSPeter Dunlap 	iscsi_cmd_t		*icmdp	= NULL;
84630e7468fSPeter Dunlap 	struct buf		*bp	= NULL;
84730e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
84830e7468fSPeter Dunlap 	idm_buf_t		*ibp;
84930e7468fSPeter Dunlap 	idm_status_t		rval;
850fcf3ce44SJohn Forte 
851fcf3ce44SJohn Forte 
85230e7468fSPeter Dunlap 	/* should only call this when the data rsp contains final rsp */
85330e7468fSPeter Dunlap 	ASSERT(idrhp->flags & ISCSI_FLAG_DATA_STATUS);
85430e7468fSPeter Dunlap 	isp = icp->conn_sess;
85530e7468fSPeter Dunlap 
856fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
85730e7468fSPeter Dunlap 	if ((rval = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)idrhp,
85830e7468fSPeter Dunlap 	    &icmdp)) != IDM_STATUS_SUCCESS) {
85930e7468fSPeter Dunlap 		if (icmdp != NULL) {
86030e7468fSPeter Dunlap 			iscsi_task_cleanup(idrhp->opcode, icmdp);
86130e7468fSPeter Dunlap 		}
862fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
86330e7468fSPeter Dunlap 		return (rval);
864fcf3ce44SJohn Forte 	}
865fcf3ce44SJohn Forte 
86630e7468fSPeter Dunlap 	/*
86730e7468fSPeter Dunlap 	 * If we are in "idm aborting" state then we shouldn't continue
86830e7468fSPeter Dunlap 	 * to process this command.  By definition this command is no longer
86930e7468fSPeter Dunlap 	 * on the active queue so we shouldn't try to remove it either.
87030e7468fSPeter Dunlap 	 */
871fcf3ce44SJohn Forte 	mutex_enter(&icmdp->cmd_mutex);
87230e7468fSPeter Dunlap 	if (icmdp->cmd_state == ISCSI_CMD_STATE_IDM_ABORTING) {
87330e7468fSPeter Dunlap 		mutex_exit(&icmdp->cmd_mutex);
87430e7468fSPeter Dunlap 		mutex_exit(&icp->conn_queue_active.mutex);
87530e7468fSPeter Dunlap 		return (IDM_STATUS_SUCCESS);
87630e7468fSPeter Dunlap 	}
87730e7468fSPeter Dunlap 	mutex_exit(&icmdp->cmd_mutex);
87830e7468fSPeter Dunlap 
87930e7468fSPeter Dunlap 	/*
88030e7468fSPeter Dunlap 	 * Holding the pending/active queue locks across the
88130e7468fSPeter Dunlap 	 * iscsi_rx_data call later in this function may cause
88230e7468fSPeter Dunlap 	 * deadlock during fault injections.  Instead remove
88330e7468fSPeter Dunlap 	 * the cmd from the active queue and release the locks.
88430e7468fSPeter Dunlap 	 * Then before returning or completing the command
88530e7468fSPeter Dunlap 	 * return the cmd to the active queue and reacquire
88630e7468fSPeter Dunlap 	 * the locks.
88730e7468fSPeter Dunlap 	 */
88830e7468fSPeter Dunlap 	iscsi_dequeue_active_cmd(icp, icmdp);
88930e7468fSPeter Dunlap 
89030e7468fSPeter Dunlap 	mutex_exit(&icp->conn_queue_active.mutex);
891fcf3ce44SJohn Forte 
89230e7468fSPeter Dunlap 	/* shorthand some values */
893fcf3ce44SJohn Forte 	bp = icmdp->cmd_un.scsi.bp;
894fcf3ce44SJohn Forte 
89530e7468fSPeter Dunlap 	/*
89630e7468fSPeter Dunlap 	 * some poorly behaved targets have been observed
89730e7468fSPeter Dunlap 	 * sending data-in pdu's during a write operation
89830e7468fSPeter Dunlap 	 */
89930e7468fSPeter Dunlap 	if (bp != NULL) {
90030e7468fSPeter Dunlap 		if (!(bp->b_flags & B_READ)) {
90130e7468fSPeter Dunlap 			cmn_err(CE_WARN,
90230e7468fSPeter Dunlap 			    "iscsi connection(%u) protocol error - "
90330e7468fSPeter Dunlap 			    "received data response during write operation "
90430e7468fSPeter Dunlap 			    "itt:0x%x",
90530e7468fSPeter Dunlap 			    icp->conn_oid, idrhp->itt);
90630e7468fSPeter Dunlap 			mutex_enter(&icp->conn_queue_active.mutex);
90730e7468fSPeter Dunlap 			iscsi_enqueue_active_cmd(icp, icmdp);
90830e7468fSPeter Dunlap 			mutex_exit(&icp->conn_queue_active.mutex);
90930e7468fSPeter Dunlap 			return (IDM_STATUS_PROTOCOL_ERROR);
91030e7468fSPeter Dunlap 		}
91130e7468fSPeter Dunlap 	}
91230e7468fSPeter Dunlap 
91330e7468fSPeter Dunlap 	ibp = icmdp->cmd_un.scsi.ibp_ibuf;
91430e7468fSPeter Dunlap 	if (ibp == NULL) {
91530e7468fSPeter Dunlap 		/*
91630e7468fSPeter Dunlap 		 * After the check of bp above we *should* have a corresponding
91730e7468fSPeter Dunlap 		 * idm_buf_t (ibp).  It's possible that the original call
91830e7468fSPeter Dunlap 		 * to idm_buf_alloc failed due to a pending connection state
91930e7468fSPeter Dunlap 		 * transition in which case this value can be NULL.  It's
92030e7468fSPeter Dunlap 		 * highly unlikely that the connection would be shutting down
92130e7468fSPeter Dunlap 		 * *and* we manage to process a data response and get to this
92230e7468fSPeter Dunlap 		 * point in the code but just in case we should check for it.
92330e7468fSPeter Dunlap 		 * This isn't really a protocol error -- we are almost certainly
92430e7468fSPeter Dunlap 		 * closing the connection anyway so just return a generic error.
92530e7468fSPeter Dunlap 		 */
92630e7468fSPeter Dunlap 		mutex_enter(&icp->conn_queue_active.mutex);
92730e7468fSPeter Dunlap 		iscsi_enqueue_active_cmd(icp, icmdp);
92830e7468fSPeter Dunlap 		mutex_exit(&icp->conn_queue_active.mutex);
92930e7468fSPeter Dunlap 		return (IDM_STATUS_FAIL);
93030e7468fSPeter Dunlap 	}
93130e7468fSPeter Dunlap 
93230e7468fSPeter Dunlap 	if (ic->ic_conn_flags & IDM_CONN_USE_SCOREBOARD) {
93330e7468fSPeter Dunlap 		icmdp->cmd_un.scsi.data_transferred =
93430e7468fSPeter Dunlap 		    icmdp->cmd_itp->idt_rx_bytes;
935fcf3ce44SJohn Forte 	} else {
93630e7468fSPeter Dunlap 		icmdp->cmd_un.scsi.data_transferred = bp->b_bcount;
93730e7468fSPeter Dunlap 		if (idrhp->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
93830e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.data_transferred -=
93930e7468fSPeter Dunlap 			    ntohl(idrhp->residual_count);
94030e7468fSPeter Dunlap 		}
941fcf3ce44SJohn Forte 	}
942fcf3ce44SJohn Forte 
94330e7468fSPeter Dunlap 	ISCSI_IO_LOG(CE_NOTE, "DEBUG: rx_process_data_rsp: icp: %p pdu: %p "
94430e7468fSPeter Dunlap 	    "itt: %d ibp: %p icmdp: %p xfer_len: %lu transferred: %lu dlen: %u",
94530e7468fSPeter Dunlap 	    (void *)icp, (void *)pdu, idrhp->itt, (void *)bp, (void *)icmdp,
94630e7468fSPeter Dunlap 	    (ibp == NULL) ? 0 : ibp->idb_xfer_len,
94730e7468fSPeter Dunlap 	    icmdp->cmd_un.scsi.data_transferred,
94830e7468fSPeter Dunlap 	    n2h24(idrhp->dlength));
94930e7468fSPeter Dunlap 
95030e7468fSPeter Dunlap 	iscsi_task_cleanup(idrhp->opcode, icmdp);
95130e7468fSPeter Dunlap 
95230e7468fSPeter Dunlap 	iscsi_data_rsp_pkt(icmdp, idrhp);
95330e7468fSPeter Dunlap 
95430e7468fSPeter Dunlap 	mutex_enter(&icp->conn_queue_active.mutex);
95530e7468fSPeter Dunlap 	iscsi_enqueue_active_cmd(icp, icmdp);
95630e7468fSPeter Dunlap 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp);
957fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_queue_active.mutex);
958fcf3ce44SJohn Forte 
95930e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
960fcf3ce44SJohn Forte }
961fcf3ce44SJohn Forte 
962fcf3ce44SJohn Forte /*
963fcf3ce44SJohn Forte  * iscsi_rx_process_nop - Process a received nop.  If nop is in response
964fcf3ce44SJohn Forte  * to a ping we sent update stats.  If initiated by the target we need
965fcf3ce44SJohn Forte  * to response back to the target with a nop.  Schedule the response.
966fcf3ce44SJohn Forte  */
967fcf3ce44SJohn Forte /* ARGSUSED */
96830e7468fSPeter Dunlap static idm_status_t
96930e7468fSPeter Dunlap iscsi_rx_process_nop(idm_conn_t *ic, idm_pdu_t *pdu)
970fcf3ce44SJohn Forte {
971fcf3ce44SJohn Forte 	iscsi_sess_t		*isp	= NULL;
97230e7468fSPeter Dunlap 	iscsi_nop_in_hdr_t	*inihp	= (iscsi_nop_in_hdr_t *)pdu->isp_hdr;
973fcf3ce44SJohn Forte 	iscsi_cmd_t		*icmdp	= NULL;
97430e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
975fcf3ce44SJohn Forte 
976fcf3ce44SJohn Forte 	if (icp->conn_expstatsn != ntohl(inihp->statsn)) {
97730e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi connection(%u/%x) protocol error - "
978fcf3ce44SJohn Forte 		    "received status out of order itt:0x%x statsn:0x%x "
97930e7468fSPeter Dunlap 		    "expstatsn:0x%x", icp->conn_oid, inihp->opcode, inihp->itt,
980fcf3ce44SJohn Forte 		    ntohl(inihp->statsn), icp->conn_expstatsn);
98130e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
982fcf3ce44SJohn Forte 	}
98330e7468fSPeter Dunlap 	isp = icp->conn_sess;
98430e7468fSPeter Dunlap 	ASSERT(isp != NULL);
985fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
986fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
987fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
988fcf3ce44SJohn Forte 	if (inihp->itt != ISCSI_RSVD_TASK_TAG) {
989fcf3ce44SJohn Forte 		if (!ISCSI_SUCCESS(iscsi_rx_process_itt_to_icmdp(
99030e7468fSPeter Dunlap 		    isp, (iscsi_hdr_t *)inihp, &icmdp))) {
99130e7468fSPeter Dunlap 			cmn_err(CE_WARN, "iscsi connection(%u) protocol error "
99230e7468fSPeter Dunlap 			    "- can not find cmd for itt:0x%x",
99330e7468fSPeter Dunlap 			    icp->conn_oid, inihp->itt);
994fcf3ce44SJohn Forte 			mutex_exit(&isp->sess_cmdsn_mutex);
995fcf3ce44SJohn Forte 			mutex_exit(&icp->conn_queue_active.mutex);
996fcf3ce44SJohn Forte 			mutex_exit(&isp->sess_queue_pending.mutex);
99730e7468fSPeter Dunlap 			return (IDM_STATUS_PROTOCOL_ERROR);
998fcf3ce44SJohn Forte 		}
999fcf3ce44SJohn Forte 	}
1000fcf3ce44SJohn Forte 
1001fcf3ce44SJohn Forte 	/* update expcmdsn and maxcmdsn */
1002fcf3ce44SJohn Forte 	iscsi_update_flow_control(isp, ntohl(inihp->maxcmdsn),
1003fcf3ce44SJohn Forte 	    ntohl(inihp->expcmdsn));
1004fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
1005fcf3ce44SJohn Forte 
1006fcf3ce44SJohn Forte 	if ((inihp->itt != ISCSI_RSVD_TASK_TAG) &&
1007fcf3ce44SJohn Forte 	    (inihp->ttt == ISCSI_RSVD_TASK_TAG)) {
1008fcf3ce44SJohn Forte 		/* This is the only type of nop that incs. the expstatsn */
1009fcf3ce44SJohn Forte 		icp->conn_expstatsn++;
1010fcf3ce44SJohn Forte 
1011fcf3ce44SJohn Forte 		/*
1012fcf3ce44SJohn Forte 		 * This is a targets response to our nop
1013fcf3ce44SJohn Forte 		 */
1014fcf3ce44SJohn Forte 		iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp);
1015fcf3ce44SJohn Forte 	} else if (inihp->ttt != ISCSI_RSVD_TASK_TAG) {
1016fcf3ce44SJohn Forte 		/*
1017fcf3ce44SJohn Forte 		 * Target requested a nop.  Send one.
1018fcf3ce44SJohn Forte 		 */
1019fcf3ce44SJohn Forte 		iscsi_handle_nop(icp, ISCSI_RSVD_TASK_TAG, inihp->ttt);
1020fcf3ce44SJohn Forte 	} else {
1021fcf3ce44SJohn Forte 		/*
1022fcf3ce44SJohn Forte 		 * This is a target-initiated ping that doesn't expect
1023fcf3ce44SJohn Forte 		 * a response; nothing to do except update our flow control
1024fcf3ce44SJohn Forte 		 * (which we do in all cases above).
1025fcf3ce44SJohn Forte 		 */
1026fcf3ce44SJohn Forte 		/* EMPTY */
1027fcf3ce44SJohn Forte 	}
1028fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_queue_active.mutex);
1029fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
1030fcf3ce44SJohn Forte 
103130e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1032fcf3ce44SJohn Forte }
1033fcf3ce44SJohn Forte 
1034fcf3ce44SJohn Forte 
1035fcf3ce44SJohn Forte /*
1036fcf3ce44SJohn Forte  * iscsi_rx_process_reject_rsp - The server rejected a PDU
1037fcf3ce44SJohn Forte  */
103830e7468fSPeter Dunlap static idm_status_t
103930e7468fSPeter Dunlap iscsi_rx_process_reject_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
1040fcf3ce44SJohn Forte {
104130e7468fSPeter Dunlap 	iscsi_reject_rsp_hdr_t	*irrhp = (iscsi_reject_rsp_hdr_t *)pdu->isp_hdr;
104230e7468fSPeter Dunlap 	iscsi_sess_t		*isp		= NULL;
104330e7468fSPeter Dunlap 	uint32_t		dlength		= 0;
104430e7468fSPeter Dunlap 	iscsi_hdr_t		*old_ihp	= NULL;
104530e7468fSPeter Dunlap 	iscsi_conn_t		*icp		= ic->ic_handle;
104630e7468fSPeter Dunlap 	uint8_t			*data 		= pdu->isp_data;
104730e7468fSPeter Dunlap 	iscsi_hdr_t		*ihp		= (iscsi_hdr_t *)irrhp;
104830e7468fSPeter Dunlap 	idm_status_t		status;
104930e7468fSPeter Dunlap 	iscsi_cmd_t		*icmdp	= NULL;
1050fcf3ce44SJohn Forte 
1051fcf3ce44SJohn Forte 	ASSERT(data != NULL);
105230e7468fSPeter Dunlap 	isp = icp->conn_sess;
105330e7468fSPeter Dunlap 	ASSERT(isp != NULL);
1054fcf3ce44SJohn Forte 
105530e7468fSPeter Dunlap 	mutex_enter(&icp->conn_queue_active.mutex);
105630e7468fSPeter Dunlap 	if ((status = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)irrhp,
105730e7468fSPeter Dunlap 	    &icmdp)) != IDM_STATUS_SUCCESS) {
105830e7468fSPeter Dunlap 		mutex_exit(&icp->conn_queue_active.mutex);
105930e7468fSPeter Dunlap 		return (status);
1060fcf3ce44SJohn Forte 	}
1061fcf3ce44SJohn Forte 
1062fcf3ce44SJohn Forte 	/* If we don't have the rejected header we can't do anything */
1063fcf3ce44SJohn Forte 	dlength = n2h24(irrhp->dlength);
1064fcf3ce44SJohn Forte 	if (dlength < sizeof (iscsi_hdr_t)) {
106530e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1066fcf3ce44SJohn Forte 	}
1067fcf3ce44SJohn Forte 
1068fcf3ce44SJohn Forte 	/* map old ihp */
1069fcf3ce44SJohn Forte 	old_ihp = (iscsi_hdr_t *)data;
1070fcf3ce44SJohn Forte 
1071fcf3ce44SJohn Forte 	switch (irrhp->reason) {
1072fcf3ce44SJohn Forte 	/*
1073fcf3ce44SJohn Forte 	 * ISCSI_REJECT_IMM_CMD_REJECT - Immediate Command Reject
1074fcf3ce44SJohn Forte 	 * too many immediate commands (original cmd can be resent)
1075fcf3ce44SJohn Forte 	 */
1076fcf3ce44SJohn Forte 	case ISCSI_REJECT_IMM_CMD_REJECT:
1077fcf3ce44SJohn Forte 		/*
1078fcf3ce44SJohn Forte 		 * We have exceeded the server's capacity for outstanding
1079fcf3ce44SJohn Forte 		 * immediate commands.   This must be a task management
1080fcf3ce44SJohn Forte 		 * command so try to find it in the abortingqueue and
1081fcf3ce44SJohn Forte 		 * complete it.
1082fcf3ce44SJohn Forte 		 */
1083fcf3ce44SJohn Forte 		if (!(old_ihp->opcode & ISCSI_OP_IMMEDIATE)) {
1084fcf3ce44SJohn Forte 			/* Rejecting IMM but old old_hdr wasn't IMM */
108530e7468fSPeter Dunlap 			return (IDM_STATUS_PROTOCOL_ERROR);
1086fcf3ce44SJohn Forte 		}
1087fcf3ce44SJohn Forte 
1088fcf3ce44SJohn Forte 		/*
1089fcf3ce44SJohn Forte 		 * We only send NOP and TASK_MGT as IMM.  All other
1090fcf3ce44SJohn Forte 		 * cases should be considered as a protocol error.
1091fcf3ce44SJohn Forte 		 */
1092fcf3ce44SJohn Forte 		switch (old_ihp->opcode & ISCSI_OPCODE_MASK) {
1093fcf3ce44SJohn Forte 		case ISCSI_OP_NOOP_OUT:
1094fcf3ce44SJohn Forte 			/*
1095fcf3ce44SJohn Forte 			 * A ping was rejected - treat this like
1096fcf3ce44SJohn Forte 			 * ping response.  The down side is we
1097fcf3ce44SJohn Forte 			 * didn't get an updated MaxCmdSn.
1098fcf3ce44SJohn Forte 			 */
1099fcf3ce44SJohn Forte 			break;
1100fcf3ce44SJohn Forte 		case ISCSI_OP_SCSI_TASK_MGT_MSG:
110130e7468fSPeter Dunlap 			(void) iscsi_rx_process_rejected_tsk_mgt(ic, old_ihp);
1102fcf3ce44SJohn Forte 			break;
1103fcf3ce44SJohn Forte 		default:
1104fcf3ce44SJohn Forte 			cmn_err(CE_WARN, "iscsi connection(%u) protocol error "
1105fcf3ce44SJohn Forte 			    "- received a reject for a command(0x%02x) not "
1106fcf3ce44SJohn Forte 			    "sent as an immediate", icp->conn_oid,
1107fcf3ce44SJohn Forte 			    old_ihp->opcode);
110830e7468fSPeter Dunlap 			status = IDM_STATUS_PROTOCOL_ERROR;
110930e7468fSPeter Dunlap 			break;
1110fcf3ce44SJohn Forte 		}
1111fcf3ce44SJohn Forte 		break;
1112fcf3ce44SJohn Forte 
1113fcf3ce44SJohn Forte 	/*
1114fcf3ce44SJohn Forte 	 * For the rest of the reject cases just use the general
1115fcf3ce44SJohn Forte 	 * hammer of dis/reconnecting.  This will resolve all
1116fcf3ce44SJohn Forte 	 * noted issues although could be more graceful.
1117fcf3ce44SJohn Forte 	 */
1118fcf3ce44SJohn Forte 	case ISCSI_REJECT_DATA_DIGEST_ERROR:
1119fcf3ce44SJohn Forte 	case ISCSI_REJECT_CMD_BEFORE_LOGIN:
1120fcf3ce44SJohn Forte 	case ISCSI_REJECT_SNACK_REJECT:
1121fcf3ce44SJohn Forte 	case ISCSI_REJECT_PROTOCOL_ERROR:
1122fcf3ce44SJohn Forte 	case ISCSI_REJECT_CMD_NOT_SUPPORTED:
1123fcf3ce44SJohn Forte 	case ISCSI_REJECT_TASK_IN_PROGRESS:
1124fcf3ce44SJohn Forte 	case ISCSI_REJECT_INVALID_DATA_ACK:
1125fcf3ce44SJohn Forte 	case ISCSI_REJECT_INVALID_PDU_FIELD:
1126fcf3ce44SJohn Forte 	case ISCSI_REJECT_LONG_OPERATION_REJECT:
1127fcf3ce44SJohn Forte 	case ISCSI_REJECT_NEGOTIATION_RESET:
1128fcf3ce44SJohn Forte 	default:
1129fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) closing connection - "
1130fcf3ce44SJohn Forte 		    "target requested itt:0x%x reason:0x%x",
1131fcf3ce44SJohn Forte 		    icp->conn_oid, ihp->itt, irrhp->reason);
113230e7468fSPeter Dunlap 		status = IDM_STATUS_PROTOCOL_ERROR;
113330e7468fSPeter Dunlap 		break;
1134fcf3ce44SJohn Forte 	}
1135fcf3ce44SJohn Forte 
113630e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1137fcf3ce44SJohn Forte }
1138fcf3ce44SJohn Forte 
1139fcf3ce44SJohn Forte 
1140fcf3ce44SJohn Forte /*
1141fcf3ce44SJohn Forte  * iscsi_rx_process_rejected_tsk_mgt -
1142fcf3ce44SJohn Forte  */
114330e7468fSPeter Dunlap /* ARGSUSED */
114430e7468fSPeter Dunlap static idm_status_t
114530e7468fSPeter Dunlap iscsi_rx_process_rejected_tsk_mgt(idm_conn_t *ic, iscsi_hdr_t *old_ihp)
1146fcf3ce44SJohn Forte {
114730e7468fSPeter Dunlap 	iscsi_sess_t		*isp	= NULL;
114830e7468fSPeter Dunlap 	iscsi_cmd_t		*icmdp	= NULL;
114930e7468fSPeter Dunlap 	iscsi_conn_t		*icp 	= NULL;
1150fcf3ce44SJohn Forte 
1151fcf3ce44SJohn Forte 	isp = icp->conn_sess;
1152fcf3ce44SJohn Forte 	ASSERT(old_ihp != NULL);
115330e7468fSPeter Dunlap 	ASSERT(isp != NULL);
1154fcf3ce44SJohn Forte 
1155fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
1156fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
1157fcf3ce44SJohn Forte 	if (!ISCSI_SUCCESS(iscsi_rx_process_itt_to_icmdp(
1158fcf3ce44SJohn Forte 	    isp, old_ihp, &icmdp))) {
1159fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_cmdsn_mutex);
1160fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
116130e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1162fcf3ce44SJohn Forte 	}
1163fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
1164fcf3ce44SJohn Forte 
1165fcf3ce44SJohn Forte 	switch (icmdp->cmd_type) {
1166fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_ABORT:
1167fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_RESET:
1168fcf3ce44SJohn Forte 		iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E4,
1169fcf3ce44SJohn Forte 		    icp->conn_sess);
1170fcf3ce44SJohn Forte 		break;
1171fcf3ce44SJohn Forte 	/* We don't send any other task mgr types */
1172fcf3ce44SJohn Forte 	default:
1173fcf3ce44SJohn Forte 		ASSERT(B_FALSE);
1174fcf3ce44SJohn Forte 		break;
1175fcf3ce44SJohn Forte 	}
1176fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_queue_active.mutex);
1177fcf3ce44SJohn Forte 
117830e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1179fcf3ce44SJohn Forte }
1180fcf3ce44SJohn Forte 
1181fcf3ce44SJohn Forte 
1182fcf3ce44SJohn Forte /*
1183fcf3ce44SJohn Forte  * iscsi_rx_process_task_mgt_rsp -
1184fcf3ce44SJohn Forte  */
1185fcf3ce44SJohn Forte /* ARGSUSED */
118630e7468fSPeter Dunlap static idm_status_t
118730e7468fSPeter Dunlap iscsi_rx_process_task_mgt_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
1188fcf3ce44SJohn Forte {
1189fcf3ce44SJohn Forte 	iscsi_sess_t			*isp		= NULL;
1190fcf3ce44SJohn Forte 	iscsi_scsi_task_mgt_rsp_hdr_t	*istmrhp	= NULL;
1191fcf3ce44SJohn Forte 	iscsi_cmd_t			*icmdp		= NULL;
119230e7468fSPeter Dunlap 	iscsi_conn_t			*icp		= ic->ic_handle;
119330e7468fSPeter Dunlap 	idm_status_t			status = IDM_STATUS_SUCCESS;
1194fcf3ce44SJohn Forte 
1195fcf3ce44SJohn Forte 	isp = icp->conn_sess;
119630e7468fSPeter Dunlap 	istmrhp = (iscsi_scsi_task_mgt_rsp_hdr_t *)pdu->isp_hdr;
1197fcf3ce44SJohn Forte 
1198fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
119930e7468fSPeter Dunlap 	if ((status = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)istmrhp,
120030e7468fSPeter Dunlap 	    &icmdp)) != IDM_STATUS_SUCCESS) {
1201fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
120230e7468fSPeter Dunlap 		return (status);
1203fcf3ce44SJohn Forte 	}
1204fcf3ce44SJohn Forte 
1205fcf3ce44SJohn Forte 	switch (icmdp->cmd_type) {
1206fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_ABORT:
1207fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_RESET:
1208fcf3ce44SJohn Forte 		switch (istmrhp->response) {
1209fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_COMPLETE:
1210fcf3ce44SJohn Forte 			/* success */
1211fcf3ce44SJohn Forte 			iscsi_cmd_state_machine(icmdp,
1212fcf3ce44SJohn Forte 			    ISCSI_CMD_EVENT_E3, isp);
1213fcf3ce44SJohn Forte 			break;
1214fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_NO_TASK:
1215fcf3ce44SJohn Forte 			/*
1216fcf3ce44SJohn Forte 			 * If the array no longer knows about
1217fcf3ce44SJohn Forte 			 * an ABORT RTT and we no longer have
1218fcf3ce44SJohn Forte 			 * a parent SCSI command it was just
1219fcf3ce44SJohn Forte 			 * completed, free this ABORT resource.
1220fcf3ce44SJohn Forte 			 * Otherwise FALLTHRU this will flag a
1221fcf3ce44SJohn Forte 			 * protocol problem.
1222fcf3ce44SJohn Forte 			 */
1223fcf3ce44SJohn Forte 			if ((icmdp->cmd_type == ISCSI_CMD_TYPE_ABORT) &&
1224fcf3ce44SJohn Forte 			    (icmdp->cmd_un.abort.icmdp == NULL)) {
1225fcf3ce44SJohn Forte 				iscsi_cmd_state_machine(icmdp,
1226fcf3ce44SJohn Forte 				    ISCSI_CMD_EVENT_E4, isp);
1227fcf3ce44SJohn Forte 				break;
1228fcf3ce44SJohn Forte 			}
1229fcf3ce44SJohn Forte 			/* FALLTHRU */
12302b79d384Sbing zhao - Sun Microsystems - Beijing China 		case SCSI_TCP_TM_RESP_REJECTED:
12312b79d384Sbing zhao - Sun Microsystems - Beijing China 			/*
12322b79d384Sbing zhao - Sun Microsystems - Beijing China 			 * If the target rejects our reset task,
12332b79d384Sbing zhao - Sun Microsystems - Beijing China 			 * we should record the response and complete
12342b79d384Sbing zhao - Sun Microsystems - Beijing China 			 * this command with the result.
12352b79d384Sbing zhao - Sun Microsystems - Beijing China 			 */
12362b79d384Sbing zhao - Sun Microsystems - Beijing China 			if (icmdp->cmd_type == ISCSI_CMD_TYPE_RESET) {
12372b79d384Sbing zhao - Sun Microsystems - Beijing China 				icmdp->cmd_un.reset.response =
12382b79d384Sbing zhao - Sun Microsystems - Beijing China 				    istmrhp->response;
12392b79d384Sbing zhao - Sun Microsystems - Beijing China 				iscsi_cmd_state_machine(icmdp,
12402b79d384Sbing zhao - Sun Microsystems - Beijing China 				    ISCSI_CMD_EVENT_E3, isp);
12412b79d384Sbing zhao - Sun Microsystems - Beijing China 				break;
12422b79d384Sbing zhao - Sun Microsystems - Beijing China 			}
12432b79d384Sbing zhao - Sun Microsystems - Beijing China 			/* FALLTHRU */
1244fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_NO_LUN:
1245fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_TASK_ALLEGIANT:
1246fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_NO_FAILOVER:
1247fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_IN_PRGRESS:
1248fcf3ce44SJohn Forte 		default:
1249fcf3ce44SJohn Forte 			/*
1250fcf3ce44SJohn Forte 			 * Something is out of sync.  Flush
1251fcf3ce44SJohn Forte 			 * active queues and resync the
1252fcf3ce44SJohn Forte 			 * the connection to try and recover
1253fcf3ce44SJohn Forte 			 * to a known state.
1254fcf3ce44SJohn Forte 			 */
125530e7468fSPeter Dunlap 			status = IDM_STATUS_PROTOCOL_ERROR;
1256fcf3ce44SJohn Forte 		}
1257fcf3ce44SJohn Forte 		break;
1258fcf3ce44SJohn Forte 
1259fcf3ce44SJohn Forte 	default:
1260fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) protocol error - "
1261fcf3ce44SJohn Forte 		    "received a task mgt response for a non-task mgt "
1262fcf3ce44SJohn Forte 		    "cmd itt:0x%x type:%d", icp->conn_oid, istmrhp->itt,
1263fcf3ce44SJohn Forte 		    icmdp->cmd_type);
126430e7468fSPeter Dunlap 		status = IDM_STATUS_PROTOCOL_ERROR;
126530e7468fSPeter Dunlap 		break;
1266fcf3ce44SJohn Forte 	}
1267fcf3ce44SJohn Forte 
1268fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_queue_active.mutex);
126930e7468fSPeter Dunlap 	return (status);
1270fcf3ce44SJohn Forte }
1271fcf3ce44SJohn Forte 
1272fcf3ce44SJohn Forte 
1273fcf3ce44SJohn Forte /*
127430e7468fSPeter Dunlap  * iscsi_rx_process_logout_rsp -
1275fcf3ce44SJohn Forte  *
1276fcf3ce44SJohn Forte  */
1277fcf3ce44SJohn Forte /* ARGSUSED */
127830e7468fSPeter Dunlap idm_status_t
127930e7468fSPeter Dunlap iscsi_rx_process_logout_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
1280fcf3ce44SJohn Forte {
128130e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
128230e7468fSPeter Dunlap 	iscsi_logout_rsp_hdr_t	*ilrhp	=
128330e7468fSPeter Dunlap 	    (iscsi_logout_rsp_hdr_t *)pdu->isp_hdr;
1284fcf3ce44SJohn Forte 	iscsi_cmd_t		*icmdp	= NULL;
128530e7468fSPeter Dunlap 	iscsi_sess_t		*isp;
128630e7468fSPeter Dunlap 	idm_status_t		status = IDM_STATUS_SUCCESS;
1287fcf3ce44SJohn Forte 
1288fcf3ce44SJohn Forte 	isp = icp->conn_sess;
1289fcf3ce44SJohn Forte 
1290fcf3ce44SJohn Forte 	if (icp->conn_expstatsn != ntohl(ilrhp->statsn)) {
129130e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi connection(%u/%x) protocol error - "
1292fcf3ce44SJohn Forte 		    "received status out of order itt:0x%x statsn:0x%x "
129330e7468fSPeter Dunlap 		    "expstatsn:0x%x", icp->conn_oid, ilrhp->opcode, ilrhp->itt,
1294fcf3ce44SJohn Forte 		    ntohl(ilrhp->statsn), icp->conn_expstatsn);
129530e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1296fcf3ce44SJohn Forte 	}
1297fcf3ce44SJohn Forte 
1298fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
1299fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
1300fcf3ce44SJohn Forte 	if (ilrhp->itt != ISCSI_RSVD_TASK_TAG) {
1301fcf3ce44SJohn Forte 		if (!ISCSI_SUCCESS(iscsi_rx_process_itt_to_icmdp(
130230e7468fSPeter Dunlap 		    isp, (iscsi_hdr_t *)ilrhp, &icmdp))) {
1303fcf3ce44SJohn Forte 			mutex_exit(&isp->sess_cmdsn_mutex);
1304fcf3ce44SJohn Forte 			mutex_exit(&icp->conn_queue_active.mutex);
130530e7468fSPeter Dunlap 			return (IDM_STATUS_PROTOCOL_ERROR);
1306fcf3ce44SJohn Forte 		}
1307fcf3ce44SJohn Forte 	}
1308fcf3ce44SJohn Forte 
1309fcf3ce44SJohn Forte 	/* update expcmdsn and maxcmdsn */
1310fcf3ce44SJohn Forte 	iscsi_update_flow_control(isp, ntohl(ilrhp->maxcmdsn),
1311fcf3ce44SJohn Forte 	    ntohl(ilrhp->expcmdsn));
1312fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
1313fcf3ce44SJohn Forte 
131430e7468fSPeter Dunlap 	ISCSI_IO_LOG(CE_NOTE,
131530e7468fSPeter Dunlap 	    "DEBUG: iscsi_rx_process_logout_rsp: response: %d",
131630e7468fSPeter Dunlap 	    ilrhp->response);
1317fcf3ce44SJohn Forte 	switch (ilrhp->response) {
1318fcf3ce44SJohn Forte 	case ISCSI_LOGOUT_CID_NOT_FOUND:
1319fcf3ce44SJohn Forte 		/*
1320fcf3ce44SJohn Forte 		 * If the target doesn't know about our connection
1321fcf3ce44SJohn Forte 		 * then we can consider our self disconnected.
1322fcf3ce44SJohn Forte 		 */
1323fcf3ce44SJohn Forte 		/* FALLTHRU */
1324fcf3ce44SJohn Forte 	case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
1325fcf3ce44SJohn Forte 		/*
1326fcf3ce44SJohn Forte 		 * We don't support ErrorRecovery levels above 0
1327fcf3ce44SJohn Forte 		 * currently so consider this success.
1328fcf3ce44SJohn Forte 		 */
1329fcf3ce44SJohn Forte 		/* FALLTHRU */
1330fcf3ce44SJohn Forte 	case ISCSI_LOGOUT_CLEANUP_FAILED:
1331fcf3ce44SJohn Forte 		/*
1332fcf3ce44SJohn Forte 		 * per spec. "cleanup failed for various reasons."
1333fcf3ce44SJohn Forte 		 * Although those various reasons are undefined.
1334fcf3ce44SJohn Forte 		 * Not sure what to do here.  So fake success,
1335fcf3ce44SJohn Forte 		 * which will disconnect the connection.
1336fcf3ce44SJohn Forte 		 */
1337fcf3ce44SJohn Forte 		/* FALLTHRU */
1338fcf3ce44SJohn Forte 	case ISCSI_LOGOUT_SUCCESS:
1339fcf3ce44SJohn Forte 		iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp);
1340fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
134130e7468fSPeter Dunlap 		iscsi_drop_conn_cleanup(icp);
1342fcf3ce44SJohn Forte 		break;
1343fcf3ce44SJohn Forte 	default:
1344fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
134530e7468fSPeter Dunlap 		status = IDM_STATUS_PROTOCOL_ERROR;
134630e7468fSPeter Dunlap 		break;
1347fcf3ce44SJohn Forte 
134830e7468fSPeter Dunlap 	}
134930e7468fSPeter Dunlap 	return (status);
1350fcf3ce44SJohn Forte }
1351fcf3ce44SJohn Forte 
1352fcf3ce44SJohn Forte /*
135330e7468fSPeter Dunlap  * iscsi_rx_process_async_rsp
1354fcf3ce44SJohn Forte  *
1355fcf3ce44SJohn Forte  */
1356fcf3ce44SJohn Forte /* ARGSUSED */
135730e7468fSPeter Dunlap static idm_status_t
135830e7468fSPeter Dunlap iscsi_rx_process_async_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
1359fcf3ce44SJohn Forte {
136030e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
136130e7468fSPeter Dunlap 	iscsi_sess_t		*isp	= icp->conn_sess;
136230e7468fSPeter Dunlap 	idm_status_t		rval	= IDM_STATUS_SUCCESS;
136330e7468fSPeter Dunlap 	iscsi_task_t		*itp;
136430e7468fSPeter Dunlap 	iscsi_async_evt_hdr_t	*iaehp	=
136530e7468fSPeter Dunlap 	    (iscsi_async_evt_hdr_t *)pdu->isp_hdr;
1366fcf3ce44SJohn Forte 
1367fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
136830e7468fSPeter Dunlap 	ASSERT(pdu != NULL);
136930e7468fSPeter Dunlap 	ASSERT(isp != NULL);
1370fcf3ce44SJohn Forte 
137130e7468fSPeter Dunlap 	mutex_enter(&isp->sess_cmdsn_mutex);
137230e7468fSPeter Dunlap 	if (icp->conn_expstatsn == ntohl(iaehp->statsn)) {
137330e7468fSPeter Dunlap 		icp->conn_expstatsn++;
137430e7468fSPeter Dunlap 	} else {
1375fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) protocol error - "
137630e7468fSPeter Dunlap 		    "received status out of order statsn:0x%x "
137730e7468fSPeter Dunlap 		    "expstatsn:0x%x", icp->conn_oid,
1378fcf3ce44SJohn Forte 		    ntohl(iaehp->statsn), icp->conn_expstatsn);
137930e7468fSPeter Dunlap 		mutex_exit(&isp->sess_cmdsn_mutex);
138030e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1381fcf3ce44SJohn Forte 	}
138230e7468fSPeter Dunlap 	mutex_exit(&isp->sess_cmdsn_mutex);
1383fcf3ce44SJohn Forte 
1384fcf3ce44SJohn Forte 	switch (iaehp->async_event) {
1385fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_SCSI_EVENT:
1386fcf3ce44SJohn Forte 		/*
1387fcf3ce44SJohn Forte 		 * SCSI asynchronous event is reported in
1388fcf3ce44SJohn Forte 		 * the sense data.  Sense data that accompanies
1389fcf3ce44SJohn Forte 		 * the report in the data segment identifies the
1390fcf3ce44SJohn Forte 		 * condition.  If the target supports SCSI
1391fcf3ce44SJohn Forte 		 * asynchronous events reporting (see [SAM2])
1392fcf3ce44SJohn Forte 		 * as indicated in the stardard INQUIRY data
1393fcf3ce44SJohn Forte 		 * (see [SPC3]), its use may be enabled by
1394fcf3ce44SJohn Forte 		 * parameters in the SCSI control mode page
1395fcf3ce44SJohn Forte 		 * (see [SPC3]).
1396fcf3ce44SJohn Forte 		 *
1397fcf3ce44SJohn Forte 		 * T-10 has removed SCSI asunchronous events
1398fcf3ce44SJohn Forte 		 * from the standard.  Although we have seen
1399fcf3ce44SJohn Forte 		 * a couple targets still spending these requests.
1400fcf3ce44SJohn Forte 		 * Those targets were specifically sending them
1401fcf3ce44SJohn Forte 		 * for notification of a LUN/Volume change
1402fcf3ce44SJohn Forte 		 * (ex. LUN addition/removal).  Take a general
1403fcf3ce44SJohn Forte 		 * action to these events of dis/reconnecting.
1404fcf3ce44SJohn Forte 		 * Once reconnected we perform a reenumeration.
1405fcf3ce44SJohn Forte 		 */
140630e7468fSPeter Dunlap 		idm_ini_conn_disconnect(ic);
1407fcf3ce44SJohn Forte 		break;
1408fcf3ce44SJohn Forte 
1409fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_REQUEST_LOGOUT:
141030e7468fSPeter Dunlap 		/*
141130e7468fSPeter Dunlap 		 * We've been asked to logout by the target --
141230e7468fSPeter Dunlap 		 * we need to treat this differently from a normal logout
141330e7468fSPeter Dunlap 		 * due to a discovery failure.  Normal logouts result in
141430e7468fSPeter Dunlap 		 * an N3 event to the session state machine and an offline
141530e7468fSPeter Dunlap 		 * of the lun.  In this case we want to put the connection
141630e7468fSPeter Dunlap 		 * into "failed" state and generate N5 to the session state
141730e7468fSPeter Dunlap 		 * machine since the initiator logged out at the target's
141830e7468fSPeter Dunlap 		 * request.  To track this we set a flag indicating we
141930e7468fSPeter Dunlap 		 * received this async logout request from the tharget
142030e7468fSPeter Dunlap 		 */
1421fcf3ce44SJohn Forte 		mutex_enter(&icp->conn_state_mutex);
142230e7468fSPeter Dunlap 		icp->conn_async_logout = B_TRUE;
1423fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_state_mutex);
142430e7468fSPeter Dunlap 
142530e7468fSPeter Dunlap 		/* Target has requested this connection to logout. */
142630e7468fSPeter Dunlap 		itp = kmem_zalloc(sizeof (iscsi_task_t), KM_SLEEP);
142730e7468fSPeter Dunlap 		itp->t_arg = icp;
142830e7468fSPeter Dunlap 		itp->t_blocking = B_FALSE;
142930e7468fSPeter Dunlap 		if (ddi_taskq_dispatch(isp->sess_taskq,
143030e7468fSPeter Dunlap 		    (void(*)())iscsi_logout_start, itp, DDI_SLEEP) !=
143130e7468fSPeter Dunlap 		    DDI_SUCCESS) {
143230e7468fSPeter Dunlap 			/* Disconnect if we couldn't dispatch the task */
143330e7468fSPeter Dunlap 			idm_ini_conn_disconnect(ic);
143430e7468fSPeter Dunlap 		}
1435fcf3ce44SJohn Forte 		break;
1436fcf3ce44SJohn Forte 
1437fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_DROPPING_CONNECTION:
1438fcf3ce44SJohn Forte 		/*
1439fcf3ce44SJohn Forte 		 * Target is going to drop our connection.
1440fcf3ce44SJohn Forte 		 *	param1 - CID which will be dropped.
1441fcf3ce44SJohn Forte 		 *	param2 - Min time to reconnect.
1442fcf3ce44SJohn Forte 		 *	param3 - Max time to reconnect.
1443fcf3ce44SJohn Forte 		 *
1444fcf3ce44SJohn Forte 		 * For now just let fail as another disconnect.
1445fcf3ce44SJohn Forte 		 *
1446fcf3ce44SJohn Forte 		 * MC/S Once we support > 1 connections then
1447fcf3ce44SJohn Forte 		 * we need to check the CID and drop that
1448fcf3ce44SJohn Forte 		 * specific connection.
1449fcf3ce44SJohn Forte 		 */
145030e7468fSPeter Dunlap 		iscsi_conn_set_login_min_max(icp, iaehp->param2,
145130e7468fSPeter Dunlap 		    iaehp->param3);
145230e7468fSPeter Dunlap 		idm_ini_conn_disconnect(ic);
1453fcf3ce44SJohn Forte 		break;
1454fcf3ce44SJohn Forte 
1455fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_DROPPING_ALL_CONNECTIONS:
1456fcf3ce44SJohn Forte 		/*
1457fcf3ce44SJohn Forte 		 * Target is going to drop ALL connections.
1458fcf3ce44SJohn Forte 		 *	param2 - Min time to reconnect.
1459fcf3ce44SJohn Forte 		 *	param3 - Max time to reconnect.
1460fcf3ce44SJohn Forte 		 *
1461fcf3ce44SJohn Forte 		 * For now just let fail as anyother disconnect.
1462fcf3ce44SJohn Forte 		 *
1463fcf3ce44SJohn Forte 		 * MC/S Once we support more than > 1 connections
1464fcf3ce44SJohn Forte 		 * then we need to drop all connections on the
1465fcf3ce44SJohn Forte 		 * session.
1466fcf3ce44SJohn Forte 		 */
146730e7468fSPeter Dunlap 		iscsi_conn_set_login_min_max(icp, iaehp->param2,
146830e7468fSPeter Dunlap 		    iaehp->param3);
146930e7468fSPeter Dunlap 		idm_ini_conn_disconnect(ic);
1470fcf3ce44SJohn Forte 		break;
1471fcf3ce44SJohn Forte 
1472fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_PARAM_NEGOTIATION:
1473fcf3ce44SJohn Forte 		/*
1474fcf3ce44SJohn Forte 		 * Target requests parameter negotiation
1475fcf3ce44SJohn Forte 		 * on this connection.
1476fcf3ce44SJohn Forte 		 *
1477fcf3ce44SJohn Forte 		 * The initiator must honor this request.  For
1478fcf3ce44SJohn Forte 		 * now we will request a logout.  We can't
1479fcf3ce44SJohn Forte 		 * just ignore this or it might force corruption?
1480fcf3ce44SJohn Forte 		 */
148130e7468fSPeter Dunlap 		itp = kmem_zalloc(sizeof (iscsi_task_t), KM_SLEEP);
148230e7468fSPeter Dunlap 		itp->t_arg = icp;
148330e7468fSPeter Dunlap 		itp->t_blocking = B_FALSE;
148430e7468fSPeter Dunlap 		if (ddi_taskq_dispatch(isp->sess_taskq,
148530e7468fSPeter Dunlap 		    (void(*)())iscsi_logout_start, itp, DDI_SLEEP) !=
148630e7468fSPeter Dunlap 		    DDI_SUCCESS) {
148730e7468fSPeter Dunlap 			/* Disconnect if we couldn't dispatch the task */
148830e7468fSPeter Dunlap 			idm_ini_conn_disconnect(ic);
148930e7468fSPeter Dunlap 		}
1490fcf3ce44SJohn Forte 		break;
1491fcf3ce44SJohn Forte 
1492fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_VENDOR_SPECIFIC:
1493fcf3ce44SJohn Forte 		/*
1494fcf3ce44SJohn Forte 		 * We currently don't handle any vendor
1495fcf3ce44SJohn Forte 		 * specific async events.  So just ignore
1496fcf3ce44SJohn Forte 		 * the request.
1497fcf3ce44SJohn Forte 		 */
149830e7468fSPeter Dunlap 		idm_ini_conn_disconnect(ic);
1499fcf3ce44SJohn Forte 		break;
1500fcf3ce44SJohn Forte 	default:
150130e7468fSPeter Dunlap 		rval = IDM_STATUS_PROTOCOL_ERROR;
1502fcf3ce44SJohn Forte 	}
1503fcf3ce44SJohn Forte 
1504fcf3ce44SJohn Forte 	return (rval);
1505fcf3ce44SJohn Forte }
1506fcf3ce44SJohn Forte 
1507fcf3ce44SJohn Forte /*
1508fcf3ce44SJohn Forte  * iscsi_rx_process_text_rsp - processes iSCSI text response.  It sets
1509fcf3ce44SJohn Forte  * the cmd_result field of the command data structure with the actual
1510fcf3ce44SJohn Forte  * status value instead of returning the status value.  The return value
1511fcf3ce44SJohn Forte  * is SUCCESS in order to let iscsi_handle_text control the operation of
1512fcf3ce44SJohn Forte  * a text request.
151330e7468fSPeter Dunlap  * Text requests are a handled a little different than other types of
1514fcf3ce44SJohn Forte  * iSCSI commands because the initiator sends additional empty text requests
1515fcf3ce44SJohn Forte  * in order to obtain the remaining responses required to complete the
1516fcf3ce44SJohn Forte  * request.  iscsi_handle_text controls the operation of text request, while
1517fcf3ce44SJohn Forte  * iscsi_rx_process_text_rsp just process the current response.
1518fcf3ce44SJohn Forte  */
151930e7468fSPeter Dunlap static idm_status_t
152030e7468fSPeter Dunlap iscsi_rx_process_text_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
1521fcf3ce44SJohn Forte {
1522fcf3ce44SJohn Forte 	iscsi_sess_t		*isp	= NULL;
152330e7468fSPeter Dunlap 	iscsi_text_rsp_hdr_t	*ithp	=
152430e7468fSPeter Dunlap 	    (iscsi_text_rsp_hdr_t *)pdu->isp_hdr;
152530e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
1526fcf3ce44SJohn Forte 	iscsi_cmd_t		*icmdp	= NULL;
1527fcf3ce44SJohn Forte 	boolean_t		final	= B_FALSE;
1528fcf3ce44SJohn Forte 	uint32_t		data_len;
152930e7468fSPeter Dunlap 	uint8_t			*data = pdu->isp_data;
153030e7468fSPeter Dunlap 	idm_status_t		rval;
1531fcf3ce44SJohn Forte 
1532fcf3ce44SJohn Forte 	isp = icp->conn_sess;
1533fcf3ce44SJohn Forte 
1534fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
153530e7468fSPeter Dunlap 	if ((rval = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)ithp,
153630e7468fSPeter Dunlap 	    &icmdp)) != IDM_STATUS_SUCCESS) {
1537fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
153830e7468fSPeter Dunlap 		return (rval);
1539fcf3ce44SJohn Forte 	}
1540fcf3ce44SJohn Forte 
1541fcf3ce44SJohn Forte 	/* update local final response flag */
1542fcf3ce44SJohn Forte 	if (ithp->flags & ISCSI_FLAG_FINAL) {
1543fcf3ce44SJohn Forte 		final = B_TRUE;
1544fcf3ce44SJohn Forte 	}
1545fcf3ce44SJohn Forte 
1546fcf3ce44SJohn Forte 	/*
1547fcf3ce44SJohn Forte 	 * validate received TTT value.  RFC3720 specifies the following:
1548fcf3ce44SJohn Forte 	 * - F bit set to 1 MUST have a reserved TTT value 0xffffffff
1549fcf3ce44SJohn Forte 	 * - F bit set to 0 MUST have a non-reserved TTT value !0xffffffff
1550fcf3ce44SJohn Forte 	 * In addition, the received TTT value must not change between
1551fcf3ce44SJohn Forte 	 * responses of a long text response
1552fcf3ce44SJohn Forte 	 */
1553fcf3ce44SJohn Forte 	if (((final == B_TRUE) && (ithp->ttt != ISCSI_RSVD_TASK_TAG)) ||
1554fcf3ce44SJohn Forte 	    ((final == B_FALSE) && (ithp->ttt == ISCSI_RSVD_TASK_TAG))) {
1555fcf3ce44SJohn Forte 		icmdp->cmd_result = ISCSI_STATUS_PROTOCOL_ERROR;
1556fcf3ce44SJohn Forte 		icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_FINAL_RSP;
1557fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
1558fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) protocol error - "
1559fcf3ce44SJohn Forte 		    "received text response with invalid flags:0x%x or "
1560fcf3ce44SJohn Forte 		    "ttt:0x%x", icp->conn_oid, ithp->flags, ithp->itt);
156130e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1562fcf3ce44SJohn Forte 	}
1563fcf3ce44SJohn Forte 
1564fcf3ce44SJohn Forte 	if ((icmdp->cmd_un.text.stage == ISCSI_CMD_TEXT_INITIAL_REQ) &&
1565fcf3ce44SJohn Forte 	    (ithp->ttt == ISCSI_RSVD_TASK_TAG) &&
1566fcf3ce44SJohn Forte 	    (final == B_FALSE)) {
1567fcf3ce44SJohn Forte 		/* TTT should have matched reserved value */
1568fcf3ce44SJohn Forte 		icmdp->cmd_result = ISCSI_STATUS_PROTOCOL_ERROR;
1569fcf3ce44SJohn Forte 		icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_FINAL_RSP;
1570fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
1571fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) protocol "
1572fcf3ce44SJohn Forte 		    "error - received text response with invalid "
1573fcf3ce44SJohn Forte 		    "ttt:0x%x", icp->conn_oid, ithp->ttt);
157430e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1575fcf3ce44SJohn Forte 	}
1576fcf3ce44SJohn Forte 
1577fcf3ce44SJohn Forte 	/*
1578fcf3ce44SJohn Forte 	 * If this is first response, save away TTT value for later use
1579fcf3ce44SJohn Forte 	 * in a long text request/response sequence
1580fcf3ce44SJohn Forte 	 */
1581fcf3ce44SJohn Forte 	if (icmdp->cmd_un.text.stage == ISCSI_CMD_TEXT_INITIAL_REQ) {
1582fcf3ce44SJohn Forte 		icmdp->cmd_un.text.ttt = ithp->ttt;
1583fcf3ce44SJohn Forte 	}
1584fcf3ce44SJohn Forte 
1585fcf3ce44SJohn Forte 	data_len = ntoh24(ithp->dlength);
1586fcf3ce44SJohn Forte 
1587fcf3ce44SJohn Forte 	/* check whether enough buffer available to copy data */
1588fcf3ce44SJohn Forte 	if ((icmdp->cmd_un.text.total_rx_len + data_len) >
1589fcf3ce44SJohn Forte 	    icmdp->cmd_un.text.buf_len) {
1590fcf3ce44SJohn Forte 		icmdp->cmd_un.text.total_rx_len += data_len;
1591fcf3ce44SJohn Forte 		icmdp->cmd_result = ISCSI_STATUS_DATA_OVERFLOW;
1592fcf3ce44SJohn Forte 		/*
1593fcf3ce44SJohn Forte 		 * DATA_OVERFLOW will result in a SUCCESS return so that
1594fcf3ce44SJohn Forte 		 * iscsi_handle_text can continue to obtain the remaining
1595fcf3ce44SJohn Forte 		 * text response if needed.
1596fcf3ce44SJohn Forte 		 */
1597fcf3ce44SJohn Forte 	} else {
1598fcf3ce44SJohn Forte 		char *buf_data = (icmdp->cmd_un.text.buf +
1599fcf3ce44SJohn Forte 		    icmdp->cmd_un.text.offset);
1600fcf3ce44SJohn Forte 
1601fcf3ce44SJohn Forte 		bcopy(data, buf_data, data_len);
1602fcf3ce44SJohn Forte 		icmdp->cmd_un.text.offset += data_len;
1603fcf3ce44SJohn Forte 		icmdp->cmd_un.text.total_rx_len += data_len;
1604fcf3ce44SJohn Forte 		icmdp->cmd_result = ISCSI_STATUS_SUCCESS;
1605fcf3ce44SJohn Forte 		bcopy(ithp->rsvd4, icmdp->cmd_un.text.lun,
1606fcf3ce44SJohn Forte 		    sizeof (icmdp->cmd_un.text.lun));
1607fcf3ce44SJohn Forte 	}
1608fcf3ce44SJohn Forte 
1609fcf3ce44SJohn Forte 	/* update stage  */
1610fcf3ce44SJohn Forte 	if (final == B_TRUE) {
1611fcf3ce44SJohn Forte 		icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_FINAL_RSP;
1612fcf3ce44SJohn Forte 	} else {
1613fcf3ce44SJohn Forte 		icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_CONTINUATION;
1614fcf3ce44SJohn Forte 	}
1615fcf3ce44SJohn Forte 
1616fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp);
1617fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_queue_active.mutex);
161830e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
161930e7468fSPeter Dunlap }
162030e7468fSPeter Dunlap 
162130e7468fSPeter Dunlap /*
162230e7468fSPeter Dunlap  * iscsi_rx_process_scsi_itt_to_icmdp - Lookup itt using IDM to find matching
162330e7468fSPeter Dunlap  * icmdp.  Verify itt in hdr and icmdp are the same.
162430e7468fSPeter Dunlap  */
162530e7468fSPeter Dunlap static iscsi_status_t
162630e7468fSPeter Dunlap iscsi_rx_process_scsi_itt_to_icmdp(iscsi_sess_t *isp, idm_conn_t *ic,
162730e7468fSPeter Dunlap     iscsi_scsi_rsp_hdr_t *ihp, iscsi_cmd_t **icmdp)
162830e7468fSPeter Dunlap {
162930e7468fSPeter Dunlap 	idm_task_t *itp;
163030e7468fSPeter Dunlap 
163130e7468fSPeter Dunlap 	ASSERT(isp != NULL);
163230e7468fSPeter Dunlap 	ASSERT(ihp != NULL);
163330e7468fSPeter Dunlap 	ASSERT(icmdp != NULL);
163430e7468fSPeter Dunlap 	ASSERT(mutex_owned(&isp->sess_cmdsn_mutex));
163530e7468fSPeter Dunlap 	itp = idm_task_find_and_complete(ic, ihp->itt, ISCSI_INI_TASK_TTT);
163630e7468fSPeter Dunlap 	if (itp == NULL) {
163730e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi session(%u) protocol error - "
163830e7468fSPeter Dunlap 		    "received unknown itt:0x%x - protocol error",
163930e7468fSPeter Dunlap 		    isp->sess_oid, ihp->itt);
164030e7468fSPeter Dunlap 		return (ISCSI_STATUS_INTERNAL_ERROR);
164130e7468fSPeter Dunlap 	}
164230e7468fSPeter Dunlap 	*icmdp = itp->idt_private;
164330e7468fSPeter Dunlap 
164430e7468fSPeter Dunlap 	idm_task_rele(itp);
164530e7468fSPeter Dunlap 
1646fcf3ce44SJohn Forte 	return (ISCSI_STATUS_SUCCESS);
164730e7468fSPeter Dunlap 
1648fcf3ce44SJohn Forte }
1649fcf3ce44SJohn Forte 
1650fcf3ce44SJohn Forte /*
1651fcf3ce44SJohn Forte  * iscsi_rx_process_itt_to_icmdp - Lookup itt in the session's
1652fcf3ce44SJohn Forte  * cmd table to find matching icmdp.  Verify itt in hdr and
1653fcf3ce44SJohn Forte  * icmdp are the same.
1654fcf3ce44SJohn Forte  */
1655fcf3ce44SJohn Forte static iscsi_status_t
1656fcf3ce44SJohn Forte iscsi_rx_process_itt_to_icmdp(iscsi_sess_t *isp, iscsi_hdr_t *ihp,
1657fcf3ce44SJohn Forte     iscsi_cmd_t **icmdp)
1658fcf3ce44SJohn Forte {
1659fcf3ce44SJohn Forte 	int cmd_table_idx = 0;
1660fcf3ce44SJohn Forte 
1661fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
1662fcf3ce44SJohn Forte 	ASSERT(ihp != NULL);
1663fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
1664fcf3ce44SJohn Forte 	ASSERT(mutex_owned(&isp->sess_cmdsn_mutex));
1665fcf3ce44SJohn Forte 
1666fcf3ce44SJohn Forte 	/* try to find an associated iscsi_pkt */
166730e7468fSPeter Dunlap 	cmd_table_idx = (ihp->itt - IDM_TASKIDS_MAX) % ISCSI_CMD_TABLE_SIZE;
1668fcf3ce44SJohn Forte 	if (isp->sess_cmd_table[cmd_table_idx] == NULL) {
1669fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi session(%u) protocol error - "
1670fcf3ce44SJohn Forte 		    "received unknown itt:0x%x - protocol error",
1671fcf3ce44SJohn Forte 		    isp->sess_oid, ihp->itt);
1672fcf3ce44SJohn Forte 		return (ISCSI_STATUS_INTERNAL_ERROR);
1673fcf3ce44SJohn Forte 	}
1674fcf3ce44SJohn Forte 
1675fcf3ce44SJohn Forte 	/* verify itt */
1676fcf3ce44SJohn Forte 	if (isp->sess_cmd_table[cmd_table_idx]->cmd_itt != ihp->itt) {
1677fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi session(%u) received itt:0x%x "
1678fcf3ce44SJohn Forte 		    " which is out of sync with itt:0x%x", isp->sess_oid,
1679fcf3ce44SJohn Forte 		    ihp->itt, isp->sess_cmd_table[cmd_table_idx]->cmd_itt);
1680fcf3ce44SJohn Forte 		return (ISCSI_STATUS_INTERNAL_ERROR);
1681fcf3ce44SJohn Forte 	}
1682fcf3ce44SJohn Forte 
168329e23f3fSandrew.rutz@sun.com 	/* ensure that icmdp is still in Active state */
168429e23f3fSandrew.rutz@sun.com 	if (isp->sess_cmd_table[cmd_table_idx]->cmd_state !=
168529e23f3fSandrew.rutz@sun.com 	    ISCSI_CMD_STATE_ACTIVE) {
168629e23f3fSandrew.rutz@sun.com 		cmn_err(CE_WARN, "iscsi session(%u) received itt:0x%x "
168729e23f3fSandrew.rutz@sun.com 		    "but icmdp (%p) is not in active state",
168829e23f3fSandrew.rutz@sun.com 		    isp->sess_oid, ihp->itt,
168929e23f3fSandrew.rutz@sun.com 		    (void *)isp->sess_cmd_table[cmd_table_idx]);
169029e23f3fSandrew.rutz@sun.com 		return (ISCSI_STATUS_INTERNAL_ERROR);
169129e23f3fSandrew.rutz@sun.com 	}
169229e23f3fSandrew.rutz@sun.com 
1693fcf3ce44SJohn Forte 	/* make sure this is a SCSI cmd */
1694fcf3ce44SJohn Forte 	*icmdp = isp->sess_cmd_table[cmd_table_idx];
1695fcf3ce44SJohn Forte 
1696fcf3ce44SJohn Forte 	return (ISCSI_STATUS_SUCCESS);
1697fcf3ce44SJohn Forte }
1698fcf3ce44SJohn Forte 
1699fcf3ce44SJohn Forte /*
1700fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1701fcf3ce44SJohn Forte  * | End of protocol receive routines					|
1702fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1703fcf3ce44SJohn Forte  */
1704fcf3ce44SJohn Forte 
1705fcf3ce44SJohn Forte /*
1706fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1707fcf3ce44SJohn Forte  * | Beginning of protocol send routines				|
1708fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1709fcf3ce44SJohn Forte  */
1710fcf3ce44SJohn Forte 
1711fcf3ce44SJohn Forte 
1712fcf3ce44SJohn Forte /*
1713fcf3ce44SJohn Forte  * iscsi_tx_thread - This thread is the driving point for all
171430e7468fSPeter Dunlap  * iSCSI PDUs after login.  No PDUs should call idm_pdu_tx()
1715fcf3ce44SJohn Forte  * directly they should be funneled through iscsi_tx_thread.
1716fcf3ce44SJohn Forte  */
1717fcf3ce44SJohn Forte void
1718fcf3ce44SJohn Forte iscsi_tx_thread(iscsi_thread_t *thread, void *arg)
1719fcf3ce44SJohn Forte {
1720fcf3ce44SJohn Forte 	iscsi_conn_t	*icp	= (iscsi_conn_t *)arg;
1721fcf3ce44SJohn Forte 	iscsi_sess_t	*isp	= NULL;
1722fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp	= NULL;
1723fcf3ce44SJohn Forte 	clock_t		tout;
1724fcf3ce44SJohn Forte 	int		ret	= 1;
1725fcf3ce44SJohn Forte 
1726fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
1727fcf3ce44SJohn Forte 	isp = icp->conn_sess;
1728fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
1729fcf3ce44SJohn Forte 	ASSERT(thread != NULL);
1730fcf3ce44SJohn Forte 	ASSERT(thread->signature == SIG_ISCSI_THREAD);
1731fcf3ce44SJohn Forte 
1732fcf3ce44SJohn Forte 	tout = SEC_TO_TICK(1);
1733fcf3ce44SJohn Forte 	/*
1734fcf3ce44SJohn Forte 	 * Transfer icmdps until shutdown by owning session.
1735fcf3ce44SJohn Forte 	 */
1736fcf3ce44SJohn Forte 	while (ret != 0) {
1737fcf3ce44SJohn Forte 
1738fcf3ce44SJohn Forte 		isp->sess_window_open = B_TRUE;
1739fcf3ce44SJohn Forte 		/*
1740fcf3ce44SJohn Forte 		 * While the window is open, there are commands available
1741fcf3ce44SJohn Forte 		 * to send and the session state allows those commands to
1742fcf3ce44SJohn Forte 		 * be sent try to transfer them.
1743fcf3ce44SJohn Forte 		 */
1744fcf3ce44SJohn Forte 		mutex_enter(&isp->sess_queue_pending.mutex);
1745fcf3ce44SJohn Forte 		while ((isp->sess_window_open == B_TRUE) &&
1746fcf3ce44SJohn Forte 		    ((icmdp = isp->sess_queue_pending.head) != NULL) &&
1747fcf3ce44SJohn Forte 		    (((icmdp->cmd_type != ISCSI_CMD_TYPE_SCSI) &&
1748fcf3ce44SJohn Forte 		    (ISCSI_CONN_STATE_FULL_FEATURE(icp->conn_state))) ||
1749fcf3ce44SJohn Forte 		    (icp->conn_state == ISCSI_CONN_STATE_LOGGED_IN))) {
1750fcf3ce44SJohn Forte 
1751fcf3ce44SJohn Forte 			/* update command with this connection info */
1752fcf3ce44SJohn Forte 			icmdp->cmd_conn = icp;
1753fcf3ce44SJohn Forte 			/* attempt to send this command */
1754fcf3ce44SJohn Forte 			iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E2, isp);
1755fcf3ce44SJohn Forte 
1756fcf3ce44SJohn Forte 			ASSERT(!mutex_owned(&isp->sess_queue_pending.mutex));
1757fcf3ce44SJohn Forte 			mutex_enter(&isp->sess_queue_pending.mutex);
1758fcf3ce44SJohn Forte 		}
1759fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_queue_pending.mutex);
1760fcf3ce44SJohn Forte 
1761fcf3ce44SJohn Forte 		/*
1762fcf3ce44SJohn Forte 		 * Go to sleep until there is something new
1763fcf3ce44SJohn Forte 		 * to process (awoken via cv_boardcast).
1764fcf3ce44SJohn Forte 		 * Or the timer goes off.
1765fcf3ce44SJohn Forte 		 */
1766fcf3ce44SJohn Forte 		ret = iscsi_thread_wait(thread, tout);
1767fcf3ce44SJohn Forte 	}
1768fcf3ce44SJohn Forte 
1769fcf3ce44SJohn Forte }
1770fcf3ce44SJohn Forte 
1771fcf3ce44SJohn Forte 
1772fcf3ce44SJohn Forte /*
1773fcf3ce44SJohn Forte  * iscsi_tx_cmd - transfers icmdp across wire as iscsi pdu
1774fcf3ce44SJohn Forte  *
1775fcf3ce44SJohn Forte  * Just prior to sending the command to the networking layer the
1776fcf3ce44SJohn Forte  * pending queue lock will be dropped.  At this point only local
1777fcf3ce44SJohn Forte  * resources will be used, not the icmdp.  Holding the queue lock
1778fcf3ce44SJohn Forte  * across the networking call can lead to a hang.  (This is due
1779fcf3ce44SJohn Forte  * to the the target driver and networking layers competing use
1780fcf3ce44SJohn Forte  * of the timeout() resources and the queue lock being held for
1781fcf3ce44SJohn Forte  * both sides.)  Upon the completion of this command the lock
1782fcf3ce44SJohn Forte  * will have been re-acquired.
1783fcf3ce44SJohn Forte  */
1784fcf3ce44SJohn Forte iscsi_status_t
1785fcf3ce44SJohn Forte iscsi_tx_cmd(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
1786fcf3ce44SJohn Forte {
1787fcf3ce44SJohn Forte 	iscsi_status_t	rval = ISCSI_STATUS_INTERNAL_ERROR;
1788fcf3ce44SJohn Forte 
1789fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
1790fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
1791fcf3ce44SJohn Forte 
1792fcf3ce44SJohn Forte 	/* transfer specific command type */
1793fcf3ce44SJohn Forte 	switch (icmdp->cmd_type) {
1794fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_SCSI:
1795fcf3ce44SJohn Forte 		rval = iscsi_tx_scsi(isp, icmdp);
1796fcf3ce44SJohn Forte 		break;
1797fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_NOP:
1798fcf3ce44SJohn Forte 		rval = iscsi_tx_nop(isp, icmdp);
1799fcf3ce44SJohn Forte 		break;
1800fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_ABORT:
1801fcf3ce44SJohn Forte 		rval = iscsi_tx_abort(isp, icmdp);
1802fcf3ce44SJohn Forte 		break;
1803fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_RESET:
1804fcf3ce44SJohn Forte 		rval = iscsi_tx_reset(isp, icmdp);
1805fcf3ce44SJohn Forte 		break;
1806fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_LOGOUT:
1807fcf3ce44SJohn Forte 		rval = iscsi_tx_logout(isp, icmdp);
1808fcf3ce44SJohn Forte 		break;
1809fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_TEXT:
1810fcf3ce44SJohn Forte 		rval = iscsi_tx_text(isp, icmdp);
1811fcf3ce44SJohn Forte 		break;
1812fcf3ce44SJohn Forte 	default:
181330e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi_tx_cmd: invalid cmdtype: %d",
181430e7468fSPeter Dunlap 		    icmdp->cmd_type);
1815fcf3ce44SJohn Forte 		ASSERT(FALSE);
1816fcf3ce44SJohn Forte 	}
1817fcf3ce44SJohn Forte 
1818fcf3ce44SJohn Forte 	ASSERT(!mutex_owned(&isp->sess_queue_pending.mutex));
1819fcf3ce44SJohn Forte 	return (rval);
1820fcf3ce44SJohn Forte }
1821fcf3ce44SJohn Forte 
1822fcf3ce44SJohn Forte /*
1823fcf3ce44SJohn Forte  * a variable length cdb can be up to 16K, but we obviously don't want
1824fcf3ce44SJohn Forte  * to put that on the stack; go with 200 bytes; if we get something
1825fcf3ce44SJohn Forte  * bigger than that we will kmem_alloc a buffer
1826fcf3ce44SJohn Forte  */
1827fcf3ce44SJohn Forte #define	DEF_CDB_LEN	200
1828fcf3ce44SJohn Forte 
1829fcf3ce44SJohn Forte /*
1830fcf3ce44SJohn Forte  * given the size of the cdb, return how many bytes the header takes,
1831fcf3ce44SJohn Forte  * which is the sizeof addl_hdr_t + the CDB size, minus the 16 bytes
1832fcf3ce44SJohn Forte  * stored in the basic header, minus sizeof (ahs_extscb)
1833fcf3ce44SJohn Forte  */
1834fcf3ce44SJohn Forte #define	ADDLHDRSZ(x)		(sizeof (iscsi_addl_hdr_t) + (x) - \
1835fcf3ce44SJohn Forte 					16 - 4)
1836fcf3ce44SJohn Forte 
183730e7468fSPeter Dunlap static void
183830e7468fSPeter Dunlap iscsi_tx_init_hdr(iscsi_sess_t *isp, iscsi_conn_t *icp,
18392b79d384Sbing zhao - Sun Microsystems - Beijing China     iscsi_text_hdr_t *ihp, int opcode, iscsi_cmd_t *icmdp)
1840fcf3ce44SJohn Forte {
184130e7468fSPeter Dunlap 	ihp->opcode		= opcode;
18422b79d384Sbing zhao - Sun Microsystems - Beijing China 	ihp->itt		= icmdp->cmd_itt;
1843fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
18442b79d384Sbing zhao - Sun Microsystems - Beijing China 	icmdp->cmd_sn		= isp->sess_cmdsn;
1845fcf3ce44SJohn Forte 	ihp->cmdsn		= htonl(isp->sess_cmdsn);
1846fcf3ce44SJohn Forte 	isp->sess_cmdsn++;
1847fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
1848fcf3ce44SJohn Forte 	ihp->expstatsn		= htonl(icp->conn_expstatsn);
1849fcf3ce44SJohn Forte 	icp->conn_laststatsn = icp->conn_expstatsn;
185030e7468fSPeter Dunlap }
1851fcf3ce44SJohn Forte 
1852fcf3ce44SJohn Forte 
185330e7468fSPeter Dunlap static void
185430e7468fSPeter Dunlap iscsi_tx_scsi_data(iscsi_cmd_t *icmdp, iscsi_scsi_cmd_hdr_t *ihp,
185530e7468fSPeter Dunlap     iscsi_conn_t *icp, idm_pdu_t *pdu)
185630e7468fSPeter Dunlap {
185730e7468fSPeter Dunlap 	struct buf		*bp		= NULL;
185830e7468fSPeter Dunlap 	size_t			buflen		= 0;
185930e7468fSPeter Dunlap 	uint32_t		first_burst_length = 0;
186030e7468fSPeter Dunlap 	struct scsi_pkt		*pkt;
186130e7468fSPeter Dunlap 
186230e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
1863fcf3ce44SJohn Forte 	bp = icmdp->cmd_un.scsi.bp;
1864fcf3ce44SJohn Forte 	if ((bp != NULL) && bp->b_bcount) {
1865fcf3ce44SJohn Forte 		buflen = bp->b_bcount;
186630e7468fSPeter Dunlap 		first_burst_length =
186730e7468fSPeter Dunlap 		    icp->conn_params.first_burst_length;
1868fcf3ce44SJohn Forte 
1869fcf3ce44SJohn Forte 		if (bp->b_flags & B_READ) {
1870fcf3ce44SJohn Forte 			ihp->flags = ISCSI_FLAG_FINAL;
1871fcf3ce44SJohn Forte 			/*
1872fcf3ce44SJohn Forte 			 * fix problem where OS sends bp (B_READ &
1873fcf3ce44SJohn Forte 			 * b_bcount!=0) for a TUR or START_STOP.
1874fcf3ce44SJohn Forte 			 * (comment came from cisco code.)
1875fcf3ce44SJohn Forte 			 */
1876fcf3ce44SJohn Forte 			if ((pkt->pkt_cdbp[0] != SCMD_TEST_UNIT_READY) &&
1877fcf3ce44SJohn Forte 			    (pkt->pkt_cdbp[0] != SCMD_START_STOP)) {
1878fcf3ce44SJohn Forte 				ihp->flags |= ISCSI_FLAG_CMD_READ;
1879fcf3ce44SJohn Forte 				ihp->data_length = htonl(buflen);
1880fcf3ce44SJohn Forte 			}
1881fcf3ce44SJohn Forte 		} else {
1882fcf3ce44SJohn Forte 			ihp->flags = ISCSI_FLAG_CMD_WRITE;
1883fcf3ce44SJohn Forte 			/*
1884fcf3ce44SJohn Forte 			 * FinalBit on the the iSCSI PDU denotes this
1885fcf3ce44SJohn Forte 			 * is the last PDU in the sequence.
1886fcf3ce44SJohn Forte 			 *
1887fcf3ce44SJohn Forte 			 * initial_r2t = true means R2T is required
1888fcf3ce44SJohn Forte 			 * for additional PDU, so there will be no more
1889fcf3ce44SJohn Forte 			 * unsolicited PDUs following
1890fcf3ce44SJohn Forte 			 */
1891fcf3ce44SJohn Forte 			if (icp->conn_params.initial_r2t) {
1892fcf3ce44SJohn Forte 				ihp->flags |= ISCSI_FLAG_FINAL;
1893fcf3ce44SJohn Forte 			}
1894fcf3ce44SJohn Forte 
1895fcf3ce44SJohn Forte 			/* Check if we should send ImmediateData */
1896fcf3ce44SJohn Forte 			if (icp->conn_params.immediate_data) {
189730e7468fSPeter Dunlap 				pdu->isp_data =
189830e7468fSPeter Dunlap 				    (uint8_t *)icmdp->
189930e7468fSPeter Dunlap 				    cmd_un.scsi.bp->b_un.b_addr;
190030e7468fSPeter Dunlap 
190130e7468fSPeter Dunlap 				pdu->isp_datalen = MIN(MIN(buflen,
1902fcf3ce44SJohn Forte 				    first_burst_length),
1903fcf3ce44SJohn Forte 				    icmdp->cmd_conn->conn_params.
1904fcf3ce44SJohn Forte 				    max_xmit_data_seg_len);
1905fcf3ce44SJohn Forte 
1906fcf3ce44SJohn Forte 				/*
1907fcf3ce44SJohn Forte 				 * if everything fits immediate, or
1908fcf3ce44SJohn Forte 				 * we can send all burst data immediate
1909fcf3ce44SJohn Forte 				 * (not unsol), set F
1910fcf3ce44SJohn Forte 				 */
191130e7468fSPeter Dunlap 				/*
191230e7468fSPeter Dunlap 				 * XXX This doesn't look right -- it's not
191330e7468fSPeter Dunlap 				 * clear how we can handle transmitting
191430e7468fSPeter Dunlap 				 * any unsolicited data.  It looks like
191530e7468fSPeter Dunlap 				 * we only support immediate data.  So what
191630e7468fSPeter Dunlap 				 * happens if we don't set ISCSI_FLAG_FINAL?
191730e7468fSPeter Dunlap 				 *
191830e7468fSPeter Dunlap 				 * Unless there's magic code somewhere that
191930e7468fSPeter Dunlap 				 * is sending the remaining PDU's we should
192030e7468fSPeter Dunlap 				 * simply set ISCSI_FLAG_FINAL and forget
192130e7468fSPeter Dunlap 				 * about sending unsolicited data.  The big
192230e7468fSPeter Dunlap 				 * win is the immediate data anyway for small
192330e7468fSPeter Dunlap 				 * PDU's.
192430e7468fSPeter Dunlap 				 */
192530e7468fSPeter Dunlap 				if ((pdu->isp_datalen == buflen) ||
192630e7468fSPeter Dunlap 				    (pdu->isp_datalen == first_burst_length)) {
1927fcf3ce44SJohn Forte 					ihp->flags |= ISCSI_FLAG_FINAL;
1928fcf3ce44SJohn Forte 				}
1929fcf3ce44SJohn Forte 
193030e7468fSPeter Dunlap 				hton24(ihp->dlength, pdu->isp_datalen);
1931fcf3ce44SJohn Forte 			}
1932fcf3ce44SJohn Forte 			/* total data transfer length */
1933fcf3ce44SJohn Forte 			ihp->data_length = htonl(buflen);
1934fcf3ce44SJohn Forte 		}
1935fcf3ce44SJohn Forte 	} else {
1936fcf3ce44SJohn Forte 		ihp->flags = ISCSI_FLAG_FINAL;
1937fcf3ce44SJohn Forte 	}
193830e7468fSPeter Dunlap 	icmdp->cmd_un.scsi.data_transferred += pdu->isp_datalen;
193930e7468fSPeter Dunlap 	/* XXX How is this different from the code above? */
194030e7468fSPeter Dunlap 	/* will idm send the next data command up to burst length? */
194130e7468fSPeter Dunlap 	/* send the burstlen if we haven't sent immediate data */
194230e7468fSPeter Dunlap 	/* CRM: should idm send difference min(buflen, first_burst) and  imm? */
194330e7468fSPeter Dunlap 	/*    (MIN(first_burst_length, buflen) - imdata > 0) */
194430e7468fSPeter Dunlap 	/* CRM_LATER: change this to generate unsolicited pdu */
194530e7468fSPeter Dunlap 	if ((buflen > 0) &&
194630e7468fSPeter Dunlap 	    ((bp->b_flags & B_READ) == 0) &&
194730e7468fSPeter Dunlap 	    (icp->conn_params.initial_r2t == 0) &&
194830e7468fSPeter Dunlap 	    pdu->isp_datalen == 0) {
194930e7468fSPeter Dunlap 
195030e7468fSPeter Dunlap 		pdu->isp_datalen = MIN(first_burst_length, buflen);
195130e7468fSPeter Dunlap 		if ((pdu->isp_datalen == buflen) ||
195230e7468fSPeter Dunlap 		    (pdu->isp_datalen == first_burst_length)) {
195330e7468fSPeter Dunlap 			ihp->flags |= ISCSI_FLAG_FINAL;
195430e7468fSPeter Dunlap 		}
195530e7468fSPeter Dunlap 		pdu->isp_data = (uint8_t *)icmdp->cmd_un.scsi.bp->b_un.b_addr;
195630e7468fSPeter Dunlap 		hton24(ihp->dlength, pdu->isp_datalen);
195730e7468fSPeter Dunlap 	}
195830e7468fSPeter Dunlap }
195930e7468fSPeter Dunlap 
196030e7468fSPeter Dunlap static void
196130e7468fSPeter Dunlap iscsi_tx_scsi_init_pkt(iscsi_cmd_t *icmdp, iscsi_scsi_cmd_hdr_t *ihp)
196230e7468fSPeter Dunlap {
196330e7468fSPeter Dunlap 	struct scsi_pkt *pkt;
196430e7468fSPeter Dunlap 
196530e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
196630e7468fSPeter Dunlap 	pkt->pkt_state = (STATE_GOT_BUS | STATE_GOT_TARGET);
196730e7468fSPeter Dunlap 	pkt->pkt_reason = CMD_INCOMPLETE;
1968fcf3ce44SJohn Forte 
1969fcf3ce44SJohn Forte 	/* tagged queuing */
1970fcf3ce44SJohn Forte 	if (pkt->pkt_flags & FLAG_HTAG) {
1971fcf3ce44SJohn Forte 		ihp->flags |= ISCSI_ATTR_HEAD_OF_QUEUE;
1972fcf3ce44SJohn Forte 	} else if (pkt->pkt_flags & FLAG_OTAG) {
1973fcf3ce44SJohn Forte 		ihp->flags |= ISCSI_ATTR_ORDERED;
1974fcf3ce44SJohn Forte 	} else if (pkt->pkt_flags & FLAG_STAG) {
1975fcf3ce44SJohn Forte 		ihp->flags |= ISCSI_ATTR_SIMPLE;
1976fcf3ce44SJohn Forte 	} else {
1977fcf3ce44SJohn Forte 		/* ihp->flags |= ISCSI_ATTR_UNTAGGED; */
1978fcf3ce44SJohn Forte 		/* EMPTY */
1979fcf3ce44SJohn Forte 	}
1980fcf3ce44SJohn Forte 
1981fcf3ce44SJohn Forte 	/* iscsi states lun is based on spc.2 */
1982fcf3ce44SJohn Forte 	ISCSI_LUN_BYTE_COPY(ihp->lun, icmdp->cmd_un.scsi.lun);
1983fcf3ce44SJohn Forte 
1984fcf3ce44SJohn Forte 	if (icmdp->cmd_un.scsi.cmdlen <= 16) {
1985fcf3ce44SJohn Forte 		/* copy the SCSI Command Block into the PDU */
1986fcf3ce44SJohn Forte 		bcopy(pkt->pkt_cdbp, ihp->scb,
1987fcf3ce44SJohn Forte 		    icmdp->cmd_un.scsi.cmdlen);
1988fcf3ce44SJohn Forte 	} else {
1989fcf3ce44SJohn Forte 		iscsi_addl_hdr_t *iahp;
1990fcf3ce44SJohn Forte 
1991fcf3ce44SJohn Forte 		iahp = (iscsi_addl_hdr_t *)ihp;
1992fcf3ce44SJohn Forte 
1993fcf3ce44SJohn Forte 		ihp->hlength = (ADDLHDRSZ(icmdp->cmd_un.scsi.cmdlen) -
1994fcf3ce44SJohn Forte 		    sizeof (iscsi_scsi_cmd_hdr_t) + 3) / 4;
1995fcf3ce44SJohn Forte 		iahp->ahs_hlen_hi = 0;
1996fcf3ce44SJohn Forte 		iahp->ahs_hlen_lo = (icmdp->cmd_un.scsi.cmdlen - 15);
1997fcf3ce44SJohn Forte 		iahp->ahs_key = 0x01;
1998fcf3ce44SJohn Forte 		iahp->ahs_resv = 0;
1999fcf3ce44SJohn Forte 		bcopy(pkt->pkt_cdbp, ihp->scb, 16);
200030e7468fSPeter Dunlap 		bcopy(((char *)pkt->pkt_cdbp) + 16, &iahp->ahs_extscb[0],
200130e7468fSPeter Dunlap 		    icmdp->cmd_un.scsi.cmdlen);
2002fcf3ce44SJohn Forte 	}
2003fcf3ce44SJohn Forte 
200430e7468fSPeter Dunlap 	/*
200530e7468fSPeter Dunlap 	 * Update all values before transfering.
200630e7468fSPeter Dunlap 	 * We should never touch the icmdp after
200730e7468fSPeter Dunlap 	 * transfering if there is no more data
200830e7468fSPeter Dunlap 	 * to send.  The only case the idm_pdu_tx()
200930e7468fSPeter Dunlap 	 * will fail is a on a connection disconnect
201030e7468fSPeter Dunlap 	 * in that case the command will be flushed.
201130e7468fSPeter Dunlap 	 */
201230e7468fSPeter Dunlap 	pkt->pkt_state |= STATE_SENT_CMD;
2013fcf3ce44SJohn Forte }
2014fcf3ce44SJohn Forte 
201530e7468fSPeter Dunlap static void
201630e7468fSPeter Dunlap iscsi_tx_scsi_init_task(iscsi_cmd_t *icmdp, iscsi_conn_t *icp,
201730e7468fSPeter Dunlap     iscsi_scsi_cmd_hdr_t *ihp)
2018fcf3ce44SJohn Forte {
201930e7468fSPeter Dunlap 	idm_task_t		*itp;
202030e7468fSPeter Dunlap 	struct buf		*bp		= NULL;
202130e7468fSPeter Dunlap 	uint32_t		data_length;
2022fcf3ce44SJohn Forte 
202330e7468fSPeter Dunlap 	bp = icmdp->cmd_un.scsi.bp;
2024fcf3ce44SJohn Forte 
202530e7468fSPeter Dunlap 	itp = icmdp->cmd_itp;
202630e7468fSPeter Dunlap 	ASSERT(itp != NULL);
202730e7468fSPeter Dunlap 	data_length = ntohl(ihp->data_length);
202830e7468fSPeter Dunlap 	ISCSI_IO_LOG(CE_NOTE,
202930e7468fSPeter Dunlap 	    "DEBUG: iscsi_tx_init_task: task_start: %p idt_tt: %x cmdsn: %x "
203030e7468fSPeter Dunlap 	    "sess_cmdsn: %x cmd: %p "
203130e7468fSPeter Dunlap 	    "cmdtype: %d datalen: %u",
203230e7468fSPeter Dunlap 	    (void *)itp, itp->idt_tt, ihp->cmdsn, icp->conn_sess->sess_cmdsn,
203330e7468fSPeter Dunlap 	    (void *)icmdp, icmdp->cmd_type, data_length);
203430e7468fSPeter Dunlap 	if (data_length > 0) {
203530e7468fSPeter Dunlap 		if (bp->b_flags & B_READ) {
203630e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.ibp_ibuf =
203730e7468fSPeter Dunlap 			    idm_buf_alloc(icp->conn_ic,
203830e7468fSPeter Dunlap 			    bp->b_un.b_addr, bp->b_bcount);
203930e7468fSPeter Dunlap 			if (icmdp->cmd_un.scsi.ibp_ibuf)
204030e7468fSPeter Dunlap 				idm_buf_bind_in(itp,
204130e7468fSPeter Dunlap 				    icmdp->cmd_un.scsi.ibp_ibuf);
204230e7468fSPeter Dunlap 		} else {
204330e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.ibp_obuf =
204430e7468fSPeter Dunlap 			    idm_buf_alloc(icp->conn_ic,
204530e7468fSPeter Dunlap 			    bp->b_un.b_addr, bp->b_bcount);
204630e7468fSPeter Dunlap 			if (icmdp->cmd_un.scsi.ibp_obuf)
204730e7468fSPeter Dunlap 				idm_buf_bind_out(itp,
204830e7468fSPeter Dunlap 				    icmdp->cmd_un.scsi.ibp_obuf);
204930e7468fSPeter Dunlap 		}
205030e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE,
205130e7468fSPeter Dunlap 		    "DEBUG: pdu_tx: task_start(%s): %p ic: %p idt_tt: %x "
205230e7468fSPeter Dunlap 		    "cmdsn: %x sess_cmdsn: %x sess_expcmdsn: %x obuf: %p "
205330e7468fSPeter Dunlap 		    "cmdp: %p cmdtype: %d "
205430e7468fSPeter Dunlap 		    "buflen: %lu " "bpaddr: %p datalen: %u ",
205530e7468fSPeter Dunlap 		    bp->b_flags & B_READ ? "B_READ" : "B_WRITE",
205630e7468fSPeter Dunlap 		    (void *)itp, (void *)icp->conn_ic,
205730e7468fSPeter Dunlap 		    itp->idt_tt, ihp->cmdsn,
205830e7468fSPeter Dunlap 		    icp->conn_sess->sess_cmdsn,
205930e7468fSPeter Dunlap 		    icp->conn_sess->sess_expcmdsn,
206030e7468fSPeter Dunlap 		    (void *)icmdp->cmd_un.scsi.ibp_ibuf,
206130e7468fSPeter Dunlap 		    (void *)icmdp, icmdp->cmd_type, bp->b_bcount,
206230e7468fSPeter Dunlap 		    (void *)bp->b_un.b_addr,
206330e7468fSPeter Dunlap 		    data_length);
206430e7468fSPeter Dunlap 	}
2065fcf3ce44SJohn Forte 
2066fcf3ce44SJohn Forte 	/*
206730e7468fSPeter Dunlap 	 * Task is now active
2068fcf3ce44SJohn Forte 	 */
206930e7468fSPeter Dunlap 	idm_task_start(itp, ISCSI_INI_TASK_TTT);
2070fcf3ce44SJohn Forte }
2071fcf3ce44SJohn Forte 
2072fcf3ce44SJohn Forte /*
207330e7468fSPeter Dunlap  * iscsi_tx_scsi -
207430e7468fSPeter Dunlap  *
2075fcf3ce44SJohn Forte  */
2076fcf3ce44SJohn Forte static iscsi_status_t
207730e7468fSPeter Dunlap iscsi_tx_scsi(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2078fcf3ce44SJohn Forte {
2079fcf3ce44SJohn Forte 	iscsi_status_t		rval		= ISCSI_STATUS_SUCCESS;
208030e7468fSPeter Dunlap 	iscsi_conn_t		*icp		= NULL;
208130e7468fSPeter Dunlap 	struct scsi_pkt		*pkt		= NULL;
208230e7468fSPeter Dunlap 	iscsi_scsi_cmd_hdr_t	*ihp		= NULL;
208330e7468fSPeter Dunlap 	int			cdblen		= 0;
208430e7468fSPeter Dunlap 	idm_pdu_t		*pdu;
208530e7468fSPeter Dunlap 	int			len;
2086fcf3ce44SJohn Forte 
2087fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2088fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2089fcf3ce44SJohn Forte 
209030e7468fSPeter Dunlap 	pdu = kmem_zalloc(sizeof (idm_pdu_t), KM_SLEEP);
209130e7468fSPeter Dunlap 
209230e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
209330e7468fSPeter Dunlap 	ASSERT(pkt != NULL);
209430e7468fSPeter Dunlap 	icp = icmdp->cmd_conn;
209530e7468fSPeter Dunlap 	ASSERT(icp != NULL);
209630e7468fSPeter Dunlap 
209730e7468fSPeter Dunlap 	/* Reset counts in case we are on a retry */
209830e7468fSPeter Dunlap 	icmdp->cmd_un.scsi.data_transferred = 0;
209930e7468fSPeter Dunlap 
210030e7468fSPeter Dunlap 	if (icmdp->cmd_un.scsi.cmdlen > DEF_CDB_LEN) {
210130e7468fSPeter Dunlap 		cdblen = icmdp->cmd_un.scsi.cmdlen;
210230e7468fSPeter Dunlap 		ihp = kmem_zalloc(ADDLHDRSZ(cdblen), KM_SLEEP);
210330e7468fSPeter Dunlap 		len = ADDLHDRSZ(cdblen);
210430e7468fSPeter Dunlap 	} else {
210530e7468fSPeter Dunlap 		/*
210630e7468fSPeter Dunlap 		 * only bzero the basic header; the additional header
210730e7468fSPeter Dunlap 		 * will be set up correctly later, if needed
210830e7468fSPeter Dunlap 		 */
210930e7468fSPeter Dunlap 		ihp = kmem_zalloc(sizeof (iscsi_scsi_cmd_hdr_t), KM_SLEEP);
211030e7468fSPeter Dunlap 		len = sizeof (iscsi_scsi_cmd_hdr_t);
2111fcf3ce44SJohn Forte 	}
2112fcf3ce44SJohn Forte 
211330e7468fSPeter Dunlap 	iscsi_tx_init_hdr(isp, icp, (iscsi_text_hdr_t *)ihp,
21142b79d384Sbing zhao - Sun Microsystems - Beijing China 	    ISCSI_OP_SCSI_CMD, icmdp);
211530e7468fSPeter Dunlap 
211630e7468fSPeter Dunlap 	idm_pdu_init(pdu, icp->conn_ic, (void *)icmdp, &iscsi_tx_done);
211730e7468fSPeter Dunlap 	idm_pdu_init_hdr(pdu, (uint8_t *)ihp, len);
211830e7468fSPeter Dunlap 	pdu->isp_data = NULL;
211930e7468fSPeter Dunlap 	pdu->isp_datalen = 0;
2120fcf3ce44SJohn Forte 
2121fcf3ce44SJohn Forte 	/*
212230e7468fSPeter Dunlap 	 * Sestion 12.11 of the iSCSI specification has a good table
212330e7468fSPeter Dunlap 	 * describing when uncolicited data and/or immediate data
212430e7468fSPeter Dunlap 	 * should be sent.
2125fcf3ce44SJohn Forte 	 */
2126fcf3ce44SJohn Forte 
212730e7468fSPeter Dunlap 	iscsi_tx_scsi_data(icmdp, ihp, icp, pdu);
212830e7468fSPeter Dunlap 
212930e7468fSPeter Dunlap 	iscsi_tx_scsi_init_pkt(icmdp, ihp);
213030e7468fSPeter Dunlap 
213130e7468fSPeter Dunlap 	/* Calls idm_task_start */
213230e7468fSPeter Dunlap 	iscsi_tx_scsi_init_task(icmdp, icp, ihp);
213330e7468fSPeter Dunlap 
2134fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
2135fcf3ce44SJohn Forte 
213630e7468fSPeter Dunlap 	idm_pdu_tx(pdu);
2137fcf3ce44SJohn Forte 
21382b79d384Sbing zhao - Sun Microsystems - Beijing China 	icmdp->cmd_misc_flags |= ISCSI_CMD_MISCFLAG_SENT;
21392b79d384Sbing zhao - Sun Microsystems - Beijing China 
214030e7468fSPeter Dunlap 	return (rval);
214130e7468fSPeter Dunlap }
2142fcf3ce44SJohn Forte 
2143fcf3ce44SJohn Forte 
214430e7468fSPeter Dunlap /* ARGSUSED */
214530e7468fSPeter Dunlap static void
214630e7468fSPeter Dunlap iscsi_tx_done(idm_pdu_t *pdu, idm_status_t status)
214730e7468fSPeter Dunlap {
214830e7468fSPeter Dunlap 	kmem_free((iscsi_hdr_t *)pdu->isp_hdr, pdu->isp_hdrlen);
214930e7468fSPeter Dunlap 	kmem_free(pdu, sizeof (idm_pdu_t));
215030e7468fSPeter Dunlap }
2151fcf3ce44SJohn Forte 
2152fcf3ce44SJohn Forte 
215330e7468fSPeter Dunlap static void
215430e7468fSPeter Dunlap iscsi_tx_pdu(iscsi_conn_t *icp, int opcode, void *hdr, int hdrlen,
215530e7468fSPeter Dunlap     iscsi_cmd_t *icmdp)
215630e7468fSPeter Dunlap {
215730e7468fSPeter Dunlap 	idm_pdu_t	*tx_pdu;
215830e7468fSPeter Dunlap 	iscsi_hdr_t	*ihp = (iscsi_hdr_t *)hdr;
2159fcf3ce44SJohn Forte 
216030e7468fSPeter Dunlap 	tx_pdu = kmem_zalloc(sizeof (idm_pdu_t), KM_SLEEP);
216130e7468fSPeter Dunlap 	ASSERT(tx_pdu != NULL);
2162fcf3ce44SJohn Forte 
216330e7468fSPeter Dunlap 	idm_pdu_init(tx_pdu, icp->conn_ic, icmdp, &iscsi_tx_done);
216430e7468fSPeter Dunlap 	idm_pdu_init_hdr(tx_pdu, hdr, hdrlen);
216530e7468fSPeter Dunlap 	if (opcode == ISCSI_OP_TEXT_CMD) {
216630e7468fSPeter Dunlap 		idm_pdu_init_data(tx_pdu,
216730e7468fSPeter Dunlap 		    (uint8_t *)icmdp->cmd_un.text.buf,
216830e7468fSPeter Dunlap 		    ntoh24(ihp->dlength));
2169fcf3ce44SJohn Forte 	}
2170fcf3ce44SJohn Forte 
217130e7468fSPeter Dunlap 	mutex_exit(&icp->conn_sess->sess_queue_pending.mutex);
217230e7468fSPeter Dunlap 	idm_pdu_tx(tx_pdu);
21732b79d384Sbing zhao - Sun Microsystems - Beijing China 	icmdp->cmd_misc_flags |= ISCSI_CMD_MISCFLAG_SENT;
2174fcf3ce44SJohn Forte }
2175fcf3ce44SJohn Forte 
2176fcf3ce44SJohn Forte 
2177fcf3ce44SJohn Forte /*
2178fcf3ce44SJohn Forte  * iscsi_tx_nop -
2179fcf3ce44SJohn Forte  *
2180fcf3ce44SJohn Forte  */
2181fcf3ce44SJohn Forte static iscsi_status_t
2182fcf3ce44SJohn Forte iscsi_tx_nop(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2183fcf3ce44SJohn Forte {
2184fcf3ce44SJohn Forte 	iscsi_status_t		rval	= ISCSI_STATUS_SUCCESS;
2185fcf3ce44SJohn Forte 	iscsi_conn_t		*icp	= NULL;
218630e7468fSPeter Dunlap 	iscsi_nop_out_hdr_t	*inohp;
2187fcf3ce44SJohn Forte 
2188fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2189fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2190fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2191fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2192fcf3ce44SJohn Forte 
219330e7468fSPeter Dunlap 	inohp = kmem_zalloc(sizeof (iscsi_nop_out_hdr_t), KM_SLEEP);
219430e7468fSPeter Dunlap 	ASSERT(inohp != NULL);
219530e7468fSPeter Dunlap 
219630e7468fSPeter Dunlap 	inohp->opcode	= ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
219730e7468fSPeter Dunlap 	inohp->flags	= ISCSI_FLAG_FINAL;
219830e7468fSPeter Dunlap 	inohp->itt	= icmdp->cmd_itt;
219930e7468fSPeter Dunlap 	inohp->ttt	= icmdp->cmd_ttt;
2200fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
22012b79d384Sbing zhao - Sun Microsystems - Beijing China 	icmdp->cmd_sn	= isp->sess_cmdsn;
220230e7468fSPeter Dunlap 	inohp->cmdsn	= htonl(isp->sess_cmdsn);
2203fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
220430e7468fSPeter Dunlap 	inohp->expstatsn	= htonl(icp->conn_expstatsn);
2205fcf3ce44SJohn Forte 	icp->conn_laststatsn = icp->conn_expstatsn;
220630e7468fSPeter Dunlap 	iscsi_tx_pdu(icp, ISCSI_OP_NOOP_OUT, inohp,
220730e7468fSPeter Dunlap 	    sizeof (iscsi_nop_out_hdr_t), icmdp);
2208fcf3ce44SJohn Forte 	return (rval);
2209fcf3ce44SJohn Forte }
2210fcf3ce44SJohn Forte 
2211fcf3ce44SJohn Forte 
2212fcf3ce44SJohn Forte /*
2213fcf3ce44SJohn Forte  * iscsi_tx_abort -
2214fcf3ce44SJohn Forte  *
2215fcf3ce44SJohn Forte  */
2216fcf3ce44SJohn Forte static iscsi_status_t
2217fcf3ce44SJohn Forte iscsi_tx_abort(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2218fcf3ce44SJohn Forte {
2219fcf3ce44SJohn Forte 	iscsi_status_t			rval	= ISCSI_STATUS_SUCCESS;
2220fcf3ce44SJohn Forte 	iscsi_conn_t			*icp	= NULL;
222130e7468fSPeter Dunlap 	iscsi_scsi_task_mgt_hdr_t	*istmh;
2222fcf3ce44SJohn Forte 
2223fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2224fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2225fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2226fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2227fcf3ce44SJohn Forte 
222830e7468fSPeter Dunlap 	istmh = kmem_zalloc(sizeof (iscsi_scsi_task_mgt_hdr_t), KM_SLEEP);
222930e7468fSPeter Dunlap 	ASSERT(istmh != NULL);
2230fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
22312b79d384Sbing zhao - Sun Microsystems - Beijing China 	icmdp->cmd_sn	= isp->sess_cmdsn;
223230e7468fSPeter Dunlap 	istmh->cmdsn	= htonl(isp->sess_cmdsn);
2233fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
223430e7468fSPeter Dunlap 	istmh->expstatsn = htonl(icp->conn_expstatsn);
2235fcf3ce44SJohn Forte 	icp->conn_laststatsn = icp->conn_expstatsn;
223630e7468fSPeter Dunlap 	istmh->itt	= icmdp->cmd_itt;
223730e7468fSPeter Dunlap 	istmh->opcode	= ISCSI_OP_SCSI_TASK_MGT_MSG | ISCSI_OP_IMMEDIATE;
223830e7468fSPeter Dunlap 	istmh->function	= ISCSI_FLAG_FINAL | ISCSI_TM_FUNC_ABORT_TASK;
223930e7468fSPeter Dunlap 	ISCSI_LUN_BYTE_COPY(istmh->lun,
2240fcf3ce44SJohn Forte 	    icmdp->cmd_un.abort.icmdp->cmd_un.scsi.lun);
224130e7468fSPeter Dunlap 	istmh->rtt	= icmdp->cmd_un.abort.icmdp->cmd_itt;
224230e7468fSPeter Dunlap 	iscsi_tx_pdu(icp, ISCSI_OP_SCSI_TASK_MGT_MSG, istmh,
224330e7468fSPeter Dunlap 	    sizeof (iscsi_scsi_task_mgt_hdr_t), icmdp);
2244fcf3ce44SJohn Forte 
2245fcf3ce44SJohn Forte 	return (rval);
2246fcf3ce44SJohn Forte }
2247fcf3ce44SJohn Forte 
2248fcf3ce44SJohn Forte 
2249fcf3ce44SJohn Forte /*
2250fcf3ce44SJohn Forte  * iscsi_tx_reset -
2251fcf3ce44SJohn Forte  *
2252fcf3ce44SJohn Forte  */
2253fcf3ce44SJohn Forte static iscsi_status_t
2254fcf3ce44SJohn Forte iscsi_tx_reset(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2255fcf3ce44SJohn Forte {
2256fcf3ce44SJohn Forte 	iscsi_status_t			rval	= ISCSI_STATUS_SUCCESS;
2257fcf3ce44SJohn Forte 	iscsi_conn_t			*icp	= NULL;
225830e7468fSPeter Dunlap 	iscsi_scsi_task_mgt_hdr_t	*istmh;
2259fcf3ce44SJohn Forte 
2260fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2261fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2262fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2263fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2264fcf3ce44SJohn Forte 
226530e7468fSPeter Dunlap 	istmh = kmem_zalloc(sizeof (iscsi_scsi_task_mgt_hdr_t), KM_SLEEP);
226630e7468fSPeter Dunlap 	ASSERT(istmh != NULL);
226730e7468fSPeter Dunlap 	istmh->opcode	= ISCSI_OP_SCSI_TASK_MGT_MSG | ISCSI_OP_IMMEDIATE;
2268fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
22692b79d384Sbing zhao - Sun Microsystems - Beijing China 	icmdp->cmd_sn	= isp->sess_cmdsn;
227030e7468fSPeter Dunlap 	istmh->cmdsn	= htonl(isp->sess_cmdsn);
2271fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
227230e7468fSPeter Dunlap 	istmh->expstatsn	= htonl(icp->conn_expstatsn);
227330e7468fSPeter Dunlap 	istmh->itt	= icmdp->cmd_itt;
2274fcf3ce44SJohn Forte 
2275fcf3ce44SJohn Forte 	switch (icmdp->cmd_un.reset.level) {
2276fcf3ce44SJohn Forte 	case RESET_LUN:
227730e7468fSPeter Dunlap 		istmh->function	= ISCSI_FLAG_FINAL |
2278fcf3ce44SJohn Forte 		    ISCSI_TM_FUNC_LOGICAL_UNIT_RESET;
227930e7468fSPeter Dunlap 		ISCSI_LUN_BYTE_COPY(istmh->lun, icmdp->cmd_lun->lun_num);
2280fcf3ce44SJohn Forte 		break;
2281fcf3ce44SJohn Forte 	case RESET_TARGET:
2282fcf3ce44SJohn Forte 	case RESET_BUS:
228330e7468fSPeter Dunlap 		istmh->function	= ISCSI_FLAG_FINAL |
2284fcf3ce44SJohn Forte 		    ISCSI_TM_FUNC_TARGET_WARM_RESET;
2285fcf3ce44SJohn Forte 		break;
2286fcf3ce44SJohn Forte 	default:
2287fcf3ce44SJohn Forte 		/* unsupported / unknown level */
2288fcf3ce44SJohn Forte 		ASSERT(FALSE);
2289fcf3ce44SJohn Forte 		break;
2290fcf3ce44SJohn Forte 	}
2291fcf3ce44SJohn Forte 
229230e7468fSPeter Dunlap 	iscsi_tx_pdu(icp, ISCSI_OP_SCSI_TASK_MGT_MSG, istmh,
229330e7468fSPeter Dunlap 	    sizeof (iscsi_scsi_task_mgt_hdr_t), icmdp);
2294fcf3ce44SJohn Forte 
2295fcf3ce44SJohn Forte 	return (rval);
2296fcf3ce44SJohn Forte }
2297fcf3ce44SJohn Forte 
2298fcf3ce44SJohn Forte 
2299fcf3ce44SJohn Forte /*
2300fcf3ce44SJohn Forte  * iscsi_tx_logout -
2301fcf3ce44SJohn Forte  *
2302fcf3ce44SJohn Forte  */
2303fcf3ce44SJohn Forte static iscsi_status_t
2304fcf3ce44SJohn Forte iscsi_tx_logout(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2305fcf3ce44SJohn Forte {
2306fcf3ce44SJohn Forte 	iscsi_status_t		rval	= ISCSI_STATUS_SUCCESS;
2307fcf3ce44SJohn Forte 	iscsi_conn_t		*icp	= NULL;
230830e7468fSPeter Dunlap 	iscsi_logout_hdr_t	*ilh;
2309fcf3ce44SJohn Forte 
2310fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2311fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2312fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2313fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2314fcf3ce44SJohn Forte 
231530e7468fSPeter Dunlap 	ilh = kmem_zalloc(sizeof (iscsi_logout_hdr_t), KM_SLEEP);
231630e7468fSPeter Dunlap 	ilh->opcode	= ISCSI_OP_LOGOUT_CMD | ISCSI_OP_IMMEDIATE;
231730e7468fSPeter Dunlap 	ilh->flags	= ISCSI_FLAG_FINAL | ISCSI_LOGOUT_REASON_CLOSE_SESSION;
231830e7468fSPeter Dunlap 	ilh->itt		= icmdp->cmd_itt;
231930e7468fSPeter Dunlap 	ilh->cid		= icp->conn_cid;
2320fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
23212b79d384Sbing zhao - Sun Microsystems - Beijing China 	icmdp->cmd_sn	= isp->sess_cmdsn;
232230e7468fSPeter Dunlap 	ilh->cmdsn	= htonl(isp->sess_cmdsn);
2323fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
232430e7468fSPeter Dunlap 	ilh->expstatsn	= htonl(icp->conn_expstatsn);
232530e7468fSPeter Dunlap 	iscsi_tx_pdu(icp, ISCSI_OP_LOGOUT_CMD, ilh,
232630e7468fSPeter Dunlap 	    sizeof (iscsi_logout_hdr_t), icmdp);
2327fcf3ce44SJohn Forte 
2328fcf3ce44SJohn Forte 	return (rval);
2329fcf3ce44SJohn Forte }
2330fcf3ce44SJohn Forte 
2331fcf3ce44SJohn Forte /*
2332fcf3ce44SJohn Forte  * iscsi_tx_text - setup iSCSI text request header and send PDU with
2333fcf3ce44SJohn Forte  * data given in the buffer attached to the command.  For a single
2334fcf3ce44SJohn Forte  * text request, the target may need to send its response in multiple
2335fcf3ce44SJohn Forte  * text response.  In this case, empty text requests are sent after
2336fcf3ce44SJohn Forte  * each received response to notify the target the initiator is ready
2337fcf3ce44SJohn Forte  * for more response.  For the initial request, the data_len field in
2338fcf3ce44SJohn Forte  * the text specific portion of a command is set to the amount of data
2339fcf3ce44SJohn Forte  * the initiator wants to send as part of the request. If additional
2340fcf3ce44SJohn Forte  * empty text requests are required for long responses, the data_len
2341fcf3ce44SJohn Forte  * field is set to 0 by the iscsi_handle_text function.
2342fcf3ce44SJohn Forte  */
2343fcf3ce44SJohn Forte static iscsi_status_t
2344fcf3ce44SJohn Forte iscsi_tx_text(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2345fcf3ce44SJohn Forte {
2346fcf3ce44SJohn Forte 	iscsi_status_t		rval	= ISCSI_STATUS_SUCCESS;
2347fcf3ce44SJohn Forte 	iscsi_conn_t		*icp	= NULL;
234830e7468fSPeter Dunlap 	iscsi_text_hdr_t	*ith;
2349fcf3ce44SJohn Forte 
2350fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2351fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2352fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2353fcf3ce44SJohn Forte 
235430e7468fSPeter Dunlap 	ith = kmem_zalloc(sizeof (iscsi_text_hdr_t), KM_SLEEP);
235530e7468fSPeter Dunlap 	ASSERT(ith != NULL);
235630e7468fSPeter Dunlap 	ith->flags	= ISCSI_FLAG_FINAL;
235730e7468fSPeter Dunlap 	hton24(ith->dlength, icmdp->cmd_un.text.data_len);
235830e7468fSPeter Dunlap 	ith->ttt		= icmdp->cmd_un.text.ttt;
235930e7468fSPeter Dunlap 	iscsi_tx_init_hdr(isp, icp, (iscsi_text_hdr_t *)ith,
23602b79d384Sbing zhao - Sun Microsystems - Beijing China 	    ISCSI_OP_TEXT_CMD, icmdp);
236130e7468fSPeter Dunlap 	bcopy(icmdp->cmd_un.text.lun, ith->rsvd4, sizeof (ith->rsvd4));
2362fcf3ce44SJohn Forte 
236330e7468fSPeter Dunlap 	iscsi_tx_pdu(icp, ISCSI_OP_TEXT_CMD, ith, sizeof (iscsi_text_hdr_t),
236430e7468fSPeter Dunlap 	    icmdp);
2365fcf3ce44SJohn Forte 
2366fcf3ce44SJohn Forte 	return (rval);
2367fcf3ce44SJohn Forte }
2368fcf3ce44SJohn Forte 
2369fcf3ce44SJohn Forte /*
2370fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
2371fcf3ce44SJohn Forte  * | End of protocol send routines					|
2372fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
2373fcf3ce44SJohn Forte  */
2374fcf3ce44SJohn Forte 
2375fcf3ce44SJohn Forte /*
2376fcf3ce44SJohn Forte  * iscsi_handle_abort -
2377fcf3ce44SJohn Forte  *
2378fcf3ce44SJohn Forte  */
2379fcf3ce44SJohn Forte void
2380fcf3ce44SJohn Forte iscsi_handle_abort(void *arg)
2381fcf3ce44SJohn Forte {
2382fcf3ce44SJohn Forte 	iscsi_sess_t	*isp		= NULL;
2383fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp		= (iscsi_cmd_t *)arg;
2384fcf3ce44SJohn Forte 	iscsi_cmd_t	*new_icmdp;
2385fcf3ce44SJohn Forte 	iscsi_conn_t	*icp;
2386fcf3ce44SJohn Forte 
2387fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2388fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2389fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2390fcf3ce44SJohn Forte 	isp = icp->conn_sess;
2391fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2392fcf3ce44SJohn Forte 
2393fcf3ce44SJohn Forte 	/* there should only be one abort */
2394fcf3ce44SJohn Forte 	ASSERT(icmdp->cmd_un.scsi.abort_icmdp == NULL);
2395fcf3ce44SJohn Forte 
2396fcf3ce44SJohn Forte 	new_icmdp = iscsi_cmd_alloc(icp, KM_SLEEP);
23972b79d384Sbing zhao - Sun Microsystems - Beijing China 	new_icmdp->cmd_type		    = ISCSI_CMD_TYPE_ABORT;
23982b79d384Sbing zhao - Sun Microsystems - Beijing China 	new_icmdp->cmd_lun		    = icmdp->cmd_lun;
23992b79d384Sbing zhao - Sun Microsystems - Beijing China 	new_icmdp->cmd_un.abort.icmdp	    = icmdp;
24002b79d384Sbing zhao - Sun Microsystems - Beijing China 	new_icmdp->cmd_conn		    = icmdp->cmd_conn;
24012b79d384Sbing zhao - Sun Microsystems - Beijing China 	icmdp->cmd_un.scsi.abort_icmdp	    = new_icmdp;
2402fcf3ce44SJohn Forte 
2403fcf3ce44SJohn Forte 	/* pending queue mutex is already held by timeout_checks */
2404fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(new_icmdp, ISCSI_CMD_EVENT_E1, isp);
2405fcf3ce44SJohn Forte }
2406fcf3ce44SJohn Forte 
240730e7468fSPeter Dunlap /*
240830e7468fSPeter Dunlap  * Callback from IDM indicating that the task has been suspended or aborted.
240930e7468fSPeter Dunlap  */
241030e7468fSPeter Dunlap void
241130e7468fSPeter Dunlap iscsi_task_aborted(idm_task_t *idt, idm_status_t status)
241230e7468fSPeter Dunlap {
241330e7468fSPeter Dunlap 	iscsi_cmd_t *icmdp = idt->idt_private;
241430e7468fSPeter Dunlap 	iscsi_conn_t *icp = icmdp->cmd_conn;
241530e7468fSPeter Dunlap 	iscsi_sess_t *isp = icp->conn_sess;
241630e7468fSPeter Dunlap 
241730e7468fSPeter Dunlap 	ASSERT(icmdp->cmd_conn != NULL);
241830e7468fSPeter Dunlap 
241930e7468fSPeter Dunlap 	switch (status) {
242030e7468fSPeter Dunlap 	case IDM_STATUS_SUSPENDED:
242130e7468fSPeter Dunlap 		/*
242230e7468fSPeter Dunlap 		 * If the task is suspended, it may be aborted later,
242330e7468fSPeter Dunlap 		 * so we can ignore this notification.
242430e7468fSPeter Dunlap 		 */
242530e7468fSPeter Dunlap 		break;
242630e7468fSPeter Dunlap 
242730e7468fSPeter Dunlap 	case IDM_STATUS_ABORTED:
242830e7468fSPeter Dunlap 		mutex_enter(&icp->conn_queue_active.mutex);
242930e7468fSPeter Dunlap 		iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E9, isp);
243030e7468fSPeter Dunlap 		mutex_exit(&icp->conn_queue_active.mutex);
243130e7468fSPeter Dunlap 		break;
243230e7468fSPeter Dunlap 
243330e7468fSPeter Dunlap 	default:
243430e7468fSPeter Dunlap 		/*
243530e7468fSPeter Dunlap 		 * Unexpected status.
243630e7468fSPeter Dunlap 		 */
243730e7468fSPeter Dunlap 		ASSERT(0);
243830e7468fSPeter Dunlap 	}
243930e7468fSPeter Dunlap 
244030e7468fSPeter Dunlap }
2441fcf3ce44SJohn Forte 
2442fcf3ce44SJohn Forte /*
2443fcf3ce44SJohn Forte  * iscsi_handle_nop -
2444fcf3ce44SJohn Forte  *
2445fcf3ce44SJohn Forte  */
2446fcf3ce44SJohn Forte static void
2447fcf3ce44SJohn Forte iscsi_handle_nop(iscsi_conn_t *icp, uint32_t itt, uint32_t ttt)
2448fcf3ce44SJohn Forte {
2449fcf3ce44SJohn Forte 	iscsi_sess_t	*isp	= NULL;
2450fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp	= NULL;
2451fcf3ce44SJohn Forte 
2452fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2453fcf3ce44SJohn Forte 	isp = icp->conn_sess;
2454fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2455fcf3ce44SJohn Forte 
2456fcf3ce44SJohn Forte 	icmdp = iscsi_cmd_alloc(icp, KM_NOSLEEP);
2457fcf3ce44SJohn Forte 	if (icmdp == NULL) {
2458fcf3ce44SJohn Forte 		return;
2459fcf3ce44SJohn Forte 	}
2460fcf3ce44SJohn Forte 
2461fcf3ce44SJohn Forte 	icmdp->cmd_type		= ISCSI_CMD_TYPE_NOP;
2462fcf3ce44SJohn Forte 	icmdp->cmd_itt		= itt;
2463fcf3ce44SJohn Forte 	icmdp->cmd_ttt		= ttt;
2464fcf3ce44SJohn Forte 	icmdp->cmd_lun		= NULL;
2465fcf3ce44SJohn Forte 	icp->conn_nop_lbolt	= ddi_get_lbolt();
2466fcf3ce44SJohn Forte 
2467fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp);
2468fcf3ce44SJohn Forte }
2469fcf3ce44SJohn Forte 
2470fcf3ce44SJohn Forte /*
24712b79d384Sbing zhao - Sun Microsystems - Beijing China  * iscsi_handle_reset - send reset request to the target
2472fcf3ce44SJohn Forte  *
2473fcf3ce44SJohn Forte  */
2474fcf3ce44SJohn Forte iscsi_status_t
2475fcf3ce44SJohn Forte iscsi_handle_reset(iscsi_sess_t *isp, int level, iscsi_lun_t *ilp)
2476fcf3ce44SJohn Forte {
2477fcf3ce44SJohn Forte 	iscsi_status_t	rval	= ISCSI_STATUS_SUCCESS;
2478fcf3ce44SJohn Forte 	iscsi_conn_t	*icp;
2479fcf3ce44SJohn Forte 	iscsi_cmd_t	icmd;
2480fcf3ce44SJohn Forte 
2481fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2482fcf3ce44SJohn Forte 
24832b79d384Sbing zhao - Sun Microsystems - Beijing China 	if (level == RESET_LUN) {
24842b79d384Sbing zhao - Sun Microsystems - Beijing China 		rw_enter(&isp->sess_lun_list_rwlock, RW_WRITER);
24852b79d384Sbing zhao - Sun Microsystems - Beijing China 		ASSERT(ilp != NULL);
24862b79d384Sbing zhao - Sun Microsystems - Beijing China 		if (ilp->lun_state & ISCSI_LUN_STATE_BUSY) {
24872b79d384Sbing zhao - Sun Microsystems - Beijing China 			rw_exit(&isp->sess_lun_list_rwlock);
24882b79d384Sbing zhao - Sun Microsystems - Beijing China 			return (ISCSI_STATUS_SUCCESS);
24892b79d384Sbing zhao - Sun Microsystems - Beijing China 		}
24902b79d384Sbing zhao - Sun Microsystems - Beijing China 		ilp->lun_state |= ISCSI_LUN_STATE_BUSY;
24912b79d384Sbing zhao - Sun Microsystems - Beijing China 		rw_exit(&isp->sess_lun_list_rwlock);
24922b79d384Sbing zhao - Sun Microsystems - Beijing China 	} else {
24932b79d384Sbing zhao - Sun Microsystems - Beijing China 		mutex_enter(&isp->sess_reset_mutex);
24942b79d384Sbing zhao - Sun Microsystems - Beijing China 		if (isp->sess_reset_in_progress == B_TRUE) {
24952b79d384Sbing zhao - Sun Microsystems - Beijing China 			/*
24962b79d384Sbing zhao - Sun Microsystems - Beijing China 			 * If the reset is in progress, it is unnecessary
24972b79d384Sbing zhao - Sun Microsystems - Beijing China 			 * to send reset to the target redunantly.
24982b79d384Sbing zhao - Sun Microsystems - Beijing China 			 */
24992b79d384Sbing zhao - Sun Microsystems - Beijing China 			mutex_exit(&isp->sess_reset_mutex);
25002b79d384Sbing zhao - Sun Microsystems - Beijing China 			return (ISCSI_STATUS_SUCCESS);
25012b79d384Sbing zhao - Sun Microsystems - Beijing China 		}
25022b79d384Sbing zhao - Sun Microsystems - Beijing China 		isp->sess_reset_in_progress = B_TRUE;
25032b79d384Sbing zhao - Sun Microsystems - Beijing China 		mutex_exit(&isp->sess_reset_mutex);
25042b79d384Sbing zhao - Sun Microsystems - Beijing China 	}
25052b79d384Sbing zhao - Sun Microsystems - Beijing China 
2506fcf3ce44SJohn Forte 	bzero(&icmd, sizeof (iscsi_cmd_t));
2507fcf3ce44SJohn Forte 	icmd.cmd_sig		= ISCSI_SIG_CMD;
2508fcf3ce44SJohn Forte 	icmd.cmd_state		= ISCSI_CMD_STATE_FREE;
2509fcf3ce44SJohn Forte 	icmd.cmd_type		= ISCSI_CMD_TYPE_RESET;
2510fcf3ce44SJohn Forte 	icmd.cmd_lun		= ilp;
2511fcf3ce44SJohn Forte 	icmd.cmd_un.reset.level	= level;
2512fcf3ce44SJohn Forte 	icmd.cmd_result		= ISCSI_STATUS_SUCCESS;
2513fcf3ce44SJohn Forte 	icmd.cmd_completed	= B_FALSE;
25142b79d384Sbing zhao - Sun Microsystems - Beijing China 	icmd.cmd_un.reset.response = SCSI_TCP_TM_RESP_COMPLETE;
25152b79d384Sbing zhao - Sun Microsystems - Beijing China 
2516fcf3ce44SJohn Forte 	mutex_init(&icmd.cmd_mutex, NULL, MUTEX_DRIVER, NULL);
2517fcf3ce44SJohn Forte 	cv_init(&icmd.cmd_completion, NULL, CV_DRIVER, NULL);
2518fcf3ce44SJohn Forte 	/*
2519fcf3ce44SJohn Forte 	 * If we received an IO and we are not in the
2520fcf3ce44SJohn Forte 	 * LOGGED_IN state we are in the process of
2521fcf3ce44SJohn Forte 	 * failing.  Just respond that we are BUSY.
2522fcf3ce44SJohn Forte 	 */
2523fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_state_mutex);
2524fcf3ce44SJohn Forte 	if (!ISCSI_SESS_STATE_FULL_FEATURE(isp->sess_state)) {
2525fcf3ce44SJohn Forte 		/* We aren't connected to the target fake success */
2526fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_state_mutex);
25272b79d384Sbing zhao - Sun Microsystems - Beijing China 
25282b79d384Sbing zhao - Sun Microsystems - Beijing China 		if (level == RESET_LUN) {
25292b79d384Sbing zhao - Sun Microsystems - Beijing China 			rw_enter(&isp->sess_lun_list_rwlock, RW_WRITER);
25302b79d384Sbing zhao - Sun Microsystems - Beijing China 			ilp->lun_state &= ~ISCSI_LUN_STATE_BUSY;
25312b79d384Sbing zhao - Sun Microsystems - Beijing China 			rw_exit(&isp->sess_lun_list_rwlock);
25322b79d384Sbing zhao - Sun Microsystems - Beijing China 		} else {
25332b79d384Sbing zhao - Sun Microsystems - Beijing China 			mutex_enter(&isp->sess_reset_mutex);
25342b79d384Sbing zhao - Sun Microsystems - Beijing China 			isp->sess_reset_in_progress = B_FALSE;
25352b79d384Sbing zhao - Sun Microsystems - Beijing China 			mutex_exit(&isp->sess_reset_mutex);
25362b79d384Sbing zhao - Sun Microsystems - Beijing China 		}
25372b79d384Sbing zhao - Sun Microsystems - Beijing China 
2538fcf3ce44SJohn Forte 		return (ISCSI_STATUS_SUCCESS);
2539fcf3ce44SJohn Forte 	}
2540fcf3ce44SJohn Forte 
2541fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
2542fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(&icmd, ISCSI_CMD_EVENT_E1, isp);
2543fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
2544fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_state_mutex);
2545fcf3ce44SJohn Forte 
2546fcf3ce44SJohn Forte 	/* stall until completed */
2547fcf3ce44SJohn Forte 	mutex_enter(&icmd.cmd_mutex);
2548fcf3ce44SJohn Forte 	while (icmd.cmd_completed == B_FALSE) {
2549fcf3ce44SJohn Forte 		cv_wait(&icmd.cmd_completion, &icmd.cmd_mutex);
2550fcf3ce44SJohn Forte 	}
2551fcf3ce44SJohn Forte 	mutex_exit(&icmd.cmd_mutex);
2552fcf3ce44SJohn Forte 
2553fcf3ce44SJohn Forte 	/* copy rval */
2554fcf3ce44SJohn Forte 	rval = icmd.cmd_result;
2555fcf3ce44SJohn Forte 
2556fcf3ce44SJohn Forte 	if (rval == ISCSI_STATUS_SUCCESS) {
2557fcf3ce44SJohn Forte 		/*
2558fcf3ce44SJohn Forte 		 * Reset was successful.  We need to flush
2559fcf3ce44SJohn Forte 		 * all active IOs.
2560fcf3ce44SJohn Forte 		 */
2561fcf3ce44SJohn Forte 		rw_enter(&isp->sess_conn_list_rwlock, RW_READER);
2562fcf3ce44SJohn Forte 		icp = isp->sess_conn_list;
2563fcf3ce44SJohn Forte 		while (icp != NULL) {
2564fcf3ce44SJohn Forte 			iscsi_cmd_t *t_icmdp = NULL;
25652b79d384Sbing zhao - Sun Microsystems - Beijing China 			iscsi_cmd_t *next_icmdp = NULL;
2566fcf3ce44SJohn Forte 
2567fcf3ce44SJohn Forte 			mutex_enter(&icp->conn_queue_active.mutex);
2568fcf3ce44SJohn Forte 			t_icmdp = icp->conn_queue_active.head;
2569fcf3ce44SJohn Forte 			while (t_icmdp != NULL) {
25702b79d384Sbing zhao - Sun Microsystems - Beijing China 				next_icmdp = t_icmdp->cmd_next;
25712b79d384Sbing zhao - Sun Microsystems - Beijing China 				mutex_enter(&t_icmdp->cmd_mutex);
25722b79d384Sbing zhao - Sun Microsystems - Beijing China 				if (!(t_icmdp->cmd_misc_flags &
25732b79d384Sbing zhao - Sun Microsystems - Beijing China 				    ISCSI_CMD_MISCFLAG_SENT)) {
25742b79d384Sbing zhao - Sun Microsystems - Beijing China 					/*
25752b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * Although this command is in the
25762b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * active queue, it has not been sent.
25772b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * Skip it.
25782b79d384Sbing zhao - Sun Microsystems - Beijing China 					 */
25792b79d384Sbing zhao - Sun Microsystems - Beijing China 					mutex_exit(&t_icmdp->cmd_mutex);
25802b79d384Sbing zhao - Sun Microsystems - Beijing China 					t_icmdp = next_icmdp;
25812b79d384Sbing zhao - Sun Microsystems - Beijing China 					continue;
25822b79d384Sbing zhao - Sun Microsystems - Beijing China 				}
25832b79d384Sbing zhao - Sun Microsystems - Beijing China 				if (level == RESET_LUN) {
25842b79d384Sbing zhao - Sun Microsystems - Beijing China 					if (icmd.cmd_lun == NULL ||
25852b79d384Sbing zhao - Sun Microsystems - Beijing China 					    t_icmdp->cmd_lun == NULL ||
25862b79d384Sbing zhao - Sun Microsystems - Beijing China 					    (icmd.cmd_lun->lun_num !=
25872b79d384Sbing zhao - Sun Microsystems - Beijing China 					    t_icmdp->cmd_lun->lun_num)) {
25882b79d384Sbing zhao - Sun Microsystems - Beijing China 						mutex_exit(&t_icmdp->cmd_mutex);
25892b79d384Sbing zhao - Sun Microsystems - Beijing China 						t_icmdp = next_icmdp;
25902b79d384Sbing zhao - Sun Microsystems - Beijing China 						continue;
25912b79d384Sbing zhao - Sun Microsystems - Beijing China 					}
25922b79d384Sbing zhao - Sun Microsystems - Beijing China 				}
25932b79d384Sbing zhao - Sun Microsystems - Beijing China 
25942b79d384Sbing zhao - Sun Microsystems - Beijing China 				if (icmd.cmd_sn == t_icmdp->cmd_sn) {
25952b79d384Sbing zhao - Sun Microsystems - Beijing China 					/*
25962b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * This command may be replied with
25972b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * UA sense key later. So currently
25982b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * it is not a suitable time to flush
25992b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * it. Mark its flag with FLUSH. There
26002b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * is no harm to keep it for a while.
26012b79d384Sbing zhao - Sun Microsystems - Beijing China 					 */
26022b79d384Sbing zhao - Sun Microsystems - Beijing China 					t_icmdp->cmd_misc_flags |=
26032b79d384Sbing zhao - Sun Microsystems - Beijing China 					    ISCSI_CMD_MISCFLAG_FLUSH;
26042b79d384Sbing zhao - Sun Microsystems - Beijing China 					if (t_icmdp->cmd_type ==
26052b79d384Sbing zhao - Sun Microsystems - Beijing China 					    ISCSI_CMD_TYPE_SCSI) {
26062b79d384Sbing zhao - Sun Microsystems - Beijing China 						t_icmdp->cmd_un.scsi.pkt_stat |=
26072b79d384Sbing zhao - Sun Microsystems - Beijing China 						    STAT_BUS_RESET;
26082b79d384Sbing zhao - Sun Microsystems - Beijing China 					}
26092b79d384Sbing zhao - Sun Microsystems - Beijing China 					mutex_exit(&t_icmdp->cmd_mutex);
26102b79d384Sbing zhao - Sun Microsystems - Beijing China 				} else if ((icmd.cmd_sn > t_icmdp->cmd_sn) ||
26112b79d384Sbing zhao - Sun Microsystems - Beijing China 				    ((t_icmdp->cmd_sn - icmd.cmd_sn) >
26122b79d384Sbing zhao - Sun Microsystems - Beijing China 				    ISCSI_CMD_SN_WRAP)) {
26132b79d384Sbing zhao - Sun Microsystems - Beijing China 					/*
26142b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * This reset request must act on all
26152b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * the commnds from the same session
26162b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * having a CmdSN lower than the task
26172b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * mangement CmdSN. So flush these
26182b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * commands here.
26192b79d384Sbing zhao - Sun Microsystems - Beijing China 					 */
26202b79d384Sbing zhao - Sun Microsystems - Beijing China 					if (t_icmdp->cmd_type ==
26212b79d384Sbing zhao - Sun Microsystems - Beijing China 					    ISCSI_CMD_TYPE_SCSI) {
26222b79d384Sbing zhao - Sun Microsystems - Beijing China 						t_icmdp->cmd_un.scsi.pkt_stat |=
26232b79d384Sbing zhao - Sun Microsystems - Beijing China 						    STAT_BUS_RESET;
26242b79d384Sbing zhao - Sun Microsystems - Beijing China 					}
26252b79d384Sbing zhao - Sun Microsystems - Beijing China 					mutex_exit(&t_icmdp->cmd_mutex);
26262b79d384Sbing zhao - Sun Microsystems - Beijing China 					iscsi_cmd_state_machine(t_icmdp,
26272b79d384Sbing zhao - Sun Microsystems - Beijing China 					    ISCSI_CMD_EVENT_E7, isp);
26282b79d384Sbing zhao - Sun Microsystems - Beijing China 				} else {
26292b79d384Sbing zhao - Sun Microsystems - Beijing China 					mutex_exit(&t_icmdp->cmd_mutex);
26302b79d384Sbing zhao - Sun Microsystems - Beijing China 				}
26312b79d384Sbing zhao - Sun Microsystems - Beijing China 
26322b79d384Sbing zhao - Sun Microsystems - Beijing China 				t_icmdp = next_icmdp;
2633fcf3ce44SJohn Forte 			}
2634fcf3ce44SJohn Forte 
2635fcf3ce44SJohn Forte 			mutex_exit(&icp->conn_queue_active.mutex);
2636fcf3ce44SJohn Forte 			icp = icp->conn_next;
2637fcf3ce44SJohn Forte 		}
2638fcf3ce44SJohn Forte 		rw_exit(&isp->sess_conn_list_rwlock);
2639fcf3ce44SJohn Forte 	}
2640fcf3ce44SJohn Forte 
2641fcf3ce44SJohn Forte 	/* clean up */
2642fcf3ce44SJohn Forte 	cv_destroy(&icmd.cmd_completion);
2643fcf3ce44SJohn Forte 	mutex_destroy(&icmd.cmd_mutex);
2644fcf3ce44SJohn Forte 
26452b79d384Sbing zhao - Sun Microsystems - Beijing China 	if (level == RESET_LUN) {
26462b79d384Sbing zhao - Sun Microsystems - Beijing China 		rw_enter(&isp->sess_lun_list_rwlock, RW_WRITER);
26472b79d384Sbing zhao - Sun Microsystems - Beijing China 		ilp->lun_state &= ~ISCSI_LUN_STATE_BUSY;
26482b79d384Sbing zhao - Sun Microsystems - Beijing China 		rw_exit(&isp->sess_lun_list_rwlock);
26492b79d384Sbing zhao - Sun Microsystems - Beijing China 	} else {
26502b79d384Sbing zhao - Sun Microsystems - Beijing China 		mutex_enter(&isp->sess_reset_mutex);
26512b79d384Sbing zhao - Sun Microsystems - Beijing China 		isp->sess_reset_in_progress = B_FALSE;
26522b79d384Sbing zhao - Sun Microsystems - Beijing China 		mutex_exit(&isp->sess_reset_mutex);
26532b79d384Sbing zhao - Sun Microsystems - Beijing China 	}
26542b79d384Sbing zhao - Sun Microsystems - Beijing China 
2655fcf3ce44SJohn Forte 	return (rval);
2656fcf3ce44SJohn Forte }
2657fcf3ce44SJohn Forte 
265830e7468fSPeter Dunlap /*
265930e7468fSPeter Dunlap  * iscsi_lgoout_start - task handler for deferred logout
266030e7468fSPeter Dunlap  */
266130e7468fSPeter Dunlap static void
266230e7468fSPeter Dunlap iscsi_logout_start(void *arg)
266330e7468fSPeter Dunlap {
266430e7468fSPeter Dunlap 	iscsi_task_t		*itp = (iscsi_task_t *)arg;
266530e7468fSPeter Dunlap 	iscsi_conn_t		*icp;
266630e7468fSPeter Dunlap 
266730e7468fSPeter Dunlap 	icp = (iscsi_conn_t *)itp->t_arg;
266830e7468fSPeter Dunlap 
266930e7468fSPeter Dunlap 	mutex_enter(&icp->conn_state_mutex);
267030e7468fSPeter Dunlap 	(void) iscsi_handle_logout(icp);
267130e7468fSPeter Dunlap 	mutex_exit(&icp->conn_state_mutex);
267230e7468fSPeter Dunlap }
2673fcf3ce44SJohn Forte 
2674fcf3ce44SJohn Forte /*
2675fcf3ce44SJohn Forte  * iscsi_handle_logout - This function will issue a logout for
2676fcf3ce44SJohn Forte  * the session from a specific connection.
2677fcf3ce44SJohn Forte  */
2678fcf3ce44SJohn Forte iscsi_status_t
2679fcf3ce44SJohn Forte iscsi_handle_logout(iscsi_conn_t *icp)
2680fcf3ce44SJohn Forte {
2681fcf3ce44SJohn Forte 	iscsi_sess_t	*isp;
268230e7468fSPeter Dunlap 	idm_conn_t	*ic;
2683fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp;
2684fcf3ce44SJohn Forte 	int		rval;
2685fcf3ce44SJohn Forte 
2686fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2687fcf3ce44SJohn Forte 	isp = icp->conn_sess;
268830e7468fSPeter Dunlap 	ic = icp->conn_ic;
2689fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2690fcf3ce44SJohn Forte 	ASSERT(isp->sess_hba != NULL);
269130e7468fSPeter Dunlap 	ASSERT(mutex_owned(&icp->conn_state_mutex));
269230e7468fSPeter Dunlap 
269330e7468fSPeter Dunlap 	/*
269430e7468fSPeter Dunlap 	 * We may want to explicitly disconnect if something goes wrong so
269530e7468fSPeter Dunlap 	 * grab a hold to ensure that the IDM connection context can't
269630e7468fSPeter Dunlap 	 * disappear.
269730e7468fSPeter Dunlap 	 */
269830e7468fSPeter Dunlap 	idm_conn_hold(ic);
2699fcf3ce44SJohn Forte 
2700fcf3ce44SJohn Forte 	icmdp = iscsi_cmd_alloc(icp, KM_SLEEP);
2701fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2702fcf3ce44SJohn Forte 	icmdp->cmd_type		= ISCSI_CMD_TYPE_LOGOUT;
2703fcf3ce44SJohn Forte 	icmdp->cmd_result	= ISCSI_STATUS_SUCCESS;
2704fcf3ce44SJohn Forte 	icmdp->cmd_completed	= B_FALSE;
2705fcf3ce44SJohn Forte 
2706fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
2707fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp);
2708fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
2709fcf3ce44SJohn Forte 
2710fcf3ce44SJohn Forte 	/*
2711fcf3ce44SJohn Forte 	 * release connection state mutex to avoid a deadlock.  This
2712fcf3ce44SJohn Forte 	 * function is called from within the connection state
2713fcf3ce44SJohn Forte 	 * machine with the lock held.  When the logout response is
2714fcf3ce44SJohn Forte 	 * received another call to the connection state machine
2715fcf3ce44SJohn Forte 	 * occurs which causes the deadlock
2716fcf3ce44SJohn Forte 	 */
2717fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_state_mutex);
2718fcf3ce44SJohn Forte 
2719fcf3ce44SJohn Forte 	/* stall until completed */
2720fcf3ce44SJohn Forte 	mutex_enter(&icmdp->cmd_mutex);
2721fcf3ce44SJohn Forte 	while (icmdp->cmd_completed == B_FALSE) {
2722fcf3ce44SJohn Forte 		cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex);
2723fcf3ce44SJohn Forte 	}
2724fcf3ce44SJohn Forte 	mutex_exit(&icmdp->cmd_mutex);
2725fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_state_mutex);
2726fcf3ce44SJohn Forte 
2727fcf3ce44SJohn Forte 	/* copy rval */
2728fcf3ce44SJohn Forte 	rval = icmdp->cmd_result;
2729fcf3ce44SJohn Forte 
2730fcf3ce44SJohn Forte 	/* clean up */
2731fcf3ce44SJohn Forte 	iscsi_cmd_free(icmdp);
2732fcf3ce44SJohn Forte 
273330e7468fSPeter Dunlap 	if (rval != 0) {
273430e7468fSPeter Dunlap 		/* If the logout failed then drop the connection */
273530e7468fSPeter Dunlap 		idm_ini_conn_disconnect(icp->conn_ic);
273630e7468fSPeter Dunlap 	}
273730e7468fSPeter Dunlap 
273830e7468fSPeter Dunlap 	/* stall until connection settles */
273930e7468fSPeter Dunlap 	while ((icp->conn_state != ISCSI_CONN_STATE_FREE) &&
274030e7468fSPeter Dunlap 	    (icp->conn_state != ISCSI_CONN_STATE_FAILED) &&
274130e7468fSPeter Dunlap 	    (icp->conn_state != ISCSI_CONN_STATE_POLLING)) {
274230e7468fSPeter Dunlap 		/* wait for transition */
274330e7468fSPeter Dunlap 		cv_wait(&icp->conn_state_change, &icp->conn_state_mutex);
274430e7468fSPeter Dunlap 	}
274530e7468fSPeter Dunlap 
274630e7468fSPeter Dunlap 	idm_conn_rele(ic);
274730e7468fSPeter Dunlap 
274830e7468fSPeter Dunlap 	/*
274930e7468fSPeter Dunlap 	 * Return value reflects whether the logout command completed --
275030e7468fSPeter Dunlap 	 * regardless of the return value the connection is closed and
275130e7468fSPeter Dunlap 	 * ready for reconnection.
275230e7468fSPeter Dunlap 	 */
2753fcf3ce44SJohn Forte 	return (rval);
2754fcf3ce44SJohn Forte }
2755fcf3ce44SJohn Forte 
275630e7468fSPeter Dunlap 
2757fcf3ce44SJohn Forte /*
2758fcf3ce44SJohn Forte  * iscsi_handle_text - main control function for iSCSI text requests.  This
2759fcf3ce44SJohn Forte  * function handles allocating the command, sending initial text request, and
2760fcf3ce44SJohn Forte  * handling long response sequence.
2761fcf3ce44SJohn Forte  * If a data overflow condition occurs, iscsi_handle_text continues to
2762fcf3ce44SJohn Forte  * receive responses until the all data has been recieved.  This allows
2763fcf3ce44SJohn Forte  * the full data length to be returned to the caller.
2764fcf3ce44SJohn Forte  */
2765fcf3ce44SJohn Forte iscsi_status_t
2766fcf3ce44SJohn Forte iscsi_handle_text(iscsi_conn_t *icp, char *buf, uint32_t buf_len,
2767fcf3ce44SJohn Forte     uint32_t data_len, uint32_t *rx_data_len)
2768fcf3ce44SJohn Forte {
2769fcf3ce44SJohn Forte 	iscsi_sess_t	*isp;
2770fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp;
2771fcf3ce44SJohn Forte 	iscsi_status_t	rval	= ISCSI_STATUS_SUCCESS;
2772fcf3ce44SJohn Forte 
2773fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2774fcf3ce44SJohn Forte 	ASSERT(buf != NULL);
2775fcf3ce44SJohn Forte 	ASSERT(rx_data_len != NULL);
2776fcf3ce44SJohn Forte 
2777fcf3ce44SJohn Forte 	isp = icp->conn_sess;
2778fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2779fcf3ce44SJohn Forte 
2780fcf3ce44SJohn Forte 	/*
2781fcf3ce44SJohn Forte 	 * Ensure data for text request command is not greater
2782fcf3ce44SJohn Forte 	 * than the negotiated maximum receive data seqment length.
2783fcf3ce44SJohn Forte 	 *
2784fcf3ce44SJohn Forte 	 * Although iSCSI allows for long text requests (multiple
2785fcf3ce44SJohn Forte 	 * pdus), this function places a restriction on text
2786fcf3ce44SJohn Forte 	 * requests to ensure it is handled by a single PDU.
2787fcf3ce44SJohn Forte 	 */
2788fcf3ce44SJohn Forte 	if (data_len > icp->conn_params.max_xmit_data_seg_len) {
2789fcf3ce44SJohn Forte 		return (ISCSI_STATUS_CMD_FAILED);
2790fcf3ce44SJohn Forte 	}
2791fcf3ce44SJohn Forte 
2792fcf3ce44SJohn Forte 	icmdp = iscsi_cmd_alloc(icp, KM_SLEEP);
2793fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2794fcf3ce44SJohn Forte 
2795fcf3ce44SJohn Forte 	icmdp->cmd_type		= ISCSI_CMD_TYPE_TEXT;
2796fcf3ce44SJohn Forte 	icmdp->cmd_result	= ISCSI_STATUS_SUCCESS;
2797d233de7eSJack Meng 	icmdp->cmd_misc_flags	&= ~ISCSI_CMD_MISCFLAG_FREE;
2798fcf3ce44SJohn Forte 	icmdp->cmd_completed	= B_FALSE;
2799fcf3ce44SJohn Forte 
2800fcf3ce44SJohn Forte 	icmdp->cmd_un.text.buf		= buf;
2801fcf3ce44SJohn Forte 	icmdp->cmd_un.text.buf_len	= buf_len;
2802fcf3ce44SJohn Forte 	icmdp->cmd_un.text.offset	= 0;
2803fcf3ce44SJohn Forte 	icmdp->cmd_un.text.data_len	= data_len;
2804fcf3ce44SJohn Forte 	icmdp->cmd_un.text.total_rx_len	= 0;
2805fcf3ce44SJohn Forte 	icmdp->cmd_un.text.ttt		= ISCSI_RSVD_TASK_TAG;
2806fcf3ce44SJohn Forte 	icmdp->cmd_un.text.stage	= ISCSI_CMD_TEXT_INITIAL_REQ;
2807fcf3ce44SJohn Forte 
2808fcf3ce44SJohn Forte long_text_response:
2809fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_state_mutex);
2810fcf3ce44SJohn Forte 	if (!ISCSI_SESS_STATE_FULL_FEATURE(isp->sess_state)) {
2811fcf3ce44SJohn Forte 		iscsi_cmd_free(icmdp);
2812fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_state_mutex);
2813fcf3ce44SJohn Forte 		return (ISCSI_STATUS_NO_CONN_LOGGED_IN);
2814fcf3ce44SJohn Forte 	}
2815fcf3ce44SJohn Forte 
2816fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
2817fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp);
2818fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
2819fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_state_mutex);
2820fcf3ce44SJohn Forte 
2821fcf3ce44SJohn Forte 	/* stall until completed */
2822fcf3ce44SJohn Forte 	mutex_enter(&icmdp->cmd_mutex);
2823fcf3ce44SJohn Forte 	while (icmdp->cmd_completed == B_FALSE) {
2824fcf3ce44SJohn Forte 		cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex);
2825fcf3ce44SJohn Forte 	}
2826fcf3ce44SJohn Forte 	mutex_exit(&icmdp->cmd_mutex);
2827fcf3ce44SJohn Forte 
2828fcf3ce44SJohn Forte 	/*
2829fcf3ce44SJohn Forte 	 * check if error occured.  If data overflow occured, continue on
2830fcf3ce44SJohn Forte 	 * to ensure we get all data so that the full data length can be
2831fcf3ce44SJohn Forte 	 * returned to the user
2832fcf3ce44SJohn Forte 	 */
2833fcf3ce44SJohn Forte 	if ((icmdp->cmd_result != ISCSI_STATUS_SUCCESS) &&
2834fcf3ce44SJohn Forte 	    (icmdp->cmd_result != ISCSI_STATUS_DATA_OVERFLOW)) {
2835fcf3ce44SJohn Forte 		cmn_err(CE_NOTE, "iscsi: SendTarget discovery failed (%d)",
2836fcf3ce44SJohn Forte 		    icmdp->cmd_result);
2837fcf3ce44SJohn Forte 		rval = icmdp->cmd_result;
2838fcf3ce44SJohn Forte 		iscsi_cmd_free(icmdp);
2839fcf3ce44SJohn Forte 		return (rval);
2840fcf3ce44SJohn Forte 	}
2841fcf3ce44SJohn Forte 
2842fcf3ce44SJohn Forte 	/* check if this was a partial text PDU  */
2843fcf3ce44SJohn Forte 	if (icmdp->cmd_un.text.stage != ISCSI_CMD_TEXT_FINAL_RSP) {
2844fcf3ce44SJohn Forte 		/*
2845fcf3ce44SJohn Forte 		 * If a paritial text rexponse received, send an empty
2846fcf3ce44SJohn Forte 		 * text request.  This follows the behaviour specified
2847fcf3ce44SJohn Forte 		 * in RFC3720 regarding long text responses.
2848fcf3ce44SJohn Forte 		 */
2849d233de7eSJack Meng 		icmdp->cmd_misc_flags		&= ~ISCSI_CMD_MISCFLAG_FREE;
2850fcf3ce44SJohn Forte 		icmdp->cmd_completed		= B_FALSE;
2851fcf3ce44SJohn Forte 		icmdp->cmd_un.text.data_len	= 0;
2852fcf3ce44SJohn Forte 		icmdp->cmd_un.text.stage	= ISCSI_CMD_TEXT_CONTINUATION;
2853fcf3ce44SJohn Forte 		goto long_text_response;
2854fcf3ce44SJohn Forte 	}
2855fcf3ce44SJohn Forte 
2856fcf3ce44SJohn Forte 	/*
2857fcf3ce44SJohn Forte 	 * set total received data length.  If data overflow this would be
2858fcf3ce44SJohn Forte 	 * amount of data that would have been received if buffer large
2859fcf3ce44SJohn Forte 	 * enough.
2860fcf3ce44SJohn Forte 	 */
2861fcf3ce44SJohn Forte 	*rx_data_len = icmdp->cmd_un.text.total_rx_len;
2862fcf3ce44SJohn Forte 
2863fcf3ce44SJohn Forte 	/* copy rval */
2864fcf3ce44SJohn Forte 	rval = icmdp->cmd_result;
2865fcf3ce44SJohn Forte 
2866fcf3ce44SJohn Forte 	/* clean up  */
2867fcf3ce44SJohn Forte 	iscsi_cmd_free(icmdp);
2868fcf3ce44SJohn Forte 
2869fcf3ce44SJohn Forte 	return (rval);
2870fcf3ce44SJohn Forte }
2871fcf3ce44SJohn Forte 
2872fcf3ce44SJohn Forte /*
2873fcf3ce44SJohn Forte  * iscsi_handle_passthru - This function is used to send a uscsi_cmd
2874fcf3ce44SJohn Forte  * to a specific target lun.  This routine is used for internal purposes
2875fcf3ce44SJohn Forte  * during enumeration and via the ISCSI_USCSICMD IOCTL.  We restrict
2876fcf3ce44SJohn Forte  * the CDBs that can be issued to a target/lun to INQUIRY, REPORT_LUNS,
2877fcf3ce44SJohn Forte  * and READ_CAPACITY for security purposes.
2878fcf3ce44SJohn Forte  *
2879fcf3ce44SJohn Forte  * The logic here is broken into three phases.
2880fcf3ce44SJohn Forte  * 1) Allocate and initialize a pkt/icmdp
2881fcf3ce44SJohn Forte  * 2) Send the pkt/icmdp
2882fcf3ce44SJohn Forte  * 3) cv_wait for completion
2883fcf3ce44SJohn Forte  */
2884fcf3ce44SJohn Forte iscsi_status_t
2885fcf3ce44SJohn Forte iscsi_handle_passthru(iscsi_sess_t *isp, uint16_t lun, struct uscsi_cmd *ucmdp)
2886fcf3ce44SJohn Forte {
2887fcf3ce44SJohn Forte 	iscsi_status_t		rval		= ISCSI_STATUS_SUCCESS;
2888fcf3ce44SJohn Forte 	iscsi_cmd_t		*icmdp		= NULL;
2889fcf3ce44SJohn Forte 	struct scsi_pkt		*pkt		= NULL;
2890fcf3ce44SJohn Forte 	struct buf		*bp		= NULL;
2891fcf3ce44SJohn Forte 	struct scsi_arq_status  *arqstat	= NULL;
2892fcf3ce44SJohn Forte 	int			rqlen		= SENSE_LENGTH;
2893fcf3ce44SJohn Forte 
2894fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2895fcf3ce44SJohn Forte 	ASSERT(ucmdp != NULL);
2896fcf3ce44SJohn Forte 
2897fcf3ce44SJohn Forte 	/*
2898fcf3ce44SJohn Forte 	 * If the caller didn't provide a sense buffer we need
2899fcf3ce44SJohn Forte 	 * to allocation one to get the scsi status.
2900fcf3ce44SJohn Forte 	 */
2901fcf3ce44SJohn Forte 	if (ucmdp->uscsi_rqlen > SENSE_LENGTH) {
2902fcf3ce44SJohn Forte 		rqlen = ucmdp->uscsi_rqlen;
2903fcf3ce44SJohn Forte 	}
2904fcf3ce44SJohn Forte 
2905fcf3ce44SJohn Forte 	/*
2906fcf3ce44SJohn Forte 	 * Step 1. Setup structs - KM_SLEEP will always succeed
2907fcf3ce44SJohn Forte 	 */
2908fcf3ce44SJohn Forte 	bp = kmem_zalloc(sizeof (struct buf), KM_SLEEP);
2909fcf3ce44SJohn Forte 	ASSERT(bp != NULL);
2910fcf3ce44SJohn Forte 	pkt = kmem_zalloc(sizeof (struct scsi_pkt), KM_SLEEP);
2911fcf3ce44SJohn Forte 	ASSERT(pkt != NULL);
2912fcf3ce44SJohn Forte 	icmdp = iscsi_cmd_alloc(NULL, KM_SLEEP);
2913fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2914fcf3ce44SJohn Forte 
2915fcf3ce44SJohn Forte 	/* setup bp structure */
2916fcf3ce44SJohn Forte 	bp->b_flags		= B_READ;
2917fcf3ce44SJohn Forte 	bp->b_bcount		= ucmdp->uscsi_buflen;
2918fcf3ce44SJohn Forte 	bp->b_un.b_addr		= ucmdp->uscsi_bufaddr;
2919fcf3ce44SJohn Forte 
2920fcf3ce44SJohn Forte 	/* setup scsi_pkt structure */
2921fcf3ce44SJohn Forte 	pkt->pkt_ha_private	= icmdp;
2922fcf3ce44SJohn Forte 	pkt->pkt_scbp		= kmem_zalloc(rqlen, KM_SLEEP);
2923fcf3ce44SJohn Forte 	pkt->pkt_cdbp		= kmem_zalloc(ucmdp->uscsi_cdblen, KM_SLEEP);
2924fcf3ce44SJohn Forte 	/* callback routine for passthru, will wake cv_wait */
2925fcf3ce44SJohn Forte 	pkt->pkt_comp		= iscsi_handle_passthru_callback;
2926fcf3ce44SJohn Forte 	pkt->pkt_time		= ucmdp->uscsi_timeout;
2927fcf3ce44SJohn Forte 
2928fcf3ce44SJohn Forte 	/* setup iscsi_cmd structure */
2929fcf3ce44SJohn Forte 	icmdp->cmd_lun			= NULL;
2930fcf3ce44SJohn Forte 	icmdp->cmd_type			= ISCSI_CMD_TYPE_SCSI;
2931fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.lun		= lun;
2932fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.pkt		= pkt;
2933fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.bp		= bp;
2934fcf3ce44SJohn Forte 	bcopy(ucmdp->uscsi_cdb, pkt->pkt_cdbp, ucmdp->uscsi_cdblen);
2935fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.cmdlen	= ucmdp->uscsi_cdblen;
2936fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.statuslen	= rqlen;
2937fcf3ce44SJohn Forte 	icmdp->cmd_crc_error_seen	= B_FALSE;
2938fcf3ce44SJohn Forte 	icmdp->cmd_completed		= B_FALSE;
2939fcf3ce44SJohn Forte 	icmdp->cmd_result		= ISCSI_STATUS_SUCCESS;
2940fcf3ce44SJohn Forte 
2941fcf3ce44SJohn Forte 	/*
2942fcf3ce44SJohn Forte 	 * Step 2. Push IO onto pending queue.  If we aren't in
2943fcf3ce44SJohn Forte 	 * FULL_FEATURE we need to fail the IO.
2944fcf3ce44SJohn Forte 	 */
2945fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_state_mutex);
2946fcf3ce44SJohn Forte 	if (!ISCSI_SESS_STATE_FULL_FEATURE(isp->sess_state)) {
2947fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_state_mutex);
2948fcf3ce44SJohn Forte 
2949fcf3ce44SJohn Forte 		iscsi_cmd_free(icmdp);
2950fcf3ce44SJohn Forte 		kmem_free(pkt->pkt_cdbp, ucmdp->uscsi_cdblen);
2951fcf3ce44SJohn Forte 		kmem_free(pkt->pkt_scbp, rqlen);
2952fcf3ce44SJohn Forte 		kmem_free(pkt, sizeof (struct scsi_pkt));
2953fcf3ce44SJohn Forte 		kmem_free(bp, sizeof (struct buf));
2954fcf3ce44SJohn Forte 
2955fcf3ce44SJohn Forte 		return (ISCSI_STATUS_CMD_FAILED);
2956fcf3ce44SJohn Forte 	}
2957fcf3ce44SJohn Forte 
2958fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
2959fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp);
2960fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
2961fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_state_mutex);
2962fcf3ce44SJohn Forte 
2963fcf3ce44SJohn Forte 	/*
2964fcf3ce44SJohn Forte 	 * Step 3. Wait on cv_wait for completion routine
2965fcf3ce44SJohn Forte 	 */
2966fcf3ce44SJohn Forte 	mutex_enter(&icmdp->cmd_mutex);
2967fcf3ce44SJohn Forte 	while (icmdp->cmd_completed == B_FALSE) {
2968fcf3ce44SJohn Forte 		cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex);
2969fcf3ce44SJohn Forte 	}
2970fcf3ce44SJohn Forte 	mutex_exit(&icmdp->cmd_mutex);
2971fcf3ce44SJohn Forte 
2972fcf3ce44SJohn Forte 	/* copy rval */
2973fcf3ce44SJohn Forte 	rval = icmdp->cmd_result;
2974fcf3ce44SJohn Forte 
2975fcf3ce44SJohn Forte 	ucmdp->uscsi_resid = pkt->pkt_resid;
2976fcf3ce44SJohn Forte 
2977fcf3ce44SJohn Forte 	/* update scsi status */
2978fcf3ce44SJohn Forte 	arqstat = (struct scsi_arq_status *)pkt->pkt_scbp;
2979fcf3ce44SJohn Forte 	ucmdp->uscsi_status = ((char *)&arqstat->sts_status)[0];
2980fcf3ce44SJohn Forte 
2981fcf3ce44SJohn Forte 	/* copy request sense buffers if caller gave space */
2982fcf3ce44SJohn Forte 	if ((ucmdp->uscsi_rqlen > 0) &&
2983fcf3ce44SJohn Forte 	    (ucmdp->uscsi_rqbuf != NULL)) {
2984fcf3ce44SJohn Forte 		bcopy(arqstat, ucmdp->uscsi_rqbuf,
2985fcf3ce44SJohn Forte 		    MIN(sizeof (struct scsi_arq_status), rqlen));
2986fcf3ce44SJohn Forte 	}
2987fcf3ce44SJohn Forte 
2988fcf3ce44SJohn Forte 	/* clean up */
2989fcf3ce44SJohn Forte 	iscsi_cmd_free(icmdp);
2990fcf3ce44SJohn Forte 	kmem_free(pkt->pkt_cdbp, ucmdp->uscsi_cdblen);
2991fcf3ce44SJohn Forte 	kmem_free(pkt->pkt_scbp, rqlen);
2992fcf3ce44SJohn Forte 	kmem_free(pkt, sizeof (struct scsi_pkt));
2993fcf3ce44SJohn Forte 	kmem_free(bp, sizeof (struct buf));
2994fcf3ce44SJohn Forte 
2995fcf3ce44SJohn Forte 	return (rval);
2996fcf3ce44SJohn Forte }
2997fcf3ce44SJohn Forte 
2998fcf3ce44SJohn Forte 
2999fcf3ce44SJohn Forte /*
3000fcf3ce44SJohn Forte  * iscsi_handle_passthru_callback -
3001fcf3ce44SJohn Forte  *
3002fcf3ce44SJohn Forte  */
3003fcf3ce44SJohn Forte static void
3004fcf3ce44SJohn Forte iscsi_handle_passthru_callback(struct scsi_pkt *pkt)
3005fcf3ce44SJohn Forte {
3006fcf3ce44SJohn Forte 	iscsi_cmd_t		*icmdp  = NULL;
3007fcf3ce44SJohn Forte 
3008fcf3ce44SJohn Forte 	ASSERT(pkt != NULL);
3009fcf3ce44SJohn Forte 	icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private;
3010fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
3011fcf3ce44SJohn Forte 
3012fcf3ce44SJohn Forte 	mutex_enter(&icmdp->cmd_mutex);
3013fcf3ce44SJohn Forte 	icmdp->cmd_completed    = B_TRUE;
3014fcf3ce44SJohn Forte 	icmdp->cmd_result	= ISCSI_STATUS_SUCCESS;
3015fcf3ce44SJohn Forte 	cv_broadcast(&icmdp->cmd_completion);
3016fcf3ce44SJohn Forte 	mutex_exit(&icmdp->cmd_mutex);
3017fcf3ce44SJohn Forte 
3018fcf3ce44SJohn Forte }
3019fcf3ce44SJohn Forte 
302030e7468fSPeter Dunlap /*
302130e7468fSPeter Dunlap  * IDM callbacks
302230e7468fSPeter Dunlap  */
302330e7468fSPeter Dunlap void
302430e7468fSPeter Dunlap iscsi_build_hdr(idm_task_t *idm_task, idm_pdu_t *pdu, uint8_t opcode)
302530e7468fSPeter Dunlap {
302630e7468fSPeter Dunlap 	iscsi_cmd_t *icmdp = idm_task->idt_private;
302730e7468fSPeter Dunlap 	iscsi_conn_t *icp = icmdp->cmd_conn;
302830e7468fSPeter Dunlap 	iscsi_data_hdr_t *ihp = (iscsi_data_hdr_t *)pdu->isp_hdr;
302930e7468fSPeter Dunlap 
303030e7468fSPeter Dunlap 	mutex_enter(&icmdp->cmd_mutex);
303130e7468fSPeter Dunlap 	if (opcode == ISCSI_OP_SCSI_DATA) {
303230e7468fSPeter Dunlap 		uint32_t	data_sn;
303330e7468fSPeter Dunlap 		uint32_t	lun;
303430e7468fSPeter Dunlap 		icmdp = idm_task->idt_private;
303530e7468fSPeter Dunlap 		icp = icmdp->cmd_conn;
303630e7468fSPeter Dunlap 		ihp->opcode	= opcode;
303730e7468fSPeter Dunlap 		ihp->itt	= icmdp->cmd_itt;
303830e7468fSPeter Dunlap 		ihp->ttt	= idm_task->idt_r2t_ttt;
303930e7468fSPeter Dunlap 		ihp->expstatsn	= htonl(icp->conn_expstatsn);
304030e7468fSPeter Dunlap 		icp->conn_laststatsn = icp->conn_expstatsn;
304130e7468fSPeter Dunlap 		data_sn = ntohl(ihp->datasn);
304230e7468fSPeter Dunlap 		data_sn++;
304330e7468fSPeter Dunlap 		lun = icmdp->cmd_un.scsi.lun;
304430e7468fSPeter Dunlap 		ISCSI_LUN_BYTE_COPY(ihp->lun, lun);
304530e7468fSPeter Dunlap 		/* CRM: upate_flow_control */
304630e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE, "DEBUG: iscsi_build_hdr"
304730e7468fSPeter Dunlap 		    "(ISCSI_OP_SCSI_DATA): task: %p icp: %p ic: %p itt: %x "
304830e7468fSPeter Dunlap 		    "exp: %d data_sn: %d", (void *)idm_task, (void *)icp,
304930e7468fSPeter Dunlap 		    (void *)icp->conn_ic, ihp->itt, icp->conn_expstatsn,
305030e7468fSPeter Dunlap 		    data_sn);
305130e7468fSPeter Dunlap 	} else {
305230e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi_build_hdr: unprocessed build "
305330e7468fSPeter Dunlap 		    "header opcode: %x", opcode);
305430e7468fSPeter Dunlap 	}
305530e7468fSPeter Dunlap 	mutex_exit(&icmdp->cmd_mutex);
305630e7468fSPeter Dunlap }
305730e7468fSPeter Dunlap 
305830e7468fSPeter Dunlap static void
305930e7468fSPeter Dunlap iscsi_process_rsp_status(iscsi_sess_t *isp, iscsi_conn_t *icp,
306030e7468fSPeter Dunlap     idm_status_t status)
306130e7468fSPeter Dunlap {
306230e7468fSPeter Dunlap 	switch (status) {
306330e7468fSPeter Dunlap 	case IDM_STATUS_SUCCESS:
306430e7468fSPeter Dunlap 		if ((isp->sess_state == ISCSI_SESS_STATE_IN_FLUSH) &&
306530e7468fSPeter Dunlap 		    (icp->conn_queue_active.count == 0)) {
306630e7468fSPeter Dunlap 			iscsi_drop_conn_cleanup(icp);
306730e7468fSPeter Dunlap 		}
306830e7468fSPeter Dunlap 		break;
306930e7468fSPeter Dunlap 	case IDM_STATUS_PROTOCOL_ERROR:
307030e7468fSPeter Dunlap 		KSTAT_INC_CONN_ERR_PROTOCOL(icp);
307130e7468fSPeter Dunlap 		iscsi_drop_conn_cleanup(icp);
307230e7468fSPeter Dunlap 		break;
307330e7468fSPeter Dunlap 	default:
307430e7468fSPeter Dunlap 		break;
307530e7468fSPeter Dunlap 	}
307630e7468fSPeter Dunlap }
307730e7468fSPeter Dunlap 
307830e7468fSPeter Dunlap static void
307930e7468fSPeter Dunlap iscsi_drop_conn_cleanup(iscsi_conn_t *icp) {
308030e7468fSPeter Dunlap 	mutex_enter(&icp->conn_state_mutex);
308130e7468fSPeter Dunlap 	idm_ini_conn_disconnect(icp->conn_ic);
308230e7468fSPeter Dunlap 	mutex_exit(&icp->conn_state_mutex);
308330e7468fSPeter Dunlap }
308430e7468fSPeter Dunlap 
308530e7468fSPeter Dunlap void
308630e7468fSPeter Dunlap iscsi_rx_error_pdu(idm_conn_t *ic, idm_pdu_t *pdu, idm_status_t status)
308730e7468fSPeter Dunlap {
308830e7468fSPeter Dunlap 	iscsi_conn_t *icp = (iscsi_conn_t *)ic->ic_handle;
308930e7468fSPeter Dunlap 	iscsi_sess_t *isp;
309030e7468fSPeter Dunlap 
309130e7468fSPeter Dunlap 	ASSERT(icp != NULL);
309230e7468fSPeter Dunlap 	isp = icp->conn_sess;
309330e7468fSPeter Dunlap 	ASSERT(isp != NULL);
309430e7468fSPeter Dunlap 	iscsi_process_rsp_status(isp, icp, status);
309530e7468fSPeter Dunlap 	idm_pdu_complete(pdu, status);
309630e7468fSPeter Dunlap }
309730e7468fSPeter Dunlap 
309830e7468fSPeter Dunlap void
309930e7468fSPeter Dunlap iscsi_rx_misc_pdu(idm_conn_t *ic, idm_pdu_t *pdu)
310030e7468fSPeter Dunlap {
310130e7468fSPeter Dunlap 	iscsi_conn_t 		*icp;
310230e7468fSPeter Dunlap 	iscsi_hdr_t		*ihp	= (iscsi_hdr_t *)pdu->isp_hdr;
310330e7468fSPeter Dunlap 	iscsi_sess_t		*isp;
310430e7468fSPeter Dunlap 	idm_status_t		status;
310530e7468fSPeter Dunlap 
310630e7468fSPeter Dunlap 	icp = ic->ic_handle;
310730e7468fSPeter Dunlap 	isp = icp->conn_sess;
310830e7468fSPeter Dunlap 	isp->sess_rx_lbolt = icp->conn_rx_lbolt = ddi_get_lbolt();
310930e7468fSPeter Dunlap 	switch (ihp->opcode & ISCSI_OPCODE_MASK) {
311030e7468fSPeter Dunlap 	case ISCSI_OP_LOGIN_RSP:
311130e7468fSPeter Dunlap 		status = iscsi_rx_process_login_pdu(ic, pdu);
311230e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
311330e7468fSPeter Dunlap 		break;
311430e7468fSPeter Dunlap 	case ISCSI_OP_LOGOUT_RSP:
311530e7468fSPeter Dunlap 		status = iscsi_rx_process_logout_rsp(ic, pdu);
311630e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
311730e7468fSPeter Dunlap 		break;
311830e7468fSPeter Dunlap 	case ISCSI_OP_REJECT_MSG:
311930e7468fSPeter Dunlap 		status = iscsi_rx_process_reject_rsp(ic, pdu);
312030e7468fSPeter Dunlap 		break;
312130e7468fSPeter Dunlap 	case ISCSI_OP_SCSI_TASK_MGT_RSP:
312230e7468fSPeter Dunlap 		status = iscsi_rx_process_task_mgt_rsp(ic, pdu);
312330e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
312430e7468fSPeter Dunlap 		break;
312530e7468fSPeter Dunlap 	case ISCSI_OP_NOOP_IN:
312630e7468fSPeter Dunlap 		status = iscsi_rx_process_nop(ic, pdu);
312730e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
312830e7468fSPeter Dunlap 		break;
312930e7468fSPeter Dunlap 	case ISCSI_OP_ASYNC_EVENT:
313030e7468fSPeter Dunlap 		status = iscsi_rx_process_async_rsp(ic, pdu);
313130e7468fSPeter Dunlap 		break;
313230e7468fSPeter Dunlap 	case ISCSI_OP_TEXT_RSP:
313330e7468fSPeter Dunlap 		status = iscsi_rx_process_text_rsp(ic, pdu);
313430e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
313530e7468fSPeter Dunlap 		break;
313630e7468fSPeter Dunlap 	default:
313730e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi connection(%u) protocol error "
313830e7468fSPeter Dunlap 		    "- received misc unsupported opcode 0x%02x",
313930e7468fSPeter Dunlap 		    icp->conn_oid, ihp->opcode);
314030e7468fSPeter Dunlap 		status = IDM_STATUS_PROTOCOL_ERROR;
314130e7468fSPeter Dunlap 		break;
314230e7468fSPeter Dunlap 	}
314330e7468fSPeter Dunlap 	iscsi_process_rsp_status(isp, icp, status);
314430e7468fSPeter Dunlap }
314530e7468fSPeter Dunlap 
3146fcf3ce44SJohn Forte /*
3147fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3148fcf3ce44SJohn Forte  * | Beginning of completion routines					|
3149fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3150fcf3ce44SJohn Forte  */
3151fcf3ce44SJohn Forte 
3152fcf3ce44SJohn Forte /*
3153fcf3ce44SJohn Forte  * iscsi_ic_thread -
3154fcf3ce44SJohn Forte  */
3155fcf3ce44SJohn Forte void
3156fcf3ce44SJohn Forte iscsi_ic_thread(iscsi_thread_t *thread, void *arg)
3157fcf3ce44SJohn Forte {
3158fcf3ce44SJohn Forte 	iscsi_sess_t	*isp = (iscsi_sess_t *)arg;
3159fcf3ce44SJohn Forte 	int		ret;
3160fcf3ce44SJohn Forte 	iscsi_queue_t	q;
3161fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp;
3162fcf3ce44SJohn Forte 	iscsi_cmd_t	*next_icmdp;
3163fcf3ce44SJohn Forte 
3164fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
3165fcf3ce44SJohn Forte 	ASSERT(thread != NULL);
3166fcf3ce44SJohn Forte 	ASSERT(thread->signature == SIG_ISCSI_THREAD);
3167fcf3ce44SJohn Forte 
3168fcf3ce44SJohn Forte 	for (;;) {
3169fcf3ce44SJohn Forte 
3170fcf3ce44SJohn Forte 		/*
3171fcf3ce44SJohn Forte 		 * We wait till iodone or somebody else wakes us up.
3172fcf3ce44SJohn Forte 		 */
3173fcf3ce44SJohn Forte 		ret = iscsi_thread_wait(thread, -1);
3174fcf3ce44SJohn Forte 
3175fcf3ce44SJohn Forte 		/*
3176fcf3ce44SJohn Forte 		 * The value should never be negative since we never timeout.
3177fcf3ce44SJohn Forte 		 */
3178fcf3ce44SJohn Forte 		ASSERT(ret >= 0);
3179fcf3ce44SJohn Forte 
3180fcf3ce44SJohn Forte 		q.count = 0;
3181fcf3ce44SJohn Forte 		q.head  = NULL;
3182fcf3ce44SJohn Forte 		q.tail  = NULL;
3183fcf3ce44SJohn Forte 		mutex_enter(&isp->sess_queue_completion.mutex);
3184fcf3ce44SJohn Forte 		icmdp = isp->sess_queue_completion.head;
3185fcf3ce44SJohn Forte 		while (icmdp != NULL) {
3186fcf3ce44SJohn Forte 			next_icmdp = icmdp->cmd_next;
3187fcf3ce44SJohn Forte 			mutex_enter(&icmdp->cmd_mutex);
3188fcf3ce44SJohn Forte 			/*
3189fcf3ce44SJohn Forte 			 * check if the associated r2t/abort has finished
319030e7468fSPeter Dunlap 			 * yet.  If not, don't complete the command.
3191fcf3ce44SJohn Forte 			 */
3192fcf3ce44SJohn Forte 			if ((icmdp->cmd_un.scsi.r2t_icmdp == NULL) &&
319330e7468fSPeter Dunlap 			    (icmdp->cmd_un.scsi.abort_icmdp == NULL)) {
3194fcf3ce44SJohn Forte 				mutex_exit(&icmdp->cmd_mutex);
3195fcf3ce44SJohn Forte 				(void) iscsi_dequeue_cmd(&isp->
3196fcf3ce44SJohn Forte 				    sess_queue_completion.head,
3197fcf3ce44SJohn Forte 				    &isp->sess_queue_completion.tail,
3198fcf3ce44SJohn Forte 				    icmdp);
3199fcf3ce44SJohn Forte 				--isp->sess_queue_completion.count;
3200fcf3ce44SJohn Forte 				iscsi_enqueue_cmd_head(&q.head,
3201fcf3ce44SJohn Forte 				    &q.tail, icmdp);
320230e7468fSPeter Dunlap 			} else {
3203fcf3ce44SJohn Forte 				mutex_exit(&icmdp->cmd_mutex);
320430e7468fSPeter Dunlap 			}
3205fcf3ce44SJohn Forte 			icmdp = next_icmdp;
3206fcf3ce44SJohn Forte 		}
3207fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_queue_completion.mutex);
3208fcf3ce44SJohn Forte 		icmdp = q.head;
3209fcf3ce44SJohn Forte 		while (icmdp != NULL) {
3210fcf3ce44SJohn Forte 			next_icmdp = icmdp->cmd_next;
3211fcf3ce44SJohn Forte 			iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E8, isp);
3212fcf3ce44SJohn Forte 			icmdp = next_icmdp;
3213fcf3ce44SJohn Forte 		}
3214fcf3ce44SJohn Forte 
3215fcf3ce44SJohn Forte 		if (ret > 0)
3216fcf3ce44SJohn Forte 			/* Somebody woke us up to work */
3217fcf3ce44SJohn Forte 			continue;
3218fcf3ce44SJohn Forte 		else
3219fcf3ce44SJohn Forte 			/*
3220fcf3ce44SJohn Forte 			 * Somebody woke us up to kill ourselves. We will
3221fcf3ce44SJohn Forte 			 * make sure, however that the completion queue is
3222fcf3ce44SJohn Forte 			 * empty before leaving.  After we've done that it
3223fcf3ce44SJohn Forte 			 * is the originator of the signal that has to make
3224fcf3ce44SJohn Forte 			 * sure no other SCSI command is posted.
3225fcf3ce44SJohn Forte 			 */
3226fcf3ce44SJohn Forte 			break;
3227fcf3ce44SJohn Forte 	}
3228fcf3ce44SJohn Forte 
3229fcf3ce44SJohn Forte }
3230fcf3ce44SJohn Forte 
3231fcf3ce44SJohn Forte /*
3232fcf3ce44SJohn Forte  * iscsi_iodone -
3233fcf3ce44SJohn Forte  *
3234fcf3ce44SJohn Forte  */
3235fcf3ce44SJohn Forte void
3236fcf3ce44SJohn Forte iscsi_iodone(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
3237fcf3ce44SJohn Forte {
3238fcf3ce44SJohn Forte 	struct scsi_pkt		*pkt	= NULL;
3239fcf3ce44SJohn Forte 	struct buf		*bp	= icmdp->cmd_un.scsi.bp;
3240fcf3ce44SJohn Forte 
3241fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
3242fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
3243fcf3ce44SJohn Forte 	pkt = icmdp->cmd_un.scsi.pkt;
3244fcf3ce44SJohn Forte 	ASSERT(pkt != NULL);
3245fcf3ce44SJohn Forte 
3246fcf3ce44SJohn Forte 	ASSERT(icmdp->cmd_un.scsi.abort_icmdp == NULL);
3247fcf3ce44SJohn Forte 	ASSERT(icmdp->cmd_un.scsi.r2t_icmdp == NULL);
3248fcf3ce44SJohn Forte 	if (pkt->pkt_reason == CMD_CMPLT) {
3249fcf3ce44SJohn Forte 		if (bp) {
3250fcf3ce44SJohn Forte 			if (bp->b_flags & B_READ) {
3251fcf3ce44SJohn Forte 				KSTAT_SESS_RX_IO_DONE(isp, bp->b_bcount);
3252fcf3ce44SJohn Forte 			} else {
3253fcf3ce44SJohn Forte 				KSTAT_SESS_TX_IO_DONE(isp, bp->b_bcount);
3254fcf3ce44SJohn Forte 			}
3255fcf3ce44SJohn Forte 		}
3256fcf3ce44SJohn Forte 	}
3257fcf3ce44SJohn Forte 
3258fcf3ce44SJohn Forte 	if (pkt->pkt_flags & FLAG_NOINTR) {
3259fcf3ce44SJohn Forte 		cv_broadcast(&icmdp->cmd_completion);
3260fcf3ce44SJohn Forte 		mutex_exit(&icmdp->cmd_mutex);
3261fcf3ce44SJohn Forte 	} else {
3262fcf3ce44SJohn Forte 		/*
3263fcf3ce44SJohn Forte 		 * Release mutex.  As soon as callback is
3264fcf3ce44SJohn Forte 		 * issued the caller may destroy the command.
3265fcf3ce44SJohn Forte 		 */
3266fcf3ce44SJohn Forte 		mutex_exit(&icmdp->cmd_mutex);
3267fcf3ce44SJohn Forte 		/*
3268fcf3ce44SJohn Forte 		 * We can't just directly call the pk_comp routine.  In
3269fcf3ce44SJohn Forte 		 * many error cases the target driver will use the calling
3270fcf3ce44SJohn Forte 		 * thread to re-drive error handling (reset, retries...)
3271fcf3ce44SJohn Forte 		 * back into the hba driver (iscsi).  If the target redrives
3272fcf3ce44SJohn Forte 		 * a reset back into the iscsi driver off this thead we have
3273fcf3ce44SJohn Forte 		 * a chance of deadlocking. So instead use the io completion
3274fcf3ce44SJohn Forte 		 * thread.
3275fcf3ce44SJohn Forte 		 */
3276fcf3ce44SJohn Forte 		(*icmdp->cmd_un.scsi.pkt->pkt_comp)(icmdp->cmd_un.scsi.pkt);
3277fcf3ce44SJohn Forte 	}
3278fcf3ce44SJohn Forte }
3279fcf3ce44SJohn Forte 
3280fcf3ce44SJohn Forte /*
3281fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3282fcf3ce44SJohn Forte  * | End of completion routines						|
3283fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3284fcf3ce44SJohn Forte  */
3285fcf3ce44SJohn Forte 
3286fcf3ce44SJohn Forte /*
3287fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3288fcf3ce44SJohn Forte  * | Beginning of watchdog routines					|
3289fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3290fcf3ce44SJohn Forte  */
3291fcf3ce44SJohn Forte 
3292fcf3ce44SJohn Forte /*
3293fcf3ce44SJohn Forte  * iscsi_watchdog_thread -
3294fcf3ce44SJohn Forte  *
3295fcf3ce44SJohn Forte  */
3296fcf3ce44SJohn Forte void
3297fcf3ce44SJohn Forte iscsi_wd_thread(iscsi_thread_t *thread, void *arg)
3298fcf3ce44SJohn Forte {
3299fcf3ce44SJohn Forte 	iscsi_sess_t	*isp = (iscsi_sess_t *)arg;
3300fcf3ce44SJohn Forte 	int		rc = 1;
3301fcf3ce44SJohn Forte 
3302fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
3303fcf3ce44SJohn Forte 
3304fcf3ce44SJohn Forte 	while (rc != NULL) {
3305fcf3ce44SJohn Forte 
3306fcf3ce44SJohn Forte 		iscsi_timeout_checks(isp);
3307fcf3ce44SJohn Forte 		iscsi_nop_checks(isp);
3308fcf3ce44SJohn Forte 
3309fcf3ce44SJohn Forte 		rc = iscsi_thread_wait(thread, SEC_TO_TICK(1));
3310fcf3ce44SJohn Forte 	}
3311fcf3ce44SJohn Forte }
3312fcf3ce44SJohn Forte 
3313fcf3ce44SJohn Forte /*
3314fcf3ce44SJohn Forte  * iscsi_timeout_checks -
3315fcf3ce44SJohn Forte  *
3316fcf3ce44SJohn Forte  */
3317fcf3ce44SJohn Forte static void
3318fcf3ce44SJohn Forte iscsi_timeout_checks(iscsi_sess_t *isp)
3319fcf3ce44SJohn Forte {
3320fcf3ce44SJohn Forte 	clock_t		now = ddi_get_lbolt();
3321fcf3ce44SJohn Forte 	iscsi_conn_t	*icp;
332230e7468fSPeter Dunlap 	iscsi_cmd_t	*icmdp, *nicmdp;
3323fcf3ce44SJohn Forte 
3324fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
3325fcf3ce44SJohn Forte 
3326fcf3ce44SJohn Forte 	/* PENDING */
3327fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_state_mutex);
3328fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
3329fcf3ce44SJohn Forte 	for (icmdp = isp->sess_queue_pending.head;
3330fcf3ce44SJohn Forte 	    icmdp; icmdp = nicmdp) {
3331fcf3ce44SJohn Forte 		nicmdp = icmdp->cmd_next;
3332fcf3ce44SJohn Forte 
3333fcf3ce44SJohn Forte 		/* Skip entries with no timeout */
3334fcf3ce44SJohn Forte 		if (icmdp->cmd_lbolt_timeout == 0)
3335fcf3ce44SJohn Forte 			continue;
3336fcf3ce44SJohn Forte 
3337fcf3ce44SJohn Forte 		/*
3338fcf3ce44SJohn Forte 		 * Skip pending queue entries for cmd_type values that depend
3339fcf3ce44SJohn Forte 		 * on having an open cmdsn window for successfull transition
3340fcf3ce44SJohn Forte 		 * from pending to the active (i.e. ones that depend on
3341fcf3ce44SJohn Forte 		 * sess_cmdsn .vs. sess_maxcmdsn). For them, the timer starts
3342fcf3ce44SJohn Forte 		 * when they are successfully moved to the active queue by
3343fcf3ce44SJohn Forte 		 * iscsi_cmd_state_pending() code.
3344fcf3ce44SJohn Forte 		 */
3345d233de7eSJack Meng 		/*
3346d233de7eSJack Meng 		 * If the cmd is stuck, at least give it a chance
3347d233de7eSJack Meng 		 * to timeout
3348d233de7eSJack Meng 		 */
3349d233de7eSJack Meng 		if (((icmdp->cmd_type == ISCSI_CMD_TYPE_SCSI) ||
3350d233de7eSJack Meng 		    (icmdp->cmd_type == ISCSI_CMD_TYPE_TEXT)) &&
3351d233de7eSJack Meng 		    !(icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_STUCK))
3352fcf3ce44SJohn Forte 			continue;
3353fcf3ce44SJohn Forte 
3354fcf3ce44SJohn Forte 		/* Skip if timeout still in the future */
3355fcf3ce44SJohn Forte 		if (now <= icmdp->cmd_lbolt_timeout)
3356fcf3ce44SJohn Forte 			continue;
3357fcf3ce44SJohn Forte 
3358fcf3ce44SJohn Forte 		/* timeout */
3359fcf3ce44SJohn Forte 		iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E6, isp);
3360fcf3ce44SJohn Forte 	}
3361fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
3362fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_state_mutex);
3363fcf3ce44SJohn Forte 
3364fcf3ce44SJohn Forte 	rw_enter(&isp->sess_conn_list_rwlock, RW_READER);
3365fcf3ce44SJohn Forte 	icp = isp->sess_conn_list;
3366fcf3ce44SJohn Forte 	while (icp != NULL) {
3367fcf3ce44SJohn Forte 
3368*aff4bce5Syi zhang - Sun Microsystems - Beijing China 		icp->conn_timeout = B_FALSE;
3369fcf3ce44SJohn Forte 		/* ACTIVE */
3370fcf3ce44SJohn Forte 		mutex_enter(&icp->conn_state_mutex);
3371fcf3ce44SJohn Forte 		mutex_enter(&isp->sess_queue_pending.mutex);
3372fcf3ce44SJohn Forte 		mutex_enter(&icp->conn_queue_active.mutex);
3373fcf3ce44SJohn Forte 		for (icmdp = icp->conn_queue_active.head;
3374fcf3ce44SJohn Forte 		    icmdp; icmdp = nicmdp) {
3375fcf3ce44SJohn Forte 			nicmdp = icmdp->cmd_next;
3376fcf3ce44SJohn Forte 
3377*aff4bce5Syi zhang - Sun Microsystems - Beijing China 			if (iscsi_nop_timeout_checks(icmdp) == B_TRUE) {
3378*aff4bce5Syi zhang - Sun Microsystems - Beijing China 				icp->conn_timeout = B_TRUE;
3379*aff4bce5Syi zhang - Sun Microsystems - Beijing China 			}
3380*aff4bce5Syi zhang - Sun Microsystems - Beijing China 
3381fcf3ce44SJohn Forte 			/* Skip entries with no timeout */
3382fcf3ce44SJohn Forte 			if (icmdp->cmd_lbolt_timeout == 0)
3383fcf3ce44SJohn Forte 				continue;
3384fcf3ce44SJohn Forte 
33852b79d384Sbing zhao - Sun Microsystems - Beijing China 			/*
33862b79d384Sbing zhao - Sun Microsystems - Beijing China 			 * Skip if command is not active or not needed
33872b79d384Sbing zhao - Sun Microsystems - Beijing China 			 * to flush.
33882b79d384Sbing zhao - Sun Microsystems - Beijing China 			 */
33892b79d384Sbing zhao - Sun Microsystems - Beijing China 			if (icmdp->cmd_state != ISCSI_CMD_STATE_ACTIVE &&
33902b79d384Sbing zhao - Sun Microsystems - Beijing China 			    !(icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_FLUSH))
3391fcf3ce44SJohn Forte 				continue;
3392fcf3ce44SJohn Forte 
3393fcf3ce44SJohn Forte 			/* Skip if timeout still in the future */
3394fcf3ce44SJohn Forte 			if (now <= icmdp->cmd_lbolt_timeout)
3395fcf3ce44SJohn Forte 				continue;
3396fcf3ce44SJohn Forte 
33972b79d384Sbing zhao - Sun Microsystems - Beijing China 			if (icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_FLUSH) {
33982b79d384Sbing zhao - Sun Microsystems - Beijing China 				/*
33992b79d384Sbing zhao - Sun Microsystems - Beijing China 				 * This command is left during target reset,
34002b79d384Sbing zhao - Sun Microsystems - Beijing China 				 * we can flush it now.
34012b79d384Sbing zhao - Sun Microsystems - Beijing China 				 */
34022b79d384Sbing zhao - Sun Microsystems - Beijing China 				iscsi_cmd_state_machine(icmdp,
34032b79d384Sbing zhao - Sun Microsystems - Beijing China 				    ISCSI_CMD_EVENT_E7, isp);
34042b79d384Sbing zhao - Sun Microsystems - Beijing China 			} else if (icmdp->cmd_state == ISCSI_CMD_STATE_ACTIVE) {
34052b79d384Sbing zhao - Sun Microsystems - Beijing China 				/* timeout */
34062b79d384Sbing zhao - Sun Microsystems - Beijing China 				iscsi_cmd_state_machine(icmdp,
34072b79d384Sbing zhao - Sun Microsystems - Beijing China 				    ISCSI_CMD_EVENT_E6, isp);
34082b79d384Sbing zhao - Sun Microsystems - Beijing China 			}
34092b79d384Sbing zhao - Sun Microsystems - Beijing China 
3410fcf3ce44SJohn Forte 		}
3411fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
3412fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_queue_pending.mutex);
3413fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_state_mutex);
3414fcf3ce44SJohn Forte 
3415fcf3ce44SJohn Forte 		icp = icp->conn_next;
3416fcf3ce44SJohn Forte 	}
3417*aff4bce5Syi zhang - Sun Microsystems - Beijing China 
3418*aff4bce5Syi zhang - Sun Microsystems - Beijing China 	icp = isp->sess_conn_list;
3419*aff4bce5Syi zhang - Sun Microsystems - Beijing China 	while (icp != NULL) {
3420*aff4bce5Syi zhang - Sun Microsystems - Beijing China 		if (icp->conn_timeout == B_TRUE) {
3421*aff4bce5Syi zhang - Sun Microsystems - Beijing China 			/* timeout on this connect detected */
3422*aff4bce5Syi zhang - Sun Microsystems - Beijing China 			idm_ini_conn_disconnect(icp->conn_ic);
3423*aff4bce5Syi zhang - Sun Microsystems - Beijing China 			icp->conn_timeout = B_FALSE;
3424*aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
3425*aff4bce5Syi zhang - Sun Microsystems - Beijing China 		icp = icp->conn_next;
3426*aff4bce5Syi zhang - Sun Microsystems - Beijing China 	}
3427fcf3ce44SJohn Forte 	rw_exit(&isp->sess_conn_list_rwlock);
3428fcf3ce44SJohn Forte }
3429fcf3ce44SJohn Forte 
3430fcf3ce44SJohn Forte /*
3431fcf3ce44SJohn Forte  * iscsi_nop_checks - sends a NOP on idle connections
3432fcf3ce44SJohn Forte  *
3433fcf3ce44SJohn Forte  * This function walks the connections on a session and
3434fcf3ce44SJohn Forte  * issues NOPs on those connections that are in FULL
3435fcf3ce44SJohn Forte  * FEATURE mode and have not received data for the
3436fcf3ce44SJohn Forte  * time period specified by iscsi_nop_delay (global).
3437fcf3ce44SJohn Forte  */
3438fcf3ce44SJohn Forte static void
3439fcf3ce44SJohn Forte iscsi_nop_checks(iscsi_sess_t *isp)
3440fcf3ce44SJohn Forte {
3441fcf3ce44SJohn Forte 	iscsi_conn_t	*icp;
3442fcf3ce44SJohn Forte 
3443fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
3444fcf3ce44SJohn Forte 
3445fcf3ce44SJohn Forte 	if (isp->sess_type == ISCSI_SESS_TYPE_DISCOVERY) {
3446fcf3ce44SJohn Forte 		return;
3447fcf3ce44SJohn Forte 	}
3448fcf3ce44SJohn Forte 
3449fcf3ce44SJohn Forte 	rw_enter(&isp->sess_conn_list_rwlock, RW_READER);
3450fcf3ce44SJohn Forte 	icp = isp->sess_conn_act;
3451fcf3ce44SJohn Forte 	if (icp != NULL) {
3452fcf3ce44SJohn Forte 
3453fcf3ce44SJohn Forte 		mutex_enter(&icp->conn_state_mutex);
3454fcf3ce44SJohn Forte 		if ((ISCSI_CONN_STATE_FULL_FEATURE(icp->conn_state)) &&
3455fcf3ce44SJohn Forte 		    (ddi_get_lbolt() > isp->sess_conn_act->conn_rx_lbolt +
3456fcf3ce44SJohn Forte 		    SEC_TO_TICK(iscsi_nop_delay)) && (ddi_get_lbolt() >
3457fcf3ce44SJohn Forte 		    isp->sess_conn_act->conn_nop_lbolt +
3458fcf3ce44SJohn Forte 		    SEC_TO_TICK(iscsi_nop_delay))) {
3459fcf3ce44SJohn Forte 
3460fcf3ce44SJohn Forte 			/*
3461fcf3ce44SJohn Forte 			 * We haven't received anything from the
3462fcf3ce44SJohn Forte 			 * target is a defined period of time,
3463fcf3ce44SJohn Forte 			 * send NOP to see if the target is alive.
3464fcf3ce44SJohn Forte 			 */
3465fcf3ce44SJohn Forte 			mutex_enter(&isp->sess_queue_pending.mutex);
3466fcf3ce44SJohn Forte 			iscsi_handle_nop(isp->sess_conn_act,
3467fcf3ce44SJohn Forte 			    0, ISCSI_RSVD_TASK_TAG);
3468fcf3ce44SJohn Forte 			mutex_exit(&isp->sess_queue_pending.mutex);
3469fcf3ce44SJohn Forte 		}
3470fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_state_mutex);
3471fcf3ce44SJohn Forte 
3472fcf3ce44SJohn Forte 		icp = icp->conn_next;
3473fcf3ce44SJohn Forte 	}
3474fcf3ce44SJohn Forte 	rw_exit(&isp->sess_conn_list_rwlock);
3475fcf3ce44SJohn Forte }
3476fcf3ce44SJohn Forte 
3477*aff4bce5Syi zhang - Sun Microsystems - Beijing China static boolean_t
3478*aff4bce5Syi zhang - Sun Microsystems - Beijing China iscsi_nop_timeout_checks(iscsi_cmd_t *icmdp)
3479*aff4bce5Syi zhang - Sun Microsystems - Beijing China {
3480*aff4bce5Syi zhang - Sun Microsystems - Beijing China 	if (icmdp->cmd_type == ISCSI_CMD_TYPE_NOP) {
3481*aff4bce5Syi zhang - Sun Microsystems - Beijing China 		if ((ddi_get_lbolt() - icmdp->cmd_lbolt_active) >
3482*aff4bce5Syi zhang - Sun Microsystems - Beijing China 		    SEC_TO_TICK(ISCSI_CONN_TIEMOUT_DETECT)) {
3483*aff4bce5Syi zhang - Sun Microsystems - Beijing China 			return (B_TRUE);
3484*aff4bce5Syi zhang - Sun Microsystems - Beijing China 		} else {
3485*aff4bce5Syi zhang - Sun Microsystems - Beijing China 			return (B_FALSE);
3486*aff4bce5Syi zhang - Sun Microsystems - Beijing China 		}
3487*aff4bce5Syi zhang - Sun Microsystems - Beijing China 	}
3488*aff4bce5Syi zhang - Sun Microsystems - Beijing China 	return (B_FALSE);
3489*aff4bce5Syi zhang - Sun Microsystems - Beijing China }
3490fcf3ce44SJohn Forte /*
3491fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3492fcf3ce44SJohn Forte  * | End of wd routines						|
3493fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3494fcf3ce44SJohn Forte  */
34952b79d384Sbing zhao - Sun Microsystems - Beijing China 
34962b79d384Sbing zhao - Sun Microsystems - Beijing China /*
34972b79d384Sbing zhao - Sun Microsystems - Beijing China  * iscsi_flush_cmd_after_reset - flush commands after reset
34982b79d384Sbing zhao - Sun Microsystems - Beijing China  *
34992b79d384Sbing zhao - Sun Microsystems - Beijing China  * Here we will flush all the commands in the same connection whose cmdsn is
35002b79d384Sbing zhao - Sun Microsystems - Beijing China  * less than the one received with the Unit Attention.
35012b79d384Sbing zhao - Sun Microsystems - Beijing China  */
35022b79d384Sbing zhao - Sun Microsystems - Beijing China static void
35032b79d384Sbing zhao - Sun Microsystems - Beijing China iscsi_flush_cmd_after_reset(uint32_t cmd_sn, uint16_t lun_num,
35042b79d384Sbing zhao - Sun Microsystems - Beijing China     iscsi_conn_t *icp)
35052b79d384Sbing zhao - Sun Microsystems - Beijing China {
35062b79d384Sbing zhao - Sun Microsystems - Beijing China 	iscsi_cmd_t	*t_icmdp    = NULL;
35072b79d384Sbing zhao - Sun Microsystems - Beijing China 	iscsi_cmd_t	*next_icmdp = NULL;
35082b79d384Sbing zhao - Sun Microsystems - Beijing China 
35092b79d384Sbing zhao - Sun Microsystems - Beijing China 	ASSERT(icp != NULL);
35102b79d384Sbing zhao - Sun Microsystems - Beijing China 
35112b79d384Sbing zhao - Sun Microsystems - Beijing China 	t_icmdp = icp->conn_queue_active.head;
35122b79d384Sbing zhao - Sun Microsystems - Beijing China 	while (t_icmdp != NULL) {
35132b79d384Sbing zhao - Sun Microsystems - Beijing China 		next_icmdp = t_icmdp->cmd_next;
35142b79d384Sbing zhao - Sun Microsystems - Beijing China 		mutex_enter(&t_icmdp->cmd_mutex);
35152b79d384Sbing zhao - Sun Microsystems - Beijing China 		/*
35162b79d384Sbing zhao - Sun Microsystems - Beijing China 		 * We will flush the commands whose cmdsn is less than the one
35172b79d384Sbing zhao - Sun Microsystems - Beijing China 		 * got Unit Attention.
35182b79d384Sbing zhao - Sun Microsystems - Beijing China 		 * Here we will check for wrap by subtracting and compare to
35192b79d384Sbing zhao - Sun Microsystems - Beijing China 		 * 1/2 of a 32 bit number, if greater then we wrapped.
35202b79d384Sbing zhao - Sun Microsystems - Beijing China 		 */
35212b79d384Sbing zhao - Sun Microsystems - Beijing China 		if ((t_icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_SENT) &&
35222b79d384Sbing zhao - Sun Microsystems - Beijing China 		    ((cmd_sn > t_icmdp->cmd_sn) ||
35232b79d384Sbing zhao - Sun Microsystems - Beijing China 		    ((t_icmdp->cmd_sn - cmd_sn) >
35242b79d384Sbing zhao - Sun Microsystems - Beijing China 		    ISCSI_CMD_SN_WRAP))) {
35252b79d384Sbing zhao - Sun Microsystems - Beijing China 			if (t_icmdp->cmd_lun != NULL &&
35262b79d384Sbing zhao - Sun Microsystems - Beijing China 			    t_icmdp->cmd_lun->lun_num == lun_num) {
35272b79d384Sbing zhao - Sun Microsystems - Beijing China 				t_icmdp->cmd_misc_flags |=
35282b79d384Sbing zhao - Sun Microsystems - Beijing China 				    ISCSI_CMD_MISCFLAG_FLUSH;
35292b79d384Sbing zhao - Sun Microsystems - Beijing China 				if (t_icmdp->cmd_type == ISCSI_CMD_TYPE_SCSI) {
35302b79d384Sbing zhao - Sun Microsystems - Beijing China 					t_icmdp->cmd_un.scsi.pkt_stat |=
35312b79d384Sbing zhao - Sun Microsystems - Beijing China 					    STAT_BUS_RESET;
35322b79d384Sbing zhao - Sun Microsystems - Beijing China 				}
35332b79d384Sbing zhao - Sun Microsystems - Beijing China 			}
35342b79d384Sbing zhao - Sun Microsystems - Beijing China 		}
35352b79d384Sbing zhao - Sun Microsystems - Beijing China 		mutex_exit(&t_icmdp->cmd_mutex);
35362b79d384Sbing zhao - Sun Microsystems - Beijing China 		t_icmdp = next_icmdp;
35372b79d384Sbing zhao - Sun Microsystems - Beijing China 	}
35382b79d384Sbing zhao - Sun Microsystems - Beijing China }
35392b79d384Sbing zhao - Sun Microsystems - Beijing China 
35402b79d384Sbing zhao - Sun Microsystems - Beijing China /*
35412b79d384Sbing zhao - Sun Microsystems - Beijing China  * iscsi_decode_sense - decode the sense data in the cmd response
35422b79d384Sbing zhao - Sun Microsystems - Beijing China  *
35432b79d384Sbing zhao - Sun Microsystems - Beijing China  * Here we only care about Unit Attention with 0x29.
35442b79d384Sbing zhao - Sun Microsystems - Beijing China  */
35452b79d384Sbing zhao - Sun Microsystems - Beijing China static boolean_t
35462b79d384Sbing zhao - Sun Microsystems - Beijing China iscsi_decode_sense(uint8_t *sense_data)
35472b79d384Sbing zhao - Sun Microsystems - Beijing China {
35482b79d384Sbing zhao - Sun Microsystems - Beijing China 	uint8_t	sense_key   = 0;
35492b79d384Sbing zhao - Sun Microsystems - Beijing China 	uint8_t	asc	    = 0;
35502b79d384Sbing zhao - Sun Microsystems - Beijing China 	boolean_t affect    = B_FALSE;
35512b79d384Sbing zhao - Sun Microsystems - Beijing China 
35522b79d384Sbing zhao - Sun Microsystems - Beijing China 	ASSERT(sense_data != NULL);
35532b79d384Sbing zhao - Sun Microsystems - Beijing China 
35542b79d384Sbing zhao - Sun Microsystems - Beijing China 	sense_key = scsi_sense_key(sense_data);
35552b79d384Sbing zhao - Sun Microsystems - Beijing China 	switch (sense_key) {
35562b79d384Sbing zhao - Sun Microsystems - Beijing China 		case KEY_UNIT_ATTENTION:
35572b79d384Sbing zhao - Sun Microsystems - Beijing China 			asc = scsi_sense_asc(sense_data);
35582b79d384Sbing zhao - Sun Microsystems - Beijing China 			switch (asc) {
35592b79d384Sbing zhao - Sun Microsystems - Beijing China 				case ISCSI_SCSI_RESET_SENSE_CODE:
35602b79d384Sbing zhao - Sun Microsystems - Beijing China 					/*
35612b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * POWER ON, RESET, OR BUS_DEVICE RESET
35622b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * OCCURRED
35632b79d384Sbing zhao - Sun Microsystems - Beijing China 					 */
35642b79d384Sbing zhao - Sun Microsystems - Beijing China 					affect = B_TRUE;
35652b79d384Sbing zhao - Sun Microsystems - Beijing China 					break;
35662b79d384Sbing zhao - Sun Microsystems - Beijing China 				default:
35672b79d384Sbing zhao - Sun Microsystems - Beijing China 					/*
35682b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * Currently we don't care
35692b79d384Sbing zhao - Sun Microsystems - Beijing China 					 * about other sense key.
35702b79d384Sbing zhao - Sun Microsystems - Beijing China 					 */
35712b79d384Sbing zhao - Sun Microsystems - Beijing China 					break;
35722b79d384Sbing zhao - Sun Microsystems - Beijing China 			}
35732b79d384Sbing zhao - Sun Microsystems - Beijing China 			break;
35742b79d384Sbing zhao - Sun Microsystems - Beijing China 		default:
35752b79d384Sbing zhao - Sun Microsystems - Beijing China 			/*
35762b79d384Sbing zhao - Sun Microsystems - Beijing China 			 * Currently we don't care
35772b79d384Sbing zhao - Sun Microsystems - Beijing China 			 * about other sense key.
35782b79d384Sbing zhao - Sun Microsystems - Beijing China 			 */
35792b79d384Sbing zhao - Sun Microsystems - Beijing China 			break;
35802b79d384Sbing zhao - Sun Microsystems - Beijing China 	}
35812b79d384Sbing zhao - Sun Microsystems - Beijing China 	return (affect);
35822b79d384Sbing zhao - Sun Microsystems - Beijing China }
3583