1a6d42e7dSPeter Dunlap /*
2a6d42e7dSPeter Dunlap  * CDDL HEADER START
3a6d42e7dSPeter Dunlap  *
4a6d42e7dSPeter Dunlap  * The contents of this file are subject to the terms of the
5a6d42e7dSPeter Dunlap  * Common Development and Distribution License (the "License").
6a6d42e7dSPeter Dunlap  * You may not use this file except in compliance with the License.
7a6d42e7dSPeter Dunlap  *
8a6d42e7dSPeter Dunlap  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9a6d42e7dSPeter Dunlap  * or http://www.opensolaris.org/os/licensing.
10a6d42e7dSPeter Dunlap  * See the License for the specific language governing permissions
11a6d42e7dSPeter Dunlap  * and limitations under the License.
12a6d42e7dSPeter Dunlap  *
13a6d42e7dSPeter Dunlap  * When distributing Covered Code, include this CDDL HEADER in each
14a6d42e7dSPeter Dunlap  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15a6d42e7dSPeter Dunlap  * If applicable, add the following below this CDDL HEADER, with the
16a6d42e7dSPeter Dunlap  * fields enclosed by brackets "[]" replaced with your own identifying
17a6d42e7dSPeter Dunlap  * information: Portions Copyright [yyyy] [name of copyright owner]
18a6d42e7dSPeter Dunlap  *
19a6d42e7dSPeter Dunlap  * CDDL HEADER END
20a6d42e7dSPeter Dunlap  */
21a6d42e7dSPeter Dunlap /*
22*d618d68dSPriya Krishnan  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23a6d42e7dSPeter Dunlap  */
24a6d42e7dSPeter Dunlap 
25a6d42e7dSPeter Dunlap #include <sys/cpuvar.h>
26a6d42e7dSPeter Dunlap #include <sys/types.h>
27a6d42e7dSPeter Dunlap #include <sys/conf.h>
28a6d42e7dSPeter Dunlap #include <sys/stat.h>
29a6d42e7dSPeter Dunlap #include <sys/file.h>
30a6d42e7dSPeter Dunlap #include <sys/ddi.h>
31a6d42e7dSPeter Dunlap #include <sys/sunddi.h>
32a6d42e7dSPeter Dunlap #include <sys/modctl.h>
33a6d42e7dSPeter Dunlap #include <sys/sysmacros.h>
34a6d42e7dSPeter Dunlap #include <sys/socket.h>
35a6d42e7dSPeter Dunlap #include <sys/strsubr.h>
36a6d42e7dSPeter Dunlap #include <sys/nvpair.h>
37a6d42e7dSPeter Dunlap 
38a6d42e7dSPeter Dunlap #include <sys/stmf.h>
39a6d42e7dSPeter Dunlap #include <sys/stmf_ioctl.h>
40a6d42e7dSPeter Dunlap #include <sys/portif.h>
41a6d42e7dSPeter Dunlap #include <sys/idm/idm.h>
42a6d42e7dSPeter Dunlap #include <sys/idm/idm_conn_sm.h>
43a6d42e7dSPeter Dunlap #include <iscsit_isns.h>
44a6d42e7dSPeter Dunlap #include <iscsit.h>
45a6d42e7dSPeter Dunlap 
46a6d42e7dSPeter Dunlap #define	ISCSIT_VERSION		BUILD_DATE "-1.18dev"
47a6d42e7dSPeter Dunlap #define	ISCSIT_NAME_VERSION	"COMSTAR ISCSIT v" ISCSIT_VERSION
48a6d42e7dSPeter Dunlap 
49a6d42e7dSPeter Dunlap /*
50a6d42e7dSPeter Dunlap  * DDI entry points.
51a6d42e7dSPeter Dunlap  */
52a6d42e7dSPeter Dunlap static int iscsit_drv_attach(dev_info_t *, ddi_attach_cmd_t);
53a6d42e7dSPeter Dunlap static int iscsit_drv_detach(dev_info_t *, ddi_detach_cmd_t);
54a6d42e7dSPeter Dunlap static int iscsit_drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
55a6d42e7dSPeter Dunlap static int iscsit_drv_open(dev_t *, int, int, cred_t *);
56a6d42e7dSPeter Dunlap static int iscsit_drv_close(dev_t, int, int, cred_t *);
57a6d42e7dSPeter Dunlap static boolean_t iscsit_drv_busy(void);
58a6d42e7dSPeter Dunlap static int iscsit_drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
59a6d42e7dSPeter Dunlap 
60a6d42e7dSPeter Dunlap extern struct mod_ops mod_miscops;
61a6d42e7dSPeter Dunlap 
62a6d42e7dSPeter Dunlap 
63a6d42e7dSPeter Dunlap static struct cb_ops iscsit_cb_ops = {
64a6d42e7dSPeter Dunlap 	iscsit_drv_open,	/* cb_open */
65a6d42e7dSPeter Dunlap 	iscsit_drv_close,	/* cb_close */
66a6d42e7dSPeter Dunlap 	nodev,			/* cb_strategy */
67a6d42e7dSPeter Dunlap 	nodev,			/* cb_print */
68a6d42e7dSPeter Dunlap 	nodev,			/* cb_dump */
69a6d42e7dSPeter Dunlap 	nodev,			/* cb_read */
70a6d42e7dSPeter Dunlap 	nodev,			/* cb_write */
71a6d42e7dSPeter Dunlap 	iscsit_drv_ioctl,	/* cb_ioctl */
72a6d42e7dSPeter Dunlap 	nodev,			/* cb_devmap */
73a6d42e7dSPeter Dunlap 	nodev,			/* cb_mmap */
74a6d42e7dSPeter Dunlap 	nodev,			/* cb_segmap */
75a6d42e7dSPeter Dunlap 	nochpoll,		/* cb_chpoll */
76a6d42e7dSPeter Dunlap 	ddi_prop_op,		/* cb_prop_op */
77a6d42e7dSPeter Dunlap 	NULL,			/* cb_streamtab */
78a6d42e7dSPeter Dunlap 	D_MP,			/* cb_flag */
79a6d42e7dSPeter Dunlap 	CB_REV,			/* cb_rev */
80a6d42e7dSPeter Dunlap 	nodev,			/* cb_aread */
81a6d42e7dSPeter Dunlap 	nodev,			/* cb_awrite */
82a6d42e7dSPeter Dunlap };
83a6d42e7dSPeter Dunlap 
84a6d42e7dSPeter Dunlap static struct dev_ops iscsit_dev_ops = {
85a6d42e7dSPeter Dunlap 	DEVO_REV,		/* devo_rev */
86a6d42e7dSPeter Dunlap 	0,			/* devo_refcnt */
87a6d42e7dSPeter Dunlap 	iscsit_drv_getinfo,	/* devo_getinfo */
88a6d42e7dSPeter Dunlap 	nulldev,		/* devo_identify */
89a6d42e7dSPeter Dunlap 	nulldev,		/* devo_probe */
90a6d42e7dSPeter Dunlap 	iscsit_drv_attach,	/* devo_attach */
91a6d42e7dSPeter Dunlap 	iscsit_drv_detach,	/* devo_detach */
92a6d42e7dSPeter Dunlap 	nodev,			/* devo_reset */
93a6d42e7dSPeter Dunlap 	&iscsit_cb_ops,		/* devo_cb_ops */
94a6d42e7dSPeter Dunlap 	NULL,			/* devo_bus_ops */
95a6d42e7dSPeter Dunlap 	NULL,			/* devo_power */
9663528ae4SJames Moore 	ddi_quiesce_not_needed,	/* quiesce */
97a6d42e7dSPeter Dunlap };
98a6d42e7dSPeter Dunlap 
99a6d42e7dSPeter Dunlap static struct modldrv modldrv = {
100a6d42e7dSPeter Dunlap 	&mod_driverops,
101a6d42e7dSPeter Dunlap 	"iSCSI Target",
102a6d42e7dSPeter Dunlap 	&iscsit_dev_ops,
103a6d42e7dSPeter Dunlap };
104a6d42e7dSPeter Dunlap 
105a6d42e7dSPeter Dunlap static struct modlinkage modlinkage = {
106a6d42e7dSPeter Dunlap 	MODREV_1,
107a6d42e7dSPeter Dunlap 	&modldrv,
108a6d42e7dSPeter Dunlap 	NULL,
109a6d42e7dSPeter Dunlap };
110a6d42e7dSPeter Dunlap 
111a6d42e7dSPeter Dunlap 
112a6d42e7dSPeter Dunlap iscsit_global_t iscsit_global;
113a6d42e7dSPeter Dunlap 
114a6d42e7dSPeter Dunlap kmem_cache_t	*iscsit_status_pdu_cache;
115a6d42e7dSPeter Dunlap 
116a6d42e7dSPeter Dunlap boolean_t	iscsit_sm_logging = B_FALSE;
117a6d42e7dSPeter Dunlap 
118a6d42e7dSPeter Dunlap static idm_status_t iscsit_init(dev_info_t *dip);
119a6d42e7dSPeter Dunlap static idm_status_t iscsit_enable_svc(iscsit_hostinfo_t *hostinfo);
120a6d42e7dSPeter Dunlap static void iscsit_disable_svc(void);
121a6d42e7dSPeter Dunlap 
122*d618d68dSPriya Krishnan static int
123*d618d68dSPriya Krishnan iscsit_check_cmdsn_and_queue(idm_pdu_t *rx_pdu);
124*d618d68dSPriya Krishnan 
125*d618d68dSPriya Krishnan static void
126*d618d68dSPriya Krishnan iscsit_add_pdu_to_queue(iscsit_sess_t *ist, idm_pdu_t *rx_pdu);
127*d618d68dSPriya Krishnan 
128*d618d68dSPriya Krishnan static idm_pdu_t *
129*d618d68dSPriya Krishnan iscsit_remove_pdu_from_queue(iscsit_sess_t *ist, uint32_t cmdsn);
130*d618d68dSPriya Krishnan 
131*d618d68dSPriya Krishnan static void
132*d618d68dSPriya Krishnan iscsit_process_pdu_in_queue(iscsit_sess_t *ist);
133*d618d68dSPriya Krishnan 
134*d618d68dSPriya Krishnan static void
135*d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor_session(iscsit_sess_t *ist);
136*d618d68dSPriya Krishnan 
137*d618d68dSPriya Krishnan static void
138*d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor(void *arg);
139*d618d68dSPriya Krishnan 
140*d618d68dSPriya Krishnan static void
141*d618d68dSPriya Krishnan iscsit_post_staged_pdu(idm_pdu_t *rx_pdu);
142*d618d68dSPriya Krishnan 
143*d618d68dSPriya Krishnan static void
144*d618d68dSPriya Krishnan iscsit_post_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu);
145*d618d68dSPriya Krishnan 
146a6d42e7dSPeter Dunlap static void
147a6d42e7dSPeter Dunlap iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
148a6d42e7dSPeter Dunlap 
149a6d42e7dSPeter Dunlap static void
150a6d42e7dSPeter Dunlap iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
151a6d42e7dSPeter Dunlap 
152a6d42e7dSPeter Dunlap static void
153a6d42e7dSPeter Dunlap iscsit_pdu_op_login_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
154a6d42e7dSPeter Dunlap 
155a6d42e7dSPeter Dunlap void
156a6d42e7dSPeter Dunlap iscsit_pdu_op_text_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
157a6d42e7dSPeter Dunlap 
158a6d42e7dSPeter Dunlap static void
159a6d42e7dSPeter Dunlap iscsit_pdu_op_logout_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
160a6d42e7dSPeter Dunlap 
161a6d42e7dSPeter Dunlap int iscsit_cmd_window();
162a6d42e7dSPeter Dunlap 
163*d618d68dSPriya Krishnan static  int
164*d618d68dSPriya Krishnan iscsit_sna_lt(uint32_t sn1, uint32_t sn2);
165*d618d68dSPriya Krishnan 
166a6d42e7dSPeter Dunlap void
167a6d42e7dSPeter Dunlap iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
168a6d42e7dSPeter Dunlap 
169a6d42e7dSPeter Dunlap static void
170a6d42e7dSPeter Dunlap iscsit_deferred_dispatch(idm_pdu_t *rx_pdu);
171a6d42e7dSPeter Dunlap 
172a6d42e7dSPeter Dunlap static void
173a6d42e7dSPeter Dunlap iscsit_deferred(void *rx_pdu_void);
174a6d42e7dSPeter Dunlap 
175a6d42e7dSPeter Dunlap static idm_status_t
176a6d42e7dSPeter Dunlap iscsit_conn_accept(idm_conn_t *ic);
177a6d42e7dSPeter Dunlap 
178a6d42e7dSPeter Dunlap static idm_status_t
179a6d42e7dSPeter Dunlap iscsit_ffp_enabled(idm_conn_t *ic);
180a6d42e7dSPeter Dunlap 
181a6d42e7dSPeter Dunlap static idm_status_t
182a6d42e7dSPeter Dunlap iscsit_ffp_disabled(idm_conn_t *ic, idm_ffp_disable_t disable_class);
183a6d42e7dSPeter Dunlap 
184a6d42e7dSPeter Dunlap static idm_status_t
185a6d42e7dSPeter Dunlap iscsit_conn_lost(idm_conn_t *ic);
186a6d42e7dSPeter Dunlap 
187a6d42e7dSPeter Dunlap static idm_status_t
188a6d42e7dSPeter Dunlap iscsit_conn_destroy(idm_conn_t *ic);
189a6d42e7dSPeter Dunlap 
190a6d42e7dSPeter Dunlap static stmf_data_buf_t *
191a6d42e7dSPeter Dunlap iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
192a6d42e7dSPeter Dunlap     uint32_t flags);
193a6d42e7dSPeter Dunlap 
194a6d42e7dSPeter Dunlap static void
195a6d42e7dSPeter Dunlap iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf);
196a6d42e7dSPeter Dunlap 
197a6d42e7dSPeter Dunlap static void
198a6d42e7dSPeter Dunlap iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status);
199a6d42e7dSPeter Dunlap 
200a6d42e7dSPeter Dunlap static void
201a6d42e7dSPeter Dunlap iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status);
202a6d42e7dSPeter Dunlap 
203a6d42e7dSPeter Dunlap static void
204a6d42e7dSPeter Dunlap iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status);
205a6d42e7dSPeter Dunlap 
206a6d42e7dSPeter Dunlap static stmf_status_t
207a6d42e7dSPeter Dunlap iscsit_idm_to_stmf(idm_status_t idmrc);
208a6d42e7dSPeter Dunlap 
209a6d42e7dSPeter Dunlap static iscsit_task_t *
210a6d42e7dSPeter Dunlap iscsit_task_alloc(iscsit_conn_t *ict);
211a6d42e7dSPeter Dunlap 
212a6d42e7dSPeter Dunlap static void
213a6d42e7dSPeter Dunlap iscsit_task_free(iscsit_task_t *itask);
214a6d42e7dSPeter Dunlap 
215a6d42e7dSPeter Dunlap static iscsit_task_t *
216a6d42e7dSPeter Dunlap iscsit_tm_task_alloc(iscsit_conn_t *ict);
217a6d42e7dSPeter Dunlap 
218a6d42e7dSPeter Dunlap static void
219a6d42e7dSPeter Dunlap iscsit_tm_task_free(iscsit_task_t *itask);
220a6d42e7dSPeter Dunlap 
22172cf3143Speter dunlap static idm_status_t
22272cf3143Speter dunlap iscsit_task_start(iscsit_task_t *itask);
22372cf3143Speter dunlap 
22472cf3143Speter dunlap static void
22572cf3143Speter dunlap iscsit_task_done(iscsit_task_t *itask);
22672cf3143Speter dunlap 
227a6d42e7dSPeter Dunlap static int
228a6d42e7dSPeter Dunlap iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags);
229a6d42e7dSPeter Dunlap 
230a6d42e7dSPeter Dunlap static void
231a6d42e7dSPeter Dunlap iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags);
232a6d42e7dSPeter Dunlap 
233a6d42e7dSPeter Dunlap static it_cfg_status_t
234a6d42e7dSPeter Dunlap iscsit_config_merge(it_config_t *cfg);
235a6d42e7dSPeter Dunlap 
236a6d42e7dSPeter Dunlap static idm_status_t
237a6d42e7dSPeter Dunlap iscsit_login_fail(idm_conn_t *ic);
238a6d42e7dSPeter Dunlap 
23972cf3143Speter dunlap static boolean_t iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn);
24072cf3143Speter dunlap static void iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu,
24172cf3143Speter dunlap     uint8_t response, uint8_t cmd_status);
24272cf3143Speter dunlap static void iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu,
24372cf3143Speter dunlap     uint8_t tm_status);
24472cf3143Speter dunlap 
245*d618d68dSPriya Krishnan /*
246*d618d68dSPriya Krishnan  * MC/S: Out-of-order commands are staged on a session-wide wait
247*d618d68dSPriya Krishnan  * queue until a system-tunable threshold is reached. A separate
248*d618d68dSPriya Krishnan  * thread is used to scan the staging queue on all the session,
249*d618d68dSPriya Krishnan  * If a delayed PDU does not arrive within a timeout, the target
250*d618d68dSPriya Krishnan  * will advance to the staged PDU that is next in sequence, skipping
251*d618d68dSPriya Krishnan  * over the missing PDU(s) to go past a hole in the sequence.
252*d618d68dSPriya Krishnan  */
253*d618d68dSPriya Krishnan volatile int rxpdu_queue_threshold = ISCSIT_RXPDU_QUEUE_THRESHOLD;
254*d618d68dSPriya Krishnan 
255*d618d68dSPriya Krishnan static kmutex_t		iscsit_rxpdu_queue_monitor_mutex;
256*d618d68dSPriya Krishnan kthread_t		*iscsit_rxpdu_queue_monitor_thr_id;
257*d618d68dSPriya Krishnan static kt_did_t		iscsit_rxpdu_queue_monitor_thr_did;
258*d618d68dSPriya Krishnan static boolean_t	iscsit_rxpdu_queue_monitor_thr_running;
259*d618d68dSPriya Krishnan static kcondvar_t	iscsit_rxpdu_queue_monitor_cv;
260*d618d68dSPriya Krishnan 
261a6d42e7dSPeter Dunlap int
262a6d42e7dSPeter Dunlap _init(void)
263a6d42e7dSPeter Dunlap {
264a6d42e7dSPeter Dunlap 	int rc;
265a6d42e7dSPeter Dunlap 
266a6d42e7dSPeter Dunlap 	rw_init(&iscsit_global.global_rwlock, NULL, RW_DRIVER, NULL);
2678c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_init(&iscsit_global.global_state_mutex, NULL,
2688c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	    MUTEX_DRIVER, NULL);
269a6d42e7dSPeter Dunlap 	iscsit_global.global_svc_state = ISE_DETACHED;
270a6d42e7dSPeter Dunlap 
271*d618d68dSPriya Krishnan 	mutex_init(&iscsit_rxpdu_queue_monitor_mutex, NULL,
272*d618d68dSPriya Krishnan 	    MUTEX_DRIVER, NULL);
273*d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_thr_id = NULL;
274*d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_thr_running = B_FALSE;
275*d618d68dSPriya Krishnan 	cv_init(&iscsit_rxpdu_queue_monitor_cv, NULL, CV_DEFAULT, NULL);
276*d618d68dSPriya Krishnan 
277a6d42e7dSPeter Dunlap 	if ((rc = mod_install(&modlinkage)) != 0) {
2788c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_destroy(&iscsit_global.global_state_mutex);
279a6d42e7dSPeter Dunlap 		rw_destroy(&iscsit_global.global_rwlock);
280a6d42e7dSPeter Dunlap 		return (rc);
281a6d42e7dSPeter Dunlap 	}
282a6d42e7dSPeter Dunlap 
283a6d42e7dSPeter Dunlap 	return (rc);
284a6d42e7dSPeter Dunlap }
285a6d42e7dSPeter Dunlap 
286a6d42e7dSPeter Dunlap int
287a6d42e7dSPeter Dunlap _info(struct modinfo *modinfop)
288a6d42e7dSPeter Dunlap {
289a6d42e7dSPeter Dunlap 	return (mod_info(&modlinkage, modinfop));
290a6d42e7dSPeter Dunlap }
291a6d42e7dSPeter Dunlap 
292a6d42e7dSPeter Dunlap int
293a6d42e7dSPeter Dunlap _fini(void)
294a6d42e7dSPeter Dunlap {
295a6d42e7dSPeter Dunlap 	int rc;
296a6d42e7dSPeter Dunlap 
297a6d42e7dSPeter Dunlap 	rc = mod_remove(&modlinkage);
298a6d42e7dSPeter Dunlap 
299a6d42e7dSPeter Dunlap 	if (rc == 0) {
300*d618d68dSPriya Krishnan 		mutex_destroy(&iscsit_rxpdu_queue_monitor_mutex);
301*d618d68dSPriya Krishnan 		cv_destroy(&iscsit_rxpdu_queue_monitor_cv);
3028c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_destroy(&iscsit_global.global_state_mutex);
303a6d42e7dSPeter Dunlap 		rw_destroy(&iscsit_global.global_rwlock);
304a6d42e7dSPeter Dunlap 	}
305a6d42e7dSPeter Dunlap 
306a6d42e7dSPeter Dunlap 	return (rc);
307a6d42e7dSPeter Dunlap }
308a6d42e7dSPeter Dunlap 
309a6d42e7dSPeter Dunlap /*
310a6d42e7dSPeter Dunlap  * DDI entry points.
311a6d42e7dSPeter Dunlap  */
312a6d42e7dSPeter Dunlap 
313a6d42e7dSPeter Dunlap /* ARGSUSED */
314a6d42e7dSPeter Dunlap static int
315a6d42e7dSPeter Dunlap iscsit_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,
316a6d42e7dSPeter Dunlap     void **result)
317a6d42e7dSPeter Dunlap {
318a6d42e7dSPeter Dunlap 	ulong_t instance = getminor((dev_t)arg);
319a6d42e7dSPeter Dunlap 
320a6d42e7dSPeter Dunlap 	switch (cmd) {
321a6d42e7dSPeter Dunlap 	case DDI_INFO_DEVT2DEVINFO:
322a6d42e7dSPeter Dunlap 		*result = iscsit_global.global_dip;
323a6d42e7dSPeter Dunlap 		return (DDI_SUCCESS);
324a6d42e7dSPeter Dunlap 
325a6d42e7dSPeter Dunlap 	case DDI_INFO_DEVT2INSTANCE:
326a6d42e7dSPeter Dunlap 		*result = (void *)instance;
327a6d42e7dSPeter Dunlap 		return (DDI_SUCCESS);
328a6d42e7dSPeter Dunlap 
329a6d42e7dSPeter Dunlap 	default:
330a6d42e7dSPeter Dunlap 		break;
331a6d42e7dSPeter Dunlap 	}
332a6d42e7dSPeter Dunlap 
333a6d42e7dSPeter Dunlap 	return (DDI_FAILURE);
334a6d42e7dSPeter Dunlap }
335a6d42e7dSPeter Dunlap 
336a6d42e7dSPeter Dunlap static int
337a6d42e7dSPeter Dunlap iscsit_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
338a6d42e7dSPeter Dunlap {
339a6d42e7dSPeter Dunlap 	if (cmd != DDI_ATTACH) {
340a6d42e7dSPeter Dunlap 		return (DDI_FAILURE);
341a6d42e7dSPeter Dunlap 	}
342a6d42e7dSPeter Dunlap 
343a6d42e7dSPeter Dunlap 	if (ddi_get_instance(dip) != 0) {
344a6d42e7dSPeter Dunlap 		/* we only allow instance 0 to attach */
345a6d42e7dSPeter Dunlap 		return (DDI_FAILURE);
346a6d42e7dSPeter Dunlap 	}
347a6d42e7dSPeter Dunlap 
348a6d42e7dSPeter Dunlap 	/* create the minor node */
349a6d42e7dSPeter Dunlap 	if (ddi_create_minor_node(dip, ISCSIT_MODNAME, S_IFCHR, 0,
350a6d42e7dSPeter Dunlap 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
351a6d42e7dSPeter Dunlap 		cmn_err(CE_WARN, "iscsit_drv_attach: "
352a6d42e7dSPeter Dunlap 		    "failed creating minor node");
353a6d42e7dSPeter Dunlap 		return (DDI_FAILURE);
354a6d42e7dSPeter Dunlap 	}
355a6d42e7dSPeter Dunlap 
356a6d42e7dSPeter Dunlap 	if (iscsit_init(dip) != IDM_STATUS_SUCCESS) {
357a6d42e7dSPeter Dunlap 		cmn_err(CE_WARN, "iscsit_drv_attach: "
358a6d42e7dSPeter Dunlap 		    "failed to initialize");
359a6d42e7dSPeter Dunlap 		ddi_remove_minor_node(dip, NULL);
360a6d42e7dSPeter Dunlap 		return (DDI_FAILURE);
361a6d42e7dSPeter Dunlap 	}
362a6d42e7dSPeter Dunlap 
363a6d42e7dSPeter Dunlap 	iscsit_global.global_svc_state = ISE_DISABLED;
364a6d42e7dSPeter Dunlap 	iscsit_global.global_dip = dip;
365a6d42e7dSPeter Dunlap 
366a6d42e7dSPeter Dunlap 	return (DDI_SUCCESS);
367a6d42e7dSPeter Dunlap }
368a6d42e7dSPeter Dunlap 
369a6d42e7dSPeter Dunlap /*ARGSUSED*/
370a6d42e7dSPeter Dunlap static int
371a6d42e7dSPeter Dunlap iscsit_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
372a6d42e7dSPeter Dunlap {
373a6d42e7dSPeter Dunlap 	if (cmd != DDI_DETACH)
374a6d42e7dSPeter Dunlap 		return (DDI_FAILURE);
375a6d42e7dSPeter Dunlap 
3768c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	/*
3778c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * drv_detach is called in a context that owns the
3788c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * device node for the /dev/pseudo device.  If this thread blocks
3798c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * for any resource, other threads that need the /dev/pseudo device
3808c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * may end up in a deadlock with this thread.Hence, we use a
3818c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * separate lock just for the structures that drv_detach needs
3828c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * to access.
3838c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 */
3848c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_enter(&iscsit_global.global_state_mutex);
385a6d42e7dSPeter Dunlap 	if (iscsit_drv_busy()) {
3868c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
387a6d42e7dSPeter Dunlap 		return (EBUSY);
388a6d42e7dSPeter Dunlap 	}
389a6d42e7dSPeter Dunlap 
390a6d42e7dSPeter Dunlap 	iscsit_global.global_dip = NULL;
391a6d42e7dSPeter Dunlap 	ddi_remove_minor_node(dip, NULL);
392a6d42e7dSPeter Dunlap 
393a6d42e7dSPeter Dunlap 	ldi_ident_release(iscsit_global.global_li);
394a6d42e7dSPeter Dunlap 	iscsit_global.global_svc_state = ISE_DETACHED;
395a6d42e7dSPeter Dunlap 
3968c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_exit(&iscsit_global.global_state_mutex);
397a6d42e7dSPeter Dunlap 
398a6d42e7dSPeter Dunlap 	return (DDI_SUCCESS);
399a6d42e7dSPeter Dunlap }
400a6d42e7dSPeter Dunlap 
401a6d42e7dSPeter Dunlap /*ARGSUSED*/
402a6d42e7dSPeter Dunlap static int
403a6d42e7dSPeter Dunlap iscsit_drv_open(dev_t *devp, int flag, int otyp, cred_t *credp)
404a6d42e7dSPeter Dunlap {
405a6d42e7dSPeter Dunlap 	return (0);
406a6d42e7dSPeter Dunlap }
407a6d42e7dSPeter Dunlap 
408a6d42e7dSPeter Dunlap /* ARGSUSED */
409a6d42e7dSPeter Dunlap static int
410a6d42e7dSPeter Dunlap iscsit_drv_close(dev_t dev, int flag, int otyp, cred_t *credp)
411a6d42e7dSPeter Dunlap {
412a6d42e7dSPeter Dunlap 	return (0);
413a6d42e7dSPeter Dunlap }
414a6d42e7dSPeter Dunlap 
415a6d42e7dSPeter Dunlap static boolean_t
416a6d42e7dSPeter Dunlap iscsit_drv_busy(void)
417a6d42e7dSPeter Dunlap {
4188c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	ASSERT(MUTEX_HELD(&iscsit_global.global_state_mutex));
4198c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
420a6d42e7dSPeter Dunlap 	switch (iscsit_global.global_svc_state) {
421a6d42e7dSPeter Dunlap 	case ISE_DISABLED:
422a6d42e7dSPeter Dunlap 	case ISE_DETACHED:
423a6d42e7dSPeter Dunlap 		return (B_FALSE);
424a6d42e7dSPeter Dunlap 	default:
425a6d42e7dSPeter Dunlap 		return (B_TRUE);
426a6d42e7dSPeter Dunlap 	}
427a6d42e7dSPeter Dunlap 	/* NOTREACHED */
428a6d42e7dSPeter Dunlap }
429a6d42e7dSPeter Dunlap 
430a6d42e7dSPeter Dunlap /* ARGSUSED */
431a6d42e7dSPeter Dunlap static int
432a6d42e7dSPeter Dunlap iscsit_drv_ioctl(dev_t drv, int cmd, intptr_t argp, int flag, cred_t *cred,
433a6d42e7dSPeter Dunlap     int *retval)
434a6d42e7dSPeter Dunlap {
435a6d42e7dSPeter Dunlap 	iscsit_ioc_set_config_t		setcfg;
436a6d42e7dSPeter Dunlap 	iscsit_ioc_set_config32_t	setcfg32;
43707a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	char				*cfg_pnvlist = NULL;
43807a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	nvlist_t			*cfg_nvlist = NULL;
43907a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	it_config_t			*cfg = NULL;
440a6d42e7dSPeter Dunlap 	idm_status_t			idmrc;
441a6d42e7dSPeter Dunlap 	int				rc = 0;
442a6d42e7dSPeter Dunlap 
443a6d42e7dSPeter Dunlap 	if (drv_priv(cred) != 0) {
444a6d42e7dSPeter Dunlap 		return (EPERM);
445a6d42e7dSPeter Dunlap 	}
446a6d42e7dSPeter Dunlap 
4478c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_enter(&iscsit_global.global_state_mutex);
448a6d42e7dSPeter Dunlap 
449a6d42e7dSPeter Dunlap 	/*
450a6d42e7dSPeter Dunlap 	 * Validate ioctl requests against global service state
451a6d42e7dSPeter Dunlap 	 */
452a6d42e7dSPeter Dunlap 	switch (iscsit_global.global_svc_state) {
453a6d42e7dSPeter Dunlap 	case ISE_ENABLED:
454a6d42e7dSPeter Dunlap 		if (cmd == ISCSIT_IOC_DISABLE_SVC) {
455a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_DISABLING;
456a6d42e7dSPeter Dunlap 		} else if (cmd == ISCSIT_IOC_ENABLE_SVC) {
457a6d42e7dSPeter Dunlap 			/* Already enabled */
4588c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_exit(&iscsit_global.global_state_mutex);
459a6d42e7dSPeter Dunlap 			return (0);
460a6d42e7dSPeter Dunlap 		} else {
461a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_BUSY;
462a6d42e7dSPeter Dunlap 		}
463a6d42e7dSPeter Dunlap 		break;
464a6d42e7dSPeter Dunlap 	case ISE_DISABLED:
465a6d42e7dSPeter Dunlap 		if (cmd == ISCSIT_IOC_ENABLE_SVC) {
466a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_ENABLING;
467a6d42e7dSPeter Dunlap 		} else if (cmd == ISCSIT_IOC_DISABLE_SVC) {
468a6d42e7dSPeter Dunlap 			/* Already disabled */
4698c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_exit(&iscsit_global.global_state_mutex);
470a6d42e7dSPeter Dunlap 			return (0);
471a6d42e7dSPeter Dunlap 		} else {
472a6d42e7dSPeter Dunlap 			rc = EFAULT;
473a6d42e7dSPeter Dunlap 		}
474a6d42e7dSPeter Dunlap 		break;
4758c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	case ISE_BUSY:
476a6d42e7dSPeter Dunlap 	case ISE_ENABLING:
477a6d42e7dSPeter Dunlap 	case ISE_DISABLING:
478a6d42e7dSPeter Dunlap 		rc = EAGAIN;
479a6d42e7dSPeter Dunlap 		break;
480a6d42e7dSPeter Dunlap 	case ISE_DETACHED:
481a6d42e7dSPeter Dunlap 	default:
482a6d42e7dSPeter Dunlap 		rc = EFAULT;
483a6d42e7dSPeter Dunlap 		break;
484a6d42e7dSPeter Dunlap 	}
485a6d42e7dSPeter Dunlap 
4868c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_exit(&iscsit_global.global_state_mutex);
487a6d42e7dSPeter Dunlap 	if (rc != 0)
488a6d42e7dSPeter Dunlap 		return (rc);
489a6d42e7dSPeter Dunlap 
490a6d42e7dSPeter Dunlap 	/* Handle ioctl request (enable/disable have already been handled) */
491a6d42e7dSPeter Dunlap 	switch (cmd) {
492a6d42e7dSPeter Dunlap 	case ISCSIT_IOC_SET_CONFIG:
49307a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		/* Any errors must set state back to ISE_ENABLED */
494a6d42e7dSPeter Dunlap 		switch (ddi_model_convert_from(flag & FMODELS)) {
495a6d42e7dSPeter Dunlap 		case DDI_MODEL_ILP32:
496a6d42e7dSPeter Dunlap 			if (ddi_copyin((void *)argp, &setcfg32,
49707a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			    sizeof (iscsit_ioc_set_config32_t), flag) != 0) {
49807a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 				rc = EFAULT;
49907a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 				goto cleanup;
50007a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			}
501a6d42e7dSPeter Dunlap 
502a6d42e7dSPeter Dunlap 			setcfg.set_cfg_pnvlist =
503a6d42e7dSPeter Dunlap 			    (char *)((uintptr_t)setcfg32.set_cfg_pnvlist);
504a6d42e7dSPeter Dunlap 			setcfg.set_cfg_vers = setcfg32.set_cfg_vers;
505a6d42e7dSPeter Dunlap 			setcfg.set_cfg_pnvlist_len =
506a6d42e7dSPeter Dunlap 			    setcfg32.set_cfg_pnvlist_len;
507a6d42e7dSPeter Dunlap 			break;
508a6d42e7dSPeter Dunlap 		case DDI_MODEL_NONE:
509a6d42e7dSPeter Dunlap 			if (ddi_copyin((void *)argp, &setcfg,
51007a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			    sizeof (iscsit_ioc_set_config_t), flag) != 0) {
51107a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 				rc = EFAULT;
51207a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 				goto cleanup;
51307a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			}
514a6d42e7dSPeter Dunlap 			break;
515fcc214c3SCharles Ting 		default:
516fcc214c3SCharles Ting 			rc = EFAULT;
517fcc214c3SCharles Ting 			goto cleanup;
518a6d42e7dSPeter Dunlap 		}
519a6d42e7dSPeter Dunlap 
520a6d42e7dSPeter Dunlap 		/* Check API version */
521a6d42e7dSPeter Dunlap 		if (setcfg.set_cfg_vers != ISCSIT_API_VERS0) {
52207a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			rc = EINVAL;
52307a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			goto cleanup;
524a6d42e7dSPeter Dunlap 		}
525a6d42e7dSPeter Dunlap 
526a6d42e7dSPeter Dunlap 		/* Config is in packed nvlist format so unpack it */
527a6d42e7dSPeter Dunlap 		cfg_pnvlist = kmem_alloc(setcfg.set_cfg_pnvlist_len,
528a6d42e7dSPeter Dunlap 		    KM_SLEEP);
529a6d42e7dSPeter Dunlap 		ASSERT(cfg_pnvlist != NULL);
530a6d42e7dSPeter Dunlap 
531a6d42e7dSPeter Dunlap 		if (ddi_copyin(setcfg.set_cfg_pnvlist, cfg_pnvlist,
532a6d42e7dSPeter Dunlap 		    setcfg.set_cfg_pnvlist_len, flag) != 0) {
53307a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			rc = EFAULT;
53407a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			goto cleanup;
535a6d42e7dSPeter Dunlap 		}
536a6d42e7dSPeter Dunlap 
53707a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		rc = nvlist_unpack(cfg_pnvlist, setcfg.set_cfg_pnvlist_len,
53807a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		    &cfg_nvlist, KM_SLEEP);
53907a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		if (rc != 0) {
54007a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			goto cleanup;
541a6d42e7dSPeter Dunlap 		}
542a6d42e7dSPeter Dunlap 
543a6d42e7dSPeter Dunlap 		/* Translate nvlist */
54407a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		rc = it_nv_to_config(cfg_nvlist, &cfg);
54507a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		if (rc != 0) {
546a6d42e7dSPeter Dunlap 			cmn_err(CE_WARN, "Configuration is invalid");
54707a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			goto cleanup;
548a6d42e7dSPeter Dunlap 		}
549a6d42e7dSPeter Dunlap 
550a6d42e7dSPeter Dunlap 		/* Update config */
55107a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		rc = iscsit_config_merge(cfg);
55207a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		/* FALLTHROUGH */
55307a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
55407a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States cleanup:
55507a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		if (cfg)
55607a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			it_config_free_cmn(cfg);
55707a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		if (cfg_pnvlist)
558a6d42e7dSPeter Dunlap 			kmem_free(cfg_pnvlist, setcfg.set_cfg_pnvlist_len);
55907a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		if (cfg_nvlist)
560a6d42e7dSPeter Dunlap 			nvlist_free(cfg_nvlist);
561a6d42e7dSPeter Dunlap 
562a6d42e7dSPeter Dunlap 		/*
563a6d42e7dSPeter Dunlap 		 * Now that the reconfig is complete set our state back to
564a6d42e7dSPeter Dunlap 		 * enabled.
565a6d42e7dSPeter Dunlap 		 */
5668c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_enter(&iscsit_global.global_state_mutex);
567a6d42e7dSPeter Dunlap 		iscsit_global.global_svc_state = ISE_ENABLED;
5688c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
569a6d42e7dSPeter Dunlap 		break;
570a6d42e7dSPeter Dunlap 	case ISCSIT_IOC_ENABLE_SVC: {
571a6d42e7dSPeter Dunlap 		iscsit_hostinfo_t hostinfo;
572a6d42e7dSPeter Dunlap 
573a6d42e7dSPeter Dunlap 		if (ddi_copyin((void *)argp, &hostinfo.length,
574a6d42e7dSPeter Dunlap 		    sizeof (hostinfo.length), flag) != 0) {
5758c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_enter(&iscsit_global.global_state_mutex);
576a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_DISABLED;
5778c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_exit(&iscsit_global.global_state_mutex);
578a6d42e7dSPeter Dunlap 			return (EFAULT);
579a6d42e7dSPeter Dunlap 		}
580a6d42e7dSPeter Dunlap 
581a6d42e7dSPeter Dunlap 		if (hostinfo.length > sizeof (hostinfo.fqhn))
582a6d42e7dSPeter Dunlap 			hostinfo.length = sizeof (hostinfo.fqhn);
583a6d42e7dSPeter Dunlap 
584a6d42e7dSPeter Dunlap 		if (ddi_copyin((void *)((caddr_t)argp +
585a6d42e7dSPeter Dunlap 		    sizeof (hostinfo.length)), &hostinfo.fqhn,
586a6d42e7dSPeter Dunlap 		    hostinfo.length, flag) != 0) {
5878c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_enter(&iscsit_global.global_state_mutex);
588a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_DISABLED;
5898c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_exit(&iscsit_global.global_state_mutex);
590a6d42e7dSPeter Dunlap 			return (EFAULT);
591a6d42e7dSPeter Dunlap 		}
592a6d42e7dSPeter Dunlap 
593a6d42e7dSPeter Dunlap 		idmrc = iscsit_enable_svc(&hostinfo);
5948c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_enter(&iscsit_global.global_state_mutex);
595a6d42e7dSPeter Dunlap 		if (idmrc == IDM_STATUS_SUCCESS) {
596a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_ENABLED;
597a6d42e7dSPeter Dunlap 		} else {
598a6d42e7dSPeter Dunlap 			rc = EIO;
599a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_DISABLED;
600a6d42e7dSPeter Dunlap 		}
6018c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
602a6d42e7dSPeter Dunlap 		break;
603a6d42e7dSPeter Dunlap 	}
604a6d42e7dSPeter Dunlap 	case ISCSIT_IOC_DISABLE_SVC:
605a6d42e7dSPeter Dunlap 		iscsit_disable_svc();
6068c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_enter(&iscsit_global.global_state_mutex);
607a6d42e7dSPeter Dunlap 		iscsit_global.global_svc_state = ISE_DISABLED;
6088c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
609a6d42e7dSPeter Dunlap 		break;
61007a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
611a6d42e7dSPeter Dunlap 	default:
612a6d42e7dSPeter Dunlap 		rc = EINVAL;
6138c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_enter(&iscsit_global.global_state_mutex);
61407a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		iscsit_global.global_svc_state = ISE_ENABLED;
6158c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
616a6d42e7dSPeter Dunlap 	}
617a6d42e7dSPeter Dunlap 
618a6d42e7dSPeter Dunlap 	return (rc);
619a6d42e7dSPeter Dunlap }
620a6d42e7dSPeter Dunlap 
621a6d42e7dSPeter Dunlap static idm_status_t
622a6d42e7dSPeter Dunlap iscsit_init(dev_info_t *dip)
623a6d42e7dSPeter Dunlap {
624a6d42e7dSPeter Dunlap 	int			rc;
625a6d42e7dSPeter Dunlap 
626a6d42e7dSPeter Dunlap 	rc = ldi_ident_from_dip(dip, &iscsit_global.global_li);
627a6d42e7dSPeter Dunlap 	ASSERT(rc == 0);  /* Failure indicates invalid argument */
628a6d42e7dSPeter Dunlap 
629a6d42e7dSPeter Dunlap 	iscsit_global.global_svc_state = ISE_DISABLED;
630a6d42e7dSPeter Dunlap 
631a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
632a6d42e7dSPeter Dunlap }
633a6d42e7dSPeter Dunlap 
634a6d42e7dSPeter Dunlap /*
635a6d42e7dSPeter Dunlap  * iscsit_enable_svc
636a6d42e7dSPeter Dunlap  *
637a6d42e7dSPeter Dunlap  * registers all the configured targets and target portals with STMF
638a6d42e7dSPeter Dunlap  */
639a6d42e7dSPeter Dunlap static idm_status_t
640a6d42e7dSPeter Dunlap iscsit_enable_svc(iscsit_hostinfo_t *hostinfo)
641a6d42e7dSPeter Dunlap {
642a6d42e7dSPeter Dunlap 	stmf_port_provider_t	*pp;
643a6d42e7dSPeter Dunlap 	stmf_dbuf_store_t	*dbuf_store;
644a6d42e7dSPeter Dunlap 	boolean_t		did_iscsit_isns_init;
645a6d42e7dSPeter Dunlap 	idm_status_t		retval = IDM_STATUS_SUCCESS;
646a6d42e7dSPeter Dunlap 
647a6d42e7dSPeter Dunlap 	ASSERT(iscsit_global.global_svc_state == ISE_ENABLING);
648a6d42e7dSPeter Dunlap 
649a6d42e7dSPeter Dunlap 	/*
650a6d42e7dSPeter Dunlap 	 * Make sure that can tell if we have partially allocated
651a6d42e7dSPeter Dunlap 	 * in case we need to exit and tear down anything allocated.
652a6d42e7dSPeter Dunlap 	 */
653a6d42e7dSPeter Dunlap 	iscsit_global.global_tsih_pool = NULL;
654a6d42e7dSPeter Dunlap 	iscsit_global.global_dbuf_store = NULL;
655a6d42e7dSPeter Dunlap 	iscsit_status_pdu_cache = NULL;
656a6d42e7dSPeter Dunlap 	pp = NULL;
657a6d42e7dSPeter Dunlap 	iscsit_global.global_pp = NULL;
658a6d42e7dSPeter Dunlap 	iscsit_global.global_default_tpg = NULL;
659a6d42e7dSPeter Dunlap 	did_iscsit_isns_init = B_FALSE;
660a6d42e7dSPeter Dunlap 	iscsit_global.global_dispatch_taskq = NULL;
661a6d42e7dSPeter Dunlap 
662a6d42e7dSPeter Dunlap 	/* Setup remaining fields in iscsit_global_t */
663a6d42e7dSPeter Dunlap 	idm_refcnt_init(&iscsit_global.global_refcnt,
664a6d42e7dSPeter Dunlap 	    &iscsit_global);
665a6d42e7dSPeter Dunlap 
666a6d42e7dSPeter Dunlap 	avl_create(&iscsit_global.global_discovery_sessions,
667a6d42e7dSPeter Dunlap 	    iscsit_sess_avl_compare, sizeof (iscsit_sess_t),
668a6d42e7dSPeter Dunlap 	    offsetof(iscsit_sess_t, ist_tgt_ln));
669a6d42e7dSPeter Dunlap 
670a6d42e7dSPeter Dunlap 	avl_create(&iscsit_global.global_target_list,
671a6d42e7dSPeter Dunlap 	    iscsit_tgt_avl_compare, sizeof (iscsit_tgt_t),
672a6d42e7dSPeter Dunlap 	    offsetof(iscsit_tgt_t, target_global_ln));
673a6d42e7dSPeter Dunlap 
674a6d42e7dSPeter Dunlap 	list_create(&iscsit_global.global_deleted_target_list,
675a6d42e7dSPeter Dunlap 	    sizeof (iscsit_tgt_t),
676a6d42e7dSPeter Dunlap 	    offsetof(iscsit_tgt_t, target_global_deleted_ln));
677a6d42e7dSPeter Dunlap 
678a6d42e7dSPeter Dunlap 	avl_create(&iscsit_global.global_tpg_list,
679a6d42e7dSPeter Dunlap 	    iscsit_tpg_avl_compare, sizeof (iscsit_tpg_t),
680a6d42e7dSPeter Dunlap 	    offsetof(iscsit_tpg_t, tpg_global_ln));
681a6d42e7dSPeter Dunlap 
682a6d42e7dSPeter Dunlap 	avl_create(&iscsit_global.global_ini_list,
683a6d42e7dSPeter Dunlap 	    iscsit_ini_avl_compare, sizeof (iscsit_ini_t),
684a6d42e7dSPeter Dunlap 	    offsetof(iscsit_ini_t, ini_global_ln));
685a6d42e7dSPeter Dunlap 
686a6d42e7dSPeter Dunlap 	iscsit_global.global_tsih_pool = vmem_create("iscsit_tsih_pool",
687a6d42e7dSPeter Dunlap 	    (void *)1, ISCSI_MAX_TSIH, 1, NULL, NULL, NULL, 0,
688a6d42e7dSPeter Dunlap 	    VM_SLEEP | VMC_IDENTIFIER);
689a6d42e7dSPeter Dunlap 
690a6d42e7dSPeter Dunlap 	/*
691a6d42e7dSPeter Dunlap 	 * Setup STMF dbuf store.  Our buffers are bound to a specific
692a6d42e7dSPeter Dunlap 	 * connection so we really can't let STMF cache buffers for us.
693a6d42e7dSPeter Dunlap 	 * Consequently we'll just allocate one global buffer store.
694a6d42e7dSPeter Dunlap 	 */
695a6d42e7dSPeter Dunlap 	dbuf_store = stmf_alloc(STMF_STRUCT_DBUF_STORE, 0, 0);
696a6d42e7dSPeter Dunlap 	if (dbuf_store == NULL) {
697a6d42e7dSPeter Dunlap 		retval = IDM_STATUS_FAIL;
698a6d42e7dSPeter Dunlap 		goto tear_down_and_return;
699a6d42e7dSPeter Dunlap 	}
700a6d42e7dSPeter Dunlap 	dbuf_store->ds_alloc_data_buf = iscsit_dbuf_alloc;
701a6d42e7dSPeter Dunlap 	dbuf_store->ds_free_data_buf = iscsit_dbuf_free;
702a6d42e7dSPeter Dunlap 	dbuf_store->ds_port_private = NULL;
703a6d42e7dSPeter Dunlap 	iscsit_global.global_dbuf_store = dbuf_store;
704a6d42e7dSPeter Dunlap 
705a6d42e7dSPeter Dunlap 	/* Status PDU cache */
706a6d42e7dSPeter Dunlap 	iscsit_status_pdu_cache = kmem_cache_create("iscsit_status_pdu_cache",
707a6d42e7dSPeter Dunlap 	    sizeof (idm_pdu_t) + sizeof (iscsi_scsi_rsp_hdr_t), 8,
708a6d42e7dSPeter Dunlap 	    &iscsit_status_pdu_constructor,
709a6d42e7dSPeter Dunlap 	    NULL, NULL, NULL, NULL, KM_SLEEP);
710a6d42e7dSPeter Dunlap 
711a6d42e7dSPeter Dunlap 	/* Default TPG and portal */
712a6d42e7dSPeter Dunlap 	iscsit_global.global_default_tpg = iscsit_tpg_createdefault();
713a6d42e7dSPeter Dunlap 	if (iscsit_global.global_default_tpg == NULL) {
714a6d42e7dSPeter Dunlap 		retval = IDM_STATUS_FAIL;
715a6d42e7dSPeter Dunlap 		goto tear_down_and_return;
716a6d42e7dSPeter Dunlap 	}
717a6d42e7dSPeter Dunlap 
718a6d42e7dSPeter Dunlap 	/* initialize isns client */
719a6d42e7dSPeter Dunlap 	(void) iscsit_isns_init(hostinfo);
720a6d42e7dSPeter Dunlap 	did_iscsit_isns_init = B_TRUE;
721a6d42e7dSPeter Dunlap 
722a6d42e7dSPeter Dunlap 	/* Register port provider */
723a6d42e7dSPeter Dunlap 	pp = stmf_alloc(STMF_STRUCT_PORT_PROVIDER, 0, 0);
724a6d42e7dSPeter Dunlap 	if (pp == NULL) {
725a6d42e7dSPeter Dunlap 		retval = IDM_STATUS_FAIL;
726a6d42e7dSPeter Dunlap 		goto tear_down_and_return;
727a6d42e7dSPeter Dunlap 	}
728a6d42e7dSPeter Dunlap 
729a6d42e7dSPeter Dunlap 	pp->pp_portif_rev = PORTIF_REV_1;
730a6d42e7dSPeter Dunlap 	pp->pp_instance = 0;
731a6d42e7dSPeter Dunlap 	pp->pp_name = ISCSIT_MODNAME;
732a6d42e7dSPeter Dunlap 	pp->pp_cb = iscsit_pp_cb;
733a6d42e7dSPeter Dunlap 
734a6d42e7dSPeter Dunlap 	iscsit_global.global_pp = pp;
735a6d42e7dSPeter Dunlap 
736a6d42e7dSPeter Dunlap 
737a6d42e7dSPeter Dunlap 	if (stmf_register_port_provider(pp) != STMF_SUCCESS) {
738a6d42e7dSPeter Dunlap 		retval = IDM_STATUS_FAIL;
739a6d42e7dSPeter Dunlap 		goto tear_down_and_return;
740a6d42e7dSPeter Dunlap 	}
741a6d42e7dSPeter Dunlap 
742a6d42e7dSPeter Dunlap 	iscsit_global.global_dispatch_taskq = taskq_create("iscsit_dispatch",
743a6d42e7dSPeter Dunlap 	    1, minclsyspri, 16, 16, TASKQ_PREPOPULATE);
744a6d42e7dSPeter Dunlap 
745*d618d68dSPriya Krishnan 	/* Scan staged PDUs, meaningful in MC/S situations */
746*d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_start();
747*d618d68dSPriya Krishnan 
748a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
749a6d42e7dSPeter Dunlap 
750a6d42e7dSPeter Dunlap tear_down_and_return:
751a6d42e7dSPeter Dunlap 
752a6d42e7dSPeter Dunlap 	if (iscsit_global.global_dispatch_taskq) {
753a6d42e7dSPeter Dunlap 		taskq_destroy(iscsit_global.global_dispatch_taskq);
754a6d42e7dSPeter Dunlap 		iscsit_global.global_dispatch_taskq = NULL;
755a6d42e7dSPeter Dunlap 	}
756a6d42e7dSPeter Dunlap 
757a6d42e7dSPeter Dunlap 	if (did_iscsit_isns_init)
758a6d42e7dSPeter Dunlap 		iscsit_isns_fini();
759a6d42e7dSPeter Dunlap 
760a6d42e7dSPeter Dunlap 	if (iscsit_global.global_default_tpg) {
761a6d42e7dSPeter Dunlap 		iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
762a6d42e7dSPeter Dunlap 		iscsit_global.global_default_tpg = NULL;
763a6d42e7dSPeter Dunlap 	}
764a6d42e7dSPeter Dunlap 
765a6d42e7dSPeter Dunlap 	if (iscsit_global.global_pp)
766a6d42e7dSPeter Dunlap 		iscsit_global.global_pp = NULL;
767a6d42e7dSPeter Dunlap 
768a6d42e7dSPeter Dunlap 	if (pp)
769a6d42e7dSPeter Dunlap 		stmf_free(pp);
770a6d42e7dSPeter Dunlap 
771a6d42e7dSPeter Dunlap 	if (iscsit_status_pdu_cache) {
772a6d42e7dSPeter Dunlap 		kmem_cache_destroy(iscsit_status_pdu_cache);
773a6d42e7dSPeter Dunlap 		iscsit_status_pdu_cache = NULL;
774a6d42e7dSPeter Dunlap 	}
775a6d42e7dSPeter Dunlap 
776a6d42e7dSPeter Dunlap 	if (iscsit_global.global_dbuf_store) {
777a6d42e7dSPeter Dunlap 		stmf_free(iscsit_global.global_dbuf_store);
778a6d42e7dSPeter Dunlap 		iscsit_global.global_dbuf_store = NULL;
779a6d42e7dSPeter Dunlap 	}
780a6d42e7dSPeter Dunlap 
781a6d42e7dSPeter Dunlap 	if (iscsit_global.global_tsih_pool) {
782a6d42e7dSPeter Dunlap 		vmem_destroy(iscsit_global.global_tsih_pool);
783a6d42e7dSPeter Dunlap 		iscsit_global.global_tsih_pool = NULL;
784a6d42e7dSPeter Dunlap 	}
785a6d42e7dSPeter Dunlap 
786a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_ini_list);
787a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_tpg_list);
788a6d42e7dSPeter Dunlap 	list_destroy(&iscsit_global.global_deleted_target_list);
789a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_target_list);
790a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_discovery_sessions);
791a6d42e7dSPeter Dunlap 
792a6d42e7dSPeter Dunlap 	idm_refcnt_destroy(&iscsit_global.global_refcnt);
793a6d42e7dSPeter Dunlap 
794a6d42e7dSPeter Dunlap 	return (retval);
795a6d42e7dSPeter Dunlap }
796a6d42e7dSPeter Dunlap 
797a6d42e7dSPeter Dunlap /*
798a6d42e7dSPeter Dunlap  * iscsit_disable_svc
799a6d42e7dSPeter Dunlap  *
800a6d42e7dSPeter Dunlap  * clean up all existing connections and deregister targets from STMF
801a6d42e7dSPeter Dunlap  */
802a6d42e7dSPeter Dunlap static void
803a6d42e7dSPeter Dunlap iscsit_disable_svc(void)
804a6d42e7dSPeter Dunlap {
805a6d42e7dSPeter Dunlap 	iscsit_sess_t	*sess;
806a6d42e7dSPeter Dunlap 
807a6d42e7dSPeter Dunlap 	ASSERT(iscsit_global.global_svc_state == ISE_DISABLING);
808a6d42e7dSPeter Dunlap 
809*d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_stop();
810*d618d68dSPriya Krishnan 
811a6d42e7dSPeter Dunlap 	/* tear down discovery sessions */
812a6d42e7dSPeter Dunlap 	for (sess = avl_first(&iscsit_global.global_discovery_sessions);
813a6d42e7dSPeter Dunlap 	    sess != NULL;
814a6d42e7dSPeter Dunlap 	    sess = AVL_NEXT(&iscsit_global.global_discovery_sessions, sess))
815a6d42e7dSPeter Dunlap 		iscsit_sess_close(sess);
816a6d42e7dSPeter Dunlap 
817a6d42e7dSPeter Dunlap 	/*
818a6d42e7dSPeter Dunlap 	 * Passing NULL to iscsit_config_merge tells it to go to an empty
819a6d42e7dSPeter Dunlap 	 * config.
820a6d42e7dSPeter Dunlap 	 */
821a6d42e7dSPeter Dunlap 	(void) iscsit_config_merge(NULL);
822a6d42e7dSPeter Dunlap 
823a6d42e7dSPeter Dunlap 	/*
824a6d42e7dSPeter Dunlap 	 * Wait until there are no more global references
825a6d42e7dSPeter Dunlap 	 */
826a6d42e7dSPeter Dunlap 	idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
827a6d42e7dSPeter Dunlap 	idm_refcnt_destroy(&iscsit_global.global_refcnt);
828a6d42e7dSPeter Dunlap 
829a6d42e7dSPeter Dunlap 	/*
830a6d42e7dSPeter Dunlap 	 * Default TPG must be destroyed after global_refcnt is 0.
831a6d42e7dSPeter Dunlap 	 */
832a6d42e7dSPeter Dunlap 	iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
833a6d42e7dSPeter Dunlap 
834a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_discovery_sessions);
835a6d42e7dSPeter Dunlap 	list_destroy(&iscsit_global.global_deleted_target_list);
836a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_target_list);
837a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_tpg_list);
838a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_ini_list);
839a6d42e7dSPeter Dunlap 
840a6d42e7dSPeter Dunlap 	taskq_destroy(iscsit_global.global_dispatch_taskq);
841a6d42e7dSPeter Dunlap 
842a6d42e7dSPeter Dunlap 	iscsit_isns_fini();
843a6d42e7dSPeter Dunlap 
844a6d42e7dSPeter Dunlap 	stmf_free(iscsit_global.global_dbuf_store);
845a6d42e7dSPeter Dunlap 	iscsit_global.global_dbuf_store = NULL;
846a6d42e7dSPeter Dunlap 
847a6d42e7dSPeter Dunlap 	(void) stmf_deregister_port_provider(iscsit_global.global_pp);
848a6d42e7dSPeter Dunlap 	stmf_free(iscsit_global.global_pp);
849a6d42e7dSPeter Dunlap 	iscsit_global.global_pp = NULL;
850a6d42e7dSPeter Dunlap 
851a6d42e7dSPeter Dunlap 	kmem_cache_destroy(iscsit_status_pdu_cache);
852a6d42e7dSPeter Dunlap 	iscsit_status_pdu_cache = NULL;
853a6d42e7dSPeter Dunlap 
854a6d42e7dSPeter Dunlap 	vmem_destroy(iscsit_global.global_tsih_pool);
855a6d42e7dSPeter Dunlap 	iscsit_global.global_tsih_pool = NULL;
856a6d42e7dSPeter Dunlap }
857a6d42e7dSPeter Dunlap 
858a6d42e7dSPeter Dunlap void
859a6d42e7dSPeter Dunlap iscsit_global_hold()
860a6d42e7dSPeter Dunlap {
8618c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	/*
8628c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * To take out a global hold, we must either own the global
8638c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * state mutex or we must be running inside of an ioctl that
8648c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * has set the global state to ISE_BUSY, ISE_DISABLING, or
8658c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * ISE_ENABLING.  We don't track the "owner" for these flags,
8668c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * so just checking if they are set is enough for now.
8678c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 */
8688c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	ASSERT((iscsit_global.global_svc_state == ISE_ENABLING) ||
8698c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	    (iscsit_global.global_svc_state == ISE_DISABLING) ||
8708c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	    (iscsit_global.global_svc_state == ISE_BUSY) ||
8718c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	    MUTEX_HELD(&iscsit_global.global_state_mutex));
8728c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
873a6d42e7dSPeter Dunlap 	idm_refcnt_hold(&iscsit_global.global_refcnt);
874a6d42e7dSPeter Dunlap }
875a6d42e7dSPeter Dunlap 
876a6d42e7dSPeter Dunlap void
877a6d42e7dSPeter Dunlap iscsit_global_rele()
878a6d42e7dSPeter Dunlap {
879a6d42e7dSPeter Dunlap 	idm_refcnt_rele(&iscsit_global.global_refcnt);
880a6d42e7dSPeter Dunlap }
881a6d42e7dSPeter Dunlap 
882a6d42e7dSPeter Dunlap void
883a6d42e7dSPeter Dunlap iscsit_global_wait_ref()
884a6d42e7dSPeter Dunlap {
885a6d42e7dSPeter Dunlap 	idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
886a6d42e7dSPeter Dunlap }
887a6d42e7dSPeter Dunlap 
888a6d42e7dSPeter Dunlap /*
889a6d42e7dSPeter Dunlap  * IDM callbacks
890a6d42e7dSPeter Dunlap  */
891a6d42e7dSPeter Dunlap 
892a6d42e7dSPeter Dunlap /*ARGSUSED*/
893a6d42e7dSPeter Dunlap void
894a6d42e7dSPeter Dunlap iscsit_rx_pdu(idm_conn_t *ic, idm_pdu_t *rx_pdu)
895a6d42e7dSPeter Dunlap {
896a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = ic->ic_handle;
897a6d42e7dSPeter Dunlap 	switch (IDM_PDU_OPCODE(rx_pdu)) {
898a6d42e7dSPeter Dunlap 	case ISCSI_OP_SCSI_CMD:
899a6d42e7dSPeter Dunlap 		ASSERT(0); /* Shouldn't happen */
900a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
901a6d42e7dSPeter Dunlap 		break;
902a6d42e7dSPeter Dunlap 	case ISCSI_OP_SNACK_CMD:
903a6d42e7dSPeter Dunlap 		/*
904a6d42e7dSPeter Dunlap 		 * We'll need to handle this when we support ERL1/2.  For
905a6d42e7dSPeter Dunlap 		 * now we treat it as a protocol error.
906a6d42e7dSPeter Dunlap 		 */
907a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
908a6d42e7dSPeter Dunlap 		idm_conn_event(ic, CE_TRANSPORT_FAIL, NULL);
909a6d42e7dSPeter Dunlap 		break;
910a6d42e7dSPeter Dunlap 	case ISCSI_OP_SCSI_TASK_MGT_MSG:
911*d618d68dSPriya Krishnan 		if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
912*d618d68dSPriya Krishnan 			iscsit_set_cmdsn(ict, rx_pdu);
913*d618d68dSPriya Krishnan 			iscsit_op_scsi_task_mgmt(ict, rx_pdu);
914*d618d68dSPriya Krishnan 		}
915a6d42e7dSPeter Dunlap 		break;
916a6d42e7dSPeter Dunlap 	case ISCSI_OP_NOOP_OUT:
917a6d42e7dSPeter Dunlap 	case ISCSI_OP_LOGIN_CMD:
918a6d42e7dSPeter Dunlap 	case ISCSI_OP_TEXT_CMD:
919a6d42e7dSPeter Dunlap 	case ISCSI_OP_LOGOUT_CMD:
920a6d42e7dSPeter Dunlap 		/*
921a6d42e7dSPeter Dunlap 		 * If/when we switch to userland processing these PDU's
922a6d42e7dSPeter Dunlap 		 * will be handled by iscsitd.
923a6d42e7dSPeter Dunlap 		 */
924a6d42e7dSPeter Dunlap 		iscsit_deferred_dispatch(rx_pdu);
925a6d42e7dSPeter Dunlap 		break;
926a6d42e7dSPeter Dunlap 	default:
927a6d42e7dSPeter Dunlap 		/* Protocol error */
928a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
929a6d42e7dSPeter Dunlap 		idm_conn_event(ic, CE_TRANSPORT_FAIL, NULL);
930a6d42e7dSPeter Dunlap 		break;
931a6d42e7dSPeter Dunlap 	}
932a6d42e7dSPeter Dunlap }
933a6d42e7dSPeter Dunlap 
934a6d42e7dSPeter Dunlap /*ARGSUSED*/
935a6d42e7dSPeter Dunlap void
936a6d42e7dSPeter Dunlap iscsit_rx_pdu_error(idm_conn_t *ic, idm_pdu_t *rx_pdu, idm_status_t status)
937a6d42e7dSPeter Dunlap {
938a6d42e7dSPeter Dunlap 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
939a6d42e7dSPeter Dunlap }
940a6d42e7dSPeter Dunlap 
941a6d42e7dSPeter Dunlap void
942a6d42e7dSPeter Dunlap iscsit_task_aborted(idm_task_t *idt, idm_status_t status)
943a6d42e7dSPeter Dunlap {
944a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = idt->idt_private;
945a6d42e7dSPeter Dunlap 
946a6d42e7dSPeter Dunlap 	switch (status) {
947a6d42e7dSPeter Dunlap 	case IDM_STATUS_SUSPENDED:
948a6d42e7dSPeter Dunlap 		break;
949a6d42e7dSPeter Dunlap 	case IDM_STATUS_ABORTED:
950a6d42e7dSPeter Dunlap 		mutex_enter(&itask->it_mutex);
951a6d42e7dSPeter Dunlap 		itask->it_aborted = B_TRUE;
952bf604c64SPeter Dunlap 		/*
953bf604c64SPeter Dunlap 		 * We rely on the fact that STMF tracks outstanding
954bf604c64SPeter Dunlap 		 * buffer transfers and will free all of our buffers
955bf604c64SPeter Dunlap 		 * before freeing the task so we don't need to
956bf604c64SPeter Dunlap 		 * explicitly free the buffers from iscsit/idm
957bf604c64SPeter Dunlap 		 */
958a6d42e7dSPeter Dunlap 		if (itask->it_stmf_abort) {
959a6d42e7dSPeter Dunlap 			mutex_exit(&itask->it_mutex);
96072cf3143Speter dunlap 			/*
96172cf3143Speter dunlap 			 * Task is no longer active
96272cf3143Speter dunlap 			 */
96372cf3143Speter dunlap 			iscsit_task_done(itask);
96472cf3143Speter dunlap 
965a6d42e7dSPeter Dunlap 			/*
966bf604c64SPeter Dunlap 			 * STMF has already asked for this task to be aborted
967bf604c64SPeter Dunlap 			 *
968a6d42e7dSPeter Dunlap 			 * STMF specification is wrong... says to return
969a6d42e7dSPeter Dunlap 			 * STMF_ABORTED, the code actually looks for
970a6d42e7dSPeter Dunlap 			 * STMF_ABORT_SUCCESS.
971a6d42e7dSPeter Dunlap 			 */
972a6d42e7dSPeter Dunlap 			stmf_task_lport_aborted(itask->it_stmf_task,
973a6d42e7dSPeter Dunlap 			    STMF_ABORT_SUCCESS, STMF_IOF_LPORT_DONE);
974a6d42e7dSPeter Dunlap 			return;
975a6d42e7dSPeter Dunlap 		} else {
976a6d42e7dSPeter Dunlap 			mutex_exit(&itask->it_mutex);
977a6d42e7dSPeter Dunlap 			/*
978bf604c64SPeter Dunlap 			 * Tell STMF to stop processing the task.
979a6d42e7dSPeter Dunlap 			 */
980a6d42e7dSPeter Dunlap 			stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
981a6d42e7dSPeter Dunlap 			    STMF_ABORTED, NULL);
982a6d42e7dSPeter Dunlap 			return;
983a6d42e7dSPeter Dunlap 		}
984a6d42e7dSPeter Dunlap 		/*NOTREACHED*/
985a6d42e7dSPeter Dunlap 	default:
986a6d42e7dSPeter Dunlap 		ASSERT(0);
987a6d42e7dSPeter Dunlap 	}
988a6d42e7dSPeter Dunlap }
989a6d42e7dSPeter Dunlap 
990a6d42e7dSPeter Dunlap /*ARGSUSED*/
991a6d42e7dSPeter Dunlap idm_status_t
992a6d42e7dSPeter Dunlap iscsit_client_notify(idm_conn_t *ic, idm_client_notify_t icn,
993a6d42e7dSPeter Dunlap     uintptr_t data)
994a6d42e7dSPeter Dunlap {
995a6d42e7dSPeter Dunlap 	idm_status_t rc = IDM_STATUS_SUCCESS;
996a6d42e7dSPeter Dunlap 
997a6d42e7dSPeter Dunlap 	/*
998a6d42e7dSPeter Dunlap 	 * IDM client notifications will never occur at interrupt level
999a6d42e7dSPeter Dunlap 	 * since they are generated from the connection state machine which
1000a6d42e7dSPeter Dunlap 	 * running on taskq threads.
1001a6d42e7dSPeter Dunlap 	 *
1002a6d42e7dSPeter Dunlap 	 */
1003a6d42e7dSPeter Dunlap 	switch (icn) {
1004a6d42e7dSPeter Dunlap 	case CN_CONNECT_ACCEPT:
1005a6d42e7dSPeter Dunlap 		rc = iscsit_conn_accept(ic); /* No data */
1006a6d42e7dSPeter Dunlap 		break;
1007a6d42e7dSPeter Dunlap 	case CN_FFP_ENABLED:
1008a6d42e7dSPeter Dunlap 		rc = iscsit_ffp_enabled(ic); /* No data */
1009a6d42e7dSPeter Dunlap 		break;
1010a6d42e7dSPeter Dunlap 	case CN_FFP_DISABLED:
1011a6d42e7dSPeter Dunlap 		/*
1012a6d42e7dSPeter Dunlap 		 * Data indicates whether this was the result of an
1013a6d42e7dSPeter Dunlap 		 * explicit logout request.
1014a6d42e7dSPeter Dunlap 		 */
1015a6d42e7dSPeter Dunlap 		rc = iscsit_ffp_disabled(ic, (idm_ffp_disable_t)data);
1016a6d42e7dSPeter Dunlap 		break;
1017a6d42e7dSPeter Dunlap 	case CN_CONNECT_LOST:
1018a6d42e7dSPeter Dunlap 		rc = iscsit_conn_lost(ic);
1019a6d42e7dSPeter Dunlap 		break;
1020a6d42e7dSPeter Dunlap 	case CN_CONNECT_DESTROY:
1021a6d42e7dSPeter Dunlap 		rc = iscsit_conn_destroy(ic);
1022a6d42e7dSPeter Dunlap 		break;
1023a6d42e7dSPeter Dunlap 	case CN_LOGIN_FAIL:
1024a6d42e7dSPeter Dunlap 		/*
1025a6d42e7dSPeter Dunlap 		 * Force the login state machine to completion
1026a6d42e7dSPeter Dunlap 		 */
1027a6d42e7dSPeter Dunlap 		rc = iscsit_login_fail(ic);
1028a6d42e7dSPeter Dunlap 		break;
1029a6d42e7dSPeter Dunlap 	default:
1030a6d42e7dSPeter Dunlap 		rc = IDM_STATUS_REJECT;
1031a6d42e7dSPeter Dunlap 		break;
1032a6d42e7dSPeter Dunlap 	}
1033a6d42e7dSPeter Dunlap 
1034a6d42e7dSPeter Dunlap 	return (rc);
1035a6d42e7dSPeter Dunlap }
1036a6d42e7dSPeter Dunlap 
103760220f10SPriya Krishnan /*
103860220f10SPriya Krishnan  * iscsit_update_statsn is invoked for all the PDUs which have the StatSN
103960220f10SPriya Krishnan  * field in the header. The StatSN is incremented if the IDM_PDU_ADVANCE_STATSN
104060220f10SPriya Krishnan  * flag is set in the pdu flags field. The StatSN is connection-wide and is
104160220f10SPriya Krishnan  * protected by the mutex ict_statsn_mutex. For Data-In PDUs, if the flag
104260220f10SPriya Krishnan  * IDM_TASK_PHASECOLLAPSE_REQ is set, the status (phase-collapse) is also filled
104360220f10SPriya Krishnan  */
104460220f10SPriya Krishnan void
104560220f10SPriya Krishnan iscsit_update_statsn(idm_task_t *idm_task, idm_pdu_t *pdu)
104660220f10SPriya Krishnan {
104760220f10SPriya Krishnan 	iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
104860220f10SPriya Krishnan 	iscsit_conn_t *ict = (iscsit_conn_t *)pdu->isp_ic->ic_handle;
104960220f10SPriya Krishnan 	iscsit_task_t *itask = NULL;
105060220f10SPriya Krishnan 	scsi_task_t *task = NULL;
105160220f10SPriya Krishnan 
105260220f10SPriya Krishnan 	mutex_enter(&ict->ict_statsn_mutex);
105360220f10SPriya Krishnan 	rsp->statsn = htonl(ict->ict_statsn);
105460220f10SPriya Krishnan 	if (pdu->isp_flags & IDM_PDU_ADVANCE_STATSN)
105560220f10SPriya Krishnan 		ict->ict_statsn++;
105660220f10SPriya Krishnan 	mutex_exit(&ict->ict_statsn_mutex);
105760220f10SPriya Krishnan 
105860220f10SPriya Krishnan 	/*
105960220f10SPriya Krishnan 	 * The last SCSI Data PDU passed for a command may also contain the
106060220f10SPriya Krishnan 	 * status if the status indicates termination with no expections, i.e.
106160220f10SPriya Krishnan 	 * no sense data or response involved. If the command completes with
106260220f10SPriya Krishnan 	 * an error, then the response and sense data will be sent in a
106360220f10SPriya Krishnan 	 * separate iSCSI Response PDU.
106460220f10SPriya Krishnan 	 */
106560220f10SPriya Krishnan 	if ((idm_task) && (idm_task->idt_flags & IDM_TASK_PHASECOLLAPSE_REQ)) {
106660220f10SPriya Krishnan 		itask = idm_task->idt_private;
106760220f10SPriya Krishnan 		task = itask->it_stmf_task;
106860220f10SPriya Krishnan 
106960220f10SPriya Krishnan 		rsp->cmd_status = task->task_scsi_status;
107060220f10SPriya Krishnan 		rsp->flags	|= ISCSI_FLAG_DATA_STATUS;
107160220f10SPriya Krishnan 		if (task->task_status_ctrl & TASK_SCTRL_OVER) {
107260220f10SPriya Krishnan 			rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
107360220f10SPriya Krishnan 		} else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
107460220f10SPriya Krishnan 			rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
107560220f10SPriya Krishnan 		}
107660220f10SPriya Krishnan 		rsp->residual_count = htonl(task->task_resid);
1077a2383ac5SPriya Krishnan 
1078a2383ac5SPriya Krishnan 		/*
1079a2383ac5SPriya Krishnan 		 * Removing the task from the session task list
1080a2383ac5SPriya Krishnan 		 * just before the status is sent in the last
1081a2383ac5SPriya Krishnan 		 * Data PDU transfer
1082a2383ac5SPriya Krishnan 		 */
1083a2383ac5SPriya Krishnan 		iscsit_task_done(itask);
108460220f10SPriya Krishnan 	}
108560220f10SPriya Krishnan }
1086a6d42e7dSPeter Dunlap 
1087a6d42e7dSPeter Dunlap void
1088a6d42e7dSPeter Dunlap iscsit_build_hdr(idm_task_t *idm_task, idm_pdu_t *pdu, uint8_t opcode)
1089a6d42e7dSPeter Dunlap {
1090a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = idm_task->idt_private;
1091a6d42e7dSPeter Dunlap 	iscsi_data_rsp_hdr_t *dh = (iscsi_data_rsp_hdr_t *)pdu->isp_hdr;
1092a6d42e7dSPeter Dunlap 
1093a6d42e7dSPeter Dunlap 	/*
1094*d618d68dSPriya Krishnan 	 * We acquired iscsit_sess_t.ist_sn_mutex in iscsit_xfer_scsi_data
1095a6d42e7dSPeter Dunlap 	 */
1096*d618d68dSPriya Krishnan 	ASSERT(MUTEX_HELD(&itask->it_ict->ict_sess->ist_sn_mutex));
1097a6d42e7dSPeter Dunlap 	/*
1098a6d42e7dSPeter Dunlap 	 * Lun is only required if the opcode == ISCSI_OP_SCSI_DATA_RSP
1099a6d42e7dSPeter Dunlap 	 * and the 'A' bit is to be set
1100a6d42e7dSPeter Dunlap 	 */
1101a6d42e7dSPeter Dunlap 	dh->opcode = opcode;
1102a6d42e7dSPeter Dunlap 	dh->itt = itask->it_itt;
1103a6d42e7dSPeter Dunlap 	dh->ttt = itask->it_ttt;
110460220f10SPriya Krishnan 
1105a6d42e7dSPeter Dunlap 	dh->expcmdsn = htonl(itask->it_ict->ict_sess->ist_expcmdsn);
1106a6d42e7dSPeter Dunlap 	dh->maxcmdsn = htonl(itask->it_ict->ict_sess->ist_maxcmdsn);
1107a6d42e7dSPeter Dunlap 
1108a6d42e7dSPeter Dunlap 	/*
1109a6d42e7dSPeter Dunlap 	 * IDM must set:
1110a6d42e7dSPeter Dunlap 	 *
1111a6d42e7dSPeter Dunlap 	 * data.flags and rtt.flags
1112a6d42e7dSPeter Dunlap 	 * data.dlength
1113a6d42e7dSPeter Dunlap 	 * data.datasn
1114a6d42e7dSPeter Dunlap 	 * data.offset
111560220f10SPriya Krishnan 	 * statsn, residual_count and cmd_status (for phase collapse)
1116a6d42e7dSPeter Dunlap 	 * rtt.rttsn
1117a6d42e7dSPeter Dunlap 	 * rtt.data_offset
1118a6d42e7dSPeter Dunlap 	 * rtt.data_length
1119a6d42e7dSPeter Dunlap 	 */
1120a6d42e7dSPeter Dunlap }
1121a6d42e7dSPeter Dunlap 
112272cf3143Speter dunlap void
112372cf3143Speter dunlap iscsit_keepalive(idm_conn_t *ic)
112472cf3143Speter dunlap {
112572cf3143Speter dunlap 	idm_pdu_t		*nop_in_pdu;
112672cf3143Speter dunlap 	iscsi_nop_in_hdr_t	*nop_in;
112772cf3143Speter dunlap 	iscsit_conn_t		*ict = ic->ic_handle;
112872cf3143Speter dunlap 
112972cf3143Speter dunlap 	/*
113072cf3143Speter dunlap 	 * IDM noticed the connection has been idle for too long so it's
113172cf3143Speter dunlap 	 * time to provoke some activity.  Build and transmit an iSCSI
113272cf3143Speter dunlap 	 * nop-in PDU -- when the initiator responds it will be counted
113372cf3143Speter dunlap 	 * as "activity" and keep the connection alive.
113472cf3143Speter dunlap 	 *
113572cf3143Speter dunlap 	 * We don't actually care about the response here at the iscsit level
113672cf3143Speter dunlap 	 * so we will just throw it away without looking at it when it arrives.
113772cf3143Speter dunlap 	 */
113872cf3143Speter dunlap 	nop_in_pdu = idm_pdu_alloc(sizeof (*nop_in), 0);
113972cf3143Speter dunlap 	idm_pdu_init(nop_in_pdu, ic, NULL, NULL);
114072cf3143Speter dunlap 	nop_in = (iscsi_nop_in_hdr_t *)nop_in_pdu->isp_hdr;
114172cf3143Speter dunlap 	bzero(nop_in, sizeof (*nop_in));
114272cf3143Speter dunlap 	nop_in->opcode = ISCSI_OP_NOOP_IN;
114372cf3143Speter dunlap 	nop_in->flags = ISCSI_FLAG_FINAL;
114472cf3143Speter dunlap 	nop_in->itt = ISCSI_RSVD_TASK_TAG;
114560220f10SPriya Krishnan 	/*
114660220f10SPriya Krishnan 	 * When the target sends a NOP-In as a Ping, the target transfer tag
114760220f10SPriya Krishnan 	 * is set to a valid (not reserved) value and the initiator task tag
114860220f10SPriya Krishnan 	 * is set to ISCSI_RSVD_TASK_TAG (0xffffffff). In this case the StatSN
114960220f10SPriya Krishnan 	 * will always contain the next sequence number but the StatSN for the
115060220f10SPriya Krishnan 	 * connection is not advanced after this PDU is sent.
115160220f10SPriya Krishnan 	 */
115260220f10SPriya Krishnan 	nop_in_pdu->isp_flags |= IDM_PDU_SET_STATSN;
115372cf3143Speter dunlap 	/*
115472cf3143Speter dunlap 	 * This works because we don't currently allocate ttt's anywhere else
115572cf3143Speter dunlap 	 * in iscsit so as long as we stay out of IDM's range we are safe.
115672cf3143Speter dunlap 	 * If we need to allocate ttt's for other PDU's in the future this will
115772cf3143Speter dunlap 	 * need to be improved.
115872cf3143Speter dunlap 	 */
115972cf3143Speter dunlap 	mutex_enter(&ict->ict_mutex);
116072cf3143Speter dunlap 	nop_in->ttt = ict->ict_keepalive_ttt;
116172cf3143Speter dunlap 	ict->ict_keepalive_ttt++;
116272cf3143Speter dunlap 	if (ict->ict_keepalive_ttt == ISCSI_RSVD_TASK_TAG)
116372cf3143Speter dunlap 		ict->ict_keepalive_ttt = IDM_TASKIDS_MAX;
116472cf3143Speter dunlap 	mutex_exit(&ict->ict_mutex);
116572cf3143Speter dunlap 
116672cf3143Speter dunlap 	iscsit_pdu_tx(nop_in_pdu);
116772cf3143Speter dunlap }
116872cf3143Speter dunlap 
1169a6d42e7dSPeter Dunlap static idm_status_t
1170a6d42e7dSPeter Dunlap iscsit_conn_accept(idm_conn_t *ic)
1171a6d42e7dSPeter Dunlap {
1172a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict;
1173a6d42e7dSPeter Dunlap 
117430e7468fSPeter Dunlap 	/*
117530e7468fSPeter Dunlap 	 * We need to get a global hold here to ensure that the service
117630e7468fSPeter Dunlap 	 * doesn't get shutdown prior to establishing a session. This
117730e7468fSPeter Dunlap 	 * gets released in iscsit_conn_destroy().
117830e7468fSPeter Dunlap 	 */
11798c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_enter(&iscsit_global.global_state_mutex);
1180e42a0851Speter dunlap 	if (iscsit_global.global_svc_state != ISE_ENABLED) {
11818c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
1182e42a0851Speter dunlap 		return (IDM_STATUS_FAIL);
1183e42a0851Speter dunlap 	}
118430e7468fSPeter Dunlap 	iscsit_global_hold();
11858c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_exit(&iscsit_global.global_state_mutex);
118630e7468fSPeter Dunlap 
1187a6d42e7dSPeter Dunlap 	/*
1188a6d42e7dSPeter Dunlap 	 * Allocate an associated iscsit structure to represent this
1189a6d42e7dSPeter Dunlap 	 * connection.  We shouldn't really create a session until we
1190a6d42e7dSPeter Dunlap 	 * get the first login PDU.
1191a6d42e7dSPeter Dunlap 	 */
1192a6d42e7dSPeter Dunlap 	ict = kmem_zalloc(sizeof (*ict), KM_SLEEP);
1193a6d42e7dSPeter Dunlap 
1194a6d42e7dSPeter Dunlap 	ict->ict_ic = ic;
1195a6d42e7dSPeter Dunlap 	ict->ict_statsn = 1;
119672cf3143Speter dunlap 	ict->ict_keepalive_ttt = IDM_TASKIDS_MAX; /* Avoid IDM TT range */
1197a6d42e7dSPeter Dunlap 	ic->ic_handle = ict;
1198a6d42e7dSPeter Dunlap 	mutex_init(&ict->ict_mutex, NULL, MUTEX_DRIVER, NULL);
119960220f10SPriya Krishnan 	mutex_init(&ict->ict_statsn_mutex, NULL, MUTEX_DRIVER, NULL);
1200a6d42e7dSPeter Dunlap 	idm_refcnt_init(&ict->ict_refcnt, ict);
1201a6d42e7dSPeter Dunlap 
1202a6d42e7dSPeter Dunlap 	/*
1203a6d42e7dSPeter Dunlap 	 * Initialize login state machine
1204a6d42e7dSPeter Dunlap 	 */
1205a6d42e7dSPeter Dunlap 	if (iscsit_login_sm_init(ict) != IDM_STATUS_SUCCESS) {
1206e42a0851Speter dunlap 		iscsit_global_rele();
12074142b486SJames Moore 		/*
12084142b486SJames Moore 		 * Cleanup the ict after idm notifies us about this failure
12094142b486SJames Moore 		 */
1210a6d42e7dSPeter Dunlap 		return (IDM_STATUS_FAIL);
1211a6d42e7dSPeter Dunlap 	}
1212a6d42e7dSPeter Dunlap 
1213a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1214a6d42e7dSPeter Dunlap }
1215a6d42e7dSPeter Dunlap 
1216a6d42e7dSPeter Dunlap idm_status_t
1217a6d42e7dSPeter Dunlap iscsit_conn_reinstate(iscsit_conn_t *reinstate_ict, iscsit_conn_t *new_ict)
1218a6d42e7dSPeter Dunlap {
1219a6d42e7dSPeter Dunlap 	idm_status_t	result;
1220a6d42e7dSPeter Dunlap 
1221a6d42e7dSPeter Dunlap 	/*
1222a6d42e7dSPeter Dunlap 	 * Note in new connection state that this connection is
1223a6d42e7dSPeter Dunlap 	 * reinstating an existing connection.
1224a6d42e7dSPeter Dunlap 	 */
1225a6d42e7dSPeter Dunlap 	new_ict->ict_reinstating = B_TRUE;
1226a6d42e7dSPeter Dunlap 	new_ict->ict_reinstate_conn = reinstate_ict;
1227a6d42e7dSPeter Dunlap 	new_ict->ict_statsn = reinstate_ict->ict_statsn;
1228a6d42e7dSPeter Dunlap 
1229a6d42e7dSPeter Dunlap 	/*
1230a6d42e7dSPeter Dunlap 	 * Now generate connection state machine event to existing connection
1231a6d42e7dSPeter Dunlap 	 * so that it starts the cleanup process.
1232a6d42e7dSPeter Dunlap 	 */
1233a6d42e7dSPeter Dunlap 	result = idm_conn_reinstate_event(reinstate_ict->ict_ic,
1234a6d42e7dSPeter Dunlap 	    new_ict->ict_ic);
1235a6d42e7dSPeter Dunlap 
1236a6d42e7dSPeter Dunlap 	return (result);
1237a6d42e7dSPeter Dunlap }
1238a6d42e7dSPeter Dunlap 
1239a6d42e7dSPeter Dunlap void
1240a6d42e7dSPeter Dunlap iscsit_conn_hold(iscsit_conn_t *ict)
1241a6d42e7dSPeter Dunlap {
1242a6d42e7dSPeter Dunlap 	idm_refcnt_hold(&ict->ict_refcnt);
1243a6d42e7dSPeter Dunlap }
1244a6d42e7dSPeter Dunlap 
1245a6d42e7dSPeter Dunlap void
1246a6d42e7dSPeter Dunlap iscsit_conn_rele(iscsit_conn_t *ict)
1247a6d42e7dSPeter Dunlap {
1248a6d42e7dSPeter Dunlap 	idm_refcnt_rele(&ict->ict_refcnt);
1249a6d42e7dSPeter Dunlap }
1250a6d42e7dSPeter Dunlap 
1251a6d42e7dSPeter Dunlap void
1252a6d42e7dSPeter Dunlap iscsit_conn_dispatch_hold(iscsit_conn_t *ict)
1253a6d42e7dSPeter Dunlap {
1254a6d42e7dSPeter Dunlap 	idm_refcnt_hold(&ict->ict_dispatch_refcnt);
1255a6d42e7dSPeter Dunlap }
1256a6d42e7dSPeter Dunlap 
1257a6d42e7dSPeter Dunlap void
1258a6d42e7dSPeter Dunlap iscsit_conn_dispatch_rele(iscsit_conn_t *ict)
1259a6d42e7dSPeter Dunlap {
1260a6d42e7dSPeter Dunlap 	idm_refcnt_rele(&ict->ict_dispatch_refcnt);
1261a6d42e7dSPeter Dunlap }
1262a6d42e7dSPeter Dunlap 
1263a6d42e7dSPeter Dunlap static idm_status_t
1264a6d42e7dSPeter Dunlap iscsit_login_fail(idm_conn_t *ic)
1265a6d42e7dSPeter Dunlap {
1266a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = ic->ic_handle;
1267a6d42e7dSPeter Dunlap 
1268a6d42e7dSPeter Dunlap 	/* Generate login state machine event */
1269a6d42e7dSPeter Dunlap 	iscsit_login_sm_event(ict, ILE_LOGIN_CONN_ERROR, NULL);
1270a6d42e7dSPeter Dunlap 
1271a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1272a6d42e7dSPeter Dunlap }
1273a6d42e7dSPeter Dunlap 
1274a6d42e7dSPeter Dunlap static idm_status_t
1275a6d42e7dSPeter Dunlap iscsit_ffp_enabled(idm_conn_t *ic)
1276a6d42e7dSPeter Dunlap {
1277a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = ic->ic_handle;
1278a6d42e7dSPeter Dunlap 
1279a6d42e7dSPeter Dunlap 	/* Generate session state machine event */
1280a6d42e7dSPeter Dunlap 	iscsit_sess_sm_event(ict->ict_sess, SE_CONN_LOGGED_IN, ict);
1281a6d42e7dSPeter Dunlap 
1282a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1283a6d42e7dSPeter Dunlap }
1284a6d42e7dSPeter Dunlap 
1285a6d42e7dSPeter Dunlap static idm_status_t
1286a6d42e7dSPeter Dunlap iscsit_ffp_disabled(idm_conn_t *ic, idm_ffp_disable_t disable_class)
1287a6d42e7dSPeter Dunlap {
1288a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = ic->ic_handle;
1289a6d42e7dSPeter Dunlap 
1290a6d42e7dSPeter Dunlap 	/* Generate session state machine event */
1291a6d42e7dSPeter Dunlap 	switch (disable_class) {
1292a6d42e7dSPeter Dunlap 	case FD_CONN_FAIL:
1293a6d42e7dSPeter Dunlap 		iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_FAIL, ict);
1294a6d42e7dSPeter Dunlap 		break;
1295a6d42e7dSPeter Dunlap 	case FD_CONN_LOGOUT:
1296a6d42e7dSPeter Dunlap 		iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_DISABLE, ict);
1297a6d42e7dSPeter Dunlap 		break;
1298a6d42e7dSPeter Dunlap 	case FD_SESS_LOGOUT:
1299a6d42e7dSPeter Dunlap 		iscsit_sess_sm_event(ict->ict_sess, SE_SESSION_CLOSE, ict);
1300a6d42e7dSPeter Dunlap 		break;
1301a6d42e7dSPeter Dunlap 	default:
1302a6d42e7dSPeter Dunlap 		ASSERT(0);
1303a6d42e7dSPeter Dunlap 	}
1304a6d42e7dSPeter Dunlap 
1305a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1306a6d42e7dSPeter Dunlap }
1307a6d42e7dSPeter Dunlap 
1308a6d42e7dSPeter Dunlap static idm_status_t
1309a6d42e7dSPeter Dunlap iscsit_conn_lost(idm_conn_t *ic)
1310a6d42e7dSPeter Dunlap {
1311*d618d68dSPriya Krishnan 	iscsit_conn_t	*ict	= ic->ic_handle;
1312*d618d68dSPriya Krishnan 	iscsit_sess_t	*ist	= ict->ict_sess;
1313*d618d68dSPriya Krishnan 	iscsit_cbuf_t	*cbuf;
1314*d618d68dSPriya Krishnan 	idm_pdu_t	*rx_pdu;
1315*d618d68dSPriya Krishnan 	int i;
1316a6d42e7dSPeter Dunlap 
1317a6d42e7dSPeter Dunlap 	mutex_enter(&ict->ict_mutex);
1318a6d42e7dSPeter Dunlap 	ict->ict_lost = B_TRUE;
1319a6d42e7dSPeter Dunlap 	mutex_exit(&ict->ict_mutex);
1320*d618d68dSPriya Krishnan 	/*
1321*d618d68dSPriya Krishnan 	 * scrub the staging queue for all PDUs on this connection
1322*d618d68dSPriya Krishnan 	 */
1323*d618d68dSPriya Krishnan 	if (ist != NULL) {
1324*d618d68dSPriya Krishnan 		mutex_enter(&ist->ist_sn_mutex);
1325*d618d68dSPriya Krishnan 		for (cbuf = ist->ist_rxpdu_queue, i = 0;
1326*d618d68dSPriya Krishnan 		    ((cbuf->cb_num_elems > 0) && (i < ISCSIT_RXPDU_QUEUE_LEN));
1327*d618d68dSPriya Krishnan 		    i++) {
1328*d618d68dSPriya Krishnan 			if (((rx_pdu = cbuf->cb_buffer[i]) != NULL) &&
1329*d618d68dSPriya Krishnan 			    (rx_pdu->isp_ic == ic)) {
1330*d618d68dSPriya Krishnan 				/* conn is lost, drop the pdu */
1331*d618d68dSPriya Krishnan 				DTRACE_PROBE3(scrubbing__staging__queue,
1332*d618d68dSPriya Krishnan 				    iscsit_sess_t *, ist, idm_conn_t *, ic,
1333*d618d68dSPriya Krishnan 				    idm_pdu_t *, rx_pdu);
1334*d618d68dSPriya Krishnan 				idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
1335*d618d68dSPriya Krishnan 				cbuf->cb_buffer[i] = NULL;
1336*d618d68dSPriya Krishnan 				cbuf->cb_num_elems--;
1337*d618d68dSPriya Krishnan 				iscsit_conn_dispatch_rele(ict);
1338*d618d68dSPriya Krishnan 			}
1339*d618d68dSPriya Krishnan 		}
1340*d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
1341*d618d68dSPriya Krishnan 	}
1342a6d42e7dSPeter Dunlap 	/*
1343a6d42e7dSPeter Dunlap 	 * Make sure there aren't any PDU's transitioning from the receive
1344a6d42e7dSPeter Dunlap 	 * handler to the dispatch taskq.
1345a6d42e7dSPeter Dunlap 	 */
1346a6d42e7dSPeter Dunlap 	idm_refcnt_wait_ref(&ict->ict_dispatch_refcnt);
1347a6d42e7dSPeter Dunlap 
1348a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1349a6d42e7dSPeter Dunlap }
1350a6d42e7dSPeter Dunlap 
1351a6d42e7dSPeter Dunlap static idm_status_t
1352a6d42e7dSPeter Dunlap iscsit_conn_destroy(idm_conn_t *ic)
1353a6d42e7dSPeter Dunlap {
1354a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = ic->ic_handle;
1355a6d42e7dSPeter Dunlap 
1356a6d42e7dSPeter Dunlap 	mutex_enter(&ict->ict_mutex);
1357a6d42e7dSPeter Dunlap 	ict->ict_destroyed = B_TRUE;
1358a6d42e7dSPeter Dunlap 	mutex_exit(&ict->ict_mutex);
1359a6d42e7dSPeter Dunlap 
1360a6d42e7dSPeter Dunlap 	/* Generate session state machine event */
1361a6d42e7dSPeter Dunlap 	if (ict->ict_sess != NULL) {
1362a6d42e7dSPeter Dunlap 		/*
1363a6d42e7dSPeter Dunlap 		 * Session state machine will call iscsit_conn_destroy_done()
1364a6d42e7dSPeter Dunlap 		 * when it has removed references to this connection.
1365a6d42e7dSPeter Dunlap 		 */
1366a6d42e7dSPeter Dunlap 		iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FAIL, ict);
1367a6d42e7dSPeter Dunlap 	}
1368a6d42e7dSPeter Dunlap 
1369a6d42e7dSPeter Dunlap 	ict->ict_ic = NULL;
1370a6d42e7dSPeter Dunlap 
1371a6d42e7dSPeter Dunlap 	idm_refcnt_wait_ref(&ict->ict_refcnt);
1372a6d42e7dSPeter Dunlap 
13734142b486SJames Moore 	/* Reap the login state machine */
13744142b486SJames Moore 	iscsit_login_sm_fini(ict);
13754142b486SJames Moore 
13764142b486SJames Moore 	/* Clean up any text command remnants */
13774142b486SJames Moore 	iscsit_text_cmd_fini(ict);
13784142b486SJames Moore 
1379a6d42e7dSPeter Dunlap 	mutex_destroy(&ict->ict_mutex);
1380a6d42e7dSPeter Dunlap 	idm_refcnt_destroy(&ict->ict_refcnt);
1381a6d42e7dSPeter Dunlap 	kmem_free(ict, sizeof (*ict));
1382a6d42e7dSPeter Dunlap 
138330e7468fSPeter Dunlap 	iscsit_global_rele();
138430e7468fSPeter Dunlap 
1385a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1386a6d42e7dSPeter Dunlap }
1387a6d42e7dSPeter Dunlap 
1388a6d42e7dSPeter Dunlap /*
1389a6d42e7dSPeter Dunlap  * STMF-related functions
1390a6d42e7dSPeter Dunlap  *
1391a6d42e7dSPeter Dunlap  * iSCSI to STMF mapping
1392a6d42e7dSPeter Dunlap  *
1393a6d42e7dSPeter Dunlap  * Session == ?
1394a6d42e7dSPeter Dunlap  * Connection == bound to local port but not itself a local port
1395a6d42e7dSPeter Dunlap  * Target
1396a6d42e7dSPeter Dunlap  * Target portal (group?) == local port (really but we're not going to do this)
1397a6d42e7dSPeter Dunlap  *	iscsit needs to map connections to local ports (whatever we decide
1398a6d42e7dSPeter Dunlap  * 	they are)
1399a6d42e7dSPeter Dunlap  * Target == ?
1400a6d42e7dSPeter Dunlap  */
1401a6d42e7dSPeter Dunlap 
1402a6d42e7dSPeter Dunlap /*ARGSUSED*/
1403a6d42e7dSPeter Dunlap static stmf_data_buf_t *
1404a6d42e7dSPeter Dunlap iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
1405a6d42e7dSPeter Dunlap     uint32_t flags)
1406a6d42e7dSPeter Dunlap {
1407a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = task->task_port_private;
1408a6d42e7dSPeter Dunlap 	idm_buf_t *idm_buffer;
1409a6d42e7dSPeter Dunlap 	iscsit_buf_t	*ibuf;
1410a6d42e7dSPeter Dunlap 	stmf_data_buf_t *result;
141126ec4a22SPriya Krishnan 	uint32_t	bsize;
1412a6d42e7dSPeter Dunlap 
1413a6d42e7dSPeter Dunlap 	/*
141401c0a610SPriya Krishnan 	 * If the requested size is larger than MaxBurstLength and the
141501c0a610SPriya Krishnan 	 * given pminsize is also larger than MaxBurstLength, then the
141601c0a610SPriya Krishnan 	 * allocation fails (dbuf = NULL) and pminsize is modified to
141701c0a610SPriya Krishnan 	 * be equal to MaxBurstLength. stmf/sbd then should re-invoke
141801c0a610SPriya Krishnan 	 * this function with the corrected values for transfer.
1419a6d42e7dSPeter Dunlap 	 */
142026ec4a22SPriya Krishnan 	ASSERT(pminsize);
142101c0a610SPriya Krishnan 	if (size <= itask->it_ict->ict_op.op_max_burst_length) {
142201c0a610SPriya Krishnan 		bsize = size;
142301c0a610SPriya Krishnan 	} else if (*pminsize <= itask->it_ict->ict_op.op_max_burst_length) {
142401c0a610SPriya Krishnan 		bsize = itask->it_ict->ict_op.op_max_burst_length;
142501c0a610SPriya Krishnan 	} else {
1426a6d42e7dSPeter Dunlap 		*pminsize = itask->it_ict->ict_op.op_max_burst_length;
1427a6d42e7dSPeter Dunlap 		return (NULL);
1428a6d42e7dSPeter Dunlap 	}
1429a6d42e7dSPeter Dunlap 
1430a6d42e7dSPeter Dunlap 	/* Alloc buffer */
143126ec4a22SPriya Krishnan 	idm_buffer = idm_buf_alloc(itask->it_ict->ict_ic, NULL, bsize);
1432a6d42e7dSPeter Dunlap 	if (idm_buffer != NULL) {
1433a6d42e7dSPeter Dunlap 		result = stmf_alloc(STMF_STRUCT_DATA_BUF,
1434a6d42e7dSPeter Dunlap 		    sizeof (iscsit_buf_t), 0);
1435a6d42e7dSPeter Dunlap 		if (result != NULL) {
1436a6d42e7dSPeter Dunlap 			/* Fill in stmf_data_buf_t */
1437a6d42e7dSPeter Dunlap 			ibuf = result->db_port_private;
1438a6d42e7dSPeter Dunlap 			ibuf->ibuf_idm_buf = idm_buffer;
1439a6d42e7dSPeter Dunlap 			ibuf->ibuf_stmf_buf = result;
1440a6d42e7dSPeter Dunlap 			ibuf->ibuf_is_immed = B_FALSE;
1441a6d42e7dSPeter Dunlap 			result->db_flags = DB_DONT_CACHE;
144226ec4a22SPriya Krishnan 			result->db_buf_size = bsize;
144326ec4a22SPriya Krishnan 			result->db_data_size = bsize;
1444a6d42e7dSPeter Dunlap 			result->db_sglist_length = 1;
1445a6d42e7dSPeter Dunlap 			result->db_sglist[0].seg_addr = idm_buffer->idb_buf;
1446a6d42e7dSPeter Dunlap 			result->db_sglist[0].seg_length =
1447a6d42e7dSPeter Dunlap 			    idm_buffer->idb_buflen;
1448a6d42e7dSPeter Dunlap 			return (result);
1449a6d42e7dSPeter Dunlap 		}
1450a6d42e7dSPeter Dunlap 
1451a6d42e7dSPeter Dunlap 		/* Couldn't get the stmf_data_buf_t so free the buffer */
1452a6d42e7dSPeter Dunlap 		idm_buf_free(idm_buffer);
1453a6d42e7dSPeter Dunlap 	}
1454a6d42e7dSPeter Dunlap 
1455a6d42e7dSPeter Dunlap 	return (NULL);
1456a6d42e7dSPeter Dunlap }
1457a6d42e7dSPeter Dunlap 
1458a6d42e7dSPeter Dunlap /*ARGSUSED*/
1459a6d42e7dSPeter Dunlap static void
1460a6d42e7dSPeter Dunlap iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf)
1461a6d42e7dSPeter Dunlap {
1462a6d42e7dSPeter Dunlap 	iscsit_buf_t *ibuf = dbuf->db_port_private;
1463a6d42e7dSPeter Dunlap 
1464a6d42e7dSPeter Dunlap 	if (ibuf->ibuf_is_immed) {
1465a6d42e7dSPeter Dunlap 		/*
1466a6d42e7dSPeter Dunlap 		 * The iscsit_buf_t structure itself will be freed with its
1467a6d42e7dSPeter Dunlap 		 * associated task.  Here we just need to free the PDU that
1468a6d42e7dSPeter Dunlap 		 * held the immediate data.
1469a6d42e7dSPeter Dunlap 		 */
1470a6d42e7dSPeter Dunlap 		idm_pdu_complete(ibuf->ibuf_immed_data_pdu, IDM_STATUS_SUCCESS);
1471a6d42e7dSPeter Dunlap 		ibuf->ibuf_immed_data_pdu = 0;
1472a6d42e7dSPeter Dunlap 	} else {
1473a6d42e7dSPeter Dunlap 		idm_buf_free(ibuf->ibuf_idm_buf);
1474a6d42e7dSPeter Dunlap 		stmf_free(dbuf);
1475a6d42e7dSPeter Dunlap 	}
1476a6d42e7dSPeter Dunlap }
1477a6d42e7dSPeter Dunlap 
1478a6d42e7dSPeter Dunlap /*ARGSUSED*/
1479a6d42e7dSPeter Dunlap stmf_status_t
1480a6d42e7dSPeter Dunlap iscsit_xfer_scsi_data(scsi_task_t *task, stmf_data_buf_t *dbuf,
1481a6d42e7dSPeter Dunlap     uint32_t ioflags)
1482a6d42e7dSPeter Dunlap {
1483a6d42e7dSPeter Dunlap 	iscsit_task_t *iscsit_task = task->task_port_private;
148460220f10SPriya Krishnan 	iscsit_sess_t *ict_sess = iscsit_task->it_ict->ict_sess;
1485a6d42e7dSPeter Dunlap 	iscsit_buf_t *ibuf = dbuf->db_port_private;
1486a6d42e7dSPeter Dunlap 	int idm_rc;
1487a6d42e7dSPeter Dunlap 
1488bf604c64SPeter Dunlap 	/*
1489bf604c64SPeter Dunlap 	 * If we are aborting then we can ignore this request
1490bf604c64SPeter Dunlap 	 */
1491bf604c64SPeter Dunlap 	if (iscsit_task->it_stmf_abort) {
1492bf604c64SPeter Dunlap 		return (STMF_SUCCESS);
1493bf604c64SPeter Dunlap 	}
1494bf604c64SPeter Dunlap 
1495a6d42e7dSPeter Dunlap 	/*
1496a6d42e7dSPeter Dunlap 	 * If it's not immediate data then start the transfer
1497a6d42e7dSPeter Dunlap 	 */
1498a6d42e7dSPeter Dunlap 	ASSERT(ibuf->ibuf_is_immed == B_FALSE);
1499a6d42e7dSPeter Dunlap 	if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
150060220f10SPriya Krishnan 		/*
150160220f10SPriya Krishnan 		 * The DB_SEND_STATUS_GOOD flag in the STMF data buffer allows
150260220f10SPriya Krishnan 		 * the port provider to phase-collapse, i.e. send the status
150360220f10SPriya Krishnan 		 * along with the final data PDU for the command. The port
150460220f10SPriya Krishnan 		 * provider passes this request to the transport layer by
150560220f10SPriya Krishnan 		 * setting a flag IDM_TASK_PHASECOLLAPSE_REQ in the task.
150660220f10SPriya Krishnan 		 */
150760220f10SPriya Krishnan 		if (dbuf->db_flags & DB_SEND_STATUS_GOOD)
150860220f10SPriya Krishnan 			iscsit_task->it_idm_task->idt_flags |=
150960220f10SPriya Krishnan 			    IDM_TASK_PHASECOLLAPSE_REQ;
1510a6d42e7dSPeter Dunlap 		/*
1511a6d42e7dSPeter Dunlap 		 * IDM will call iscsit_build_hdr so lock now to serialize
1512a6d42e7dSPeter Dunlap 		 * access to the SN values.  We need to lock here to enforce
1513a6d42e7dSPeter Dunlap 		 * lock ordering
1514a6d42e7dSPeter Dunlap 		 */
1515*d618d68dSPriya Krishnan 		mutex_enter(&ict_sess->ist_sn_mutex);
1516a6d42e7dSPeter Dunlap 		idm_rc = idm_buf_tx_to_ini(iscsit_task->it_idm_task,
1517a6d42e7dSPeter Dunlap 		    ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1518a6d42e7dSPeter Dunlap 		    dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1519*d618d68dSPriya Krishnan 		mutex_exit(&ict_sess->ist_sn_mutex);
1520a6d42e7dSPeter Dunlap 
1521a6d42e7dSPeter Dunlap 		return (iscsit_idm_to_stmf(idm_rc));
1522a6d42e7dSPeter Dunlap 	} else if (dbuf->db_flags & DB_DIRECTION_FROM_RPORT) {
1523a6d42e7dSPeter Dunlap 		/* Grab the SN lock (see comment above) */
1524*d618d68dSPriya Krishnan 		mutex_enter(&ict_sess->ist_sn_mutex);
1525a6d42e7dSPeter Dunlap 		idm_rc = idm_buf_rx_from_ini(iscsit_task->it_idm_task,
1526a6d42e7dSPeter Dunlap 		    ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1527a6d42e7dSPeter Dunlap 		    dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1528*d618d68dSPriya Krishnan 		mutex_exit(&ict_sess->ist_sn_mutex);
1529a6d42e7dSPeter Dunlap 
1530a6d42e7dSPeter Dunlap 		return (iscsit_idm_to_stmf(idm_rc));
1531a6d42e7dSPeter Dunlap 	}
1532a6d42e7dSPeter Dunlap 
1533a6d42e7dSPeter Dunlap 	/* What are we supposed to do if there is no direction? */
1534a6d42e7dSPeter Dunlap 	return (STMF_INVALID_ARG);
1535a6d42e7dSPeter Dunlap }
1536a6d42e7dSPeter Dunlap 
1537a6d42e7dSPeter Dunlap static void
1538a6d42e7dSPeter Dunlap iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status)
1539a6d42e7dSPeter Dunlap {
1540a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = idb->idb_task_binding->idt_private;
1541a6d42e7dSPeter Dunlap 	stmf_data_buf_t *dbuf = idb->idb_cb_arg;
1542a6d42e7dSPeter Dunlap 
1543a6d42e7dSPeter Dunlap 	dbuf->db_xfer_status = iscsit_idm_to_stmf(status);
1544a6d42e7dSPeter Dunlap 
1545bf604c64SPeter Dunlap 	/*
1546bf604c64SPeter Dunlap 	 * If the task has been aborted then we don't need to call STMF
1547bf604c64SPeter Dunlap 	 */
1548bf604c64SPeter Dunlap 	if (itask->it_stmf_abort) {
1549bf604c64SPeter Dunlap 		return;
1550bf604c64SPeter Dunlap 	}
1551bf604c64SPeter Dunlap 
1552a6d42e7dSPeter Dunlap 	/*
155360220f10SPriya Krishnan 	 * For ISCSI over TCP (not iSER), the last SCSI Data PDU passed
155460220f10SPriya Krishnan 	 * for a successful command contains the status as requested by
155560220f10SPriya Krishnan 	 * by COMSTAR (via the DB_SEND_STATUS_GOOD flag). But the iSER
155660220f10SPriya Krishnan 	 * transport does not support phase-collapse. So pretend we are
155760220f10SPriya Krishnan 	 * COMSTAR and send the status in a separate PDU now.
1558a6d42e7dSPeter Dunlap 	 */
155960220f10SPriya Krishnan 	if (idb->idb_task_binding->idt_flags & IDM_TASK_PHASECOLLAPSE_SUCCESS) {
156060220f10SPriya Krishnan 		/*
1561a2383ac5SPriya Krishnan 		 * Mark task complete and notify COMSTAR
1562a2383ac5SPriya Krishnan 		 * that the status has been sent.
156360220f10SPriya Krishnan 		 */
156460220f10SPriya Krishnan 		itask->it_idm_task->idt_state = TASK_COMPLETE;
156560220f10SPriya Krishnan 		stmf_send_status_done(itask->it_stmf_task,
156660220f10SPriya Krishnan 		    iscsit_idm_to_stmf(status), STMF_IOF_LPORT_DONE);
156760220f10SPriya Krishnan 	} else if ((dbuf->db_flags & DB_SEND_STATUS_GOOD) &&
1568a6d42e7dSPeter Dunlap 	    status == IDM_STATUS_SUCCESS) {
156960220f10SPriya Krishnan 
1570a6d42e7dSPeter Dunlap 		/*
157170a38f02SPriya Krishnan 		 * The iscsi target port provider - for iSER, emulates the
157270a38f02SPriya Krishnan 		 * DB_SEND_STATUS_GOOD optimization if requested by STMF;
157370a38f02SPriya Krishnan 		 * it sends the status in a separate PDU after the data
157470a38f02SPriya Krishnan 		 * transfer. In this case the port provider should first
157570a38f02SPriya Krishnan 		 * call stmf_data_xfer_done() to mark the transfer complete
157670a38f02SPriya Krishnan 		 * and then send the status. Although STMF will free the
157770a38f02SPriya Krishnan 		 * buffer at the time the task is freed, even if the transfer
157870a38f02SPriya Krishnan 		 * is not marked complete, this behavior makes statistics
157970a38f02SPriya Krishnan 		 * gathering and task state tracking more difficult than it
158070a38f02SPriya Krishnan 		 * needs to be.
1581a6d42e7dSPeter Dunlap 		 */
158270a38f02SPriya Krishnan 		stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
1583a6d42e7dSPeter Dunlap 		if (iscsit_send_scsi_status(itask->it_stmf_task, 0)
158463528ae4SJames Moore 		    != STMF_SUCCESS) {
158570a38f02SPriya Krishnan 			stmf_send_status_done(itask->it_stmf_task,
158670a38f02SPriya Krishnan 			    STMF_FAILURE, STMF_IOF_LPORT_DONE);
1587a6d42e7dSPeter Dunlap 		}
1588a6d42e7dSPeter Dunlap 	} else {
1589a6d42e7dSPeter Dunlap 		stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
159070a38f02SPriya Krishnan 		/* don't touch dbuf after stmf_data_xfer_done */
1591a6d42e7dSPeter Dunlap 	}
1592a6d42e7dSPeter Dunlap }
1593a6d42e7dSPeter Dunlap 
1594bf604c64SPeter Dunlap 
1595a6d42e7dSPeter Dunlap /*ARGSUSED*/
1596a6d42e7dSPeter Dunlap stmf_status_t
1597a6d42e7dSPeter Dunlap iscsit_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
1598a6d42e7dSPeter Dunlap {
1599a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = task->task_port_private;
1600a6d42e7dSPeter Dunlap 	iscsi_scsi_rsp_hdr_t *rsp;
1601a6d42e7dSPeter Dunlap 	idm_pdu_t *pdu;
1602a6d42e7dSPeter Dunlap 	int resp_datalen;
1603a6d42e7dSPeter Dunlap 
1604a6d42e7dSPeter Dunlap 	/*
1605a6d42e7dSPeter Dunlap 	 * If this task is aborted then we don't need to respond.
1606a6d42e7dSPeter Dunlap 	 */
1607a6d42e7dSPeter Dunlap 	if (itask->it_stmf_abort) {
1608a6d42e7dSPeter Dunlap 		return (STMF_SUCCESS);
1609a6d42e7dSPeter Dunlap 	}
1610a6d42e7dSPeter Dunlap 
1611a6d42e7dSPeter Dunlap 	/*
1612a6d42e7dSPeter Dunlap 	 * If this is a task management status, handle it elsewhere.
1613a6d42e7dSPeter Dunlap 	 */
1614a6d42e7dSPeter Dunlap 	if (task->task_mgmt_function != TM_NONE) {
1615a6d42e7dSPeter Dunlap 		/*
1616a6d42e7dSPeter Dunlap 		 * Don't wait for the PDU completion to tell STMF
1617a6d42e7dSPeter Dunlap 		 * the task is done -- it doesn't really matter and
1618a6d42e7dSPeter Dunlap 		 * it makes life complicated if STMF later asks us to
1619a6d42e7dSPeter Dunlap 		 * abort the request and we don't know whether the
1620a6d42e7dSPeter Dunlap 		 * status has been sent or not.
1621a6d42e7dSPeter Dunlap 		 */
1622a6d42e7dSPeter Dunlap 		itask->it_tm_responded = B_TRUE;
1623a6d42e7dSPeter Dunlap 		iscsit_send_task_mgmt_resp(itask->it_tm_pdu,
1624a6d42e7dSPeter Dunlap 		    (task->task_completion_status == STMF_SUCCESS) ?
1625a6d42e7dSPeter Dunlap 		    SCSI_TCP_TM_RESP_COMPLETE : SCSI_TCP_TM_RESP_FUNC_NOT_SUPP);
1626a6d42e7dSPeter Dunlap 		stmf_send_status_done(task, STMF_SUCCESS,
1627a6d42e7dSPeter Dunlap 		    STMF_IOF_LPORT_DONE);
1628a6d42e7dSPeter Dunlap 		return (STMF_SUCCESS);
1629a6d42e7dSPeter Dunlap 	}
1630a6d42e7dSPeter Dunlap 
163172cf3143Speter dunlap 	/*
163272cf3143Speter dunlap 	 * Remove the task from the session task list
163372cf3143Speter dunlap 	 */
163472cf3143Speter dunlap 	iscsit_task_done(itask);
163572cf3143Speter dunlap 
163672cf3143Speter dunlap 	/*
163772cf3143Speter dunlap 	 * Send status
163872cf3143Speter dunlap 	 */
1639a6d42e7dSPeter Dunlap 	mutex_enter(&itask->it_idm_task->idt_mutex);
1640a6d42e7dSPeter Dunlap 	if ((itask->it_idm_task->idt_state == TASK_ACTIVE) &&
1641a6d42e7dSPeter Dunlap 	    (task->task_completion_status == STMF_SUCCESS) &&
1642a6d42e7dSPeter Dunlap 	    (task->task_sense_length == 0) &&
1643a6d42e7dSPeter Dunlap 	    (task->task_resid == 0)) {
1644a6d42e7dSPeter Dunlap 		itask->it_idm_task->idt_state = TASK_COMPLETE;
1645a6d42e7dSPeter Dunlap 		/* PDU callback releases task hold */
1646a6d42e7dSPeter Dunlap 		idm_task_hold(itask->it_idm_task);
1647a6d42e7dSPeter Dunlap 		mutex_exit(&itask->it_idm_task->idt_mutex);
1648a6d42e7dSPeter Dunlap 		/*
1649a6d42e7dSPeter Dunlap 		 * Fast path.  Cached status PDU's are already
1650a6d42e7dSPeter Dunlap 		 * initialized.  We just need to fill in
165160220f10SPriya Krishnan 		 * connection and task information. StatSN is
165260220f10SPriya Krishnan 		 * incremented by 1 for every status sent a
165360220f10SPriya Krishnan 		 * connection.
1654a6d42e7dSPeter Dunlap 		 */
1655a6d42e7dSPeter Dunlap 		pdu = kmem_cache_alloc(iscsit_status_pdu_cache, KM_SLEEP);
1656a6d42e7dSPeter Dunlap 		pdu->isp_ic = itask->it_ict->ict_ic;
1657a6d42e7dSPeter Dunlap 		pdu->isp_private = itask;
165860220f10SPriya Krishnan 		pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1659a6d42e7dSPeter Dunlap 
1660a6d42e7dSPeter Dunlap 		rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1661a6d42e7dSPeter Dunlap 		rsp->itt = itask->it_itt;
16621d7b78a3SPriya Krishnan 		/*
16631d7b78a3SPriya Krishnan 		 * ExpDataSN is the number of R2T and Data-In (read)
16641d7b78a3SPriya Krishnan 		 * PDUs the target has sent for the SCSI command.
16651d7b78a3SPriya Krishnan 		 *
16661d7b78a3SPriya Krishnan 		 * Since there is no support for bidirectional transfer
16671d7b78a3SPriya Krishnan 		 * yet, either idt_exp_datasn or idt_exp_rttsn, but not
16681d7b78a3SPriya Krishnan 		 * both is valid at any time
16691d7b78a3SPriya Krishnan 		 */
16701d7b78a3SPriya Krishnan 		rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
16711d7b78a3SPriya Krishnan 		    htonl(itask->it_idm_task->idt_exp_datasn):
16721d7b78a3SPriya Krishnan 		    htonl(itask->it_idm_task->idt_exp_rttsn);
1673a6d42e7dSPeter Dunlap 		rsp->cmd_status = task->task_scsi_status;
1674a6d42e7dSPeter Dunlap 		iscsit_pdu_tx(pdu);
1675a6d42e7dSPeter Dunlap 		return (STMF_SUCCESS);
1676a6d42e7dSPeter Dunlap 	} else {
1677a6d42e7dSPeter Dunlap 		if (itask->it_idm_task->idt_state != TASK_ACTIVE) {
1678a6d42e7dSPeter Dunlap 			mutex_exit(&itask->it_idm_task->idt_mutex);
167963528ae4SJames Moore 			return (STMF_FAILURE);
1680a6d42e7dSPeter Dunlap 		}
1681a6d42e7dSPeter Dunlap 		itask->it_idm_task->idt_state = TASK_COMPLETE;
1682a6d42e7dSPeter Dunlap 		/* PDU callback releases task hold */
1683a6d42e7dSPeter Dunlap 		idm_task_hold(itask->it_idm_task);
1684a6d42e7dSPeter Dunlap 		mutex_exit(&itask->it_idm_task->idt_mutex);
1685a6d42e7dSPeter Dunlap 
1686a6d42e7dSPeter Dunlap 		resp_datalen = (task->task_sense_length == 0) ? 0 :
1687a6d42e7dSPeter Dunlap 		    (task->task_sense_length + sizeof (uint16_t));
1688a6d42e7dSPeter Dunlap 
1689a6d42e7dSPeter Dunlap 		pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
1690a6d42e7dSPeter Dunlap 		idm_pdu_init(pdu, itask->it_ict->ict_ic, itask,
1691a6d42e7dSPeter Dunlap 		    iscsit_send_status_done);
169260220f10SPriya Krishnan 		pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1693a6d42e7dSPeter Dunlap 
1694a6d42e7dSPeter Dunlap 		rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1695a6d42e7dSPeter Dunlap 		bzero(rsp, sizeof (*rsp));
1696a6d42e7dSPeter Dunlap 		rsp->opcode = ISCSI_OP_SCSI_RSP;
1697a6d42e7dSPeter Dunlap 
1698a6d42e7dSPeter Dunlap 		rsp->flags = ISCSI_FLAG_FINAL;
1699a6d42e7dSPeter Dunlap 		if (task->task_status_ctrl & TASK_SCTRL_OVER) {
1700a6d42e7dSPeter Dunlap 			rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
1701a6d42e7dSPeter Dunlap 		} else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
1702a6d42e7dSPeter Dunlap 			rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
1703a6d42e7dSPeter Dunlap 		}
1704a6d42e7dSPeter Dunlap 
1705a6d42e7dSPeter Dunlap 		rsp->bi_residual_count = 0;
1706a6d42e7dSPeter Dunlap 		rsp->residual_count = htonl(task->task_resid);
1707a6d42e7dSPeter Dunlap 		rsp->itt = itask->it_itt;
1708a6d42e7dSPeter Dunlap 		rsp->response = ISCSI_STATUS_CMD_COMPLETED;
17091d7b78a3SPriya Krishnan 		rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
17101d7b78a3SPriya Krishnan 		    htonl(itask->it_idm_task->idt_exp_datasn):
17111d7b78a3SPriya Krishnan 		    htonl(itask->it_idm_task->idt_exp_rttsn);
1712a6d42e7dSPeter Dunlap 		rsp->cmd_status = task->task_scsi_status;
1713a6d42e7dSPeter Dunlap 		if (task->task_sense_length != 0) {
1714a6d42e7dSPeter Dunlap 			/*
1715a6d42e7dSPeter Dunlap 			 * Add a byte to provide the sense length in
1716a6d42e7dSPeter Dunlap 			 * the response
1717a6d42e7dSPeter Dunlap 			 */
1718a6d42e7dSPeter Dunlap 			*(uint16_t *)((void *)pdu->isp_data) =
1719a6d42e7dSPeter Dunlap 			    htons(task->task_sense_length);
1720a6d42e7dSPeter Dunlap 			bcopy(task->task_sense_data,
1721a6d42e7dSPeter Dunlap 			    (uint8_t *)pdu->isp_data +
1722a6d42e7dSPeter Dunlap 			    sizeof (uint16_t),
1723a6d42e7dSPeter Dunlap 			    task->task_sense_length);
1724a6d42e7dSPeter Dunlap 			hton24(rsp->dlength, resp_datalen);
1725a6d42e7dSPeter Dunlap 		}
1726a6d42e7dSPeter Dunlap 
172730e7468fSPeter Dunlap 		DTRACE_PROBE5(iscsi__scsi__response,
172830e7468fSPeter Dunlap 		    iscsit_conn_t *, itask->it_ict,
172930e7468fSPeter Dunlap 		    uint8_t, rsp->response,
173030e7468fSPeter Dunlap 		    uint8_t, rsp->cmd_status,
173130e7468fSPeter Dunlap 		    idm_pdu_t *, pdu,
173230e7468fSPeter Dunlap 		    scsi_task_t *, task);
173330e7468fSPeter Dunlap 
1734a6d42e7dSPeter Dunlap 		iscsit_pdu_tx(pdu);
1735a6d42e7dSPeter Dunlap 
1736a6d42e7dSPeter Dunlap 		return (STMF_SUCCESS);
1737a6d42e7dSPeter Dunlap 	}
1738a6d42e7dSPeter Dunlap }
1739a6d42e7dSPeter Dunlap 
1740a6d42e7dSPeter Dunlap /*ARGSUSED*/
1741a6d42e7dSPeter Dunlap static void
1742a6d42e7dSPeter Dunlap iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status)
1743a6d42e7dSPeter Dunlap {
1744bf604c64SPeter Dunlap 	iscsit_task_t	*itask;
1745bf604c64SPeter Dunlap 	boolean_t	aborted;
1746a6d42e7dSPeter Dunlap 
1747a6d42e7dSPeter Dunlap 	itask = pdu->isp_private;
1748bf604c64SPeter Dunlap 	aborted = itask->it_stmf_abort;
1749bf604c64SPeter Dunlap 
1750bf604c64SPeter Dunlap 	/*
1751bf604c64SPeter Dunlap 	 * After releasing the hold the task may be freed at any time so
1752bf604c64SPeter Dunlap 	 * don't touch it.
1753bf604c64SPeter Dunlap 	 */
1754a6d42e7dSPeter Dunlap 	idm_task_rele(itask->it_idm_task);
1755bf604c64SPeter Dunlap 	if (!aborted) {
1756bf604c64SPeter Dunlap 		stmf_send_status_done(itask->it_stmf_task,
1757bf604c64SPeter Dunlap 		    iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1758bf604c64SPeter Dunlap 	}
1759a6d42e7dSPeter Dunlap 	kmem_cache_free(iscsit_status_pdu_cache, pdu);
1760a6d42e7dSPeter Dunlap }
1761a6d42e7dSPeter Dunlap 
1762a6d42e7dSPeter Dunlap /*ARGSUSED*/
1763a6d42e7dSPeter Dunlap static void
1764a6d42e7dSPeter Dunlap iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status)
1765a6d42e7dSPeter Dunlap {
1766a6d42e7dSPeter Dunlap 	iscsit_task_t	 *itask;
1767bf604c64SPeter Dunlap 	boolean_t	aborted;
1768a6d42e7dSPeter Dunlap 
1769a6d42e7dSPeter Dunlap 	itask = pdu->isp_private;
1770bf604c64SPeter Dunlap 	aborted = itask->it_stmf_abort;
1771bf604c64SPeter Dunlap 
1772bf604c64SPeter Dunlap 	/*
1773bf604c64SPeter Dunlap 	 * After releasing the hold the task may be freed at any time so
1774bf604c64SPeter Dunlap 	 * don't touch it.
1775bf604c64SPeter Dunlap 	 */
1776a6d42e7dSPeter Dunlap 	idm_task_rele(itask->it_idm_task);
1777bf604c64SPeter Dunlap 	if (!aborted) {
1778bf604c64SPeter Dunlap 		stmf_send_status_done(itask->it_stmf_task,
1779bf604c64SPeter Dunlap 		    iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1780bf604c64SPeter Dunlap 	}
1781a6d42e7dSPeter Dunlap 	idm_pdu_free(pdu);
1782a6d42e7dSPeter Dunlap }
1783a6d42e7dSPeter Dunlap 
1784a6d42e7dSPeter Dunlap 
1785a6d42e7dSPeter Dunlap void
1786a6d42e7dSPeter Dunlap iscsit_lport_task_free(scsi_task_t *task)
1787a6d42e7dSPeter Dunlap {
1788a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = task->task_port_private;
1789a6d42e7dSPeter Dunlap 
1790a6d42e7dSPeter Dunlap 	/* We only call idm_task_start for regular tasks, not task management */
1791a6d42e7dSPeter Dunlap 	if (task->task_mgmt_function == TM_NONE) {
1792a6d42e7dSPeter Dunlap 		idm_task_done(itask->it_idm_task);
1793a6d42e7dSPeter Dunlap 		iscsit_task_free(itask);
1794a6d42e7dSPeter Dunlap 		return;
1795a6d42e7dSPeter Dunlap 	} else {
1796a6d42e7dSPeter Dunlap 		iscsit_tm_task_free(itask);
1797a6d42e7dSPeter Dunlap 	}
1798a6d42e7dSPeter Dunlap }
1799a6d42e7dSPeter Dunlap 
1800a6d42e7dSPeter Dunlap /*ARGSUSED*/
1801a6d42e7dSPeter Dunlap stmf_status_t
1802a6d42e7dSPeter Dunlap iscsit_abort(stmf_local_port_t *lport, int abort_cmd, void *arg, uint32_t flags)
1803a6d42e7dSPeter Dunlap {
1804a6d42e7dSPeter Dunlap 	scsi_task_t	*st = (scsi_task_t *)arg;
1805a6d42e7dSPeter Dunlap 	iscsit_task_t	*iscsit_task;
1806a6d42e7dSPeter Dunlap 	idm_task_t	*idt;
1807a6d42e7dSPeter Dunlap 
1808a6d42e7dSPeter Dunlap 	/*
1809a6d42e7dSPeter Dunlap 	 * If this is a task management request then there's really not much to
1810a6d42e7dSPeter Dunlap 	 * do.
1811a6d42e7dSPeter Dunlap 	 */
1812a6d42e7dSPeter Dunlap 	if (st->task_mgmt_function != TM_NONE) {
1813a6d42e7dSPeter Dunlap 		return (STMF_ABORT_SUCCESS);
1814a6d42e7dSPeter Dunlap 	}
1815a6d42e7dSPeter Dunlap 
1816a6d42e7dSPeter Dunlap 	/*
1817a6d42e7dSPeter Dunlap 	 * Regular task, start cleaning up
1818a6d42e7dSPeter Dunlap 	 */
1819a6d42e7dSPeter Dunlap 	iscsit_task = st->task_port_private;
1820a6d42e7dSPeter Dunlap 	idt = iscsit_task->it_idm_task;
1821a6d42e7dSPeter Dunlap 	mutex_enter(&iscsit_task->it_mutex);
1822a6d42e7dSPeter Dunlap 	iscsit_task->it_stmf_abort = B_TRUE;
1823a6d42e7dSPeter Dunlap 	if (iscsit_task->it_aborted) {
1824a6d42e7dSPeter Dunlap 		mutex_exit(&iscsit_task->it_mutex);
182572cf3143Speter dunlap 		/*
182672cf3143Speter dunlap 		 * Task is no longer active
182772cf3143Speter dunlap 		 */
182872cf3143Speter dunlap 		iscsit_task_done(iscsit_task);
182972cf3143Speter dunlap 
1830a6d42e7dSPeter Dunlap 		/*
1831a6d42e7dSPeter Dunlap 		 * STMF specification is wrong... says to return
1832a6d42e7dSPeter Dunlap 		 * STMF_ABORTED, the code actually looks for
1833a6d42e7dSPeter Dunlap 		 * STMF_ABORT_SUCCESS.
1834a6d42e7dSPeter Dunlap 		 */
1835a6d42e7dSPeter Dunlap 		return (STMF_ABORT_SUCCESS);
1836a6d42e7dSPeter Dunlap 	} else {
1837a6d42e7dSPeter Dunlap 		mutex_exit(&iscsit_task->it_mutex);
1838a6d42e7dSPeter Dunlap 		/*
1839a6d42e7dSPeter Dunlap 		 * Call IDM to abort the task.  Due to a variety of
1840a6d42e7dSPeter Dunlap 		 * circumstances the task may already be in the process of
1841a6d42e7dSPeter Dunlap 		 * aborting.
1842a6d42e7dSPeter Dunlap 		 * We'll let IDM worry about rationalizing all that except
1843a6d42e7dSPeter Dunlap 		 * for one particular instance.  If the state of the task
1844a6d42e7dSPeter Dunlap 		 * is TASK_COMPLETE, we need to indicate to the framework
1845a6d42e7dSPeter Dunlap 		 * that we are in fact done.  This typically happens with
1846a6d42e7dSPeter Dunlap 		 * framework-initiated task management type requests
1847a6d42e7dSPeter Dunlap 		 * (e.g. abort task).
1848a6d42e7dSPeter Dunlap 		 */
1849a6d42e7dSPeter Dunlap 		if (idt->idt_state == TASK_COMPLETE) {
1850a6d42e7dSPeter Dunlap 			idm_refcnt_wait_ref(&idt->idt_refcnt);
1851a6d42e7dSPeter Dunlap 			return (STMF_ABORT_SUCCESS);
1852a6d42e7dSPeter Dunlap 		} else {
1853a6d42e7dSPeter Dunlap 			idm_task_abort(idt->idt_ic, idt, AT_TASK_MGMT_ABORT);
1854a6d42e7dSPeter Dunlap 			return (STMF_SUCCESS);
1855a6d42e7dSPeter Dunlap 		}
1856a6d42e7dSPeter Dunlap 	}
1857a6d42e7dSPeter Dunlap 
1858a6d42e7dSPeter Dunlap 	/*NOTREACHED*/
1859a6d42e7dSPeter Dunlap }
1860a6d42e7dSPeter Dunlap 
1861a6d42e7dSPeter Dunlap /*ARGSUSED*/
1862a6d42e7dSPeter Dunlap void
1863a6d42e7dSPeter Dunlap iscsit_ctl(stmf_local_port_t *lport, int cmd, void *arg)
1864a6d42e7dSPeter Dunlap {
1865a6d42e7dSPeter Dunlap 	iscsit_tgt_t		*iscsit_tgt;
1866a6d42e7dSPeter Dunlap 
1867a6d42e7dSPeter Dunlap 	ASSERT((cmd == STMF_CMD_LPORT_ONLINE) ||
1868a6d42e7dSPeter Dunlap 	    (cmd == STMF_ACK_LPORT_ONLINE_COMPLETE) ||
1869a6d42e7dSPeter Dunlap 	    (cmd == STMF_CMD_LPORT_OFFLINE) ||
1870a6d42e7dSPeter Dunlap 	    (cmd == STMF_ACK_LPORT_OFFLINE_COMPLETE));
1871a6d42e7dSPeter Dunlap 
1872a6d42e7dSPeter Dunlap 	iscsit_tgt = (iscsit_tgt_t *)lport->lport_port_private;
1873a6d42e7dSPeter Dunlap 
1874a6d42e7dSPeter Dunlap 	switch (cmd) {
1875a6d42e7dSPeter Dunlap 	case STMF_CMD_LPORT_ONLINE:
1876a6d42e7dSPeter Dunlap 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_REQ);
1877a6d42e7dSPeter Dunlap 		break;
1878a6d42e7dSPeter Dunlap 	case STMF_CMD_LPORT_OFFLINE:
1879a6d42e7dSPeter Dunlap 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_REQ);
1880a6d42e7dSPeter Dunlap 		break;
1881a6d42e7dSPeter Dunlap 	case STMF_ACK_LPORT_ONLINE_COMPLETE:
1882a6d42e7dSPeter Dunlap 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_COMPLETE_ACK);
1883a6d42e7dSPeter Dunlap 		break;
1884a6d42e7dSPeter Dunlap 	case STMF_ACK_LPORT_OFFLINE_COMPLETE:
1885a6d42e7dSPeter Dunlap 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_COMPLETE_ACK);
1886a6d42e7dSPeter Dunlap 		break;
1887a6d42e7dSPeter Dunlap 
1888a6d42e7dSPeter Dunlap 	default:
1889a6d42e7dSPeter Dunlap 		break;
1890a6d42e7dSPeter Dunlap 	}
1891a6d42e7dSPeter Dunlap }
1892a6d42e7dSPeter Dunlap 
1893a6d42e7dSPeter Dunlap static stmf_status_t
1894a6d42e7dSPeter Dunlap iscsit_idm_to_stmf(idm_status_t idmrc)
1895a6d42e7dSPeter Dunlap {
1896a6d42e7dSPeter Dunlap 	switch (idmrc) {
1897a6d42e7dSPeter Dunlap 	case IDM_STATUS_SUCCESS:
1898a6d42e7dSPeter Dunlap 		return (STMF_SUCCESS);
1899a6d42e7dSPeter Dunlap 	default:
1900a6d42e7dSPeter Dunlap 		return (STMF_FAILURE);
1901a6d42e7dSPeter Dunlap 	}
1902a6d42e7dSPeter Dunlap 	/*NOTREACHED*/
1903a6d42e7dSPeter Dunlap }
1904a6d42e7dSPeter Dunlap 
1905*d618d68dSPriya Krishnan void
1906*d618d68dSPriya Krishnan iscsit_op_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu)
1907*d618d68dSPriya Krishnan {
1908*d618d68dSPriya Krishnan 	iscsit_conn_t		*ict = ic->ic_handle;
1909*d618d68dSPriya Krishnan 
1910*d618d68dSPriya Krishnan 	if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
1911*d618d68dSPriya Krishnan 		iscsit_post_scsi_cmd(ic, rx_pdu);
1912*d618d68dSPriya Krishnan 	}
1913*d618d68dSPriya Krishnan 	iscsit_process_pdu_in_queue(ict->ict_sess);
1914*d618d68dSPriya Krishnan }
1915a6d42e7dSPeter Dunlap 
1916a6d42e7dSPeter Dunlap /*
1917a6d42e7dSPeter Dunlap  * ISCSI protocol
1918a6d42e7dSPeter Dunlap  */
1919a6d42e7dSPeter Dunlap 
1920a6d42e7dSPeter Dunlap void
1921*d618d68dSPriya Krishnan iscsit_post_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu)
1922a6d42e7dSPeter Dunlap {
1923a6d42e7dSPeter Dunlap 	iscsit_conn_t		*ict;
1924a6d42e7dSPeter Dunlap 	iscsit_task_t		*itask;
1925a6d42e7dSPeter Dunlap 	scsi_task_t		*task;
1926a6d42e7dSPeter Dunlap 	iscsit_buf_t		*ibuf;
1927a6d42e7dSPeter Dunlap 	iscsi_scsi_cmd_hdr_t	*iscsi_scsi =
1928a6d42e7dSPeter Dunlap 	    (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
1929a6d42e7dSPeter Dunlap 	iscsi_addl_hdr_t	*ahs_hdr;
1930a6d42e7dSPeter Dunlap 	uint16_t		addl_cdb_len = 0;
1931a6d42e7dSPeter Dunlap 
1932a6d42e7dSPeter Dunlap 	ict = ic->ic_handle;
1933a6d42e7dSPeter Dunlap 
1934a6d42e7dSPeter Dunlap 	itask = iscsit_task_alloc(ict);
1935a6d42e7dSPeter Dunlap 	if (itask == NULL) {
193672cf3143Speter dunlap 		/* Finish processing request */
193772cf3143Speter dunlap 		iscsit_set_cmdsn(ict, rx_pdu);
193872cf3143Speter dunlap 
1939a6d42e7dSPeter Dunlap 		iscsit_send_direct_scsi_resp(ict, rx_pdu,
1940a6d42e7dSPeter Dunlap 		    ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
1941a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
1942a6d42e7dSPeter Dunlap 		return;
1943a6d42e7dSPeter Dunlap 	}
1944a6d42e7dSPeter Dunlap 
1945a6d42e7dSPeter Dunlap 	/*
1946a6d42e7dSPeter Dunlap 	 * Note CmdSN and ITT in task.  IDM will have already validated this
1947a6d42e7dSPeter Dunlap 	 * request against the connection state so we don't need to check
1948a6d42e7dSPeter Dunlap 	 * that (the connection may have changed state in the meantime but
1949a6d42e7dSPeter Dunlap 	 * we will catch that when we try to send a response)
1950a6d42e7dSPeter Dunlap 	 */
1951a6d42e7dSPeter Dunlap 	itask->it_cmdsn = ntohl(iscsi_scsi->cmdsn);
1952a6d42e7dSPeter Dunlap 	itask->it_itt = iscsi_scsi->itt;
1953a6d42e7dSPeter Dunlap 
1954a6d42e7dSPeter Dunlap 	/*
1955a6d42e7dSPeter Dunlap 	 * Check for extended CDB AHS
1956a6d42e7dSPeter Dunlap 	 */
1957a6d42e7dSPeter Dunlap 	if (iscsi_scsi->hlength > 0) {
1958a6d42e7dSPeter Dunlap 		ahs_hdr = (iscsi_addl_hdr_t *)iscsi_scsi;
1959a6d42e7dSPeter Dunlap 		addl_cdb_len = ((ahs_hdr->ahs_hlen_hi << 8) |
1960a6d42e7dSPeter Dunlap 		    ahs_hdr->ahs_hlen_lo) - 1; /* Adjust for reserved byte */
1961a6d42e7dSPeter Dunlap 		if (((addl_cdb_len + 4) / sizeof (uint32_t)) >
1962a6d42e7dSPeter Dunlap 		    iscsi_scsi->hlength) {
1963a6d42e7dSPeter Dunlap 			/* Mangled header info, drop it */
1964a6d42e7dSPeter Dunlap 			idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
1965a6d42e7dSPeter Dunlap 			return;
1966a6d42e7dSPeter Dunlap 		}
1967a6d42e7dSPeter Dunlap 	}
1968a6d42e7dSPeter Dunlap 
1969a6d42e7dSPeter Dunlap 	ict = rx_pdu->isp_ic->ic_handle; /* IDM client private */
1970a6d42e7dSPeter Dunlap 
197172cf3143Speter dunlap 	/*
197272cf3143Speter dunlap 	 * Add task to session list.  This function will also check to
197372cf3143Speter dunlap 	 * ensure that the task does not already exist.
197472cf3143Speter dunlap 	 */
197572cf3143Speter dunlap 	if (iscsit_task_start(itask) != IDM_STATUS_SUCCESS) {
197672cf3143Speter dunlap 		/*
197772cf3143Speter dunlap 		 * Task exists, free all resources and reject.  Don't
197872cf3143Speter dunlap 		 * update expcmdsn in this case because RFC 3720 says
197972cf3143Speter dunlap 		 * "The CmdSN of the rejected command PDU (if it is a
198072cf3143Speter dunlap 		 * non-immediate command) MUST NOT be considered received
198172cf3143Speter dunlap 		 * by the target (i.e., a command sequence gap must be
198272cf3143Speter dunlap 		 * assumed for the CmdSN), even though the CmdSN of the
198372cf3143Speter dunlap 		 * rejected command PDU may be reliably ascertained.  Upon
198472cf3143Speter dunlap 		 * receiving the Reject, the initiator MUST plug the CmdSN
198572cf3143Speter dunlap 		 * gap in order to continue to use the session.  The gap
198672cf3143Speter dunlap 		 * may be plugged either by transmitting a command PDU
198772cf3143Speter dunlap 		 * with the same CmdSN, or by aborting the task (see section
198872cf3143Speter dunlap 		 * 6.9 on how an abort may plug a CmdSN gap)." (Section 6.3)
198972cf3143Speter dunlap 		 */
199072cf3143Speter dunlap 		iscsit_task_free(itask);
199172cf3143Speter dunlap 		iscsit_send_reject(ict, rx_pdu, ISCSI_REJECT_TASK_IN_PROGRESS);
199272cf3143Speter dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
199372cf3143Speter dunlap 		return;
199472cf3143Speter dunlap 	}
199572cf3143Speter dunlap 
199672cf3143Speter dunlap 	/* Update sequence numbers */
199772cf3143Speter dunlap 	iscsit_set_cmdsn(ict, rx_pdu);
199872cf3143Speter dunlap 
199972cf3143Speter dunlap 	/*
200072cf3143Speter dunlap 	 * Allocate STMF task
200172cf3143Speter dunlap 	 */
2002a6d42e7dSPeter Dunlap 	itask->it_stmf_task = stmf_task_alloc(
2003a6d42e7dSPeter Dunlap 	    itask->it_ict->ict_sess->ist_lport,
2004a6d42e7dSPeter Dunlap 	    itask->it_ict->ict_sess->ist_stmf_sess, iscsi_scsi->lun,
2005a6d42e7dSPeter Dunlap 	    16 + addl_cdb_len, 0);
2006a6d42e7dSPeter Dunlap 	if (itask->it_stmf_task == NULL) {
2007a6d42e7dSPeter Dunlap 		/*
2008a6d42e7dSPeter Dunlap 		 * Either stmf really couldn't get memory for a task or,
2009a6d42e7dSPeter Dunlap 		 * more likely, the LU is currently in reset.  Either way
2010a6d42e7dSPeter Dunlap 		 * we have no choice but to fail the request.
2011a6d42e7dSPeter Dunlap 		 */
201272cf3143Speter dunlap 		iscsit_task_done(itask);
2013a6d42e7dSPeter Dunlap 		iscsit_task_free(itask);
2014a6d42e7dSPeter Dunlap 		iscsit_send_direct_scsi_resp(ict, rx_pdu,
2015a6d42e7dSPeter Dunlap 		    ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
201672cf3143Speter dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2017a6d42e7dSPeter Dunlap 		return;
2018a6d42e7dSPeter Dunlap 	}
2019a6d42e7dSPeter Dunlap 
2020a6d42e7dSPeter Dunlap 	task = itask->it_stmf_task;
2021a6d42e7dSPeter Dunlap 	task->task_port_private = itask;
2022a6d42e7dSPeter Dunlap 
2023a6d42e7dSPeter Dunlap 	bcopy(iscsi_scsi->lun, task->task_lun_no, sizeof (task->task_lun_no));
2024a6d42e7dSPeter Dunlap 
2025a6d42e7dSPeter Dunlap 	/*
2026a6d42e7dSPeter Dunlap 	 * iSCSI and Comstar use the same values.  Should we rely on this
2027a6d42e7dSPeter Dunlap 	 * or translate them bit-wise?
2028a6d42e7dSPeter Dunlap 	 */
2029a6d42e7dSPeter Dunlap 
2030a6d42e7dSPeter Dunlap 	task->task_flags =
2031a6d42e7dSPeter Dunlap 	    (((iscsi_scsi->flags & ISCSI_FLAG_CMD_READ) ? TF_READ_DATA : 0) |
2032a6d42e7dSPeter Dunlap 	    ((iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ? TF_WRITE_DATA : 0) |
2033a6d42e7dSPeter Dunlap 	    ((rx_pdu->isp_datalen == 0) ? 0 : TF_INITIAL_BURST));
2034a6d42e7dSPeter Dunlap 
2035a6d42e7dSPeter Dunlap 	switch (iscsi_scsi->flags & ISCSI_FLAG_CMD_ATTR_MASK) {
2036a6d42e7dSPeter Dunlap 	case ISCSI_ATTR_UNTAGGED:
2037a6d42e7dSPeter Dunlap 		break;
2038a6d42e7dSPeter Dunlap 	case ISCSI_ATTR_SIMPLE:
2039a6d42e7dSPeter Dunlap 		task->task_additional_flags |= TF_ATTR_SIMPLE_QUEUE;
2040a6d42e7dSPeter Dunlap 		break;
2041a6d42e7dSPeter Dunlap 	case ISCSI_ATTR_ORDERED:
2042a6d42e7dSPeter Dunlap 		task->task_additional_flags |= TF_ATTR_ORDERED_QUEUE;
2043a6d42e7dSPeter Dunlap 		break;
2044a6d42e7dSPeter Dunlap 	case ISCSI_ATTR_HEAD_OF_QUEUE:
2045a6d42e7dSPeter Dunlap 		task->task_additional_flags |= TF_ATTR_HEAD_OF_QUEUE;
2046a6d42e7dSPeter Dunlap 		break;
2047a6d42e7dSPeter Dunlap 	case ISCSI_ATTR_ACA:
2048a6d42e7dSPeter Dunlap 		task->task_additional_flags |= TF_ATTR_ACA;
2049a6d42e7dSPeter Dunlap 		break;
2050a6d42e7dSPeter Dunlap 	default:
2051a6d42e7dSPeter Dunlap 		/* Protocol error but just take it, treat as untagged */
2052a6d42e7dSPeter Dunlap 		break;
2053a6d42e7dSPeter Dunlap 	}
2054a6d42e7dSPeter Dunlap 
2055a6d42e7dSPeter Dunlap 
2056a6d42e7dSPeter Dunlap 	task->task_additional_flags = 0;
2057a6d42e7dSPeter Dunlap 	task->task_priority = 0;
2058a6d42e7dSPeter Dunlap 	task->task_mgmt_function = TM_NONE;
2059a6d42e7dSPeter Dunlap 
2060a6d42e7dSPeter Dunlap 	/*
2061a6d42e7dSPeter Dunlap 	 * This "task_max_nbufs" doesn't map well to BIDI.  We probably need
2062a6d42e7dSPeter Dunlap 	 * parameter for each direction.  "MaxOutstandingR2T" may very well
2063a6d42e7dSPeter Dunlap 	 * be set to one which could prevent us from doing simultaneous
2064a6d42e7dSPeter Dunlap 	 * transfers in each direction.
2065a6d42e7dSPeter Dunlap 	 */
2066a6d42e7dSPeter Dunlap 	task->task_max_nbufs = (iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ?
2067a6d42e7dSPeter Dunlap 	    ict->ict_op.op_max_outstanding_r2t : STMF_BUFS_MAX;
2068a6d42e7dSPeter Dunlap 	task->task_cmd_seq_no = ntohl(iscsi_scsi->itt);
2069a6d42e7dSPeter Dunlap 	task->task_expected_xfer_length = ntohl(iscsi_scsi->data_length);
2070a6d42e7dSPeter Dunlap 
2071a6d42e7dSPeter Dunlap 	/* Copy CDB */
2072a6d42e7dSPeter Dunlap 	bcopy(iscsi_scsi->scb, task->task_cdb, 16);
2073a6d42e7dSPeter Dunlap 	if (addl_cdb_len > 0) {
2074a6d42e7dSPeter Dunlap 		bcopy(ahs_hdr->ahs_extscb, task->task_cdb + 16, addl_cdb_len);
2075a6d42e7dSPeter Dunlap 	}
2076a6d42e7dSPeter Dunlap 
2077a668b114SPriya Krishnan 	DTRACE_ISCSI_3(scsi__command, idm_conn_t *, ic,
2078a668b114SPriya Krishnan 	    iscsi_scsi_cmd_hdr_t *, (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr,
2079a668b114SPriya Krishnan 	    scsi_task_t *, task);
2080a668b114SPriya Krishnan 
2081a6d42e7dSPeter Dunlap 	/*
2082a6d42e7dSPeter Dunlap 	 * Copy the transport header into the task handle from the PDU
2083a6d42e7dSPeter Dunlap 	 * handle. The transport header describes this task's remote tagged
2084a6d42e7dSPeter Dunlap 	 * buffer.
2085a6d42e7dSPeter Dunlap 	 */
2086a6d42e7dSPeter Dunlap 	if (rx_pdu->isp_transport_hdrlen != 0) {
2087a6d42e7dSPeter Dunlap 		bcopy(rx_pdu->isp_transport_hdr,
2088a6d42e7dSPeter Dunlap 		    itask->it_idm_task->idt_transport_hdr,
2089a6d42e7dSPeter Dunlap 		    rx_pdu->isp_transport_hdrlen);
2090a6d42e7dSPeter Dunlap 	}
2091a6d42e7dSPeter Dunlap 
2092a6d42e7dSPeter Dunlap 	/*
2093a6d42e7dSPeter Dunlap 	 * Tell IDM about our new active task
2094a6d42e7dSPeter Dunlap 	 */
2095a6d42e7dSPeter Dunlap 	idm_task_start(itask->it_idm_task, (uintptr_t)itask->it_itt);
2096a6d42e7dSPeter Dunlap 
2097a6d42e7dSPeter Dunlap 	/*
2098a6d42e7dSPeter Dunlap 	 * If we have any immediate data then setup the immediate buffer
2099a6d42e7dSPeter Dunlap 	 * context that comes with the task
2100a6d42e7dSPeter Dunlap 	 */
2101a6d42e7dSPeter Dunlap 	if (rx_pdu->isp_datalen) {
2102a6d42e7dSPeter Dunlap 		ibuf = itask->it_immed_data;
2103a6d42e7dSPeter Dunlap 		ibuf->ibuf_immed_data_pdu = rx_pdu;
2104a6d42e7dSPeter Dunlap 		ibuf->ibuf_stmf_buf->db_data_size = rx_pdu->isp_datalen;
2105a6d42e7dSPeter Dunlap 		ibuf->ibuf_stmf_buf->db_buf_size = rx_pdu->isp_datalen;
2106a6d42e7dSPeter Dunlap 		ibuf->ibuf_stmf_buf->db_relative_offset = 0;
2107a6d42e7dSPeter Dunlap 		ibuf->ibuf_stmf_buf->db_sglist[0].seg_length =
2108a6d42e7dSPeter Dunlap 		    rx_pdu->isp_datalen;
2109a6d42e7dSPeter Dunlap 		ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr = rx_pdu->isp_data;
2110a6d42e7dSPeter Dunlap 
2111a668b114SPriya Krishnan 		DTRACE_ISCSI_8(xfer__start, idm_conn_t *, ic,
2112a668b114SPriya Krishnan 		    uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
2113a668b114SPriya Krishnan 		    uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
2114a668b114SPriya Krishnan 		    uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
2115a668b114SPriya Krishnan 		    uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
2116a668b114SPriya Krishnan 
21173badd853SPriya Krishnan 		/*
21183badd853SPriya Krishnan 		 * For immediate data transfer, there is no callback from
21193badd853SPriya Krishnan 		 * stmf to indicate that the initial burst of data is
21203badd853SPriya Krishnan 		 * transferred successfully. In some cases, the task can
21213badd853SPriya Krishnan 		 * get freed before execution returns from stmf_post_task.
21223badd853SPriya Krishnan 		 * Although this xfer-start/done probe accurately tracks
21233badd853SPriya Krishnan 		 * the size of the transfer, it does only provide a best
21243badd853SPriya Krishnan 		 * effort on the timing of the transfer.
21253badd853SPriya Krishnan 		 */
2126a668b114SPriya Krishnan 		DTRACE_ISCSI_8(xfer__done, idm_conn_t *, ic,
2127a668b114SPriya Krishnan 		    uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
2128a668b114SPriya Krishnan 		    uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
2129a668b114SPriya Krishnan 		    uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
2130a668b114SPriya Krishnan 		    uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
21313badd853SPriya Krishnan 		stmf_post_task(task, ibuf->ibuf_stmf_buf);
2132a6d42e7dSPeter Dunlap 	} else {
21333badd853SPriya Krishnan 
2134a6d42e7dSPeter Dunlap 		stmf_post_task(task, NULL);
2135a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2136a6d42e7dSPeter Dunlap 	}
2137a6d42e7dSPeter Dunlap }
2138a6d42e7dSPeter Dunlap 
2139a6d42e7dSPeter Dunlap /*ARGSUSED*/
2140a6d42e7dSPeter Dunlap void
2141a6d42e7dSPeter Dunlap iscsit_deferred_dispatch(idm_pdu_t *rx_pdu)
2142a6d42e7dSPeter Dunlap {
2143a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
2144a6d42e7dSPeter Dunlap 
2145a6d42e7dSPeter Dunlap 	/*
2146a6d42e7dSPeter Dunlap 	 * If the connection has been lost then ignore new PDU's
2147a6d42e7dSPeter Dunlap 	 */
2148a6d42e7dSPeter Dunlap 	mutex_enter(&ict->ict_mutex);
2149a6d42e7dSPeter Dunlap 	if (ict->ict_lost) {
2150a6d42e7dSPeter Dunlap 		mutex_exit(&ict->ict_mutex);
2151a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2152a6d42e7dSPeter Dunlap 		return;
2153a6d42e7dSPeter Dunlap 	}
2154a6d42e7dSPeter Dunlap 
2155a6d42e7dSPeter Dunlap 	/*
2156a6d42e7dSPeter Dunlap 	 * Grab a hold on the connection to prevent it from going away
2157a6d42e7dSPeter Dunlap 	 * between now and when the taskq function is called.
2158a6d42e7dSPeter Dunlap 	 */
2159a6d42e7dSPeter Dunlap 	iscsit_conn_dispatch_hold(ict);
2160a6d42e7dSPeter Dunlap 	mutex_exit(&ict->ict_mutex);
2161a6d42e7dSPeter Dunlap 
2162a6d42e7dSPeter Dunlap 	if (taskq_dispatch(iscsit_global.global_dispatch_taskq,
2163a6d42e7dSPeter Dunlap 	    iscsit_deferred, rx_pdu, DDI_NOSLEEP) == NULL) {
2164a6d42e7dSPeter Dunlap 		/*
2165a6d42e7dSPeter Dunlap 		 * In the unlikely scenario that we couldn't get the resources
2166a6d42e7dSPeter Dunlap 		 * to dispatch the PDU then just drop it.
2167a6d42e7dSPeter Dunlap 		 */
2168a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2169a6d42e7dSPeter Dunlap 		idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL);
2170a6d42e7dSPeter Dunlap 		iscsit_conn_dispatch_rele(ict);
2171a6d42e7dSPeter Dunlap 	}
2172a6d42e7dSPeter Dunlap }
2173a6d42e7dSPeter Dunlap 
2174a6d42e7dSPeter Dunlap static void
2175a6d42e7dSPeter Dunlap iscsit_deferred(void *rx_pdu_void)
2176a6d42e7dSPeter Dunlap {
2177*d618d68dSPriya Krishnan 	idm_pdu_t		*rx_pdu = rx_pdu_void;
2178*d618d68dSPriya Krishnan 	idm_conn_t		*ic = rx_pdu->isp_ic;
2179*d618d68dSPriya Krishnan 	iscsit_conn_t		*ict = ic->ic_handle;
2180a6d42e7dSPeter Dunlap 
2181*d618d68dSPriya Krishnan 	/*
2182*d618d68dSPriya Krishnan 	 * NOP and Task Management Commands can be marked for immediate
2183*d618d68dSPriya Krishnan 	 * delivery. Commands marked as 'Immediate' are to be considered
2184*d618d68dSPriya Krishnan 	 * for execution as soon as they arrive on the target. So these
2185*d618d68dSPriya Krishnan 	 * should not be checked for sequence order and put in a queue.
2186*d618d68dSPriya Krishnan 	 * The CmdSN is not advanced for Immediate Commands.
2187*d618d68dSPriya Krishnan 	 */
2188a6d42e7dSPeter Dunlap 	switch (IDM_PDU_OPCODE(rx_pdu)) {
2189a6d42e7dSPeter Dunlap 	case ISCSI_OP_NOOP_OUT:
2190*d618d68dSPriya Krishnan 		if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2191*d618d68dSPriya Krishnan 			iscsit_set_cmdsn(ict, rx_pdu);
2192*d618d68dSPriya Krishnan 			iscsit_pdu_op_noop(ict, rx_pdu);
2193*d618d68dSPriya Krishnan 		}
2194a6d42e7dSPeter Dunlap 		break;
2195a6d42e7dSPeter Dunlap 	case ISCSI_OP_LOGIN_CMD:
2196a6d42e7dSPeter Dunlap 		iscsit_pdu_op_login_cmd(ict, rx_pdu);
2197*d618d68dSPriya Krishnan 		iscsit_conn_dispatch_rele(ict);
2198*d618d68dSPriya Krishnan 		return;
2199a6d42e7dSPeter Dunlap 	case ISCSI_OP_TEXT_CMD:
2200*d618d68dSPriya Krishnan 		if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2201*d618d68dSPriya Krishnan 			iscsit_set_cmdsn(ict, rx_pdu);
2202*d618d68dSPriya Krishnan 			iscsit_pdu_op_text_cmd(ict, rx_pdu);
2203*d618d68dSPriya Krishnan 		}
2204a6d42e7dSPeter Dunlap 		break;
2205a6d42e7dSPeter Dunlap 	case ISCSI_OP_LOGOUT_CMD:
2206*d618d68dSPriya Krishnan 		if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2207*d618d68dSPriya Krishnan 			iscsit_set_cmdsn(ict, rx_pdu);
2208*d618d68dSPriya Krishnan 			iscsit_pdu_op_logout_cmd(ict, rx_pdu);
2209*d618d68dSPriya Krishnan 		}
2210a6d42e7dSPeter Dunlap 		break;
2211a6d42e7dSPeter Dunlap 	default:
2212a6d42e7dSPeter Dunlap 		/* Protocol error.  IDM should have caught this */
2213a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2214a6d42e7dSPeter Dunlap 		ASSERT(0);
2215a6d42e7dSPeter Dunlap 		break;
2216a6d42e7dSPeter Dunlap 	}
2217*d618d68dSPriya Krishnan 	/*
2218*d618d68dSPriya Krishnan 	 * Check if there are other PDUs in the session staging queue
2219*d618d68dSPriya Krishnan 	 * waiting to be posted to SCSI layer.
2220*d618d68dSPriya Krishnan 	 */
2221*d618d68dSPriya Krishnan 	iscsit_process_pdu_in_queue(ict->ict_sess);
2222a6d42e7dSPeter Dunlap 
2223a6d42e7dSPeter Dunlap 	iscsit_conn_dispatch_rele(ict);
2224a6d42e7dSPeter Dunlap }
2225a6d42e7dSPeter Dunlap 
2226a6d42e7dSPeter Dunlap static void
2227a6d42e7dSPeter Dunlap iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu,
2228a6d42e7dSPeter Dunlap     uint8_t response, uint8_t cmd_status)
2229a6d42e7dSPeter Dunlap {
2230a6d42e7dSPeter Dunlap 	idm_pdu_t			*rsp_pdu;
2231a6d42e7dSPeter Dunlap 	idm_conn_t			*ic;
2232a6d42e7dSPeter Dunlap 	iscsi_scsi_rsp_hdr_t		*resp;
223330e7468fSPeter Dunlap 	iscsi_scsi_cmd_hdr_t		*req =
223430e7468fSPeter Dunlap 	    (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2235a6d42e7dSPeter Dunlap 
2236a6d42e7dSPeter Dunlap 	ic = ict->ict_ic;
2237a6d42e7dSPeter Dunlap 
2238a6d42e7dSPeter Dunlap 	rsp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_rsp_hdr_t), 0);
2239a6d42e7dSPeter Dunlap 	idm_pdu_init(rsp_pdu, ic, NULL, NULL);
224060220f10SPriya Krishnan 	/*
224160220f10SPriya Krishnan 	 * StatSN is incremented by 1 for every response sent on
224260220f10SPriya Krishnan 	 * a connection except for responses sent as a result of
224360220f10SPriya Krishnan 	 * a retry or SNACK
224460220f10SPriya Krishnan 	 */
224560220f10SPriya Krishnan 	rsp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
224660220f10SPriya Krishnan 
2247a6d42e7dSPeter Dunlap 	resp = (iscsi_scsi_rsp_hdr_t *)rsp_pdu->isp_hdr;
2248a6d42e7dSPeter Dunlap 
2249a6d42e7dSPeter Dunlap 	resp->opcode = ISCSI_OP_SCSI_RSP;
2250a6d42e7dSPeter Dunlap 	resp->flags = ISCSI_FLAG_FINAL;
2251a6d42e7dSPeter Dunlap 	resp->response = response;
2252a6d42e7dSPeter Dunlap 	resp->cmd_status = cmd_status;
225330e7468fSPeter Dunlap 	resp->itt = req->itt;
225430e7468fSPeter Dunlap 	if ((response == ISCSI_STATUS_CMD_COMPLETED) &&
225530e7468fSPeter Dunlap 	    (req->data_length != 0) &&
225630e7468fSPeter Dunlap 	    ((req->flags & ISCSI_FLAG_CMD_READ) ||
225730e7468fSPeter Dunlap 	    (req->flags & ISCSI_FLAG_CMD_WRITE))) {
225830e7468fSPeter Dunlap 		resp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
225930e7468fSPeter Dunlap 		resp->residual_count = req->data_length;
226030e7468fSPeter Dunlap 	}
226130e7468fSPeter Dunlap 
226230e7468fSPeter Dunlap 	DTRACE_PROBE4(iscsi__scsi__direct__response,
226330e7468fSPeter Dunlap 	    iscsit_conn_t *, ict,
226430e7468fSPeter Dunlap 	    uint8_t, resp->response,
226530e7468fSPeter Dunlap 	    uint8_t, resp->cmd_status,
226630e7468fSPeter Dunlap 	    idm_pdu_t *, rsp_pdu);
2267a6d42e7dSPeter Dunlap 
2268a6d42e7dSPeter Dunlap 	iscsit_pdu_tx(rsp_pdu);
2269a6d42e7dSPeter Dunlap }
2270a6d42e7dSPeter Dunlap 
2271a6d42e7dSPeter Dunlap void
2272a6d42e7dSPeter Dunlap iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu, uint8_t tm_status)
2273a6d42e7dSPeter Dunlap {
2274a6d42e7dSPeter Dunlap 	iscsi_scsi_task_mgt_rsp_hdr_t	*tm_resp;
2275a6d42e7dSPeter Dunlap 
227660220f10SPriya Krishnan 	/*
227760220f10SPriya Krishnan 	 * The target must take note of the last-sent StatSN.
227860220f10SPriya Krishnan 	 * The StatSN is to be incremented after sending a
227960220f10SPriya Krishnan 	 * task management response. Digest recovery can only
228060220f10SPriya Krishnan 	 * work if StatSN is incremented.
228160220f10SPriya Krishnan 	 */
228260220f10SPriya Krishnan 	tm_resp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2283a6d42e7dSPeter Dunlap 	tm_resp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2284a6d42e7dSPeter Dunlap 	tm_resp->response = tm_status;
228530e7468fSPeter Dunlap 
228630e7468fSPeter Dunlap 	DTRACE_PROBE3(iscsi__scsi__tm__response,
228730e7468fSPeter Dunlap 	    iscsit_conn_t *, tm_resp_pdu->isp_ic->ic_handle,
228830e7468fSPeter Dunlap 	    uint8_t, tm_resp->response,
228930e7468fSPeter Dunlap 	    idm_pdu_t *, tm_resp_pdu);
2290a6d42e7dSPeter Dunlap 	iscsit_pdu_tx(tm_resp_pdu);
2291a6d42e7dSPeter Dunlap }
2292a6d42e7dSPeter Dunlap 
2293a6d42e7dSPeter Dunlap void
2294a6d42e7dSPeter Dunlap iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2295a6d42e7dSPeter Dunlap {
2296a6d42e7dSPeter Dunlap 	idm_pdu_t			*tm_resp_pdu;
2297a6d42e7dSPeter Dunlap 	iscsit_task_t			*itask;
2298a6d42e7dSPeter Dunlap 	iscsit_task_t			*tm_itask;
2299a6d42e7dSPeter Dunlap 	scsi_task_t			*task;
2300a6d42e7dSPeter Dunlap 	iscsi_scsi_task_mgt_hdr_t 	*iscsi_tm =
2301a6d42e7dSPeter Dunlap 	    (iscsi_scsi_task_mgt_hdr_t *)rx_pdu->isp_hdr;
2302a6d42e7dSPeter Dunlap 	iscsi_scsi_task_mgt_rsp_hdr_t 	*iscsi_tm_rsp =
2303a6d42e7dSPeter Dunlap 	    (iscsi_scsi_task_mgt_rsp_hdr_t *)rx_pdu->isp_hdr;
2304a6d42e7dSPeter Dunlap 	uint32_t			rtt, cmdsn, refcmdsn;
2305a6d42e7dSPeter Dunlap 	uint8_t				tm_func;
2306a6d42e7dSPeter Dunlap 
2307a6d42e7dSPeter Dunlap 	/*
2308a6d42e7dSPeter Dunlap 	 * Setup response PDU (response field will get filled in later)
2309a6d42e7dSPeter Dunlap 	 */
2310a6d42e7dSPeter Dunlap 	tm_resp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_task_mgt_rsp_hdr_t), 0);
2311a6d42e7dSPeter Dunlap 	if (tm_resp_pdu == NULL) {
2312a6d42e7dSPeter Dunlap 		/* Can't respond, just drop it */
2313a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2314a6d42e7dSPeter Dunlap 		return;
2315a6d42e7dSPeter Dunlap 	}
2316a6d42e7dSPeter Dunlap 	idm_pdu_init(tm_resp_pdu, ict->ict_ic, NULL, NULL);
2317a6d42e7dSPeter Dunlap 	iscsi_tm_rsp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2318a6d42e7dSPeter Dunlap 	bzero(iscsi_tm_rsp, sizeof (iscsi_scsi_task_mgt_rsp_hdr_t));
2319a6d42e7dSPeter Dunlap 	iscsi_tm_rsp->opcode = ISCSI_OP_SCSI_TASK_MGT_RSP;
2320a6d42e7dSPeter Dunlap 	iscsi_tm_rsp->flags = ISCSI_FLAG_FINAL;
2321a6d42e7dSPeter Dunlap 	iscsi_tm_rsp->itt = rx_pdu->isp_hdr->itt;
2322a6d42e7dSPeter Dunlap 
2323a6d42e7dSPeter Dunlap 	/*
2324a6d42e7dSPeter Dunlap 	 * Figure out what we're being asked to do.
2325a6d42e7dSPeter Dunlap 	 */
232630e7468fSPeter Dunlap 	DTRACE_PROBE4(iscsi__scsi__tm__request,
232730e7468fSPeter Dunlap 	    iscsit_conn_t *, ict,
232830e7468fSPeter Dunlap 	    uint8_t, (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK),
232930e7468fSPeter Dunlap 	    uint32_t, iscsi_tm->rtt,
233030e7468fSPeter Dunlap 	    idm_pdu_t *, rx_pdu);
2331a6d42e7dSPeter Dunlap 	switch (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK) {
2332a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_ABORT_TASK:
2333a6d42e7dSPeter Dunlap 		/*
2334a6d42e7dSPeter Dunlap 		 * STMF doesn't currently support the "abort task" task
2335a6d42e7dSPeter Dunlap 		 * management command although it does support aborting
2336a6d42e7dSPeter Dunlap 		 * an individual task.  We'll get STMF to abort the task
2337a6d42e7dSPeter Dunlap 		 * for us but handle the details of the task management
2338a6d42e7dSPeter Dunlap 		 * command ourselves.
2339a6d42e7dSPeter Dunlap 		 *
2340a6d42e7dSPeter Dunlap 		 * Find the task associated with the referenced task tag.
2341a6d42e7dSPeter Dunlap 		 */
2342a6d42e7dSPeter Dunlap 		rtt = iscsi_tm->rtt;
2343a6d42e7dSPeter Dunlap 		itask = (iscsit_task_t *)idm_task_find_by_handle(ict->ict_ic,
2344a6d42e7dSPeter Dunlap 		    (uintptr_t)rtt);
2345a6d42e7dSPeter Dunlap 
2346a6d42e7dSPeter Dunlap 		if (itask == NULL) {
2347a6d42e7dSPeter Dunlap 			cmdsn = ntohl(iscsi_tm->cmdsn);
2348a6d42e7dSPeter Dunlap 			refcmdsn = ntohl(iscsi_tm->refcmdsn);
2349a6d42e7dSPeter Dunlap 
2350a6d42e7dSPeter Dunlap 			/*
2351*d618d68dSPriya Krishnan 			 * Task was not found. But the SCSI command could be
2352*d618d68dSPriya Krishnan 			 * on the rxpdu wait queue. If RefCmdSN is within
2353*d618d68dSPriya Krishnan 			 * the CmdSN window and less than CmdSN of the TM
2354*d618d68dSPriya Krishnan 			 * function, return "Function Complete". Otherwise,
2355*d618d68dSPriya Krishnan 			 * return "Task Does Not Exist".
2356a6d42e7dSPeter Dunlap 			 */
2357a6d42e7dSPeter Dunlap 
2358a6d42e7dSPeter Dunlap 			if (iscsit_cmdsn_in_window(ict, refcmdsn) &&
2359*d618d68dSPriya Krishnan 			    iscsit_sna_lt(refcmdsn, cmdsn)) {
2360*d618d68dSPriya Krishnan 				mutex_enter(&ict->ict_sess->ist_sn_mutex);
2361*d618d68dSPriya Krishnan 				(void) iscsit_remove_pdu_from_queue(
2362*d618d68dSPriya Krishnan 				    ict->ict_sess, refcmdsn);
2363*d618d68dSPriya Krishnan 				iscsit_conn_dispatch_rele(ict);
2364*d618d68dSPriya Krishnan 				mutex_exit(&ict->ict_sess->ist_sn_mutex);
2365a6d42e7dSPeter Dunlap 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2366a6d42e7dSPeter Dunlap 				    SCSI_TCP_TM_RESP_COMPLETE);
2367a6d42e7dSPeter Dunlap 			} else {
2368a6d42e7dSPeter Dunlap 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2369a6d42e7dSPeter Dunlap 				    SCSI_TCP_TM_RESP_NO_TASK);
2370a6d42e7dSPeter Dunlap 			}
2371a6d42e7dSPeter Dunlap 		} else {
2372a6d42e7dSPeter Dunlap 
2373a6d42e7dSPeter Dunlap 			/*
2374a6d42e7dSPeter Dunlap 			 * Tell STMF to abort the task.  This will do no harm
2375a6d42e7dSPeter Dunlap 			 * if the task is already complete.
2376a6d42e7dSPeter Dunlap 			 */
2377a6d42e7dSPeter Dunlap 			stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
2378a6d42e7dSPeter Dunlap 			    STMF_ABORTED, NULL);
2379a6d42e7dSPeter Dunlap 
2380a6d42e7dSPeter Dunlap 			/*
2381a6d42e7dSPeter Dunlap 			 * Make sure the task hasn't already completed
2382a6d42e7dSPeter Dunlap 			 */
2383a6d42e7dSPeter Dunlap 			mutex_enter(&itask->it_idm_task->idt_mutex);
2384a6d42e7dSPeter Dunlap 			if ((itask->it_idm_task->idt_state == TASK_COMPLETE) ||
2385a6d42e7dSPeter Dunlap 			    (itask->it_idm_task->idt_state == TASK_IDLE)) {
2386a6d42e7dSPeter Dunlap 				/*
2387a6d42e7dSPeter Dunlap 				 * Task is complete, return "Task Does Not
2388a6d42e7dSPeter Dunlap 				 * Exist"
2389a6d42e7dSPeter Dunlap 				 */
2390a6d42e7dSPeter Dunlap 				mutex_exit(&itask->it_idm_task->idt_mutex);
2391a6d42e7dSPeter Dunlap 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2392a6d42e7dSPeter Dunlap 				    SCSI_TCP_TM_RESP_NO_TASK);
2393a6d42e7dSPeter Dunlap 			} else {
2394a6d42e7dSPeter Dunlap 				/*
2395a6d42e7dSPeter Dunlap 				 * STMF is now aborting the task, return
2396a6d42e7dSPeter Dunlap 				 * "Function Complete"
2397a6d42e7dSPeter Dunlap 				 */
2398a6d42e7dSPeter Dunlap 				mutex_exit(&itask->it_idm_task->idt_mutex);
2399a6d42e7dSPeter Dunlap 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2400a6d42e7dSPeter Dunlap 				    SCSI_TCP_TM_RESP_COMPLETE);
2401a6d42e7dSPeter Dunlap 			}
2402a6d42e7dSPeter Dunlap 			idm_task_rele(itask->it_idm_task);
2403a6d42e7dSPeter Dunlap 		}
2404a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2405a6d42e7dSPeter Dunlap 		return;
2406a6d42e7dSPeter Dunlap 
2407a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_ABORT_TASK_SET:
2408a6d42e7dSPeter Dunlap 		tm_func = TM_ABORT_TASK_SET;
2409a6d42e7dSPeter Dunlap 		break;
2410a6d42e7dSPeter Dunlap 
2411a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_CLEAR_ACA:
2412a6d42e7dSPeter Dunlap 		tm_func = TM_CLEAR_ACA;
2413a6d42e7dSPeter Dunlap 		break;
2414a6d42e7dSPeter Dunlap 
2415a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2416a6d42e7dSPeter Dunlap 		tm_func = TM_CLEAR_TASK_SET;
2417a6d42e7dSPeter Dunlap 		break;
2418a6d42e7dSPeter Dunlap 
2419a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2420a6d42e7dSPeter Dunlap 		tm_func = TM_LUN_RESET;
2421a6d42e7dSPeter Dunlap 		break;
2422a6d42e7dSPeter Dunlap 
2423a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2424a6d42e7dSPeter Dunlap 		tm_func = TM_TARGET_WARM_RESET;
2425a6d42e7dSPeter Dunlap 		break;
2426a6d42e7dSPeter Dunlap 
2427a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2428a6d42e7dSPeter Dunlap 		tm_func = TM_TARGET_COLD_RESET;
2429a6d42e7dSPeter Dunlap 		break;
2430a6d42e7dSPeter Dunlap 
2431a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_TASK_REASSIGN:
2432a6d42e7dSPeter Dunlap 		/*
2433a6d42e7dSPeter Dunlap 		 * We do not currently support allegiance reassignment.  When
2434a6d42e7dSPeter Dunlap 		 * we start supporting ERL1+, we will need to.
2435a6d42e7dSPeter Dunlap 		 */
2436a6d42e7dSPeter Dunlap 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2437a6d42e7dSPeter Dunlap 		    SCSI_TCP_TM_RESP_NO_ALLG_REASSN);
2438a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2439a6d42e7dSPeter Dunlap 		return;
2440a6d42e7dSPeter Dunlap 
2441a6d42e7dSPeter Dunlap 	default:
2442a6d42e7dSPeter Dunlap 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2443a6d42e7dSPeter Dunlap 		    SCSI_TCP_TM_RESP_REJECTED);
2444a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2445a6d42e7dSPeter Dunlap 		return;
2446a6d42e7dSPeter Dunlap 	}
2447a6d42e7dSPeter Dunlap 
2448a6d42e7dSPeter Dunlap 	tm_itask = iscsit_tm_task_alloc(ict);
2449a6d42e7dSPeter Dunlap 	if (tm_itask == NULL) {
2450a6d42e7dSPeter Dunlap 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2451a6d42e7dSPeter Dunlap 		    SCSI_TCP_TM_RESP_REJECTED);
2452a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2453a6d42e7dSPeter Dunlap 		return;
2454a6d42e7dSPeter Dunlap 	}
2455a6d42e7dSPeter Dunlap 
2456a6d42e7dSPeter Dunlap 
2457a6d42e7dSPeter Dunlap 	task = stmf_task_alloc(ict->ict_sess->ist_lport,
2458a6d42e7dSPeter Dunlap 	    ict->ict_sess->ist_stmf_sess, iscsi_tm->lun,
2459a6d42e7dSPeter Dunlap 	    0, STMF_TASK_EXT_NONE);
2460a6d42e7dSPeter Dunlap 	if (task == NULL) {
2461a6d42e7dSPeter Dunlap 		/*
2462a6d42e7dSPeter Dunlap 		 * If this happens, either the LU is in reset, couldn't
2463a6d42e7dSPeter Dunlap 		 * get memory, or some other condition in which we simply
2464a6d42e7dSPeter Dunlap 		 * can't complete this request.  It would be nice to return
2465a6d42e7dSPeter Dunlap 		 * an error code like "busy" but the closest we have is
2466a6d42e7dSPeter Dunlap 		 * "rejected".
2467a6d42e7dSPeter Dunlap 		 */
2468a6d42e7dSPeter Dunlap 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2469a6d42e7dSPeter Dunlap 		    SCSI_TCP_TM_RESP_REJECTED);
2470a6d42e7dSPeter Dunlap 		iscsit_tm_task_free(tm_itask);
2471a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2472a6d42e7dSPeter Dunlap 		return;
2473a6d42e7dSPeter Dunlap 	}
2474a6d42e7dSPeter Dunlap 
2475a6d42e7dSPeter Dunlap 	tm_itask->it_tm_pdu = tm_resp_pdu;
2476a6d42e7dSPeter Dunlap 	tm_itask->it_stmf_task = task;
2477a6d42e7dSPeter Dunlap 	task->task_port_private = tm_itask;
2478a6d42e7dSPeter Dunlap 	task->task_mgmt_function = tm_func;
2479a6d42e7dSPeter Dunlap 	task->task_additional_flags = TASK_AF_NO_EXPECTED_XFER_LENGTH;
2480a6d42e7dSPeter Dunlap 	task->task_priority = 0;
2481a6d42e7dSPeter Dunlap 	task->task_max_nbufs = STMF_BUFS_MAX;
2482a6d42e7dSPeter Dunlap 	task->task_cmd_seq_no = iscsi_tm->itt;
2483a6d42e7dSPeter Dunlap 	task->task_expected_xfer_length = 0;
2484a6d42e7dSPeter Dunlap 
2485a6d42e7dSPeter Dunlap 	stmf_post_task(task, NULL);
2486a6d42e7dSPeter Dunlap 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2487a6d42e7dSPeter Dunlap }
2488a6d42e7dSPeter Dunlap 
2489a6d42e7dSPeter Dunlap static void
2490a6d42e7dSPeter Dunlap iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2491a6d42e7dSPeter Dunlap {
2492a6d42e7dSPeter Dunlap 	iscsi_nop_out_hdr_t *out = (iscsi_nop_out_hdr_t *)rx_pdu->isp_hdr;
2493a6d42e7dSPeter Dunlap 	iscsi_nop_in_hdr_t *in;
2494a6d42e7dSPeter Dunlap 	int resp_datalen;
2495a6d42e7dSPeter Dunlap 	idm_pdu_t *resp;
2496a6d42e7dSPeter Dunlap 
2497a6d42e7dSPeter Dunlap 	/* Ignore the response from initiator */
249872cf3143Speter dunlap 	if ((out->itt == ISCSI_RSVD_TASK_TAG) ||
249972cf3143Speter dunlap 	    (out->ttt != ISCSI_RSVD_TASK_TAG)) {
250072cf3143Speter dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2501a6d42e7dSPeter Dunlap 		return;
250272cf3143Speter dunlap 	}
2503a6d42e7dSPeter Dunlap 
2504a6d42e7dSPeter Dunlap 	/* Allocate a PDU to respond */
2505a6d42e7dSPeter Dunlap 	resp_datalen = ntoh24(out->dlength);
2506a6d42e7dSPeter Dunlap 	resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
2507a6d42e7dSPeter Dunlap 	idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
2508a6d42e7dSPeter Dunlap 	if (resp_datalen > 0) {
2509a6d42e7dSPeter Dunlap 		bcopy(rx_pdu->isp_data, resp->isp_data, resp_datalen);
2510a6d42e7dSPeter Dunlap 	}
2511a6d42e7dSPeter Dunlap 
251260220f10SPriya Krishnan 	/*
251360220f10SPriya Krishnan 	 * When sending a NOP-In as a response to a NOP-Out from the initiator,
251460220f10SPriya Krishnan 	 * the target must respond with the same initiator task tag that was
251560220f10SPriya Krishnan 	 * provided in the NOP-Out request, the target transfer tag must be
251660220f10SPriya Krishnan 	 * ISCSI_RSVD_TASK_TAG (0xffffffff) and StatSN will contain the next
251760220f10SPriya Krishnan 	 * status sequence number. The StatSN for the connection is advanced
251860220f10SPriya Krishnan 	 * after this PDU is sent.
251960220f10SPriya Krishnan 	 */
2520a6d42e7dSPeter Dunlap 	in = (iscsi_nop_in_hdr_t *)resp->isp_hdr;
2521a6d42e7dSPeter Dunlap 	bzero(in, sizeof (*in));
2522a6d42e7dSPeter Dunlap 	in->opcode = ISCSI_OP_NOOP_IN;
2523a6d42e7dSPeter Dunlap 	in->flags = ISCSI_FLAG_FINAL;
2524a6d42e7dSPeter Dunlap 	bcopy(out->lun, in->lun, 8);
2525a6d42e7dSPeter Dunlap 	in->itt		= out->itt;
2526a6d42e7dSPeter Dunlap 	in->ttt		= ISCSI_RSVD_TASK_TAG;
2527a6d42e7dSPeter Dunlap 	hton24(in->dlength, resp_datalen);
252860220f10SPriya Krishnan 	resp->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2529a6d42e7dSPeter Dunlap 	/* Any other field in resp to be set? */
2530a6d42e7dSPeter Dunlap 	iscsit_pdu_tx(resp);
2531a6d42e7dSPeter Dunlap 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2532a6d42e7dSPeter Dunlap }
2533a6d42e7dSPeter Dunlap 
2534a6d42e7dSPeter Dunlap static void
2535a6d42e7dSPeter Dunlap iscsit_pdu_op_login_cmd(iscsit_conn_t	*ict, idm_pdu_t *rx_pdu)
2536a6d42e7dSPeter Dunlap {
2537a6d42e7dSPeter Dunlap 
2538a6d42e7dSPeter Dunlap 	/*
2539a6d42e7dSPeter Dunlap 	 * Submit PDU to login state machine.  State machine will free the
2540a6d42e7dSPeter Dunlap 	 * PDU.
2541a6d42e7dSPeter Dunlap 	 */
2542a6d42e7dSPeter Dunlap 	iscsit_login_sm_event(ict, ILE_LOGIN_RCV, rx_pdu);
2543a6d42e7dSPeter Dunlap }
2544a6d42e7dSPeter Dunlap 
2545a6d42e7dSPeter Dunlap void
2546a6d42e7dSPeter Dunlap iscsit_pdu_op_logout_cmd(iscsit_conn_t	*ict, idm_pdu_t *rx_pdu)
2547a6d42e7dSPeter Dunlap {
2548a6d42e7dSPeter Dunlap 	iscsi_logout_hdr_t 	*logout_req =
2549a6d42e7dSPeter Dunlap 	    (iscsi_logout_hdr_t *)rx_pdu->isp_hdr;
2550a6d42e7dSPeter Dunlap 	iscsi_logout_rsp_hdr_t	*logout_rsp;
2551a6d42e7dSPeter Dunlap 	idm_pdu_t *resp;
2552a6d42e7dSPeter Dunlap 
2553a6d42e7dSPeter Dunlap 	/* Allocate a PDU to respond */
2554a6d42e7dSPeter Dunlap 	resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2555a6d42e7dSPeter Dunlap 	idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
255660220f10SPriya Krishnan 	/*
255760220f10SPriya Krishnan 	 * The StatSN is to be sent to the initiator,
255860220f10SPriya Krishnan 	 * it is not required to increment the number
255960220f10SPriya Krishnan 	 * as the connection is terminating.
256060220f10SPriya Krishnan 	 */
256160220f10SPriya Krishnan 	resp->isp_flags |= IDM_PDU_SET_STATSN;
2562a6d42e7dSPeter Dunlap 	/*
2563a6d42e7dSPeter Dunlap 	 * Logout results in the immediate termination of all tasks except
2564a6d42e7dSPeter Dunlap 	 * if the logout reason is ISCSI_LOGOUT_REASON_RECOVERY.  The
2565a6d42e7dSPeter Dunlap 	 * connection state machine will drive this task cleanup automatically
2566a6d42e7dSPeter Dunlap 	 * so we don't need to handle that here.
2567a6d42e7dSPeter Dunlap 	 */
2568a6d42e7dSPeter Dunlap 	logout_rsp = (iscsi_logout_rsp_hdr_t *)resp->isp_hdr;
2569a6d42e7dSPeter Dunlap 	bzero(logout_rsp, sizeof (*logout_rsp));
2570a6d42e7dSPeter Dunlap 	logout_rsp->opcode = ISCSI_OP_LOGOUT_RSP;
2571a6d42e7dSPeter Dunlap 	logout_rsp->flags = ISCSI_FLAG_FINAL;
2572a6d42e7dSPeter Dunlap 	logout_rsp->itt = logout_req->itt;
2573a6d42e7dSPeter Dunlap 	if ((logout_req->flags & ISCSI_FLAG_LOGOUT_REASON_MASK) >
2574a6d42e7dSPeter Dunlap 	    ISCSI_LOGOUT_REASON_RECOVERY) {
2575a6d42e7dSPeter Dunlap 		logout_rsp->response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2576a6d42e7dSPeter Dunlap 	} else {
2577a6d42e7dSPeter Dunlap 		logout_rsp->response = ISCSI_LOGOUT_SUCCESS;
2578a6d42e7dSPeter Dunlap 	}
2579a6d42e7dSPeter Dunlap 
2580a6d42e7dSPeter Dunlap 	iscsit_pdu_tx(resp);
2581a6d42e7dSPeter Dunlap 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2582a6d42e7dSPeter Dunlap }
2583a6d42e7dSPeter Dunlap 
2584a6d42e7dSPeter Dunlap /*
2585a6d42e7dSPeter Dunlap  * Calculate the number of outstanding commands we can process
2586a6d42e7dSPeter Dunlap  */
2587a6d42e7dSPeter Dunlap int
2588a6d42e7dSPeter Dunlap iscsit_cmd_window()
2589a6d42e7dSPeter Dunlap {
2590*d618d68dSPriya Krishnan 	/*
2591*d618d68dSPriya Krishnan 	 * Instead of using a pre-defined constant for the command window,
2592*d618d68dSPriya Krishnan 	 * it should be made confiurable and dynamic. With MC/S, sequence
2593*d618d68dSPriya Krishnan 	 * numbers will be used up at a much faster rate than with SC/S.
2594*d618d68dSPriya Krishnan 	 */
2595*d618d68dSPriya Krishnan 	return	(ISCSIT_MAX_WINDOW);
2596a6d42e7dSPeter Dunlap }
2597a6d42e7dSPeter Dunlap 
2598a6d42e7dSPeter Dunlap /*
2599a6d42e7dSPeter Dunlap  * Set local registers based on incoming PDU
2600a6d42e7dSPeter Dunlap  */
2601a6d42e7dSPeter Dunlap void
2602a6d42e7dSPeter Dunlap iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2603a6d42e7dSPeter Dunlap {
2604a6d42e7dSPeter Dunlap 	iscsit_sess_t *ist;
2605a6d42e7dSPeter Dunlap 	iscsi_scsi_cmd_hdr_t *req;
2606a6d42e7dSPeter Dunlap 
2607a6d42e7dSPeter Dunlap 	ist = ict->ict_sess;
2608a6d42e7dSPeter Dunlap 
2609a6d42e7dSPeter Dunlap 	req = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2610*d618d68dSPriya Krishnan 	if (req->opcode & ISCSI_OP_IMMEDIATE) {
2611*d618d68dSPriya Krishnan 		/* no cmdsn increment for immediate PDUs */
2612*d618d68dSPriya Krishnan 		return;
2613*d618d68dSPriya Krishnan 	}
2614a6d42e7dSPeter Dunlap 
2615*d618d68dSPriya Krishnan 	/* Ensure that the ExpCmdSN advances in an orderly manner */
2616*d618d68dSPriya Krishnan 	mutex_enter(&ist->ist_sn_mutex);
2617a6d42e7dSPeter Dunlap 	ist->ist_expcmdsn = ntohl(req->cmdsn) + 1;
2618a6d42e7dSPeter Dunlap 	ist->ist_maxcmdsn = ntohl(req->cmdsn) + iscsit_cmd_window();
2619*d618d68dSPriya Krishnan 	mutex_exit(&ist->ist_sn_mutex);
2620a6d42e7dSPeter Dunlap }
2621a6d42e7dSPeter Dunlap 
2622a6d42e7dSPeter Dunlap /*
2623a6d42e7dSPeter Dunlap  * Wrapper funtion, calls iscsi_calc_rspsn and idm_pdu_tx
2624a6d42e7dSPeter Dunlap  */
2625a6d42e7dSPeter Dunlap void
2626a6d42e7dSPeter Dunlap iscsit_pdu_tx(idm_pdu_t *pdu)
2627a6d42e7dSPeter Dunlap {
2628a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = pdu->isp_ic->ic_handle;
262960220f10SPriya Krishnan 	iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
263060220f10SPriya Krishnan 	iscsit_sess_t *ist = ict->ict_sess;
2631a6d42e7dSPeter Dunlap 
2632a6d42e7dSPeter Dunlap 	/*
263360220f10SPriya Krishnan 	 * The command sequence numbers are session-wide and must stay
263460220f10SPriya Krishnan 	 * consistent across the transfer, so protect the cmdsn with a
2635*d618d68dSPriya Krishnan 	 * mutex lock on the session. The status sequence number will
263660220f10SPriya Krishnan 	 * be updated just before the transport layer transmits the PDU.
2637a6d42e7dSPeter Dunlap 	 */
263860220f10SPriya Krishnan 
2639*d618d68dSPriya Krishnan 	mutex_enter(&ict->ict_sess->ist_sn_mutex);
264060220f10SPriya Krishnan 	/* Set ExpCmdSN and MaxCmdSN */
264160220f10SPriya Krishnan 	rsp->maxcmdsn = htonl(ist->ist_maxcmdsn);
264260220f10SPriya Krishnan 	rsp->expcmdsn = htonl(ist->ist_expcmdsn);
2643a6d42e7dSPeter Dunlap 	idm_pdu_tx(pdu);
2644*d618d68dSPriya Krishnan 	mutex_exit(&ict->ict_sess->ist_sn_mutex);
2645a6d42e7dSPeter Dunlap }
2646a6d42e7dSPeter Dunlap 
2647a6d42e7dSPeter Dunlap /*
2648a6d42e7dSPeter Dunlap  * Internal functions
2649a6d42e7dSPeter Dunlap  */
2650a6d42e7dSPeter Dunlap 
2651a6d42e7dSPeter Dunlap void
2652a6d42e7dSPeter Dunlap iscsit_send_async_event(iscsit_conn_t *ict, uint8_t event)
2653a6d42e7dSPeter Dunlap {
2654a6d42e7dSPeter Dunlap 	idm_pdu_t		*abt;
2655a6d42e7dSPeter Dunlap 	iscsi_async_evt_hdr_t	*async_abt;
2656a6d42e7dSPeter Dunlap 
2657a6d42e7dSPeter Dunlap 	/*
2658a6d42e7dSPeter Dunlap 	 * Get a PDU to build the abort request.
2659a6d42e7dSPeter Dunlap 	 */
2660a6d42e7dSPeter Dunlap 	abt = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2661a6d42e7dSPeter Dunlap 	if (abt == NULL) {
2662a6d42e7dSPeter Dunlap 		idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL);
2663a6d42e7dSPeter Dunlap 		return;
2664a6d42e7dSPeter Dunlap 	}
2665a6d42e7dSPeter Dunlap 
266660220f10SPriya Krishnan 	/*
266760220f10SPriya Krishnan 	 * A asynchronous message is sent by the target to request a logout.
266860220f10SPriya Krishnan 	 * The StatSN for the connection is advanced after the PDU is sent
266960220f10SPriya Krishnan 	 * to allow for initiator and target state synchronization.
267060220f10SPriya Krishnan 	 */
267172cf3143Speter dunlap 	idm_pdu_init(abt, ict->ict_ic, NULL, NULL);
2672a6d42e7dSPeter Dunlap 	abt->isp_datalen = 0;
267360220f10SPriya Krishnan 	abt->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2674a6d42e7dSPeter Dunlap 
2675a6d42e7dSPeter Dunlap 	async_abt = (iscsi_async_evt_hdr_t *)abt->isp_hdr;
2676a6d42e7dSPeter Dunlap 	bzero(async_abt, sizeof (*async_abt));
2677a6d42e7dSPeter Dunlap 	async_abt->opcode = ISCSI_OP_ASYNC_EVENT;
2678a6d42e7dSPeter Dunlap 	async_abt->async_event = event;
2679a6d42e7dSPeter Dunlap 	async_abt->flags = ISCSI_FLAG_FINAL;
2680a6d42e7dSPeter Dunlap 	async_abt->rsvd4[0] = 0xff;
2681a6d42e7dSPeter Dunlap 	async_abt->rsvd4[1] = 0xff;
2682a6d42e7dSPeter Dunlap 	async_abt->rsvd4[2] = 0xff;
2683a6d42e7dSPeter Dunlap 	async_abt->rsvd4[3] = 0xff;
2684a6d42e7dSPeter Dunlap 
2685a6d42e7dSPeter Dunlap 	switch (event) {
2686a6d42e7dSPeter Dunlap 	case ISCSI_ASYNC_EVENT_REQUEST_LOGOUT:
2687a6d42e7dSPeter Dunlap 		async_abt->param3 = htons(IDM_LOGOUT_SECONDS);
2688a6d42e7dSPeter Dunlap 		break;
2689a6d42e7dSPeter Dunlap 	case ISCSI_ASYNC_EVENT_SCSI_EVENT:
2690a6d42e7dSPeter Dunlap 	case ISCSI_ASYNC_EVENT_DROPPING_CONNECTION:
2691a6d42e7dSPeter Dunlap 	case ISCSI_ASYNC_EVENT_DROPPING_ALL_CONNECTIONS:
2692a6d42e7dSPeter Dunlap 	case ISCSI_ASYNC_EVENT_PARAM_NEGOTIATION:
2693a6d42e7dSPeter Dunlap 	default:
2694a6d42e7dSPeter Dunlap 		ASSERT(0);
2695a6d42e7dSPeter Dunlap 	}
2696a6d42e7dSPeter Dunlap 
2697a6d42e7dSPeter Dunlap 	iscsit_pdu_tx(abt);
2698a6d42e7dSPeter Dunlap }
2699a6d42e7dSPeter Dunlap 
270072cf3143Speter dunlap void
270172cf3143Speter dunlap iscsit_send_reject(iscsit_conn_t *ict, idm_pdu_t *rejected_pdu, uint8_t reason)
270272cf3143Speter dunlap {
270372cf3143Speter dunlap 	idm_pdu_t		*reject_pdu;
270472cf3143Speter dunlap 	iscsi_reject_rsp_hdr_t	*reject;
270572cf3143Speter dunlap 
270672cf3143Speter dunlap 	/*
270772cf3143Speter dunlap 	 * Get a PDU to build the abort request.
270872cf3143Speter dunlap 	 */
270972cf3143Speter dunlap 	reject_pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t),
271072cf3143Speter dunlap 	    rejected_pdu->isp_hdrlen);
271172cf3143Speter dunlap 	if (reject_pdu == NULL) {
271272cf3143Speter dunlap 		idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL);
271372cf3143Speter dunlap 		return;
271472cf3143Speter dunlap 	}
271572cf3143Speter dunlap 	idm_pdu_init(reject_pdu, ict->ict_ic, NULL, NULL);
271660220f10SPriya Krishnan 	/* StatSN is advanced after a Reject PDU */
271760220f10SPriya Krishnan 	reject_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
271872cf3143Speter dunlap 	reject_pdu->isp_datalen = rejected_pdu->isp_hdrlen;
271972cf3143Speter dunlap 	bcopy(rejected_pdu->isp_hdr, reject_pdu->isp_data,
272072cf3143Speter dunlap 	    rejected_pdu->isp_hdrlen);
272172cf3143Speter dunlap 
272272cf3143Speter dunlap 	reject = (iscsi_reject_rsp_hdr_t *)reject_pdu->isp_hdr;
272372cf3143Speter dunlap 	bzero(reject, sizeof (*reject));
272472cf3143Speter dunlap 	reject->opcode = ISCSI_OP_REJECT_MSG;
272572cf3143Speter dunlap 	reject->reason = reason;
272672cf3143Speter dunlap 	reject->flags = ISCSI_FLAG_FINAL;
272772cf3143Speter dunlap 	hton24(reject->dlength, rejected_pdu->isp_hdrlen);
272872cf3143Speter dunlap 	reject->must_be_ff[0] = 0xff;
272972cf3143Speter dunlap 	reject->must_be_ff[1] = 0xff;
273072cf3143Speter dunlap 	reject->must_be_ff[2] = 0xff;
273172cf3143Speter dunlap 	reject->must_be_ff[3] = 0xff;
273272cf3143Speter dunlap 
273372cf3143Speter dunlap 	iscsit_pdu_tx(reject_pdu);
273472cf3143Speter dunlap }
273572cf3143Speter dunlap 
2736a6d42e7dSPeter Dunlap 
2737a6d42e7dSPeter Dunlap static iscsit_task_t *
2738a6d42e7dSPeter Dunlap iscsit_task_alloc(iscsit_conn_t *ict)
2739a6d42e7dSPeter Dunlap {
2740a6d42e7dSPeter Dunlap 	iscsit_task_t *itask;
2741a6d42e7dSPeter Dunlap 	iscsit_buf_t *immed_ibuf;
2742a6d42e7dSPeter Dunlap 
2743a6d42e7dSPeter Dunlap 	/*
2744a6d42e7dSPeter Dunlap 	 * Possible items to pre-alloc if we cache iscsit_task_t's:
2745a6d42e7dSPeter Dunlap 	 *
2746a6d42e7dSPeter Dunlap 	 * Status PDU w/ sense buffer
2747a6d42e7dSPeter Dunlap 	 * stmf_data_buf_t for immediate data
2748a6d42e7dSPeter Dunlap 	 */
2749a6d42e7dSPeter Dunlap 	itask = kmem_alloc(sizeof (iscsit_task_t) + sizeof (iscsit_buf_t) +
2750a6d42e7dSPeter Dunlap 	    sizeof (stmf_data_buf_t), KM_NOSLEEP);
2751a6d42e7dSPeter Dunlap 	if (itask != NULL) {
2752a6d42e7dSPeter Dunlap 		mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2753a6d42e7dSPeter Dunlap 		itask->it_aborted = itask->it_stmf_abort =
2754a6d42e7dSPeter Dunlap 		    itask->it_tm_task = 0;
2755a6d42e7dSPeter Dunlap 
2756a6d42e7dSPeter Dunlap 		immed_ibuf = (iscsit_buf_t *)(itask + 1);
2757a6d42e7dSPeter Dunlap 		bzero(immed_ibuf, sizeof (*immed_ibuf));
2758a6d42e7dSPeter Dunlap 		immed_ibuf->ibuf_is_immed = B_TRUE;
2759a6d42e7dSPeter Dunlap 		immed_ibuf->ibuf_stmf_buf = (stmf_data_buf_t *)(immed_ibuf + 1);
2760a6d42e7dSPeter Dunlap 
2761a6d42e7dSPeter Dunlap 		bzero(immed_ibuf->ibuf_stmf_buf, sizeof (stmf_data_buf_t));
2762a6d42e7dSPeter Dunlap 		immed_ibuf->ibuf_stmf_buf->db_port_private = immed_ibuf;
2763a6d42e7dSPeter Dunlap 		immed_ibuf->ibuf_stmf_buf->db_sglist_length = 1;
2764a6d42e7dSPeter Dunlap 		immed_ibuf->ibuf_stmf_buf->db_flags = DB_DIRECTION_FROM_RPORT |
2765a6d42e7dSPeter Dunlap 		    DB_DONT_CACHE;
2766a6d42e7dSPeter Dunlap 		itask->it_immed_data = immed_ibuf;
2767a6d42e7dSPeter Dunlap 		itask->it_idm_task = idm_task_alloc(ict->ict_ic);
2768a6d42e7dSPeter Dunlap 		if (itask->it_idm_task != NULL) {
2769a6d42e7dSPeter Dunlap 			itask->it_idm_task->idt_private = itask;
2770a6d42e7dSPeter Dunlap 			itask->it_ict = ict;
2771a6d42e7dSPeter Dunlap 			itask->it_ttt = itask->it_idm_task->idt_tt;
2772a6d42e7dSPeter Dunlap 			return (itask);
2773a6d42e7dSPeter Dunlap 		} else {
2774a6d42e7dSPeter Dunlap 			kmem_free(itask, sizeof (iscsit_task_t) +
2775a6d42e7dSPeter Dunlap 			    sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2776a6d42e7dSPeter Dunlap 		}
2777a6d42e7dSPeter Dunlap 	}
2778a6d42e7dSPeter Dunlap 
2779a6d42e7dSPeter Dunlap 	return (NULL);
2780a6d42e7dSPeter Dunlap }
2781a6d42e7dSPeter Dunlap 
2782a6d42e7dSPeter Dunlap static void
2783a6d42e7dSPeter Dunlap iscsit_task_free(iscsit_task_t *itask)
2784a6d42e7dSPeter Dunlap {
2785a6d42e7dSPeter Dunlap 	idm_task_free(itask->it_idm_task);
2786a6d42e7dSPeter Dunlap 	mutex_destroy(&itask->it_mutex);
2787a6d42e7dSPeter Dunlap 	kmem_free(itask, sizeof (iscsit_task_t) +
2788a6d42e7dSPeter Dunlap 	    sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2789a6d42e7dSPeter Dunlap }
2790a6d42e7dSPeter Dunlap 
2791a6d42e7dSPeter Dunlap static iscsit_task_t *
2792a6d42e7dSPeter Dunlap iscsit_tm_task_alloc(iscsit_conn_t *ict)
2793a6d42e7dSPeter Dunlap {
2794a6d42e7dSPeter Dunlap 	iscsit_task_t *itask;
2795a6d42e7dSPeter Dunlap 
2796a6d42e7dSPeter Dunlap 	itask = kmem_zalloc(sizeof (iscsit_task_t), KM_NOSLEEP);
2797a6d42e7dSPeter Dunlap 	if (itask != NULL) {
2798a6d42e7dSPeter Dunlap 		idm_conn_hold(ict->ict_ic);
2799a6d42e7dSPeter Dunlap 		mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2800a6d42e7dSPeter Dunlap 		itask->it_aborted = itask->it_stmf_abort =
2801a6d42e7dSPeter Dunlap 		    itask->it_tm_responded = 0;
2802a6d42e7dSPeter Dunlap 		itask->it_tm_pdu = NULL;
2803a6d42e7dSPeter Dunlap 		itask->it_tm_task = 1;
2804a6d42e7dSPeter Dunlap 		itask->it_ict = ict;
2805a6d42e7dSPeter Dunlap 	}
2806a6d42e7dSPeter Dunlap 
2807a6d42e7dSPeter Dunlap 	return (itask);
2808a6d42e7dSPeter Dunlap }
2809a6d42e7dSPeter Dunlap 
2810a6d42e7dSPeter Dunlap static void
2811a6d42e7dSPeter Dunlap iscsit_tm_task_free(iscsit_task_t *itask)
2812a6d42e7dSPeter Dunlap {
2813a6d42e7dSPeter Dunlap 	/*
2814a6d42e7dSPeter Dunlap 	 * If we responded then the call to idm_pdu_complete will free the
2815a6d42e7dSPeter Dunlap 	 * PDU.  Otherwise we got aborted before the TM function could
2816a6d42e7dSPeter Dunlap 	 * complete and we need to free the PDU explicitly.
2817a6d42e7dSPeter Dunlap 	 */
2818a6d42e7dSPeter Dunlap 	if (itask->it_tm_pdu != NULL && !itask->it_tm_responded)
2819a6d42e7dSPeter Dunlap 		idm_pdu_free(itask->it_tm_pdu);
2820a6d42e7dSPeter Dunlap 	idm_conn_rele(itask->it_ict->ict_ic);
2821a6d42e7dSPeter Dunlap 	mutex_destroy(&itask->it_mutex);
2822a6d42e7dSPeter Dunlap 	kmem_free(itask, sizeof (iscsit_task_t));
2823a6d42e7dSPeter Dunlap }
2824a6d42e7dSPeter Dunlap 
282572cf3143Speter dunlap static idm_status_t
282672cf3143Speter dunlap iscsit_task_start(iscsit_task_t *itask)
282772cf3143Speter dunlap {
282872cf3143Speter dunlap 	iscsit_sess_t *ist = itask->it_ict->ict_sess;
282972cf3143Speter dunlap 	avl_index_t		where;
283072cf3143Speter dunlap 
283172cf3143Speter dunlap 	/*
283272cf3143Speter dunlap 	 * Sanity check the ITT and ensure that this task does not already
283372cf3143Speter dunlap 	 * exist.  If not then add the task to the session task list.
283472cf3143Speter dunlap 	 */
283572cf3143Speter dunlap 	mutex_enter(&ist->ist_mutex);
283672cf3143Speter dunlap 	mutex_enter(&itask->it_mutex);
283772cf3143Speter dunlap 	itask->it_active = 1;
283872cf3143Speter dunlap 	if (avl_find(&ist->ist_task_list, itask, &where) == NULL) {
283972cf3143Speter dunlap 		/* New task, add to AVL */
284072cf3143Speter dunlap 		avl_insert(&ist->ist_task_list, itask, where);
284172cf3143Speter dunlap 		mutex_exit(&itask->it_mutex);
284272cf3143Speter dunlap 		mutex_exit(&ist->ist_mutex);
284372cf3143Speter dunlap 		return (IDM_STATUS_SUCCESS);
284472cf3143Speter dunlap 	}
284572cf3143Speter dunlap 	mutex_exit(&itask->it_mutex);
284672cf3143Speter dunlap 	mutex_exit(&ist->ist_mutex);
284772cf3143Speter dunlap 
284872cf3143Speter dunlap 	return (IDM_STATUS_REJECT);
284972cf3143Speter dunlap }
285072cf3143Speter dunlap 
285172cf3143Speter dunlap static void
285272cf3143Speter dunlap iscsit_task_done(iscsit_task_t *itask)
285372cf3143Speter dunlap {
285472cf3143Speter dunlap 	iscsit_sess_t *ist = itask->it_ict->ict_sess;
285572cf3143Speter dunlap 
285672cf3143Speter dunlap 	mutex_enter(&ist->ist_mutex);
285772cf3143Speter dunlap 	mutex_enter(&itask->it_mutex);
285872cf3143Speter dunlap 	if (itask->it_active) {
285972cf3143Speter dunlap 		avl_remove(&ist->ist_task_list, itask);
286072cf3143Speter dunlap 		itask->it_active = 0;
286172cf3143Speter dunlap 	}
286272cf3143Speter dunlap 	mutex_exit(&itask->it_mutex);
286372cf3143Speter dunlap 	mutex_exit(&ist->ist_mutex);
286472cf3143Speter dunlap }
286572cf3143Speter dunlap 
2866a6d42e7dSPeter Dunlap /*
2867a6d42e7dSPeter Dunlap  * iscsit status PDU cache
2868a6d42e7dSPeter Dunlap  */
2869a6d42e7dSPeter Dunlap 
2870a6d42e7dSPeter Dunlap /*ARGSUSED*/
2871a6d42e7dSPeter Dunlap static int
2872a6d42e7dSPeter Dunlap iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags)
2873a6d42e7dSPeter Dunlap {
2874a6d42e7dSPeter Dunlap 	idm_pdu_t *pdu = pdu_void;
2875a6d42e7dSPeter Dunlap 	iscsi_scsi_rsp_hdr_t *rsp;
2876a6d42e7dSPeter Dunlap 
2877a6d42e7dSPeter Dunlap 	bzero(pdu, sizeof (idm_pdu_t));
2878a6d42e7dSPeter Dunlap 	pdu->isp_callback = iscsit_send_good_status_done;
2879a6d42e7dSPeter Dunlap 	pdu->isp_magic = IDM_PDU_MAGIC;
2880a6d42e7dSPeter Dunlap 	pdu->isp_hdr = (iscsi_hdr_t *)(pdu + 1); /* Ptr arithmetic */
2881a6d42e7dSPeter Dunlap 	pdu->isp_hdrlen = sizeof (iscsi_hdr_t);
2882a6d42e7dSPeter Dunlap 
2883a6d42e7dSPeter Dunlap 	/* Setup status response */
2884a6d42e7dSPeter Dunlap 	rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
2885a6d42e7dSPeter Dunlap 	bzero(rsp, sizeof (*rsp));
2886a6d42e7dSPeter Dunlap 	rsp->opcode = ISCSI_OP_SCSI_RSP;
2887a6d42e7dSPeter Dunlap 	rsp->flags = ISCSI_FLAG_FINAL;
2888a6d42e7dSPeter Dunlap 	rsp->response = ISCSI_STATUS_CMD_COMPLETED;
2889a6d42e7dSPeter Dunlap 
2890a6d42e7dSPeter Dunlap 	return (0);
2891a6d42e7dSPeter Dunlap }
2892a6d42e7dSPeter Dunlap 
2893a6d42e7dSPeter Dunlap /*
2894a6d42e7dSPeter Dunlap  * iscsit private data handler
2895a6d42e7dSPeter Dunlap  */
2896a6d42e7dSPeter Dunlap 
2897a6d42e7dSPeter Dunlap /*ARGSUSED*/
2898a6d42e7dSPeter Dunlap static void
2899a6d42e7dSPeter Dunlap iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags)
2900a6d42e7dSPeter Dunlap {
2901a6d42e7dSPeter Dunlap 	it_config_t		*cfg;
2902a6d42e7dSPeter Dunlap 	nvlist_t		*nvl;
29038c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	iscsit_service_enabled_t	old_state;
2904a6d42e7dSPeter Dunlap 
2905a6d42e7dSPeter Dunlap 	if ((cmd != STMF_PROVIDER_DATA_UPDATED) || (arg == NULL)) {
2906a6d42e7dSPeter Dunlap 		return;
2907a6d42e7dSPeter Dunlap 	}
2908a6d42e7dSPeter Dunlap 
2909a6d42e7dSPeter Dunlap 	nvl = (nvlist_t *)arg;
2910a6d42e7dSPeter Dunlap 
2911a6d42e7dSPeter Dunlap 	/* Translate nvlist */
2912a6d42e7dSPeter Dunlap 	if (it_nv_to_config(nvl, &cfg) != 0) {
2913a6d42e7dSPeter Dunlap 		cmn_err(CE_WARN, "Configuration is invalid");
2914a6d42e7dSPeter Dunlap 		return;
2915a6d42e7dSPeter Dunlap 	}
2916a6d42e7dSPeter Dunlap 
29178c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	/* Check that no iSCSI ioctl is currently running */
29188c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_enter(&iscsit_global.global_state_mutex);
29198c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	old_state = iscsit_global.global_svc_state;
29208c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	switch (iscsit_global.global_svc_state) {
29218c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	case ISE_ENABLED:
29228c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	case ISE_DISABLED:
29238c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		iscsit_global.global_svc_state = ISE_BUSY;
29248c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		break;
29258c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	case ISE_ENABLING:
29268c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		/*
29278c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		 * It is OK for the iscsit_pp_cb to be called from inside of
29288c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		 * an iSCSI ioctl only if we are currently executing inside
29298c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		 * of stmf_register_port_provider.
29308c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		 */
29318c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		ASSERT((flags & STMF_PCB_PREG_COMPLETE) != 0);
29328c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		break;
29338c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	default:
29348c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		cmn_err(CE_WARN, "iscsit_pp_cb called when global_svc_state"
29358c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		    " is not ENABLED(0x%x) -- ignoring",
29368c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		    iscsit_global.global_svc_state);
29378c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
29388c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		it_config_free_cmn(cfg);
29398c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		return;
29408c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	}
29418c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_exit(&iscsit_global.global_state_mutex);
29428c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
2943a6d42e7dSPeter Dunlap 	/* Update config */
2944a6d42e7dSPeter Dunlap 	(void) iscsit_config_merge(cfg);
2945a6d42e7dSPeter Dunlap 
2946a6d42e7dSPeter Dunlap 	it_config_free_cmn(cfg);
29478c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
29488c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	/* Restore old iSCSI driver global state */
29498c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_enter(&iscsit_global.global_state_mutex);
29508c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	ASSERT(iscsit_global.global_svc_state == ISE_BUSY ||
29518c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	    iscsit_global.global_svc_state == ISE_ENABLING);
29528c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	iscsit_global.global_svc_state = old_state;
29538c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_exit(&iscsit_global.global_state_mutex);
2954a6d42e7dSPeter Dunlap }
2955a6d42e7dSPeter Dunlap 
2956a6d42e7dSPeter Dunlap 
2957a6d42e7dSPeter Dunlap static it_cfg_status_t
2958a6d42e7dSPeter Dunlap iscsit_config_merge(it_config_t *in_cfg)
2959a6d42e7dSPeter Dunlap {
2960a6d42e7dSPeter Dunlap 	it_cfg_status_t	status;
2961a6d42e7dSPeter Dunlap 	it_config_t	*cfg;
2962a6d42e7dSPeter Dunlap 	it_config_t	tmp_cfg;
2963a6d42e7dSPeter Dunlap 	list_t		tpg_del_list;
2964a6d42e7dSPeter Dunlap 
2965a6d42e7dSPeter Dunlap 	if (in_cfg) {
2966a6d42e7dSPeter Dunlap 		cfg = in_cfg;
2967a6d42e7dSPeter Dunlap 	} else {
2968a6d42e7dSPeter Dunlap 		/* Make empty config */
2969a6d42e7dSPeter Dunlap 		bzero(&tmp_cfg, sizeof (tmp_cfg));
2970a6d42e7dSPeter Dunlap 		cfg = &tmp_cfg;
2971a6d42e7dSPeter Dunlap 	}
2972a6d42e7dSPeter Dunlap 
2973a6d42e7dSPeter Dunlap 	list_create(&tpg_del_list,  sizeof (iscsit_tpg_t),
2974a6d42e7dSPeter Dunlap 	    offsetof(iscsit_tpg_t, tpg_delete_ln));
2975a6d42e7dSPeter Dunlap 
2976a6d42e7dSPeter Dunlap 	/*
2977a6d42e7dSPeter Dunlap 	 * Update targets, initiator contexts, target portal groups,
2978a6d42e7dSPeter Dunlap 	 * and iSNS client
2979a6d42e7dSPeter Dunlap 	 */
2980a6d42e7dSPeter Dunlap 	ISCSIT_GLOBAL_LOCK(RW_WRITER);
2981a6d42e7dSPeter Dunlap 	if (((status = iscsit_config_merge_tpg(cfg, &tpg_del_list))
2982a6d42e7dSPeter Dunlap 	    != 0) ||
2983a6d42e7dSPeter Dunlap 	    ((status = iscsit_config_merge_tgt(cfg)) != 0) ||
2984a6d42e7dSPeter Dunlap 	    ((status = iscsit_config_merge_ini(cfg)) != 0) ||
2985a6d42e7dSPeter Dunlap 	    ((status = isnst_config_merge(cfg)) != 0)) {
2986a6d42e7dSPeter Dunlap 		ISCSIT_GLOBAL_UNLOCK();
2987a6d42e7dSPeter Dunlap 		return (status);
2988a6d42e7dSPeter Dunlap 	}
2989a6d42e7dSPeter Dunlap 
2990a6d42e7dSPeter Dunlap 	/* Update other global config parameters */
2991a6d42e7dSPeter Dunlap 	if (iscsit_global.global_props) {
2992a6d42e7dSPeter Dunlap 		nvlist_free(iscsit_global.global_props);
2993a6d42e7dSPeter Dunlap 		iscsit_global.global_props = NULL;
2994a6d42e7dSPeter Dunlap 	}
2995a6d42e7dSPeter Dunlap 	if (in_cfg) {
2996a6d42e7dSPeter Dunlap 		(void) nvlist_dup(cfg->config_global_properties,
2997a6d42e7dSPeter Dunlap 		    &iscsit_global.global_props, KM_SLEEP);
2998a6d42e7dSPeter Dunlap 	}
2999a6d42e7dSPeter Dunlap 	ISCSIT_GLOBAL_UNLOCK();
3000a6d42e7dSPeter Dunlap 
3001a6d42e7dSPeter Dunlap 	iscsit_config_destroy_tpgs(&tpg_del_list);
3002a6d42e7dSPeter Dunlap 
3003a6d42e7dSPeter Dunlap 	list_destroy(&tpg_del_list);
3004a6d42e7dSPeter Dunlap 
3005a6d42e7dSPeter Dunlap 	return (ITCFG_SUCCESS);
3006a6d42e7dSPeter Dunlap }
3007a6d42e7dSPeter Dunlap 
3008a6d42e7dSPeter Dunlap /*
3009a6d42e7dSPeter Dunlap  * iscsit_sna_lt[e]
3010a6d42e7dSPeter Dunlap  *
3011a6d42e7dSPeter Dunlap  * Compare serial numbers using serial number arithmetic as defined in
3012a6d42e7dSPeter Dunlap  * RFC 1982.
3013a6d42e7dSPeter Dunlap  *
3014ab003da8SJim Dunham  * NOTE: This code is duplicated in the isns server. It ought to be common.
3015a6d42e7dSPeter Dunlap  */
3016a6d42e7dSPeter Dunlap 
3017a6d42e7dSPeter Dunlap static int
3018a6d42e7dSPeter Dunlap iscsit_sna_lt(uint32_t sn1, uint32_t sn2)
3019a6d42e7dSPeter Dunlap {
3020a6d42e7dSPeter Dunlap 	return ((sn1 != sn2) &&
3021a6d42e7dSPeter Dunlap 	    (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
3022a6d42e7dSPeter Dunlap 	    ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
3023a6d42e7dSPeter Dunlap }
3024a6d42e7dSPeter Dunlap 
3025a6d42e7dSPeter Dunlap static int
3026a6d42e7dSPeter Dunlap iscsit_sna_lte(uint32_t sn1, uint32_t sn2)
3027a6d42e7dSPeter Dunlap {
3028a6d42e7dSPeter Dunlap 	return ((sn1 == sn2) ||
3029a6d42e7dSPeter Dunlap 	    (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
3030a6d42e7dSPeter Dunlap 	    ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
3031a6d42e7dSPeter Dunlap }
3032a6d42e7dSPeter Dunlap 
3033a6d42e7dSPeter Dunlap 
3034a6d42e7dSPeter Dunlap static boolean_t
3035a6d42e7dSPeter Dunlap iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn)
3036a6d42e7dSPeter Dunlap {
3037a6d42e7dSPeter Dunlap 	iscsit_sess_t	*ist = ict->ict_sess;
3038a6d42e7dSPeter Dunlap 	int		rval = B_TRUE;
3039a6d42e7dSPeter Dunlap 
3040a6d42e7dSPeter Dunlap 	ist = ict->ict_sess;
3041a6d42e7dSPeter Dunlap 
3042*d618d68dSPriya Krishnan 	mutex_enter(&ist->ist_sn_mutex);
3043a6d42e7dSPeter Dunlap 
3044a6d42e7dSPeter Dunlap 	/*
3045a6d42e7dSPeter Dunlap 	 * If cmdsn is less than ist_expcmdsn - iscsit_cmd_window() or
3046a6d42e7dSPeter Dunlap 	 * greater than ist_expcmdsn, it's not in the window.
3047a6d42e7dSPeter Dunlap 	 */
3048a6d42e7dSPeter Dunlap 
3049a6d42e7dSPeter Dunlap 	if (iscsit_sna_lt(cmdsn, (ist->ist_expcmdsn - iscsit_cmd_window())) ||
3050a6d42e7dSPeter Dunlap 	    !iscsit_sna_lte(cmdsn, ist->ist_expcmdsn)) {
3051a6d42e7dSPeter Dunlap 		rval = B_FALSE;
3052a6d42e7dSPeter Dunlap 	}
3053a6d42e7dSPeter Dunlap 
3054*d618d68dSPriya Krishnan 	mutex_exit(&ist->ist_sn_mutex);
3055a6d42e7dSPeter Dunlap 
3056a6d42e7dSPeter Dunlap 	return (rval);
3057a6d42e7dSPeter Dunlap }
3058*d618d68dSPriya Krishnan 
3059*d618d68dSPriya Krishnan /*
3060*d618d68dSPriya Krishnan  * iscsit_check_cmdsn_and_queue
3061*d618d68dSPriya Krishnan  *
3062*d618d68dSPriya Krishnan  * Independent of the order in which the iSCSI target receives non-immediate
3063*d618d68dSPriya Krishnan  * command PDU across the entire session and any multiple connections within
3064*d618d68dSPriya Krishnan  * the session, the target must deliver the commands to the SCSI layer in
3065*d618d68dSPriya Krishnan  * CmdSN order. So out-of-order non-immediate commands are queued up on a
3066*d618d68dSPriya Krishnan  * session-wide wait queue. Duplicate commands are ignored.
3067*d618d68dSPriya Krishnan  *
3068*d618d68dSPriya Krishnan  */
3069*d618d68dSPriya Krishnan static int
3070*d618d68dSPriya Krishnan iscsit_check_cmdsn_and_queue(idm_pdu_t *rx_pdu)
3071*d618d68dSPriya Krishnan {
3072*d618d68dSPriya Krishnan 	idm_conn_t		*ic = rx_pdu->isp_ic;
3073*d618d68dSPriya Krishnan 	iscsit_conn_t		*ict = ic->ic_handle;
3074*d618d68dSPriya Krishnan 	iscsit_sess_t		*ist = ict->ict_sess;
3075*d618d68dSPriya Krishnan 	iscsi_scsi_cmd_hdr_t	*hdr = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
3076*d618d68dSPriya Krishnan 
3077*d618d68dSPriya Krishnan 	mutex_enter(&ist->ist_sn_mutex);
3078*d618d68dSPriya Krishnan 	if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
3079*d618d68dSPriya Krishnan 		/* do not queue, handle it immediately */
3080*d618d68dSPriya Krishnan 		DTRACE_PROBE2(immediate__cmd, iscsit_sess_t *, ist,
3081*d618d68dSPriya Krishnan 		    idm_pdu_t *, rx_pdu);
3082*d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
3083*d618d68dSPriya Krishnan 		return (ISCSIT_CMDSN_EQ_EXPCMDSN);
3084*d618d68dSPriya Krishnan 	}
3085*d618d68dSPriya Krishnan 	if (iscsit_sna_lt(ist->ist_expcmdsn, ntohl(hdr->cmdsn))) {
3086*d618d68dSPriya Krishnan 		/*
3087*d618d68dSPriya Krishnan 		 * Out-of-order commands (cmdSN higher than ExpCmdSN)
3088*d618d68dSPriya Krishnan 		 * are staged on a fixed-size circular buffer until
3089*d618d68dSPriya Krishnan 		 * the missing command is delivered to the SCSI layer.
3090*d618d68dSPriya Krishnan 		 * Irrespective of the order of insertion into the
3091*d618d68dSPriya Krishnan 		 * staging queue, the commands are processed out of the
3092*d618d68dSPriya Krishnan 		 * queue in cmdSN order only.
3093*d618d68dSPriya Krishnan 		 */
3094*d618d68dSPriya Krishnan 		rx_pdu->isp_queue_time = ddi_get_time();
3095*d618d68dSPriya Krishnan 		iscsit_add_pdu_to_queue(ist, rx_pdu);
3096*d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
3097*d618d68dSPriya Krishnan 		return (ISCSIT_CMDSN_GT_EXPCMDSN);
3098*d618d68dSPriya Krishnan 	} else if (iscsit_sna_lt(ntohl(hdr->cmdsn), ist->ist_expcmdsn)) {
3099*d618d68dSPriya Krishnan 		DTRACE_PROBE3(cmdsn__lt__expcmdsn, iscsit_sess_t *, ist,
3100*d618d68dSPriya Krishnan 		    iscsit_conn_t *, ict, idm_pdu_t *, rx_pdu);
3101*d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
3102*d618d68dSPriya Krishnan 		return (ISCSIT_CMDSN_LT_EXPCMDSN);
3103*d618d68dSPriya Krishnan 	} else {
3104*d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
3105*d618d68dSPriya Krishnan 		return (ISCSIT_CMDSN_EQ_EXPCMDSN);
3106*d618d68dSPriya Krishnan 	}
3107*d618d68dSPriya Krishnan }
3108*d618d68dSPriya Krishnan 
3109*d618d68dSPriya Krishnan /*
3110*d618d68dSPriya Krishnan  * iscsit_add_pdu_to_queue() adds PDUs into the array indexed by
3111*d618d68dSPriya Krishnan  * their cmdsn value. The length of the array is kept above the
3112*d618d68dSPriya Krishnan  * maximum window size. The window keeps the cmdsn within a range
3113*d618d68dSPriya Krishnan  * such that there are no collisons. e.g. the assumption is that
3114*d618d68dSPriya Krishnan  * the windowing checks make it impossible to receive PDUs that
3115*d618d68dSPriya Krishnan  * index into the same location in the array.
3116*d618d68dSPriya Krishnan  */
3117*d618d68dSPriya Krishnan static void
3118*d618d68dSPriya Krishnan iscsit_add_pdu_to_queue(iscsit_sess_t *ist, idm_pdu_t *rx_pdu)
3119*d618d68dSPriya Krishnan {
3120*d618d68dSPriya Krishnan 	iscsit_cbuf_t	*cbuf	= ist->ist_rxpdu_queue;
3121*d618d68dSPriya Krishnan 	iscsit_conn_t	*ict 	= rx_pdu->isp_ic->ic_handle;
3122*d618d68dSPriya Krishnan 	uint32_t	cmdsn	=
3123*d618d68dSPriya Krishnan 	    ((iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr)->cmdsn;
3124*d618d68dSPriya Krishnan 	uint32_t	index;
3125*d618d68dSPriya Krishnan 
3126*d618d68dSPriya Krishnan 	ASSERT(MUTEX_HELD(&ist->ist_sn_mutex));
3127*d618d68dSPriya Krishnan 	/*
3128*d618d68dSPriya Krishnan 	 * If the connection is being torn down, then
3129*d618d68dSPriya Krishnan 	 * don't add the PDU to the staging queue
3130*d618d68dSPriya Krishnan 	 */
3131*d618d68dSPriya Krishnan 	mutex_enter(&ict->ict_mutex);
3132*d618d68dSPriya Krishnan 	if (ict->ict_lost) {
3133*d618d68dSPriya Krishnan 		mutex_exit(&ict->ict_mutex);
3134*d618d68dSPriya Krishnan 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
3135*d618d68dSPriya Krishnan 		return;
3136*d618d68dSPriya Krishnan 	}
3137*d618d68dSPriya Krishnan 	iscsit_conn_dispatch_hold(ict);
3138*d618d68dSPriya Krishnan 	mutex_exit(&ict->ict_mutex);
3139*d618d68dSPriya Krishnan 
3140*d618d68dSPriya Krishnan 	index = ntohl(cmdsn) % ISCSIT_RXPDU_QUEUE_LEN;
3141*d618d68dSPriya Krishnan 	ASSERT(cbuf->cb_buffer[index] == NULL);
3142*d618d68dSPriya Krishnan 	cbuf->cb_buffer[index] = rx_pdu;
3143*d618d68dSPriya Krishnan 	cbuf->cb_num_elems++;
3144*d618d68dSPriya Krishnan }
3145*d618d68dSPriya Krishnan 
3146*d618d68dSPriya Krishnan static idm_pdu_t *
3147*d618d68dSPriya Krishnan iscsit_remove_pdu_from_queue(iscsit_sess_t *ist, uint32_t cmdsn)
3148*d618d68dSPriya Krishnan {
3149*d618d68dSPriya Krishnan 	iscsit_cbuf_t	*cbuf	= ist->ist_rxpdu_queue;
3150*d618d68dSPriya Krishnan 	idm_pdu_t	*pdu	= NULL;
3151*d618d68dSPriya Krishnan 	uint32_t	index;
3152*d618d68dSPriya Krishnan 
3153*d618d68dSPriya Krishnan 	ASSERT(MUTEX_HELD(&ist->ist_sn_mutex));
3154*d618d68dSPriya Krishnan 	index = cmdsn % ISCSIT_RXPDU_QUEUE_LEN;
3155*d618d68dSPriya Krishnan 	if ((pdu = cbuf->cb_buffer[index]) != NULL) {
3156*d618d68dSPriya Krishnan 		ASSERT(cmdsn ==
3157*d618d68dSPriya Krishnan 		    ntohl(((iscsi_scsi_cmd_hdr_t *)pdu->isp_hdr)->cmdsn));
3158*d618d68dSPriya Krishnan 		cbuf->cb_buffer[index] = NULL;
3159*d618d68dSPriya Krishnan 		cbuf->cb_num_elems--;
3160*d618d68dSPriya Krishnan 		return (pdu);
3161*d618d68dSPriya Krishnan 	}
3162*d618d68dSPriya Krishnan 	return (NULL);
3163*d618d68dSPriya Krishnan }
3164*d618d68dSPriya Krishnan 
3165*d618d68dSPriya Krishnan /*
3166*d618d68dSPriya Krishnan  * iscsit_process_pdu_in_queue() finds the next pdu in sequence
3167*d618d68dSPriya Krishnan  * and posts it to the SCSI layer
3168*d618d68dSPriya Krishnan  */
3169*d618d68dSPriya Krishnan static void
3170*d618d68dSPriya Krishnan iscsit_process_pdu_in_queue(iscsit_sess_t *ist)
3171*d618d68dSPriya Krishnan {
3172*d618d68dSPriya Krishnan 	iscsit_cbuf_t	*cbuf	= ist->ist_rxpdu_queue;
3173*d618d68dSPriya Krishnan 	idm_pdu_t	*pdu = NULL;
3174*d618d68dSPriya Krishnan 	uint32_t	expcmdsn;
3175*d618d68dSPriya Krishnan 
3176*d618d68dSPriya Krishnan 	for (;;) {
3177*d618d68dSPriya Krishnan 		mutex_enter(&ist->ist_sn_mutex);
3178*d618d68dSPriya Krishnan 		if (cbuf->cb_num_elems == 0) {
3179*d618d68dSPriya Krishnan 			mutex_exit(&ist->ist_sn_mutex);
3180*d618d68dSPriya Krishnan 			break;
3181*d618d68dSPriya Krishnan 		}
3182*d618d68dSPriya Krishnan 		expcmdsn = ist->ist_expcmdsn;
3183*d618d68dSPriya Krishnan 		if ((pdu = iscsit_remove_pdu_from_queue(ist, expcmdsn))
3184*d618d68dSPriya Krishnan 		    == NULL) {
3185*d618d68dSPriya Krishnan 			mutex_exit(&ist->ist_sn_mutex);
3186*d618d68dSPriya Krishnan 			break;
3187*d618d68dSPriya Krishnan 		}
3188*d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
3189*d618d68dSPriya Krishnan 		iscsit_post_staged_pdu(pdu);
3190*d618d68dSPriya Krishnan 	}
3191*d618d68dSPriya Krishnan }
3192*d618d68dSPriya Krishnan 
3193*d618d68dSPriya Krishnan static void
3194*d618d68dSPriya Krishnan iscsit_post_staged_pdu(idm_pdu_t *rx_pdu)
3195*d618d68dSPriya Krishnan {
3196*d618d68dSPriya Krishnan 	iscsit_conn_t	*ict	= rx_pdu->isp_ic->ic_handle;
3197*d618d68dSPriya Krishnan 
3198*d618d68dSPriya Krishnan 	/* Post the PDU to the SCSI layer */
3199*d618d68dSPriya Krishnan 	switch (IDM_PDU_OPCODE(rx_pdu)) {
3200*d618d68dSPriya Krishnan 	case ISCSI_OP_NOOP_OUT:
3201*d618d68dSPriya Krishnan 		iscsit_set_cmdsn(ict, rx_pdu);
3202*d618d68dSPriya Krishnan 		iscsit_pdu_op_noop(ict, rx_pdu);
3203*d618d68dSPriya Krishnan 		break;
3204*d618d68dSPriya Krishnan 	case ISCSI_OP_TEXT_CMD:
3205*d618d68dSPriya Krishnan 		iscsit_set_cmdsn(ict, rx_pdu);
3206*d618d68dSPriya Krishnan 		iscsit_pdu_op_text_cmd(ict, rx_pdu);
3207*d618d68dSPriya Krishnan 		break;
3208*d618d68dSPriya Krishnan 	case ISCSI_OP_SCSI_TASK_MGT_MSG:
3209*d618d68dSPriya Krishnan 		iscsit_set_cmdsn(ict, rx_pdu);
3210*d618d68dSPriya Krishnan 		iscsit_op_scsi_task_mgmt(ict, rx_pdu);
3211*d618d68dSPriya Krishnan 		break;
3212*d618d68dSPriya Krishnan 	case ISCSI_OP_SCSI_CMD:
3213*d618d68dSPriya Krishnan 		/* cmdSN will be incremented after creating itask */
3214*d618d68dSPriya Krishnan 		iscsit_post_scsi_cmd(rx_pdu->isp_ic, rx_pdu);
3215*d618d68dSPriya Krishnan 		break;
3216*d618d68dSPriya Krishnan 	case ISCSI_OP_LOGOUT_CMD:
3217*d618d68dSPriya Krishnan 		iscsit_set_cmdsn(ict, rx_pdu);
3218*d618d68dSPriya Krishnan 		iscsit_pdu_op_logout_cmd(ict, rx_pdu);
3219*d618d68dSPriya Krishnan 		break;
3220*d618d68dSPriya Krishnan 	default:
3221*d618d68dSPriya Krishnan 		/* No other PDUs should be placed on the queue */
3222*d618d68dSPriya Krishnan 		ASSERT(0);
3223*d618d68dSPriya Krishnan 	}
3224*d618d68dSPriya Krishnan 	iscsit_conn_dispatch_rele(ict); /* release hold on the conn */
3225*d618d68dSPriya Krishnan }
3226*d618d68dSPriya Krishnan 
3227*d618d68dSPriya Krishnan /* ARGSUSED */
3228*d618d68dSPriya Krishnan void
3229*d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor_start(void)
3230*d618d68dSPriya Krishnan {
3231*d618d68dSPriya Krishnan 	mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3232*d618d68dSPriya Krishnan 	if (iscsit_rxpdu_queue_monitor_thr_running) {
3233*d618d68dSPriya Krishnan 		mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3234*d618d68dSPriya Krishnan 		return;
3235*d618d68dSPriya Krishnan 	}
3236*d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_thr_id =
3237*d618d68dSPriya Krishnan 	    thread_create(NULL, 0, iscsit_rxpdu_queue_monitor, NULL,
3238*d618d68dSPriya Krishnan 	    0, &p0, TS_RUN, minclsyspri);
3239*d618d68dSPriya Krishnan 	while (!iscsit_rxpdu_queue_monitor_thr_running) {
3240*d618d68dSPriya Krishnan 		cv_wait(&iscsit_rxpdu_queue_monitor_cv,
3241*d618d68dSPriya Krishnan 		    &iscsit_rxpdu_queue_monitor_mutex);
3242*d618d68dSPriya Krishnan 	}
3243*d618d68dSPriya Krishnan 	mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3244*d618d68dSPriya Krishnan 
3245*d618d68dSPriya Krishnan }
3246*d618d68dSPriya Krishnan 
3247*d618d68dSPriya Krishnan /* ARGSUSED */
3248*d618d68dSPriya Krishnan void
3249*d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor_stop(void)
3250*d618d68dSPriya Krishnan {
3251*d618d68dSPriya Krishnan 	mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3252*d618d68dSPriya Krishnan 	if (iscsit_rxpdu_queue_monitor_thr_running) {
3253*d618d68dSPriya Krishnan 		iscsit_rxpdu_queue_monitor_thr_running = B_FALSE;
3254*d618d68dSPriya Krishnan 		cv_signal(&iscsit_rxpdu_queue_monitor_cv);
3255*d618d68dSPriya Krishnan 		mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3256*d618d68dSPriya Krishnan 
3257*d618d68dSPriya Krishnan 		thread_join(iscsit_rxpdu_queue_monitor_thr_did);
3258*d618d68dSPriya Krishnan 		return;
3259*d618d68dSPriya Krishnan 	}
3260*d618d68dSPriya Krishnan 	mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3261*d618d68dSPriya Krishnan }
3262*d618d68dSPriya Krishnan 
3263*d618d68dSPriya Krishnan /*
3264*d618d68dSPriya Krishnan  * A separate thread is used to scan the staging queue on all the
3265*d618d68dSPriya Krishnan  * sessions, If a delayed PDU does not arrive within a timeout, the
3266*d618d68dSPriya Krishnan  * target will advance to the staged PDU that is next in sequence
3267*d618d68dSPriya Krishnan  * and exceeded the threshold wait time. It is up to the initiator
3268*d618d68dSPriya Krishnan  * to note that the target has not acknowledged a particular cmdsn
3269*d618d68dSPriya Krishnan  * and take appropriate action.
3270*d618d68dSPriya Krishnan  */
3271*d618d68dSPriya Krishnan /* ARGSUSED */
3272*d618d68dSPriya Krishnan static void
3273*d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor(void *arg)
3274*d618d68dSPriya Krishnan {
3275*d618d68dSPriya Krishnan 	iscsit_tgt_t	*tgt;
3276*d618d68dSPriya Krishnan 	iscsit_sess_t	*ist;
3277*d618d68dSPriya Krishnan 
3278*d618d68dSPriya Krishnan 	mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3279*d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_thr_did = curthread->t_did;
3280*d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_thr_running = B_TRUE;
3281*d618d68dSPriya Krishnan 	cv_signal(&iscsit_rxpdu_queue_monitor_cv);
3282*d618d68dSPriya Krishnan 
3283*d618d68dSPriya Krishnan 	while (iscsit_rxpdu_queue_monitor_thr_running) {
3284*d618d68dSPriya Krishnan 		ISCSIT_GLOBAL_LOCK(RW_READER);
3285*d618d68dSPriya Krishnan 		for (tgt = avl_first(&iscsit_global.global_target_list);
3286*d618d68dSPriya Krishnan 		    tgt != NULL;
3287*d618d68dSPriya Krishnan 		    tgt = AVL_NEXT(&iscsit_global.global_target_list, tgt)) {
3288*d618d68dSPriya Krishnan 			mutex_enter(&tgt->target_mutex);
3289*d618d68dSPriya Krishnan 			for (ist = avl_first(&tgt->target_sess_list);
3290*d618d68dSPriya Krishnan 			    ist != NULL;
3291*d618d68dSPriya Krishnan 			    ist = AVL_NEXT(&tgt->target_sess_list, ist)) {
3292*d618d68dSPriya Krishnan 
3293*d618d68dSPriya Krishnan 				iscsit_rxpdu_queue_monitor_session(ist);
3294*d618d68dSPriya Krishnan 			}
3295*d618d68dSPriya Krishnan 			mutex_exit(&tgt->target_mutex);
3296*d618d68dSPriya Krishnan 		}
3297*d618d68dSPriya Krishnan 		ISCSIT_GLOBAL_UNLOCK();
3298*d618d68dSPriya Krishnan 		if (iscsit_rxpdu_queue_monitor_thr_running == B_FALSE) {
3299*d618d68dSPriya Krishnan 			break;
3300*d618d68dSPriya Krishnan 		}
3301*d618d68dSPriya Krishnan 		(void) cv_reltimedwait(&iscsit_rxpdu_queue_monitor_cv,
3302*d618d68dSPriya Krishnan 		    &iscsit_rxpdu_queue_monitor_mutex,
3303*d618d68dSPriya Krishnan 		    ISCSIT_RXPDU_QUEUE_MONITOR_INTERVAL * drv_usectohz(1000000),
3304*d618d68dSPriya Krishnan 		    TR_CLOCK_TICK);
3305*d618d68dSPriya Krishnan 	}
3306*d618d68dSPriya Krishnan 	mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3307*d618d68dSPriya Krishnan 	thread_exit();
3308*d618d68dSPriya Krishnan }
3309*d618d68dSPriya Krishnan 
3310*d618d68dSPriya Krishnan static void
3311*d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor_session(iscsit_sess_t *ist)
3312*d618d68dSPriya Krishnan {
3313*d618d68dSPriya Krishnan 	iscsit_cbuf_t	*cbuf	= ist->ist_rxpdu_queue;
3314*d618d68dSPriya Krishnan 	idm_pdu_t	*next_pdu = NULL;
3315*d618d68dSPriya Krishnan 	uint32_t	index, next_cmdsn, i;
3316*d618d68dSPriya Krishnan 
3317*d618d68dSPriya Krishnan 	/*
3318*d618d68dSPriya Krishnan 	 * Assume that all PDUs in the staging queue have a cmdsn >= expcmdsn.
3319*d618d68dSPriya Krishnan 	 * Starting with the expcmdsn, iterate over the staged PDUs to find
3320*d618d68dSPriya Krishnan 	 * the next PDU with a wait time greater than the threshold. If found
3321*d618d68dSPriya Krishnan 	 * advance the staged PDU to the SCSI layer, skipping over the missing
3322*d618d68dSPriya Krishnan 	 * PDU(s) to get past the hole in the command sequence. It is up to
3323*d618d68dSPriya Krishnan 	 * the initiator to note that the target has not acknowledged a cmdsn
3324*d618d68dSPriya Krishnan 	 * and take appropriate action.
3325*d618d68dSPriya Krishnan 	 *
3326*d618d68dSPriya Krishnan 	 * Since the PDU(s) arrive in any random order, it is possible that
3327*d618d68dSPriya Krishnan 	 * that the actual wait time for a particular PDU is much longer than
3328*d618d68dSPriya Krishnan 	 * the defined threshold. e.g. Consider a case where commands are sent
3329*d618d68dSPriya Krishnan 	 * over 4 different connections, and cmdsn = 1004 arrives first, then
3330*d618d68dSPriya Krishnan 	 * 1003, and 1002 and 1001 are lost due to a connection failure.
3331*d618d68dSPriya Krishnan 	 * So now 1003 is waiting for 1002 to be delivered, and although the
3332*d618d68dSPriya Krishnan 	 * wait time of 1004 > wait time of 1003, only 1003 will be considered
3333*d618d68dSPriya Krishnan 	 * by the monitor thread. 1004 will be automatically processed by
3334*d618d68dSPriya Krishnan 	 * iscsit_process_pdu_in_queue() once the scan is complete and the
3335*d618d68dSPriya Krishnan 	 * expcmdsn becomes current.
3336*d618d68dSPriya Krishnan 	 */
3337*d618d68dSPriya Krishnan 	mutex_enter(&ist->ist_sn_mutex);
3338*d618d68dSPriya Krishnan 	cbuf = ist->ist_rxpdu_queue;
3339*d618d68dSPriya Krishnan 	if (cbuf->cb_num_elems == 0) {
3340*d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
3341*d618d68dSPriya Krishnan 		return;
3342*d618d68dSPriya Krishnan 	}
3343*d618d68dSPriya Krishnan 	for (next_pdu = NULL, i = 0; ; i++) {
3344*d618d68dSPriya Krishnan 		next_cmdsn = ist->ist_expcmdsn + i; /* start at expcmdsn */
3345*d618d68dSPriya Krishnan 		index = next_cmdsn % ISCSIT_RXPDU_QUEUE_LEN;
3346*d618d68dSPriya Krishnan 		if ((next_pdu = cbuf->cb_buffer[index]) != NULL) {
3347*d618d68dSPriya Krishnan 			/*
3348*d618d68dSPriya Krishnan 			 * If the PDU wait time has not exceeded threshold
3349*d618d68dSPriya Krishnan 			 * stop scanning the staging queue until the timer
3350*d618d68dSPriya Krishnan 			 * fires again
3351*d618d68dSPriya Krishnan 			 */
3352*d618d68dSPriya Krishnan 			if ((ddi_get_time() - next_pdu->isp_queue_time)
3353*d618d68dSPriya Krishnan 			    < rxpdu_queue_threshold) {
3354*d618d68dSPriya Krishnan 				mutex_exit(&ist->ist_sn_mutex);
3355*d618d68dSPriya Krishnan 				return;
3356*d618d68dSPriya Krishnan 			}
3357*d618d68dSPriya Krishnan 			/*
3358*d618d68dSPriya Krishnan 			 * Remove the next PDU from the queue and post it
3359*d618d68dSPriya Krishnan 			 * to the SCSI layer, skipping over the missing
3360*d618d68dSPriya Krishnan 			 * PDU. Stop scanning the staging queue until
3361*d618d68dSPriya Krishnan 			 * the monitor timer fires again
3362*d618d68dSPriya Krishnan 			 */
3363*d618d68dSPriya Krishnan 			(void) iscsit_remove_pdu_from_queue(ist, next_cmdsn);
3364*d618d68dSPriya Krishnan 			mutex_exit(&ist->ist_sn_mutex);
3365*d618d68dSPriya Krishnan 			DTRACE_PROBE3(advanced__to__blocked__cmdsn,
3366*d618d68dSPriya Krishnan 			    iscsit_sess_t *, ist, idm_pdu_t *, next_pdu,
3367*d618d68dSPriya Krishnan 			    uint32_t, next_cmdsn);
3368*d618d68dSPriya Krishnan 			iscsit_post_staged_pdu(next_pdu);
3369*d618d68dSPriya Krishnan 			/* Deliver any subsequent PDUs immediately */
3370*d618d68dSPriya Krishnan 			iscsit_process_pdu_in_queue(ist);
3371*d618d68dSPriya Krishnan 			return;
3372*d618d68dSPriya Krishnan 		}
3373*d618d68dSPriya Krishnan 		/*
3374*d618d68dSPriya Krishnan 		 * Skipping over i PDUs, e.g. a case where commands 1001 and
3375*d618d68dSPriya Krishnan 		 * 1002 are lost in the network, skip over both and post 1003
3376*d618d68dSPriya Krishnan 		 * expcmdsn then becomes 1004 at the end of the scan.
3377*d618d68dSPriya Krishnan 		 */
3378*d618d68dSPriya Krishnan 		DTRACE_PROBE2(skipping__over__cmdsn, iscsit_sess_t *, ist,
3379*d618d68dSPriya Krishnan 		    uint32_t, next_cmdsn);
3380*d618d68dSPriya Krishnan 	}
3381*d618d68dSPriya Krishnan 	/*
3382*d618d68dSPriya Krishnan 	 * following the assumption, staged cmdsn >= expcmdsn, this statement
3383*d618d68dSPriya Krishnan 	 * is never reached.
3384*d618d68dSPriya Krishnan 	 */
3385*d618d68dSPriya Krishnan }
3386