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() */
36fcf3ce44SJohn Forte 
37fcf3ce44SJohn Forte #include "iscsi.h"		/* iscsi driver */
381a1a84a3SPeter Dunlap #include <sys/iscsi_protocol.h>	/* iscsi protocol */
39fcf3ce44SJohn Forte 
4030e7468fSPeter Dunlap #define	ISCSI_INI_TASK_TTT	0xffffffff
4130e7468fSPeter Dunlap 
4230e7468fSPeter Dunlap boolean_t iscsi_io_logging = B_FALSE;
4330e7468fSPeter Dunlap 
4430e7468fSPeter Dunlap #define	ISCSI_CHECK_SCSI_READ(ICHK_CMD, ICHK_HDR, ICHK_LEN, ICHK_TYPE)	\
4530e7468fSPeter Dunlap 	if (idm_pattern_checking)  {					\
4630e7468fSPeter Dunlap 		struct scsi_pkt *pkt = (ICHK_CMD)->cmd_un.scsi.pkt;	\
4730e7468fSPeter Dunlap 		if (((ICHK_HDR)->response == 0) && 			\
4830e7468fSPeter Dunlap 		    ((ICHK_HDR)->cmd_status == 0) &&			\
4930e7468fSPeter Dunlap 		    ((pkt->pkt_cdbp[0] == SCMD_READ_G1) ||		\
5030e7468fSPeter Dunlap 		    (pkt->pkt_cdbp[0] == SCMD_READ_G4) || 		\
5130e7468fSPeter Dunlap 		    (pkt->pkt_cdbp[0] == SCMD_READ) || 			\
5230e7468fSPeter Dunlap 		    (pkt->pkt_cdbp[0] == SCMD_READ_G5))) {		\
5330e7468fSPeter Dunlap 			idm_buf_t *idb = (ICHK_CMD)->cmd_un.scsi.ibp_ibuf; \
5430e7468fSPeter Dunlap 			IDM_BUFPAT_CHECK(idb, ICHK_LEN, ICHK_TYPE); \
5530e7468fSPeter Dunlap 		}						\
5630e7468fSPeter Dunlap 	}
5730e7468fSPeter Dunlap 
58fcf3ce44SJohn Forte /* generic io helpers */
59fcf3ce44SJohn Forte static uint32_t n2h24(uchar_t *ptr);
60fcf3ce44SJohn Forte static int iscsi_sna_lt(uint32_t n1, uint32_t n2);
6130e7468fSPeter Dunlap void iscsi_update_flow_control(iscsi_sess_t *isp,
62fcf3ce44SJohn Forte     uint32_t max, uint32_t exp);
6330e7468fSPeter Dunlap static iscsi_status_t iscsi_rx_process_scsi_itt_to_icmdp(iscsi_sess_t *isp,
6430e7468fSPeter Dunlap     idm_conn_t *ic, iscsi_scsi_rsp_hdr_t *ihp, iscsi_cmd_t **icmdp);
65fcf3ce44SJohn Forte static iscsi_status_t iscsi_rx_process_itt_to_icmdp(iscsi_sess_t *isp,
66fcf3ce44SJohn Forte     iscsi_hdr_t *ihp, iscsi_cmd_t **icmdp);
6730e7468fSPeter Dunlap static void iscsi_process_rsp_status(iscsi_sess_t *isp, iscsi_conn_t *icp,
6830e7468fSPeter Dunlap     idm_status_t status);
6930e7468fSPeter Dunlap static void iscsi_drop_conn_cleanup(iscsi_conn_t *icp);
70fcf3ce44SJohn Forte 
7130e7468fSPeter Dunlap /* callbacks from idm */
7230e7468fSPeter Dunlap static idm_pdu_cb_t iscsi_tx_done;
7330e7468fSPeter Dunlap 
7430e7468fSPeter Dunlap /* receivers */
7530e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_nop(idm_conn_t *ic, idm_pdu_t *pdu);
7630e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_data_rsp(idm_conn_t *ic,
7730e7468fSPeter Dunlap     idm_pdu_t *pdu);
7830e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_cmd_rsp(idm_conn_t *ic, idm_pdu_t *pdu);
7930e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_reject_rsp(idm_conn_t *ic,
8030e7468fSPeter Dunlap     idm_pdu_t *pdu);
8130e7468fSPeter Dunlap 
8230e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_rejected_tsk_mgt(idm_conn_t *ic,
8330e7468fSPeter Dunlap     iscsi_hdr_t *old_ihp);
8430e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_task_mgt_rsp(idm_conn_t *ic,
8530e7468fSPeter Dunlap     idm_pdu_t *pdu);
8630e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_logout_rsp(idm_conn_t *ic,
8730e7468fSPeter Dunlap     idm_pdu_t *pdu);
8830e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_async_rsp(idm_conn_t *ic,
8930e7468fSPeter Dunlap     idm_pdu_t *pdu);
9030e7468fSPeter Dunlap static idm_status_t iscsi_rx_process_text_rsp(idm_conn_t *ic,
9130e7468fSPeter Dunlap     idm_pdu_t *pdu);
92fcf3ce44SJohn Forte 
93fcf3ce44SJohn Forte /* senders */
94fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_scsi(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
95fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_nop(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
96fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_abort(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
97fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_reset(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
98fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_logout(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
99fcf3ce44SJohn Forte static iscsi_status_t iscsi_tx_text(iscsi_sess_t *isp, iscsi_cmd_t *icmdp);
100fcf3ce44SJohn Forte 
101fcf3ce44SJohn Forte 
102fcf3ce44SJohn Forte /* helpers */
10330e7468fSPeter Dunlap static void iscsi_logout_start(void *arg);
104fcf3ce44SJohn Forte static void iscsi_handle_passthru_callback(struct scsi_pkt *pkt);
105fcf3ce44SJohn Forte static void iscsi_handle_nop(iscsi_conn_t *icp, uint32_t itt, uint32_t ttt);
106fcf3ce44SJohn Forte 
107fcf3ce44SJohn Forte static void iscsi_timeout_checks(iscsi_sess_t *isp);
108fcf3ce44SJohn Forte static void iscsi_nop_checks(iscsi_sess_t *isp);
109fcf3ce44SJohn Forte 
110fcf3ce44SJohn Forte /*
111fcf3ce44SJohn Forte  * This file contains the main guts of the iSCSI protocol layer.
112fcf3ce44SJohn Forte  * It's broken into 5 sections; Basic helper functions, RX IO path,
113fcf3ce44SJohn Forte  * TX IO path, Completion (IC) IO path, and watchdog (WD) routines.
114fcf3ce44SJohn Forte  *
115fcf3ce44SJohn Forte  * The IO flow model is similiar to the below diagram.  The
116fcf3ce44SJohn Forte  * iscsi session, connection and command state machines are used
117fcf3ce44SJohn Forte  * to drive IO through this flow diagram.  Reference those files
118fcf3ce44SJohn Forte  * to get a detailed description of their respective state models
119fcf3ce44SJohn Forte  * prior to their xxx_state_machine_function().
120fcf3ce44SJohn Forte  *
121fcf3ce44SJohn Forte  * tran_start() -> CMD_E1     TX_THREAD                   RX_THREAD
122fcf3ce44SJohn Forte  *                   |            T                           T
123fcf3ce44SJohn Forte  *                   V            T                           T
124fcf3ce44SJohn Forte  *                PENDING_Q  --CMD_E2--> ACTIVE_Q -      --CMD_E3--+
125fcf3ce44SJohn Forte  *                                T                \ C        T    |
126fcf3ce44SJohn Forte  *                                T                 \M        T    |
127fcf3ce44SJohn Forte  *                                                   D        T    |
128fcf3ce44SJohn Forte  *                                       WD_THREAD TT|TT      T    |
129fcf3ce44SJohn Forte  *                                                  /E        T    |
130fcf3ce44SJohn Forte  *                                                 / 6        T    |
131fcf3ce44SJohn Forte  *                                     ABORTING_Q<-      --CMD_E3--+
132fcf3ce44SJohn Forte  *                                                            T    |
133fcf3ce44SJohn Forte  *                                T                           T    |
134fcf3ce44SJohn Forte  *                                T                                |
135fcf3ce44SJohn Forte  *               callback()  <--CMD_E#-- COMPLETION_Q <------------+
136fcf3ce44SJohn Forte  *                                T
137fcf3ce44SJohn Forte  *                                T
138fcf3ce44SJohn Forte  *                            IC_THREAD
139fcf3ce44SJohn Forte  *
140fcf3ce44SJohn Forte  * External and internal command are ran thru this same state
141fcf3ce44SJohn Forte  * machine.  All commands enter the state machine by receiving an
142fcf3ce44SJohn Forte  * ISCSI_CMD_EVENT_E1.  This event places the command into the
143fcf3ce44SJohn Forte  * PENDING_Q.  Next when resources are available the TX_THREAD
144fcf3ce44SJohn Forte  * issues a E2 event on the command.  This sends the command
145fcf3ce44SJohn Forte  * to the TCP stack and places the command on the ACTIVE_Q.  While
146fcf3ce44SJohn Forte  * on the PENDIING_Q and ACTIVE_Q, the command is monitored via the
147fcf3ce44SJohn Forte  * WD_THREAD to ensure the pkt_time has not elapsed.  If elapsed the
148fcf3ce44SJohn Forte  * command is issued an E6(timeout) event which moves either (if pending)
149fcf3ce44SJohn Forte  * completed the command or (if active) moves the command to the
150fcf3ce44SJohn Forte  * aborting queue and issues a SCSI TASK MANAGEMENT ABORT command
151fcf3ce44SJohn Forte  * to cancel the IO request.  If the original command is completed
152fcf3ce44SJohn Forte  * or the TASK MANAGEMENT command completes the command is moved
153fcf3ce44SJohn Forte  * to the COMPLETION_Q via a E3 event.  The IC_THREAD then processes
154fcf3ce44SJohn Forte  * the COMPLETION_Q and issues the scsi_pkt callback.  This
155fcf3ce44SJohn Forte  * callback can not be processed directly from the RX_THREAD
156fcf3ce44SJohn Forte  * because the callback might call back into the iscsi driver
157fcf3ce44SJohn Forte  * causing a deadlock condition.
158fcf3ce44SJohn Forte  *
159fcf3ce44SJohn Forte  * For more details on the complete CMD state machine reference
160fcf3ce44SJohn Forte  * the state machine diagram in iscsi_cmd.c.  The connection state
161fcf3ce44SJohn Forte  * machine is driven via IO events in this file.  Then session
162fcf3ce44SJohn Forte  * events are driven by the connection events.  For complete
163fcf3ce44SJohn Forte  * details on these state machines reference iscsi_sess.c and
164fcf3ce44SJohn Forte  * iscsi_conn.c
165fcf3ce44SJohn Forte  */
166fcf3ce44SJohn Forte 
167fcf3ce44SJohn Forte 
168fcf3ce44SJohn Forte /*
169fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
170fcf3ce44SJohn Forte  * | io helper routines							|
171fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
172fcf3ce44SJohn Forte  */
173fcf3ce44SJohn Forte 
174fcf3ce44SJohn Forte /*
175fcf3ce44SJohn Forte  * n2h24 - native to host 24 bit integer translation.
176fcf3ce44SJohn Forte  */
177fcf3ce44SJohn Forte static uint32_t
178fcf3ce44SJohn Forte n2h24(uchar_t *ptr)
179fcf3ce44SJohn Forte {
180fcf3ce44SJohn Forte 	uint32_t idx;
181fcf3ce44SJohn Forte 	bcopy(ptr, &idx, 3);
182fcf3ce44SJohn Forte 	return (ntohl(idx) >> 8);
183fcf3ce44SJohn Forte }
184fcf3ce44SJohn Forte 
185fcf3ce44SJohn Forte /*
186fcf3ce44SJohn Forte  * iscsi_sna_lt - Serial Number Arithmetic, 32 bits, less than, RFC1982
187fcf3ce44SJohn Forte  */
188fcf3ce44SJohn Forte static int
189fcf3ce44SJohn Forte iscsi_sna_lt(uint32_t n1, uint32_t n2)
190fcf3ce44SJohn Forte {
191fcf3ce44SJohn Forte 	return ((n1 != n2) &&
192fcf3ce44SJohn Forte 	    (((n1 < n2) && ((n2 - n1) < ISCSI_SNA32_CHECK)) ||
193fcf3ce44SJohn Forte 	    ((n1 > n2) && ((n1 - n2) > ISCSI_SNA32_CHECK))));
194fcf3ce44SJohn Forte }
195fcf3ce44SJohn Forte 
196fcf3ce44SJohn Forte /*
197fcf3ce44SJohn Forte  * iscsi_sna_lte - Serial Number Arithmetic, 32 bits, less than or equal,
198fcf3ce44SJohn Forte  * RFC1982
199fcf3ce44SJohn Forte  */
200fcf3ce44SJohn Forte int
201fcf3ce44SJohn Forte iscsi_sna_lte(uint32_t n1, uint32_t n2)
202fcf3ce44SJohn Forte {
203fcf3ce44SJohn Forte 	return ((n1 == n2) ||
204fcf3ce44SJohn Forte 	    (((n1 < n2) && ((n2 - n1) < ISCSI_SNA32_CHECK)) ||
205fcf3ce44SJohn Forte 	    ((n1 > n2) && ((n1 - n2) > ISCSI_SNA32_CHECK))));
206fcf3ce44SJohn Forte }
207fcf3ce44SJohn Forte 
208fcf3ce44SJohn Forte /*
209fcf3ce44SJohn Forte  * iscsi_update_flow_control - Update expcmdsn and maxcmdsn iSCSI
210fcf3ce44SJohn Forte  * flow control information for a session
211fcf3ce44SJohn Forte  */
21230e7468fSPeter Dunlap void
213fcf3ce44SJohn Forte iscsi_update_flow_control(iscsi_sess_t *isp, uint32_t max, uint32_t exp)
214fcf3ce44SJohn Forte {
215fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
216fcf3ce44SJohn Forte 	ASSERT(mutex_owned(&isp->sess_cmdsn_mutex));
217fcf3ce44SJohn Forte 
218fcf3ce44SJohn Forte 	if (!iscsi_sna_lt(max, (exp - 1))) {
219fcf3ce44SJohn Forte 
220fcf3ce44SJohn Forte 		if (!iscsi_sna_lte(exp, isp->sess_expcmdsn)) {
221fcf3ce44SJohn Forte 			isp->sess_expcmdsn = exp;
222fcf3ce44SJohn Forte 		}
223fcf3ce44SJohn Forte 
224fcf3ce44SJohn Forte 		if (!iscsi_sna_lte(max, isp->sess_maxcmdsn)) {
225fcf3ce44SJohn Forte 			isp->sess_maxcmdsn = max;
226fcf3ce44SJohn Forte 			if (iscsi_sna_lte(isp->sess_cmdsn,
227fcf3ce44SJohn Forte 			    isp->sess_maxcmdsn)) {
228fcf3ce44SJohn Forte 				/*
229fcf3ce44SJohn Forte 				 * the window is open again - schedule
230fcf3ce44SJohn Forte 				 * to send any held tasks soon
231fcf3ce44SJohn Forte 				 */
232fcf3ce44SJohn Forte 				iscsi_sess_redrive_io(isp);
233fcf3ce44SJohn Forte 			}
234fcf3ce44SJohn Forte 		}
235fcf3ce44SJohn Forte 	}
236fcf3ce44SJohn Forte }
237fcf3ce44SJohn Forte 
238fcf3ce44SJohn Forte 
239fcf3ce44SJohn Forte /*
240fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
241fcf3ce44SJohn Forte  * | io receive and processing routines					|
242fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
243fcf3ce44SJohn Forte  */
244fcf3ce44SJohn Forte 
245fcf3ce44SJohn Forte /*
24630e7468fSPeter Dunlap  * iscsi_rx_scsi_rsp - called from idm
24730e7468fSPeter Dunlap  * For each opcode type fan out the processing.
248fcf3ce44SJohn Forte  */
249fcf3ce44SJohn Forte void
25030e7468fSPeter Dunlap iscsi_rx_scsi_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
251fcf3ce44SJohn Forte {
25230e7468fSPeter Dunlap 	iscsi_conn_t	*icp;
25330e7468fSPeter Dunlap 	iscsi_sess_t	*isp;
25430e7468fSPeter Dunlap 	iscsi_hdr_t	*ihp;
25530e7468fSPeter Dunlap 	idm_status_t	status;
256fcf3ce44SJohn Forte 
25730e7468fSPeter Dunlap 	ASSERT(ic != NULL);
25830e7468fSPeter Dunlap 	ASSERT(pdu != NULL);
25930e7468fSPeter Dunlap 	icp		= ic->ic_handle;
260fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
26130e7468fSPeter Dunlap 	ihp		= (iscsi_hdr_t *)pdu->isp_hdr;
262fcf3ce44SJohn Forte 	ASSERT(ihp != NULL);
26330e7468fSPeter Dunlap 	isp		= icp->conn_sess;
264fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
265fcf3ce44SJohn Forte 
26630e7468fSPeter Dunlap 	/* reset the session timer when we receive the response */
26730e7468fSPeter Dunlap 	isp->sess_rx_lbolt = icp->conn_rx_lbolt = ddi_get_lbolt();
268fcf3ce44SJohn Forte 
269fcf3ce44SJohn Forte 	/* fan out the hdr processing */
270fcf3ce44SJohn Forte 	switch (ihp->opcode & ISCSI_OPCODE_MASK) {
271fcf3ce44SJohn Forte 	case ISCSI_OP_SCSI_DATA_RSP:
27230e7468fSPeter Dunlap 		status = iscsi_rx_process_data_rsp(ic, pdu);
273fcf3ce44SJohn Forte 		break;
274fcf3ce44SJohn Forte 	case ISCSI_OP_SCSI_RSP:
27530e7468fSPeter Dunlap 		status = iscsi_rx_process_cmd_rsp(ic, pdu);
27630e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
277fcf3ce44SJohn Forte 		break;
278fcf3ce44SJohn Forte 	default:
279fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) protocol error - "
28030e7468fSPeter Dunlap 		    "received pdu with unsupported opcode 0x%02x",
281fcf3ce44SJohn Forte 		    icp->conn_oid, ihp->opcode);
28230e7468fSPeter Dunlap 		status = IDM_STATUS_PROTOCOL_ERROR;
283fcf3ce44SJohn Forte 	}
28430e7468fSPeter Dunlap 	iscsi_process_rsp_status(isp, icp, status);
285fcf3ce44SJohn Forte }
286fcf3ce44SJohn Forte 
28730e7468fSPeter Dunlap void
28830e7468fSPeter Dunlap iscsi_task_cleanup(int opcode, iscsi_cmd_t *icmdp)
289fcf3ce44SJohn Forte {
29030e7468fSPeter Dunlap 	struct buf 	*bp;
29130e7468fSPeter Dunlap 	idm_buf_t	*ibp, *obp;
29230e7468fSPeter Dunlap 	idm_task_t	*itp;
293fcf3ce44SJohn Forte 
29430e7468fSPeter Dunlap 	itp = icmdp->cmd_itp;
29530e7468fSPeter Dunlap 	ASSERT(itp != NULL);
29630e7468fSPeter Dunlap 	ASSERT((opcode == ISCSI_OP_SCSI_DATA_RSP) ||
29730e7468fSPeter Dunlap 	    (opcode == ISCSI_OP_SCSI_RSP));
298fcf3ce44SJohn Forte 
29930e7468fSPeter Dunlap 	bp = icmdp->cmd_un.scsi.bp;
30030e7468fSPeter Dunlap 	ibp = icmdp->cmd_un.scsi.ibp_ibuf;
30130e7468fSPeter Dunlap 	obp = icmdp->cmd_un.scsi.ibp_obuf;
30230e7468fSPeter Dunlap 	ISCSI_IO_LOG(CE_NOTE, "DEBUG: task_cleanup: itp: %p opcode: %d "
30330e7468fSPeter Dunlap 	    "icmdp: %p bp: %p ibp: %p", (void *)itp, opcode,
30430e7468fSPeter Dunlap 	    (void *)icmdp, (void *)bp, (void *)ibp);
30530e7468fSPeter Dunlap 	if (bp && bp->b_bcount) {
30630e7468fSPeter Dunlap 		if (ibp != NULL && bp->b_flags & B_READ) {
30730e7468fSPeter Dunlap 			idm_buf_unbind_in(itp, ibp);
30830e7468fSPeter Dunlap 			idm_buf_free(ibp);
30930e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.ibp_ibuf = NULL;
31030e7468fSPeter Dunlap 		} else if (obp != NULL && !(bp->b_flags & B_READ)) {
31130e7468fSPeter Dunlap 			idm_buf_unbind_out(itp, obp);
31230e7468fSPeter Dunlap 			idm_buf_free(obp);
31330e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.ibp_obuf = NULL;
314fcf3ce44SJohn Forte 		}
315fcf3ce44SJohn Forte 	}
316fcf3ce44SJohn Forte 
31730e7468fSPeter Dunlap 	idm_task_done(itp);
31830e7468fSPeter Dunlap }
31930e7468fSPeter Dunlap 
32030e7468fSPeter Dunlap idm_status_t
32130e7468fSPeter Dunlap iscsi_rx_chk(iscsi_conn_t *icp, iscsi_sess_t *isp,
32230e7468fSPeter Dunlap     iscsi_scsi_rsp_hdr_t *irhp, iscsi_cmd_t **icmdp)
32330e7468fSPeter Dunlap {
32430e7468fSPeter Dunlap 	iscsi_status_t rval;
32530e7468fSPeter Dunlap 
326fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
32730e7468fSPeter Dunlap 
32830e7468fSPeter Dunlap 	if (icp->conn_expstatsn == ntohl(irhp->statsn)) {
32930e7468fSPeter Dunlap 		icp->conn_expstatsn++;
33030e7468fSPeter Dunlap 	} else {
33130e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi connection(%u/%x) protocol error - "
33230e7468fSPeter Dunlap 		    "received status out of order itt:0x%x statsn:0x%x "
33330e7468fSPeter Dunlap 		    "expstatsn:0x%x", icp->conn_oid, irhp->opcode,
33430e7468fSPeter Dunlap 		    irhp->itt, ntohl(irhp->statsn), icp->conn_expstatsn);
335fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_cmdsn_mutex);
33630e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
33730e7468fSPeter Dunlap 	}
33830e7468fSPeter Dunlap 
33930e7468fSPeter Dunlap 	/* get icmdp so we can cleanup on error */
34030e7468fSPeter Dunlap 	if ((irhp->opcode == ISCSI_OP_SCSI_DATA_RSP) ||
34130e7468fSPeter Dunlap 	    (irhp->opcode == ISCSI_OP_SCSI_RSP)) {
34230e7468fSPeter Dunlap 		rval = iscsi_rx_process_scsi_itt_to_icmdp(isp, icp->conn_ic,
34330e7468fSPeter Dunlap 		    irhp, icmdp);
34430e7468fSPeter Dunlap 	} else {
34530e7468fSPeter Dunlap 		rval = iscsi_rx_process_itt_to_icmdp(isp,
34630e7468fSPeter Dunlap 		    (iscsi_hdr_t *)irhp, icmdp);
34730e7468fSPeter Dunlap 	}
34830e7468fSPeter Dunlap 
34930e7468fSPeter Dunlap 	if (!ISCSI_SUCCESS(rval)) {
35030e7468fSPeter Dunlap 		mutex_exit(&isp->sess_cmdsn_mutex);
35130e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
352fcf3ce44SJohn Forte 	}
353fcf3ce44SJohn Forte 
354fcf3ce44SJohn Forte 	/* update expcmdsn and maxcmdsn */
35530e7468fSPeter Dunlap 	iscsi_update_flow_control(isp, ntohl(irhp->maxcmdsn),
35630e7468fSPeter Dunlap 	    ntohl(irhp->expcmdsn));
357fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
35830e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
35930e7468fSPeter Dunlap }
360fcf3ce44SJohn Forte 
36130e7468fSPeter Dunlap static void
36230e7468fSPeter Dunlap iscsi_cmd_rsp_chk(iscsi_cmd_t *icmdp, iscsi_scsi_rsp_hdr_t *issrhp)
36330e7468fSPeter Dunlap {
36430e7468fSPeter Dunlap 	struct scsi_pkt *pkt;
36530e7468fSPeter Dunlap 	size_t data_transferred;
366fcf3ce44SJohn Forte 
36730e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
36830e7468fSPeter Dunlap 	pkt->pkt_resid = 0;
36930e7468fSPeter Dunlap 	data_transferred = icmdp->cmd_un.scsi.data_transferred;
37030e7468fSPeter Dunlap 	/* Check the residual count */
37130e7468fSPeter Dunlap 	if ((icmdp->cmd_un.scsi.bp) &&
37230e7468fSPeter Dunlap 	    (data_transferred != icmdp->cmd_un.scsi.bp->b_bcount)) {
373fcf3ce44SJohn Forte 		/*
37430e7468fSPeter Dunlap 		 * We didn't xfer the expected amount of data -
37530e7468fSPeter Dunlap 		 * the residual_count in the header is only
37630e7468fSPeter Dunlap 		 * valid if the underflow flag is set.
377fcf3ce44SJohn Forte 		 */
37830e7468fSPeter Dunlap 		if (issrhp->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
37930e7468fSPeter Dunlap 			pkt->pkt_resid = ntohl(issrhp->residual_count);
38030e7468fSPeter Dunlap 		} else {
38130e7468fSPeter Dunlap 			if (icmdp->cmd_un.scsi.bp->b_bcount >
38230e7468fSPeter Dunlap 			    data_transferred) {
38330e7468fSPeter Dunlap 				/*
38430e7468fSPeter Dunlap 				 * Some data fell on the floor
38530e7468fSPeter Dunlap 				 * somehow - probably a CRC error
38630e7468fSPeter Dunlap 				 */
38730e7468fSPeter Dunlap 				pkt->pkt_resid =
38830e7468fSPeter Dunlap 				    icmdp->cmd_un.scsi.bp->b_bcount -
38930e7468fSPeter Dunlap 				    data_transferred;
39030e7468fSPeter Dunlap 			}
391fcf3ce44SJohn Forte 		}
39230e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE,
39330e7468fSPeter Dunlap 		    "DEBUG: iscsi_rx_cmd_rsp_chk: itt: %u"
39430e7468fSPeter Dunlap 		    "data_trans != b_count data_transferred: %lu "
39530e7468fSPeter Dunlap 		    "b_count: %lu cmd_status: %d flags: %d resid: %lu",
39630e7468fSPeter Dunlap 		    issrhp->itt, data_transferred,
39730e7468fSPeter Dunlap 		    icmdp->cmd_un.scsi.bp->b_bcount,
39830e7468fSPeter Dunlap 		    issrhp->cmd_status & STATUS_MASK,
39930e7468fSPeter Dunlap 		    issrhp->flags, pkt->pkt_resid);
40030e7468fSPeter Dunlap 	}
40130e7468fSPeter Dunlap 	/* set flags that tell SCSA that the command is complete */
40230e7468fSPeter Dunlap 	if (icmdp->cmd_crc_error_seen == B_FALSE) {
40330e7468fSPeter Dunlap 		/* Set successful completion */
40430e7468fSPeter Dunlap 		pkt->pkt_reason = CMD_CMPLT;
40530e7468fSPeter Dunlap 		if (icmdp->cmd_un.scsi.bp) {
40630e7468fSPeter Dunlap 			pkt->pkt_state |= (STATE_XFERRED_DATA |
40730e7468fSPeter Dunlap 			    STATE_GOT_STATUS);
40830e7468fSPeter Dunlap 		} else {
40930e7468fSPeter Dunlap 			pkt->pkt_state |= STATE_GOT_STATUS;
41030e7468fSPeter Dunlap 		}
41130e7468fSPeter Dunlap 	} else {
412fcf3ce44SJohn Forte 		/*
41330e7468fSPeter Dunlap 		 * Some of the data was found to have an incorrect
41430e7468fSPeter Dunlap 		 * error at the protocol error.
415fcf3ce44SJohn Forte 		 */
41630e7468fSPeter Dunlap 		pkt->pkt_reason = CMD_PER_FAIL;
41730e7468fSPeter Dunlap 		pkt->pkt_statistics |= STAT_PERR;
41830e7468fSPeter Dunlap 		if (icmdp->cmd_un.scsi.bp) {
41930e7468fSPeter Dunlap 			pkt->pkt_resid =
42030e7468fSPeter Dunlap 			    icmdp->cmd_un.scsi.bp->b_bcount;
421fcf3ce44SJohn Forte 		} else {
42230e7468fSPeter Dunlap 			pkt->pkt_resid = 0;
423fcf3ce44SJohn Forte 		}
424fcf3ce44SJohn Forte 	}
42530e7468fSPeter Dunlap }
42630e7468fSPeter Dunlap 
42730e7468fSPeter Dunlap static void
42830e7468fSPeter Dunlap iscsi_cmd_rsp_cmd_status(iscsi_cmd_t *icmdp, iscsi_scsi_rsp_hdr_t *issrhp,
42930e7468fSPeter Dunlap     uint8_t *data)
43030e7468fSPeter Dunlap {
431*5279807dSJack Meng 	int32_t			dlength		= 0;
43230e7468fSPeter Dunlap 	struct scsi_arq_status	*arqstat	= NULL;
43330e7468fSPeter Dunlap 	size_t			senselen	= 0;
434*5279807dSJack Meng 	int32_t			statuslen	= 0;
435*5279807dSJack Meng 	int32_t			senselen_to	= 0;
43630e7468fSPeter Dunlap 	struct scsi_pkt		*pkt;
43730e7468fSPeter Dunlap 
43830e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
43930e7468fSPeter Dunlap 	dlength = n2h24(issrhp->dlength);
440fcf3ce44SJohn Forte 
441fcf3ce44SJohn Forte 	/*
44230e7468fSPeter Dunlap 	 * Process iSCSI Cmd Response Status
44330e7468fSPeter Dunlap 	 * RFC 3720 Sectionn 10.4.2.
444fcf3ce44SJohn Forte 	 */
44530e7468fSPeter Dunlap 	switch (issrhp->cmd_status & STATUS_MASK) {
44630e7468fSPeter Dunlap 	case STATUS_GOOD:
44730e7468fSPeter Dunlap 		/* pass SCSI status up stack */
44830e7468fSPeter Dunlap 		if (pkt->pkt_scbp) {
44930e7468fSPeter Dunlap 			pkt->pkt_scbp[0] = issrhp->cmd_status;
45030e7468fSPeter Dunlap 		}
45130e7468fSPeter Dunlap 		break;
45230e7468fSPeter Dunlap 	case STATUS_CHECK:
45330e7468fSPeter Dunlap 		/*
45430e7468fSPeter Dunlap 		 * Verify we received a sense buffer and
45530e7468fSPeter Dunlap 		 * that there is the correct amount of
45630e7468fSPeter Dunlap 		 * request sense space to copy it to.
45730e7468fSPeter Dunlap 		 */
45830e7468fSPeter Dunlap 		if ((dlength > 1) &&
45930e7468fSPeter Dunlap 		    (pkt->pkt_scbp != NULL) &&
46030e7468fSPeter Dunlap 		    (icmdp->cmd_un.scsi.statuslen >=
46130e7468fSPeter Dunlap 		    sizeof (struct scsi_arq_status))) {
462fcf3ce44SJohn Forte 			/*
46330e7468fSPeter Dunlap 			 * If a bad command status is received we
46430e7468fSPeter Dunlap 			 * need to reset the pkt_resid to zero.
46530e7468fSPeter Dunlap 			 * The target driver compares its value
46630e7468fSPeter Dunlap 			 * before checking other error flags.
46730e7468fSPeter Dunlap 			 * (ex. check conditions)
468fcf3ce44SJohn Forte 			 */
46930e7468fSPeter Dunlap 			pkt->pkt_resid = 0;
47030e7468fSPeter Dunlap 
47130e7468fSPeter Dunlap 			/* get sense length from first 2 bytes */
47230e7468fSPeter Dunlap 			senselen = ((data[0] << 8) | data[1]) &
47330e7468fSPeter Dunlap 			    (size_t)0xFFFF;
47430e7468fSPeter Dunlap 			ISCSI_IO_LOG(CE_NOTE,
47530e7468fSPeter Dunlap 			    "DEBUG: iscsi_rx_cmd_rsp_cmd_status status_check: "
47630e7468fSPeter Dunlap 			    "dlen: %d scbp: %p statuslen: %d arq: %d senselen:"
47730e7468fSPeter Dunlap 			    " %lu", dlength, (void *)pkt->pkt_scbp,
47830e7468fSPeter Dunlap 			    icmdp->cmd_un.scsi.statuslen,
47930e7468fSPeter Dunlap 			    (int)sizeof (struct scsi_arq_status),
48030e7468fSPeter Dunlap 			    senselen);
48130e7468fSPeter Dunlap 
48230e7468fSPeter Dunlap 			/* Sanity-check on the sense length */
48330e7468fSPeter Dunlap 			if ((senselen + 2) > dlength) {
48430e7468fSPeter Dunlap 				senselen = dlength - 2;
485fcf3ce44SJohn Forte 			}
486fcf3ce44SJohn Forte 
48730e7468fSPeter Dunlap 			/*
48830e7468fSPeter Dunlap 			 * If there was a Data Digest error then
48930e7468fSPeter Dunlap 			 * the sense data cannot be trusted.
49030e7468fSPeter Dunlap 			 */
49130e7468fSPeter Dunlap 			if (icmdp->cmd_crc_error_seen) {
49230e7468fSPeter Dunlap 				senselen = 0;
49330e7468fSPeter Dunlap 			}
494fcf3ce44SJohn Forte 
49530e7468fSPeter Dunlap 			/* automatic request sense */
49630e7468fSPeter Dunlap 			arqstat =
49730e7468fSPeter Dunlap 			    (struct scsi_arq_status *)pkt->pkt_scbp;
49830e7468fSPeter Dunlap 
49930e7468fSPeter Dunlap 			/* pass SCSI status up stack */
50030e7468fSPeter Dunlap 			*((uchar_t *)&arqstat->sts_status) =
50130e7468fSPeter Dunlap 			    issrhp->cmd_status;
502fcf3ce44SJohn Forte 
503fcf3ce44SJohn Forte 			/*
50430e7468fSPeter Dunlap 			 * Set the status for the automatic
50530e7468fSPeter Dunlap 			 * request sense command
506fcf3ce44SJohn Forte 			 */
50730e7468fSPeter Dunlap 			arqstat->sts_rqpkt_state = (STATE_GOT_BUS |
50830e7468fSPeter Dunlap 			    STATE_GOT_TARGET | STATE_SENT_CMD |
50930e7468fSPeter Dunlap 			    STATE_XFERRED_DATA | STATE_GOT_STATUS |
51030e7468fSPeter Dunlap 			    STATE_ARQ_DONE);
511fcf3ce44SJohn Forte 
51230e7468fSPeter Dunlap 			*((uchar_t *)&arqstat->sts_rqpkt_status) =
51330e7468fSPeter Dunlap 			    STATUS_GOOD;
514fcf3ce44SJohn Forte 
51530e7468fSPeter Dunlap 			arqstat->sts_rqpkt_reason = CMD_CMPLT;
51630e7468fSPeter Dunlap 			statuslen = icmdp->cmd_un.scsi.statuslen;
517fcf3ce44SJohn Forte 
51830e7468fSPeter Dunlap 			if (senselen == 0) {
51930e7468fSPeter Dunlap 				/* auto request sense failed */
52030e7468fSPeter Dunlap 				arqstat->sts_rqpkt_status.sts_chk = 1;
52130e7468fSPeter Dunlap 				arqstat->sts_rqpkt_resid = statuslen;
52230e7468fSPeter Dunlap 			} else if (senselen < statuslen) {
52330e7468fSPeter Dunlap 				/* auto request sense short */
52430e7468fSPeter Dunlap 				arqstat->sts_rqpkt_resid = statuslen - senselen;
52530e7468fSPeter Dunlap 			} else {
52630e7468fSPeter Dunlap 				/* auto request sense complete */
52730e7468fSPeter Dunlap 				arqstat->sts_rqpkt_resid = 0;
52830e7468fSPeter Dunlap 			}
52930e7468fSPeter Dunlap 			arqstat->sts_rqpkt_statistics = 0;
53030e7468fSPeter Dunlap 			pkt->pkt_state |= STATE_ARQ_DONE;
531fcf3ce44SJohn Forte 
53230e7468fSPeter Dunlap 			if (icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_XARQ) {
53330e7468fSPeter Dunlap 				pkt->pkt_state |= STATE_XARQ_DONE;
53430e7468fSPeter Dunlap 			}
53530e7468fSPeter Dunlap 
536*5279807dSJack Meng 			senselen_to =  pkt->pkt_scblen -
537*5279807dSJack Meng 			    sizeof (struct scsi_arq_status) +
538*5279807dSJack Meng 			    sizeof (struct scsi_extended_sense);
539*5279807dSJack Meng 
54030e7468fSPeter Dunlap 			/* copy auto request sense */
541*5279807dSJack Meng 			dlength = min(senselen, senselen_to);
542*5279807dSJack Meng 			if (dlength > 0) {
54330e7468fSPeter Dunlap 				bcopy(&data[2], (uchar_t *)&arqstat->
54430e7468fSPeter Dunlap 				    sts_sensedata, dlength);
54530e7468fSPeter Dunlap 			}
54630e7468fSPeter Dunlap 			break;
547fcf3ce44SJohn Forte 		}
54830e7468fSPeter Dunlap 		/* FALLTHRU */
54930e7468fSPeter Dunlap 	case STATUS_BUSY:
55030e7468fSPeter Dunlap 	case STATUS_RESERVATION_CONFLICT:
55130e7468fSPeter Dunlap 	case STATUS_QFULL:
55230e7468fSPeter Dunlap 	case STATUS_ACA_ACTIVE:
55330e7468fSPeter Dunlap 	default:
55430e7468fSPeter Dunlap 		/*
55530e7468fSPeter Dunlap 		 * If a bad command status is received we need to
55630e7468fSPeter Dunlap 		 * reset the pkt_resid to zero.  The target driver
55730e7468fSPeter Dunlap 		 * compares its value before checking other error
55830e7468fSPeter Dunlap 		 * flags. (ex. check conditions)
55930e7468fSPeter Dunlap 		 */
56030e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE,
56130e7468fSPeter Dunlap 		    "DEBUG: iscsi_rx_cmd_rsp_cmd_status: status: "
56230e7468fSPeter Dunlap 		    "%d cmd_status: %d dlen: %u scbp: %p statuslen: %d "
56330e7468fSPeter Dunlap 		    "arg_len: %d", issrhp->cmd_status & STATUS_MASK,
56430e7468fSPeter Dunlap 		    issrhp->cmd_status, dlength, (void *)pkt->pkt_scbp,
56530e7468fSPeter Dunlap 		    icmdp->cmd_un.scsi.statuslen,
56630e7468fSPeter Dunlap 		    (int)sizeof (struct scsi_arq_status));
56730e7468fSPeter Dunlap 		pkt->pkt_resid = 0;
56830e7468fSPeter Dunlap 		/* pass SCSI status up stack */
56930e7468fSPeter Dunlap 		if (pkt->pkt_scbp) {
57030e7468fSPeter Dunlap 			pkt->pkt_scbp[0] = issrhp->cmd_status;
57130e7468fSPeter Dunlap 		}
57230e7468fSPeter Dunlap 	}
57330e7468fSPeter Dunlap }
574fcf3ce44SJohn Forte 
57530e7468fSPeter Dunlap /*
57630e7468fSPeter Dunlap  * iscsi_rx_process_login_pdup - Process login response PDU.  This function
57730e7468fSPeter Dunlap  * copies the data into the connection context so that the login code can
57830e7468fSPeter Dunlap  * interpret it.
57930e7468fSPeter Dunlap  */
58030e7468fSPeter Dunlap 
58130e7468fSPeter Dunlap idm_status_t
58230e7468fSPeter Dunlap iscsi_rx_process_login_pdu(idm_conn_t *ic, idm_pdu_t *pdu)
58330e7468fSPeter Dunlap {
58430e7468fSPeter Dunlap 	iscsi_conn_t 		*icp;
58530e7468fSPeter Dunlap 
58630e7468fSPeter Dunlap 	icp = ic->ic_handle;
58730e7468fSPeter Dunlap 
58830e7468fSPeter Dunlap 	/*
58930e7468fSPeter Dunlap 	 * Copy header and data into connection structure so iscsi_login()
59030e7468fSPeter Dunlap 	 * can process it.
59130e7468fSPeter Dunlap 	 */
59230e7468fSPeter Dunlap 	mutex_enter(&icp->conn_login_mutex);
59330e7468fSPeter Dunlap 	/*
59430e7468fSPeter Dunlap 	 * If conn_login_state != LOGIN_TX then we are not ready to handle
59530e7468fSPeter Dunlap 	 * this login response and we should just  drop it.
59630e7468fSPeter Dunlap 	 */
59730e7468fSPeter Dunlap 	if (icp->conn_login_state == LOGIN_TX) {
59830e7468fSPeter Dunlap 		icp->conn_login_datalen = pdu->isp_datalen;
59930e7468fSPeter Dunlap 		bcopy(pdu->isp_hdr, &icp->conn_login_resp_hdr,
60030e7468fSPeter Dunlap 		    sizeof (iscsi_hdr_t));
60130e7468fSPeter Dunlap 		/*
60230e7468fSPeter Dunlap 		 * Login code is sloppy with it's NULL handling so make sure
60330e7468fSPeter Dunlap 		 * we don't leave any stale data in there.
60430e7468fSPeter Dunlap 		 */
60530e7468fSPeter Dunlap 		bzero(icp->conn_login_data, icp->conn_login_max_data_length);
60630e7468fSPeter Dunlap 		bcopy(pdu->isp_data, icp->conn_login_data,
60730e7468fSPeter Dunlap 		    MIN(pdu->isp_datalen, icp->conn_login_max_data_length));
60830e7468fSPeter Dunlap 		iscsi_login_update_state_locked(icp, LOGIN_RX);
609fcf3ce44SJohn Forte 	}
61030e7468fSPeter Dunlap 	mutex_exit(&icp->conn_login_mutex);
611fcf3ce44SJohn Forte 
61230e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
613fcf3ce44SJohn Forte }
614fcf3ce44SJohn Forte 
615fcf3ce44SJohn Forte /*
616fcf3ce44SJohn Forte  * iscsi_rx_process_cmd_rsp - Process received scsi command response.  This
617fcf3ce44SJohn Forte  * will contain sense data if the command was not successful.  This data needs
618fcf3ce44SJohn Forte  * to be copied into the scsi_pkt.  Otherwise we just complete the IO.
619fcf3ce44SJohn Forte  */
62030e7468fSPeter Dunlap static idm_status_t
62130e7468fSPeter Dunlap iscsi_rx_process_cmd_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
622fcf3ce44SJohn Forte {
62330e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
62430e7468fSPeter Dunlap 	iscsi_sess_t		*isp	= icp->conn_sess;
62530e7468fSPeter Dunlap 	iscsi_scsi_rsp_hdr_t	*issrhp	= (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
62630e7468fSPeter Dunlap 	uint8_t			*data	= pdu->isp_data;
62730e7468fSPeter Dunlap 	iscsi_cmd_t		*icmdp	= NULL;
62830e7468fSPeter Dunlap 	struct scsi_pkt		*pkt	= NULL;
62930e7468fSPeter Dunlap 	idm_status_t		rval;
63030e7468fSPeter Dunlap 	struct buf		*bp;
631fcf3ce44SJohn Forte 
632fcf3ce44SJohn Forte 	/* make sure we get status in order */
63330e7468fSPeter Dunlap 	mutex_enter(&icp->conn_queue_active.mutex);
63430e7468fSPeter Dunlap 
63530e7468fSPeter Dunlap 	if ((rval = iscsi_rx_chk(icp, isp, issrhp,
63630e7468fSPeter Dunlap 	    &icmdp)) != IDM_STATUS_SUCCESS) {
63730e7468fSPeter Dunlap 		if (icmdp != NULL) {
63830e7468fSPeter Dunlap 			iscsi_task_cleanup(issrhp->opcode, icmdp);
63930e7468fSPeter Dunlap 		}
64030e7468fSPeter Dunlap 		mutex_exit(&icp->conn_queue_active.mutex);
64130e7468fSPeter Dunlap 		return (rval);
642fcf3ce44SJohn Forte 	}
643fcf3ce44SJohn Forte 
64430e7468fSPeter Dunlap 	/*
64530e7468fSPeter Dunlap 	 * If we are in "idm aborting" state then we shouldn't continue
64630e7468fSPeter Dunlap 	 * to process this command.  By definition this command is no longer
64730e7468fSPeter Dunlap 	 * on the active queue so we shouldn't try to remove it either.
64830e7468fSPeter Dunlap 	 */
64930e7468fSPeter Dunlap 	mutex_enter(&icmdp->cmd_mutex);
65030e7468fSPeter Dunlap 	if (icmdp->cmd_state == ISCSI_CMD_STATE_IDM_ABORTING) {
65130e7468fSPeter Dunlap 		mutex_exit(&icmdp->cmd_mutex);
652fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
65330e7468fSPeter Dunlap 		return (IDM_STATUS_SUCCESS);
654fcf3ce44SJohn Forte 	}
65530e7468fSPeter Dunlap 	mutex_exit(&icmdp->cmd_mutex);
656fcf3ce44SJohn Forte 
65730e7468fSPeter Dunlap 	/* Get the IDM buffer and bytes transferred */
65830e7468fSPeter Dunlap 	bp = icmdp->cmd_un.scsi.bp;
65930e7468fSPeter Dunlap 	if (ic->ic_conn_flags & IDM_CONN_USE_SCOREBOARD) {
66030e7468fSPeter Dunlap 		/* Transport tracks bytes transferred so use those counts */
66130e7468fSPeter Dunlap 		if (bp && (bp->b_flags & B_READ)) {
66230e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.data_transferred +=
66330e7468fSPeter Dunlap 			    icmdp->cmd_itp->idt_rx_bytes;
66430e7468fSPeter Dunlap 		} else {
66530e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.data_transferred +=
66630e7468fSPeter Dunlap 			    icmdp->cmd_itp->idt_tx_bytes;
66730e7468fSPeter Dunlap 		}
66830e7468fSPeter Dunlap 	} else {
66930e7468fSPeter Dunlap 		/*
67030e7468fSPeter Dunlap 		 * Some transports cannot track the bytes transferred on
67130e7468fSPeter Dunlap 		 * the initiator side (like iSER) so we have to use the
67230e7468fSPeter Dunlap 		 * status info.  If the response field indicates that
67330e7468fSPeter Dunlap 		 * the command actually completed then we will assume
67430e7468fSPeter Dunlap 		 * the data_transferred value represents the entire buffer
67530e7468fSPeter Dunlap 		 * unless the resid field says otherwise.  This is a bit
67630e7468fSPeter Dunlap 		 * unintuitive but it's really impossible to know what
67730e7468fSPeter Dunlap 		 * has been transferred without detailed consideration
67830e7468fSPeter Dunlap 		 * of the SCSI status and sense key and that is outside
67930e7468fSPeter Dunlap 		 * the scope of the transport.  Instead the target/class driver
68030e7468fSPeter Dunlap 		 * can consider these values along with the resid and figure
68130e7468fSPeter Dunlap 		 * it out.  The data_transferred concept is just belt and
68230e7468fSPeter Dunlap 		 * suspenders anyway -- RFC 3720 actually explicitly rejects
68330e7468fSPeter Dunlap 		 * scoreboarding ("Initiators SHOULD NOT keep track of the
68430e7468fSPeter Dunlap 		 * data transferred to or from the target (scoreboarding)")
68530e7468fSPeter Dunlap 		 * perhaps for this very reason.
68630e7468fSPeter Dunlap 		 */
68730e7468fSPeter Dunlap 		if (issrhp->response != 0) {
68830e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.data_transferred = 0;
68930e7468fSPeter Dunlap 		} else {
69030e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.data_transferred =
69130e7468fSPeter Dunlap 			    (bp == NULL) ? 0 : bp->b_bcount;
69230e7468fSPeter Dunlap 			if (issrhp->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
69330e7468fSPeter Dunlap 				icmdp->cmd_un.scsi.data_transferred -=
69430e7468fSPeter Dunlap 				    ntohl(issrhp->residual_count);
69530e7468fSPeter Dunlap 			}
69630e7468fSPeter Dunlap 		}
69730e7468fSPeter Dunlap 	}
698fcf3ce44SJohn Forte 
69930e7468fSPeter Dunlap 	ISCSI_CHECK_SCSI_READ(icmdp, issrhp,
70030e7468fSPeter Dunlap 	    icmdp->cmd_un.scsi.data_transferred,
70130e7468fSPeter Dunlap 	    BP_CHECK_THOROUGH);
70230e7468fSPeter Dunlap 
70330e7468fSPeter Dunlap 	ISCSI_IO_LOG(CE_NOTE, "DEBUG: rx_process_cmd_rsp: ic: %p pdu: %p itt:"
70430e7468fSPeter Dunlap 	    " %x expcmdsn: %x sess_cmd: %x sess_expcmdsn: %x data_transfered:"
70530e7468fSPeter Dunlap 	    " %lu ibp: %p obp: %p", (void *)ic, (void *)pdu, issrhp->itt,
70630e7468fSPeter Dunlap 	    issrhp->expcmdsn, isp->sess_cmdsn, isp->sess_expcmdsn,
70730e7468fSPeter Dunlap 	    icmdp->cmd_un.scsi.data_transferred,
70830e7468fSPeter Dunlap 	    (void *)icmdp->cmd_un.scsi.ibp_ibuf,
70930e7468fSPeter Dunlap 	    (void *)icmdp->cmd_un.scsi.ibp_obuf);
71030e7468fSPeter Dunlap 
71130e7468fSPeter Dunlap 	iscsi_task_cleanup(issrhp->opcode, icmdp);
712fcf3ce44SJohn Forte 
713fcf3ce44SJohn Forte 	if (issrhp->response) {
714fcf3ce44SJohn Forte 		/* The target failed the command. */
71530e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE, "DEBUG: rx_process_cmd_rsp: ic: %p pdu:"
71630e7468fSPeter Dunlap 		    " %p response: %d bcount: %lu", (void *)ic, (void *)pdu,
71730e7468fSPeter Dunlap 		    issrhp->response, icmdp->cmd_un.scsi.bp->b_bcount);
71830e7468fSPeter Dunlap 		pkt = icmdp->cmd_un.scsi.pkt;
719fcf3ce44SJohn Forte 		pkt->pkt_reason = CMD_TRAN_ERR;
720fcf3ce44SJohn Forte 		if (icmdp->cmd_un.scsi.bp) {
721fcf3ce44SJohn Forte 			pkt->pkt_resid = icmdp->cmd_un.scsi.bp->b_bcount;
722fcf3ce44SJohn Forte 		} else {
723fcf3ce44SJohn Forte 			pkt->pkt_resid = 0;
724fcf3ce44SJohn Forte 		}
725fcf3ce44SJohn Forte 	} else {
726fcf3ce44SJohn Forte 		/* success */
72730e7468fSPeter Dunlap 		iscsi_cmd_rsp_chk(icmdp, issrhp);
72830e7468fSPeter Dunlap 		iscsi_cmd_rsp_cmd_status(icmdp, issrhp, data);
72930e7468fSPeter Dunlap 	}
730fcf3ce44SJohn Forte 
73130e7468fSPeter Dunlap 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp);
73230e7468fSPeter Dunlap 	mutex_exit(&icp->conn_queue_active.mutex);
73330e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
73430e7468fSPeter Dunlap }
735fcf3ce44SJohn Forte 
73630e7468fSPeter Dunlap static void
73730e7468fSPeter Dunlap iscsi_data_rsp_pkt(iscsi_cmd_t *icmdp, iscsi_data_rsp_hdr_t *idrhp)
73830e7468fSPeter Dunlap {
73930e7468fSPeter Dunlap 	struct buf		*bp	= NULL;
74030e7468fSPeter Dunlap 	size_t			data_transferred;
74130e7468fSPeter Dunlap 	struct scsi_pkt		*pkt;
742fcf3ce44SJohn Forte 
74330e7468fSPeter Dunlap 	bp = icmdp->cmd_un.scsi.bp;
74430e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
74530e7468fSPeter Dunlap 	data_transferred = icmdp->cmd_un.scsi.data_transferred;
74630e7468fSPeter Dunlap 	/*
74730e7468fSPeter Dunlap 	 * The command* must be completed now, since we won't get a command
74830e7468fSPeter Dunlap 	 * response PDU. The cmd_status and residual_count are
74930e7468fSPeter Dunlap 	 * not meaningful unless status_present is set.
75030e7468fSPeter Dunlap 	 */
75130e7468fSPeter Dunlap 	pkt->pkt_resid = 0;
75230e7468fSPeter Dunlap 	/* Check the residual count */
75330e7468fSPeter Dunlap 	if (bp && (data_transferred != bp->b_bcount)) {
754fcf3ce44SJohn Forte 		/*
75530e7468fSPeter Dunlap 		 * We didn't xfer the expected amount of data -
75630e7468fSPeter Dunlap 		 * the residual_count in the header is only valid
75730e7468fSPeter Dunlap 		 * if the underflow flag is set.
758fcf3ce44SJohn Forte 		 */
75930e7468fSPeter Dunlap 		if (idrhp->flags & ISCSI_FLAG_DATA_UNDERFLOW) {
76030e7468fSPeter Dunlap 			pkt->pkt_resid = ntohl(idrhp->residual_count);
76130e7468fSPeter Dunlap 			ISCSI_IO_LOG(CE_NOTE, "DEBUG: iscsi_data_rsp_pkt: "
76230e7468fSPeter Dunlap 			    "underflow: itt: %d "
76330e7468fSPeter Dunlap 			    "transferred: %lu count: %lu", idrhp->itt,
76430e7468fSPeter Dunlap 			    data_transferred, bp->b_bcount);
76530e7468fSPeter Dunlap 		} else {
76630e7468fSPeter Dunlap 			if (bp->b_bcount > data_transferred) {
76730e7468fSPeter Dunlap 				/* Some data fell on the floor somehw */
76830e7468fSPeter Dunlap 				ISCSI_IO_LOG(CE_NOTE, "DEBUG: "
76930e7468fSPeter Dunlap 				    "iscsi_data_rsp_pkt: data fell: itt: %d "
77030e7468fSPeter Dunlap 				    "transferred: %lu count: %lu", idrhp->itt,
77130e7468fSPeter Dunlap 				    data_transferred, bp->b_bcount);
77230e7468fSPeter Dunlap 				pkt->pkt_resid =
77330e7468fSPeter Dunlap 				    bp->b_bcount - data_transferred;
774fcf3ce44SJohn Forte 			}
77530e7468fSPeter Dunlap 		}
77630e7468fSPeter Dunlap 	}
777fcf3ce44SJohn Forte 
77830e7468fSPeter Dunlap 	pkt->pkt_reason = CMD_CMPLT;
77930e7468fSPeter Dunlap 	pkt->pkt_state |= (STATE_XFERRED_DATA | STATE_GOT_STATUS);
780fcf3ce44SJohn Forte 
78130e7468fSPeter Dunlap 	if (((idrhp->cmd_status & STATUS_MASK) != STATUS_GOOD) &&
78230e7468fSPeter Dunlap 	    (icmdp->cmd_un.scsi.statuslen >=
78330e7468fSPeter Dunlap 	    sizeof (struct scsi_arq_status)) && pkt->pkt_scbp) {
784fcf3ce44SJohn Forte 
78530e7468fSPeter Dunlap 		/*
78630e7468fSPeter Dunlap 		 * Not supposed to get exception status here!
78730e7468fSPeter Dunlap 		 * We have no request sense data so just do the
78830e7468fSPeter Dunlap 		 * best we can
78930e7468fSPeter Dunlap 		 */
79030e7468fSPeter Dunlap 		struct scsi_arq_status *arqstat =
79130e7468fSPeter Dunlap 		    (struct scsi_arq_status *)pkt->pkt_scbp;
792fcf3ce44SJohn Forte 
793fcf3ce44SJohn Forte 
79430e7468fSPeter Dunlap 		bzero(arqstat, sizeof (struct scsi_arq_status));
795fcf3ce44SJohn Forte 
79630e7468fSPeter Dunlap 		*((uchar_t *)&arqstat->sts_status) =
79730e7468fSPeter Dunlap 		    idrhp->cmd_status;
798fcf3ce44SJohn Forte 
79930e7468fSPeter Dunlap 		arqstat->sts_rqpkt_resid =
80030e7468fSPeter Dunlap 		    sizeof (struct scsi_extended_sense);
80130e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE, "DEBUG: iscsi_data_rsp_pkt: "
80230e7468fSPeter Dunlap 		    "exception status: itt: %d resid: %d",
80330e7468fSPeter Dunlap 		    idrhp->itt, arqstat->sts_rqpkt_resid);
804b4243054SSheng-Liang Eric Zhang 
80530e7468fSPeter Dunlap 	} else if (pkt->pkt_scbp) {
80630e7468fSPeter Dunlap 		/* just pass along the status we got */
80730e7468fSPeter Dunlap 		pkt->pkt_scbp[0] = idrhp->cmd_status;
808fcf3ce44SJohn Forte 	}
809fcf3ce44SJohn Forte }
810fcf3ce44SJohn Forte 
811fcf3ce44SJohn Forte /*
81230e7468fSPeter Dunlap  * iscsi_rx_process_data_rsp -
81330e7468fSPeter Dunlap  * This currently processes the final data sequence denoted by the data response
81430e7468fSPeter Dunlap  * PDU Status bit being set.  We will not receive the SCSI response.
81530e7468fSPeter Dunlap  * This bit denotes that the PDU is the successful completion of the
81630e7468fSPeter Dunlap  * command.
817fcf3ce44SJohn Forte  */
81830e7468fSPeter Dunlap static idm_status_t
81930e7468fSPeter Dunlap iscsi_rx_process_data_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
820fcf3ce44SJohn Forte {
82130e7468fSPeter Dunlap 	iscsi_sess_t		*isp	= NULL;
82230e7468fSPeter Dunlap 	iscsi_data_rsp_hdr_t	*idrhp	= (iscsi_data_rsp_hdr_t *)pdu->isp_hdr;
82330e7468fSPeter Dunlap 	iscsi_cmd_t		*icmdp	= NULL;
82430e7468fSPeter Dunlap 	struct buf		*bp	= NULL;
82530e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
82630e7468fSPeter Dunlap 	idm_buf_t		*ibp;
82730e7468fSPeter Dunlap 	idm_status_t		rval;
828fcf3ce44SJohn Forte 
829fcf3ce44SJohn Forte 
83030e7468fSPeter Dunlap 	/* should only call this when the data rsp contains final rsp */
83130e7468fSPeter Dunlap 	ASSERT(idrhp->flags & ISCSI_FLAG_DATA_STATUS);
83230e7468fSPeter Dunlap 	isp = icp->conn_sess;
83330e7468fSPeter Dunlap 
834fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
83530e7468fSPeter Dunlap 	if ((rval = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)idrhp,
83630e7468fSPeter Dunlap 	    &icmdp)) != IDM_STATUS_SUCCESS) {
83730e7468fSPeter Dunlap 		if (icmdp != NULL) {
83830e7468fSPeter Dunlap 			iscsi_task_cleanup(idrhp->opcode, icmdp);
83930e7468fSPeter Dunlap 		}
840fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
84130e7468fSPeter Dunlap 		return (rval);
842fcf3ce44SJohn Forte 	}
843fcf3ce44SJohn Forte 
84430e7468fSPeter Dunlap 	/*
84530e7468fSPeter Dunlap 	 * If we are in "idm aborting" state then we shouldn't continue
84630e7468fSPeter Dunlap 	 * to process this command.  By definition this command is no longer
84730e7468fSPeter Dunlap 	 * on the active queue so we shouldn't try to remove it either.
84830e7468fSPeter Dunlap 	 */
849fcf3ce44SJohn Forte 	mutex_enter(&icmdp->cmd_mutex);
85030e7468fSPeter Dunlap 	if (icmdp->cmd_state == ISCSI_CMD_STATE_IDM_ABORTING) {
85130e7468fSPeter Dunlap 		mutex_exit(&icmdp->cmd_mutex);
85230e7468fSPeter Dunlap 		mutex_exit(&icp->conn_queue_active.mutex);
85330e7468fSPeter Dunlap 		return (IDM_STATUS_SUCCESS);
85430e7468fSPeter Dunlap 	}
85530e7468fSPeter Dunlap 	mutex_exit(&icmdp->cmd_mutex);
85630e7468fSPeter Dunlap 
85730e7468fSPeter Dunlap 	/*
85830e7468fSPeter Dunlap 	 * Holding the pending/active queue locks across the
85930e7468fSPeter Dunlap 	 * iscsi_rx_data call later in this function may cause
86030e7468fSPeter Dunlap 	 * deadlock during fault injections.  Instead remove
86130e7468fSPeter Dunlap 	 * the cmd from the active queue and release the locks.
86230e7468fSPeter Dunlap 	 * Then before returning or completing the command
86330e7468fSPeter Dunlap 	 * return the cmd to the active queue and reacquire
86430e7468fSPeter Dunlap 	 * the locks.
86530e7468fSPeter Dunlap 	 */
86630e7468fSPeter Dunlap 	iscsi_dequeue_active_cmd(icp, icmdp);
86730e7468fSPeter Dunlap 
86830e7468fSPeter Dunlap 	mutex_exit(&icp->conn_queue_active.mutex);
869fcf3ce44SJohn Forte 
87030e7468fSPeter Dunlap 	/* shorthand some values */
871fcf3ce44SJohn Forte 	bp = icmdp->cmd_un.scsi.bp;
872fcf3ce44SJohn Forte 
87330e7468fSPeter Dunlap 	/*
87430e7468fSPeter Dunlap 	 * some poorly behaved targets have been observed
87530e7468fSPeter Dunlap 	 * sending data-in pdu's during a write operation
87630e7468fSPeter Dunlap 	 */
87730e7468fSPeter Dunlap 	if (bp != NULL) {
87830e7468fSPeter Dunlap 		if (!(bp->b_flags & B_READ)) {
87930e7468fSPeter Dunlap 			cmn_err(CE_WARN,
88030e7468fSPeter Dunlap 			    "iscsi connection(%u) protocol error - "
88130e7468fSPeter Dunlap 			    "received data response during write operation "
88230e7468fSPeter Dunlap 			    "itt:0x%x",
88330e7468fSPeter Dunlap 			    icp->conn_oid, idrhp->itt);
88430e7468fSPeter Dunlap 			mutex_enter(&icp->conn_queue_active.mutex);
88530e7468fSPeter Dunlap 			iscsi_enqueue_active_cmd(icp, icmdp);
88630e7468fSPeter Dunlap 			mutex_exit(&icp->conn_queue_active.mutex);
88730e7468fSPeter Dunlap 			return (IDM_STATUS_PROTOCOL_ERROR);
88830e7468fSPeter Dunlap 		}
88930e7468fSPeter Dunlap 	}
89030e7468fSPeter Dunlap 
89130e7468fSPeter Dunlap 	ibp = icmdp->cmd_un.scsi.ibp_ibuf;
89230e7468fSPeter Dunlap 	if (ibp == NULL) {
89330e7468fSPeter Dunlap 		/*
89430e7468fSPeter Dunlap 		 * After the check of bp above we *should* have a corresponding
89530e7468fSPeter Dunlap 		 * idm_buf_t (ibp).  It's possible that the original call
89630e7468fSPeter Dunlap 		 * to idm_buf_alloc failed due to a pending connection state
89730e7468fSPeter Dunlap 		 * transition in which case this value can be NULL.  It's
89830e7468fSPeter Dunlap 		 * highly unlikely that the connection would be shutting down
89930e7468fSPeter Dunlap 		 * *and* we manage to process a data response and get to this
90030e7468fSPeter Dunlap 		 * point in the code but just in case we should check for it.
90130e7468fSPeter Dunlap 		 * This isn't really a protocol error -- we are almost certainly
90230e7468fSPeter Dunlap 		 * closing the connection anyway so just return a generic error.
90330e7468fSPeter Dunlap 		 */
90430e7468fSPeter Dunlap 		mutex_enter(&icp->conn_queue_active.mutex);
90530e7468fSPeter Dunlap 		iscsi_enqueue_active_cmd(icp, icmdp);
90630e7468fSPeter Dunlap 		mutex_exit(&icp->conn_queue_active.mutex);
90730e7468fSPeter Dunlap 		return (IDM_STATUS_FAIL);
90830e7468fSPeter Dunlap 	}
90930e7468fSPeter Dunlap 
91030e7468fSPeter Dunlap 	if (ic->ic_conn_flags & IDM_CONN_USE_SCOREBOARD) {
91130e7468fSPeter Dunlap 		icmdp->cmd_un.scsi.data_transferred =
91230e7468fSPeter Dunlap 		    icmdp->cmd_itp->idt_rx_bytes;
913fcf3ce44SJohn Forte 	} else {
91430e7468fSPeter Dunlap 		icmdp->cmd_un.scsi.data_transferred = bp->b_bcount;
91530e7468fSPeter Dunlap 		if (idrhp->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
91630e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.data_transferred -=
91730e7468fSPeter Dunlap 			    ntohl(idrhp->residual_count);
91830e7468fSPeter Dunlap 		}
919fcf3ce44SJohn Forte 	}
920fcf3ce44SJohn Forte 
92130e7468fSPeter Dunlap 	ISCSI_IO_LOG(CE_NOTE, "DEBUG: rx_process_data_rsp: icp: %p pdu: %p "
92230e7468fSPeter Dunlap 	    "itt: %d ibp: %p icmdp: %p xfer_len: %lu transferred: %lu dlen: %u",
92330e7468fSPeter Dunlap 	    (void *)icp, (void *)pdu, idrhp->itt, (void *)bp, (void *)icmdp,
92430e7468fSPeter Dunlap 	    (ibp == NULL) ? 0 : ibp->idb_xfer_len,
92530e7468fSPeter Dunlap 	    icmdp->cmd_un.scsi.data_transferred,
92630e7468fSPeter Dunlap 	    n2h24(idrhp->dlength));
92730e7468fSPeter Dunlap 
92830e7468fSPeter Dunlap 	iscsi_task_cleanup(idrhp->opcode, icmdp);
92930e7468fSPeter Dunlap 
93030e7468fSPeter Dunlap 	iscsi_data_rsp_pkt(icmdp, idrhp);
93130e7468fSPeter Dunlap 
93230e7468fSPeter Dunlap 	mutex_enter(&icp->conn_queue_active.mutex);
93330e7468fSPeter Dunlap 	iscsi_enqueue_active_cmd(icp, icmdp);
93430e7468fSPeter Dunlap 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp);
935fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_queue_active.mutex);
936fcf3ce44SJohn Forte 
93730e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
938fcf3ce44SJohn Forte }
939fcf3ce44SJohn Forte 
940fcf3ce44SJohn Forte /*
941fcf3ce44SJohn Forte  * iscsi_rx_process_nop - Process a received nop.  If nop is in response
942fcf3ce44SJohn Forte  * to a ping we sent update stats.  If initiated by the target we need
943fcf3ce44SJohn Forte  * to response back to the target with a nop.  Schedule the response.
944fcf3ce44SJohn Forte  */
945fcf3ce44SJohn Forte /* ARGSUSED */
94630e7468fSPeter Dunlap static idm_status_t
94730e7468fSPeter Dunlap iscsi_rx_process_nop(idm_conn_t *ic, idm_pdu_t *pdu)
948fcf3ce44SJohn Forte {
949fcf3ce44SJohn Forte 	iscsi_sess_t		*isp	= NULL;
95030e7468fSPeter Dunlap 	iscsi_nop_in_hdr_t	*inihp	= (iscsi_nop_in_hdr_t *)pdu->isp_hdr;
951fcf3ce44SJohn Forte 	iscsi_cmd_t		*icmdp	= NULL;
95230e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
953fcf3ce44SJohn Forte 
954fcf3ce44SJohn Forte 	if (icp->conn_expstatsn != ntohl(inihp->statsn)) {
95530e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi connection(%u/%x) protocol error - "
956fcf3ce44SJohn Forte 		    "received status out of order itt:0x%x statsn:0x%x "
95730e7468fSPeter Dunlap 		    "expstatsn:0x%x", icp->conn_oid, inihp->opcode, inihp->itt,
958fcf3ce44SJohn Forte 		    ntohl(inihp->statsn), icp->conn_expstatsn);
95930e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
960fcf3ce44SJohn Forte 	}
96130e7468fSPeter Dunlap 	isp = icp->conn_sess;
96230e7468fSPeter Dunlap 	ASSERT(isp != NULL);
963fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
964fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
965fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
966fcf3ce44SJohn Forte 	if (inihp->itt != ISCSI_RSVD_TASK_TAG) {
967fcf3ce44SJohn Forte 		if (!ISCSI_SUCCESS(iscsi_rx_process_itt_to_icmdp(
96830e7468fSPeter Dunlap 		    isp, (iscsi_hdr_t *)inihp, &icmdp))) {
96930e7468fSPeter Dunlap 			cmn_err(CE_WARN, "iscsi connection(%u) protocol error "
97030e7468fSPeter Dunlap 			    "- can not find cmd for itt:0x%x",
97130e7468fSPeter Dunlap 			    icp->conn_oid, inihp->itt);
972fcf3ce44SJohn Forte 			mutex_exit(&isp->sess_cmdsn_mutex);
973fcf3ce44SJohn Forte 			mutex_exit(&icp->conn_queue_active.mutex);
974fcf3ce44SJohn Forte 			mutex_exit(&isp->sess_queue_pending.mutex);
97530e7468fSPeter Dunlap 			return (IDM_STATUS_PROTOCOL_ERROR);
976fcf3ce44SJohn Forte 		}
977fcf3ce44SJohn Forte 	}
978fcf3ce44SJohn Forte 
979fcf3ce44SJohn Forte 	/* update expcmdsn and maxcmdsn */
980fcf3ce44SJohn Forte 	iscsi_update_flow_control(isp, ntohl(inihp->maxcmdsn),
981fcf3ce44SJohn Forte 	    ntohl(inihp->expcmdsn));
982fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
983fcf3ce44SJohn Forte 
984fcf3ce44SJohn Forte 	if ((inihp->itt != ISCSI_RSVD_TASK_TAG) &&
985fcf3ce44SJohn Forte 	    (inihp->ttt == ISCSI_RSVD_TASK_TAG)) {
986fcf3ce44SJohn Forte 		/* This is the only type of nop that incs. the expstatsn */
987fcf3ce44SJohn Forte 		icp->conn_expstatsn++;
988fcf3ce44SJohn Forte 
989fcf3ce44SJohn Forte 		/*
990fcf3ce44SJohn Forte 		 * This is a targets response to our nop
991fcf3ce44SJohn Forte 		 */
992fcf3ce44SJohn Forte 		iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp);
993fcf3ce44SJohn Forte 	} else if (inihp->ttt != ISCSI_RSVD_TASK_TAG) {
994fcf3ce44SJohn Forte 		/*
995fcf3ce44SJohn Forte 		 * Target requested a nop.  Send one.
996fcf3ce44SJohn Forte 		 */
997fcf3ce44SJohn Forte 		iscsi_handle_nop(icp, ISCSI_RSVD_TASK_TAG, inihp->ttt);
998fcf3ce44SJohn Forte 	} else {
999fcf3ce44SJohn Forte 		/*
1000fcf3ce44SJohn Forte 		 * This is a target-initiated ping that doesn't expect
1001fcf3ce44SJohn Forte 		 * a response; nothing to do except update our flow control
1002fcf3ce44SJohn Forte 		 * (which we do in all cases above).
1003fcf3ce44SJohn Forte 		 */
1004fcf3ce44SJohn Forte 		/* EMPTY */
1005fcf3ce44SJohn Forte 	}
1006fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_queue_active.mutex);
1007fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
1008fcf3ce44SJohn Forte 
100930e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1010fcf3ce44SJohn Forte }
1011fcf3ce44SJohn Forte 
1012fcf3ce44SJohn Forte 
1013fcf3ce44SJohn Forte /*
1014fcf3ce44SJohn Forte  * iscsi_rx_process_reject_rsp - The server rejected a PDU
1015fcf3ce44SJohn Forte  */
101630e7468fSPeter Dunlap static idm_status_t
101730e7468fSPeter Dunlap iscsi_rx_process_reject_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
1018fcf3ce44SJohn Forte {
101930e7468fSPeter Dunlap 	iscsi_reject_rsp_hdr_t	*irrhp = (iscsi_reject_rsp_hdr_t *)pdu->isp_hdr;
102030e7468fSPeter Dunlap 	iscsi_sess_t		*isp		= NULL;
102130e7468fSPeter Dunlap 	uint32_t		dlength		= 0;
102230e7468fSPeter Dunlap 	iscsi_hdr_t		*old_ihp	= NULL;
102330e7468fSPeter Dunlap 	iscsi_conn_t		*icp		= ic->ic_handle;
102430e7468fSPeter Dunlap 	uint8_t			*data 		= pdu->isp_data;
102530e7468fSPeter Dunlap 	iscsi_hdr_t		*ihp		= (iscsi_hdr_t *)irrhp;
102630e7468fSPeter Dunlap 	idm_status_t		status;
102730e7468fSPeter Dunlap 	iscsi_cmd_t		*icmdp	= NULL;
1028fcf3ce44SJohn Forte 
1029fcf3ce44SJohn Forte 	ASSERT(data != NULL);
103030e7468fSPeter Dunlap 	isp = icp->conn_sess;
103130e7468fSPeter Dunlap 	ASSERT(isp != NULL);
1032fcf3ce44SJohn Forte 
103330e7468fSPeter Dunlap 	mutex_enter(&icp->conn_queue_active.mutex);
103430e7468fSPeter Dunlap 	if ((status = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)irrhp,
103530e7468fSPeter Dunlap 	    &icmdp)) != IDM_STATUS_SUCCESS) {
103630e7468fSPeter Dunlap 		mutex_exit(&icp->conn_queue_active.mutex);
103730e7468fSPeter Dunlap 		return (status);
1038fcf3ce44SJohn Forte 	}
1039fcf3ce44SJohn Forte 
1040fcf3ce44SJohn Forte 	/* If we don't have the rejected header we can't do anything */
1041fcf3ce44SJohn Forte 	dlength = n2h24(irrhp->dlength);
1042fcf3ce44SJohn Forte 	if (dlength < sizeof (iscsi_hdr_t)) {
104330e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1044fcf3ce44SJohn Forte 	}
1045fcf3ce44SJohn Forte 
1046fcf3ce44SJohn Forte 	/* map old ihp */
1047fcf3ce44SJohn Forte 	old_ihp = (iscsi_hdr_t *)data;
1048fcf3ce44SJohn Forte 
1049fcf3ce44SJohn Forte 	switch (irrhp->reason) {
1050fcf3ce44SJohn Forte 	/*
1051fcf3ce44SJohn Forte 	 * ISCSI_REJECT_IMM_CMD_REJECT - Immediate Command Reject
1052fcf3ce44SJohn Forte 	 * too many immediate commands (original cmd can be resent)
1053fcf3ce44SJohn Forte 	 */
1054fcf3ce44SJohn Forte 	case ISCSI_REJECT_IMM_CMD_REJECT:
1055fcf3ce44SJohn Forte 		/*
1056fcf3ce44SJohn Forte 		 * We have exceeded the server's capacity for outstanding
1057fcf3ce44SJohn Forte 		 * immediate commands.   This must be a task management
1058fcf3ce44SJohn Forte 		 * command so try to find it in the abortingqueue and
1059fcf3ce44SJohn Forte 		 * complete it.
1060fcf3ce44SJohn Forte 		 */
1061fcf3ce44SJohn Forte 		if (!(old_ihp->opcode & ISCSI_OP_IMMEDIATE)) {
1062fcf3ce44SJohn Forte 			/* Rejecting IMM but old old_hdr wasn't IMM */
106330e7468fSPeter Dunlap 			return (IDM_STATUS_PROTOCOL_ERROR);
1064fcf3ce44SJohn Forte 		}
1065fcf3ce44SJohn Forte 
1066fcf3ce44SJohn Forte 		/*
1067fcf3ce44SJohn Forte 		 * We only send NOP and TASK_MGT as IMM.  All other
1068fcf3ce44SJohn Forte 		 * cases should be considered as a protocol error.
1069fcf3ce44SJohn Forte 		 */
1070fcf3ce44SJohn Forte 		switch (old_ihp->opcode & ISCSI_OPCODE_MASK) {
1071fcf3ce44SJohn Forte 		case ISCSI_OP_NOOP_OUT:
1072fcf3ce44SJohn Forte 			/*
1073fcf3ce44SJohn Forte 			 * A ping was rejected - treat this like
1074fcf3ce44SJohn Forte 			 * ping response.  The down side is we
1075fcf3ce44SJohn Forte 			 * didn't get an updated MaxCmdSn.
1076fcf3ce44SJohn Forte 			 */
1077fcf3ce44SJohn Forte 			break;
1078fcf3ce44SJohn Forte 		case ISCSI_OP_SCSI_TASK_MGT_MSG:
107930e7468fSPeter Dunlap 			(void) iscsi_rx_process_rejected_tsk_mgt(ic, old_ihp);
1080fcf3ce44SJohn Forte 			break;
1081fcf3ce44SJohn Forte 		default:
1082fcf3ce44SJohn Forte 			cmn_err(CE_WARN, "iscsi connection(%u) protocol error "
1083fcf3ce44SJohn Forte 			    "- received a reject for a command(0x%02x) not "
1084fcf3ce44SJohn Forte 			    "sent as an immediate", icp->conn_oid,
1085fcf3ce44SJohn Forte 			    old_ihp->opcode);
108630e7468fSPeter Dunlap 			status = IDM_STATUS_PROTOCOL_ERROR;
108730e7468fSPeter Dunlap 			break;
1088fcf3ce44SJohn Forte 		}
1089fcf3ce44SJohn Forte 		break;
1090fcf3ce44SJohn Forte 
1091fcf3ce44SJohn Forte 	/*
1092fcf3ce44SJohn Forte 	 * For the rest of the reject cases just use the general
1093fcf3ce44SJohn Forte 	 * hammer of dis/reconnecting.  This will resolve all
1094fcf3ce44SJohn Forte 	 * noted issues although could be more graceful.
1095fcf3ce44SJohn Forte 	 */
1096fcf3ce44SJohn Forte 	case ISCSI_REJECT_DATA_DIGEST_ERROR:
1097fcf3ce44SJohn Forte 	case ISCSI_REJECT_CMD_BEFORE_LOGIN:
1098fcf3ce44SJohn Forte 	case ISCSI_REJECT_SNACK_REJECT:
1099fcf3ce44SJohn Forte 	case ISCSI_REJECT_PROTOCOL_ERROR:
1100fcf3ce44SJohn Forte 	case ISCSI_REJECT_CMD_NOT_SUPPORTED:
1101fcf3ce44SJohn Forte 	case ISCSI_REJECT_TASK_IN_PROGRESS:
1102fcf3ce44SJohn Forte 	case ISCSI_REJECT_INVALID_DATA_ACK:
1103fcf3ce44SJohn Forte 	case ISCSI_REJECT_INVALID_PDU_FIELD:
1104fcf3ce44SJohn Forte 	case ISCSI_REJECT_LONG_OPERATION_REJECT:
1105fcf3ce44SJohn Forte 	case ISCSI_REJECT_NEGOTIATION_RESET:
1106fcf3ce44SJohn Forte 	default:
1107fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) closing connection - "
1108fcf3ce44SJohn Forte 		    "target requested itt:0x%x reason:0x%x",
1109fcf3ce44SJohn Forte 		    icp->conn_oid, ihp->itt, irrhp->reason);
111030e7468fSPeter Dunlap 		status = IDM_STATUS_PROTOCOL_ERROR;
111130e7468fSPeter Dunlap 		break;
1112fcf3ce44SJohn Forte 	}
1113fcf3ce44SJohn Forte 
111430e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1115fcf3ce44SJohn Forte }
1116fcf3ce44SJohn Forte 
1117fcf3ce44SJohn Forte 
1118fcf3ce44SJohn Forte /*
1119fcf3ce44SJohn Forte  * iscsi_rx_process_rejected_tsk_mgt -
1120fcf3ce44SJohn Forte  */
112130e7468fSPeter Dunlap /* ARGSUSED */
112230e7468fSPeter Dunlap static idm_status_t
112330e7468fSPeter Dunlap iscsi_rx_process_rejected_tsk_mgt(idm_conn_t *ic, iscsi_hdr_t *old_ihp)
1124fcf3ce44SJohn Forte {
112530e7468fSPeter Dunlap 	iscsi_sess_t		*isp	= NULL;
112630e7468fSPeter Dunlap 	iscsi_cmd_t		*icmdp	= NULL;
112730e7468fSPeter Dunlap 	iscsi_conn_t		*icp 	= NULL;
1128fcf3ce44SJohn Forte 
1129fcf3ce44SJohn Forte 	isp = icp->conn_sess;
1130fcf3ce44SJohn Forte 	ASSERT(old_ihp != NULL);
113130e7468fSPeter Dunlap 	ASSERT(isp != NULL);
1132fcf3ce44SJohn Forte 
1133fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
1134fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
1135fcf3ce44SJohn Forte 	if (!ISCSI_SUCCESS(iscsi_rx_process_itt_to_icmdp(
1136fcf3ce44SJohn Forte 	    isp, old_ihp, &icmdp))) {
1137fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_cmdsn_mutex);
1138fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
113930e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1140fcf3ce44SJohn Forte 	}
1141fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
1142fcf3ce44SJohn Forte 
1143fcf3ce44SJohn Forte 	switch (icmdp->cmd_type) {
1144fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_ABORT:
1145fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_RESET:
1146fcf3ce44SJohn Forte 		iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E4,
1147fcf3ce44SJohn Forte 		    icp->conn_sess);
1148fcf3ce44SJohn Forte 		break;
1149fcf3ce44SJohn Forte 	/* We don't send any other task mgr types */
1150fcf3ce44SJohn Forte 	default:
1151fcf3ce44SJohn Forte 		ASSERT(B_FALSE);
1152fcf3ce44SJohn Forte 		break;
1153fcf3ce44SJohn Forte 	}
1154fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_queue_active.mutex);
1155fcf3ce44SJohn Forte 
115630e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1157fcf3ce44SJohn Forte }
1158fcf3ce44SJohn Forte 
1159fcf3ce44SJohn Forte 
1160fcf3ce44SJohn Forte /*
1161fcf3ce44SJohn Forte  * iscsi_rx_process_task_mgt_rsp -
1162fcf3ce44SJohn Forte  */
1163fcf3ce44SJohn Forte /* ARGSUSED */
116430e7468fSPeter Dunlap static idm_status_t
116530e7468fSPeter Dunlap iscsi_rx_process_task_mgt_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
1166fcf3ce44SJohn Forte {
1167fcf3ce44SJohn Forte 	iscsi_sess_t			*isp		= NULL;
1168fcf3ce44SJohn Forte 	iscsi_scsi_task_mgt_rsp_hdr_t	*istmrhp	= NULL;
1169fcf3ce44SJohn Forte 	iscsi_cmd_t			*icmdp		= NULL;
117030e7468fSPeter Dunlap 	iscsi_conn_t			*icp		= ic->ic_handle;
117130e7468fSPeter Dunlap 	idm_status_t			status = IDM_STATUS_SUCCESS;
1172fcf3ce44SJohn Forte 
1173fcf3ce44SJohn Forte 	isp = icp->conn_sess;
117430e7468fSPeter Dunlap 	istmrhp = (iscsi_scsi_task_mgt_rsp_hdr_t *)pdu->isp_hdr;
1175fcf3ce44SJohn Forte 
1176fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
117730e7468fSPeter Dunlap 	if ((status = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)istmrhp,
117830e7468fSPeter Dunlap 	    &icmdp)) != IDM_STATUS_SUCCESS) {
1179fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
118030e7468fSPeter Dunlap 		return (status);
1181fcf3ce44SJohn Forte 	}
1182fcf3ce44SJohn Forte 
1183fcf3ce44SJohn Forte 	switch (icmdp->cmd_type) {
1184fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_ABORT:
1185fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_RESET:
1186fcf3ce44SJohn Forte 		switch (istmrhp->response) {
1187fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_COMPLETE:
1188fcf3ce44SJohn Forte 			/* success */
1189fcf3ce44SJohn Forte 			iscsi_cmd_state_machine(icmdp,
1190fcf3ce44SJohn Forte 			    ISCSI_CMD_EVENT_E3, isp);
1191fcf3ce44SJohn Forte 			break;
1192fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_NO_TASK:
1193fcf3ce44SJohn Forte 			/*
1194fcf3ce44SJohn Forte 			 * If the array no longer knows about
1195fcf3ce44SJohn Forte 			 * an ABORT RTT and we no longer have
1196fcf3ce44SJohn Forte 			 * a parent SCSI command it was just
1197fcf3ce44SJohn Forte 			 * completed, free this ABORT resource.
1198fcf3ce44SJohn Forte 			 * Otherwise FALLTHRU this will flag a
1199fcf3ce44SJohn Forte 			 * protocol problem.
1200fcf3ce44SJohn Forte 			 */
1201fcf3ce44SJohn Forte 			if ((icmdp->cmd_type == ISCSI_CMD_TYPE_ABORT) &&
1202fcf3ce44SJohn Forte 			    (icmdp->cmd_un.abort.icmdp == NULL)) {
1203fcf3ce44SJohn Forte 				iscsi_cmd_state_machine(icmdp,
1204fcf3ce44SJohn Forte 				    ISCSI_CMD_EVENT_E4, isp);
1205fcf3ce44SJohn Forte 				break;
1206fcf3ce44SJohn Forte 			}
1207fcf3ce44SJohn Forte 			/* FALLTHRU */
1208fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_NO_LUN:
1209fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_TASK_ALLEGIANT:
1210fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_NO_FAILOVER:
1211fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_IN_PRGRESS:
1212fcf3ce44SJohn Forte 		case SCSI_TCP_TM_RESP_REJECTED:
1213fcf3ce44SJohn Forte 		default:
1214fcf3ce44SJohn Forte 			/*
1215fcf3ce44SJohn Forte 			 * Something is out of sync.  Flush
1216fcf3ce44SJohn Forte 			 * active queues and resync the
1217fcf3ce44SJohn Forte 			 * the connection to try and recover
1218fcf3ce44SJohn Forte 			 * to a known state.
1219fcf3ce44SJohn Forte 			 */
122030e7468fSPeter Dunlap 			status = IDM_STATUS_PROTOCOL_ERROR;
1221fcf3ce44SJohn Forte 		}
1222fcf3ce44SJohn Forte 		break;
1223fcf3ce44SJohn Forte 
1224fcf3ce44SJohn Forte 	default:
1225fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) protocol error - "
1226fcf3ce44SJohn Forte 		    "received a task mgt response for a non-task mgt "
1227fcf3ce44SJohn Forte 		    "cmd itt:0x%x type:%d", icp->conn_oid, istmrhp->itt,
1228fcf3ce44SJohn Forte 		    icmdp->cmd_type);
122930e7468fSPeter Dunlap 		status = IDM_STATUS_PROTOCOL_ERROR;
123030e7468fSPeter Dunlap 		break;
1231fcf3ce44SJohn Forte 	}
1232fcf3ce44SJohn Forte 
1233fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_queue_active.mutex);
123430e7468fSPeter Dunlap 	return (status);
1235fcf3ce44SJohn Forte }
1236fcf3ce44SJohn Forte 
1237fcf3ce44SJohn Forte 
1238fcf3ce44SJohn Forte /*
123930e7468fSPeter Dunlap  * iscsi_rx_process_logout_rsp -
1240fcf3ce44SJohn Forte  *
1241fcf3ce44SJohn Forte  */
1242fcf3ce44SJohn Forte /* ARGSUSED */
124330e7468fSPeter Dunlap idm_status_t
124430e7468fSPeter Dunlap iscsi_rx_process_logout_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
1245fcf3ce44SJohn Forte {
124630e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
124730e7468fSPeter Dunlap 	iscsi_logout_rsp_hdr_t	*ilrhp	=
124830e7468fSPeter Dunlap 	    (iscsi_logout_rsp_hdr_t *)pdu->isp_hdr;
1249fcf3ce44SJohn Forte 	iscsi_cmd_t		*icmdp	= NULL;
125030e7468fSPeter Dunlap 	iscsi_sess_t		*isp;
125130e7468fSPeter Dunlap 	idm_status_t		status = IDM_STATUS_SUCCESS;
1252fcf3ce44SJohn Forte 
1253fcf3ce44SJohn Forte 	isp = icp->conn_sess;
1254fcf3ce44SJohn Forte 
1255fcf3ce44SJohn Forte 	if (icp->conn_expstatsn != ntohl(ilrhp->statsn)) {
125630e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi connection(%u/%x) protocol error - "
1257fcf3ce44SJohn Forte 		    "received status out of order itt:0x%x statsn:0x%x "
125830e7468fSPeter Dunlap 		    "expstatsn:0x%x", icp->conn_oid, ilrhp->opcode, ilrhp->itt,
1259fcf3ce44SJohn Forte 		    ntohl(ilrhp->statsn), icp->conn_expstatsn);
126030e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1261fcf3ce44SJohn Forte 	}
1262fcf3ce44SJohn Forte 
1263fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
1264fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
1265fcf3ce44SJohn Forte 	if (ilrhp->itt != ISCSI_RSVD_TASK_TAG) {
1266fcf3ce44SJohn Forte 		if (!ISCSI_SUCCESS(iscsi_rx_process_itt_to_icmdp(
126730e7468fSPeter Dunlap 		    isp, (iscsi_hdr_t *)ilrhp, &icmdp))) {
1268fcf3ce44SJohn Forte 			mutex_exit(&isp->sess_cmdsn_mutex);
1269fcf3ce44SJohn Forte 			mutex_exit(&icp->conn_queue_active.mutex);
127030e7468fSPeter Dunlap 			return (IDM_STATUS_PROTOCOL_ERROR);
1271fcf3ce44SJohn Forte 		}
1272fcf3ce44SJohn Forte 	}
1273fcf3ce44SJohn Forte 
1274fcf3ce44SJohn Forte 	/* update expcmdsn and maxcmdsn */
1275fcf3ce44SJohn Forte 	iscsi_update_flow_control(isp, ntohl(ilrhp->maxcmdsn),
1276fcf3ce44SJohn Forte 	    ntohl(ilrhp->expcmdsn));
1277fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
1278fcf3ce44SJohn Forte 
127930e7468fSPeter Dunlap 	ISCSI_IO_LOG(CE_NOTE,
128030e7468fSPeter Dunlap 	    "DEBUG: iscsi_rx_process_logout_rsp: response: %d",
128130e7468fSPeter Dunlap 	    ilrhp->response);
1282fcf3ce44SJohn Forte 	switch (ilrhp->response) {
1283fcf3ce44SJohn Forte 	case ISCSI_LOGOUT_CID_NOT_FOUND:
1284fcf3ce44SJohn Forte 		/*
1285fcf3ce44SJohn Forte 		 * If the target doesn't know about our connection
1286fcf3ce44SJohn Forte 		 * then we can consider our self disconnected.
1287fcf3ce44SJohn Forte 		 */
1288fcf3ce44SJohn Forte 		/* FALLTHRU */
1289fcf3ce44SJohn Forte 	case ISCSI_LOGOUT_RECOVERY_UNSUPPORTED:
1290fcf3ce44SJohn Forte 		/*
1291fcf3ce44SJohn Forte 		 * We don't support ErrorRecovery levels above 0
1292fcf3ce44SJohn Forte 		 * currently so consider this success.
1293fcf3ce44SJohn Forte 		 */
1294fcf3ce44SJohn Forte 		/* FALLTHRU */
1295fcf3ce44SJohn Forte 	case ISCSI_LOGOUT_CLEANUP_FAILED:
1296fcf3ce44SJohn Forte 		/*
1297fcf3ce44SJohn Forte 		 * per spec. "cleanup failed for various reasons."
1298fcf3ce44SJohn Forte 		 * Although those various reasons are undefined.
1299fcf3ce44SJohn Forte 		 * Not sure what to do here.  So fake success,
1300fcf3ce44SJohn Forte 		 * which will disconnect the connection.
1301fcf3ce44SJohn Forte 		 */
1302fcf3ce44SJohn Forte 		/* FALLTHRU */
1303fcf3ce44SJohn Forte 	case ISCSI_LOGOUT_SUCCESS:
1304fcf3ce44SJohn Forte 		iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp);
1305fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
130630e7468fSPeter Dunlap 		iscsi_drop_conn_cleanup(icp);
1307fcf3ce44SJohn Forte 		break;
1308fcf3ce44SJohn Forte 	default:
1309fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
131030e7468fSPeter Dunlap 		status = IDM_STATUS_PROTOCOL_ERROR;
131130e7468fSPeter Dunlap 		break;
1312fcf3ce44SJohn Forte 
131330e7468fSPeter Dunlap 	}
131430e7468fSPeter Dunlap 	return (status);
1315fcf3ce44SJohn Forte }
1316fcf3ce44SJohn Forte 
1317fcf3ce44SJohn Forte /*
131830e7468fSPeter Dunlap  * iscsi_rx_process_async_rsp
1319fcf3ce44SJohn Forte  *
1320fcf3ce44SJohn Forte  */
1321fcf3ce44SJohn Forte /* ARGSUSED */
132230e7468fSPeter Dunlap static idm_status_t
132330e7468fSPeter Dunlap iscsi_rx_process_async_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
1324fcf3ce44SJohn Forte {
132530e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
132630e7468fSPeter Dunlap 	iscsi_sess_t		*isp	= icp->conn_sess;
132730e7468fSPeter Dunlap 	idm_status_t		rval	= IDM_STATUS_SUCCESS;
132830e7468fSPeter Dunlap 	iscsi_task_t		*itp;
132930e7468fSPeter Dunlap 	iscsi_async_evt_hdr_t	*iaehp	=
133030e7468fSPeter Dunlap 	    (iscsi_async_evt_hdr_t *)pdu->isp_hdr;
1331fcf3ce44SJohn Forte 
1332fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
133330e7468fSPeter Dunlap 	ASSERT(pdu != NULL);
133430e7468fSPeter Dunlap 	ASSERT(isp != NULL);
1335fcf3ce44SJohn Forte 
133630e7468fSPeter Dunlap 	mutex_enter(&isp->sess_cmdsn_mutex);
133730e7468fSPeter Dunlap 	if (icp->conn_expstatsn == ntohl(iaehp->statsn)) {
133830e7468fSPeter Dunlap 		icp->conn_expstatsn++;
133930e7468fSPeter Dunlap 	} else {
1340fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) protocol error - "
134130e7468fSPeter Dunlap 		    "received status out of order statsn:0x%x "
134230e7468fSPeter Dunlap 		    "expstatsn:0x%x", icp->conn_oid,
1343fcf3ce44SJohn Forte 		    ntohl(iaehp->statsn), icp->conn_expstatsn);
134430e7468fSPeter Dunlap 		mutex_exit(&isp->sess_cmdsn_mutex);
134530e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1346fcf3ce44SJohn Forte 	}
134730e7468fSPeter Dunlap 	mutex_exit(&isp->sess_cmdsn_mutex);
1348fcf3ce44SJohn Forte 
1349fcf3ce44SJohn Forte 	switch (iaehp->async_event) {
1350fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_SCSI_EVENT:
1351fcf3ce44SJohn Forte 		/*
1352fcf3ce44SJohn Forte 		 * SCSI asynchronous event is reported in
1353fcf3ce44SJohn Forte 		 * the sense data.  Sense data that accompanies
1354fcf3ce44SJohn Forte 		 * the report in the data segment identifies the
1355fcf3ce44SJohn Forte 		 * condition.  If the target supports SCSI
1356fcf3ce44SJohn Forte 		 * asynchronous events reporting (see [SAM2])
1357fcf3ce44SJohn Forte 		 * as indicated in the stardard INQUIRY data
1358fcf3ce44SJohn Forte 		 * (see [SPC3]), its use may be enabled by
1359fcf3ce44SJohn Forte 		 * parameters in the SCSI control mode page
1360fcf3ce44SJohn Forte 		 * (see [SPC3]).
1361fcf3ce44SJohn Forte 		 *
1362fcf3ce44SJohn Forte 		 * T-10 has removed SCSI asunchronous events
1363fcf3ce44SJohn Forte 		 * from the standard.  Although we have seen
1364fcf3ce44SJohn Forte 		 * a couple targets still spending these requests.
1365fcf3ce44SJohn Forte 		 * Those targets were specifically sending them
1366fcf3ce44SJohn Forte 		 * for notification of a LUN/Volume change
1367fcf3ce44SJohn Forte 		 * (ex. LUN addition/removal).  Take a general
1368fcf3ce44SJohn Forte 		 * action to these events of dis/reconnecting.
1369fcf3ce44SJohn Forte 		 * Once reconnected we perform a reenumeration.
1370fcf3ce44SJohn Forte 		 */
137130e7468fSPeter Dunlap 		idm_ini_conn_disconnect(ic);
1372fcf3ce44SJohn Forte 		break;
1373fcf3ce44SJohn Forte 
1374fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_REQUEST_LOGOUT:
137530e7468fSPeter Dunlap 		/*
137630e7468fSPeter Dunlap 		 * We've been asked to logout by the target --
137730e7468fSPeter Dunlap 		 * we need to treat this differently from a normal logout
137830e7468fSPeter Dunlap 		 * due to a discovery failure.  Normal logouts result in
137930e7468fSPeter Dunlap 		 * an N3 event to the session state machine and an offline
138030e7468fSPeter Dunlap 		 * of the lun.  In this case we want to put the connection
138130e7468fSPeter Dunlap 		 * into "failed" state and generate N5 to the session state
138230e7468fSPeter Dunlap 		 * machine since the initiator logged out at the target's
138330e7468fSPeter Dunlap 		 * request.  To track this we set a flag indicating we
138430e7468fSPeter Dunlap 		 * received this async logout request from the tharget
138530e7468fSPeter Dunlap 		 */
1386fcf3ce44SJohn Forte 		mutex_enter(&icp->conn_state_mutex);
138730e7468fSPeter Dunlap 		icp->conn_async_logout = B_TRUE;
1388fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_state_mutex);
138930e7468fSPeter Dunlap 
139030e7468fSPeter Dunlap 		/* Target has requested this connection to logout. */
139130e7468fSPeter Dunlap 		itp = kmem_zalloc(sizeof (iscsi_task_t), KM_SLEEP);
139230e7468fSPeter Dunlap 		itp->t_arg = icp;
139330e7468fSPeter Dunlap 		itp->t_blocking = B_FALSE;
139430e7468fSPeter Dunlap 		if (ddi_taskq_dispatch(isp->sess_taskq,
139530e7468fSPeter Dunlap 		    (void(*)())iscsi_logout_start, itp, DDI_SLEEP) !=
139630e7468fSPeter Dunlap 		    DDI_SUCCESS) {
139730e7468fSPeter Dunlap 			/* Disconnect if we couldn't dispatch the task */
139830e7468fSPeter Dunlap 			idm_ini_conn_disconnect(ic);
139930e7468fSPeter Dunlap 		}
1400fcf3ce44SJohn Forte 		break;
1401fcf3ce44SJohn Forte 
1402fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_DROPPING_CONNECTION:
1403fcf3ce44SJohn Forte 		/*
1404fcf3ce44SJohn Forte 		 * Target is going to drop our connection.
1405fcf3ce44SJohn Forte 		 *	param1 - CID which will be dropped.
1406fcf3ce44SJohn Forte 		 *	param2 - Min time to reconnect.
1407fcf3ce44SJohn Forte 		 *	param3 - Max time to reconnect.
1408fcf3ce44SJohn Forte 		 *
1409fcf3ce44SJohn Forte 		 * For now just let fail as another disconnect.
1410fcf3ce44SJohn Forte 		 *
1411fcf3ce44SJohn Forte 		 * MC/S Once we support > 1 connections then
1412fcf3ce44SJohn Forte 		 * we need to check the CID and drop that
1413fcf3ce44SJohn Forte 		 * specific connection.
1414fcf3ce44SJohn Forte 		 */
141530e7468fSPeter Dunlap 		iscsi_conn_set_login_min_max(icp, iaehp->param2,
141630e7468fSPeter Dunlap 		    iaehp->param3);
141730e7468fSPeter Dunlap 		idm_ini_conn_disconnect(ic);
1418fcf3ce44SJohn Forte 		break;
1419fcf3ce44SJohn Forte 
1420fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_DROPPING_ALL_CONNECTIONS:
1421fcf3ce44SJohn Forte 		/*
1422fcf3ce44SJohn Forte 		 * Target is going to drop ALL connections.
1423fcf3ce44SJohn Forte 		 *	param2 - Min time to reconnect.
1424fcf3ce44SJohn Forte 		 *	param3 - Max time to reconnect.
1425fcf3ce44SJohn Forte 		 *
1426fcf3ce44SJohn Forte 		 * For now just let fail as anyother disconnect.
1427fcf3ce44SJohn Forte 		 *
1428fcf3ce44SJohn Forte 		 * MC/S Once we support more than > 1 connections
1429fcf3ce44SJohn Forte 		 * then we need to drop all connections on the
1430fcf3ce44SJohn Forte 		 * session.
1431fcf3ce44SJohn Forte 		 */
143230e7468fSPeter Dunlap 		iscsi_conn_set_login_min_max(icp, iaehp->param2,
143330e7468fSPeter Dunlap 		    iaehp->param3);
143430e7468fSPeter Dunlap 		idm_ini_conn_disconnect(ic);
1435fcf3ce44SJohn Forte 		break;
1436fcf3ce44SJohn Forte 
1437fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_PARAM_NEGOTIATION:
1438fcf3ce44SJohn Forte 		/*
1439fcf3ce44SJohn Forte 		 * Target requests parameter negotiation
1440fcf3ce44SJohn Forte 		 * on this connection.
1441fcf3ce44SJohn Forte 		 *
1442fcf3ce44SJohn Forte 		 * The initiator must honor this request.  For
1443fcf3ce44SJohn Forte 		 * now we will request a logout.  We can't
1444fcf3ce44SJohn Forte 		 * just ignore this or it might force corruption?
1445fcf3ce44SJohn Forte 		 */
144630e7468fSPeter Dunlap 		itp = kmem_zalloc(sizeof (iscsi_task_t), KM_SLEEP);
144730e7468fSPeter Dunlap 		itp->t_arg = icp;
144830e7468fSPeter Dunlap 		itp->t_blocking = B_FALSE;
144930e7468fSPeter Dunlap 		if (ddi_taskq_dispatch(isp->sess_taskq,
145030e7468fSPeter Dunlap 		    (void(*)())iscsi_logout_start, itp, DDI_SLEEP) !=
145130e7468fSPeter Dunlap 		    DDI_SUCCESS) {
145230e7468fSPeter Dunlap 			/* Disconnect if we couldn't dispatch the task */
145330e7468fSPeter Dunlap 			idm_ini_conn_disconnect(ic);
145430e7468fSPeter Dunlap 		}
1455fcf3ce44SJohn Forte 		break;
1456fcf3ce44SJohn Forte 
1457fcf3ce44SJohn Forte 	case ISCSI_ASYNC_EVENT_VENDOR_SPECIFIC:
1458fcf3ce44SJohn Forte 		/*
1459fcf3ce44SJohn Forte 		 * We currently don't handle any vendor
1460fcf3ce44SJohn Forte 		 * specific async events.  So just ignore
1461fcf3ce44SJohn Forte 		 * the request.
1462fcf3ce44SJohn Forte 		 */
146330e7468fSPeter Dunlap 		idm_ini_conn_disconnect(ic);
1464fcf3ce44SJohn Forte 		break;
1465fcf3ce44SJohn Forte 	default:
146630e7468fSPeter Dunlap 		rval = IDM_STATUS_PROTOCOL_ERROR;
1467fcf3ce44SJohn Forte 	}
1468fcf3ce44SJohn Forte 
1469fcf3ce44SJohn Forte 	return (rval);
1470fcf3ce44SJohn Forte }
1471fcf3ce44SJohn Forte 
1472fcf3ce44SJohn Forte /*
1473fcf3ce44SJohn Forte  * iscsi_rx_process_text_rsp - processes iSCSI text response.  It sets
1474fcf3ce44SJohn Forte  * the cmd_result field of the command data structure with the actual
1475fcf3ce44SJohn Forte  * status value instead of returning the status value.  The return value
1476fcf3ce44SJohn Forte  * is SUCCESS in order to let iscsi_handle_text control the operation of
1477fcf3ce44SJohn Forte  * a text request.
147830e7468fSPeter Dunlap  * Text requests are a handled a little different than other types of
1479fcf3ce44SJohn Forte  * iSCSI commands because the initiator sends additional empty text requests
1480fcf3ce44SJohn Forte  * in order to obtain the remaining responses required to complete the
1481fcf3ce44SJohn Forte  * request.  iscsi_handle_text controls the operation of text request, while
1482fcf3ce44SJohn Forte  * iscsi_rx_process_text_rsp just process the current response.
1483fcf3ce44SJohn Forte  */
148430e7468fSPeter Dunlap static idm_status_t
148530e7468fSPeter Dunlap iscsi_rx_process_text_rsp(idm_conn_t *ic, idm_pdu_t *pdu)
1486fcf3ce44SJohn Forte {
1487fcf3ce44SJohn Forte 	iscsi_sess_t		*isp	= NULL;
148830e7468fSPeter Dunlap 	iscsi_text_rsp_hdr_t	*ithp	=
148930e7468fSPeter Dunlap 	    (iscsi_text_rsp_hdr_t *)pdu->isp_hdr;
149030e7468fSPeter Dunlap 	iscsi_conn_t		*icp	= ic->ic_handle;
1491fcf3ce44SJohn Forte 	iscsi_cmd_t		*icmdp	= NULL;
1492fcf3ce44SJohn Forte 	boolean_t		final	= B_FALSE;
1493fcf3ce44SJohn Forte 	uint32_t		data_len;
149430e7468fSPeter Dunlap 	uint8_t			*data = pdu->isp_data;
149530e7468fSPeter Dunlap 	idm_status_t		rval;
1496fcf3ce44SJohn Forte 
1497fcf3ce44SJohn Forte 	isp = icp->conn_sess;
1498fcf3ce44SJohn Forte 
1499fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_queue_active.mutex);
150030e7468fSPeter Dunlap 	if ((rval = iscsi_rx_chk(icp, isp, (iscsi_scsi_rsp_hdr_t *)ithp,
150130e7468fSPeter Dunlap 	    &icmdp)) != IDM_STATUS_SUCCESS) {
1502fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
150330e7468fSPeter Dunlap 		return (rval);
1504fcf3ce44SJohn Forte 	}
1505fcf3ce44SJohn Forte 
1506fcf3ce44SJohn Forte 	/* update local final response flag */
1507fcf3ce44SJohn Forte 	if (ithp->flags & ISCSI_FLAG_FINAL) {
1508fcf3ce44SJohn Forte 		final = B_TRUE;
1509fcf3ce44SJohn Forte 	}
1510fcf3ce44SJohn Forte 
1511fcf3ce44SJohn Forte 	/*
1512fcf3ce44SJohn Forte 	 * validate received TTT value.  RFC3720 specifies the following:
1513fcf3ce44SJohn Forte 	 * - F bit set to 1 MUST have a reserved TTT value 0xffffffff
1514fcf3ce44SJohn Forte 	 * - F bit set to 0 MUST have a non-reserved TTT value !0xffffffff
1515fcf3ce44SJohn Forte 	 * In addition, the received TTT value must not change between
1516fcf3ce44SJohn Forte 	 * responses of a long text response
1517fcf3ce44SJohn Forte 	 */
1518fcf3ce44SJohn Forte 	if (((final == B_TRUE) && (ithp->ttt != ISCSI_RSVD_TASK_TAG)) ||
1519fcf3ce44SJohn Forte 	    ((final == B_FALSE) && (ithp->ttt == ISCSI_RSVD_TASK_TAG))) {
1520fcf3ce44SJohn Forte 		icmdp->cmd_result = ISCSI_STATUS_PROTOCOL_ERROR;
1521fcf3ce44SJohn Forte 		icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_FINAL_RSP;
1522fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
1523fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) protocol error - "
1524fcf3ce44SJohn Forte 		    "received text response with invalid flags:0x%x or "
1525fcf3ce44SJohn Forte 		    "ttt:0x%x", icp->conn_oid, ithp->flags, ithp->itt);
152630e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1527fcf3ce44SJohn Forte 	}
1528fcf3ce44SJohn Forte 
1529fcf3ce44SJohn Forte 	if ((icmdp->cmd_un.text.stage == ISCSI_CMD_TEXT_INITIAL_REQ) &&
1530fcf3ce44SJohn Forte 	    (ithp->ttt == ISCSI_RSVD_TASK_TAG) &&
1531fcf3ce44SJohn Forte 	    (final == B_FALSE)) {
1532fcf3ce44SJohn Forte 		/* TTT should have matched reserved value */
1533fcf3ce44SJohn Forte 		icmdp->cmd_result = ISCSI_STATUS_PROTOCOL_ERROR;
1534fcf3ce44SJohn Forte 		icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_FINAL_RSP;
1535fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
1536fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi connection(%u) protocol "
1537fcf3ce44SJohn Forte 		    "error - received text response with invalid "
1538fcf3ce44SJohn Forte 		    "ttt:0x%x", icp->conn_oid, ithp->ttt);
153930e7468fSPeter Dunlap 		return (IDM_STATUS_PROTOCOL_ERROR);
1540fcf3ce44SJohn Forte 	}
1541fcf3ce44SJohn Forte 
1542fcf3ce44SJohn Forte 	/*
1543fcf3ce44SJohn Forte 	 * If this is first response, save away TTT value for later use
1544fcf3ce44SJohn Forte 	 * in a long text request/response sequence
1545fcf3ce44SJohn Forte 	 */
1546fcf3ce44SJohn Forte 	if (icmdp->cmd_un.text.stage == ISCSI_CMD_TEXT_INITIAL_REQ) {
1547fcf3ce44SJohn Forte 		icmdp->cmd_un.text.ttt = ithp->ttt;
1548fcf3ce44SJohn Forte 	}
1549fcf3ce44SJohn Forte 
1550fcf3ce44SJohn Forte 	data_len = ntoh24(ithp->dlength);
1551fcf3ce44SJohn Forte 
1552fcf3ce44SJohn Forte 	/* check whether enough buffer available to copy data */
1553fcf3ce44SJohn Forte 	if ((icmdp->cmd_un.text.total_rx_len + data_len) >
1554fcf3ce44SJohn Forte 	    icmdp->cmd_un.text.buf_len) {
1555fcf3ce44SJohn Forte 		icmdp->cmd_un.text.total_rx_len += data_len;
1556fcf3ce44SJohn Forte 		icmdp->cmd_result = ISCSI_STATUS_DATA_OVERFLOW;
1557fcf3ce44SJohn Forte 		/*
1558fcf3ce44SJohn Forte 		 * DATA_OVERFLOW will result in a SUCCESS return so that
1559fcf3ce44SJohn Forte 		 * iscsi_handle_text can continue to obtain the remaining
1560fcf3ce44SJohn Forte 		 * text response if needed.
1561fcf3ce44SJohn Forte 		 */
1562fcf3ce44SJohn Forte 	} else {
1563fcf3ce44SJohn Forte 		char *buf_data = (icmdp->cmd_un.text.buf +
1564fcf3ce44SJohn Forte 		    icmdp->cmd_un.text.offset);
1565fcf3ce44SJohn Forte 
1566fcf3ce44SJohn Forte 		bcopy(data, buf_data, data_len);
1567fcf3ce44SJohn Forte 		icmdp->cmd_un.text.offset += data_len;
1568fcf3ce44SJohn Forte 		icmdp->cmd_un.text.total_rx_len += data_len;
1569fcf3ce44SJohn Forte 		icmdp->cmd_result = ISCSI_STATUS_SUCCESS;
1570fcf3ce44SJohn Forte 		bcopy(ithp->rsvd4, icmdp->cmd_un.text.lun,
1571fcf3ce44SJohn Forte 		    sizeof (icmdp->cmd_un.text.lun));
1572fcf3ce44SJohn Forte 	}
1573fcf3ce44SJohn Forte 
1574fcf3ce44SJohn Forte 	/* update stage  */
1575fcf3ce44SJohn Forte 	if (final == B_TRUE) {
1576fcf3ce44SJohn Forte 		icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_FINAL_RSP;
1577fcf3ce44SJohn Forte 	} else {
1578fcf3ce44SJohn Forte 		icmdp->cmd_un.text.stage = ISCSI_CMD_TEXT_CONTINUATION;
1579fcf3ce44SJohn Forte 	}
1580fcf3ce44SJohn Forte 
1581fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E3, isp);
1582fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_queue_active.mutex);
158330e7468fSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
158430e7468fSPeter Dunlap }
158530e7468fSPeter Dunlap 
158630e7468fSPeter Dunlap /*
158730e7468fSPeter Dunlap  * iscsi_rx_process_scsi_itt_to_icmdp - Lookup itt using IDM to find matching
158830e7468fSPeter Dunlap  * icmdp.  Verify itt in hdr and icmdp are the same.
158930e7468fSPeter Dunlap  */
159030e7468fSPeter Dunlap static iscsi_status_t
159130e7468fSPeter Dunlap iscsi_rx_process_scsi_itt_to_icmdp(iscsi_sess_t *isp, idm_conn_t *ic,
159230e7468fSPeter Dunlap     iscsi_scsi_rsp_hdr_t *ihp, iscsi_cmd_t **icmdp)
159330e7468fSPeter Dunlap {
159430e7468fSPeter Dunlap 	idm_task_t *itp;
159530e7468fSPeter Dunlap 
159630e7468fSPeter Dunlap 	ASSERT(isp != NULL);
159730e7468fSPeter Dunlap 	ASSERT(ihp != NULL);
159830e7468fSPeter Dunlap 	ASSERT(icmdp != NULL);
159930e7468fSPeter Dunlap 	ASSERT(mutex_owned(&isp->sess_cmdsn_mutex));
160030e7468fSPeter Dunlap 	itp = idm_task_find_and_complete(ic, ihp->itt, ISCSI_INI_TASK_TTT);
160130e7468fSPeter Dunlap 	if (itp == NULL) {
160230e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi session(%u) protocol error - "
160330e7468fSPeter Dunlap 		    "received unknown itt:0x%x - protocol error",
160430e7468fSPeter Dunlap 		    isp->sess_oid, ihp->itt);
160530e7468fSPeter Dunlap 		return (ISCSI_STATUS_INTERNAL_ERROR);
160630e7468fSPeter Dunlap 	}
160730e7468fSPeter Dunlap 	*icmdp = itp->idt_private;
160830e7468fSPeter Dunlap 
160930e7468fSPeter Dunlap 	idm_task_rele(itp);
161030e7468fSPeter Dunlap 
1611fcf3ce44SJohn Forte 	return (ISCSI_STATUS_SUCCESS);
161230e7468fSPeter Dunlap 
1613fcf3ce44SJohn Forte }
1614fcf3ce44SJohn Forte 
1615fcf3ce44SJohn Forte /*
1616fcf3ce44SJohn Forte  * iscsi_rx_process_itt_to_icmdp - Lookup itt in the session's
1617fcf3ce44SJohn Forte  * cmd table to find matching icmdp.  Verify itt in hdr and
1618fcf3ce44SJohn Forte  * icmdp are the same.
1619fcf3ce44SJohn Forte  */
1620fcf3ce44SJohn Forte static iscsi_status_t
1621fcf3ce44SJohn Forte iscsi_rx_process_itt_to_icmdp(iscsi_sess_t *isp, iscsi_hdr_t *ihp,
1622fcf3ce44SJohn Forte     iscsi_cmd_t **icmdp)
1623fcf3ce44SJohn Forte {
1624fcf3ce44SJohn Forte 	int cmd_table_idx = 0;
1625fcf3ce44SJohn Forte 
1626fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
1627fcf3ce44SJohn Forte 	ASSERT(ihp != NULL);
1628fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
1629fcf3ce44SJohn Forte 	ASSERT(mutex_owned(&isp->sess_cmdsn_mutex));
1630fcf3ce44SJohn Forte 
1631fcf3ce44SJohn Forte 	/* try to find an associated iscsi_pkt */
163230e7468fSPeter Dunlap 	cmd_table_idx = (ihp->itt - IDM_TASKIDS_MAX) % ISCSI_CMD_TABLE_SIZE;
1633fcf3ce44SJohn Forte 	if (isp->sess_cmd_table[cmd_table_idx] == NULL) {
1634fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi session(%u) protocol error - "
1635fcf3ce44SJohn Forte 		    "received unknown itt:0x%x - protocol error",
1636fcf3ce44SJohn Forte 		    isp->sess_oid, ihp->itt);
1637fcf3ce44SJohn Forte 		return (ISCSI_STATUS_INTERNAL_ERROR);
1638fcf3ce44SJohn Forte 	}
1639fcf3ce44SJohn Forte 
1640fcf3ce44SJohn Forte 	/* verify itt */
1641fcf3ce44SJohn Forte 	if (isp->sess_cmd_table[cmd_table_idx]->cmd_itt != ihp->itt) {
1642fcf3ce44SJohn Forte 		cmn_err(CE_WARN, "iscsi session(%u) received itt:0x%x "
1643fcf3ce44SJohn Forte 		    " which is out of sync with itt:0x%x", isp->sess_oid,
1644fcf3ce44SJohn Forte 		    ihp->itt, isp->sess_cmd_table[cmd_table_idx]->cmd_itt);
1645fcf3ce44SJohn Forte 		return (ISCSI_STATUS_INTERNAL_ERROR);
1646fcf3ce44SJohn Forte 	}
1647fcf3ce44SJohn Forte 
164829e23f3fSandrew.rutz@sun.com 	/* ensure that icmdp is still in Active state */
164929e23f3fSandrew.rutz@sun.com 	if (isp->sess_cmd_table[cmd_table_idx]->cmd_state !=
165029e23f3fSandrew.rutz@sun.com 	    ISCSI_CMD_STATE_ACTIVE) {
165129e23f3fSandrew.rutz@sun.com 		cmn_err(CE_WARN, "iscsi session(%u) received itt:0x%x "
165229e23f3fSandrew.rutz@sun.com 		    "but icmdp (%p) is not in active state",
165329e23f3fSandrew.rutz@sun.com 		    isp->sess_oid, ihp->itt,
165429e23f3fSandrew.rutz@sun.com 		    (void *)isp->sess_cmd_table[cmd_table_idx]);
165529e23f3fSandrew.rutz@sun.com 		return (ISCSI_STATUS_INTERNAL_ERROR);
165629e23f3fSandrew.rutz@sun.com 	}
165729e23f3fSandrew.rutz@sun.com 
1658fcf3ce44SJohn Forte 	/* make sure this is a SCSI cmd */
1659fcf3ce44SJohn Forte 	*icmdp = isp->sess_cmd_table[cmd_table_idx];
1660fcf3ce44SJohn Forte 
1661fcf3ce44SJohn Forte 	return (ISCSI_STATUS_SUCCESS);
1662fcf3ce44SJohn Forte }
1663fcf3ce44SJohn Forte 
1664fcf3ce44SJohn Forte /*
1665fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1666fcf3ce44SJohn Forte  * | End of protocol receive routines					|
1667fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1668fcf3ce44SJohn Forte  */
1669fcf3ce44SJohn Forte 
1670fcf3ce44SJohn Forte /*
1671fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1672fcf3ce44SJohn Forte  * | Beginning of protocol send routines				|
1673fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
1674fcf3ce44SJohn Forte  */
1675fcf3ce44SJohn Forte 
1676fcf3ce44SJohn Forte 
1677fcf3ce44SJohn Forte /*
1678fcf3ce44SJohn Forte  * iscsi_tx_thread - This thread is the driving point for all
167930e7468fSPeter Dunlap  * iSCSI PDUs after login.  No PDUs should call idm_pdu_tx()
1680fcf3ce44SJohn Forte  * directly they should be funneled through iscsi_tx_thread.
1681fcf3ce44SJohn Forte  */
1682fcf3ce44SJohn Forte void
1683fcf3ce44SJohn Forte iscsi_tx_thread(iscsi_thread_t *thread, void *arg)
1684fcf3ce44SJohn Forte {
1685fcf3ce44SJohn Forte 	iscsi_conn_t	*icp	= (iscsi_conn_t *)arg;
1686fcf3ce44SJohn Forte 	iscsi_sess_t	*isp	= NULL;
1687fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp	= NULL;
1688fcf3ce44SJohn Forte 	clock_t		tout;
1689fcf3ce44SJohn Forte 	int		ret	= 1;
1690fcf3ce44SJohn Forte 
1691fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
1692fcf3ce44SJohn Forte 	isp = icp->conn_sess;
1693fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
1694fcf3ce44SJohn Forte 	ASSERT(thread != NULL);
1695fcf3ce44SJohn Forte 	ASSERT(thread->signature == SIG_ISCSI_THREAD);
1696fcf3ce44SJohn Forte 
1697fcf3ce44SJohn Forte 	tout = SEC_TO_TICK(1);
1698fcf3ce44SJohn Forte 	/*
1699fcf3ce44SJohn Forte 	 * Transfer icmdps until shutdown by owning session.
1700fcf3ce44SJohn Forte 	 */
1701fcf3ce44SJohn Forte 	while (ret != 0) {
1702fcf3ce44SJohn Forte 
1703fcf3ce44SJohn Forte 		isp->sess_window_open = B_TRUE;
1704fcf3ce44SJohn Forte 		/*
1705fcf3ce44SJohn Forte 		 * While the window is open, there are commands available
1706fcf3ce44SJohn Forte 		 * to send and the session state allows those commands to
1707fcf3ce44SJohn Forte 		 * be sent try to transfer them.
1708fcf3ce44SJohn Forte 		 */
1709fcf3ce44SJohn Forte 		mutex_enter(&isp->sess_queue_pending.mutex);
1710fcf3ce44SJohn Forte 		while ((isp->sess_window_open == B_TRUE) &&
1711fcf3ce44SJohn Forte 		    ((icmdp = isp->sess_queue_pending.head) != NULL) &&
1712fcf3ce44SJohn Forte 		    (((icmdp->cmd_type != ISCSI_CMD_TYPE_SCSI) &&
1713fcf3ce44SJohn Forte 		    (ISCSI_CONN_STATE_FULL_FEATURE(icp->conn_state))) ||
1714fcf3ce44SJohn Forte 		    (icp->conn_state == ISCSI_CONN_STATE_LOGGED_IN))) {
1715fcf3ce44SJohn Forte 
1716fcf3ce44SJohn Forte 			/* update command with this connection info */
1717fcf3ce44SJohn Forte 			icmdp->cmd_conn = icp;
1718fcf3ce44SJohn Forte 			/* attempt to send this command */
1719fcf3ce44SJohn Forte 			iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E2, isp);
1720fcf3ce44SJohn Forte 
1721fcf3ce44SJohn Forte 			ASSERT(!mutex_owned(&isp->sess_queue_pending.mutex));
1722fcf3ce44SJohn Forte 			mutex_enter(&isp->sess_queue_pending.mutex);
1723fcf3ce44SJohn Forte 		}
1724fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_queue_pending.mutex);
1725fcf3ce44SJohn Forte 
1726fcf3ce44SJohn Forte 		/*
1727fcf3ce44SJohn Forte 		 * Go to sleep until there is something new
1728fcf3ce44SJohn Forte 		 * to process (awoken via cv_boardcast).
1729fcf3ce44SJohn Forte 		 * Or the timer goes off.
1730fcf3ce44SJohn Forte 		 */
1731fcf3ce44SJohn Forte 		ret = iscsi_thread_wait(thread, tout);
1732fcf3ce44SJohn Forte 	}
1733fcf3ce44SJohn Forte 
1734fcf3ce44SJohn Forte }
1735fcf3ce44SJohn Forte 
1736fcf3ce44SJohn Forte 
1737fcf3ce44SJohn Forte /*
1738fcf3ce44SJohn Forte  * iscsi_tx_cmd - transfers icmdp across wire as iscsi pdu
1739fcf3ce44SJohn Forte  *
1740fcf3ce44SJohn Forte  * Just prior to sending the command to the networking layer the
1741fcf3ce44SJohn Forte  * pending queue lock will be dropped.  At this point only local
1742fcf3ce44SJohn Forte  * resources will be used, not the icmdp.  Holding the queue lock
1743fcf3ce44SJohn Forte  * across the networking call can lead to a hang.  (This is due
1744fcf3ce44SJohn Forte  * to the the target driver and networking layers competing use
1745fcf3ce44SJohn Forte  * of the timeout() resources and the queue lock being held for
1746fcf3ce44SJohn Forte  * both sides.)  Upon the completion of this command the lock
1747fcf3ce44SJohn Forte  * will have been re-acquired.
1748fcf3ce44SJohn Forte  */
1749fcf3ce44SJohn Forte iscsi_status_t
1750fcf3ce44SJohn Forte iscsi_tx_cmd(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
1751fcf3ce44SJohn Forte {
1752fcf3ce44SJohn Forte 	iscsi_status_t	rval = ISCSI_STATUS_INTERNAL_ERROR;
1753fcf3ce44SJohn Forte 
1754fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
1755fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
1756fcf3ce44SJohn Forte 
1757fcf3ce44SJohn Forte 	/* transfer specific command type */
1758fcf3ce44SJohn Forte 	switch (icmdp->cmd_type) {
1759fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_SCSI:
1760fcf3ce44SJohn Forte 		rval = iscsi_tx_scsi(isp, icmdp);
1761fcf3ce44SJohn Forte 		break;
1762fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_NOP:
1763fcf3ce44SJohn Forte 		rval = iscsi_tx_nop(isp, icmdp);
1764fcf3ce44SJohn Forte 		break;
1765fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_ABORT:
1766fcf3ce44SJohn Forte 		rval = iscsi_tx_abort(isp, icmdp);
1767fcf3ce44SJohn Forte 		break;
1768fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_RESET:
1769fcf3ce44SJohn Forte 		rval = iscsi_tx_reset(isp, icmdp);
1770fcf3ce44SJohn Forte 		break;
1771fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_LOGOUT:
1772fcf3ce44SJohn Forte 		rval = iscsi_tx_logout(isp, icmdp);
1773fcf3ce44SJohn Forte 		break;
1774fcf3ce44SJohn Forte 	case ISCSI_CMD_TYPE_TEXT:
1775fcf3ce44SJohn Forte 		rval = iscsi_tx_text(isp, icmdp);
1776fcf3ce44SJohn Forte 		break;
1777fcf3ce44SJohn Forte 	default:
177830e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi_tx_cmd: invalid cmdtype: %d",
177930e7468fSPeter Dunlap 		    icmdp->cmd_type);
1780fcf3ce44SJohn Forte 		ASSERT(FALSE);
1781fcf3ce44SJohn Forte 	}
1782fcf3ce44SJohn Forte 
1783fcf3ce44SJohn Forte 	ASSERT(!mutex_owned(&isp->sess_queue_pending.mutex));
1784fcf3ce44SJohn Forte 	return (rval);
1785fcf3ce44SJohn Forte }
1786fcf3ce44SJohn Forte 
1787fcf3ce44SJohn Forte /*
1788fcf3ce44SJohn Forte  * a variable length cdb can be up to 16K, but we obviously don't want
1789fcf3ce44SJohn Forte  * to put that on the stack; go with 200 bytes; if we get something
1790fcf3ce44SJohn Forte  * bigger than that we will kmem_alloc a buffer
1791fcf3ce44SJohn Forte  */
1792fcf3ce44SJohn Forte #define	DEF_CDB_LEN	200
1793fcf3ce44SJohn Forte 
1794fcf3ce44SJohn Forte /*
1795fcf3ce44SJohn Forte  * given the size of the cdb, return how many bytes the header takes,
1796fcf3ce44SJohn Forte  * which is the sizeof addl_hdr_t + the CDB size, minus the 16 bytes
1797fcf3ce44SJohn Forte  * stored in the basic header, minus sizeof (ahs_extscb)
1798fcf3ce44SJohn Forte  */
1799fcf3ce44SJohn Forte #define	ADDLHDRSZ(x)		(sizeof (iscsi_addl_hdr_t) + (x) - \
1800fcf3ce44SJohn Forte 					16 - 4)
1801fcf3ce44SJohn Forte 
180230e7468fSPeter Dunlap static void
180330e7468fSPeter Dunlap iscsi_tx_init_hdr(iscsi_sess_t *isp, iscsi_conn_t *icp,
180430e7468fSPeter Dunlap     iscsi_text_hdr_t *ihp, int opcode, uint32_t cmd_itt)
1805fcf3ce44SJohn Forte {
180630e7468fSPeter Dunlap 	ihp->opcode		= opcode;
180730e7468fSPeter Dunlap 	ihp->itt		= cmd_itt;
1808fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
1809fcf3ce44SJohn Forte 	ihp->cmdsn		= htonl(isp->sess_cmdsn);
1810fcf3ce44SJohn Forte 	isp->sess_cmdsn++;
1811fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
1812fcf3ce44SJohn Forte 	ihp->expstatsn		= htonl(icp->conn_expstatsn);
1813fcf3ce44SJohn Forte 	icp->conn_laststatsn = icp->conn_expstatsn;
181430e7468fSPeter Dunlap }
1815fcf3ce44SJohn Forte 
1816fcf3ce44SJohn Forte 
181730e7468fSPeter Dunlap static void
181830e7468fSPeter Dunlap iscsi_tx_scsi_data(iscsi_cmd_t *icmdp, iscsi_scsi_cmd_hdr_t *ihp,
181930e7468fSPeter Dunlap     iscsi_conn_t *icp, idm_pdu_t *pdu)
182030e7468fSPeter Dunlap {
182130e7468fSPeter Dunlap 	struct buf		*bp		= NULL;
182230e7468fSPeter Dunlap 	size_t			buflen		= 0;
182330e7468fSPeter Dunlap 	uint32_t		first_burst_length = 0;
182430e7468fSPeter Dunlap 	struct scsi_pkt		*pkt;
182530e7468fSPeter Dunlap 
182630e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
1827fcf3ce44SJohn Forte 	bp = icmdp->cmd_un.scsi.bp;
1828fcf3ce44SJohn Forte 	if ((bp != NULL) && bp->b_bcount) {
1829fcf3ce44SJohn Forte 		buflen = bp->b_bcount;
183030e7468fSPeter Dunlap 		first_burst_length =
183130e7468fSPeter Dunlap 		    icp->conn_params.first_burst_length;
1832fcf3ce44SJohn Forte 
1833fcf3ce44SJohn Forte 		if (bp->b_flags & B_READ) {
1834fcf3ce44SJohn Forte 			ihp->flags = ISCSI_FLAG_FINAL;
1835fcf3ce44SJohn Forte 			/*
1836fcf3ce44SJohn Forte 			 * fix problem where OS sends bp (B_READ &
1837fcf3ce44SJohn Forte 			 * b_bcount!=0) for a TUR or START_STOP.
1838fcf3ce44SJohn Forte 			 * (comment came from cisco code.)
1839fcf3ce44SJohn Forte 			 */
1840fcf3ce44SJohn Forte 			if ((pkt->pkt_cdbp[0] != SCMD_TEST_UNIT_READY) &&
1841fcf3ce44SJohn Forte 			    (pkt->pkt_cdbp[0] != SCMD_START_STOP)) {
1842fcf3ce44SJohn Forte 				ihp->flags |= ISCSI_FLAG_CMD_READ;
1843fcf3ce44SJohn Forte 				ihp->data_length = htonl(buflen);
1844fcf3ce44SJohn Forte 			}
1845fcf3ce44SJohn Forte 		} else {
1846fcf3ce44SJohn Forte 			ihp->flags = ISCSI_FLAG_CMD_WRITE;
1847fcf3ce44SJohn Forte 			/*
1848fcf3ce44SJohn Forte 			 * FinalBit on the the iSCSI PDU denotes this
1849fcf3ce44SJohn Forte 			 * is the last PDU in the sequence.
1850fcf3ce44SJohn Forte 			 *
1851fcf3ce44SJohn Forte 			 * initial_r2t = true means R2T is required
1852fcf3ce44SJohn Forte 			 * for additional PDU, so there will be no more
1853fcf3ce44SJohn Forte 			 * unsolicited PDUs following
1854fcf3ce44SJohn Forte 			 */
1855fcf3ce44SJohn Forte 			if (icp->conn_params.initial_r2t) {
1856fcf3ce44SJohn Forte 				ihp->flags |= ISCSI_FLAG_FINAL;
1857fcf3ce44SJohn Forte 			}
1858fcf3ce44SJohn Forte 
1859fcf3ce44SJohn Forte 			/* Check if we should send ImmediateData */
1860fcf3ce44SJohn Forte 			if (icp->conn_params.immediate_data) {
186130e7468fSPeter Dunlap 				pdu->isp_data =
186230e7468fSPeter Dunlap 				    (uint8_t *)icmdp->
186330e7468fSPeter Dunlap 				    cmd_un.scsi.bp->b_un.b_addr;
186430e7468fSPeter Dunlap 
186530e7468fSPeter Dunlap 				pdu->isp_datalen = MIN(MIN(buflen,
1866fcf3ce44SJohn Forte 				    first_burst_length),
1867fcf3ce44SJohn Forte 				    icmdp->cmd_conn->conn_params.
1868fcf3ce44SJohn Forte 				    max_xmit_data_seg_len);
1869fcf3ce44SJohn Forte 
1870fcf3ce44SJohn Forte 				/*
1871fcf3ce44SJohn Forte 				 * if everything fits immediate, or
1872fcf3ce44SJohn Forte 				 * we can send all burst data immediate
1873fcf3ce44SJohn Forte 				 * (not unsol), set F
1874fcf3ce44SJohn Forte 				 */
187530e7468fSPeter Dunlap 				/*
187630e7468fSPeter Dunlap 				 * XXX This doesn't look right -- it's not
187730e7468fSPeter Dunlap 				 * clear how we can handle transmitting
187830e7468fSPeter Dunlap 				 * any unsolicited data.  It looks like
187930e7468fSPeter Dunlap 				 * we only support immediate data.  So what
188030e7468fSPeter Dunlap 				 * happens if we don't set ISCSI_FLAG_FINAL?
188130e7468fSPeter Dunlap 				 *
188230e7468fSPeter Dunlap 				 * Unless there's magic code somewhere that
188330e7468fSPeter Dunlap 				 * is sending the remaining PDU's we should
188430e7468fSPeter Dunlap 				 * simply set ISCSI_FLAG_FINAL and forget
188530e7468fSPeter Dunlap 				 * about sending unsolicited data.  The big
188630e7468fSPeter Dunlap 				 * win is the immediate data anyway for small
188730e7468fSPeter Dunlap 				 * PDU's.
188830e7468fSPeter Dunlap 				 */
188930e7468fSPeter Dunlap 				if ((pdu->isp_datalen == buflen) ||
189030e7468fSPeter Dunlap 				    (pdu->isp_datalen == first_burst_length)) {
1891fcf3ce44SJohn Forte 					ihp->flags |= ISCSI_FLAG_FINAL;
1892fcf3ce44SJohn Forte 				}
1893fcf3ce44SJohn Forte 
189430e7468fSPeter Dunlap 				hton24(ihp->dlength, pdu->isp_datalen);
1895fcf3ce44SJohn Forte 			}
1896fcf3ce44SJohn Forte 			/* total data transfer length */
1897fcf3ce44SJohn Forte 			ihp->data_length = htonl(buflen);
1898fcf3ce44SJohn Forte 		}
1899fcf3ce44SJohn Forte 	} else {
1900fcf3ce44SJohn Forte 		ihp->flags = ISCSI_FLAG_FINAL;
1901fcf3ce44SJohn Forte 	}
190230e7468fSPeter Dunlap 	icmdp->cmd_un.scsi.data_transferred += pdu->isp_datalen;
190330e7468fSPeter Dunlap 	/* XXX How is this different from the code above? */
190430e7468fSPeter Dunlap 	/* will idm send the next data command up to burst length? */
190530e7468fSPeter Dunlap 	/* send the burstlen if we haven't sent immediate data */
190630e7468fSPeter Dunlap 	/* CRM: should idm send difference min(buflen, first_burst) and  imm? */
190730e7468fSPeter Dunlap 	/*    (MIN(first_burst_length, buflen) - imdata > 0) */
190830e7468fSPeter Dunlap 	/* CRM_LATER: change this to generate unsolicited pdu */
190930e7468fSPeter Dunlap 	if ((buflen > 0) &&
191030e7468fSPeter Dunlap 	    ((bp->b_flags & B_READ) == 0) &&
191130e7468fSPeter Dunlap 	    (icp->conn_params.initial_r2t == 0) &&
191230e7468fSPeter Dunlap 	    pdu->isp_datalen == 0) {
191330e7468fSPeter Dunlap 
191430e7468fSPeter Dunlap 		pdu->isp_datalen = MIN(first_burst_length, buflen);
191530e7468fSPeter Dunlap 		if ((pdu->isp_datalen == buflen) ||
191630e7468fSPeter Dunlap 		    (pdu->isp_datalen == first_burst_length)) {
191730e7468fSPeter Dunlap 			ihp->flags |= ISCSI_FLAG_FINAL;
191830e7468fSPeter Dunlap 		}
191930e7468fSPeter Dunlap 		pdu->isp_data = (uint8_t *)icmdp->cmd_un.scsi.bp->b_un.b_addr;
192030e7468fSPeter Dunlap 		hton24(ihp->dlength, pdu->isp_datalen);
192130e7468fSPeter Dunlap 	}
192230e7468fSPeter Dunlap }
192330e7468fSPeter Dunlap 
192430e7468fSPeter Dunlap static void
192530e7468fSPeter Dunlap iscsi_tx_scsi_init_pkt(iscsi_cmd_t *icmdp, iscsi_scsi_cmd_hdr_t *ihp)
192630e7468fSPeter Dunlap {
192730e7468fSPeter Dunlap 	struct scsi_pkt *pkt;
192830e7468fSPeter Dunlap 
192930e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
193030e7468fSPeter Dunlap 	pkt->pkt_state = (STATE_GOT_BUS | STATE_GOT_TARGET);
193130e7468fSPeter Dunlap 	pkt->pkt_reason = CMD_INCOMPLETE;
1932fcf3ce44SJohn Forte 
1933fcf3ce44SJohn Forte 	/* tagged queuing */
1934fcf3ce44SJohn Forte 	if (pkt->pkt_flags & FLAG_HTAG) {
1935fcf3ce44SJohn Forte 		ihp->flags |= ISCSI_ATTR_HEAD_OF_QUEUE;
1936fcf3ce44SJohn Forte 	} else if (pkt->pkt_flags & FLAG_OTAG) {
1937fcf3ce44SJohn Forte 		ihp->flags |= ISCSI_ATTR_ORDERED;
1938fcf3ce44SJohn Forte 	} else if (pkt->pkt_flags & FLAG_STAG) {
1939fcf3ce44SJohn Forte 		ihp->flags |= ISCSI_ATTR_SIMPLE;
1940fcf3ce44SJohn Forte 	} else {
1941fcf3ce44SJohn Forte 		/* ihp->flags |= ISCSI_ATTR_UNTAGGED; */
1942fcf3ce44SJohn Forte 		/* EMPTY */
1943fcf3ce44SJohn Forte 	}
1944fcf3ce44SJohn Forte 
1945fcf3ce44SJohn Forte 	/* iscsi states lun is based on spc.2 */
1946fcf3ce44SJohn Forte 	ISCSI_LUN_BYTE_COPY(ihp->lun, icmdp->cmd_un.scsi.lun);
1947fcf3ce44SJohn Forte 
1948fcf3ce44SJohn Forte 	if (icmdp->cmd_un.scsi.cmdlen <= 16) {
1949fcf3ce44SJohn Forte 		/* copy the SCSI Command Block into the PDU */
1950fcf3ce44SJohn Forte 		bcopy(pkt->pkt_cdbp, ihp->scb,
1951fcf3ce44SJohn Forte 		    icmdp->cmd_un.scsi.cmdlen);
1952fcf3ce44SJohn Forte 	} else {
1953fcf3ce44SJohn Forte 		iscsi_addl_hdr_t *iahp;
1954fcf3ce44SJohn Forte 
1955fcf3ce44SJohn Forte 		iahp = (iscsi_addl_hdr_t *)ihp;
1956fcf3ce44SJohn Forte 
1957fcf3ce44SJohn Forte 		ihp->hlength = (ADDLHDRSZ(icmdp->cmd_un.scsi.cmdlen) -
1958fcf3ce44SJohn Forte 		    sizeof (iscsi_scsi_cmd_hdr_t) + 3) / 4;
1959fcf3ce44SJohn Forte 		iahp->ahs_hlen_hi = 0;
1960fcf3ce44SJohn Forte 		iahp->ahs_hlen_lo = (icmdp->cmd_un.scsi.cmdlen - 15);
1961fcf3ce44SJohn Forte 		iahp->ahs_key = 0x01;
1962fcf3ce44SJohn Forte 		iahp->ahs_resv = 0;
1963fcf3ce44SJohn Forte 		bcopy(pkt->pkt_cdbp, ihp->scb, 16);
196430e7468fSPeter Dunlap 		bcopy(((char *)pkt->pkt_cdbp) + 16, &iahp->ahs_extscb[0],
196530e7468fSPeter Dunlap 		    icmdp->cmd_un.scsi.cmdlen);
1966fcf3ce44SJohn Forte 	}
1967fcf3ce44SJohn Forte 
196830e7468fSPeter Dunlap 	/*
196930e7468fSPeter Dunlap 	 * Update all values before transfering.
197030e7468fSPeter Dunlap 	 * We should never touch the icmdp after
197130e7468fSPeter Dunlap 	 * transfering if there is no more data
197230e7468fSPeter Dunlap 	 * to send.  The only case the idm_pdu_tx()
197330e7468fSPeter Dunlap 	 * will fail is a on a connection disconnect
197430e7468fSPeter Dunlap 	 * in that case the command will be flushed.
197530e7468fSPeter Dunlap 	 */
197630e7468fSPeter Dunlap 	pkt->pkt_state |= STATE_SENT_CMD;
1977fcf3ce44SJohn Forte }
1978fcf3ce44SJohn Forte 
197930e7468fSPeter Dunlap static void
198030e7468fSPeter Dunlap iscsi_tx_scsi_init_task(iscsi_cmd_t *icmdp, iscsi_conn_t *icp,
198130e7468fSPeter Dunlap     iscsi_scsi_cmd_hdr_t *ihp)
1982fcf3ce44SJohn Forte {
198330e7468fSPeter Dunlap 	idm_task_t		*itp;
198430e7468fSPeter Dunlap 	struct buf		*bp		= NULL;
198530e7468fSPeter Dunlap 	uint32_t		data_length;
1986fcf3ce44SJohn Forte 
198730e7468fSPeter Dunlap 	bp = icmdp->cmd_un.scsi.bp;
1988fcf3ce44SJohn Forte 
198930e7468fSPeter Dunlap 	itp = icmdp->cmd_itp;
199030e7468fSPeter Dunlap 	ASSERT(itp != NULL);
199130e7468fSPeter Dunlap 	data_length = ntohl(ihp->data_length);
199230e7468fSPeter Dunlap 	ISCSI_IO_LOG(CE_NOTE,
199330e7468fSPeter Dunlap 	    "DEBUG: iscsi_tx_init_task: task_start: %p idt_tt: %x cmdsn: %x "
199430e7468fSPeter Dunlap 	    "sess_cmdsn: %x cmd: %p "
199530e7468fSPeter Dunlap 	    "cmdtype: %d datalen: %u",
199630e7468fSPeter Dunlap 	    (void *)itp, itp->idt_tt, ihp->cmdsn, icp->conn_sess->sess_cmdsn,
199730e7468fSPeter Dunlap 	    (void *)icmdp, icmdp->cmd_type, data_length);
199830e7468fSPeter Dunlap 	if (data_length > 0) {
199930e7468fSPeter Dunlap 		if (bp->b_flags & B_READ) {
200030e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.ibp_ibuf =
200130e7468fSPeter Dunlap 			    idm_buf_alloc(icp->conn_ic,
200230e7468fSPeter Dunlap 			    bp->b_un.b_addr, bp->b_bcount);
200330e7468fSPeter Dunlap 			if (icmdp->cmd_un.scsi.ibp_ibuf)
200430e7468fSPeter Dunlap 				idm_buf_bind_in(itp,
200530e7468fSPeter Dunlap 				    icmdp->cmd_un.scsi.ibp_ibuf);
200630e7468fSPeter Dunlap 		} else {
200730e7468fSPeter Dunlap 			icmdp->cmd_un.scsi.ibp_obuf =
200830e7468fSPeter Dunlap 			    idm_buf_alloc(icp->conn_ic,
200930e7468fSPeter Dunlap 			    bp->b_un.b_addr, bp->b_bcount);
201030e7468fSPeter Dunlap 			if (icmdp->cmd_un.scsi.ibp_obuf)
201130e7468fSPeter Dunlap 				idm_buf_bind_out(itp,
201230e7468fSPeter Dunlap 				    icmdp->cmd_un.scsi.ibp_obuf);
201330e7468fSPeter Dunlap 		}
201430e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE,
201530e7468fSPeter Dunlap 		    "DEBUG: pdu_tx: task_start(%s): %p ic: %p idt_tt: %x "
201630e7468fSPeter Dunlap 		    "cmdsn: %x sess_cmdsn: %x sess_expcmdsn: %x obuf: %p "
201730e7468fSPeter Dunlap 		    "cmdp: %p cmdtype: %d "
201830e7468fSPeter Dunlap 		    "buflen: %lu " "bpaddr: %p datalen: %u ",
201930e7468fSPeter Dunlap 		    bp->b_flags & B_READ ? "B_READ" : "B_WRITE",
202030e7468fSPeter Dunlap 		    (void *)itp, (void *)icp->conn_ic,
202130e7468fSPeter Dunlap 		    itp->idt_tt, ihp->cmdsn,
202230e7468fSPeter Dunlap 		    icp->conn_sess->sess_cmdsn,
202330e7468fSPeter Dunlap 		    icp->conn_sess->sess_expcmdsn,
202430e7468fSPeter Dunlap 		    (void *)icmdp->cmd_un.scsi.ibp_ibuf,
202530e7468fSPeter Dunlap 		    (void *)icmdp, icmdp->cmd_type, bp->b_bcount,
202630e7468fSPeter Dunlap 		    (void *)bp->b_un.b_addr,
202730e7468fSPeter Dunlap 		    data_length);
202830e7468fSPeter Dunlap 	}
2029fcf3ce44SJohn Forte 
2030fcf3ce44SJohn Forte 	/*
203130e7468fSPeter Dunlap 	 * Task is now active
2032fcf3ce44SJohn Forte 	 */
203330e7468fSPeter Dunlap 	idm_task_start(itp, ISCSI_INI_TASK_TTT);
2034fcf3ce44SJohn Forte }
2035fcf3ce44SJohn Forte 
2036fcf3ce44SJohn Forte /*
203730e7468fSPeter Dunlap  * iscsi_tx_scsi -
203830e7468fSPeter Dunlap  *
2039fcf3ce44SJohn Forte  */
2040fcf3ce44SJohn Forte static iscsi_status_t
204130e7468fSPeter Dunlap iscsi_tx_scsi(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2042fcf3ce44SJohn Forte {
2043fcf3ce44SJohn Forte 	iscsi_status_t		rval		= ISCSI_STATUS_SUCCESS;
204430e7468fSPeter Dunlap 	iscsi_conn_t		*icp		= NULL;
204530e7468fSPeter Dunlap 	struct scsi_pkt		*pkt		= NULL;
204630e7468fSPeter Dunlap 	iscsi_scsi_cmd_hdr_t	*ihp		= NULL;
204730e7468fSPeter Dunlap 	int			cdblen		= 0;
204830e7468fSPeter Dunlap 	idm_pdu_t		*pdu;
204930e7468fSPeter Dunlap 	int			len;
2050fcf3ce44SJohn Forte 
2051fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2052fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2053fcf3ce44SJohn Forte 
205430e7468fSPeter Dunlap 	pdu = kmem_zalloc(sizeof (idm_pdu_t), KM_SLEEP);
205530e7468fSPeter Dunlap 
205630e7468fSPeter Dunlap 	pkt = icmdp->cmd_un.scsi.pkt;
205730e7468fSPeter Dunlap 	ASSERT(pkt != NULL);
205830e7468fSPeter Dunlap 	icp = icmdp->cmd_conn;
205930e7468fSPeter Dunlap 	ASSERT(icp != NULL);
206030e7468fSPeter Dunlap 
206130e7468fSPeter Dunlap 	/* Reset counts in case we are on a retry */
206230e7468fSPeter Dunlap 	icmdp->cmd_un.scsi.data_transferred = 0;
206330e7468fSPeter Dunlap 
206430e7468fSPeter Dunlap 	if (icmdp->cmd_un.scsi.cmdlen > DEF_CDB_LEN) {
206530e7468fSPeter Dunlap 		cdblen = icmdp->cmd_un.scsi.cmdlen;
206630e7468fSPeter Dunlap 		ihp = kmem_zalloc(ADDLHDRSZ(cdblen), KM_SLEEP);
206730e7468fSPeter Dunlap 		len = ADDLHDRSZ(cdblen);
206830e7468fSPeter Dunlap 	} else {
206930e7468fSPeter Dunlap 		/*
207030e7468fSPeter Dunlap 		 * only bzero the basic header; the additional header
207130e7468fSPeter Dunlap 		 * will be set up correctly later, if needed
207230e7468fSPeter Dunlap 		 */
207330e7468fSPeter Dunlap 		ihp = kmem_zalloc(sizeof (iscsi_scsi_cmd_hdr_t), KM_SLEEP);
207430e7468fSPeter Dunlap 		len = sizeof (iscsi_scsi_cmd_hdr_t);
2075fcf3ce44SJohn Forte 	}
2076fcf3ce44SJohn Forte 
207730e7468fSPeter Dunlap 	iscsi_tx_init_hdr(isp, icp, (iscsi_text_hdr_t *)ihp,
207830e7468fSPeter Dunlap 	    ISCSI_OP_SCSI_CMD, icmdp->cmd_itt);
207930e7468fSPeter Dunlap 
208030e7468fSPeter Dunlap 	idm_pdu_init(pdu, icp->conn_ic, (void *)icmdp, &iscsi_tx_done);
208130e7468fSPeter Dunlap 	idm_pdu_init_hdr(pdu, (uint8_t *)ihp, len);
208230e7468fSPeter Dunlap 	pdu->isp_data = NULL;
208330e7468fSPeter Dunlap 	pdu->isp_datalen = 0;
2084fcf3ce44SJohn Forte 
2085fcf3ce44SJohn Forte 	/*
208630e7468fSPeter Dunlap 	 * Sestion 12.11 of the iSCSI specification has a good table
208730e7468fSPeter Dunlap 	 * describing when uncolicited data and/or immediate data
208830e7468fSPeter Dunlap 	 * should be sent.
2089fcf3ce44SJohn Forte 	 */
2090fcf3ce44SJohn Forte 
209130e7468fSPeter Dunlap 	iscsi_tx_scsi_data(icmdp, ihp, icp, pdu);
209230e7468fSPeter Dunlap 
209330e7468fSPeter Dunlap 	iscsi_tx_scsi_init_pkt(icmdp, ihp);
209430e7468fSPeter Dunlap 
209530e7468fSPeter Dunlap 	/* Calls idm_task_start */
209630e7468fSPeter Dunlap 	iscsi_tx_scsi_init_task(icmdp, icp, ihp);
209730e7468fSPeter Dunlap 
2098fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
2099fcf3ce44SJohn Forte 
210030e7468fSPeter Dunlap 	idm_pdu_tx(pdu);
2101fcf3ce44SJohn Forte 
210230e7468fSPeter Dunlap 	return (rval);
210330e7468fSPeter Dunlap }
2104fcf3ce44SJohn Forte 
2105fcf3ce44SJohn Forte 
210630e7468fSPeter Dunlap /* ARGSUSED */
210730e7468fSPeter Dunlap static void
210830e7468fSPeter Dunlap iscsi_tx_done(idm_pdu_t *pdu, idm_status_t status)
210930e7468fSPeter Dunlap {
211030e7468fSPeter Dunlap 	kmem_free((iscsi_hdr_t *)pdu->isp_hdr, pdu->isp_hdrlen);
211130e7468fSPeter Dunlap 	kmem_free(pdu, sizeof (idm_pdu_t));
211230e7468fSPeter Dunlap }
2113fcf3ce44SJohn Forte 
2114fcf3ce44SJohn Forte 
211530e7468fSPeter Dunlap static void
211630e7468fSPeter Dunlap iscsi_tx_pdu(iscsi_conn_t *icp, int opcode, void *hdr, int hdrlen,
211730e7468fSPeter Dunlap     iscsi_cmd_t *icmdp)
211830e7468fSPeter Dunlap {
211930e7468fSPeter Dunlap 	idm_pdu_t	*tx_pdu;
212030e7468fSPeter Dunlap 	iscsi_hdr_t	*ihp = (iscsi_hdr_t *)hdr;
2121fcf3ce44SJohn Forte 
212230e7468fSPeter Dunlap 	tx_pdu = kmem_zalloc(sizeof (idm_pdu_t), KM_SLEEP);
212330e7468fSPeter Dunlap 	ASSERT(tx_pdu != NULL);
2124fcf3ce44SJohn Forte 
212530e7468fSPeter Dunlap 	idm_pdu_init(tx_pdu, icp->conn_ic, icmdp, &iscsi_tx_done);
212630e7468fSPeter Dunlap 	idm_pdu_init_hdr(tx_pdu, hdr, hdrlen);
212730e7468fSPeter Dunlap 	if (opcode == ISCSI_OP_TEXT_CMD) {
212830e7468fSPeter Dunlap 		idm_pdu_init_data(tx_pdu,
212930e7468fSPeter Dunlap 		    (uint8_t *)icmdp->cmd_un.text.buf,
213030e7468fSPeter Dunlap 		    ntoh24(ihp->dlength));
2131fcf3ce44SJohn Forte 	}
2132fcf3ce44SJohn Forte 
213330e7468fSPeter Dunlap 	mutex_exit(&icp->conn_sess->sess_queue_pending.mutex);
213430e7468fSPeter Dunlap 	idm_pdu_tx(tx_pdu);
2135fcf3ce44SJohn Forte }
2136fcf3ce44SJohn Forte 
2137fcf3ce44SJohn Forte 
2138fcf3ce44SJohn Forte /*
2139fcf3ce44SJohn Forte  * iscsi_tx_nop -
2140fcf3ce44SJohn Forte  *
2141fcf3ce44SJohn Forte  */
2142fcf3ce44SJohn Forte static iscsi_status_t
2143fcf3ce44SJohn Forte iscsi_tx_nop(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2144fcf3ce44SJohn Forte {
2145fcf3ce44SJohn Forte 	iscsi_status_t		rval	= ISCSI_STATUS_SUCCESS;
2146fcf3ce44SJohn Forte 	iscsi_conn_t		*icp	= NULL;
214730e7468fSPeter Dunlap 	iscsi_nop_out_hdr_t	*inohp;
2148fcf3ce44SJohn Forte 
2149fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2150fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2151fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2152fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2153fcf3ce44SJohn Forte 
215430e7468fSPeter Dunlap 	inohp = kmem_zalloc(sizeof (iscsi_nop_out_hdr_t), KM_SLEEP);
215530e7468fSPeter Dunlap 	ASSERT(inohp != NULL);
215630e7468fSPeter Dunlap 
215730e7468fSPeter Dunlap 	inohp->opcode	= ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
215830e7468fSPeter Dunlap 	inohp->flags	= ISCSI_FLAG_FINAL;
215930e7468fSPeter Dunlap 	inohp->itt	= icmdp->cmd_itt;
216030e7468fSPeter Dunlap 	inohp->ttt	= icmdp->cmd_ttt;
2161fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
216230e7468fSPeter Dunlap 	inohp->cmdsn	= htonl(isp->sess_cmdsn);
2163fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
216430e7468fSPeter Dunlap 	inohp->expstatsn	= htonl(icp->conn_expstatsn);
2165fcf3ce44SJohn Forte 	icp->conn_laststatsn = icp->conn_expstatsn;
216630e7468fSPeter Dunlap 	iscsi_tx_pdu(icp, ISCSI_OP_NOOP_OUT, inohp,
216730e7468fSPeter Dunlap 	    sizeof (iscsi_nop_out_hdr_t), icmdp);
2168fcf3ce44SJohn Forte 	return (rval);
2169fcf3ce44SJohn Forte }
2170fcf3ce44SJohn Forte 
2171fcf3ce44SJohn Forte 
2172fcf3ce44SJohn Forte /*
2173fcf3ce44SJohn Forte  * iscsi_tx_abort -
2174fcf3ce44SJohn Forte  *
2175fcf3ce44SJohn Forte  */
2176fcf3ce44SJohn Forte static iscsi_status_t
2177fcf3ce44SJohn Forte iscsi_tx_abort(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2178fcf3ce44SJohn Forte {
2179fcf3ce44SJohn Forte 	iscsi_status_t			rval	= ISCSI_STATUS_SUCCESS;
2180fcf3ce44SJohn Forte 	iscsi_conn_t			*icp	= NULL;
218130e7468fSPeter Dunlap 	iscsi_scsi_task_mgt_hdr_t	*istmh;
2182fcf3ce44SJohn Forte 
2183fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2184fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2185fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2186fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2187fcf3ce44SJohn Forte 
218830e7468fSPeter Dunlap 	istmh = kmem_zalloc(sizeof (iscsi_scsi_task_mgt_hdr_t), KM_SLEEP);
218930e7468fSPeter Dunlap 	ASSERT(istmh != NULL);
2190fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
219130e7468fSPeter Dunlap 	istmh->cmdsn	= htonl(isp->sess_cmdsn);
2192fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
219330e7468fSPeter Dunlap 	istmh->expstatsn = htonl(icp->conn_expstatsn);
2194fcf3ce44SJohn Forte 	icp->conn_laststatsn = icp->conn_expstatsn;
219530e7468fSPeter Dunlap 	istmh->itt	= icmdp->cmd_itt;
219630e7468fSPeter Dunlap 	istmh->opcode	= ISCSI_OP_SCSI_TASK_MGT_MSG | ISCSI_OP_IMMEDIATE;
219730e7468fSPeter Dunlap 	istmh->function	= ISCSI_FLAG_FINAL | ISCSI_TM_FUNC_ABORT_TASK;
219830e7468fSPeter Dunlap 	ISCSI_LUN_BYTE_COPY(istmh->lun,
2199fcf3ce44SJohn Forte 	    icmdp->cmd_un.abort.icmdp->cmd_un.scsi.lun);
220030e7468fSPeter Dunlap 	istmh->rtt	= icmdp->cmd_un.abort.icmdp->cmd_itt;
220130e7468fSPeter Dunlap 	iscsi_tx_pdu(icp, ISCSI_OP_SCSI_TASK_MGT_MSG, istmh,
220230e7468fSPeter Dunlap 	    sizeof (iscsi_scsi_task_mgt_hdr_t), icmdp);
2203fcf3ce44SJohn Forte 
2204fcf3ce44SJohn Forte 	return (rval);
2205fcf3ce44SJohn Forte }
2206fcf3ce44SJohn Forte 
2207fcf3ce44SJohn Forte 
2208fcf3ce44SJohn Forte /*
2209fcf3ce44SJohn Forte  * iscsi_tx_reset -
2210fcf3ce44SJohn Forte  *
2211fcf3ce44SJohn Forte  */
2212fcf3ce44SJohn Forte static iscsi_status_t
2213fcf3ce44SJohn Forte iscsi_tx_reset(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2214fcf3ce44SJohn Forte {
2215fcf3ce44SJohn Forte 	iscsi_status_t			rval	= ISCSI_STATUS_SUCCESS;
2216fcf3ce44SJohn Forte 	iscsi_conn_t			*icp	= NULL;
221730e7468fSPeter Dunlap 	iscsi_scsi_task_mgt_hdr_t	*istmh;
2218fcf3ce44SJohn Forte 
2219fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2220fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2221fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2222fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2223fcf3ce44SJohn Forte 
222430e7468fSPeter Dunlap 	istmh = kmem_zalloc(sizeof (iscsi_scsi_task_mgt_hdr_t), KM_SLEEP);
222530e7468fSPeter Dunlap 	ASSERT(istmh != NULL);
222630e7468fSPeter Dunlap 	istmh->opcode	= ISCSI_OP_SCSI_TASK_MGT_MSG | ISCSI_OP_IMMEDIATE;
2227fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
222830e7468fSPeter Dunlap 	istmh->cmdsn	= htonl(isp->sess_cmdsn);
2229fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
223030e7468fSPeter Dunlap 	istmh->expstatsn	= htonl(icp->conn_expstatsn);
223130e7468fSPeter Dunlap 	istmh->itt	= icmdp->cmd_itt;
2232fcf3ce44SJohn Forte 
2233fcf3ce44SJohn Forte 	switch (icmdp->cmd_un.reset.level) {
2234fcf3ce44SJohn Forte 	case RESET_LUN:
223530e7468fSPeter Dunlap 		istmh->function	= ISCSI_FLAG_FINAL |
2236fcf3ce44SJohn Forte 		    ISCSI_TM_FUNC_LOGICAL_UNIT_RESET;
223730e7468fSPeter Dunlap 		ISCSI_LUN_BYTE_COPY(istmh->lun, icmdp->cmd_lun->lun_num);
2238fcf3ce44SJohn Forte 		break;
2239fcf3ce44SJohn Forte 	case RESET_TARGET:
2240fcf3ce44SJohn Forte 	case RESET_BUS:
224130e7468fSPeter Dunlap 		istmh->function	= ISCSI_FLAG_FINAL |
2242fcf3ce44SJohn Forte 		    ISCSI_TM_FUNC_TARGET_WARM_RESET;
2243fcf3ce44SJohn Forte 		break;
2244fcf3ce44SJohn Forte 	default:
2245fcf3ce44SJohn Forte 		/* unsupported / unknown level */
2246fcf3ce44SJohn Forte 		ASSERT(FALSE);
2247fcf3ce44SJohn Forte 		break;
2248fcf3ce44SJohn Forte 	}
2249fcf3ce44SJohn Forte 
225030e7468fSPeter Dunlap 	iscsi_tx_pdu(icp, ISCSI_OP_SCSI_TASK_MGT_MSG, istmh,
225130e7468fSPeter Dunlap 	    sizeof (iscsi_scsi_task_mgt_hdr_t), icmdp);
2252fcf3ce44SJohn Forte 
2253fcf3ce44SJohn Forte 	return (rval);
2254fcf3ce44SJohn Forte }
2255fcf3ce44SJohn Forte 
2256fcf3ce44SJohn Forte 
2257fcf3ce44SJohn Forte /*
2258fcf3ce44SJohn Forte  * iscsi_tx_logout -
2259fcf3ce44SJohn Forte  *
2260fcf3ce44SJohn Forte  */
2261fcf3ce44SJohn Forte static iscsi_status_t
2262fcf3ce44SJohn Forte iscsi_tx_logout(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2263fcf3ce44SJohn Forte {
2264fcf3ce44SJohn Forte 	iscsi_status_t		rval	= ISCSI_STATUS_SUCCESS;
2265fcf3ce44SJohn Forte 	iscsi_conn_t		*icp	= NULL;
226630e7468fSPeter Dunlap 	iscsi_logout_hdr_t	*ilh;
2267fcf3ce44SJohn Forte 
2268fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2269fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2270fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2271fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2272fcf3ce44SJohn Forte 
227330e7468fSPeter Dunlap 	ilh = kmem_zalloc(sizeof (iscsi_logout_hdr_t), KM_SLEEP);
227430e7468fSPeter Dunlap 	ilh->opcode	= ISCSI_OP_LOGOUT_CMD | ISCSI_OP_IMMEDIATE;
227530e7468fSPeter Dunlap 	ilh->flags	= ISCSI_FLAG_FINAL | ISCSI_LOGOUT_REASON_CLOSE_SESSION;
227630e7468fSPeter Dunlap 	ilh->itt		= icmdp->cmd_itt;
227730e7468fSPeter Dunlap 	ilh->cid		= icp->conn_cid;
2278fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_cmdsn_mutex);
227930e7468fSPeter Dunlap 	ilh->cmdsn	= htonl(isp->sess_cmdsn);
2280fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_cmdsn_mutex);
228130e7468fSPeter Dunlap 	ilh->expstatsn	= htonl(icp->conn_expstatsn);
228230e7468fSPeter Dunlap 	iscsi_tx_pdu(icp, ISCSI_OP_LOGOUT_CMD, ilh,
228330e7468fSPeter Dunlap 	    sizeof (iscsi_logout_hdr_t), icmdp);
2284fcf3ce44SJohn Forte 
2285fcf3ce44SJohn Forte 	return (rval);
2286fcf3ce44SJohn Forte }
2287fcf3ce44SJohn Forte 
2288fcf3ce44SJohn Forte /*
2289fcf3ce44SJohn Forte  * iscsi_tx_text - setup iSCSI text request header and send PDU with
2290fcf3ce44SJohn Forte  * data given in the buffer attached to the command.  For a single
2291fcf3ce44SJohn Forte  * text request, the target may need to send its response in multiple
2292fcf3ce44SJohn Forte  * text response.  In this case, empty text requests are sent after
2293fcf3ce44SJohn Forte  * each received response to notify the target the initiator is ready
2294fcf3ce44SJohn Forte  * for more response.  For the initial request, the data_len field in
2295fcf3ce44SJohn Forte  * the text specific portion of a command is set to the amount of data
2296fcf3ce44SJohn Forte  * the initiator wants to send as part of the request. If additional
2297fcf3ce44SJohn Forte  * empty text requests are required for long responses, the data_len
2298fcf3ce44SJohn Forte  * field is set to 0 by the iscsi_handle_text function.
2299fcf3ce44SJohn Forte  */
2300fcf3ce44SJohn Forte static iscsi_status_t
2301fcf3ce44SJohn Forte iscsi_tx_text(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
2302fcf3ce44SJohn Forte {
2303fcf3ce44SJohn Forte 	iscsi_status_t		rval	= ISCSI_STATUS_SUCCESS;
2304fcf3ce44SJohn Forte 	iscsi_conn_t		*icp	= NULL;
230530e7468fSPeter Dunlap 	iscsi_text_hdr_t	*ith;
2306fcf3ce44SJohn Forte 
2307fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2308fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2309fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2310fcf3ce44SJohn Forte 
231130e7468fSPeter Dunlap 	ith = kmem_zalloc(sizeof (iscsi_text_hdr_t), KM_SLEEP);
231230e7468fSPeter Dunlap 	ASSERT(ith != NULL);
231330e7468fSPeter Dunlap 	ith->flags	= ISCSI_FLAG_FINAL;
231430e7468fSPeter Dunlap 	hton24(ith->dlength, icmdp->cmd_un.text.data_len);
231530e7468fSPeter Dunlap 	ith->ttt		= icmdp->cmd_un.text.ttt;
231630e7468fSPeter Dunlap 	iscsi_tx_init_hdr(isp, icp, (iscsi_text_hdr_t *)ith,
231730e7468fSPeter Dunlap 	    ISCSI_OP_TEXT_CMD, icmdp->cmd_itt);
231830e7468fSPeter Dunlap 	bcopy(icmdp->cmd_un.text.lun, ith->rsvd4, sizeof (ith->rsvd4));
2319fcf3ce44SJohn Forte 
232030e7468fSPeter Dunlap 	iscsi_tx_pdu(icp, ISCSI_OP_TEXT_CMD, ith, sizeof (iscsi_text_hdr_t),
232130e7468fSPeter Dunlap 	    icmdp);
2322fcf3ce44SJohn Forte 
2323fcf3ce44SJohn Forte 	return (rval);
2324fcf3ce44SJohn Forte }
2325fcf3ce44SJohn Forte 
2326fcf3ce44SJohn Forte /*
2327fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
2328fcf3ce44SJohn Forte  * | End of protocol send routines					|
2329fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
2330fcf3ce44SJohn Forte  */
2331fcf3ce44SJohn Forte 
2332fcf3ce44SJohn Forte /*
2333fcf3ce44SJohn Forte  * iscsi_handle_abort -
2334fcf3ce44SJohn Forte  *
2335fcf3ce44SJohn Forte  */
2336fcf3ce44SJohn Forte void
2337fcf3ce44SJohn Forte iscsi_handle_abort(void *arg)
2338fcf3ce44SJohn Forte {
2339fcf3ce44SJohn Forte 	iscsi_sess_t	*isp		= NULL;
2340fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp		= (iscsi_cmd_t *)arg;
2341fcf3ce44SJohn Forte 	iscsi_cmd_t	*new_icmdp;
2342fcf3ce44SJohn Forte 	iscsi_conn_t	*icp;
2343fcf3ce44SJohn Forte 
2344fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2345fcf3ce44SJohn Forte 	icp = icmdp->cmd_conn;
2346fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2347fcf3ce44SJohn Forte 	isp = icp->conn_sess;
2348fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2349fcf3ce44SJohn Forte 
2350fcf3ce44SJohn Forte 	/* there should only be one abort */
2351fcf3ce44SJohn Forte 	ASSERT(icmdp->cmd_un.scsi.abort_icmdp == NULL);
2352fcf3ce44SJohn Forte 
2353fcf3ce44SJohn Forte 	new_icmdp = iscsi_cmd_alloc(icp, KM_SLEEP);
2354fcf3ce44SJohn Forte 	new_icmdp->cmd_type		= ISCSI_CMD_TYPE_ABORT;
2355fcf3ce44SJohn Forte 	new_icmdp->cmd_lun		= icmdp->cmd_lun;
2356fcf3ce44SJohn Forte 	new_icmdp->cmd_un.abort.icmdp	= icmdp;
2357fcf3ce44SJohn Forte 	new_icmdp->cmd_conn		= icmdp->cmd_conn;
2358fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.abort_icmdp	= new_icmdp;
2359fcf3ce44SJohn Forte 
2360fcf3ce44SJohn Forte 	/* pending queue mutex is already held by timeout_checks */
2361fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(new_icmdp, ISCSI_CMD_EVENT_E1, isp);
2362fcf3ce44SJohn Forte }
2363fcf3ce44SJohn Forte 
236430e7468fSPeter Dunlap /*
236530e7468fSPeter Dunlap  * Callback from IDM indicating that the task has been suspended or aborted.
236630e7468fSPeter Dunlap  */
236730e7468fSPeter Dunlap void
236830e7468fSPeter Dunlap iscsi_task_aborted(idm_task_t *idt, idm_status_t status)
236930e7468fSPeter Dunlap {
237030e7468fSPeter Dunlap 	iscsi_cmd_t *icmdp = idt->idt_private;
237130e7468fSPeter Dunlap 	iscsi_conn_t *icp = icmdp->cmd_conn;
237230e7468fSPeter Dunlap 	iscsi_sess_t *isp = icp->conn_sess;
237330e7468fSPeter Dunlap 
237430e7468fSPeter Dunlap 	ASSERT(icmdp->cmd_conn != NULL);
237530e7468fSPeter Dunlap 
237630e7468fSPeter Dunlap 	switch (status) {
237730e7468fSPeter Dunlap 	case IDM_STATUS_SUSPENDED:
237830e7468fSPeter Dunlap 		/*
237930e7468fSPeter Dunlap 		 * If the task is suspended, it may be aborted later,
238030e7468fSPeter Dunlap 		 * so we can ignore this notification.
238130e7468fSPeter Dunlap 		 */
238230e7468fSPeter Dunlap 		break;
238330e7468fSPeter Dunlap 
238430e7468fSPeter Dunlap 	case IDM_STATUS_ABORTED:
238530e7468fSPeter Dunlap 		mutex_enter(&icp->conn_queue_active.mutex);
238630e7468fSPeter Dunlap 		iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E9, isp);
238730e7468fSPeter Dunlap 		mutex_exit(&icp->conn_queue_active.mutex);
238830e7468fSPeter Dunlap 		break;
238930e7468fSPeter Dunlap 
239030e7468fSPeter Dunlap 	default:
239130e7468fSPeter Dunlap 		/*
239230e7468fSPeter Dunlap 		 * Unexpected status.
239330e7468fSPeter Dunlap 		 */
239430e7468fSPeter Dunlap 		ASSERT(0);
239530e7468fSPeter Dunlap 	}
239630e7468fSPeter Dunlap 
239730e7468fSPeter Dunlap }
2398fcf3ce44SJohn Forte 
2399fcf3ce44SJohn Forte /*
2400fcf3ce44SJohn Forte  * iscsi_handle_nop -
2401fcf3ce44SJohn Forte  *
2402fcf3ce44SJohn Forte  */
2403fcf3ce44SJohn Forte static void
2404fcf3ce44SJohn Forte iscsi_handle_nop(iscsi_conn_t *icp, uint32_t itt, uint32_t ttt)
2405fcf3ce44SJohn Forte {
2406fcf3ce44SJohn Forte 	iscsi_sess_t	*isp	= NULL;
2407fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp	= NULL;
2408fcf3ce44SJohn Forte 
2409fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2410fcf3ce44SJohn Forte 	isp = icp->conn_sess;
2411fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2412fcf3ce44SJohn Forte 
2413fcf3ce44SJohn Forte 	icmdp = iscsi_cmd_alloc(icp, KM_NOSLEEP);
2414fcf3ce44SJohn Forte 	if (icmdp == NULL) {
2415fcf3ce44SJohn Forte 		return;
2416fcf3ce44SJohn Forte 	}
2417fcf3ce44SJohn Forte 
2418fcf3ce44SJohn Forte 	icmdp->cmd_type		= ISCSI_CMD_TYPE_NOP;
2419fcf3ce44SJohn Forte 	icmdp->cmd_itt		= itt;
2420fcf3ce44SJohn Forte 	icmdp->cmd_ttt		= ttt;
2421fcf3ce44SJohn Forte 	icmdp->cmd_lun		= NULL;
2422fcf3ce44SJohn Forte 	icp->conn_nop_lbolt	= ddi_get_lbolt();
2423fcf3ce44SJohn Forte 
2424fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp);
2425fcf3ce44SJohn Forte }
2426fcf3ce44SJohn Forte 
2427fcf3ce44SJohn Forte /*
2428fcf3ce44SJohn Forte  * iscsi_handle_reset -
2429fcf3ce44SJohn Forte  *
2430fcf3ce44SJohn Forte  */
2431fcf3ce44SJohn Forte iscsi_status_t
2432fcf3ce44SJohn Forte iscsi_handle_reset(iscsi_sess_t *isp, int level, iscsi_lun_t *ilp)
2433fcf3ce44SJohn Forte {
2434fcf3ce44SJohn Forte 	iscsi_status_t	rval	= ISCSI_STATUS_SUCCESS;
2435fcf3ce44SJohn Forte 	iscsi_conn_t	*icp;
2436fcf3ce44SJohn Forte 	iscsi_cmd_t	icmd;
2437fcf3ce44SJohn Forte 
2438fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2439fcf3ce44SJohn Forte 
2440fcf3ce44SJohn Forte 	bzero(&icmd, sizeof (iscsi_cmd_t));
2441fcf3ce44SJohn Forte 	icmd.cmd_sig		= ISCSI_SIG_CMD;
2442fcf3ce44SJohn Forte 	icmd.cmd_state		= ISCSI_CMD_STATE_FREE;
2443fcf3ce44SJohn Forte 	icmd.cmd_type		= ISCSI_CMD_TYPE_RESET;
2444fcf3ce44SJohn Forte 	icmd.cmd_lun		= ilp;
2445fcf3ce44SJohn Forte 	icmd.cmd_un.reset.level	= level;
2446fcf3ce44SJohn Forte 	icmd.cmd_result		= ISCSI_STATUS_SUCCESS;
2447fcf3ce44SJohn Forte 	icmd.cmd_completed	= B_FALSE;
2448fcf3ce44SJohn Forte 	mutex_init(&icmd.cmd_mutex, NULL, MUTEX_DRIVER, NULL);
2449fcf3ce44SJohn Forte 	cv_init(&icmd.cmd_completion, NULL, CV_DRIVER, NULL);
2450fcf3ce44SJohn Forte 	/*
2451fcf3ce44SJohn Forte 	 * If we received an IO and we are not in the
2452fcf3ce44SJohn Forte 	 * LOGGED_IN state we are in the process of
2453fcf3ce44SJohn Forte 	 * failing.  Just respond that we are BUSY.
2454fcf3ce44SJohn Forte 	 */
2455fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_state_mutex);
2456fcf3ce44SJohn Forte 	if (!ISCSI_SESS_STATE_FULL_FEATURE(isp->sess_state)) {
2457fcf3ce44SJohn Forte 		/* We aren't connected to the target fake success */
2458fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_state_mutex);
2459fcf3ce44SJohn Forte 		return (ISCSI_STATUS_SUCCESS);
2460fcf3ce44SJohn Forte 	}
2461fcf3ce44SJohn Forte 
2462fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
2463fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(&icmd, ISCSI_CMD_EVENT_E1, isp);
2464fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
2465fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_state_mutex);
2466fcf3ce44SJohn Forte 
2467fcf3ce44SJohn Forte 	/* stall until completed */
2468fcf3ce44SJohn Forte 	mutex_enter(&icmd.cmd_mutex);
2469fcf3ce44SJohn Forte 	while (icmd.cmd_completed == B_FALSE) {
2470fcf3ce44SJohn Forte 		cv_wait(&icmd.cmd_completion, &icmd.cmd_mutex);
2471fcf3ce44SJohn Forte 	}
2472fcf3ce44SJohn Forte 	mutex_exit(&icmd.cmd_mutex);
2473fcf3ce44SJohn Forte 
2474fcf3ce44SJohn Forte 	/* copy rval */
2475fcf3ce44SJohn Forte 	rval = icmd.cmd_result;
2476fcf3ce44SJohn Forte 
2477fcf3ce44SJohn Forte 	if (rval == ISCSI_STATUS_SUCCESS) {
2478fcf3ce44SJohn Forte 		/*
2479fcf3ce44SJohn Forte 		 * Reset was successful.  We need to flush
2480fcf3ce44SJohn Forte 		 * all active IOs.
2481fcf3ce44SJohn Forte 		 */
2482fcf3ce44SJohn Forte 		rw_enter(&isp->sess_conn_list_rwlock, RW_READER);
2483fcf3ce44SJohn Forte 		icp = isp->sess_conn_list;
2484fcf3ce44SJohn Forte 		while (icp != NULL) {
2485fcf3ce44SJohn Forte 			iscsi_cmd_t *t_icmdp = NULL;
2486fcf3ce44SJohn Forte 
2487fcf3ce44SJohn Forte 			mutex_enter(&icp->conn_queue_active.mutex);
2488fcf3ce44SJohn Forte 			t_icmdp = icp->conn_queue_active.head;
2489fcf3ce44SJohn Forte 			while (t_icmdp != NULL) {
2490fcf3ce44SJohn Forte 				iscsi_cmd_state_machine(t_icmdp,
2491fcf3ce44SJohn Forte 				    ISCSI_CMD_EVENT_E7, isp);
2492fcf3ce44SJohn Forte 				t_icmdp = icp->conn_queue_active.head;
2493fcf3ce44SJohn Forte 			}
2494fcf3ce44SJohn Forte 
2495fcf3ce44SJohn Forte 			mutex_exit(&icp->conn_queue_active.mutex);
2496fcf3ce44SJohn Forte 			icp = icp->conn_next;
2497fcf3ce44SJohn Forte 		}
2498fcf3ce44SJohn Forte 		rw_exit(&isp->sess_conn_list_rwlock);
2499fcf3ce44SJohn Forte 	}
2500fcf3ce44SJohn Forte 
2501fcf3ce44SJohn Forte 	/* clean up */
2502fcf3ce44SJohn Forte 	cv_destroy(&icmd.cmd_completion);
2503fcf3ce44SJohn Forte 	mutex_destroy(&icmd.cmd_mutex);
2504fcf3ce44SJohn Forte 
2505fcf3ce44SJohn Forte 	return (rval);
2506fcf3ce44SJohn Forte }
2507fcf3ce44SJohn Forte 
250830e7468fSPeter Dunlap /*
250930e7468fSPeter Dunlap  * iscsi_lgoout_start - task handler for deferred logout
251030e7468fSPeter Dunlap  */
251130e7468fSPeter Dunlap static void
251230e7468fSPeter Dunlap iscsi_logout_start(void *arg)
251330e7468fSPeter Dunlap {
251430e7468fSPeter Dunlap 	iscsi_task_t		*itp = (iscsi_task_t *)arg;
251530e7468fSPeter Dunlap 	iscsi_conn_t		*icp;
251630e7468fSPeter Dunlap 
251730e7468fSPeter Dunlap 	icp = (iscsi_conn_t *)itp->t_arg;
251830e7468fSPeter Dunlap 
251930e7468fSPeter Dunlap 	mutex_enter(&icp->conn_state_mutex);
252030e7468fSPeter Dunlap 	(void) iscsi_handle_logout(icp);
252130e7468fSPeter Dunlap 	mutex_exit(&icp->conn_state_mutex);
252230e7468fSPeter Dunlap }
2523fcf3ce44SJohn Forte 
2524fcf3ce44SJohn Forte /*
2525fcf3ce44SJohn Forte  * iscsi_handle_logout - This function will issue a logout for
2526fcf3ce44SJohn Forte  * the session from a specific connection.
2527fcf3ce44SJohn Forte  */
2528fcf3ce44SJohn Forte iscsi_status_t
2529fcf3ce44SJohn Forte iscsi_handle_logout(iscsi_conn_t *icp)
2530fcf3ce44SJohn Forte {
2531fcf3ce44SJohn Forte 	iscsi_sess_t	*isp;
253230e7468fSPeter Dunlap 	idm_conn_t	*ic;
2533fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp;
2534fcf3ce44SJohn Forte 	int		rval;
2535fcf3ce44SJohn Forte 
2536fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2537fcf3ce44SJohn Forte 	isp = icp->conn_sess;
253830e7468fSPeter Dunlap 	ic = icp->conn_ic;
2539fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2540fcf3ce44SJohn Forte 	ASSERT(isp->sess_hba != NULL);
254130e7468fSPeter Dunlap 	ASSERT(mutex_owned(&icp->conn_state_mutex));
254230e7468fSPeter Dunlap 
254330e7468fSPeter Dunlap 	/*
254430e7468fSPeter Dunlap 	 * We may want to explicitly disconnect if something goes wrong so
254530e7468fSPeter Dunlap 	 * grab a hold to ensure that the IDM connection context can't
254630e7468fSPeter Dunlap 	 * disappear.
254730e7468fSPeter Dunlap 	 */
254830e7468fSPeter Dunlap 	idm_conn_hold(ic);
2549fcf3ce44SJohn Forte 
2550fcf3ce44SJohn Forte 	icmdp = iscsi_cmd_alloc(icp, KM_SLEEP);
2551fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2552fcf3ce44SJohn Forte 	icmdp->cmd_type		= ISCSI_CMD_TYPE_LOGOUT;
2553fcf3ce44SJohn Forte 	icmdp->cmd_result	= ISCSI_STATUS_SUCCESS;
2554fcf3ce44SJohn Forte 	icmdp->cmd_completed	= B_FALSE;
2555fcf3ce44SJohn Forte 
2556fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
2557fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp);
2558fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
2559fcf3ce44SJohn Forte 
2560fcf3ce44SJohn Forte 	/*
2561fcf3ce44SJohn Forte 	 * release connection state mutex to avoid a deadlock.  This
2562fcf3ce44SJohn Forte 	 * function is called from within the connection state
2563fcf3ce44SJohn Forte 	 * machine with the lock held.  When the logout response is
2564fcf3ce44SJohn Forte 	 * received another call to the connection state machine
2565fcf3ce44SJohn Forte 	 * occurs which causes the deadlock
2566fcf3ce44SJohn Forte 	 */
2567fcf3ce44SJohn Forte 	mutex_exit(&icp->conn_state_mutex);
2568fcf3ce44SJohn Forte 
2569fcf3ce44SJohn Forte 	/* stall until completed */
2570fcf3ce44SJohn Forte 	mutex_enter(&icmdp->cmd_mutex);
2571fcf3ce44SJohn Forte 	while (icmdp->cmd_completed == B_FALSE) {
2572fcf3ce44SJohn Forte 		cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex);
2573fcf3ce44SJohn Forte 	}
2574fcf3ce44SJohn Forte 	mutex_exit(&icmdp->cmd_mutex);
2575fcf3ce44SJohn Forte 	mutex_enter(&icp->conn_state_mutex);
2576fcf3ce44SJohn Forte 
2577fcf3ce44SJohn Forte 	/* copy rval */
2578fcf3ce44SJohn Forte 	rval = icmdp->cmd_result;
2579fcf3ce44SJohn Forte 
2580fcf3ce44SJohn Forte 	/* clean up */
2581fcf3ce44SJohn Forte 	iscsi_cmd_free(icmdp);
2582fcf3ce44SJohn Forte 
258330e7468fSPeter Dunlap 	if (rval != 0) {
258430e7468fSPeter Dunlap 		/* If the logout failed then drop the connection */
258530e7468fSPeter Dunlap 		idm_ini_conn_disconnect(icp->conn_ic);
258630e7468fSPeter Dunlap 	}
258730e7468fSPeter Dunlap 
258830e7468fSPeter Dunlap 	/* stall until connection settles */
258930e7468fSPeter Dunlap 	while ((icp->conn_state != ISCSI_CONN_STATE_FREE) &&
259030e7468fSPeter Dunlap 	    (icp->conn_state != ISCSI_CONN_STATE_FAILED) &&
259130e7468fSPeter Dunlap 	    (icp->conn_state != ISCSI_CONN_STATE_POLLING)) {
259230e7468fSPeter Dunlap 		/* wait for transition */
259330e7468fSPeter Dunlap 		cv_wait(&icp->conn_state_change, &icp->conn_state_mutex);
259430e7468fSPeter Dunlap 	}
259530e7468fSPeter Dunlap 
259630e7468fSPeter Dunlap 	idm_conn_rele(ic);
259730e7468fSPeter Dunlap 
259830e7468fSPeter Dunlap 	/*
259930e7468fSPeter Dunlap 	 * Return value reflects whether the logout command completed --
260030e7468fSPeter Dunlap 	 * regardless of the return value the connection is closed and
260130e7468fSPeter Dunlap 	 * ready for reconnection.
260230e7468fSPeter Dunlap 	 */
2603fcf3ce44SJohn Forte 	return (rval);
2604fcf3ce44SJohn Forte }
2605fcf3ce44SJohn Forte 
260630e7468fSPeter Dunlap 
2607fcf3ce44SJohn Forte /*
2608fcf3ce44SJohn Forte  * iscsi_handle_text - main control function for iSCSI text requests.  This
2609fcf3ce44SJohn Forte  * function handles allocating the command, sending initial text request, and
2610fcf3ce44SJohn Forte  * handling long response sequence.
2611fcf3ce44SJohn Forte  * If a data overflow condition occurs, iscsi_handle_text continues to
2612fcf3ce44SJohn Forte  * receive responses until the all data has been recieved.  This allows
2613fcf3ce44SJohn Forte  * the full data length to be returned to the caller.
2614fcf3ce44SJohn Forte  */
2615fcf3ce44SJohn Forte iscsi_status_t
2616fcf3ce44SJohn Forte iscsi_handle_text(iscsi_conn_t *icp, char *buf, uint32_t buf_len,
2617fcf3ce44SJohn Forte     uint32_t data_len, uint32_t *rx_data_len)
2618fcf3ce44SJohn Forte {
2619fcf3ce44SJohn Forte 	iscsi_sess_t	*isp;
2620fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp;
2621fcf3ce44SJohn Forte 	iscsi_status_t	rval	= ISCSI_STATUS_SUCCESS;
2622fcf3ce44SJohn Forte 
2623fcf3ce44SJohn Forte 	ASSERT(icp != NULL);
2624fcf3ce44SJohn Forte 	ASSERT(buf != NULL);
2625fcf3ce44SJohn Forte 	ASSERT(rx_data_len != NULL);
2626fcf3ce44SJohn Forte 
2627fcf3ce44SJohn Forte 	isp = icp->conn_sess;
2628fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2629fcf3ce44SJohn Forte 
2630fcf3ce44SJohn Forte 	/*
2631fcf3ce44SJohn Forte 	 * Ensure data for text request command is not greater
2632fcf3ce44SJohn Forte 	 * than the negotiated maximum receive data seqment length.
2633fcf3ce44SJohn Forte 	 *
2634fcf3ce44SJohn Forte 	 * Although iSCSI allows for long text requests (multiple
2635fcf3ce44SJohn Forte 	 * pdus), this function places a restriction on text
2636fcf3ce44SJohn Forte 	 * requests to ensure it is handled by a single PDU.
2637fcf3ce44SJohn Forte 	 */
2638fcf3ce44SJohn Forte 	if (data_len > icp->conn_params.max_xmit_data_seg_len) {
2639fcf3ce44SJohn Forte 		return (ISCSI_STATUS_CMD_FAILED);
2640fcf3ce44SJohn Forte 	}
2641fcf3ce44SJohn Forte 
2642fcf3ce44SJohn Forte 	icmdp = iscsi_cmd_alloc(icp, KM_SLEEP);
2643fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2644fcf3ce44SJohn Forte 
2645fcf3ce44SJohn Forte 	icmdp->cmd_type		= ISCSI_CMD_TYPE_TEXT;
2646fcf3ce44SJohn Forte 	icmdp->cmd_result	= ISCSI_STATUS_SUCCESS;
2647d233de7eSJack Meng 	icmdp->cmd_misc_flags	&= ~ISCSI_CMD_MISCFLAG_FREE;
2648fcf3ce44SJohn Forte 	icmdp->cmd_completed	= B_FALSE;
2649fcf3ce44SJohn Forte 
2650fcf3ce44SJohn Forte 	icmdp->cmd_un.text.buf		= buf;
2651fcf3ce44SJohn Forte 	icmdp->cmd_un.text.buf_len	= buf_len;
2652fcf3ce44SJohn Forte 	icmdp->cmd_un.text.offset	= 0;
2653fcf3ce44SJohn Forte 	icmdp->cmd_un.text.data_len	= data_len;
2654fcf3ce44SJohn Forte 	icmdp->cmd_un.text.total_rx_len	= 0;
2655fcf3ce44SJohn Forte 	icmdp->cmd_un.text.ttt		= ISCSI_RSVD_TASK_TAG;
2656fcf3ce44SJohn Forte 	icmdp->cmd_un.text.stage	= ISCSI_CMD_TEXT_INITIAL_REQ;
2657fcf3ce44SJohn Forte 
2658fcf3ce44SJohn Forte long_text_response:
2659fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_state_mutex);
2660fcf3ce44SJohn Forte 	if (!ISCSI_SESS_STATE_FULL_FEATURE(isp->sess_state)) {
2661fcf3ce44SJohn Forte 		iscsi_cmd_free(icmdp);
2662fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_state_mutex);
2663fcf3ce44SJohn Forte 		return (ISCSI_STATUS_NO_CONN_LOGGED_IN);
2664fcf3ce44SJohn Forte 	}
2665fcf3ce44SJohn Forte 
2666fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
2667fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp);
2668fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
2669fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_state_mutex);
2670fcf3ce44SJohn Forte 
2671fcf3ce44SJohn Forte 	/* stall until completed */
2672fcf3ce44SJohn Forte 	mutex_enter(&icmdp->cmd_mutex);
2673fcf3ce44SJohn Forte 	while (icmdp->cmd_completed == B_FALSE) {
2674fcf3ce44SJohn Forte 		cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex);
2675fcf3ce44SJohn Forte 	}
2676fcf3ce44SJohn Forte 	mutex_exit(&icmdp->cmd_mutex);
2677fcf3ce44SJohn Forte 
2678fcf3ce44SJohn Forte 	/*
2679fcf3ce44SJohn Forte 	 * check if error occured.  If data overflow occured, continue on
2680fcf3ce44SJohn Forte 	 * to ensure we get all data so that the full data length can be
2681fcf3ce44SJohn Forte 	 * returned to the user
2682fcf3ce44SJohn Forte 	 */
2683fcf3ce44SJohn Forte 	if ((icmdp->cmd_result != ISCSI_STATUS_SUCCESS) &&
2684fcf3ce44SJohn Forte 	    (icmdp->cmd_result != ISCSI_STATUS_DATA_OVERFLOW)) {
2685fcf3ce44SJohn Forte 		cmn_err(CE_NOTE, "iscsi: SendTarget discovery failed (%d)",
2686fcf3ce44SJohn Forte 		    icmdp->cmd_result);
2687fcf3ce44SJohn Forte 		rval = icmdp->cmd_result;
2688fcf3ce44SJohn Forte 		iscsi_cmd_free(icmdp);
2689fcf3ce44SJohn Forte 		return (rval);
2690fcf3ce44SJohn Forte 	}
2691fcf3ce44SJohn Forte 
2692fcf3ce44SJohn Forte 	/* check if this was a partial text PDU  */
2693fcf3ce44SJohn Forte 	if (icmdp->cmd_un.text.stage != ISCSI_CMD_TEXT_FINAL_RSP) {
2694fcf3ce44SJohn Forte 		/*
2695fcf3ce44SJohn Forte 		 * If a paritial text rexponse received, send an empty
2696fcf3ce44SJohn Forte 		 * text request.  This follows the behaviour specified
2697fcf3ce44SJohn Forte 		 * in RFC3720 regarding long text responses.
2698fcf3ce44SJohn Forte 		 */
2699d233de7eSJack Meng 		icmdp->cmd_misc_flags		&= ~ISCSI_CMD_MISCFLAG_FREE;
2700fcf3ce44SJohn Forte 		icmdp->cmd_completed		= B_FALSE;
2701fcf3ce44SJohn Forte 		icmdp->cmd_un.text.data_len	= 0;
2702fcf3ce44SJohn Forte 		icmdp->cmd_un.text.stage	= ISCSI_CMD_TEXT_CONTINUATION;
2703fcf3ce44SJohn Forte 		goto long_text_response;
2704fcf3ce44SJohn Forte 	}
2705fcf3ce44SJohn Forte 
2706fcf3ce44SJohn Forte 	/*
2707fcf3ce44SJohn Forte 	 * set total received data length.  If data overflow this would be
2708fcf3ce44SJohn Forte 	 * amount of data that would have been received if buffer large
2709fcf3ce44SJohn Forte 	 * enough.
2710fcf3ce44SJohn Forte 	 */
2711fcf3ce44SJohn Forte 	*rx_data_len = icmdp->cmd_un.text.total_rx_len;
2712fcf3ce44SJohn Forte 
2713fcf3ce44SJohn Forte 	/* copy rval */
2714fcf3ce44SJohn Forte 	rval = icmdp->cmd_result;
2715fcf3ce44SJohn Forte 
2716fcf3ce44SJohn Forte 	/* clean up  */
2717fcf3ce44SJohn Forte 	iscsi_cmd_free(icmdp);
2718fcf3ce44SJohn Forte 
2719fcf3ce44SJohn Forte 	return (rval);
2720fcf3ce44SJohn Forte }
2721fcf3ce44SJohn Forte 
2722fcf3ce44SJohn Forte /*
2723fcf3ce44SJohn Forte  * iscsi_handle_passthru - This function is used to send a uscsi_cmd
2724fcf3ce44SJohn Forte  * to a specific target lun.  This routine is used for internal purposes
2725fcf3ce44SJohn Forte  * during enumeration and via the ISCSI_USCSICMD IOCTL.  We restrict
2726fcf3ce44SJohn Forte  * the CDBs that can be issued to a target/lun to INQUIRY, REPORT_LUNS,
2727fcf3ce44SJohn Forte  * and READ_CAPACITY for security purposes.
2728fcf3ce44SJohn Forte  *
2729fcf3ce44SJohn Forte  * The logic here is broken into three phases.
2730fcf3ce44SJohn Forte  * 1) Allocate and initialize a pkt/icmdp
2731fcf3ce44SJohn Forte  * 2) Send the pkt/icmdp
2732fcf3ce44SJohn Forte  * 3) cv_wait for completion
2733fcf3ce44SJohn Forte  */
2734fcf3ce44SJohn Forte iscsi_status_t
2735fcf3ce44SJohn Forte iscsi_handle_passthru(iscsi_sess_t *isp, uint16_t lun, struct uscsi_cmd *ucmdp)
2736fcf3ce44SJohn Forte {
2737fcf3ce44SJohn Forte 	iscsi_status_t		rval		= ISCSI_STATUS_SUCCESS;
2738fcf3ce44SJohn Forte 	iscsi_cmd_t		*icmdp		= NULL;
2739fcf3ce44SJohn Forte 	struct scsi_pkt		*pkt		= NULL;
2740fcf3ce44SJohn Forte 	struct buf		*bp		= NULL;
2741fcf3ce44SJohn Forte 	struct scsi_arq_status  *arqstat	= NULL;
2742fcf3ce44SJohn Forte 	int			rqlen		= SENSE_LENGTH;
2743fcf3ce44SJohn Forte 
2744fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
2745fcf3ce44SJohn Forte 	ASSERT(ucmdp != NULL);
2746fcf3ce44SJohn Forte 
2747fcf3ce44SJohn Forte 	/*
2748fcf3ce44SJohn Forte 	 * If the caller didn't provide a sense buffer we need
2749fcf3ce44SJohn Forte 	 * to allocation one to get the scsi status.
2750fcf3ce44SJohn Forte 	 */
2751fcf3ce44SJohn Forte 	if (ucmdp->uscsi_rqlen > SENSE_LENGTH) {
2752fcf3ce44SJohn Forte 		rqlen = ucmdp->uscsi_rqlen;
2753fcf3ce44SJohn Forte 	}
2754fcf3ce44SJohn Forte 
2755fcf3ce44SJohn Forte 	/*
2756fcf3ce44SJohn Forte 	 * Step 1. Setup structs - KM_SLEEP will always succeed
2757fcf3ce44SJohn Forte 	 */
2758fcf3ce44SJohn Forte 	bp = kmem_zalloc(sizeof (struct buf), KM_SLEEP);
2759fcf3ce44SJohn Forte 	ASSERT(bp != NULL);
2760fcf3ce44SJohn Forte 	pkt = kmem_zalloc(sizeof (struct scsi_pkt), KM_SLEEP);
2761fcf3ce44SJohn Forte 	ASSERT(pkt != NULL);
2762fcf3ce44SJohn Forte 	icmdp = iscsi_cmd_alloc(NULL, KM_SLEEP);
2763fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2764fcf3ce44SJohn Forte 
2765fcf3ce44SJohn Forte 	/* setup bp structure */
2766fcf3ce44SJohn Forte 	bp->b_flags		= B_READ;
2767fcf3ce44SJohn Forte 	bp->b_bcount		= ucmdp->uscsi_buflen;
2768fcf3ce44SJohn Forte 	bp->b_un.b_addr		= ucmdp->uscsi_bufaddr;
2769fcf3ce44SJohn Forte 
2770fcf3ce44SJohn Forte 	/* setup scsi_pkt structure */
2771fcf3ce44SJohn Forte 	pkt->pkt_ha_private	= icmdp;
2772fcf3ce44SJohn Forte 	pkt->pkt_scbp		= kmem_zalloc(rqlen, KM_SLEEP);
2773fcf3ce44SJohn Forte 	pkt->pkt_cdbp		= kmem_zalloc(ucmdp->uscsi_cdblen, KM_SLEEP);
2774fcf3ce44SJohn Forte 	/* callback routine for passthru, will wake cv_wait */
2775fcf3ce44SJohn Forte 	pkt->pkt_comp		= iscsi_handle_passthru_callback;
2776fcf3ce44SJohn Forte 	pkt->pkt_time		= ucmdp->uscsi_timeout;
2777fcf3ce44SJohn Forte 
2778fcf3ce44SJohn Forte 	/* setup iscsi_cmd structure */
2779fcf3ce44SJohn Forte 	icmdp->cmd_lun			= NULL;
2780fcf3ce44SJohn Forte 	icmdp->cmd_type			= ISCSI_CMD_TYPE_SCSI;
2781fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.lun		= lun;
2782fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.pkt		= pkt;
2783fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.bp		= bp;
2784fcf3ce44SJohn Forte 	bcopy(ucmdp->uscsi_cdb, pkt->pkt_cdbp, ucmdp->uscsi_cdblen);
2785fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.cmdlen	= ucmdp->uscsi_cdblen;
2786fcf3ce44SJohn Forte 	icmdp->cmd_un.scsi.statuslen	= rqlen;
2787fcf3ce44SJohn Forte 	icmdp->cmd_crc_error_seen	= B_FALSE;
2788fcf3ce44SJohn Forte 	icmdp->cmd_completed		= B_FALSE;
2789fcf3ce44SJohn Forte 	icmdp->cmd_result		= ISCSI_STATUS_SUCCESS;
2790fcf3ce44SJohn Forte 
2791fcf3ce44SJohn Forte 	/*
2792fcf3ce44SJohn Forte 	 * Step 2. Push IO onto pending queue.  If we aren't in
2793fcf3ce44SJohn Forte 	 * FULL_FEATURE we need to fail the IO.
2794fcf3ce44SJohn Forte 	 */
2795fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_state_mutex);
2796fcf3ce44SJohn Forte 	if (!ISCSI_SESS_STATE_FULL_FEATURE(isp->sess_state)) {
2797fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_state_mutex);
2798fcf3ce44SJohn Forte 
2799fcf3ce44SJohn Forte 		iscsi_cmd_free(icmdp);
2800fcf3ce44SJohn Forte 		kmem_free(pkt->pkt_cdbp, ucmdp->uscsi_cdblen);
2801fcf3ce44SJohn Forte 		kmem_free(pkt->pkt_scbp, rqlen);
2802fcf3ce44SJohn Forte 		kmem_free(pkt, sizeof (struct scsi_pkt));
2803fcf3ce44SJohn Forte 		kmem_free(bp, sizeof (struct buf));
2804fcf3ce44SJohn Forte 
2805fcf3ce44SJohn Forte 		return (ISCSI_STATUS_CMD_FAILED);
2806fcf3ce44SJohn Forte 	}
2807fcf3ce44SJohn Forte 
2808fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
2809fcf3ce44SJohn Forte 	iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp);
2810fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
2811fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_state_mutex);
2812fcf3ce44SJohn Forte 
2813fcf3ce44SJohn Forte 	/*
2814fcf3ce44SJohn Forte 	 * Step 3. Wait on cv_wait for completion routine
2815fcf3ce44SJohn Forte 	 */
2816fcf3ce44SJohn Forte 	mutex_enter(&icmdp->cmd_mutex);
2817fcf3ce44SJohn Forte 	while (icmdp->cmd_completed == B_FALSE) {
2818fcf3ce44SJohn Forte 		cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex);
2819fcf3ce44SJohn Forte 	}
2820fcf3ce44SJohn Forte 	mutex_exit(&icmdp->cmd_mutex);
2821fcf3ce44SJohn Forte 
2822fcf3ce44SJohn Forte 	/* copy rval */
2823fcf3ce44SJohn Forte 	rval = icmdp->cmd_result;
2824fcf3ce44SJohn Forte 
2825fcf3ce44SJohn Forte 	ucmdp->uscsi_resid = pkt->pkt_resid;
2826fcf3ce44SJohn Forte 
2827fcf3ce44SJohn Forte 	/* update scsi status */
2828fcf3ce44SJohn Forte 	arqstat = (struct scsi_arq_status *)pkt->pkt_scbp;
2829fcf3ce44SJohn Forte 	ucmdp->uscsi_status = ((char *)&arqstat->sts_status)[0];
2830fcf3ce44SJohn Forte 
2831fcf3ce44SJohn Forte 	/* copy request sense buffers if caller gave space */
2832fcf3ce44SJohn Forte 	if ((ucmdp->uscsi_rqlen > 0) &&
2833fcf3ce44SJohn Forte 	    (ucmdp->uscsi_rqbuf != NULL)) {
2834fcf3ce44SJohn Forte 		bcopy(arqstat, ucmdp->uscsi_rqbuf,
2835fcf3ce44SJohn Forte 		    MIN(sizeof (struct scsi_arq_status), rqlen));
2836fcf3ce44SJohn Forte 	}
2837fcf3ce44SJohn Forte 
2838fcf3ce44SJohn Forte 	/* clean up */
2839fcf3ce44SJohn Forte 	iscsi_cmd_free(icmdp);
2840fcf3ce44SJohn Forte 	kmem_free(pkt->pkt_cdbp, ucmdp->uscsi_cdblen);
2841fcf3ce44SJohn Forte 	kmem_free(pkt->pkt_scbp, rqlen);
2842fcf3ce44SJohn Forte 	kmem_free(pkt, sizeof (struct scsi_pkt));
2843fcf3ce44SJohn Forte 	kmem_free(bp, sizeof (struct buf));
2844fcf3ce44SJohn Forte 
2845fcf3ce44SJohn Forte 	return (rval);
2846fcf3ce44SJohn Forte }
2847fcf3ce44SJohn Forte 
2848fcf3ce44SJohn Forte 
2849fcf3ce44SJohn Forte /*
2850fcf3ce44SJohn Forte  * iscsi_handle_passthru_callback -
2851fcf3ce44SJohn Forte  *
2852fcf3ce44SJohn Forte  */
2853fcf3ce44SJohn Forte static void
2854fcf3ce44SJohn Forte iscsi_handle_passthru_callback(struct scsi_pkt *pkt)
2855fcf3ce44SJohn Forte {
2856fcf3ce44SJohn Forte 	iscsi_cmd_t		*icmdp  = NULL;
2857fcf3ce44SJohn Forte 
2858fcf3ce44SJohn Forte 	ASSERT(pkt != NULL);
2859fcf3ce44SJohn Forte 	icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private;
2860fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
2861fcf3ce44SJohn Forte 
2862fcf3ce44SJohn Forte 	mutex_enter(&icmdp->cmd_mutex);
2863fcf3ce44SJohn Forte 	icmdp->cmd_completed    = B_TRUE;
2864fcf3ce44SJohn Forte 	icmdp->cmd_result	= ISCSI_STATUS_SUCCESS;
2865fcf3ce44SJohn Forte 	cv_broadcast(&icmdp->cmd_completion);
2866fcf3ce44SJohn Forte 	mutex_exit(&icmdp->cmd_mutex);
2867fcf3ce44SJohn Forte 
2868fcf3ce44SJohn Forte }
2869fcf3ce44SJohn Forte 
287030e7468fSPeter Dunlap /*
287130e7468fSPeter Dunlap  * IDM callbacks
287230e7468fSPeter Dunlap  */
287330e7468fSPeter Dunlap void
287430e7468fSPeter Dunlap iscsi_build_hdr(idm_task_t *idm_task, idm_pdu_t *pdu, uint8_t opcode)
287530e7468fSPeter Dunlap {
287630e7468fSPeter Dunlap 	iscsi_cmd_t *icmdp = idm_task->idt_private;
287730e7468fSPeter Dunlap 	iscsi_conn_t *icp = icmdp->cmd_conn;
287830e7468fSPeter Dunlap 	iscsi_data_hdr_t *ihp = (iscsi_data_hdr_t *)pdu->isp_hdr;
287930e7468fSPeter Dunlap 
288030e7468fSPeter Dunlap 	mutex_enter(&icmdp->cmd_mutex);
288130e7468fSPeter Dunlap 	if (opcode == ISCSI_OP_SCSI_DATA) {
288230e7468fSPeter Dunlap 		uint32_t	data_sn;
288330e7468fSPeter Dunlap 		uint32_t	lun;
288430e7468fSPeter Dunlap 		icmdp = idm_task->idt_private;
288530e7468fSPeter Dunlap 		icp = icmdp->cmd_conn;
288630e7468fSPeter Dunlap 		ihp->opcode	= opcode;
288730e7468fSPeter Dunlap 		ihp->itt	= icmdp->cmd_itt;
288830e7468fSPeter Dunlap 		ihp->ttt	= idm_task->idt_r2t_ttt;
288930e7468fSPeter Dunlap 		ihp->expstatsn	= htonl(icp->conn_expstatsn);
289030e7468fSPeter Dunlap 		icp->conn_laststatsn = icp->conn_expstatsn;
289130e7468fSPeter Dunlap 		data_sn = ntohl(ihp->datasn);
289230e7468fSPeter Dunlap 		data_sn++;
289330e7468fSPeter Dunlap 		lun = icmdp->cmd_un.scsi.lun;
289430e7468fSPeter Dunlap 		ISCSI_LUN_BYTE_COPY(ihp->lun, lun);
289530e7468fSPeter Dunlap 		/* CRM: upate_flow_control */
289630e7468fSPeter Dunlap 		ISCSI_IO_LOG(CE_NOTE, "DEBUG: iscsi_build_hdr"
289730e7468fSPeter Dunlap 		    "(ISCSI_OP_SCSI_DATA): task: %p icp: %p ic: %p itt: %x "
289830e7468fSPeter Dunlap 		    "exp: %d data_sn: %d", (void *)idm_task, (void *)icp,
289930e7468fSPeter Dunlap 		    (void *)icp->conn_ic, ihp->itt, icp->conn_expstatsn,
290030e7468fSPeter Dunlap 		    data_sn);
290130e7468fSPeter Dunlap 	} else {
290230e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi_build_hdr: unprocessed build "
290330e7468fSPeter Dunlap 		    "header opcode: %x", opcode);
290430e7468fSPeter Dunlap 	}
290530e7468fSPeter Dunlap 	mutex_exit(&icmdp->cmd_mutex);
290630e7468fSPeter Dunlap }
290730e7468fSPeter Dunlap 
290830e7468fSPeter Dunlap static void
290930e7468fSPeter Dunlap iscsi_process_rsp_status(iscsi_sess_t *isp, iscsi_conn_t *icp,
291030e7468fSPeter Dunlap     idm_status_t status)
291130e7468fSPeter Dunlap {
291230e7468fSPeter Dunlap 	switch (status) {
291330e7468fSPeter Dunlap 	case IDM_STATUS_SUCCESS:
291430e7468fSPeter Dunlap 		if ((isp->sess_state == ISCSI_SESS_STATE_IN_FLUSH) &&
291530e7468fSPeter Dunlap 		    (icp->conn_queue_active.count == 0)) {
291630e7468fSPeter Dunlap 			iscsi_drop_conn_cleanup(icp);
291730e7468fSPeter Dunlap 		}
291830e7468fSPeter Dunlap 		break;
291930e7468fSPeter Dunlap 	case IDM_STATUS_PROTOCOL_ERROR:
292030e7468fSPeter Dunlap 		KSTAT_INC_CONN_ERR_PROTOCOL(icp);
292130e7468fSPeter Dunlap 		iscsi_drop_conn_cleanup(icp);
292230e7468fSPeter Dunlap 		break;
292330e7468fSPeter Dunlap 	default:
292430e7468fSPeter Dunlap 		break;
292530e7468fSPeter Dunlap 	}
292630e7468fSPeter Dunlap }
292730e7468fSPeter Dunlap 
292830e7468fSPeter Dunlap static void
292930e7468fSPeter Dunlap iscsi_drop_conn_cleanup(iscsi_conn_t *icp) {
293030e7468fSPeter Dunlap 	mutex_enter(&icp->conn_state_mutex);
293130e7468fSPeter Dunlap 	idm_ini_conn_disconnect(icp->conn_ic);
293230e7468fSPeter Dunlap 	mutex_exit(&icp->conn_state_mutex);
293330e7468fSPeter Dunlap }
293430e7468fSPeter Dunlap 
293530e7468fSPeter Dunlap void
293630e7468fSPeter Dunlap iscsi_rx_error_pdu(idm_conn_t *ic, idm_pdu_t *pdu, idm_status_t status)
293730e7468fSPeter Dunlap {
293830e7468fSPeter Dunlap 	iscsi_conn_t *icp = (iscsi_conn_t *)ic->ic_handle;
293930e7468fSPeter Dunlap 	iscsi_sess_t *isp;
294030e7468fSPeter Dunlap 
294130e7468fSPeter Dunlap 	ASSERT(icp != NULL);
294230e7468fSPeter Dunlap 	isp = icp->conn_sess;
294330e7468fSPeter Dunlap 	ASSERT(isp != NULL);
294430e7468fSPeter Dunlap 	iscsi_process_rsp_status(isp, icp, status);
294530e7468fSPeter Dunlap 	idm_pdu_complete(pdu, status);
294630e7468fSPeter Dunlap }
294730e7468fSPeter Dunlap 
294830e7468fSPeter Dunlap void
294930e7468fSPeter Dunlap iscsi_rx_misc_pdu(idm_conn_t *ic, idm_pdu_t *pdu)
295030e7468fSPeter Dunlap {
295130e7468fSPeter Dunlap 	iscsi_conn_t 		*icp;
295230e7468fSPeter Dunlap 	iscsi_hdr_t		*ihp	= (iscsi_hdr_t *)pdu->isp_hdr;
295330e7468fSPeter Dunlap 	iscsi_sess_t		*isp;
295430e7468fSPeter Dunlap 	idm_status_t		status;
295530e7468fSPeter Dunlap 
295630e7468fSPeter Dunlap 	icp = ic->ic_handle;
295730e7468fSPeter Dunlap 	isp = icp->conn_sess;
295830e7468fSPeter Dunlap 	isp->sess_rx_lbolt = icp->conn_rx_lbolt = ddi_get_lbolt();
295930e7468fSPeter Dunlap 	switch (ihp->opcode & ISCSI_OPCODE_MASK) {
296030e7468fSPeter Dunlap 	case ISCSI_OP_LOGIN_RSP:
296130e7468fSPeter Dunlap 		status = iscsi_rx_process_login_pdu(ic, pdu);
296230e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
296330e7468fSPeter Dunlap 		break;
296430e7468fSPeter Dunlap 	case ISCSI_OP_LOGOUT_RSP:
296530e7468fSPeter Dunlap 		status = iscsi_rx_process_logout_rsp(ic, pdu);
296630e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
296730e7468fSPeter Dunlap 		break;
296830e7468fSPeter Dunlap 	case ISCSI_OP_REJECT_MSG:
296930e7468fSPeter Dunlap 		status = iscsi_rx_process_reject_rsp(ic, pdu);
297030e7468fSPeter Dunlap 		break;
297130e7468fSPeter Dunlap 	case ISCSI_OP_SCSI_TASK_MGT_RSP:
297230e7468fSPeter Dunlap 		status = iscsi_rx_process_task_mgt_rsp(ic, pdu);
297330e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
297430e7468fSPeter Dunlap 		break;
297530e7468fSPeter Dunlap 	case ISCSI_OP_NOOP_IN:
297630e7468fSPeter Dunlap 		status = iscsi_rx_process_nop(ic, pdu);
297730e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
297830e7468fSPeter Dunlap 		break;
297930e7468fSPeter Dunlap 	case ISCSI_OP_ASYNC_EVENT:
298030e7468fSPeter Dunlap 		status = iscsi_rx_process_async_rsp(ic, pdu);
298130e7468fSPeter Dunlap 		break;
298230e7468fSPeter Dunlap 	case ISCSI_OP_TEXT_RSP:
298330e7468fSPeter Dunlap 		status = iscsi_rx_process_text_rsp(ic, pdu);
298430e7468fSPeter Dunlap 		idm_pdu_complete(pdu, status);
298530e7468fSPeter Dunlap 		break;
298630e7468fSPeter Dunlap 	default:
298730e7468fSPeter Dunlap 		cmn_err(CE_WARN, "iscsi connection(%u) protocol error "
298830e7468fSPeter Dunlap 		    "- received misc unsupported opcode 0x%02x",
298930e7468fSPeter Dunlap 		    icp->conn_oid, ihp->opcode);
299030e7468fSPeter Dunlap 		status = IDM_STATUS_PROTOCOL_ERROR;
299130e7468fSPeter Dunlap 		break;
299230e7468fSPeter Dunlap 	}
299330e7468fSPeter Dunlap 	iscsi_process_rsp_status(isp, icp, status);
299430e7468fSPeter Dunlap }
299530e7468fSPeter Dunlap 
2996fcf3ce44SJohn Forte /*
2997fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
2998fcf3ce44SJohn Forte  * | Beginning of completion routines					|
2999fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3000fcf3ce44SJohn Forte  */
3001fcf3ce44SJohn Forte 
3002fcf3ce44SJohn Forte /*
3003fcf3ce44SJohn Forte  * iscsi_ic_thread -
3004fcf3ce44SJohn Forte  */
3005fcf3ce44SJohn Forte void
3006fcf3ce44SJohn Forte iscsi_ic_thread(iscsi_thread_t *thread, void *arg)
3007fcf3ce44SJohn Forte {
3008fcf3ce44SJohn Forte 	iscsi_sess_t	*isp = (iscsi_sess_t *)arg;
3009fcf3ce44SJohn Forte 	int		ret;
3010fcf3ce44SJohn Forte 	iscsi_queue_t	q;
3011fcf3ce44SJohn Forte 	iscsi_cmd_t	*icmdp;
3012fcf3ce44SJohn Forte 	iscsi_cmd_t	*next_icmdp;
3013fcf3ce44SJohn Forte 
3014fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
3015fcf3ce44SJohn Forte 	ASSERT(thread != NULL);
3016fcf3ce44SJohn Forte 	ASSERT(thread->signature == SIG_ISCSI_THREAD);
3017fcf3ce44SJohn Forte 
3018fcf3ce44SJohn Forte 	for (;;) {
3019fcf3ce44SJohn Forte 
3020fcf3ce44SJohn Forte 		/*
3021fcf3ce44SJohn Forte 		 * We wait till iodone or somebody else wakes us up.
3022fcf3ce44SJohn Forte 		 */
3023fcf3ce44SJohn Forte 		ret = iscsi_thread_wait(thread, -1);
3024fcf3ce44SJohn Forte 
3025fcf3ce44SJohn Forte 		/*
3026fcf3ce44SJohn Forte 		 * The value should never be negative since we never timeout.
3027fcf3ce44SJohn Forte 		 */
3028fcf3ce44SJohn Forte 		ASSERT(ret >= 0);
3029fcf3ce44SJohn Forte 
3030fcf3ce44SJohn Forte 		q.count = 0;
3031fcf3ce44SJohn Forte 		q.head  = NULL;
3032fcf3ce44SJohn Forte 		q.tail  = NULL;
3033fcf3ce44SJohn Forte 		mutex_enter(&isp->sess_queue_completion.mutex);
3034fcf3ce44SJohn Forte 		icmdp = isp->sess_queue_completion.head;
3035fcf3ce44SJohn Forte 		while (icmdp != NULL) {
3036fcf3ce44SJohn Forte 			next_icmdp = icmdp->cmd_next;
3037fcf3ce44SJohn Forte 			mutex_enter(&icmdp->cmd_mutex);
3038fcf3ce44SJohn Forte 			/*
3039fcf3ce44SJohn Forte 			 * check if the associated r2t/abort has finished
304030e7468fSPeter Dunlap 			 * yet.  If not, don't complete the command.
3041fcf3ce44SJohn Forte 			 */
3042fcf3ce44SJohn Forte 			if ((icmdp->cmd_un.scsi.r2t_icmdp == NULL) &&
304330e7468fSPeter Dunlap 			    (icmdp->cmd_un.scsi.abort_icmdp == NULL)) {
3044fcf3ce44SJohn Forte 				mutex_exit(&icmdp->cmd_mutex);
3045fcf3ce44SJohn Forte 				(void) iscsi_dequeue_cmd(&isp->
3046fcf3ce44SJohn Forte 				    sess_queue_completion.head,
3047fcf3ce44SJohn Forte 				    &isp->sess_queue_completion.tail,
3048fcf3ce44SJohn Forte 				    icmdp);
3049fcf3ce44SJohn Forte 				--isp->sess_queue_completion.count;
3050fcf3ce44SJohn Forte 				iscsi_enqueue_cmd_head(&q.head,
3051fcf3ce44SJohn Forte 				    &q.tail, icmdp);
305230e7468fSPeter Dunlap 			} else {
3053fcf3ce44SJohn Forte 				mutex_exit(&icmdp->cmd_mutex);
305430e7468fSPeter Dunlap 			}
3055fcf3ce44SJohn Forte 			icmdp = next_icmdp;
3056fcf3ce44SJohn Forte 		}
3057fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_queue_completion.mutex);
3058fcf3ce44SJohn Forte 		icmdp = q.head;
3059fcf3ce44SJohn Forte 		while (icmdp != NULL) {
3060fcf3ce44SJohn Forte 			next_icmdp = icmdp->cmd_next;
3061fcf3ce44SJohn Forte 			iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E8, isp);
3062fcf3ce44SJohn Forte 			icmdp = next_icmdp;
3063fcf3ce44SJohn Forte 		}
3064fcf3ce44SJohn Forte 
3065fcf3ce44SJohn Forte 		if (ret > 0)
3066fcf3ce44SJohn Forte 			/* Somebody woke us up to work */
3067fcf3ce44SJohn Forte 			continue;
3068fcf3ce44SJohn Forte 		else
3069fcf3ce44SJohn Forte 			/*
3070fcf3ce44SJohn Forte 			 * Somebody woke us up to kill ourselves. We will
3071fcf3ce44SJohn Forte 			 * make sure, however that the completion queue is
3072fcf3ce44SJohn Forte 			 * empty before leaving.  After we've done that it
3073fcf3ce44SJohn Forte 			 * is the originator of the signal that has to make
3074fcf3ce44SJohn Forte 			 * sure no other SCSI command is posted.
3075fcf3ce44SJohn Forte 			 */
3076fcf3ce44SJohn Forte 			break;
3077fcf3ce44SJohn Forte 	}
3078fcf3ce44SJohn Forte 
3079fcf3ce44SJohn Forte }
3080fcf3ce44SJohn Forte 
3081fcf3ce44SJohn Forte /*
3082fcf3ce44SJohn Forte  * iscsi_iodone -
3083fcf3ce44SJohn Forte  *
3084fcf3ce44SJohn Forte  */
3085fcf3ce44SJohn Forte void
3086fcf3ce44SJohn Forte iscsi_iodone(iscsi_sess_t *isp, iscsi_cmd_t *icmdp)
3087fcf3ce44SJohn Forte {
3088fcf3ce44SJohn Forte 	struct scsi_pkt		*pkt	= NULL;
3089fcf3ce44SJohn Forte 	struct buf		*bp	= icmdp->cmd_un.scsi.bp;
3090fcf3ce44SJohn Forte 
3091fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
3092fcf3ce44SJohn Forte 	ASSERT(icmdp != NULL);
3093fcf3ce44SJohn Forte 	pkt = icmdp->cmd_un.scsi.pkt;
3094fcf3ce44SJohn Forte 	ASSERT(pkt != NULL);
3095fcf3ce44SJohn Forte 
3096fcf3ce44SJohn Forte 	ASSERT(icmdp->cmd_un.scsi.abort_icmdp == NULL);
3097fcf3ce44SJohn Forte 	ASSERT(icmdp->cmd_un.scsi.r2t_icmdp == NULL);
3098fcf3ce44SJohn Forte 	if (pkt->pkt_reason == CMD_CMPLT) {
3099fcf3ce44SJohn Forte 		if (bp) {
3100fcf3ce44SJohn Forte 			if (bp->b_flags & B_READ) {
3101fcf3ce44SJohn Forte 				KSTAT_SESS_RX_IO_DONE(isp, bp->b_bcount);
3102fcf3ce44SJohn Forte 			} else {
3103fcf3ce44SJohn Forte 				KSTAT_SESS_TX_IO_DONE(isp, bp->b_bcount);
3104fcf3ce44SJohn Forte 			}
3105fcf3ce44SJohn Forte 		}
3106fcf3ce44SJohn Forte 	}
3107fcf3ce44SJohn Forte 
3108fcf3ce44SJohn Forte 	if (pkt->pkt_flags & FLAG_NOINTR) {
3109fcf3ce44SJohn Forte 		cv_broadcast(&icmdp->cmd_completion);
3110fcf3ce44SJohn Forte 		mutex_exit(&icmdp->cmd_mutex);
3111fcf3ce44SJohn Forte 	} else {
3112fcf3ce44SJohn Forte 		/*
3113fcf3ce44SJohn Forte 		 * Release mutex.  As soon as callback is
3114fcf3ce44SJohn Forte 		 * issued the caller may destroy the command.
3115fcf3ce44SJohn Forte 		 */
3116fcf3ce44SJohn Forte 		mutex_exit(&icmdp->cmd_mutex);
3117fcf3ce44SJohn Forte 		/*
3118fcf3ce44SJohn Forte 		 * We can't just directly call the pk_comp routine.  In
3119fcf3ce44SJohn Forte 		 * many error cases the target driver will use the calling
3120fcf3ce44SJohn Forte 		 * thread to re-drive error handling (reset, retries...)
3121fcf3ce44SJohn Forte 		 * back into the hba driver (iscsi).  If the target redrives
3122fcf3ce44SJohn Forte 		 * a reset back into the iscsi driver off this thead we have
3123fcf3ce44SJohn Forte 		 * a chance of deadlocking. So instead use the io completion
3124fcf3ce44SJohn Forte 		 * thread.
3125fcf3ce44SJohn Forte 		 */
3126fcf3ce44SJohn Forte 		(*icmdp->cmd_un.scsi.pkt->pkt_comp)(icmdp->cmd_un.scsi.pkt);
3127fcf3ce44SJohn Forte 	}
3128fcf3ce44SJohn Forte }
3129fcf3ce44SJohn Forte 
3130fcf3ce44SJohn Forte /*
3131fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3132fcf3ce44SJohn Forte  * | End of completion routines						|
3133fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3134fcf3ce44SJohn Forte  */
3135fcf3ce44SJohn Forte 
3136fcf3ce44SJohn Forte /*
3137fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3138fcf3ce44SJohn Forte  * | Beginning of watchdog routines					|
3139fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3140fcf3ce44SJohn Forte  */
3141fcf3ce44SJohn Forte 
3142fcf3ce44SJohn Forte /*
3143fcf3ce44SJohn Forte  * iscsi_watchdog_thread -
3144fcf3ce44SJohn Forte  *
3145fcf3ce44SJohn Forte  */
3146fcf3ce44SJohn Forte void
3147fcf3ce44SJohn Forte iscsi_wd_thread(iscsi_thread_t *thread, void *arg)
3148fcf3ce44SJohn Forte {
3149fcf3ce44SJohn Forte 	iscsi_sess_t	*isp = (iscsi_sess_t *)arg;
3150fcf3ce44SJohn Forte 	int		rc = 1;
3151fcf3ce44SJohn Forte 
3152fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
3153fcf3ce44SJohn Forte 
3154fcf3ce44SJohn Forte 	while (rc != NULL) {
3155fcf3ce44SJohn Forte 
3156fcf3ce44SJohn Forte 		iscsi_timeout_checks(isp);
3157fcf3ce44SJohn Forte 		iscsi_nop_checks(isp);
3158fcf3ce44SJohn Forte 
3159fcf3ce44SJohn Forte 		rc = iscsi_thread_wait(thread, SEC_TO_TICK(1));
3160fcf3ce44SJohn Forte 	}
3161fcf3ce44SJohn Forte }
3162fcf3ce44SJohn Forte 
3163fcf3ce44SJohn Forte /*
3164fcf3ce44SJohn Forte  * iscsi_timeout_checks -
3165fcf3ce44SJohn Forte  *
3166fcf3ce44SJohn Forte  */
3167fcf3ce44SJohn Forte static void
3168fcf3ce44SJohn Forte iscsi_timeout_checks(iscsi_sess_t *isp)
3169fcf3ce44SJohn Forte {
3170fcf3ce44SJohn Forte 	clock_t		now = ddi_get_lbolt();
3171fcf3ce44SJohn Forte 	iscsi_conn_t	*icp;
317230e7468fSPeter Dunlap 	iscsi_cmd_t	*icmdp, *nicmdp;
3173fcf3ce44SJohn Forte 
3174fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
3175fcf3ce44SJohn Forte 
3176fcf3ce44SJohn Forte 	/* PENDING */
3177fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_state_mutex);
3178fcf3ce44SJohn Forte 	mutex_enter(&isp->sess_queue_pending.mutex);
3179fcf3ce44SJohn Forte 	for (icmdp = isp->sess_queue_pending.head;
3180fcf3ce44SJohn Forte 	    icmdp; icmdp = nicmdp) {
3181fcf3ce44SJohn Forte 		nicmdp = icmdp->cmd_next;
3182fcf3ce44SJohn Forte 
3183fcf3ce44SJohn Forte 		/* Skip entries with no timeout */
3184fcf3ce44SJohn Forte 		if (icmdp->cmd_lbolt_timeout == 0)
3185fcf3ce44SJohn Forte 			continue;
3186fcf3ce44SJohn Forte 
3187fcf3ce44SJohn Forte 		/*
3188fcf3ce44SJohn Forte 		 * Skip pending queue entries for cmd_type values that depend
3189fcf3ce44SJohn Forte 		 * on having an open cmdsn window for successfull transition
3190fcf3ce44SJohn Forte 		 * from pending to the active (i.e. ones that depend on
3191fcf3ce44SJohn Forte 		 * sess_cmdsn .vs. sess_maxcmdsn). For them, the timer starts
3192fcf3ce44SJohn Forte 		 * when they are successfully moved to the active queue by
3193fcf3ce44SJohn Forte 		 * iscsi_cmd_state_pending() code.
3194fcf3ce44SJohn Forte 		 */
3195d233de7eSJack Meng 		/*
3196d233de7eSJack Meng 		 * If the cmd is stuck, at least give it a chance
3197d233de7eSJack Meng 		 * to timeout
3198d233de7eSJack Meng 		 */
3199d233de7eSJack Meng 		if (((icmdp->cmd_type == ISCSI_CMD_TYPE_SCSI) ||
3200d233de7eSJack Meng 		    (icmdp->cmd_type == ISCSI_CMD_TYPE_TEXT)) &&
3201d233de7eSJack Meng 		    !(icmdp->cmd_misc_flags & ISCSI_CMD_MISCFLAG_STUCK))
3202fcf3ce44SJohn Forte 			continue;
3203fcf3ce44SJohn Forte 
3204fcf3ce44SJohn Forte 		/* Skip if timeout still in the future */
3205fcf3ce44SJohn Forte 		if (now <= icmdp->cmd_lbolt_timeout)
3206fcf3ce44SJohn Forte 			continue;
3207fcf3ce44SJohn Forte 
3208fcf3ce44SJohn Forte 		/* timeout */
3209fcf3ce44SJohn Forte 		iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E6, isp);
3210fcf3ce44SJohn Forte 	}
3211fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_queue_pending.mutex);
3212fcf3ce44SJohn Forte 	mutex_exit(&isp->sess_state_mutex);
3213fcf3ce44SJohn Forte 
3214fcf3ce44SJohn Forte 	rw_enter(&isp->sess_conn_list_rwlock, RW_READER);
3215fcf3ce44SJohn Forte 	icp = isp->sess_conn_list;
3216fcf3ce44SJohn Forte 	while (icp != NULL) {
3217fcf3ce44SJohn Forte 
3218fcf3ce44SJohn Forte 		/* ACTIVE */
3219fcf3ce44SJohn Forte 		mutex_enter(&icp->conn_state_mutex);
3220fcf3ce44SJohn Forte 		mutex_enter(&isp->sess_queue_pending.mutex);
3221fcf3ce44SJohn Forte 		mutex_enter(&icp->conn_queue_active.mutex);
3222fcf3ce44SJohn Forte 		for (icmdp = icp->conn_queue_active.head;
3223fcf3ce44SJohn Forte 		    icmdp; icmdp = nicmdp) {
3224fcf3ce44SJohn Forte 			nicmdp = icmdp->cmd_next;
3225fcf3ce44SJohn Forte 
3226fcf3ce44SJohn Forte 			/* Skip entries with no timeout */
3227fcf3ce44SJohn Forte 			if (icmdp->cmd_lbolt_timeout == 0)
3228fcf3ce44SJohn Forte 				continue;
3229fcf3ce44SJohn Forte 
3230fcf3ce44SJohn Forte 			/* Skip if command is not active */
3231fcf3ce44SJohn Forte 			if (icmdp->cmd_state != ISCSI_CMD_STATE_ACTIVE)
3232fcf3ce44SJohn Forte 				continue;
3233fcf3ce44SJohn Forte 
3234fcf3ce44SJohn Forte 			/* Skip if timeout still in the future */
3235fcf3ce44SJohn Forte 			if (now <= icmdp->cmd_lbolt_timeout)
3236fcf3ce44SJohn Forte 				continue;
3237fcf3ce44SJohn Forte 
3238fcf3ce44SJohn Forte 			/* timeout */
3239fcf3ce44SJohn Forte 			iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E6, isp);
3240fcf3ce44SJohn Forte 		}
3241fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_queue_active.mutex);
3242fcf3ce44SJohn Forte 		mutex_exit(&isp->sess_queue_pending.mutex);
3243fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_state_mutex);
3244fcf3ce44SJohn Forte 
3245fcf3ce44SJohn Forte 		icp = icp->conn_next;
3246fcf3ce44SJohn Forte 	}
3247fcf3ce44SJohn Forte 	rw_exit(&isp->sess_conn_list_rwlock);
3248fcf3ce44SJohn Forte }
3249fcf3ce44SJohn Forte 
3250fcf3ce44SJohn Forte /*
3251fcf3ce44SJohn Forte  * iscsi_nop_checks - sends a NOP on idle connections
3252fcf3ce44SJohn Forte  *
3253fcf3ce44SJohn Forte  * This function walks the connections on a session and
3254fcf3ce44SJohn Forte  * issues NOPs on those connections that are in FULL
3255fcf3ce44SJohn Forte  * FEATURE mode and have not received data for the
3256fcf3ce44SJohn Forte  * time period specified by iscsi_nop_delay (global).
3257fcf3ce44SJohn Forte  */
3258fcf3ce44SJohn Forte static void
3259fcf3ce44SJohn Forte iscsi_nop_checks(iscsi_sess_t *isp)
3260fcf3ce44SJohn Forte {
3261fcf3ce44SJohn Forte 	iscsi_conn_t	*icp;
3262fcf3ce44SJohn Forte 
3263fcf3ce44SJohn Forte 	ASSERT(isp != NULL);
3264fcf3ce44SJohn Forte 
3265fcf3ce44SJohn Forte 	if (isp->sess_type == ISCSI_SESS_TYPE_DISCOVERY) {
3266fcf3ce44SJohn Forte 		return;
3267fcf3ce44SJohn Forte 	}
3268fcf3ce44SJohn Forte 
3269fcf3ce44SJohn Forte 	rw_enter(&isp->sess_conn_list_rwlock, RW_READER);
3270fcf3ce44SJohn Forte 	icp = isp->sess_conn_act;
3271fcf3ce44SJohn Forte 	if (icp != NULL) {
3272fcf3ce44SJohn Forte 
3273fcf3ce44SJohn Forte 		mutex_enter(&icp->conn_state_mutex);
3274fcf3ce44SJohn Forte 		if ((ISCSI_CONN_STATE_FULL_FEATURE(icp->conn_state)) &&
3275fcf3ce44SJohn Forte 		    (ddi_get_lbolt() > isp->sess_conn_act->conn_rx_lbolt +
3276fcf3ce44SJohn Forte 		    SEC_TO_TICK(iscsi_nop_delay)) && (ddi_get_lbolt() >
3277fcf3ce44SJohn Forte 		    isp->sess_conn_act->conn_nop_lbolt +
3278fcf3ce44SJohn Forte 		    SEC_TO_TICK(iscsi_nop_delay))) {
3279fcf3ce44SJohn Forte 
3280fcf3ce44SJohn Forte 			/*
3281fcf3ce44SJohn Forte 			 * We haven't received anything from the
3282fcf3ce44SJohn Forte 			 * target is a defined period of time,
3283fcf3ce44SJohn Forte 			 * send NOP to see if the target is alive.
3284fcf3ce44SJohn Forte 			 */
3285fcf3ce44SJohn Forte 			mutex_enter(&isp->sess_queue_pending.mutex);
3286fcf3ce44SJohn Forte 			iscsi_handle_nop(isp->sess_conn_act,
3287fcf3ce44SJohn Forte 			    0, ISCSI_RSVD_TASK_TAG);
3288fcf3ce44SJohn Forte 			mutex_exit(&isp->sess_queue_pending.mutex);
3289fcf3ce44SJohn Forte 		}
3290fcf3ce44SJohn Forte 		mutex_exit(&icp->conn_state_mutex);
3291fcf3ce44SJohn Forte 
3292fcf3ce44SJohn Forte 		icp = icp->conn_next;
3293fcf3ce44SJohn Forte 	}
3294fcf3ce44SJohn Forte 	rw_exit(&isp->sess_conn_list_rwlock);
3295fcf3ce44SJohn Forte }
3296fcf3ce44SJohn Forte 
3297fcf3ce44SJohn Forte /*
3298fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3299fcf3ce44SJohn Forte  * | End of wd routines						|
3300fcf3ce44SJohn Forte  * +--------------------------------------------------------------------+
3301fcf3ce44SJohn Forte  */
3302