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 /*
22d618d68dSPriya Krishnan  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
233e2514dcSDan McDonald  *
24a481fa48SRick McNeal  * Copyright 2014, 2015 Nexenta Systems, Inc. All rights reserved.
252727bb05STim Kordas  * Copyright (c) 2017, Joyent, Inc.  All rights reserved.
266319b0c7SGarrett D'Amore  */
27a6d42e7dSPeter Dunlap 
28a6d42e7dSPeter Dunlap #include <sys/cpuvar.h>
29a6d42e7dSPeter Dunlap #include <sys/types.h>
30a6d42e7dSPeter Dunlap #include <sys/conf.h>
31a6d42e7dSPeter Dunlap #include <sys/stat.h>
32a6d42e7dSPeter Dunlap #include <sys/file.h>
33a6d42e7dSPeter Dunlap #include <sys/ddi.h>
34a6d42e7dSPeter Dunlap #include <sys/sunddi.h>
35a6d42e7dSPeter Dunlap #include <sys/modctl.h>
36a6d42e7dSPeter Dunlap #include <sys/sysmacros.h>
37a6d42e7dSPeter Dunlap #include <sys/socket.h>
38a6d42e7dSPeter Dunlap #include <sys/strsubr.h>
39a6d42e7dSPeter Dunlap #include <sys/nvpair.h>
40a6d42e7dSPeter Dunlap 
41a6d42e7dSPeter Dunlap #include <sys/stmf.h>
42a6d42e7dSPeter Dunlap #include <sys/stmf_ioctl.h>
43a6d42e7dSPeter Dunlap #include <sys/portif.h>
44a6d42e7dSPeter Dunlap #include <sys/idm/idm.h>
45a6d42e7dSPeter Dunlap #include <sys/idm/idm_conn_sm.h>
464558d122SViswanathan Kannappan 
474558d122SViswanathan Kannappan #include "iscsit_isns.h"
484558d122SViswanathan Kannappan #include "iscsit.h"
49a6d42e7dSPeter Dunlap 
50a6d42e7dSPeter Dunlap #define	ISCSIT_VERSION		BUILD_DATE "-1.18dev"
51a6d42e7dSPeter Dunlap #define	ISCSIT_NAME_VERSION	"COMSTAR ISCSIT v" ISCSIT_VERSION
52a6d42e7dSPeter Dunlap 
53a6d42e7dSPeter Dunlap /*
54a6d42e7dSPeter Dunlap  * DDI entry points.
55a6d42e7dSPeter Dunlap  */
56a6d42e7dSPeter Dunlap static int iscsit_drv_attach(dev_info_t *, ddi_attach_cmd_t);
57a6d42e7dSPeter Dunlap static int iscsit_drv_detach(dev_info_t *, ddi_detach_cmd_t);
58a6d42e7dSPeter Dunlap static int iscsit_drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
59a6d42e7dSPeter Dunlap static int iscsit_drv_open(dev_t *, int, int, cred_t *);
60a6d42e7dSPeter Dunlap static int iscsit_drv_close(dev_t, int, int, cred_t *);
61a6d42e7dSPeter Dunlap static boolean_t iscsit_drv_busy(void);
62a6d42e7dSPeter Dunlap static int iscsit_drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
63a6d42e7dSPeter Dunlap 
64a6d42e7dSPeter Dunlap extern struct mod_ops mod_miscops;
65a6d42e7dSPeter Dunlap 
66a6d42e7dSPeter Dunlap 
67a6d42e7dSPeter Dunlap static struct cb_ops iscsit_cb_ops = {
68a6d42e7dSPeter Dunlap 	iscsit_drv_open,	/* cb_open */
69a6d42e7dSPeter Dunlap 	iscsit_drv_close,	/* cb_close */
70a6d42e7dSPeter Dunlap 	nodev,			/* cb_strategy */
71a6d42e7dSPeter Dunlap 	nodev,			/* cb_print */
72a6d42e7dSPeter Dunlap 	nodev,			/* cb_dump */
73a6d42e7dSPeter Dunlap 	nodev,			/* cb_read */
74a6d42e7dSPeter Dunlap 	nodev,			/* cb_write */
75a6d42e7dSPeter Dunlap 	iscsit_drv_ioctl,	/* cb_ioctl */
76a6d42e7dSPeter Dunlap 	nodev,			/* cb_devmap */
77a6d42e7dSPeter Dunlap 	nodev,			/* cb_mmap */
78a6d42e7dSPeter Dunlap 	nodev,			/* cb_segmap */
79a6d42e7dSPeter Dunlap 	nochpoll,		/* cb_chpoll */
80a6d42e7dSPeter Dunlap 	ddi_prop_op,		/* cb_prop_op */
81a6d42e7dSPeter Dunlap 	NULL,			/* cb_streamtab */
82a6d42e7dSPeter Dunlap 	D_MP,			/* cb_flag */
83a6d42e7dSPeter Dunlap 	CB_REV,			/* cb_rev */
84a6d42e7dSPeter Dunlap 	nodev,			/* cb_aread */
85a6d42e7dSPeter Dunlap 	nodev,			/* cb_awrite */
86a6d42e7dSPeter Dunlap };
87a6d42e7dSPeter Dunlap 
88a6d42e7dSPeter Dunlap static struct dev_ops iscsit_dev_ops = {
89a6d42e7dSPeter Dunlap 	DEVO_REV,		/* devo_rev */
90a6d42e7dSPeter Dunlap 	0,			/* devo_refcnt */
91a6d42e7dSPeter Dunlap 	iscsit_drv_getinfo,	/* devo_getinfo */
92a6d42e7dSPeter Dunlap 	nulldev,		/* devo_identify */
93a6d42e7dSPeter Dunlap 	nulldev,		/* devo_probe */
94a6d42e7dSPeter Dunlap 	iscsit_drv_attach,	/* devo_attach */
95a6d42e7dSPeter Dunlap 	iscsit_drv_detach,	/* devo_detach */
96a6d42e7dSPeter Dunlap 	nodev,			/* devo_reset */
97a6d42e7dSPeter Dunlap 	&iscsit_cb_ops,		/* devo_cb_ops */
98a6d42e7dSPeter Dunlap 	NULL,			/* devo_bus_ops */
99a6d42e7dSPeter Dunlap 	NULL,			/* devo_power */
10063528ae4SJames Moore 	ddi_quiesce_not_needed,	/* quiesce */
101a6d42e7dSPeter Dunlap };
102a6d42e7dSPeter Dunlap 
103a6d42e7dSPeter Dunlap static struct modldrv modldrv = {
104a6d42e7dSPeter Dunlap 	&mod_driverops,
105a6d42e7dSPeter Dunlap 	"iSCSI Target",
106a6d42e7dSPeter Dunlap 	&iscsit_dev_ops,
107a6d42e7dSPeter Dunlap };
108a6d42e7dSPeter Dunlap 
109a6d42e7dSPeter Dunlap static struct modlinkage modlinkage = {
110a6d42e7dSPeter Dunlap 	MODREV_1,
111a6d42e7dSPeter Dunlap 	&modldrv,
112a6d42e7dSPeter Dunlap 	NULL,
113a6d42e7dSPeter Dunlap };
114a6d42e7dSPeter Dunlap 
115a6d42e7dSPeter Dunlap 
116a6d42e7dSPeter Dunlap iscsit_global_t iscsit_global;
117a6d42e7dSPeter Dunlap 
118a6d42e7dSPeter Dunlap kmem_cache_t	*iscsit_status_pdu_cache;
119a6d42e7dSPeter Dunlap 
120a6d42e7dSPeter Dunlap boolean_t	iscsit_sm_logging = B_FALSE;
121a6d42e7dSPeter Dunlap 
1223fc1e17eSPriya Krishnan kmutex_t	login_sm_session_mutex;
1233fc1e17eSPriya Krishnan 
124a6d42e7dSPeter Dunlap static idm_status_t iscsit_init(dev_info_t *dip);
125a6d42e7dSPeter Dunlap static idm_status_t iscsit_enable_svc(iscsit_hostinfo_t *hostinfo);
126a6d42e7dSPeter Dunlap static void iscsit_disable_svc(void);
127a6d42e7dSPeter Dunlap 
1282727bb05STim Kordas static boolean_t
129d618d68dSPriya Krishnan iscsit_check_cmdsn_and_queue(idm_pdu_t *rx_pdu);
130d618d68dSPriya Krishnan 
131d618d68dSPriya Krishnan static void
132d618d68dSPriya Krishnan iscsit_add_pdu_to_queue(iscsit_sess_t *ist, idm_pdu_t *rx_pdu);
133d618d68dSPriya Krishnan 
134d618d68dSPriya Krishnan static idm_pdu_t *
135d618d68dSPriya Krishnan iscsit_remove_pdu_from_queue(iscsit_sess_t *ist, uint32_t cmdsn);
136d618d68dSPriya Krishnan 
137d618d68dSPriya Krishnan static void
138d618d68dSPriya Krishnan iscsit_process_pdu_in_queue(iscsit_sess_t *ist);
139d618d68dSPriya Krishnan 
140d618d68dSPriya Krishnan static void
141d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor_session(iscsit_sess_t *ist);
142d618d68dSPriya Krishnan 
143d618d68dSPriya Krishnan static void
144d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor(void *arg);
145d618d68dSPriya Krishnan 
146d618d68dSPriya Krishnan static void
147d618d68dSPriya Krishnan iscsit_post_staged_pdu(idm_pdu_t *rx_pdu);
148d618d68dSPriya Krishnan 
149d618d68dSPriya Krishnan static void
150d618d68dSPriya Krishnan iscsit_post_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu);
151d618d68dSPriya Krishnan 
152a6d42e7dSPeter Dunlap static void
153a6d42e7dSPeter Dunlap iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
154a6d42e7dSPeter Dunlap 
155a6d42e7dSPeter Dunlap static void
156a6d42e7dSPeter Dunlap iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
157a6d42e7dSPeter Dunlap 
158a6d42e7dSPeter Dunlap static void
159a6d42e7dSPeter Dunlap iscsit_pdu_op_login_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
160a6d42e7dSPeter Dunlap 
161a6d42e7dSPeter Dunlap void
162a6d42e7dSPeter Dunlap iscsit_pdu_op_text_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
163a6d42e7dSPeter Dunlap 
164a6d42e7dSPeter Dunlap static void
165a6d42e7dSPeter Dunlap iscsit_pdu_op_logout_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
166a6d42e7dSPeter Dunlap 
167a6d42e7dSPeter Dunlap int iscsit_cmd_window();
168a6d42e7dSPeter Dunlap 
169d618d68dSPriya Krishnan static  int
170d618d68dSPriya Krishnan iscsit_sna_lt(uint32_t sn1, uint32_t sn2);
171d618d68dSPriya Krishnan 
172a6d42e7dSPeter Dunlap void
173a6d42e7dSPeter Dunlap iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
174a6d42e7dSPeter Dunlap 
175a6d42e7dSPeter Dunlap static void
176a6d42e7dSPeter Dunlap iscsit_deferred_dispatch(idm_pdu_t *rx_pdu);
177a6d42e7dSPeter Dunlap 
178a6d42e7dSPeter Dunlap static void
179a6d42e7dSPeter Dunlap iscsit_deferred(void *rx_pdu_void);
180a6d42e7dSPeter Dunlap 
181a6d42e7dSPeter Dunlap static idm_status_t
182a6d42e7dSPeter Dunlap iscsit_conn_accept(idm_conn_t *ic);
183a6d42e7dSPeter Dunlap 
184a6d42e7dSPeter Dunlap static idm_status_t
185a6d42e7dSPeter Dunlap iscsit_ffp_enabled(idm_conn_t *ic);
186a6d42e7dSPeter Dunlap 
187a6d42e7dSPeter Dunlap static idm_status_t
188a6d42e7dSPeter Dunlap iscsit_ffp_disabled(idm_conn_t *ic, idm_ffp_disable_t disable_class);
189a6d42e7dSPeter Dunlap 
190a6d42e7dSPeter Dunlap static idm_status_t
191a6d42e7dSPeter Dunlap iscsit_conn_lost(idm_conn_t *ic);
192a6d42e7dSPeter Dunlap 
193a6d42e7dSPeter Dunlap static idm_status_t
194a6d42e7dSPeter Dunlap iscsit_conn_destroy(idm_conn_t *ic);
195a6d42e7dSPeter Dunlap 
196a6d42e7dSPeter Dunlap static stmf_data_buf_t *
197a6d42e7dSPeter Dunlap iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
198a6d42e7dSPeter Dunlap     uint32_t flags);
199a6d42e7dSPeter Dunlap 
200a6d42e7dSPeter Dunlap static void
201a6d42e7dSPeter Dunlap iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf);
202a6d42e7dSPeter Dunlap 
203a6d42e7dSPeter Dunlap static void
204a6d42e7dSPeter Dunlap iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status);
205a6d42e7dSPeter Dunlap 
206a6d42e7dSPeter Dunlap static void
207a6d42e7dSPeter Dunlap iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status);
208a6d42e7dSPeter Dunlap 
209a6d42e7dSPeter Dunlap static void
210a6d42e7dSPeter Dunlap iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status);
211a6d42e7dSPeter Dunlap 
212a6d42e7dSPeter Dunlap static stmf_status_t
213a6d42e7dSPeter Dunlap iscsit_idm_to_stmf(idm_status_t idmrc);
214a6d42e7dSPeter Dunlap 
215a6d42e7dSPeter Dunlap static iscsit_task_t *
216a6d42e7dSPeter Dunlap iscsit_task_alloc(iscsit_conn_t *ict);
217a6d42e7dSPeter Dunlap 
218a6d42e7dSPeter Dunlap static void
219a6d42e7dSPeter Dunlap iscsit_task_free(iscsit_task_t *itask);
220a6d42e7dSPeter Dunlap 
221a6d42e7dSPeter Dunlap static iscsit_task_t *
222a6d42e7dSPeter Dunlap iscsit_tm_task_alloc(iscsit_conn_t *ict);
223a6d42e7dSPeter Dunlap 
224a6d42e7dSPeter Dunlap static void
225a6d42e7dSPeter Dunlap iscsit_tm_task_free(iscsit_task_t *itask);
226a6d42e7dSPeter Dunlap 
22772cf3143Speter dunlap static idm_status_t
22872cf3143Speter dunlap iscsit_task_start(iscsit_task_t *itask);
22972cf3143Speter dunlap 
23072cf3143Speter dunlap static void
23172cf3143Speter dunlap iscsit_task_done(iscsit_task_t *itask);
23272cf3143Speter dunlap 
233a6d42e7dSPeter Dunlap static int
234a6d42e7dSPeter Dunlap iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags);
235a6d42e7dSPeter Dunlap 
236a6d42e7dSPeter Dunlap static void
237a6d42e7dSPeter Dunlap iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags);
238a6d42e7dSPeter Dunlap 
239a6d42e7dSPeter Dunlap static it_cfg_status_t
240a6d42e7dSPeter Dunlap iscsit_config_merge(it_config_t *cfg);
241a6d42e7dSPeter Dunlap 
242a6d42e7dSPeter Dunlap static idm_status_t
243a6d42e7dSPeter Dunlap iscsit_login_fail(idm_conn_t *ic);
244a6d42e7dSPeter Dunlap 
24572cf3143Speter dunlap static boolean_t iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn);
24672cf3143Speter dunlap static void iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu,
24772cf3143Speter dunlap     uint8_t response, uint8_t cmd_status);
24872cf3143Speter dunlap static void iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu,
24972cf3143Speter dunlap     uint8_t tm_status);
25072cf3143Speter dunlap 
251d618d68dSPriya Krishnan /*
252d618d68dSPriya Krishnan  * MC/S: Out-of-order commands are staged on a session-wide wait
253d618d68dSPriya Krishnan  * queue until a system-tunable threshold is reached. A separate
254d618d68dSPriya Krishnan  * thread is used to scan the staging queue on all the session,
255d618d68dSPriya Krishnan  * If a delayed PDU does not arrive within a timeout, the target
256d618d68dSPriya Krishnan  * will advance to the staged PDU that is next in sequence, skipping
257d618d68dSPriya Krishnan  * over the missing PDU(s) to go past a hole in the sequence.
258d618d68dSPriya Krishnan  */
259d618d68dSPriya Krishnan volatile int rxpdu_queue_threshold = ISCSIT_RXPDU_QUEUE_THRESHOLD;
260d618d68dSPriya Krishnan 
261d618d68dSPriya Krishnan static kmutex_t		iscsit_rxpdu_queue_monitor_mutex;
262d618d68dSPriya Krishnan kthread_t		*iscsit_rxpdu_queue_monitor_thr_id;
263d618d68dSPriya Krishnan static kt_did_t		iscsit_rxpdu_queue_monitor_thr_did;
264d618d68dSPriya Krishnan static boolean_t	iscsit_rxpdu_queue_monitor_thr_running;
265d618d68dSPriya Krishnan static kcondvar_t	iscsit_rxpdu_queue_monitor_cv;
266d618d68dSPriya Krishnan 
267a6d42e7dSPeter Dunlap int
268a6d42e7dSPeter Dunlap _init(void)
269a6d42e7dSPeter Dunlap {
270a6d42e7dSPeter Dunlap 	int rc;
271a6d42e7dSPeter Dunlap 
272a6d42e7dSPeter Dunlap 	rw_init(&iscsit_global.global_rwlock, NULL, RW_DRIVER, NULL);
2738c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_init(&iscsit_global.global_state_mutex, NULL,
2748c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	    MUTEX_DRIVER, NULL);
275a6d42e7dSPeter Dunlap 	iscsit_global.global_svc_state = ISE_DETACHED;
276a6d42e7dSPeter Dunlap 
277d618d68dSPriya Krishnan 	mutex_init(&iscsit_rxpdu_queue_monitor_mutex, NULL,
278d618d68dSPriya Krishnan 	    MUTEX_DRIVER, NULL);
2793fc1e17eSPriya Krishnan 	mutex_init(&login_sm_session_mutex, NULL, MUTEX_DRIVER, NULL);
280d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_thr_id = NULL;
281d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_thr_running = B_FALSE;
282d618d68dSPriya Krishnan 	cv_init(&iscsit_rxpdu_queue_monitor_cv, NULL, CV_DEFAULT, NULL);
283d618d68dSPriya Krishnan 
284a6d42e7dSPeter Dunlap 	if ((rc = mod_install(&modlinkage)) != 0) {
2858c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_destroy(&iscsit_global.global_state_mutex);
286a6d42e7dSPeter Dunlap 		rw_destroy(&iscsit_global.global_rwlock);
287a6d42e7dSPeter Dunlap 		return (rc);
288a6d42e7dSPeter Dunlap 	}
289a6d42e7dSPeter Dunlap 
290a6d42e7dSPeter Dunlap 	return (rc);
291a6d42e7dSPeter Dunlap }
292a6d42e7dSPeter Dunlap 
293a6d42e7dSPeter Dunlap int
294a6d42e7dSPeter Dunlap _info(struct modinfo *modinfop)
295a6d42e7dSPeter Dunlap {
296a6d42e7dSPeter Dunlap 	return (mod_info(&modlinkage, modinfop));
297a6d42e7dSPeter Dunlap }
298a6d42e7dSPeter Dunlap 
299a6d42e7dSPeter Dunlap int
300a6d42e7dSPeter Dunlap _fini(void)
301a6d42e7dSPeter Dunlap {
302a6d42e7dSPeter Dunlap 	int rc;
303a6d42e7dSPeter Dunlap 
304a6d42e7dSPeter Dunlap 	rc = mod_remove(&modlinkage);
305a6d42e7dSPeter Dunlap 
306a6d42e7dSPeter Dunlap 	if (rc == 0) {
307d618d68dSPriya Krishnan 		mutex_destroy(&iscsit_rxpdu_queue_monitor_mutex);
3083fc1e17eSPriya Krishnan 		mutex_destroy(&login_sm_session_mutex);
309d618d68dSPriya Krishnan 		cv_destroy(&iscsit_rxpdu_queue_monitor_cv);
3108c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_destroy(&iscsit_global.global_state_mutex);
311a6d42e7dSPeter Dunlap 		rw_destroy(&iscsit_global.global_rwlock);
312a6d42e7dSPeter Dunlap 	}
313a6d42e7dSPeter Dunlap 
314a6d42e7dSPeter Dunlap 	return (rc);
315a6d42e7dSPeter Dunlap }
316a6d42e7dSPeter Dunlap 
317a6d42e7dSPeter Dunlap /*
318a6d42e7dSPeter Dunlap  * DDI entry points.
319a6d42e7dSPeter Dunlap  */
320a6d42e7dSPeter Dunlap 
321a6d42e7dSPeter Dunlap /* ARGSUSED */
322a6d42e7dSPeter Dunlap static int
323a6d42e7dSPeter Dunlap iscsit_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,
324a6d42e7dSPeter Dunlap     void **result)
325a6d42e7dSPeter Dunlap {
326a6d42e7dSPeter Dunlap 	ulong_t instance = getminor((dev_t)arg);
327a6d42e7dSPeter Dunlap 
328a6d42e7dSPeter Dunlap 	switch (cmd) {
329a6d42e7dSPeter Dunlap 	case DDI_INFO_DEVT2DEVINFO:
330a6d42e7dSPeter Dunlap 		*result = iscsit_global.global_dip;
331a6d42e7dSPeter Dunlap 		return (DDI_SUCCESS);
332a6d42e7dSPeter Dunlap 
333a6d42e7dSPeter Dunlap 	case DDI_INFO_DEVT2INSTANCE:
334a6d42e7dSPeter Dunlap 		*result = (void *)instance;
335a6d42e7dSPeter Dunlap 		return (DDI_SUCCESS);
336a6d42e7dSPeter Dunlap 
337a6d42e7dSPeter Dunlap 	default:
338a6d42e7dSPeter Dunlap 		break;
339a6d42e7dSPeter Dunlap 	}
340a6d42e7dSPeter Dunlap 
341a6d42e7dSPeter Dunlap 	return (DDI_FAILURE);
342a6d42e7dSPeter Dunlap }
343a6d42e7dSPeter Dunlap 
344a6d42e7dSPeter Dunlap static int
345a6d42e7dSPeter Dunlap iscsit_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
346a6d42e7dSPeter Dunlap {
347a6d42e7dSPeter Dunlap 	if (cmd != DDI_ATTACH) {
348a6d42e7dSPeter Dunlap 		return (DDI_FAILURE);
349a6d42e7dSPeter Dunlap 	}
350a6d42e7dSPeter Dunlap 
351a6d42e7dSPeter Dunlap 	if (ddi_get_instance(dip) != 0) {
352a6d42e7dSPeter Dunlap 		/* we only allow instance 0 to attach */
353a6d42e7dSPeter Dunlap 		return (DDI_FAILURE);
354a6d42e7dSPeter Dunlap 	}
355a6d42e7dSPeter Dunlap 
356a6d42e7dSPeter Dunlap 	/* create the minor node */
357a6d42e7dSPeter Dunlap 	if (ddi_create_minor_node(dip, ISCSIT_MODNAME, S_IFCHR, 0,
358a6d42e7dSPeter Dunlap 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
359a6d42e7dSPeter Dunlap 		cmn_err(CE_WARN, "iscsit_drv_attach: "
360a6d42e7dSPeter Dunlap 		    "failed creating minor node");
361a6d42e7dSPeter Dunlap 		return (DDI_FAILURE);
362a6d42e7dSPeter Dunlap 	}
363a6d42e7dSPeter Dunlap 
364a6d42e7dSPeter Dunlap 	if (iscsit_init(dip) != IDM_STATUS_SUCCESS) {
365a6d42e7dSPeter Dunlap 		cmn_err(CE_WARN, "iscsit_drv_attach: "
366a6d42e7dSPeter Dunlap 		    "failed to initialize");
367a6d42e7dSPeter Dunlap 		ddi_remove_minor_node(dip, NULL);
368a6d42e7dSPeter Dunlap 		return (DDI_FAILURE);
369a6d42e7dSPeter Dunlap 	}
370a6d42e7dSPeter Dunlap 
371a6d42e7dSPeter Dunlap 	iscsit_global.global_svc_state = ISE_DISABLED;
372a6d42e7dSPeter Dunlap 	iscsit_global.global_dip = dip;
373a6d42e7dSPeter Dunlap 
374a6d42e7dSPeter Dunlap 	return (DDI_SUCCESS);
375a6d42e7dSPeter Dunlap }
376a6d42e7dSPeter Dunlap 
377a6d42e7dSPeter Dunlap /*ARGSUSED*/
378a6d42e7dSPeter Dunlap static int
379a6d42e7dSPeter Dunlap iscsit_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
380a6d42e7dSPeter Dunlap {
381a6d42e7dSPeter Dunlap 	if (cmd != DDI_DETACH)
382a6d42e7dSPeter Dunlap 		return (DDI_FAILURE);
383a6d42e7dSPeter Dunlap 
3848c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	/*
3858c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * drv_detach is called in a context that owns the
3868c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * device node for the /dev/pseudo device.  If this thread blocks
3878c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * for any resource, other threads that need the /dev/pseudo device
3888c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * may end up in a deadlock with this thread.Hence, we use a
3898c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * separate lock just for the structures that drv_detach needs
3908c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * to access.
3918c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 */
3928c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_enter(&iscsit_global.global_state_mutex);
393a6d42e7dSPeter Dunlap 	if (iscsit_drv_busy()) {
3948c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
395a6d42e7dSPeter Dunlap 		return (EBUSY);
396a6d42e7dSPeter Dunlap 	}
397a6d42e7dSPeter Dunlap 
398a6d42e7dSPeter Dunlap 	iscsit_global.global_dip = NULL;
399a6d42e7dSPeter Dunlap 	ddi_remove_minor_node(dip, NULL);
400a6d42e7dSPeter Dunlap 
401a6d42e7dSPeter Dunlap 	ldi_ident_release(iscsit_global.global_li);
402a6d42e7dSPeter Dunlap 	iscsit_global.global_svc_state = ISE_DETACHED;
403a6d42e7dSPeter Dunlap 
4048c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_exit(&iscsit_global.global_state_mutex);
405a6d42e7dSPeter Dunlap 
406a6d42e7dSPeter Dunlap 	return (DDI_SUCCESS);
407a6d42e7dSPeter Dunlap }
408a6d42e7dSPeter Dunlap 
409a6d42e7dSPeter Dunlap /*ARGSUSED*/
410a6d42e7dSPeter Dunlap static int
411a6d42e7dSPeter Dunlap iscsit_drv_open(dev_t *devp, int flag, int otyp, cred_t *credp)
412a6d42e7dSPeter Dunlap {
413a6d42e7dSPeter Dunlap 	return (0);
414a6d42e7dSPeter Dunlap }
415a6d42e7dSPeter Dunlap 
416a6d42e7dSPeter Dunlap /* ARGSUSED */
417a6d42e7dSPeter Dunlap static int
418a6d42e7dSPeter Dunlap iscsit_drv_close(dev_t dev, int flag, int otyp, cred_t *credp)
419a6d42e7dSPeter Dunlap {
420a6d42e7dSPeter Dunlap 	return (0);
421a6d42e7dSPeter Dunlap }
422a6d42e7dSPeter Dunlap 
423a6d42e7dSPeter Dunlap static boolean_t
424a6d42e7dSPeter Dunlap iscsit_drv_busy(void)
425a6d42e7dSPeter Dunlap {
4268c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	ASSERT(MUTEX_HELD(&iscsit_global.global_state_mutex));
4278c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
428a6d42e7dSPeter Dunlap 	switch (iscsit_global.global_svc_state) {
429a6d42e7dSPeter Dunlap 	case ISE_DISABLED:
430a6d42e7dSPeter Dunlap 	case ISE_DETACHED:
431a6d42e7dSPeter Dunlap 		return (B_FALSE);
432a6d42e7dSPeter Dunlap 	default:
433a6d42e7dSPeter Dunlap 		return (B_TRUE);
434a6d42e7dSPeter Dunlap 	}
435a6d42e7dSPeter Dunlap 	/* NOTREACHED */
436a6d42e7dSPeter Dunlap }
437a6d42e7dSPeter Dunlap 
438a6d42e7dSPeter Dunlap /* ARGSUSED */
439a6d42e7dSPeter Dunlap static int
440a6d42e7dSPeter Dunlap iscsit_drv_ioctl(dev_t drv, int cmd, intptr_t argp, int flag, cred_t *cred,
441a6d42e7dSPeter Dunlap     int *retval)
442a6d42e7dSPeter Dunlap {
443a6d42e7dSPeter Dunlap 	iscsit_ioc_set_config_t		setcfg;
444a6d42e7dSPeter Dunlap 	iscsit_ioc_set_config32_t	setcfg32;
44507a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	char				*cfg_pnvlist = NULL;
44607a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	nvlist_t			*cfg_nvlist = NULL;
44707a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	it_config_t			*cfg = NULL;
448a6d42e7dSPeter Dunlap 	idm_status_t			idmrc;
449a6d42e7dSPeter Dunlap 	int				rc = 0;
450a6d42e7dSPeter Dunlap 
451a6d42e7dSPeter Dunlap 	if (drv_priv(cred) != 0) {
452a6d42e7dSPeter Dunlap 		return (EPERM);
453a6d42e7dSPeter Dunlap 	}
454a6d42e7dSPeter Dunlap 
4558c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_enter(&iscsit_global.global_state_mutex);
456a6d42e7dSPeter Dunlap 
457a6d42e7dSPeter Dunlap 	/*
458a6d42e7dSPeter Dunlap 	 * Validate ioctl requests against global service state
459a6d42e7dSPeter Dunlap 	 */
460a6d42e7dSPeter Dunlap 	switch (iscsit_global.global_svc_state) {
461a6d42e7dSPeter Dunlap 	case ISE_ENABLED:
462a6d42e7dSPeter Dunlap 		if (cmd == ISCSIT_IOC_DISABLE_SVC) {
463a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_DISABLING;
464a6d42e7dSPeter Dunlap 		} else if (cmd == ISCSIT_IOC_ENABLE_SVC) {
465a6d42e7dSPeter Dunlap 			/* Already enabled */
4668c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_exit(&iscsit_global.global_state_mutex);
467a6d42e7dSPeter Dunlap 			return (0);
468a6d42e7dSPeter Dunlap 		} else {
469a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_BUSY;
470a6d42e7dSPeter Dunlap 		}
471a6d42e7dSPeter Dunlap 		break;
472a6d42e7dSPeter Dunlap 	case ISE_DISABLED:
473a6d42e7dSPeter Dunlap 		if (cmd == ISCSIT_IOC_ENABLE_SVC) {
474a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_ENABLING;
475a6d42e7dSPeter Dunlap 		} else if (cmd == ISCSIT_IOC_DISABLE_SVC) {
476a6d42e7dSPeter Dunlap 			/* Already disabled */
4778c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_exit(&iscsit_global.global_state_mutex);
478a6d42e7dSPeter Dunlap 			return (0);
479a6d42e7dSPeter Dunlap 		} else {
480a6d42e7dSPeter Dunlap 			rc = EFAULT;
481a6d42e7dSPeter Dunlap 		}
482a6d42e7dSPeter Dunlap 		break;
4838c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	case ISE_BUSY:
484a6d42e7dSPeter Dunlap 	case ISE_ENABLING:
485a6d42e7dSPeter Dunlap 	case ISE_DISABLING:
486a6d42e7dSPeter Dunlap 		rc = EAGAIN;
487a6d42e7dSPeter Dunlap 		break;
488a6d42e7dSPeter Dunlap 	case ISE_DETACHED:
489a6d42e7dSPeter Dunlap 	default:
490a6d42e7dSPeter Dunlap 		rc = EFAULT;
491a6d42e7dSPeter Dunlap 		break;
492a6d42e7dSPeter Dunlap 	}
493a6d42e7dSPeter Dunlap 
4948c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_exit(&iscsit_global.global_state_mutex);
495a6d42e7dSPeter Dunlap 	if (rc != 0)
496a6d42e7dSPeter Dunlap 		return (rc);
497a6d42e7dSPeter Dunlap 
498a6d42e7dSPeter Dunlap 	/* Handle ioctl request (enable/disable have already been handled) */
499a6d42e7dSPeter Dunlap 	switch (cmd) {
500a6d42e7dSPeter Dunlap 	case ISCSIT_IOC_SET_CONFIG:
50107a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		/* Any errors must set state back to ISE_ENABLED */
502a6d42e7dSPeter Dunlap 		switch (ddi_model_convert_from(flag & FMODELS)) {
503a6d42e7dSPeter Dunlap 		case DDI_MODEL_ILP32:
504a6d42e7dSPeter Dunlap 			if (ddi_copyin((void *)argp, &setcfg32,
50507a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			    sizeof (iscsit_ioc_set_config32_t), flag) != 0) {
50607a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 				rc = EFAULT;
50707a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 				goto cleanup;
50807a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			}
509a6d42e7dSPeter Dunlap 
510a6d42e7dSPeter Dunlap 			setcfg.set_cfg_pnvlist =
511a6d42e7dSPeter Dunlap 			    (char *)((uintptr_t)setcfg32.set_cfg_pnvlist);
512a6d42e7dSPeter Dunlap 			setcfg.set_cfg_vers = setcfg32.set_cfg_vers;
513a6d42e7dSPeter Dunlap 			setcfg.set_cfg_pnvlist_len =
514a6d42e7dSPeter Dunlap 			    setcfg32.set_cfg_pnvlist_len;
515a6d42e7dSPeter Dunlap 			break;
516a6d42e7dSPeter Dunlap 		case DDI_MODEL_NONE:
517a6d42e7dSPeter Dunlap 			if (ddi_copyin((void *)argp, &setcfg,
51807a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			    sizeof (iscsit_ioc_set_config_t), flag) != 0) {
51907a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 				rc = EFAULT;
52007a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 				goto cleanup;
52107a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			}
522a6d42e7dSPeter Dunlap 			break;
523fcc214c3SCharles Ting 		default:
524fcc214c3SCharles Ting 			rc = EFAULT;
525fcc214c3SCharles Ting 			goto cleanup;
526a6d42e7dSPeter Dunlap 		}
527a6d42e7dSPeter Dunlap 
528a6d42e7dSPeter Dunlap 		/* Check API version */
529a6d42e7dSPeter Dunlap 		if (setcfg.set_cfg_vers != ISCSIT_API_VERS0) {
53007a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			rc = EINVAL;
53107a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			goto cleanup;
532a6d42e7dSPeter Dunlap 		}
533a6d42e7dSPeter Dunlap 
534a6d42e7dSPeter Dunlap 		/* Config is in packed nvlist format so unpack it */
535a6d42e7dSPeter Dunlap 		cfg_pnvlist = kmem_alloc(setcfg.set_cfg_pnvlist_len,
536a6d42e7dSPeter Dunlap 		    KM_SLEEP);
537a6d42e7dSPeter Dunlap 		ASSERT(cfg_pnvlist != NULL);
538a6d42e7dSPeter Dunlap 
539a6d42e7dSPeter Dunlap 		if (ddi_copyin(setcfg.set_cfg_pnvlist, cfg_pnvlist,
540a6d42e7dSPeter Dunlap 		    setcfg.set_cfg_pnvlist_len, flag) != 0) {
54107a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			rc = EFAULT;
54207a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			goto cleanup;
543a6d42e7dSPeter Dunlap 		}
544a6d42e7dSPeter Dunlap 
54507a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		rc = nvlist_unpack(cfg_pnvlist, setcfg.set_cfg_pnvlist_len,
54607a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		    &cfg_nvlist, KM_SLEEP);
54707a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		if (rc != 0) {
54807a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			goto cleanup;
549a6d42e7dSPeter Dunlap 		}
550a6d42e7dSPeter Dunlap 
551a6d42e7dSPeter Dunlap 		/* Translate nvlist */
55207a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		rc = it_nv_to_config(cfg_nvlist, &cfg);
55307a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		if (rc != 0) {
554a6d42e7dSPeter Dunlap 			cmn_err(CE_WARN, "Configuration is invalid");
55507a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			goto cleanup;
556a6d42e7dSPeter Dunlap 		}
557a6d42e7dSPeter Dunlap 
558a6d42e7dSPeter Dunlap 		/* Update config */
55907a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		rc = iscsit_config_merge(cfg);
56007a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		/* FALLTHROUGH */
56107a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
56207a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States cleanup:
56307a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		if (cfg)
56407a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			it_config_free_cmn(cfg);
56507a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		if (cfg_pnvlist)
566a6d42e7dSPeter Dunlap 			kmem_free(cfg_pnvlist, setcfg.set_cfg_pnvlist_len);
567aab83bb8SJosef 'Jeff' Sipek 		nvlist_free(cfg_nvlist);
568a6d42e7dSPeter Dunlap 
569a6d42e7dSPeter Dunlap 		/*
570a6d42e7dSPeter Dunlap 		 * Now that the reconfig is complete set our state back to
571a6d42e7dSPeter Dunlap 		 * enabled.
572a6d42e7dSPeter Dunlap 		 */
5738c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_enter(&iscsit_global.global_state_mutex);
574a6d42e7dSPeter Dunlap 		iscsit_global.global_svc_state = ISE_ENABLED;
5758c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
576a6d42e7dSPeter Dunlap 		break;
577a6d42e7dSPeter Dunlap 	case ISCSIT_IOC_ENABLE_SVC: {
578a6d42e7dSPeter Dunlap 		iscsit_hostinfo_t hostinfo;
579a6d42e7dSPeter Dunlap 
580a6d42e7dSPeter Dunlap 		if (ddi_copyin((void *)argp, &hostinfo.length,
581a6d42e7dSPeter Dunlap 		    sizeof (hostinfo.length), flag) != 0) {
5828c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_enter(&iscsit_global.global_state_mutex);
583a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_DISABLED;
5848c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_exit(&iscsit_global.global_state_mutex);
585a6d42e7dSPeter Dunlap 			return (EFAULT);
586a6d42e7dSPeter Dunlap 		}
587a6d42e7dSPeter Dunlap 
588a6d42e7dSPeter Dunlap 		if (hostinfo.length > sizeof (hostinfo.fqhn))
589a6d42e7dSPeter Dunlap 			hostinfo.length = sizeof (hostinfo.fqhn);
590a6d42e7dSPeter Dunlap 
591a6d42e7dSPeter Dunlap 		if (ddi_copyin((void *)((caddr_t)argp +
592a6d42e7dSPeter Dunlap 		    sizeof (hostinfo.length)), &hostinfo.fqhn,
593a6d42e7dSPeter Dunlap 		    hostinfo.length, flag) != 0) {
5948c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_enter(&iscsit_global.global_state_mutex);
595a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_DISABLED;
5968c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 			mutex_exit(&iscsit_global.global_state_mutex);
597a6d42e7dSPeter Dunlap 			return (EFAULT);
598a6d42e7dSPeter Dunlap 		}
599a6d42e7dSPeter Dunlap 
600a6d42e7dSPeter Dunlap 		idmrc = iscsit_enable_svc(&hostinfo);
6018c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_enter(&iscsit_global.global_state_mutex);
602a6d42e7dSPeter Dunlap 		if (idmrc == IDM_STATUS_SUCCESS) {
603a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_ENABLED;
604a6d42e7dSPeter Dunlap 		} else {
605a6d42e7dSPeter Dunlap 			rc = EIO;
606a6d42e7dSPeter Dunlap 			iscsit_global.global_svc_state = ISE_DISABLED;
607a6d42e7dSPeter Dunlap 		}
6088c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
609a6d42e7dSPeter Dunlap 		break;
610a6d42e7dSPeter Dunlap 	}
611a6d42e7dSPeter Dunlap 	case ISCSIT_IOC_DISABLE_SVC:
612a6d42e7dSPeter Dunlap 		iscsit_disable_svc();
6138c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_enter(&iscsit_global.global_state_mutex);
614a6d42e7dSPeter Dunlap 		iscsit_global.global_svc_state = ISE_DISABLED;
6158c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
616a6d42e7dSPeter Dunlap 		break;
61707a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
618a6d42e7dSPeter Dunlap 	default:
619a6d42e7dSPeter Dunlap 		rc = EINVAL;
6208c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_enter(&iscsit_global.global_state_mutex);
62107a2bfd1SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		iscsit_global.global_svc_state = ISE_ENABLED;
6228c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
623a6d42e7dSPeter Dunlap 	}
624a6d42e7dSPeter Dunlap 
625a6d42e7dSPeter Dunlap 	return (rc);
626a6d42e7dSPeter Dunlap }
627a6d42e7dSPeter Dunlap 
628a6d42e7dSPeter Dunlap static idm_status_t
629a6d42e7dSPeter Dunlap iscsit_init(dev_info_t *dip)
630a6d42e7dSPeter Dunlap {
631a6d42e7dSPeter Dunlap 	int			rc;
632a6d42e7dSPeter Dunlap 
633a6d42e7dSPeter Dunlap 	rc = ldi_ident_from_dip(dip, &iscsit_global.global_li);
634a6d42e7dSPeter Dunlap 	ASSERT(rc == 0);  /* Failure indicates invalid argument */
635a6d42e7dSPeter Dunlap 
636a6d42e7dSPeter Dunlap 	iscsit_global.global_svc_state = ISE_DISABLED;
637a6d42e7dSPeter Dunlap 
638a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
639a6d42e7dSPeter Dunlap }
640a6d42e7dSPeter Dunlap 
641a6d42e7dSPeter Dunlap /*
642a6d42e7dSPeter Dunlap  * iscsit_enable_svc
643a6d42e7dSPeter Dunlap  *
644a6d42e7dSPeter Dunlap  * registers all the configured targets and target portals with STMF
645a6d42e7dSPeter Dunlap  */
646a6d42e7dSPeter Dunlap static idm_status_t
647a6d42e7dSPeter Dunlap iscsit_enable_svc(iscsit_hostinfo_t *hostinfo)
648a6d42e7dSPeter Dunlap {
649a6d42e7dSPeter Dunlap 	stmf_port_provider_t	*pp;
650a6d42e7dSPeter Dunlap 	stmf_dbuf_store_t	*dbuf_store;
651a6d42e7dSPeter Dunlap 	boolean_t		did_iscsit_isns_init;
652a6d42e7dSPeter Dunlap 	idm_status_t		retval = IDM_STATUS_SUCCESS;
653a6d42e7dSPeter Dunlap 
654a6d42e7dSPeter Dunlap 	ASSERT(iscsit_global.global_svc_state == ISE_ENABLING);
655a6d42e7dSPeter Dunlap 
656a6d42e7dSPeter Dunlap 	/*
657a6d42e7dSPeter Dunlap 	 * Make sure that can tell if we have partially allocated
658a6d42e7dSPeter Dunlap 	 * in case we need to exit and tear down anything allocated.
659a6d42e7dSPeter Dunlap 	 */
660a6d42e7dSPeter Dunlap 	iscsit_global.global_tsih_pool = NULL;
661a6d42e7dSPeter Dunlap 	iscsit_global.global_dbuf_store = NULL;
662a6d42e7dSPeter Dunlap 	iscsit_status_pdu_cache = NULL;
663a6d42e7dSPeter Dunlap 	pp = NULL;
664a6d42e7dSPeter Dunlap 	iscsit_global.global_pp = NULL;
665a6d42e7dSPeter Dunlap 	iscsit_global.global_default_tpg = NULL;
666a6d42e7dSPeter Dunlap 	did_iscsit_isns_init = B_FALSE;
667a6d42e7dSPeter Dunlap 	iscsit_global.global_dispatch_taskq = NULL;
668a6d42e7dSPeter Dunlap 
669a6d42e7dSPeter Dunlap 	/* Setup remaining fields in iscsit_global_t */
670a6d42e7dSPeter Dunlap 	idm_refcnt_init(&iscsit_global.global_refcnt,
671a6d42e7dSPeter Dunlap 	    &iscsit_global);
672a6d42e7dSPeter Dunlap 
673a6d42e7dSPeter Dunlap 	avl_create(&iscsit_global.global_discovery_sessions,
674a6d42e7dSPeter Dunlap 	    iscsit_sess_avl_compare, sizeof (iscsit_sess_t),
675a6d42e7dSPeter Dunlap 	    offsetof(iscsit_sess_t, ist_tgt_ln));
676a6d42e7dSPeter Dunlap 
677a6d42e7dSPeter Dunlap 	avl_create(&iscsit_global.global_target_list,
678a6d42e7dSPeter Dunlap 	    iscsit_tgt_avl_compare, sizeof (iscsit_tgt_t),
679a6d42e7dSPeter Dunlap 	    offsetof(iscsit_tgt_t, target_global_ln));
680a6d42e7dSPeter Dunlap 
681a6d42e7dSPeter Dunlap 	list_create(&iscsit_global.global_deleted_target_list,
682a6d42e7dSPeter Dunlap 	    sizeof (iscsit_tgt_t),
683a6d42e7dSPeter Dunlap 	    offsetof(iscsit_tgt_t, target_global_deleted_ln));
684a6d42e7dSPeter Dunlap 
685a6d42e7dSPeter Dunlap 	avl_create(&iscsit_global.global_tpg_list,
686a6d42e7dSPeter Dunlap 	    iscsit_tpg_avl_compare, sizeof (iscsit_tpg_t),
687a6d42e7dSPeter Dunlap 	    offsetof(iscsit_tpg_t, tpg_global_ln));
688a6d42e7dSPeter Dunlap 
689a6d42e7dSPeter Dunlap 	avl_create(&iscsit_global.global_ini_list,
690a6d42e7dSPeter Dunlap 	    iscsit_ini_avl_compare, sizeof (iscsit_ini_t),
691a6d42e7dSPeter Dunlap 	    offsetof(iscsit_ini_t, ini_global_ln));
692a6d42e7dSPeter Dunlap 
693a6d42e7dSPeter Dunlap 	iscsit_global.global_tsih_pool = vmem_create("iscsit_tsih_pool",
694a6d42e7dSPeter Dunlap 	    (void *)1, ISCSI_MAX_TSIH, 1, NULL, NULL, NULL, 0,
695a6d42e7dSPeter Dunlap 	    VM_SLEEP | VMC_IDENTIFIER);
696a6d42e7dSPeter Dunlap 
697a6d42e7dSPeter Dunlap 	/*
698a6d42e7dSPeter Dunlap 	 * Setup STMF dbuf store.  Our buffers are bound to a specific
699a6d42e7dSPeter Dunlap 	 * connection so we really can't let STMF cache buffers for us.
700a6d42e7dSPeter Dunlap 	 * Consequently we'll just allocate one global buffer store.
701a6d42e7dSPeter Dunlap 	 */
702a6d42e7dSPeter Dunlap 	dbuf_store = stmf_alloc(STMF_STRUCT_DBUF_STORE, 0, 0);
703a6d42e7dSPeter Dunlap 	if (dbuf_store == NULL) {
704a6d42e7dSPeter Dunlap 		retval = IDM_STATUS_FAIL;
705a6d42e7dSPeter Dunlap 		goto tear_down_and_return;
706a6d42e7dSPeter Dunlap 	}
707a6d42e7dSPeter Dunlap 	dbuf_store->ds_alloc_data_buf = iscsit_dbuf_alloc;
708a6d42e7dSPeter Dunlap 	dbuf_store->ds_free_data_buf = iscsit_dbuf_free;
709a6d42e7dSPeter Dunlap 	dbuf_store->ds_port_private = NULL;
710a6d42e7dSPeter Dunlap 	iscsit_global.global_dbuf_store = dbuf_store;
711a6d42e7dSPeter Dunlap 
712a6d42e7dSPeter Dunlap 	/* Status PDU cache */
713a6d42e7dSPeter Dunlap 	iscsit_status_pdu_cache = kmem_cache_create("iscsit_status_pdu_cache",
714a6d42e7dSPeter Dunlap 	    sizeof (idm_pdu_t) + sizeof (iscsi_scsi_rsp_hdr_t), 8,
715a6d42e7dSPeter Dunlap 	    &iscsit_status_pdu_constructor,
716a6d42e7dSPeter Dunlap 	    NULL, NULL, NULL, NULL, KM_SLEEP);
717a6d42e7dSPeter Dunlap 
718a6d42e7dSPeter Dunlap 	/* Default TPG and portal */
719a6d42e7dSPeter Dunlap 	iscsit_global.global_default_tpg = iscsit_tpg_createdefault();
720a6d42e7dSPeter Dunlap 	if (iscsit_global.global_default_tpg == NULL) {
721a6d42e7dSPeter Dunlap 		retval = IDM_STATUS_FAIL;
722a6d42e7dSPeter Dunlap 		goto tear_down_and_return;
723a6d42e7dSPeter Dunlap 	}
724a6d42e7dSPeter Dunlap 
725a6d42e7dSPeter Dunlap 	/* initialize isns client */
726a6d42e7dSPeter Dunlap 	(void) iscsit_isns_init(hostinfo);
727a6d42e7dSPeter Dunlap 	did_iscsit_isns_init = B_TRUE;
728a6d42e7dSPeter Dunlap 
729a6d42e7dSPeter Dunlap 	/* Register port provider */
730a6d42e7dSPeter Dunlap 	pp = stmf_alloc(STMF_STRUCT_PORT_PROVIDER, 0, 0);
731a6d42e7dSPeter Dunlap 	if (pp == NULL) {
732a6d42e7dSPeter Dunlap 		retval = IDM_STATUS_FAIL;
733a6d42e7dSPeter Dunlap 		goto tear_down_and_return;
734a6d42e7dSPeter Dunlap 	}
735a6d42e7dSPeter Dunlap 
736a6d42e7dSPeter Dunlap 	pp->pp_portif_rev = PORTIF_REV_1;
737a6d42e7dSPeter Dunlap 	pp->pp_instance = 0;
738a6d42e7dSPeter Dunlap 	pp->pp_name = ISCSIT_MODNAME;
739a6d42e7dSPeter Dunlap 	pp->pp_cb = iscsit_pp_cb;
740a6d42e7dSPeter Dunlap 
741a6d42e7dSPeter Dunlap 	iscsit_global.global_pp = pp;
742a6d42e7dSPeter Dunlap 
743a6d42e7dSPeter Dunlap 
744a6d42e7dSPeter Dunlap 	if (stmf_register_port_provider(pp) != STMF_SUCCESS) {
745a6d42e7dSPeter Dunlap 		retval = IDM_STATUS_FAIL;
746a6d42e7dSPeter Dunlap 		goto tear_down_and_return;
747a6d42e7dSPeter Dunlap 	}
748a6d42e7dSPeter Dunlap 
749a6d42e7dSPeter Dunlap 	iscsit_global.global_dispatch_taskq = taskq_create("iscsit_dispatch",
750a6d42e7dSPeter Dunlap 	    1, minclsyspri, 16, 16, TASKQ_PREPOPULATE);
751a6d42e7dSPeter Dunlap 
752d618d68dSPriya Krishnan 	/* Scan staged PDUs, meaningful in MC/S situations */
753d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_start();
754d618d68dSPriya Krishnan 
755a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
756a6d42e7dSPeter Dunlap 
757a6d42e7dSPeter Dunlap tear_down_and_return:
758a6d42e7dSPeter Dunlap 
759a6d42e7dSPeter Dunlap 	if (iscsit_global.global_dispatch_taskq) {
760a6d42e7dSPeter Dunlap 		taskq_destroy(iscsit_global.global_dispatch_taskq);
761a6d42e7dSPeter Dunlap 		iscsit_global.global_dispatch_taskq = NULL;
762a6d42e7dSPeter Dunlap 	}
763a6d42e7dSPeter Dunlap 
764a6d42e7dSPeter Dunlap 	if (did_iscsit_isns_init)
765a6d42e7dSPeter Dunlap 		iscsit_isns_fini();
766a6d42e7dSPeter Dunlap 
767a6d42e7dSPeter Dunlap 	if (iscsit_global.global_default_tpg) {
768a6d42e7dSPeter Dunlap 		iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
769a6d42e7dSPeter Dunlap 		iscsit_global.global_default_tpg = NULL;
770a6d42e7dSPeter Dunlap 	}
771a6d42e7dSPeter Dunlap 
772a6d42e7dSPeter Dunlap 	if (iscsit_global.global_pp)
773a6d42e7dSPeter Dunlap 		iscsit_global.global_pp = NULL;
774a6d42e7dSPeter Dunlap 
775a6d42e7dSPeter Dunlap 	if (pp)
776a6d42e7dSPeter Dunlap 		stmf_free(pp);
777a6d42e7dSPeter Dunlap 
778a6d42e7dSPeter Dunlap 	if (iscsit_status_pdu_cache) {
779a6d42e7dSPeter Dunlap 		kmem_cache_destroy(iscsit_status_pdu_cache);
780a6d42e7dSPeter Dunlap 		iscsit_status_pdu_cache = NULL;
781a6d42e7dSPeter Dunlap 	}
782a6d42e7dSPeter Dunlap 
783a6d42e7dSPeter Dunlap 	if (iscsit_global.global_dbuf_store) {
784a6d42e7dSPeter Dunlap 		stmf_free(iscsit_global.global_dbuf_store);
785a6d42e7dSPeter Dunlap 		iscsit_global.global_dbuf_store = NULL;
786a6d42e7dSPeter Dunlap 	}
787a6d42e7dSPeter Dunlap 
788a6d42e7dSPeter Dunlap 	if (iscsit_global.global_tsih_pool) {
789a6d42e7dSPeter Dunlap 		vmem_destroy(iscsit_global.global_tsih_pool);
790a6d42e7dSPeter Dunlap 		iscsit_global.global_tsih_pool = NULL;
791a6d42e7dSPeter Dunlap 	}
792a6d42e7dSPeter Dunlap 
793a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_ini_list);
794a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_tpg_list);
795a6d42e7dSPeter Dunlap 	list_destroy(&iscsit_global.global_deleted_target_list);
796a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_target_list);
797a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_discovery_sessions);
798a6d42e7dSPeter Dunlap 
799a6d42e7dSPeter Dunlap 	idm_refcnt_destroy(&iscsit_global.global_refcnt);
800a6d42e7dSPeter Dunlap 
801a6d42e7dSPeter Dunlap 	return (retval);
802a6d42e7dSPeter Dunlap }
803a6d42e7dSPeter Dunlap 
804a6d42e7dSPeter Dunlap /*
805a6d42e7dSPeter Dunlap  * iscsit_disable_svc
806a6d42e7dSPeter Dunlap  *
807a6d42e7dSPeter Dunlap  * clean up all existing connections and deregister targets from STMF
808a6d42e7dSPeter Dunlap  */
809a6d42e7dSPeter Dunlap static void
810a6d42e7dSPeter Dunlap iscsit_disable_svc(void)
811a6d42e7dSPeter Dunlap {
812a6d42e7dSPeter Dunlap 	iscsit_sess_t	*sess;
813a6d42e7dSPeter Dunlap 
814a6d42e7dSPeter Dunlap 	ASSERT(iscsit_global.global_svc_state == ISE_DISABLING);
815a6d42e7dSPeter Dunlap 
816d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_stop();
817d618d68dSPriya Krishnan 
818a6d42e7dSPeter Dunlap 	/* tear down discovery sessions */
819a6d42e7dSPeter Dunlap 	for (sess = avl_first(&iscsit_global.global_discovery_sessions);
820a6d42e7dSPeter Dunlap 	    sess != NULL;
821a6d42e7dSPeter Dunlap 	    sess = AVL_NEXT(&iscsit_global.global_discovery_sessions, sess))
822a6d42e7dSPeter Dunlap 		iscsit_sess_close(sess);
823a6d42e7dSPeter Dunlap 
824a6d42e7dSPeter Dunlap 	/*
825a6d42e7dSPeter Dunlap 	 * Passing NULL to iscsit_config_merge tells it to go to an empty
826a6d42e7dSPeter Dunlap 	 * config.
827a6d42e7dSPeter Dunlap 	 */
828a6d42e7dSPeter Dunlap 	(void) iscsit_config_merge(NULL);
829a6d42e7dSPeter Dunlap 
830a6d42e7dSPeter Dunlap 	/*
831a6d42e7dSPeter Dunlap 	 * Wait until there are no more global references
832a6d42e7dSPeter Dunlap 	 */
833a6d42e7dSPeter Dunlap 	idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
834a6d42e7dSPeter Dunlap 	idm_refcnt_destroy(&iscsit_global.global_refcnt);
835a6d42e7dSPeter Dunlap 
836a6d42e7dSPeter Dunlap 	/*
837a6d42e7dSPeter Dunlap 	 * Default TPG must be destroyed after global_refcnt is 0.
838a6d42e7dSPeter Dunlap 	 */
839a6d42e7dSPeter Dunlap 	iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
840a6d42e7dSPeter Dunlap 
841a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_discovery_sessions);
842a6d42e7dSPeter Dunlap 	list_destroy(&iscsit_global.global_deleted_target_list);
843a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_target_list);
844a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_tpg_list);
845a6d42e7dSPeter Dunlap 	avl_destroy(&iscsit_global.global_ini_list);
846a6d42e7dSPeter Dunlap 
847a6d42e7dSPeter Dunlap 	taskq_destroy(iscsit_global.global_dispatch_taskq);
848a6d42e7dSPeter Dunlap 
849a6d42e7dSPeter Dunlap 	iscsit_isns_fini();
850a6d42e7dSPeter Dunlap 
851a6d42e7dSPeter Dunlap 	stmf_free(iscsit_global.global_dbuf_store);
852a6d42e7dSPeter Dunlap 	iscsit_global.global_dbuf_store = NULL;
853a6d42e7dSPeter Dunlap 
854a6d42e7dSPeter Dunlap 	(void) stmf_deregister_port_provider(iscsit_global.global_pp);
855a6d42e7dSPeter Dunlap 	stmf_free(iscsit_global.global_pp);
856a6d42e7dSPeter Dunlap 	iscsit_global.global_pp = NULL;
857a6d42e7dSPeter Dunlap 
858a6d42e7dSPeter Dunlap 	kmem_cache_destroy(iscsit_status_pdu_cache);
859a6d42e7dSPeter Dunlap 	iscsit_status_pdu_cache = NULL;
860a6d42e7dSPeter Dunlap 
861a6d42e7dSPeter Dunlap 	vmem_destroy(iscsit_global.global_tsih_pool);
862a6d42e7dSPeter Dunlap 	iscsit_global.global_tsih_pool = NULL;
863a6d42e7dSPeter Dunlap }
864a6d42e7dSPeter Dunlap 
865a6d42e7dSPeter Dunlap void
866a6d42e7dSPeter Dunlap iscsit_global_hold()
867a6d42e7dSPeter Dunlap {
8688c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	/*
8698c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * To take out a global hold, we must either own the global
8708c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * state mutex or we must be running inside of an ioctl that
8718c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * has set the global state to ISE_BUSY, ISE_DISABLING, or
8728c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * ISE_ENABLING.  We don't track the "owner" for these flags,
8738c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 * so just checking if they are set is enough for now.
8748c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	 */
8758c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	ASSERT((iscsit_global.global_svc_state == ISE_ENABLING) ||
8768c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	    (iscsit_global.global_svc_state == ISE_DISABLING) ||
8778c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	    (iscsit_global.global_svc_state == ISE_BUSY) ||
8788c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	    MUTEX_HELD(&iscsit_global.global_state_mutex));
8798c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
880a6d42e7dSPeter Dunlap 	idm_refcnt_hold(&iscsit_global.global_refcnt);
881a6d42e7dSPeter Dunlap }
882a6d42e7dSPeter Dunlap 
883a6d42e7dSPeter Dunlap void
884a6d42e7dSPeter Dunlap iscsit_global_rele()
885a6d42e7dSPeter Dunlap {
886a6d42e7dSPeter Dunlap 	idm_refcnt_rele(&iscsit_global.global_refcnt);
887a6d42e7dSPeter Dunlap }
888a6d42e7dSPeter Dunlap 
889a6d42e7dSPeter Dunlap void
890a6d42e7dSPeter Dunlap iscsit_global_wait_ref()
891a6d42e7dSPeter Dunlap {
892a6d42e7dSPeter Dunlap 	idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
893a6d42e7dSPeter Dunlap }
894a6d42e7dSPeter Dunlap 
895a6d42e7dSPeter Dunlap /*
896a6d42e7dSPeter Dunlap  * IDM callbacks
897a6d42e7dSPeter Dunlap  */
898a6d42e7dSPeter Dunlap 
899a6d42e7dSPeter Dunlap /*ARGSUSED*/
900a6d42e7dSPeter Dunlap void
901a6d42e7dSPeter Dunlap iscsit_rx_pdu(idm_conn_t *ic, idm_pdu_t *rx_pdu)
902a6d42e7dSPeter Dunlap {
903a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = ic->ic_handle;
904a6d42e7dSPeter Dunlap 	switch (IDM_PDU_OPCODE(rx_pdu)) {
905a6d42e7dSPeter Dunlap 	case ISCSI_OP_SCSI_CMD:
906a6d42e7dSPeter Dunlap 		ASSERT(0); /* Shouldn't happen */
907a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
908a6d42e7dSPeter Dunlap 		break;
909a6d42e7dSPeter Dunlap 	case ISCSI_OP_SNACK_CMD:
910a6d42e7dSPeter Dunlap 		/*
911a6d42e7dSPeter Dunlap 		 * We'll need to handle this when we support ERL1/2.  For
912a6d42e7dSPeter Dunlap 		 * now we treat it as a protocol error.
913a6d42e7dSPeter Dunlap 		 */
914a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
915*1f99504aSToomas Soome 		idm_conn_event(ic, CE_TRANSPORT_FAIL, 0);
916a6d42e7dSPeter Dunlap 		break;
917a6d42e7dSPeter Dunlap 	case ISCSI_OP_SCSI_TASK_MGT_MSG:
918d618d68dSPriya Krishnan 		if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
919d618d68dSPriya Krishnan 			iscsit_set_cmdsn(ict, rx_pdu);
920d618d68dSPriya Krishnan 			iscsit_op_scsi_task_mgmt(ict, rx_pdu);
921d618d68dSPriya Krishnan 		}
922a6d42e7dSPeter Dunlap 		break;
923a6d42e7dSPeter Dunlap 	case ISCSI_OP_NOOP_OUT:
924a6d42e7dSPeter Dunlap 	case ISCSI_OP_LOGIN_CMD:
925a6d42e7dSPeter Dunlap 	case ISCSI_OP_TEXT_CMD:
926a6d42e7dSPeter Dunlap 	case ISCSI_OP_LOGOUT_CMD:
927a6d42e7dSPeter Dunlap 		/*
928a6d42e7dSPeter Dunlap 		 * If/when we switch to userland processing these PDU's
929a6d42e7dSPeter Dunlap 		 * will be handled by iscsitd.
930a6d42e7dSPeter Dunlap 		 */
931a6d42e7dSPeter Dunlap 		iscsit_deferred_dispatch(rx_pdu);
932a6d42e7dSPeter Dunlap 		break;
933a6d42e7dSPeter Dunlap 	default:
934a6d42e7dSPeter Dunlap 		/* Protocol error */
935a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
936*1f99504aSToomas Soome 		idm_conn_event(ic, CE_TRANSPORT_FAIL, 0);
937a6d42e7dSPeter Dunlap 		break;
938a6d42e7dSPeter Dunlap 	}
939a6d42e7dSPeter Dunlap }
940a6d42e7dSPeter Dunlap 
941a6d42e7dSPeter Dunlap /*ARGSUSED*/
942a6d42e7dSPeter Dunlap void
943a6d42e7dSPeter Dunlap iscsit_rx_pdu_error(idm_conn_t *ic, idm_pdu_t *rx_pdu, idm_status_t status)
944a6d42e7dSPeter Dunlap {
945a6d42e7dSPeter Dunlap 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
946a6d42e7dSPeter Dunlap }
947a6d42e7dSPeter Dunlap 
948a481fa48SRick McNeal /*
949a481fa48SRick McNeal  * iscsit_rx_scsi_rsp -- cause the connection to be closed if response rx'd
950a481fa48SRick McNeal  *
951a481fa48SRick McNeal  * A target sends an SCSI Response PDU, it should never receive one.
952a481fa48SRick McNeal  * This has been seen when running the Codemonicon suite of tests which
953a481fa48SRick McNeal  * does negative testing of the protocol. If such a condition occurs using
954a481fa48SRick McNeal  * a normal initiator it most likely means there's data corruption in the
955a481fa48SRick McNeal  * header and that's grounds for dropping the connection as well.
956a481fa48SRick McNeal  */
957a481fa48SRick McNeal void
958a481fa48SRick McNeal iscsit_rx_scsi_rsp(idm_conn_t *ic, idm_pdu_t *rx_pdu)
959a481fa48SRick McNeal {
960a481fa48SRick McNeal 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
961*1f99504aSToomas Soome 	idm_conn_event(ic, CE_TRANSPORT_FAIL, 0);
962a481fa48SRick McNeal }
963a481fa48SRick McNeal 
964a6d42e7dSPeter Dunlap void
965a6d42e7dSPeter Dunlap iscsit_task_aborted(idm_task_t *idt, idm_status_t status)
966a6d42e7dSPeter Dunlap {
967a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = idt->idt_private;
968a6d42e7dSPeter Dunlap 
969a6d42e7dSPeter Dunlap 	switch (status) {
970a6d42e7dSPeter Dunlap 	case IDM_STATUS_SUSPENDED:
971a6d42e7dSPeter Dunlap 		break;
972a6d42e7dSPeter Dunlap 	case IDM_STATUS_ABORTED:
973a6d42e7dSPeter Dunlap 		mutex_enter(&itask->it_mutex);
974a6d42e7dSPeter Dunlap 		itask->it_aborted = B_TRUE;
975bf604c64SPeter Dunlap 		/*
976bf604c64SPeter Dunlap 		 * We rely on the fact that STMF tracks outstanding
977bf604c64SPeter Dunlap 		 * buffer transfers and will free all of our buffers
978bf604c64SPeter Dunlap 		 * before freeing the task so we don't need to
979bf604c64SPeter Dunlap 		 * explicitly free the buffers from iscsit/idm
980bf604c64SPeter Dunlap 		 */
981a6d42e7dSPeter Dunlap 		if (itask->it_stmf_abort) {
982a6d42e7dSPeter Dunlap 			mutex_exit(&itask->it_mutex);
98372cf3143Speter dunlap 			/*
98472cf3143Speter dunlap 			 * Task is no longer active
98572cf3143Speter dunlap 			 */
98672cf3143Speter dunlap 			iscsit_task_done(itask);
98772cf3143Speter dunlap 
988a6d42e7dSPeter Dunlap 			/*
989bf604c64SPeter Dunlap 			 * STMF has already asked for this task to be aborted
990bf604c64SPeter Dunlap 			 *
991a6d42e7dSPeter Dunlap 			 * STMF specification is wrong... says to return
992a6d42e7dSPeter Dunlap 			 * STMF_ABORTED, the code actually looks for
993a6d42e7dSPeter Dunlap 			 * STMF_ABORT_SUCCESS.
994a6d42e7dSPeter Dunlap 			 */
995a6d42e7dSPeter Dunlap 			stmf_task_lport_aborted(itask->it_stmf_task,
996a6d42e7dSPeter Dunlap 			    STMF_ABORT_SUCCESS, STMF_IOF_LPORT_DONE);
997a6d42e7dSPeter Dunlap 			return;
998a6d42e7dSPeter Dunlap 		} else {
999a6d42e7dSPeter Dunlap 			mutex_exit(&itask->it_mutex);
1000a6d42e7dSPeter Dunlap 			/*
1001bf604c64SPeter Dunlap 			 * Tell STMF to stop processing the task.
1002a6d42e7dSPeter Dunlap 			 */
1003a6d42e7dSPeter Dunlap 			stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
1004a6d42e7dSPeter Dunlap 			    STMF_ABORTED, NULL);
1005a6d42e7dSPeter Dunlap 			return;
1006a6d42e7dSPeter Dunlap 		}
1007a6d42e7dSPeter Dunlap 		/*NOTREACHED*/
1008a6d42e7dSPeter Dunlap 	default:
1009a6d42e7dSPeter Dunlap 		ASSERT(0);
1010a6d42e7dSPeter Dunlap 	}
1011a6d42e7dSPeter Dunlap }
1012a6d42e7dSPeter Dunlap 
1013a6d42e7dSPeter Dunlap /*ARGSUSED*/
1014a6d42e7dSPeter Dunlap idm_status_t
1015a6d42e7dSPeter Dunlap iscsit_client_notify(idm_conn_t *ic, idm_client_notify_t icn,
1016a6d42e7dSPeter Dunlap     uintptr_t data)
1017a6d42e7dSPeter Dunlap {
1018a6d42e7dSPeter Dunlap 	idm_status_t rc = IDM_STATUS_SUCCESS;
1019a6d42e7dSPeter Dunlap 
1020a6d42e7dSPeter Dunlap 	/*
1021a6d42e7dSPeter Dunlap 	 * IDM client notifications will never occur at interrupt level
1022a6d42e7dSPeter Dunlap 	 * since they are generated from the connection state machine which
1023a6d42e7dSPeter Dunlap 	 * running on taskq threads.
1024a6d42e7dSPeter Dunlap 	 *
1025a6d42e7dSPeter Dunlap 	 */
1026a6d42e7dSPeter Dunlap 	switch (icn) {
1027a6d42e7dSPeter Dunlap 	case CN_CONNECT_ACCEPT:
1028a6d42e7dSPeter Dunlap 		rc = iscsit_conn_accept(ic); /* No data */
1029a6d42e7dSPeter Dunlap 		break;
1030a6d42e7dSPeter Dunlap 	case CN_FFP_ENABLED:
1031a6d42e7dSPeter Dunlap 		rc = iscsit_ffp_enabled(ic); /* No data */
1032a6d42e7dSPeter Dunlap 		break;
1033a6d42e7dSPeter Dunlap 	case CN_FFP_DISABLED:
1034a6d42e7dSPeter Dunlap 		/*
1035a6d42e7dSPeter Dunlap 		 * Data indicates whether this was the result of an
1036a6d42e7dSPeter Dunlap 		 * explicit logout request.
1037a6d42e7dSPeter Dunlap 		 */
1038a6d42e7dSPeter Dunlap 		rc = iscsit_ffp_disabled(ic, (idm_ffp_disable_t)data);
1039a6d42e7dSPeter Dunlap 		break;
1040a6d42e7dSPeter Dunlap 	case CN_CONNECT_LOST:
1041a6d42e7dSPeter Dunlap 		rc = iscsit_conn_lost(ic);
1042a6d42e7dSPeter Dunlap 		break;
1043a6d42e7dSPeter Dunlap 	case CN_CONNECT_DESTROY:
1044a6d42e7dSPeter Dunlap 		rc = iscsit_conn_destroy(ic);
1045a6d42e7dSPeter Dunlap 		break;
1046a6d42e7dSPeter Dunlap 	case CN_LOGIN_FAIL:
1047a6d42e7dSPeter Dunlap 		/*
1048a6d42e7dSPeter Dunlap 		 * Force the login state machine to completion
1049a6d42e7dSPeter Dunlap 		 */
1050a6d42e7dSPeter Dunlap 		rc = iscsit_login_fail(ic);
1051a6d42e7dSPeter Dunlap 		break;
1052a6d42e7dSPeter Dunlap 	default:
1053a6d42e7dSPeter Dunlap 		rc = IDM_STATUS_REJECT;
1054a6d42e7dSPeter Dunlap 		break;
1055a6d42e7dSPeter Dunlap 	}
1056a6d42e7dSPeter Dunlap 
1057a6d42e7dSPeter Dunlap 	return (rc);
1058a6d42e7dSPeter Dunlap }
1059a6d42e7dSPeter Dunlap 
106060220f10SPriya Krishnan /*
106160220f10SPriya Krishnan  * iscsit_update_statsn is invoked for all the PDUs which have the StatSN
106260220f10SPriya Krishnan  * field in the header. The StatSN is incremented if the IDM_PDU_ADVANCE_STATSN
106360220f10SPriya Krishnan  * flag is set in the pdu flags field. The StatSN is connection-wide and is
106460220f10SPriya Krishnan  * protected by the mutex ict_statsn_mutex. For Data-In PDUs, if the flag
106560220f10SPriya Krishnan  * IDM_TASK_PHASECOLLAPSE_REQ is set, the status (phase-collapse) is also filled
106660220f10SPriya Krishnan  */
106760220f10SPriya Krishnan void
106860220f10SPriya Krishnan iscsit_update_statsn(idm_task_t *idm_task, idm_pdu_t *pdu)
106960220f10SPriya Krishnan {
107060220f10SPriya Krishnan 	iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
107160220f10SPriya Krishnan 	iscsit_conn_t *ict = (iscsit_conn_t *)pdu->isp_ic->ic_handle;
107260220f10SPriya Krishnan 	iscsit_task_t *itask = NULL;
107360220f10SPriya Krishnan 	scsi_task_t *task = NULL;
107460220f10SPriya Krishnan 
107560220f10SPriya Krishnan 	mutex_enter(&ict->ict_statsn_mutex);
107660220f10SPriya Krishnan 	rsp->statsn = htonl(ict->ict_statsn);
107760220f10SPriya Krishnan 	if (pdu->isp_flags & IDM_PDU_ADVANCE_STATSN)
107860220f10SPriya Krishnan 		ict->ict_statsn++;
107960220f10SPriya Krishnan 	mutex_exit(&ict->ict_statsn_mutex);
108060220f10SPriya Krishnan 
108160220f10SPriya Krishnan 	/*
108260220f10SPriya Krishnan 	 * The last SCSI Data PDU passed for a command may also contain the
108360220f10SPriya Krishnan 	 * status if the status indicates termination with no expections, i.e.
108460220f10SPriya Krishnan 	 * no sense data or response involved. If the command completes with
108560220f10SPriya Krishnan 	 * an error, then the response and sense data will be sent in a
108660220f10SPriya Krishnan 	 * separate iSCSI Response PDU.
108760220f10SPriya Krishnan 	 */
108860220f10SPriya Krishnan 	if ((idm_task) && (idm_task->idt_flags & IDM_TASK_PHASECOLLAPSE_REQ)) {
108960220f10SPriya Krishnan 		itask = idm_task->idt_private;
109060220f10SPriya Krishnan 		task = itask->it_stmf_task;
109160220f10SPriya Krishnan 
109260220f10SPriya Krishnan 		rsp->cmd_status = task->task_scsi_status;
109360220f10SPriya Krishnan 		rsp->flags	|= ISCSI_FLAG_DATA_STATUS;
109460220f10SPriya Krishnan 		if (task->task_status_ctrl & TASK_SCTRL_OVER) {
109560220f10SPriya Krishnan 			rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
109660220f10SPriya Krishnan 		} else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
109760220f10SPriya Krishnan 			rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
109860220f10SPriya Krishnan 		}
109960220f10SPriya Krishnan 		rsp->residual_count = htonl(task->task_resid);
1100a2383ac5SPriya Krishnan 
1101a2383ac5SPriya Krishnan 		/*
1102a2383ac5SPriya Krishnan 		 * Removing the task from the session task list
1103a2383ac5SPriya Krishnan 		 * just before the status is sent in the last
1104a2383ac5SPriya Krishnan 		 * Data PDU transfer
1105a2383ac5SPriya Krishnan 		 */
1106a2383ac5SPriya Krishnan 		iscsit_task_done(itask);
110760220f10SPriya Krishnan 	}
110860220f10SPriya Krishnan }
1109a6d42e7dSPeter Dunlap 
1110a6d42e7dSPeter Dunlap void
1111a6d42e7dSPeter Dunlap iscsit_build_hdr(idm_task_t *idm_task, idm_pdu_t *pdu, uint8_t opcode)
1112a6d42e7dSPeter Dunlap {
1113a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = idm_task->idt_private;
1114a6d42e7dSPeter Dunlap 	iscsi_data_rsp_hdr_t *dh = (iscsi_data_rsp_hdr_t *)pdu->isp_hdr;
1115a6d42e7dSPeter Dunlap 
1116a6d42e7dSPeter Dunlap 	/*
1117d618d68dSPriya Krishnan 	 * We acquired iscsit_sess_t.ist_sn_mutex in iscsit_xfer_scsi_data
1118a6d42e7dSPeter Dunlap 	 */
1119d618d68dSPriya Krishnan 	ASSERT(MUTEX_HELD(&itask->it_ict->ict_sess->ist_sn_mutex));
1120a6d42e7dSPeter Dunlap 	/*
112174f49fd4SPriya Krishnan 	 * On incoming data, the target transfer tag and Lun is only
112274f49fd4SPriya Krishnan 	 * provided by the target if the A bit is set, Since the target
112374f49fd4SPriya Krishnan 	 * does not currently support Error Recovery Level 1, the A
112474f49fd4SPriya Krishnan 	 * bit is never set.
1125a6d42e7dSPeter Dunlap 	 */
1126a6d42e7dSPeter Dunlap 	dh->opcode = opcode;
1127a6d42e7dSPeter Dunlap 	dh->itt = itask->it_itt;
112874f49fd4SPriya Krishnan 	dh->ttt = ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_SCSI_DATA_RSP) ?
112974f49fd4SPriya Krishnan 	    ISCSI_RSVD_TASK_TAG : itask->it_ttt;
113060220f10SPriya Krishnan 
1131a6d42e7dSPeter Dunlap 	dh->expcmdsn = htonl(itask->it_ict->ict_sess->ist_expcmdsn);
1132a6d42e7dSPeter Dunlap 	dh->maxcmdsn = htonl(itask->it_ict->ict_sess->ist_maxcmdsn);
1133a6d42e7dSPeter Dunlap 
1134a6d42e7dSPeter Dunlap 	/*
1135a6d42e7dSPeter Dunlap 	 * IDM must set:
1136a6d42e7dSPeter Dunlap 	 *
1137a6d42e7dSPeter Dunlap 	 * data.flags and rtt.flags
1138a6d42e7dSPeter Dunlap 	 * data.dlength
1139a6d42e7dSPeter Dunlap 	 * data.datasn
1140a6d42e7dSPeter Dunlap 	 * data.offset
114160220f10SPriya Krishnan 	 * statsn, residual_count and cmd_status (for phase collapse)
1142a6d42e7dSPeter Dunlap 	 * rtt.rttsn
1143a6d42e7dSPeter Dunlap 	 * rtt.data_offset
1144a6d42e7dSPeter Dunlap 	 * rtt.data_length
1145a6d42e7dSPeter Dunlap 	 */
1146a6d42e7dSPeter Dunlap }
1147a6d42e7dSPeter Dunlap 
114872cf3143Speter dunlap void
114972cf3143Speter dunlap iscsit_keepalive(idm_conn_t *ic)
115072cf3143Speter dunlap {
115172cf3143Speter dunlap 	idm_pdu_t		*nop_in_pdu;
115272cf3143Speter dunlap 	iscsi_nop_in_hdr_t	*nop_in;
115372cf3143Speter dunlap 	iscsit_conn_t		*ict = ic->ic_handle;
115472cf3143Speter dunlap 
115572cf3143Speter dunlap 	/*
115672cf3143Speter dunlap 	 * IDM noticed the connection has been idle for too long so it's
115772cf3143Speter dunlap 	 * time to provoke some activity.  Build and transmit an iSCSI
115872cf3143Speter dunlap 	 * nop-in PDU -- when the initiator responds it will be counted
115972cf3143Speter dunlap 	 * as "activity" and keep the connection alive.
116072cf3143Speter dunlap 	 *
116172cf3143Speter dunlap 	 * We don't actually care about the response here at the iscsit level
116272cf3143Speter dunlap 	 * so we will just throw it away without looking at it when it arrives.
116372cf3143Speter dunlap 	 */
116472cf3143Speter dunlap 	nop_in_pdu = idm_pdu_alloc(sizeof (*nop_in), 0);
116572cf3143Speter dunlap 	idm_pdu_init(nop_in_pdu, ic, NULL, NULL);
116672cf3143Speter dunlap 	nop_in = (iscsi_nop_in_hdr_t *)nop_in_pdu->isp_hdr;
116772cf3143Speter dunlap 	bzero(nop_in, sizeof (*nop_in));
116872cf3143Speter dunlap 	nop_in->opcode = ISCSI_OP_NOOP_IN;
116972cf3143Speter dunlap 	nop_in->flags = ISCSI_FLAG_FINAL;
117072cf3143Speter dunlap 	nop_in->itt = ISCSI_RSVD_TASK_TAG;
117160220f10SPriya Krishnan 	/*
117260220f10SPriya Krishnan 	 * When the target sends a NOP-In as a Ping, the target transfer tag
117360220f10SPriya Krishnan 	 * is set to a valid (not reserved) value and the initiator task tag
117460220f10SPriya Krishnan 	 * is set to ISCSI_RSVD_TASK_TAG (0xffffffff). In this case the StatSN
117560220f10SPriya Krishnan 	 * will always contain the next sequence number but the StatSN for the
117660220f10SPriya Krishnan 	 * connection is not advanced after this PDU is sent.
117760220f10SPriya Krishnan 	 */
117860220f10SPriya Krishnan 	nop_in_pdu->isp_flags |= IDM_PDU_SET_STATSN;
117972cf3143Speter dunlap 	/*
118072cf3143Speter dunlap 	 * This works because we don't currently allocate ttt's anywhere else
118172cf3143Speter dunlap 	 * in iscsit so as long as we stay out of IDM's range we are safe.
118272cf3143Speter dunlap 	 * If we need to allocate ttt's for other PDU's in the future this will
118372cf3143Speter dunlap 	 * need to be improved.
118472cf3143Speter dunlap 	 */
118572cf3143Speter dunlap 	mutex_enter(&ict->ict_mutex);
118672cf3143Speter dunlap 	nop_in->ttt = ict->ict_keepalive_ttt;
118772cf3143Speter dunlap 	ict->ict_keepalive_ttt++;
118872cf3143Speter dunlap 	if (ict->ict_keepalive_ttt == ISCSI_RSVD_TASK_TAG)
118972cf3143Speter dunlap 		ict->ict_keepalive_ttt = IDM_TASKIDS_MAX;
119072cf3143Speter dunlap 	mutex_exit(&ict->ict_mutex);
119172cf3143Speter dunlap 
119272cf3143Speter dunlap 	iscsit_pdu_tx(nop_in_pdu);
119372cf3143Speter dunlap }
119472cf3143Speter dunlap 
1195a6d42e7dSPeter Dunlap static idm_status_t
1196a6d42e7dSPeter Dunlap iscsit_conn_accept(idm_conn_t *ic)
1197a6d42e7dSPeter Dunlap {
1198a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict;
1199a6d42e7dSPeter Dunlap 
120030e7468fSPeter Dunlap 	/*
120130e7468fSPeter Dunlap 	 * We need to get a global hold here to ensure that the service
120230e7468fSPeter Dunlap 	 * doesn't get shutdown prior to establishing a session. This
120330e7468fSPeter Dunlap 	 * gets released in iscsit_conn_destroy().
120430e7468fSPeter Dunlap 	 */
12058c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_enter(&iscsit_global.global_state_mutex);
1206e42a0851Speter dunlap 	if (iscsit_global.global_svc_state != ISE_ENABLED) {
12078c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
1208e42a0851Speter dunlap 		return (IDM_STATUS_FAIL);
1209e42a0851Speter dunlap 	}
121030e7468fSPeter Dunlap 	iscsit_global_hold();
12118c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_exit(&iscsit_global.global_state_mutex);
121230e7468fSPeter Dunlap 
1213a6d42e7dSPeter Dunlap 	/*
1214a6d42e7dSPeter Dunlap 	 * Allocate an associated iscsit structure to represent this
1215a6d42e7dSPeter Dunlap 	 * connection.  We shouldn't really create a session until we
1216a6d42e7dSPeter Dunlap 	 * get the first login PDU.
1217a6d42e7dSPeter Dunlap 	 */
1218a6d42e7dSPeter Dunlap 	ict = kmem_zalloc(sizeof (*ict), KM_SLEEP);
1219a6d42e7dSPeter Dunlap 
1220a6d42e7dSPeter Dunlap 	ict->ict_ic = ic;
1221a6d42e7dSPeter Dunlap 	ict->ict_statsn = 1;
122272cf3143Speter dunlap 	ict->ict_keepalive_ttt = IDM_TASKIDS_MAX; /* Avoid IDM TT range */
1223a6d42e7dSPeter Dunlap 	ic->ic_handle = ict;
1224a6d42e7dSPeter Dunlap 	mutex_init(&ict->ict_mutex, NULL, MUTEX_DRIVER, NULL);
122560220f10SPriya Krishnan 	mutex_init(&ict->ict_statsn_mutex, NULL, MUTEX_DRIVER, NULL);
1226a6d42e7dSPeter Dunlap 	idm_refcnt_init(&ict->ict_refcnt, ict);
1227a6d42e7dSPeter Dunlap 
1228a6d42e7dSPeter Dunlap 	/*
1229a6d42e7dSPeter Dunlap 	 * Initialize login state machine
1230a6d42e7dSPeter Dunlap 	 */
1231a6d42e7dSPeter Dunlap 	if (iscsit_login_sm_init(ict) != IDM_STATUS_SUCCESS) {
1232e42a0851Speter dunlap 		iscsit_global_rele();
12334142b486SJames Moore 		/*
12344142b486SJames Moore 		 * Cleanup the ict after idm notifies us about this failure
12354142b486SJames Moore 		 */
1236a6d42e7dSPeter Dunlap 		return (IDM_STATUS_FAIL);
1237a6d42e7dSPeter Dunlap 	}
1238a6d42e7dSPeter Dunlap 
1239a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1240a6d42e7dSPeter Dunlap }
1241a6d42e7dSPeter Dunlap 
1242a6d42e7dSPeter Dunlap idm_status_t
1243a6d42e7dSPeter Dunlap iscsit_conn_reinstate(iscsit_conn_t *reinstate_ict, iscsit_conn_t *new_ict)
1244a6d42e7dSPeter Dunlap {
1245a6d42e7dSPeter Dunlap 	idm_status_t	result;
1246a6d42e7dSPeter Dunlap 
1247a6d42e7dSPeter Dunlap 	/*
1248a6d42e7dSPeter Dunlap 	 * Note in new connection state that this connection is
1249a6d42e7dSPeter Dunlap 	 * reinstating an existing connection.
1250a6d42e7dSPeter Dunlap 	 */
1251a6d42e7dSPeter Dunlap 	new_ict->ict_reinstating = B_TRUE;
1252a6d42e7dSPeter Dunlap 	new_ict->ict_reinstate_conn = reinstate_ict;
1253a6d42e7dSPeter Dunlap 	new_ict->ict_statsn = reinstate_ict->ict_statsn;
1254a6d42e7dSPeter Dunlap 
1255a6d42e7dSPeter Dunlap 	/*
1256a6d42e7dSPeter Dunlap 	 * Now generate connection state machine event to existing connection
1257a6d42e7dSPeter Dunlap 	 * so that it starts the cleanup process.
1258a6d42e7dSPeter Dunlap 	 */
1259a6d42e7dSPeter Dunlap 	result = idm_conn_reinstate_event(reinstate_ict->ict_ic,
1260a6d42e7dSPeter Dunlap 	    new_ict->ict_ic);
1261a6d42e7dSPeter Dunlap 
1262a6d42e7dSPeter Dunlap 	return (result);
1263a6d42e7dSPeter Dunlap }
1264a6d42e7dSPeter Dunlap 
1265a6d42e7dSPeter Dunlap void
1266a6d42e7dSPeter Dunlap iscsit_conn_hold(iscsit_conn_t *ict)
1267a6d42e7dSPeter Dunlap {
1268a6d42e7dSPeter Dunlap 	idm_refcnt_hold(&ict->ict_refcnt);
1269a6d42e7dSPeter Dunlap }
1270a6d42e7dSPeter Dunlap 
1271a6d42e7dSPeter Dunlap void
1272a6d42e7dSPeter Dunlap iscsit_conn_rele(iscsit_conn_t *ict)
1273a6d42e7dSPeter Dunlap {
1274a6d42e7dSPeter Dunlap 	idm_refcnt_rele(&ict->ict_refcnt);
1275a6d42e7dSPeter Dunlap }
1276a6d42e7dSPeter Dunlap 
1277a6d42e7dSPeter Dunlap void
1278a6d42e7dSPeter Dunlap iscsit_conn_dispatch_hold(iscsit_conn_t *ict)
1279a6d42e7dSPeter Dunlap {
1280a6d42e7dSPeter Dunlap 	idm_refcnt_hold(&ict->ict_dispatch_refcnt);
1281a6d42e7dSPeter Dunlap }
1282a6d42e7dSPeter Dunlap 
1283a6d42e7dSPeter Dunlap void
1284a6d42e7dSPeter Dunlap iscsit_conn_dispatch_rele(iscsit_conn_t *ict)
1285a6d42e7dSPeter Dunlap {
1286a6d42e7dSPeter Dunlap 	idm_refcnt_rele(&ict->ict_dispatch_refcnt);
1287a6d42e7dSPeter Dunlap }
1288a6d42e7dSPeter Dunlap 
1289a6d42e7dSPeter Dunlap static idm_status_t
1290a6d42e7dSPeter Dunlap iscsit_login_fail(idm_conn_t *ic)
1291a6d42e7dSPeter Dunlap {
1292a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = ic->ic_handle;
1293a6d42e7dSPeter Dunlap 
1294a6d42e7dSPeter Dunlap 	/* Generate login state machine event */
1295a6d42e7dSPeter Dunlap 	iscsit_login_sm_event(ict, ILE_LOGIN_CONN_ERROR, NULL);
1296a6d42e7dSPeter Dunlap 
1297a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1298a6d42e7dSPeter Dunlap }
1299a6d42e7dSPeter Dunlap 
1300a6d42e7dSPeter Dunlap static idm_status_t
1301a6d42e7dSPeter Dunlap iscsit_ffp_enabled(idm_conn_t *ic)
1302a6d42e7dSPeter Dunlap {
1303a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = ic->ic_handle;
1304a6d42e7dSPeter Dunlap 
1305a6d42e7dSPeter Dunlap 	/* Generate session state machine event */
1306a6d42e7dSPeter Dunlap 	iscsit_sess_sm_event(ict->ict_sess, SE_CONN_LOGGED_IN, ict);
1307a6d42e7dSPeter Dunlap 
1308a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1309a6d42e7dSPeter Dunlap }
1310a6d42e7dSPeter Dunlap 
1311a6d42e7dSPeter Dunlap static idm_status_t
1312a6d42e7dSPeter Dunlap iscsit_ffp_disabled(idm_conn_t *ic, idm_ffp_disable_t disable_class)
1313a6d42e7dSPeter Dunlap {
1314a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = ic->ic_handle;
1315a6d42e7dSPeter Dunlap 
1316a6d42e7dSPeter Dunlap 	/* Generate session state machine event */
1317a6d42e7dSPeter Dunlap 	switch (disable_class) {
1318a6d42e7dSPeter Dunlap 	case FD_CONN_FAIL:
1319a6d42e7dSPeter Dunlap 		iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_FAIL, ict);
1320a6d42e7dSPeter Dunlap 		break;
1321a6d42e7dSPeter Dunlap 	case FD_CONN_LOGOUT:
1322a6d42e7dSPeter Dunlap 		iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_DISABLE, ict);
1323a6d42e7dSPeter Dunlap 		break;
1324a6d42e7dSPeter Dunlap 	case FD_SESS_LOGOUT:
1325a6d42e7dSPeter Dunlap 		iscsit_sess_sm_event(ict->ict_sess, SE_SESSION_CLOSE, ict);
1326a6d42e7dSPeter Dunlap 		break;
1327a6d42e7dSPeter Dunlap 	default:
1328a6d42e7dSPeter Dunlap 		ASSERT(0);
1329a6d42e7dSPeter Dunlap 	}
1330a6d42e7dSPeter Dunlap 
1331a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1332a6d42e7dSPeter Dunlap }
1333a6d42e7dSPeter Dunlap 
1334a6d42e7dSPeter Dunlap static idm_status_t
1335a6d42e7dSPeter Dunlap iscsit_conn_lost(idm_conn_t *ic)
1336a6d42e7dSPeter Dunlap {
1337d618d68dSPriya Krishnan 	iscsit_conn_t	*ict	= ic->ic_handle;
1338d618d68dSPriya Krishnan 	iscsit_sess_t	*ist	= ict->ict_sess;
1339d618d68dSPriya Krishnan 	iscsit_cbuf_t	*cbuf;
1340d618d68dSPriya Krishnan 	idm_pdu_t	*rx_pdu;
1341d618d68dSPriya Krishnan 	int i;
1342a6d42e7dSPeter Dunlap 
1343a6d42e7dSPeter Dunlap 	mutex_enter(&ict->ict_mutex);
1344a6d42e7dSPeter Dunlap 	ict->ict_lost = B_TRUE;
1345a6d42e7dSPeter Dunlap 	mutex_exit(&ict->ict_mutex);
1346d618d68dSPriya Krishnan 	/*
1347d618d68dSPriya Krishnan 	 * scrub the staging queue for all PDUs on this connection
1348d618d68dSPriya Krishnan 	 */
1349d618d68dSPriya Krishnan 	if (ist != NULL) {
1350d618d68dSPriya Krishnan 		mutex_enter(&ist->ist_sn_mutex);
1351d618d68dSPriya Krishnan 		for (cbuf = ist->ist_rxpdu_queue, i = 0;
1352d618d68dSPriya Krishnan 		    ((cbuf->cb_num_elems > 0) && (i < ISCSIT_RXPDU_QUEUE_LEN));
1353d618d68dSPriya Krishnan 		    i++) {
1354d618d68dSPriya Krishnan 			if (((rx_pdu = cbuf->cb_buffer[i]) != NULL) &&
1355d618d68dSPriya Krishnan 			    (rx_pdu->isp_ic == ic)) {
1356d618d68dSPriya Krishnan 				/* conn is lost, drop the pdu */
1357d618d68dSPriya Krishnan 				DTRACE_PROBE3(scrubbing__staging__queue,
1358d618d68dSPriya Krishnan 				    iscsit_sess_t *, ist, idm_conn_t *, ic,
1359d618d68dSPriya Krishnan 				    idm_pdu_t *, rx_pdu);
1360d618d68dSPriya Krishnan 				idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
1361d618d68dSPriya Krishnan 				cbuf->cb_buffer[i] = NULL;
1362d618d68dSPriya Krishnan 				cbuf->cb_num_elems--;
1363d618d68dSPriya Krishnan 				iscsit_conn_dispatch_rele(ict);
1364d618d68dSPriya Krishnan 			}
1365d618d68dSPriya Krishnan 		}
1366d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
1367d618d68dSPriya Krishnan 	}
1368a6d42e7dSPeter Dunlap 	/*
1369a6d42e7dSPeter Dunlap 	 * Make sure there aren't any PDU's transitioning from the receive
1370a6d42e7dSPeter Dunlap 	 * handler to the dispatch taskq.
1371a6d42e7dSPeter Dunlap 	 */
1372a6d42e7dSPeter Dunlap 	idm_refcnt_wait_ref(&ict->ict_dispatch_refcnt);
1373a6d42e7dSPeter Dunlap 
1374a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1375a6d42e7dSPeter Dunlap }
1376a6d42e7dSPeter Dunlap 
1377a6d42e7dSPeter Dunlap static idm_status_t
1378a6d42e7dSPeter Dunlap iscsit_conn_destroy(idm_conn_t *ic)
1379a6d42e7dSPeter Dunlap {
1380a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = ic->ic_handle;
1381a6d42e7dSPeter Dunlap 
1382a6d42e7dSPeter Dunlap 	mutex_enter(&ict->ict_mutex);
1383a6d42e7dSPeter Dunlap 	ict->ict_destroyed = B_TRUE;
1384a6d42e7dSPeter Dunlap 	mutex_exit(&ict->ict_mutex);
1385a6d42e7dSPeter Dunlap 
1386a6d42e7dSPeter Dunlap 	/* Generate session state machine event */
1387a6d42e7dSPeter Dunlap 	if (ict->ict_sess != NULL) {
1388a6d42e7dSPeter Dunlap 		/*
1389a6d42e7dSPeter Dunlap 		 * Session state machine will call iscsit_conn_destroy_done()
1390a6d42e7dSPeter Dunlap 		 * when it has removed references to this connection.
1391a6d42e7dSPeter Dunlap 		 */
1392a6d42e7dSPeter Dunlap 		iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FAIL, ict);
1393a6d42e7dSPeter Dunlap 	}
1394a6d42e7dSPeter Dunlap 
1395a6d42e7dSPeter Dunlap 	idm_refcnt_wait_ref(&ict->ict_refcnt);
139647715e7fSPriya Krishnan 	/*
139747715e7fSPriya Krishnan 	 * The session state machine does not need to post
139847715e7fSPriya Krishnan 	 * events to IDM any longer, so it is safe to set
139947715e7fSPriya Krishnan 	 * the idm connection reference to NULL
140047715e7fSPriya Krishnan 	 */
140147715e7fSPriya Krishnan 	ict->ict_ic = NULL;
1402a6d42e7dSPeter Dunlap 
14034142b486SJames Moore 	/* Reap the login state machine */
14044142b486SJames Moore 	iscsit_login_sm_fini(ict);
14054142b486SJames Moore 
14064142b486SJames Moore 	/* Clean up any text command remnants */
14074142b486SJames Moore 	iscsit_text_cmd_fini(ict);
14084142b486SJames Moore 
1409a6d42e7dSPeter Dunlap 	mutex_destroy(&ict->ict_mutex);
1410a6d42e7dSPeter Dunlap 	idm_refcnt_destroy(&ict->ict_refcnt);
1411a6d42e7dSPeter Dunlap 	kmem_free(ict, sizeof (*ict));
1412a6d42e7dSPeter Dunlap 
141330e7468fSPeter Dunlap 	iscsit_global_rele();
141430e7468fSPeter Dunlap 
1415a6d42e7dSPeter Dunlap 	return (IDM_STATUS_SUCCESS);
1416a6d42e7dSPeter Dunlap }
1417a6d42e7dSPeter Dunlap 
141847715e7fSPriya Krishnan void
141947715e7fSPriya Krishnan iscsit_conn_logout(iscsit_conn_t *ict)
142047715e7fSPriya Krishnan {
142147715e7fSPriya Krishnan 	/*
142247715e7fSPriya Krishnan 	 * If the iscsi connection is active, then
142347715e7fSPriya Krishnan 	 * logout the IDM connection by sending a
142447715e7fSPriya Krishnan 	 * CE_LOGOUT_SESSION_SUCCESS, else, no action
142547715e7fSPriya Krishnan 	 * needs to be taken because the connection
142647715e7fSPriya Krishnan 	 * is already in the teardown process.
142747715e7fSPriya Krishnan 	 */
142847715e7fSPriya Krishnan 	mutex_enter(&ict->ict_mutex);
142947715e7fSPriya Krishnan 	if (ict->ict_lost == B_FALSE && ict->ict_destroyed == B_FALSE) {
1430*1f99504aSToomas Soome 		idm_conn_event(ict->ict_ic, CE_LOGOUT_SESSION_SUCCESS, 0);
143147715e7fSPriya Krishnan 	}
143247715e7fSPriya Krishnan 	mutex_exit(&ict->ict_mutex);
143347715e7fSPriya Krishnan }
143447715e7fSPriya Krishnan 
1435a6d42e7dSPeter Dunlap /*
1436a6d42e7dSPeter Dunlap  * STMF-related functions
1437a6d42e7dSPeter Dunlap  *
1438a6d42e7dSPeter Dunlap  * iSCSI to STMF mapping
1439a6d42e7dSPeter Dunlap  *
1440a6d42e7dSPeter Dunlap  * Session == ?
1441a6d42e7dSPeter Dunlap  * Connection == bound to local port but not itself a local port
1442a6d42e7dSPeter Dunlap  * Target
1443a6d42e7dSPeter Dunlap  * Target portal (group?) == local port (really but we're not going to do this)
1444a6d42e7dSPeter Dunlap  *	iscsit needs to map connections to local ports (whatever we decide
1445*1f99504aSToomas Soome  *	they are)
1446a6d42e7dSPeter Dunlap  * Target == ?
1447a6d42e7dSPeter Dunlap  */
1448a6d42e7dSPeter Dunlap 
1449a6d42e7dSPeter Dunlap /*ARGSUSED*/
1450a6d42e7dSPeter Dunlap static stmf_data_buf_t *
1451a6d42e7dSPeter Dunlap iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
1452a6d42e7dSPeter Dunlap     uint32_t flags)
1453a6d42e7dSPeter Dunlap {
1454a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = task->task_port_private;
1455a6d42e7dSPeter Dunlap 	idm_buf_t *idm_buffer;
1456a6d42e7dSPeter Dunlap 	iscsit_buf_t	*ibuf;
1457a6d42e7dSPeter Dunlap 	stmf_data_buf_t *result;
145826ec4a22SPriya Krishnan 	uint32_t	bsize;
1459a6d42e7dSPeter Dunlap 
1460a6d42e7dSPeter Dunlap 	/*
146101c0a610SPriya Krishnan 	 * If the requested size is larger than MaxBurstLength and the
146201c0a610SPriya Krishnan 	 * given pminsize is also larger than MaxBurstLength, then the
146301c0a610SPriya Krishnan 	 * allocation fails (dbuf = NULL) and pminsize is modified to
146401c0a610SPriya Krishnan 	 * be equal to MaxBurstLength. stmf/sbd then should re-invoke
146501c0a610SPriya Krishnan 	 * this function with the corrected values for transfer.
1466a6d42e7dSPeter Dunlap 	 */
146726ec4a22SPriya Krishnan 	ASSERT(pminsize);
146801c0a610SPriya Krishnan 	if (size <= itask->it_ict->ict_op.op_max_burst_length) {
146901c0a610SPriya Krishnan 		bsize = size;
147001c0a610SPriya Krishnan 	} else if (*pminsize <= itask->it_ict->ict_op.op_max_burst_length) {
147101c0a610SPriya Krishnan 		bsize = itask->it_ict->ict_op.op_max_burst_length;
147201c0a610SPriya Krishnan 	} else {
1473a6d42e7dSPeter Dunlap 		*pminsize = itask->it_ict->ict_op.op_max_burst_length;
1474a6d42e7dSPeter Dunlap 		return (NULL);
1475a6d42e7dSPeter Dunlap 	}
1476a6d42e7dSPeter Dunlap 
1477a6d42e7dSPeter Dunlap 	/* Alloc buffer */
147826ec4a22SPriya Krishnan 	idm_buffer = idm_buf_alloc(itask->it_ict->ict_ic, NULL, bsize);
1479a6d42e7dSPeter Dunlap 	if (idm_buffer != NULL) {
1480a6d42e7dSPeter Dunlap 		result = stmf_alloc(STMF_STRUCT_DATA_BUF,
1481a6d42e7dSPeter Dunlap 		    sizeof (iscsit_buf_t), 0);
1482a6d42e7dSPeter Dunlap 		if (result != NULL) {
1483a6d42e7dSPeter Dunlap 			/* Fill in stmf_data_buf_t */
1484a6d42e7dSPeter Dunlap 			ibuf = result->db_port_private;
1485a6d42e7dSPeter Dunlap 			ibuf->ibuf_idm_buf = idm_buffer;
1486a6d42e7dSPeter Dunlap 			ibuf->ibuf_stmf_buf = result;
1487a6d42e7dSPeter Dunlap 			ibuf->ibuf_is_immed = B_FALSE;
1488a6d42e7dSPeter Dunlap 			result->db_flags = DB_DONT_CACHE;
148926ec4a22SPriya Krishnan 			result->db_buf_size = bsize;
149026ec4a22SPriya Krishnan 			result->db_data_size = bsize;
1491a6d42e7dSPeter Dunlap 			result->db_sglist_length = 1;
1492a6d42e7dSPeter Dunlap 			result->db_sglist[0].seg_addr = idm_buffer->idb_buf;
1493a6d42e7dSPeter Dunlap 			result->db_sglist[0].seg_length =
1494a6d42e7dSPeter Dunlap 			    idm_buffer->idb_buflen;
1495a6d42e7dSPeter Dunlap 			return (result);
1496a6d42e7dSPeter Dunlap 		}
1497a6d42e7dSPeter Dunlap 
1498a6d42e7dSPeter Dunlap 		/* Couldn't get the stmf_data_buf_t so free the buffer */
1499a6d42e7dSPeter Dunlap 		idm_buf_free(idm_buffer);
1500a6d42e7dSPeter Dunlap 	}
1501a6d42e7dSPeter Dunlap 
1502a6d42e7dSPeter Dunlap 	return (NULL);
1503a6d42e7dSPeter Dunlap }
1504a6d42e7dSPeter Dunlap 
1505a6d42e7dSPeter Dunlap /*ARGSUSED*/
1506a6d42e7dSPeter Dunlap static void
1507a6d42e7dSPeter Dunlap iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf)
1508a6d42e7dSPeter Dunlap {
1509a6d42e7dSPeter Dunlap 	iscsit_buf_t *ibuf = dbuf->db_port_private;
1510a6d42e7dSPeter Dunlap 
1511a6d42e7dSPeter Dunlap 	if (ibuf->ibuf_is_immed) {
1512a6d42e7dSPeter Dunlap 		/*
1513a6d42e7dSPeter Dunlap 		 * The iscsit_buf_t structure itself will be freed with its
1514a6d42e7dSPeter Dunlap 		 * associated task.  Here we just need to free the PDU that
1515a6d42e7dSPeter Dunlap 		 * held the immediate data.
1516a6d42e7dSPeter Dunlap 		 */
1517a6d42e7dSPeter Dunlap 		idm_pdu_complete(ibuf->ibuf_immed_data_pdu, IDM_STATUS_SUCCESS);
1518a6d42e7dSPeter Dunlap 		ibuf->ibuf_immed_data_pdu = 0;
1519a6d42e7dSPeter Dunlap 	} else {
1520a6d42e7dSPeter Dunlap 		idm_buf_free(ibuf->ibuf_idm_buf);
1521a6d42e7dSPeter Dunlap 		stmf_free(dbuf);
1522a6d42e7dSPeter Dunlap 	}
1523a6d42e7dSPeter Dunlap }
1524a6d42e7dSPeter Dunlap 
1525a6d42e7dSPeter Dunlap /*ARGSUSED*/
1526a6d42e7dSPeter Dunlap stmf_status_t
1527a6d42e7dSPeter Dunlap iscsit_xfer_scsi_data(scsi_task_t *task, stmf_data_buf_t *dbuf,
1528a6d42e7dSPeter Dunlap     uint32_t ioflags)
1529a6d42e7dSPeter Dunlap {
1530a6d42e7dSPeter Dunlap 	iscsit_task_t *iscsit_task = task->task_port_private;
153160220f10SPriya Krishnan 	iscsit_sess_t *ict_sess = iscsit_task->it_ict->ict_sess;
1532a6d42e7dSPeter Dunlap 	iscsit_buf_t *ibuf = dbuf->db_port_private;
1533a6d42e7dSPeter Dunlap 	int idm_rc;
1534a6d42e7dSPeter Dunlap 
1535bf604c64SPeter Dunlap 	/*
1536bf604c64SPeter Dunlap 	 * If we are aborting then we can ignore this request
1537bf604c64SPeter Dunlap 	 */
1538bf604c64SPeter Dunlap 	if (iscsit_task->it_stmf_abort) {
1539bf604c64SPeter Dunlap 		return (STMF_SUCCESS);
1540bf604c64SPeter Dunlap 	}
1541bf604c64SPeter Dunlap 
1542a6d42e7dSPeter Dunlap 	/*
1543a6d42e7dSPeter Dunlap 	 * If it's not immediate data then start the transfer
1544a6d42e7dSPeter Dunlap 	 */
1545a6d42e7dSPeter Dunlap 	if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
15462727bb05STim Kordas 		if (ibuf->ibuf_is_immed)
15472727bb05STim Kordas 			return (iscsit_idm_to_stmf(IDM_STATUS_SUCCESS));
154860220f10SPriya Krishnan 		/*
154960220f10SPriya Krishnan 		 * The DB_SEND_STATUS_GOOD flag in the STMF data buffer allows
155060220f10SPriya Krishnan 		 * the port provider to phase-collapse, i.e. send the status
155160220f10SPriya Krishnan 		 * along with the final data PDU for the command. The port
155260220f10SPriya Krishnan 		 * provider passes this request to the transport layer by
155360220f10SPriya Krishnan 		 * setting a flag IDM_TASK_PHASECOLLAPSE_REQ in the task.
155460220f10SPriya Krishnan 		 */
155560220f10SPriya Krishnan 		if (dbuf->db_flags & DB_SEND_STATUS_GOOD)
155660220f10SPriya Krishnan 			iscsit_task->it_idm_task->idt_flags |=
155760220f10SPriya Krishnan 			    IDM_TASK_PHASECOLLAPSE_REQ;
1558a6d42e7dSPeter Dunlap 		/*
1559a6d42e7dSPeter Dunlap 		 * IDM will call iscsit_build_hdr so lock now to serialize
1560a6d42e7dSPeter Dunlap 		 * access to the SN values.  We need to lock here to enforce
1561a6d42e7dSPeter Dunlap 		 * lock ordering
1562a6d42e7dSPeter Dunlap 		 */
1563d618d68dSPriya Krishnan 		mutex_enter(&ict_sess->ist_sn_mutex);
1564a6d42e7dSPeter Dunlap 		idm_rc = idm_buf_tx_to_ini(iscsit_task->it_idm_task,
1565a6d42e7dSPeter Dunlap 		    ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1566a6d42e7dSPeter Dunlap 		    dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1567d618d68dSPriya Krishnan 		mutex_exit(&ict_sess->ist_sn_mutex);
1568a6d42e7dSPeter Dunlap 
1569a6d42e7dSPeter Dunlap 		return (iscsit_idm_to_stmf(idm_rc));
1570a6d42e7dSPeter Dunlap 	} else if (dbuf->db_flags & DB_DIRECTION_FROM_RPORT) {
15712727bb05STim Kordas 		ASSERT(ibuf->ibuf_is_immed == B_FALSE);
1572a6d42e7dSPeter Dunlap 		/* Grab the SN lock (see comment above) */
1573d618d68dSPriya Krishnan 		mutex_enter(&ict_sess->ist_sn_mutex);
1574a6d42e7dSPeter Dunlap 		idm_rc = idm_buf_rx_from_ini(iscsit_task->it_idm_task,
1575a6d42e7dSPeter Dunlap 		    ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1576a6d42e7dSPeter Dunlap 		    dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1577d618d68dSPriya Krishnan 		mutex_exit(&ict_sess->ist_sn_mutex);
1578a6d42e7dSPeter Dunlap 
1579a6d42e7dSPeter Dunlap 		return (iscsit_idm_to_stmf(idm_rc));
1580a6d42e7dSPeter Dunlap 	}
1581a6d42e7dSPeter Dunlap 
1582a6d42e7dSPeter Dunlap 	/* What are we supposed to do if there is no direction? */
1583a6d42e7dSPeter Dunlap 	return (STMF_INVALID_ARG);
1584a6d42e7dSPeter Dunlap }
1585a6d42e7dSPeter Dunlap 
1586a6d42e7dSPeter Dunlap static void
1587a6d42e7dSPeter Dunlap iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status)
1588a6d42e7dSPeter Dunlap {
1589a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = idb->idb_task_binding->idt_private;
1590a6d42e7dSPeter Dunlap 	stmf_data_buf_t *dbuf = idb->idb_cb_arg;
1591a6d42e7dSPeter Dunlap 
1592a6d42e7dSPeter Dunlap 	dbuf->db_xfer_status = iscsit_idm_to_stmf(status);
1593a6d42e7dSPeter Dunlap 
1594bf604c64SPeter Dunlap 	/*
1595bf604c64SPeter Dunlap 	 * If the task has been aborted then we don't need to call STMF
1596bf604c64SPeter Dunlap 	 */
1597bf604c64SPeter Dunlap 	if (itask->it_stmf_abort) {
1598bf604c64SPeter Dunlap 		return;
1599bf604c64SPeter Dunlap 	}
1600bf604c64SPeter Dunlap 
1601a6d42e7dSPeter Dunlap 	/*
160260220f10SPriya Krishnan 	 * For ISCSI over TCP (not iSER), the last SCSI Data PDU passed
160360220f10SPriya Krishnan 	 * for a successful command contains the status as requested by
160460220f10SPriya Krishnan 	 * by COMSTAR (via the DB_SEND_STATUS_GOOD flag). But the iSER
160560220f10SPriya Krishnan 	 * transport does not support phase-collapse. So pretend we are
160660220f10SPriya Krishnan 	 * COMSTAR and send the status in a separate PDU now.
1607a6d42e7dSPeter Dunlap 	 */
160860220f10SPriya Krishnan 	if (idb->idb_task_binding->idt_flags & IDM_TASK_PHASECOLLAPSE_SUCCESS) {
160960220f10SPriya Krishnan 		/*
1610a2383ac5SPriya Krishnan 		 * Mark task complete and notify COMSTAR
1611a2383ac5SPriya Krishnan 		 * that the status has been sent.
161260220f10SPriya Krishnan 		 */
161360220f10SPriya Krishnan 		itask->it_idm_task->idt_state = TASK_COMPLETE;
161460220f10SPriya Krishnan 		stmf_send_status_done(itask->it_stmf_task,
161560220f10SPriya Krishnan 		    iscsit_idm_to_stmf(status), STMF_IOF_LPORT_DONE);
161660220f10SPriya Krishnan 	} else if ((dbuf->db_flags & DB_SEND_STATUS_GOOD) &&
1617a6d42e7dSPeter Dunlap 	    status == IDM_STATUS_SUCCESS) {
161860220f10SPriya Krishnan 
1619a6d42e7dSPeter Dunlap 		/*
162070a38f02SPriya Krishnan 		 * The iscsi target port provider - for iSER, emulates the
162170a38f02SPriya Krishnan 		 * DB_SEND_STATUS_GOOD optimization if requested by STMF;
162270a38f02SPriya Krishnan 		 * it sends the status in a separate PDU after the data
162370a38f02SPriya Krishnan 		 * transfer. In this case the port provider should first
162470a38f02SPriya Krishnan 		 * call stmf_data_xfer_done() to mark the transfer complete
162570a38f02SPriya Krishnan 		 * and then send the status. Although STMF will free the
162670a38f02SPriya Krishnan 		 * buffer at the time the task is freed, even if the transfer
162770a38f02SPriya Krishnan 		 * is not marked complete, this behavior makes statistics
162870a38f02SPriya Krishnan 		 * gathering and task state tracking more difficult than it
162970a38f02SPriya Krishnan 		 * needs to be.
1630a6d42e7dSPeter Dunlap 		 */
163170a38f02SPriya Krishnan 		stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
1632a6d42e7dSPeter Dunlap 		if (iscsit_send_scsi_status(itask->it_stmf_task, 0)
163363528ae4SJames Moore 		    != STMF_SUCCESS) {
163470a38f02SPriya Krishnan 			stmf_send_status_done(itask->it_stmf_task,
163570a38f02SPriya Krishnan 			    STMF_FAILURE, STMF_IOF_LPORT_DONE);
1636a6d42e7dSPeter Dunlap 		}
1637a6d42e7dSPeter Dunlap 	} else {
1638a6d42e7dSPeter Dunlap 		stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
163970a38f02SPriya Krishnan 		/* don't touch dbuf after stmf_data_xfer_done */
1640a6d42e7dSPeter Dunlap 	}
1641a6d42e7dSPeter Dunlap }
1642a6d42e7dSPeter Dunlap 
1643bf604c64SPeter Dunlap 
1644a6d42e7dSPeter Dunlap /*ARGSUSED*/
1645a6d42e7dSPeter Dunlap stmf_status_t
1646a6d42e7dSPeter Dunlap iscsit_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
1647a6d42e7dSPeter Dunlap {
1648a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = task->task_port_private;
1649a6d42e7dSPeter Dunlap 	iscsi_scsi_rsp_hdr_t *rsp;
1650a6d42e7dSPeter Dunlap 	idm_pdu_t *pdu;
1651a6d42e7dSPeter Dunlap 	int resp_datalen;
1652a6d42e7dSPeter Dunlap 
1653a6d42e7dSPeter Dunlap 	/*
1654a6d42e7dSPeter Dunlap 	 * If this task is aborted then we don't need to respond.
1655a6d42e7dSPeter Dunlap 	 */
1656a6d42e7dSPeter Dunlap 	if (itask->it_stmf_abort) {
1657a6d42e7dSPeter Dunlap 		return (STMF_SUCCESS);
1658a6d42e7dSPeter Dunlap 	}
1659a6d42e7dSPeter Dunlap 
1660a6d42e7dSPeter Dunlap 	/*
1661a6d42e7dSPeter Dunlap 	 * If this is a task management status, handle it elsewhere.
1662a6d42e7dSPeter Dunlap 	 */
1663a6d42e7dSPeter Dunlap 	if (task->task_mgmt_function != TM_NONE) {
1664a6d42e7dSPeter Dunlap 		/*
1665a6d42e7dSPeter Dunlap 		 * Don't wait for the PDU completion to tell STMF
1666a6d42e7dSPeter Dunlap 		 * the task is done -- it doesn't really matter and
1667a6d42e7dSPeter Dunlap 		 * it makes life complicated if STMF later asks us to
1668a6d42e7dSPeter Dunlap 		 * abort the request and we don't know whether the
1669a6d42e7dSPeter Dunlap 		 * status has been sent or not.
1670a6d42e7dSPeter Dunlap 		 */
1671a6d42e7dSPeter Dunlap 		itask->it_tm_responded = B_TRUE;
1672a6d42e7dSPeter Dunlap 		iscsit_send_task_mgmt_resp(itask->it_tm_pdu,
1673a6d42e7dSPeter Dunlap 		    (task->task_completion_status == STMF_SUCCESS) ?
1674a6d42e7dSPeter Dunlap 		    SCSI_TCP_TM_RESP_COMPLETE : SCSI_TCP_TM_RESP_FUNC_NOT_SUPP);
1675a6d42e7dSPeter Dunlap 		stmf_send_status_done(task, STMF_SUCCESS,
1676a6d42e7dSPeter Dunlap 		    STMF_IOF_LPORT_DONE);
1677a6d42e7dSPeter Dunlap 		return (STMF_SUCCESS);
1678a6d42e7dSPeter Dunlap 	}
1679a6d42e7dSPeter Dunlap 
168072cf3143Speter dunlap 	/*
168172cf3143Speter dunlap 	 * Remove the task from the session task list
168272cf3143Speter dunlap 	 */
168372cf3143Speter dunlap 	iscsit_task_done(itask);
168472cf3143Speter dunlap 
168572cf3143Speter dunlap 	/*
168672cf3143Speter dunlap 	 * Send status
168772cf3143Speter dunlap 	 */
1688a6d42e7dSPeter Dunlap 	mutex_enter(&itask->it_idm_task->idt_mutex);
1689a6d42e7dSPeter Dunlap 	if ((itask->it_idm_task->idt_state == TASK_ACTIVE) &&
1690a6d42e7dSPeter Dunlap 	    (task->task_completion_status == STMF_SUCCESS) &&
1691a6d42e7dSPeter Dunlap 	    (task->task_sense_length == 0) &&
1692a6d42e7dSPeter Dunlap 	    (task->task_resid == 0)) {
1693a6d42e7dSPeter Dunlap 		itask->it_idm_task->idt_state = TASK_COMPLETE;
1694a6d42e7dSPeter Dunlap 		/* PDU callback releases task hold */
1695a6d42e7dSPeter Dunlap 		idm_task_hold(itask->it_idm_task);
1696a6d42e7dSPeter Dunlap 		mutex_exit(&itask->it_idm_task->idt_mutex);
1697a6d42e7dSPeter Dunlap 		/*
1698a6d42e7dSPeter Dunlap 		 * Fast path.  Cached status PDU's are already
1699a6d42e7dSPeter Dunlap 		 * initialized.  We just need to fill in
170060220f10SPriya Krishnan 		 * connection and task information. StatSN is
170160220f10SPriya Krishnan 		 * incremented by 1 for every status sent a
170260220f10SPriya Krishnan 		 * connection.
1703a6d42e7dSPeter Dunlap 		 */
1704a6d42e7dSPeter Dunlap 		pdu = kmem_cache_alloc(iscsit_status_pdu_cache, KM_SLEEP);
1705a6d42e7dSPeter Dunlap 		pdu->isp_ic = itask->it_ict->ict_ic;
1706a6d42e7dSPeter Dunlap 		pdu->isp_private = itask;
170760220f10SPriya Krishnan 		pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1708a6d42e7dSPeter Dunlap 
1709a6d42e7dSPeter Dunlap 		rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1710a6d42e7dSPeter Dunlap 		rsp->itt = itask->it_itt;
17111d7b78a3SPriya Krishnan 		/*
17121d7b78a3SPriya Krishnan 		 * ExpDataSN is the number of R2T and Data-In (read)
17131d7b78a3SPriya Krishnan 		 * PDUs the target has sent for the SCSI command.
17141d7b78a3SPriya Krishnan 		 *
17151d7b78a3SPriya Krishnan 		 * Since there is no support for bidirectional transfer
17161d7b78a3SPriya Krishnan 		 * yet, either idt_exp_datasn or idt_exp_rttsn, but not
17171d7b78a3SPriya Krishnan 		 * both is valid at any time
17181d7b78a3SPriya Krishnan 		 */
17191d7b78a3SPriya Krishnan 		rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
17201d7b78a3SPriya Krishnan 		    htonl(itask->it_idm_task->idt_exp_datasn):
17211d7b78a3SPriya Krishnan 		    htonl(itask->it_idm_task->idt_exp_rttsn);
1722a6d42e7dSPeter Dunlap 		rsp->cmd_status = task->task_scsi_status;
1723a6d42e7dSPeter Dunlap 		iscsit_pdu_tx(pdu);
1724a6d42e7dSPeter Dunlap 		return (STMF_SUCCESS);
1725a6d42e7dSPeter Dunlap 	} else {
1726a6d42e7dSPeter Dunlap 		if (itask->it_idm_task->idt_state != TASK_ACTIVE) {
1727a6d42e7dSPeter Dunlap 			mutex_exit(&itask->it_idm_task->idt_mutex);
172863528ae4SJames Moore 			return (STMF_FAILURE);
1729a6d42e7dSPeter Dunlap 		}
1730a6d42e7dSPeter Dunlap 		itask->it_idm_task->idt_state = TASK_COMPLETE;
1731a6d42e7dSPeter Dunlap 		/* PDU callback releases task hold */
1732a6d42e7dSPeter Dunlap 		idm_task_hold(itask->it_idm_task);
1733a6d42e7dSPeter Dunlap 		mutex_exit(&itask->it_idm_task->idt_mutex);
1734a6d42e7dSPeter Dunlap 
1735a6d42e7dSPeter Dunlap 		resp_datalen = (task->task_sense_length == 0) ? 0 :
1736a6d42e7dSPeter Dunlap 		    (task->task_sense_length + sizeof (uint16_t));
1737a6d42e7dSPeter Dunlap 
1738a6d42e7dSPeter Dunlap 		pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
1739a6d42e7dSPeter Dunlap 		idm_pdu_init(pdu, itask->it_ict->ict_ic, itask,
1740a6d42e7dSPeter Dunlap 		    iscsit_send_status_done);
174160220f10SPriya Krishnan 		pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1742a6d42e7dSPeter Dunlap 
1743a6d42e7dSPeter Dunlap 		rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1744a6d42e7dSPeter Dunlap 		bzero(rsp, sizeof (*rsp));
1745a6d42e7dSPeter Dunlap 		rsp->opcode = ISCSI_OP_SCSI_RSP;
1746a6d42e7dSPeter Dunlap 
1747a6d42e7dSPeter Dunlap 		rsp->flags = ISCSI_FLAG_FINAL;
1748a6d42e7dSPeter Dunlap 		if (task->task_status_ctrl & TASK_SCTRL_OVER) {
1749a6d42e7dSPeter Dunlap 			rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
1750a6d42e7dSPeter Dunlap 		} else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
1751a6d42e7dSPeter Dunlap 			rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
1752a6d42e7dSPeter Dunlap 		}
1753a6d42e7dSPeter Dunlap 
1754a6d42e7dSPeter Dunlap 		rsp->bi_residual_count = 0;
1755a6d42e7dSPeter Dunlap 		rsp->residual_count = htonl(task->task_resid);
1756a6d42e7dSPeter Dunlap 		rsp->itt = itask->it_itt;
1757a6d42e7dSPeter Dunlap 		rsp->response = ISCSI_STATUS_CMD_COMPLETED;
17581d7b78a3SPriya Krishnan 		rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
17591d7b78a3SPriya Krishnan 		    htonl(itask->it_idm_task->idt_exp_datasn):
17601d7b78a3SPriya Krishnan 		    htonl(itask->it_idm_task->idt_exp_rttsn);
1761a6d42e7dSPeter Dunlap 		rsp->cmd_status = task->task_scsi_status;
1762a6d42e7dSPeter Dunlap 		if (task->task_sense_length != 0) {
1763a6d42e7dSPeter Dunlap 			/*
1764a6d42e7dSPeter Dunlap 			 * Add a byte to provide the sense length in
1765a6d42e7dSPeter Dunlap 			 * the response
1766a6d42e7dSPeter Dunlap 			 */
1767a6d42e7dSPeter Dunlap 			*(uint16_t *)((void *)pdu->isp_data) =
1768a6d42e7dSPeter Dunlap 			    htons(task->task_sense_length);
1769a6d42e7dSPeter Dunlap 			bcopy(task->task_sense_data,
1770a6d42e7dSPeter Dunlap 			    (uint8_t *)pdu->isp_data +
1771a6d42e7dSPeter Dunlap 			    sizeof (uint16_t),
1772a6d42e7dSPeter Dunlap 			    task->task_sense_length);
1773a6d42e7dSPeter Dunlap 			hton24(rsp->dlength, resp_datalen);
1774a6d42e7dSPeter Dunlap 		}
1775a6d42e7dSPeter Dunlap 
177630e7468fSPeter Dunlap 		DTRACE_PROBE5(iscsi__scsi__response,
177730e7468fSPeter Dunlap 		    iscsit_conn_t *, itask->it_ict,
177830e7468fSPeter Dunlap 		    uint8_t, rsp->response,
177930e7468fSPeter Dunlap 		    uint8_t, rsp->cmd_status,
178030e7468fSPeter Dunlap 		    idm_pdu_t *, pdu,
178130e7468fSPeter Dunlap 		    scsi_task_t *, task);
178230e7468fSPeter Dunlap 
1783a6d42e7dSPeter Dunlap 		iscsit_pdu_tx(pdu);
1784a6d42e7dSPeter Dunlap 
1785a6d42e7dSPeter Dunlap 		return (STMF_SUCCESS);
1786a6d42e7dSPeter Dunlap 	}
1787a6d42e7dSPeter Dunlap }
1788a6d42e7dSPeter Dunlap 
1789a6d42e7dSPeter Dunlap /*ARGSUSED*/
1790a6d42e7dSPeter Dunlap static void
1791a6d42e7dSPeter Dunlap iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status)
1792a6d42e7dSPeter Dunlap {
1793bf604c64SPeter Dunlap 	iscsit_task_t	*itask;
1794bf604c64SPeter Dunlap 	boolean_t	aborted;
1795a6d42e7dSPeter Dunlap 
1796a6d42e7dSPeter Dunlap 	itask = pdu->isp_private;
1797bf604c64SPeter Dunlap 	aborted = itask->it_stmf_abort;
1798bf604c64SPeter Dunlap 
1799bf604c64SPeter Dunlap 	/*
1800bf604c64SPeter Dunlap 	 * After releasing the hold the task may be freed at any time so
1801bf604c64SPeter Dunlap 	 * don't touch it.
1802bf604c64SPeter Dunlap 	 */
1803a6d42e7dSPeter Dunlap 	idm_task_rele(itask->it_idm_task);
1804bf604c64SPeter Dunlap 	if (!aborted) {
1805bf604c64SPeter Dunlap 		stmf_send_status_done(itask->it_stmf_task,
1806bf604c64SPeter Dunlap 		    iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1807bf604c64SPeter Dunlap 	}
1808a6d42e7dSPeter Dunlap 	kmem_cache_free(iscsit_status_pdu_cache, pdu);
1809a6d42e7dSPeter Dunlap }
1810a6d42e7dSPeter Dunlap 
1811a6d42e7dSPeter Dunlap /*ARGSUSED*/
1812a6d42e7dSPeter Dunlap static void
1813a6d42e7dSPeter Dunlap iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status)
1814a6d42e7dSPeter Dunlap {
1815a6d42e7dSPeter Dunlap 	iscsit_task_t	 *itask;
1816bf604c64SPeter Dunlap 	boolean_t	aborted;
1817a6d42e7dSPeter Dunlap 
1818a6d42e7dSPeter Dunlap 	itask = pdu->isp_private;
1819bf604c64SPeter Dunlap 	aborted = itask->it_stmf_abort;
1820bf604c64SPeter Dunlap 
1821bf604c64SPeter Dunlap 	/*
1822bf604c64SPeter Dunlap 	 * After releasing the hold the task may be freed at any time so
1823bf604c64SPeter Dunlap 	 * don't touch it.
1824bf604c64SPeter Dunlap 	 */
1825a6d42e7dSPeter Dunlap 	idm_task_rele(itask->it_idm_task);
1826bf604c64SPeter Dunlap 	if (!aborted) {
1827bf604c64SPeter Dunlap 		stmf_send_status_done(itask->it_stmf_task,
1828bf604c64SPeter Dunlap 		    iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1829bf604c64SPeter Dunlap 	}
1830a6d42e7dSPeter Dunlap 	idm_pdu_free(pdu);
1831a6d42e7dSPeter Dunlap }
1832a6d42e7dSPeter Dunlap 
1833a6d42e7dSPeter Dunlap 
1834a6d42e7dSPeter Dunlap void
1835a6d42e7dSPeter Dunlap iscsit_lport_task_free(scsi_task_t *task)
1836a6d42e7dSPeter Dunlap {
1837a6d42e7dSPeter Dunlap 	iscsit_task_t *itask = task->task_port_private;
1838a6d42e7dSPeter Dunlap 
1839a6d42e7dSPeter Dunlap 	/* We only call idm_task_start for regular tasks, not task management */
1840a6d42e7dSPeter Dunlap 	if (task->task_mgmt_function == TM_NONE) {
1841a6d42e7dSPeter Dunlap 		idm_task_done(itask->it_idm_task);
1842a6d42e7dSPeter Dunlap 		iscsit_task_free(itask);
1843a6d42e7dSPeter Dunlap 		return;
1844a6d42e7dSPeter Dunlap 	} else {
1845a6d42e7dSPeter Dunlap 		iscsit_tm_task_free(itask);
1846a6d42e7dSPeter Dunlap 	}
1847a6d42e7dSPeter Dunlap }
1848a6d42e7dSPeter Dunlap 
1849a6d42e7dSPeter Dunlap /*ARGSUSED*/
1850a6d42e7dSPeter Dunlap stmf_status_t
1851a6d42e7dSPeter Dunlap iscsit_abort(stmf_local_port_t *lport, int abort_cmd, void *arg, uint32_t flags)
1852a6d42e7dSPeter Dunlap {
1853a6d42e7dSPeter Dunlap 	scsi_task_t	*st = (scsi_task_t *)arg;
1854a6d42e7dSPeter Dunlap 	iscsit_task_t	*iscsit_task;
1855a6d42e7dSPeter Dunlap 	idm_task_t	*idt;
1856a6d42e7dSPeter Dunlap 
1857a6d42e7dSPeter Dunlap 	/*
1858a6d42e7dSPeter Dunlap 	 * If this is a task management request then there's really not much to
1859a6d42e7dSPeter Dunlap 	 * do.
1860a6d42e7dSPeter Dunlap 	 */
1861a6d42e7dSPeter Dunlap 	if (st->task_mgmt_function != TM_NONE) {
1862a6d42e7dSPeter Dunlap 		return (STMF_ABORT_SUCCESS);
1863a6d42e7dSPeter Dunlap 	}
1864a6d42e7dSPeter Dunlap 
1865a6d42e7dSPeter Dunlap 	/*
1866a6d42e7dSPeter Dunlap 	 * Regular task, start cleaning up
1867a6d42e7dSPeter Dunlap 	 */
1868a6d42e7dSPeter Dunlap 	iscsit_task = st->task_port_private;
1869a6d42e7dSPeter Dunlap 	idt = iscsit_task->it_idm_task;
1870a6d42e7dSPeter Dunlap 	mutex_enter(&iscsit_task->it_mutex);
1871a6d42e7dSPeter Dunlap 	iscsit_task->it_stmf_abort = B_TRUE;
1872a6d42e7dSPeter Dunlap 	if (iscsit_task->it_aborted) {
1873a6d42e7dSPeter Dunlap 		mutex_exit(&iscsit_task->it_mutex);
187472cf3143Speter dunlap 		/*
187572cf3143Speter dunlap 		 * Task is no longer active
187672cf3143Speter dunlap 		 */
187772cf3143Speter dunlap 		iscsit_task_done(iscsit_task);
187872cf3143Speter dunlap 
1879a6d42e7dSPeter Dunlap 		/*
1880a6d42e7dSPeter Dunlap 		 * STMF specification is wrong... says to return
1881a6d42e7dSPeter Dunlap 		 * STMF_ABORTED, the code actually looks for
1882a6d42e7dSPeter Dunlap 		 * STMF_ABORT_SUCCESS.
1883a6d42e7dSPeter Dunlap 		 */
1884a6d42e7dSPeter Dunlap 		return (STMF_ABORT_SUCCESS);
1885a6d42e7dSPeter Dunlap 	} else {
1886a6d42e7dSPeter Dunlap 		mutex_exit(&iscsit_task->it_mutex);
1887a6d42e7dSPeter Dunlap 		/*
1888a6d42e7dSPeter Dunlap 		 * Call IDM to abort the task.  Due to a variety of
1889a6d42e7dSPeter Dunlap 		 * circumstances the task may already be in the process of
1890a6d42e7dSPeter Dunlap 		 * aborting.
1891a6d42e7dSPeter Dunlap 		 * We'll let IDM worry about rationalizing all that except
1892a6d42e7dSPeter Dunlap 		 * for one particular instance.  If the state of the task
1893a6d42e7dSPeter Dunlap 		 * is TASK_COMPLETE, we need to indicate to the framework
1894a6d42e7dSPeter Dunlap 		 * that we are in fact done.  This typically happens with
1895a6d42e7dSPeter Dunlap 		 * framework-initiated task management type requests
1896a6d42e7dSPeter Dunlap 		 * (e.g. abort task).
1897a6d42e7dSPeter Dunlap 		 */
1898a6d42e7dSPeter Dunlap 		if (idt->idt_state == TASK_COMPLETE) {
1899a6d42e7dSPeter Dunlap 			idm_refcnt_wait_ref(&idt->idt_refcnt);
1900a6d42e7dSPeter Dunlap 			return (STMF_ABORT_SUCCESS);
1901a6d42e7dSPeter Dunlap 		} else {
1902a6d42e7dSPeter Dunlap 			idm_task_abort(idt->idt_ic, idt, AT_TASK_MGMT_ABORT);
1903a6d42e7dSPeter Dunlap 			return (STMF_SUCCESS);
1904a6d42e7dSPeter Dunlap 		}
1905a6d42e7dSPeter Dunlap 	}
1906a6d42e7dSPeter Dunlap 
1907a6d42e7dSPeter Dunlap 	/*NOTREACHED*/
1908a6d42e7dSPeter Dunlap }
1909a6d42e7dSPeter Dunlap 
1910a6d42e7dSPeter Dunlap /*ARGSUSED*/
1911a6d42e7dSPeter Dunlap void
1912a6d42e7dSPeter Dunlap iscsit_ctl(stmf_local_port_t *lport, int cmd, void *arg)
1913a6d42e7dSPeter Dunlap {
1914a6d42e7dSPeter Dunlap 	iscsit_tgt_t		*iscsit_tgt;
1915a6d42e7dSPeter Dunlap 
1916a6d42e7dSPeter Dunlap 	ASSERT((cmd == STMF_CMD_LPORT_ONLINE) ||
1917a6d42e7dSPeter Dunlap 	    (cmd == STMF_ACK_LPORT_ONLINE_COMPLETE) ||
1918a6d42e7dSPeter Dunlap 	    (cmd == STMF_CMD_LPORT_OFFLINE) ||
1919a6d42e7dSPeter Dunlap 	    (cmd == STMF_ACK_LPORT_OFFLINE_COMPLETE));
1920a6d42e7dSPeter Dunlap 
1921a6d42e7dSPeter Dunlap 	iscsit_tgt = (iscsit_tgt_t *)lport->lport_port_private;
1922a6d42e7dSPeter Dunlap 
1923a6d42e7dSPeter Dunlap 	switch (cmd) {
1924a6d42e7dSPeter Dunlap 	case STMF_CMD_LPORT_ONLINE:
1925a6d42e7dSPeter Dunlap 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_REQ);
1926a6d42e7dSPeter Dunlap 		break;
1927a6d42e7dSPeter Dunlap 	case STMF_CMD_LPORT_OFFLINE:
1928a6d42e7dSPeter Dunlap 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_REQ);
1929a6d42e7dSPeter Dunlap 		break;
1930a6d42e7dSPeter Dunlap 	case STMF_ACK_LPORT_ONLINE_COMPLETE:
1931a6d42e7dSPeter Dunlap 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_COMPLETE_ACK);
1932a6d42e7dSPeter Dunlap 		break;
1933a6d42e7dSPeter Dunlap 	case STMF_ACK_LPORT_OFFLINE_COMPLETE:
1934a6d42e7dSPeter Dunlap 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_COMPLETE_ACK);
1935a6d42e7dSPeter Dunlap 		break;
1936a6d42e7dSPeter Dunlap 
1937a6d42e7dSPeter Dunlap 	default:
1938a6d42e7dSPeter Dunlap 		break;
1939a6d42e7dSPeter Dunlap 	}
1940a6d42e7dSPeter Dunlap }
1941a6d42e7dSPeter Dunlap 
1942a6d42e7dSPeter Dunlap static stmf_status_t
1943a6d42e7dSPeter Dunlap iscsit_idm_to_stmf(idm_status_t idmrc)
1944a6d42e7dSPeter Dunlap {
1945a6d42e7dSPeter Dunlap 	switch (idmrc) {
1946a6d42e7dSPeter Dunlap 	case IDM_STATUS_SUCCESS:
1947a6d42e7dSPeter Dunlap 		return (STMF_SUCCESS);
1948a6d42e7dSPeter Dunlap 	default:
1949a6d42e7dSPeter Dunlap 		return (STMF_FAILURE);
1950a6d42e7dSPeter Dunlap 	}
1951a6d42e7dSPeter Dunlap 	/*NOTREACHED*/
1952a6d42e7dSPeter Dunlap }
1953a6d42e7dSPeter Dunlap 
1954d618d68dSPriya Krishnan void
1955d618d68dSPriya Krishnan iscsit_op_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu)
1956d618d68dSPriya Krishnan {
1957d618d68dSPriya Krishnan 	iscsit_conn_t		*ict = ic->ic_handle;
1958d618d68dSPriya Krishnan 
1959d618d68dSPriya Krishnan 	if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
1960d618d68dSPriya Krishnan 		iscsit_post_scsi_cmd(ic, rx_pdu);
1961d618d68dSPriya Krishnan 	}
1962d618d68dSPriya Krishnan 	iscsit_process_pdu_in_queue(ict->ict_sess);
1963d618d68dSPriya Krishnan }
1964a6d42e7dSPeter Dunlap 
1965a6d42e7dSPeter Dunlap /*
1966a6d42e7dSPeter Dunlap  * ISCSI protocol
1967a6d42e7dSPeter Dunlap  */
1968a6d42e7dSPeter Dunlap 
1969a6d42e7dSPeter Dunlap void
1970d618d68dSPriya Krishnan iscsit_post_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu)
1971a6d42e7dSPeter Dunlap {
1972a6d42e7dSPeter Dunlap 	iscsit_conn_t		*ict;
1973a6d42e7dSPeter Dunlap 	iscsit_task_t		*itask;
1974a6d42e7dSPeter Dunlap 	scsi_task_t		*task;
1975a6d42e7dSPeter Dunlap 	iscsit_buf_t		*ibuf;
1976a6d42e7dSPeter Dunlap 	iscsi_scsi_cmd_hdr_t	*iscsi_scsi =
1977a6d42e7dSPeter Dunlap 	    (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
1978a6d42e7dSPeter Dunlap 	iscsi_addl_hdr_t	*ahs_hdr;
1979a6d42e7dSPeter Dunlap 	uint16_t		addl_cdb_len = 0;
1980a6d42e7dSPeter Dunlap 
1981a6d42e7dSPeter Dunlap 	ict = ic->ic_handle;
1982a6d42e7dSPeter Dunlap 
1983a6d42e7dSPeter Dunlap 	itask = iscsit_task_alloc(ict);
1984a6d42e7dSPeter Dunlap 	if (itask == NULL) {
198572cf3143Speter dunlap 		/* Finish processing request */
198672cf3143Speter dunlap 		iscsit_set_cmdsn(ict, rx_pdu);
198772cf3143Speter dunlap 
1988a6d42e7dSPeter Dunlap 		iscsit_send_direct_scsi_resp(ict, rx_pdu,
1989a6d42e7dSPeter Dunlap 		    ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
1990a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
1991a6d42e7dSPeter Dunlap 		return;
1992a6d42e7dSPeter Dunlap 	}
1993a6d42e7dSPeter Dunlap 
1994a6d42e7dSPeter Dunlap 	/*
1995a6d42e7dSPeter Dunlap 	 * Note CmdSN and ITT in task.  IDM will have already validated this
1996a6d42e7dSPeter Dunlap 	 * request against the connection state so we don't need to check
1997a6d42e7dSPeter Dunlap 	 * that (the connection may have changed state in the meantime but
1998a6d42e7dSPeter Dunlap 	 * we will catch that when we try to send a response)
1999a6d42e7dSPeter Dunlap 	 */
2000a6d42e7dSPeter Dunlap 	itask->it_cmdsn = ntohl(iscsi_scsi->cmdsn);
2001a6d42e7dSPeter Dunlap 	itask->it_itt = iscsi_scsi->itt;
2002a6d42e7dSPeter Dunlap 
2003a6d42e7dSPeter Dunlap 	/*
2004a6d42e7dSPeter Dunlap 	 * Check for extended CDB AHS
2005a6d42e7dSPeter Dunlap 	 */
2006a6d42e7dSPeter Dunlap 	if (iscsi_scsi->hlength > 0) {
2007a6d42e7dSPeter Dunlap 		ahs_hdr = (iscsi_addl_hdr_t *)iscsi_scsi;
2008a6d42e7dSPeter Dunlap 		addl_cdb_len = ((ahs_hdr->ahs_hlen_hi << 8) |
2009a6d42e7dSPeter Dunlap 		    ahs_hdr->ahs_hlen_lo) - 1; /* Adjust for reserved byte */
2010a6d42e7dSPeter Dunlap 		if (((addl_cdb_len + 4) / sizeof (uint32_t)) >
2011a6d42e7dSPeter Dunlap 		    iscsi_scsi->hlength) {
2012a6d42e7dSPeter Dunlap 			/* Mangled header info, drop it */
2013a6d42e7dSPeter Dunlap 			idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2014a6d42e7dSPeter Dunlap 			return;
2015a6d42e7dSPeter Dunlap 		}
2016a6d42e7dSPeter Dunlap 	}
2017a6d42e7dSPeter Dunlap 
2018a6d42e7dSPeter Dunlap 	ict = rx_pdu->isp_ic->ic_handle; /* IDM client private */
2019a6d42e7dSPeter Dunlap 
202072cf3143Speter dunlap 	/*
202172cf3143Speter dunlap 	 * Add task to session list.  This function will also check to
202272cf3143Speter dunlap 	 * ensure that the task does not already exist.
202372cf3143Speter dunlap 	 */
202472cf3143Speter dunlap 	if (iscsit_task_start(itask) != IDM_STATUS_SUCCESS) {
202572cf3143Speter dunlap 		/*
202672cf3143Speter dunlap 		 * Task exists, free all resources and reject.  Don't
202772cf3143Speter dunlap 		 * update expcmdsn in this case because RFC 3720 says
202872cf3143Speter dunlap 		 * "The CmdSN of the rejected command PDU (if it is a
202972cf3143Speter dunlap 		 * non-immediate command) MUST NOT be considered received
203072cf3143Speter dunlap 		 * by the target (i.e., a command sequence gap must be
203172cf3143Speter dunlap 		 * assumed for the CmdSN), even though the CmdSN of the
203272cf3143Speter dunlap 		 * rejected command PDU may be reliably ascertained.  Upon
203372cf3143Speter dunlap 		 * receiving the Reject, the initiator MUST plug the CmdSN
203472cf3143Speter dunlap 		 * gap in order to continue to use the session.  The gap
203572cf3143Speter dunlap 		 * may be plugged either by transmitting a command PDU
203672cf3143Speter dunlap 		 * with the same CmdSN, or by aborting the task (see section
203772cf3143Speter dunlap 		 * 6.9 on how an abort may plug a CmdSN gap)." (Section 6.3)
203872cf3143Speter dunlap 		 */
203972cf3143Speter dunlap 		iscsit_task_free(itask);
204072cf3143Speter dunlap 		iscsit_send_reject(ict, rx_pdu, ISCSI_REJECT_TASK_IN_PROGRESS);
204172cf3143Speter dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
204272cf3143Speter dunlap 		return;
204372cf3143Speter dunlap 	}
204472cf3143Speter dunlap 
204572cf3143Speter dunlap 	/* Update sequence numbers */
204672cf3143Speter dunlap 	iscsit_set_cmdsn(ict, rx_pdu);
204772cf3143Speter dunlap 
204872cf3143Speter dunlap 	/*
204972cf3143Speter dunlap 	 * Allocate STMF task
205072cf3143Speter dunlap 	 */
2051a6d42e7dSPeter Dunlap 	itask->it_stmf_task = stmf_task_alloc(
2052a6d42e7dSPeter Dunlap 	    itask->it_ict->ict_sess->ist_lport,
2053a6d42e7dSPeter Dunlap 	    itask->it_ict->ict_sess->ist_stmf_sess, iscsi_scsi->lun,
2054a6d42e7dSPeter Dunlap 	    16 + addl_cdb_len, 0);
2055a6d42e7dSPeter Dunlap 	if (itask->it_stmf_task == NULL) {
2056a6d42e7dSPeter Dunlap 		/*
2057a6d42e7dSPeter Dunlap 		 * Either stmf really couldn't get memory for a task or,
2058a6d42e7dSPeter Dunlap 		 * more likely, the LU is currently in reset.  Either way
2059a6d42e7dSPeter Dunlap 		 * we have no choice but to fail the request.
2060a6d42e7dSPeter Dunlap 		 */
206172cf3143Speter dunlap 		iscsit_task_done(itask);
2062a6d42e7dSPeter Dunlap 		iscsit_task_free(itask);
2063a6d42e7dSPeter Dunlap 		iscsit_send_direct_scsi_resp(ict, rx_pdu,
2064a6d42e7dSPeter Dunlap 		    ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
206572cf3143Speter dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2066a6d42e7dSPeter Dunlap 		return;
2067a6d42e7dSPeter Dunlap 	}
2068a6d42e7dSPeter Dunlap 
2069a6d42e7dSPeter Dunlap 	task = itask->it_stmf_task;
2070a6d42e7dSPeter Dunlap 	task->task_port_private = itask;
2071a6d42e7dSPeter Dunlap 
2072a6d42e7dSPeter Dunlap 	bcopy(iscsi_scsi->lun, task->task_lun_no, sizeof (task->task_lun_no));
2073a6d42e7dSPeter Dunlap 
2074a6d42e7dSPeter Dunlap 	/*
2075a6d42e7dSPeter Dunlap 	 * iSCSI and Comstar use the same values.  Should we rely on this
2076a6d42e7dSPeter Dunlap 	 * or translate them bit-wise?
2077a6d42e7dSPeter Dunlap 	 */
2078a6d42e7dSPeter Dunlap 
2079a6d42e7dSPeter Dunlap 	task->task_flags =
2080a6d42e7dSPeter Dunlap 	    (((iscsi_scsi->flags & ISCSI_FLAG_CMD_READ) ? TF_READ_DATA : 0) |
2081a6d42e7dSPeter Dunlap 	    ((iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ? TF_WRITE_DATA : 0) |
2082a6d42e7dSPeter Dunlap 	    ((rx_pdu->isp_datalen == 0) ? 0 : TF_INITIAL_BURST));
2083a6d42e7dSPeter Dunlap 
2084a6d42e7dSPeter Dunlap 	switch (iscsi_scsi->flags & ISCSI_FLAG_CMD_ATTR_MASK) {
2085a6d42e7dSPeter Dunlap 	case ISCSI_ATTR_UNTAGGED:
2086a6d42e7dSPeter Dunlap 		break;
2087a6d42e7dSPeter Dunlap 	case ISCSI_ATTR_SIMPLE:
2088a6d42e7dSPeter Dunlap 		task->task_additional_flags |= TF_ATTR_SIMPLE_QUEUE;
2089a6d42e7dSPeter Dunlap 		break;
2090a6d42e7dSPeter Dunlap 	case ISCSI_ATTR_ORDERED:
2091a6d42e7dSPeter Dunlap 		task->task_additional_flags |= TF_ATTR_ORDERED_QUEUE;
2092a6d42e7dSPeter Dunlap 		break;
2093a6d42e7dSPeter Dunlap 	case ISCSI_ATTR_HEAD_OF_QUEUE:
2094a6d42e7dSPeter Dunlap 		task->task_additional_flags |= TF_ATTR_HEAD_OF_QUEUE;
2095a6d42e7dSPeter Dunlap 		break;
2096a6d42e7dSPeter Dunlap 	case ISCSI_ATTR_ACA:
2097a6d42e7dSPeter Dunlap 		task->task_additional_flags |= TF_ATTR_ACA;
2098a6d42e7dSPeter Dunlap 		break;
2099a6d42e7dSPeter Dunlap 	default:
2100a6d42e7dSPeter Dunlap 		/* Protocol error but just take it, treat as untagged */
2101a6d42e7dSPeter Dunlap 		break;
2102a6d42e7dSPeter Dunlap 	}
2103a6d42e7dSPeter Dunlap 
2104a6d42e7dSPeter Dunlap 
2105a6d42e7dSPeter Dunlap 	task->task_additional_flags = 0;
2106a6d42e7dSPeter Dunlap 	task->task_priority = 0;
2107a6d42e7dSPeter Dunlap 	task->task_mgmt_function = TM_NONE;
2108a6d42e7dSPeter Dunlap 
2109a6d42e7dSPeter Dunlap 	/*
2110a6d42e7dSPeter Dunlap 	 * This "task_max_nbufs" doesn't map well to BIDI.  We probably need
2111a6d42e7dSPeter Dunlap 	 * parameter for each direction.  "MaxOutstandingR2T" may very well
2112a6d42e7dSPeter Dunlap 	 * be set to one which could prevent us from doing simultaneous
2113a6d42e7dSPeter Dunlap 	 * transfers in each direction.
2114a6d42e7dSPeter Dunlap 	 */
2115a6d42e7dSPeter Dunlap 	task->task_max_nbufs = (iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ?
2116a6d42e7dSPeter Dunlap 	    ict->ict_op.op_max_outstanding_r2t : STMF_BUFS_MAX;
2117a6d42e7dSPeter Dunlap 	task->task_cmd_seq_no = ntohl(iscsi_scsi->itt);
2118a6d42e7dSPeter Dunlap 	task->task_expected_xfer_length = ntohl(iscsi_scsi->data_length);
2119a6d42e7dSPeter Dunlap 
2120a6d42e7dSPeter Dunlap 	/* Copy CDB */
2121a6d42e7dSPeter Dunlap 	bcopy(iscsi_scsi->scb, task->task_cdb, 16);
2122a6d42e7dSPeter Dunlap 	if (addl_cdb_len > 0) {
2123a6d42e7dSPeter Dunlap 		bcopy(ahs_hdr->ahs_extscb, task->task_cdb + 16, addl_cdb_len);
2124a6d42e7dSPeter Dunlap 	}
2125a6d42e7dSPeter Dunlap 
2126a668b114SPriya Krishnan 	DTRACE_ISCSI_3(scsi__command, idm_conn_t *, ic,
2127a668b114SPriya Krishnan 	    iscsi_scsi_cmd_hdr_t *, (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr,
2128a668b114SPriya Krishnan 	    scsi_task_t *, task);
2129a668b114SPriya Krishnan 
2130a6d42e7dSPeter Dunlap 	/*
2131a6d42e7dSPeter Dunlap 	 * Copy the transport header into the task handle from the PDU
2132a6d42e7dSPeter Dunlap 	 * handle. The transport header describes this task's remote tagged
2133a6d42e7dSPeter Dunlap 	 * buffer.
2134a6d42e7dSPeter Dunlap 	 */
2135a6d42e7dSPeter Dunlap 	if (rx_pdu->isp_transport_hdrlen != 0) {
2136a6d42e7dSPeter Dunlap 		bcopy(rx_pdu->isp_transport_hdr,
2137a6d42e7dSPeter Dunlap 		    itask->it_idm_task->idt_transport_hdr,
2138a6d42e7dSPeter Dunlap 		    rx_pdu->isp_transport_hdrlen);
2139a6d42e7dSPeter Dunlap 	}
2140a6d42e7dSPeter Dunlap 
2141a6d42e7dSPeter Dunlap 	/*
2142a6d42e7dSPeter Dunlap 	 * Tell IDM about our new active task
2143a6d42e7dSPeter Dunlap 	 */
2144a6d42e7dSPeter Dunlap 	idm_task_start(itask->it_idm_task, (uintptr_t)itask->it_itt);
2145a6d42e7dSPeter Dunlap 
2146a6d42e7dSPeter Dunlap 	/*
2147a6d42e7dSPeter Dunlap 	 * If we have any immediate data then setup the immediate buffer
2148a6d42e7dSPeter Dunlap 	 * context that comes with the task
2149a6d42e7dSPeter Dunlap 	 */
2150a6d42e7dSPeter Dunlap 	if (rx_pdu->isp_datalen) {
2151a6d42e7dSPeter Dunlap 		ibuf = itask->it_immed_data;
2152a6d42e7dSPeter Dunlap 		ibuf->ibuf_immed_data_pdu = rx_pdu;
2153a6d42e7dSPeter Dunlap 		ibuf->ibuf_stmf_buf->db_data_size = rx_pdu->isp_datalen;
2154a6d42e7dSPeter Dunlap 		ibuf->ibuf_stmf_buf->db_buf_size = rx_pdu->isp_datalen;
2155a6d42e7dSPeter Dunlap 		ibuf->ibuf_stmf_buf->db_relative_offset = 0;
2156a6d42e7dSPeter Dunlap 		ibuf->ibuf_stmf_buf->db_sglist[0].seg_length =
2157a6d42e7dSPeter Dunlap 		    rx_pdu->isp_datalen;
2158a6d42e7dSPeter Dunlap 		ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr = rx_pdu->isp_data;
2159a6d42e7dSPeter Dunlap 
2160a668b114SPriya Krishnan 		DTRACE_ISCSI_8(xfer__start, idm_conn_t *, ic,
2161a668b114SPriya Krishnan 		    uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
2162a668b114SPriya Krishnan 		    uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
2163a668b114SPriya Krishnan 		    uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
2164a668b114SPriya Krishnan 		    uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
2165a668b114SPriya Krishnan 
21663badd853SPriya Krishnan 		/*
21673badd853SPriya Krishnan 		 * For immediate data transfer, there is no callback from
21683badd853SPriya Krishnan 		 * stmf to indicate that the initial burst of data is
21693badd853SPriya Krishnan 		 * transferred successfully. In some cases, the task can
21703badd853SPriya Krishnan 		 * get freed before execution returns from stmf_post_task.
21713badd853SPriya Krishnan 		 * Although this xfer-start/done probe accurately tracks
21723badd853SPriya Krishnan 		 * the size of the transfer, it does only provide a best
21733badd853SPriya Krishnan 		 * effort on the timing of the transfer.
21743badd853SPriya Krishnan 		 */
2175a668b114SPriya Krishnan 		DTRACE_ISCSI_8(xfer__done, idm_conn_t *, ic,
2176a668b114SPriya Krishnan 		    uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
2177a668b114SPriya Krishnan 		    uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
2178a668b114SPriya Krishnan 		    uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
2179a668b114SPriya Krishnan 		    uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
21803badd853SPriya Krishnan 		stmf_post_task(task, ibuf->ibuf_stmf_buf);
2181a6d42e7dSPeter Dunlap 	} else {
21823badd853SPriya Krishnan 
2183a6d42e7dSPeter Dunlap 		stmf_post_task(task, NULL);
2184a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2185a6d42e7dSPeter Dunlap 	}
2186a6d42e7dSPeter Dunlap }
2187a6d42e7dSPeter Dunlap 
2188a6d42e7dSPeter Dunlap void
2189a6d42e7dSPeter Dunlap iscsit_deferred_dispatch(idm_pdu_t *rx_pdu)
2190a6d42e7dSPeter Dunlap {
2191a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
2192a6d42e7dSPeter Dunlap 
2193241996a3SDan McDonald 	/*
2194241996a3SDan McDonald 	 * If this isn't a login packet, we need a session.  Otherwise
2195241996a3SDan McDonald 	 * this is a protocol error (perhaps one IDM should've caught?).
2196241996a3SDan McDonald 	 */
2197241996a3SDan McDonald 	if (IDM_PDU_OPCODE(rx_pdu) != ISCSI_OP_LOGIN_CMD &&
2198241996a3SDan McDonald 	    ict->ict_sess == NULL) {
2199241996a3SDan McDonald 		DTRACE_PROBE2(iscsi__idm__deferred__no__session,
2200241996a3SDan McDonald 		    iscsit_conn_t *, ict, idm_pdu_t *, rx_pdu);
2201241996a3SDan McDonald 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2202241996a3SDan McDonald 		return;
2203241996a3SDan McDonald 	}
2204241996a3SDan McDonald 
2205a6d42e7dSPeter Dunlap 	/*
2206a6d42e7dSPeter Dunlap 	 * If the connection has been lost then ignore new PDU's
2207a6d42e7dSPeter Dunlap 	 */
2208a6d42e7dSPeter Dunlap 	mutex_enter(&ict->ict_mutex);
2209a6d42e7dSPeter Dunlap 	if (ict->ict_lost) {
2210a6d42e7dSPeter Dunlap 		mutex_exit(&ict->ict_mutex);
2211a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2212a6d42e7dSPeter Dunlap 		return;
2213a6d42e7dSPeter Dunlap 	}
2214a6d42e7dSPeter Dunlap 
2215a6d42e7dSPeter Dunlap 	/*
2216a6d42e7dSPeter Dunlap 	 * Grab a hold on the connection to prevent it from going away
2217a6d42e7dSPeter Dunlap 	 * between now and when the taskq function is called.
2218a6d42e7dSPeter Dunlap 	 */
2219a6d42e7dSPeter Dunlap 	iscsit_conn_dispatch_hold(ict);
2220a6d42e7dSPeter Dunlap 	mutex_exit(&ict->ict_mutex);
2221a6d42e7dSPeter Dunlap 
22226319b0c7SGarrett D'Amore 	taskq_dispatch_ent(iscsit_global.global_dispatch_taskq,
22236319b0c7SGarrett D'Amore 	    iscsit_deferred, rx_pdu, 0, &rx_pdu->isp_tqent);
2224a6d42e7dSPeter Dunlap }
2225a6d42e7dSPeter Dunlap 
2226a6d42e7dSPeter Dunlap static void
2227a6d42e7dSPeter Dunlap iscsit_deferred(void *rx_pdu_void)
2228a6d42e7dSPeter Dunlap {
2229d618d68dSPriya Krishnan 	idm_pdu_t		*rx_pdu = rx_pdu_void;
2230d618d68dSPriya Krishnan 	idm_conn_t		*ic = rx_pdu->isp_ic;
2231d618d68dSPriya Krishnan 	iscsit_conn_t		*ict = ic->ic_handle;
2232a6d42e7dSPeter Dunlap 
2233d618d68dSPriya Krishnan 	/*
2234d618d68dSPriya Krishnan 	 * NOP and Task Management Commands can be marked for immediate
2235d618d68dSPriya Krishnan 	 * delivery. Commands marked as 'Immediate' are to be considered
2236d618d68dSPriya Krishnan 	 * for execution as soon as they arrive on the target. So these
2237d618d68dSPriya Krishnan 	 * should not be checked for sequence order and put in a queue.
2238d618d68dSPriya Krishnan 	 * The CmdSN is not advanced for Immediate Commands.
2239d618d68dSPriya Krishnan 	 */
2240a6d42e7dSPeter Dunlap 	switch (IDM_PDU_OPCODE(rx_pdu)) {
2241a6d42e7dSPeter Dunlap 	case ISCSI_OP_NOOP_OUT:
2242d618d68dSPriya Krishnan 		if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2243d618d68dSPriya Krishnan 			iscsit_set_cmdsn(ict, rx_pdu);
2244d618d68dSPriya Krishnan 			iscsit_pdu_op_noop(ict, rx_pdu);
2245d618d68dSPriya Krishnan 		}
2246a6d42e7dSPeter Dunlap 		break;
2247a6d42e7dSPeter Dunlap 	case ISCSI_OP_LOGIN_CMD:
2248a6d42e7dSPeter Dunlap 		iscsit_pdu_op_login_cmd(ict, rx_pdu);
2249d618d68dSPriya Krishnan 		iscsit_conn_dispatch_rele(ict);
2250d618d68dSPriya Krishnan 		return;
2251a6d42e7dSPeter Dunlap 	case ISCSI_OP_TEXT_CMD:
2252d618d68dSPriya Krishnan 		if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2253d618d68dSPriya Krishnan 			iscsit_set_cmdsn(ict, rx_pdu);
2254d618d68dSPriya Krishnan 			iscsit_pdu_op_text_cmd(ict, rx_pdu);
2255d618d68dSPriya Krishnan 		}
2256a6d42e7dSPeter Dunlap 		break;
2257a6d42e7dSPeter Dunlap 	case ISCSI_OP_LOGOUT_CMD:
2258d618d68dSPriya Krishnan 		if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2259d618d68dSPriya Krishnan 			iscsit_set_cmdsn(ict, rx_pdu);
2260d618d68dSPriya Krishnan 			iscsit_pdu_op_logout_cmd(ict, rx_pdu);
2261d618d68dSPriya Krishnan 		}
2262a6d42e7dSPeter Dunlap 		break;
2263a6d42e7dSPeter Dunlap 	default:
2264a6d42e7dSPeter Dunlap 		/* Protocol error.  IDM should have caught this */
2265a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2266a6d42e7dSPeter Dunlap 		ASSERT(0);
2267a6d42e7dSPeter Dunlap 		break;
2268a6d42e7dSPeter Dunlap 	}
2269d618d68dSPriya Krishnan 	/*
2270d618d68dSPriya Krishnan 	 * Check if there are other PDUs in the session staging queue
2271d618d68dSPriya Krishnan 	 * waiting to be posted to SCSI layer.
2272d618d68dSPriya Krishnan 	 */
2273d618d68dSPriya Krishnan 	iscsit_process_pdu_in_queue(ict->ict_sess);
2274a6d42e7dSPeter Dunlap 
2275a6d42e7dSPeter Dunlap 	iscsit_conn_dispatch_rele(ict);
2276a6d42e7dSPeter Dunlap }
2277a6d42e7dSPeter Dunlap 
2278a6d42e7dSPeter Dunlap static void
2279a6d42e7dSPeter Dunlap iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu,
2280a6d42e7dSPeter Dunlap     uint8_t response, uint8_t cmd_status)
2281a6d42e7dSPeter Dunlap {
2282a6d42e7dSPeter Dunlap 	idm_pdu_t			*rsp_pdu;
2283a6d42e7dSPeter Dunlap 	idm_conn_t			*ic;
2284a6d42e7dSPeter Dunlap 	iscsi_scsi_rsp_hdr_t		*resp;
228530e7468fSPeter Dunlap 	iscsi_scsi_cmd_hdr_t		*req =
228630e7468fSPeter Dunlap 	    (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2287a6d42e7dSPeter Dunlap 
2288a6d42e7dSPeter Dunlap 	ic = ict->ict_ic;
2289a6d42e7dSPeter Dunlap 
2290a6d42e7dSPeter Dunlap 	rsp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_rsp_hdr_t), 0);
2291a6d42e7dSPeter Dunlap 	idm_pdu_init(rsp_pdu, ic, NULL, NULL);
229260220f10SPriya Krishnan 	/*
229360220f10SPriya Krishnan 	 * StatSN is incremented by 1 for every response sent on
229460220f10SPriya Krishnan 	 * a connection except for responses sent as a result of
229560220f10SPriya Krishnan 	 * a retry or SNACK
229660220f10SPriya Krishnan 	 */
229760220f10SPriya Krishnan 	rsp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
229860220f10SPriya Krishnan 
2299a6d42e7dSPeter Dunlap 	resp = (iscsi_scsi_rsp_hdr_t *)rsp_pdu->isp_hdr;
2300a6d42e7dSPeter Dunlap 
2301a6d42e7dSPeter Dunlap 	resp->opcode = ISCSI_OP_SCSI_RSP;
2302a6d42e7dSPeter Dunlap 	resp->flags = ISCSI_FLAG_FINAL;
2303a6d42e7dSPeter Dunlap 	resp->response = response;
2304a6d42e7dSPeter Dunlap 	resp->cmd_status = cmd_status;
230530e7468fSPeter Dunlap 	resp->itt = req->itt;
230630e7468fSPeter Dunlap 	if ((response == ISCSI_STATUS_CMD_COMPLETED) &&
230730e7468fSPeter Dunlap 	    (req->data_length != 0) &&
230830e7468fSPeter Dunlap 	    ((req->flags & ISCSI_FLAG_CMD_READ) ||
230930e7468fSPeter Dunlap 	    (req->flags & ISCSI_FLAG_CMD_WRITE))) {
231030e7468fSPeter Dunlap 		resp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
231130e7468fSPeter Dunlap 		resp->residual_count = req->data_length;
231230e7468fSPeter Dunlap 	}
231330e7468fSPeter Dunlap 
231430e7468fSPeter Dunlap 	DTRACE_PROBE4(iscsi__scsi__direct__response,
231530e7468fSPeter Dunlap 	    iscsit_conn_t *, ict,
231630e7468fSPeter Dunlap 	    uint8_t, resp->response,
231730e7468fSPeter Dunlap 	    uint8_t, resp->cmd_status,
231830e7468fSPeter Dunlap 	    idm_pdu_t *, rsp_pdu);
2319a6d42e7dSPeter Dunlap 
2320a6d42e7dSPeter Dunlap 	iscsit_pdu_tx(rsp_pdu);
2321a6d42e7dSPeter Dunlap }
2322a6d42e7dSPeter Dunlap 
2323a6d42e7dSPeter Dunlap void
2324a6d42e7dSPeter Dunlap iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu, uint8_t tm_status)
2325a6d42e7dSPeter Dunlap {
2326a6d42e7dSPeter Dunlap 	iscsi_scsi_task_mgt_rsp_hdr_t	*tm_resp;
2327a6d42e7dSPeter Dunlap 
232860220f10SPriya Krishnan 	/*
232960220f10SPriya Krishnan 	 * The target must take note of the last-sent StatSN.
233060220f10SPriya Krishnan 	 * The StatSN is to be incremented after sending a
233160220f10SPriya Krishnan 	 * task management response. Digest recovery can only
233260220f10SPriya Krishnan 	 * work if StatSN is incremented.
233360220f10SPriya Krishnan 	 */
233460220f10SPriya Krishnan 	tm_resp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2335a6d42e7dSPeter Dunlap 	tm_resp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2336a6d42e7dSPeter Dunlap 	tm_resp->response = tm_status;
233730e7468fSPeter Dunlap 
233830e7468fSPeter Dunlap 	DTRACE_PROBE3(iscsi__scsi__tm__response,
233930e7468fSPeter Dunlap 	    iscsit_conn_t *, tm_resp_pdu->isp_ic->ic_handle,
234030e7468fSPeter Dunlap 	    uint8_t, tm_resp->response,
234130e7468fSPeter Dunlap 	    idm_pdu_t *, tm_resp_pdu);
2342a6d42e7dSPeter Dunlap 	iscsit_pdu_tx(tm_resp_pdu);
2343a6d42e7dSPeter Dunlap }
2344a6d42e7dSPeter Dunlap 
2345a6d42e7dSPeter Dunlap void
2346a6d42e7dSPeter Dunlap iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2347a6d42e7dSPeter Dunlap {
2348a6d42e7dSPeter Dunlap 	idm_pdu_t			*tm_resp_pdu;
2349a6d42e7dSPeter Dunlap 	iscsit_task_t			*itask;
2350a6d42e7dSPeter Dunlap 	iscsit_task_t			*tm_itask;
2351a6d42e7dSPeter Dunlap 	scsi_task_t			*task;
2352*1f99504aSToomas Soome 	iscsi_scsi_task_mgt_hdr_t	*iscsi_tm =
2353a6d42e7dSPeter Dunlap 	    (iscsi_scsi_task_mgt_hdr_t *)rx_pdu->isp_hdr;
2354*1f99504aSToomas Soome 	iscsi_scsi_task_mgt_rsp_hdr_t	*iscsi_tm_rsp =
2355a6d42e7dSPeter Dunlap 	    (iscsi_scsi_task_mgt_rsp_hdr_t *)rx_pdu->isp_hdr;
2356a6d42e7dSPeter Dunlap 	uint32_t			rtt, cmdsn, refcmdsn;
2357a6d42e7dSPeter Dunlap 	uint8_t				tm_func;
2358a6d42e7dSPeter Dunlap 
2359a6d42e7dSPeter Dunlap 	/*
2360a6d42e7dSPeter Dunlap 	 * Setup response PDU (response field will get filled in later)
2361a6d42e7dSPeter Dunlap 	 */
2362a6d42e7dSPeter Dunlap 	tm_resp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_task_mgt_rsp_hdr_t), 0);
2363a6d42e7dSPeter Dunlap 	if (tm_resp_pdu == NULL) {
2364a6d42e7dSPeter Dunlap 		/* Can't respond, just drop it */
2365a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2366a6d42e7dSPeter Dunlap 		return;
2367a6d42e7dSPeter Dunlap 	}
2368a6d42e7dSPeter Dunlap 	idm_pdu_init(tm_resp_pdu, ict->ict_ic, NULL, NULL);
2369a6d42e7dSPeter Dunlap 	iscsi_tm_rsp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2370a6d42e7dSPeter Dunlap 	bzero(iscsi_tm_rsp, sizeof (iscsi_scsi_task_mgt_rsp_hdr_t));
2371a6d42e7dSPeter Dunlap 	iscsi_tm_rsp->opcode = ISCSI_OP_SCSI_TASK_MGT_RSP;
2372a6d42e7dSPeter Dunlap 	iscsi_tm_rsp->flags = ISCSI_FLAG_FINAL;
2373a6d42e7dSPeter Dunlap 	iscsi_tm_rsp->itt = rx_pdu->isp_hdr->itt;
2374a6d42e7dSPeter Dunlap 
2375a6d42e7dSPeter Dunlap 	/*
2376a6d42e7dSPeter Dunlap 	 * Figure out what we're being asked to do.
2377a6d42e7dSPeter Dunlap 	 */
237830e7468fSPeter Dunlap 	DTRACE_PROBE4(iscsi__scsi__tm__request,
237930e7468fSPeter Dunlap 	    iscsit_conn_t *, ict,
238030e7468fSPeter Dunlap 	    uint8_t, (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK),
238130e7468fSPeter Dunlap 	    uint32_t, iscsi_tm->rtt,
238230e7468fSPeter Dunlap 	    idm_pdu_t *, rx_pdu);
2383a6d42e7dSPeter Dunlap 	switch (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK) {
2384a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_ABORT_TASK:
2385a6d42e7dSPeter Dunlap 		/*
2386a6d42e7dSPeter Dunlap 		 * STMF doesn't currently support the "abort task" task
2387a6d42e7dSPeter Dunlap 		 * management command although it does support aborting
2388a6d42e7dSPeter Dunlap 		 * an individual task.  We'll get STMF to abort the task
2389a6d42e7dSPeter Dunlap 		 * for us but handle the details of the task management
2390a6d42e7dSPeter Dunlap 		 * command ourselves.
2391a6d42e7dSPeter Dunlap 		 *
2392a6d42e7dSPeter Dunlap 		 * Find the task associated with the referenced task tag.
2393a6d42e7dSPeter Dunlap 		 */
2394a6d42e7dSPeter Dunlap 		rtt = iscsi_tm->rtt;
2395a6d42e7dSPeter Dunlap 		itask = (iscsit_task_t *)idm_task_find_by_handle(ict->ict_ic,
2396a6d42e7dSPeter Dunlap 		    (uintptr_t)rtt);
2397a6d42e7dSPeter Dunlap 
2398a6d42e7dSPeter Dunlap 		if (itask == NULL) {
2399a6d42e7dSPeter Dunlap 			cmdsn = ntohl(iscsi_tm->cmdsn);
2400a6d42e7dSPeter Dunlap 			refcmdsn = ntohl(iscsi_tm->refcmdsn);
2401a6d42e7dSPeter Dunlap 
2402a6d42e7dSPeter Dunlap 			/*
2403d618d68dSPriya Krishnan 			 * Task was not found. But the SCSI command could be
2404d618d68dSPriya Krishnan 			 * on the rxpdu wait queue. If RefCmdSN is within
2405d618d68dSPriya Krishnan 			 * the CmdSN window and less than CmdSN of the TM
2406d618d68dSPriya Krishnan 			 * function, return "Function Complete". Otherwise,
2407d618d68dSPriya Krishnan 			 * return "Task Does Not Exist".
2408a6d42e7dSPeter Dunlap 			 */
2409a6d42e7dSPeter Dunlap 
2410a6d42e7dSPeter Dunlap 			if (iscsit_cmdsn_in_window(ict, refcmdsn) &&
2411d618d68dSPriya Krishnan 			    iscsit_sna_lt(refcmdsn, cmdsn)) {
2412d618d68dSPriya Krishnan 				mutex_enter(&ict->ict_sess->ist_sn_mutex);
2413a481fa48SRick McNeal 				if (iscsit_remove_pdu_from_queue(
2414a481fa48SRick McNeal 				    ict->ict_sess, refcmdsn)) {
2415a481fa48SRick McNeal 					iscsit_conn_dispatch_rele(ict);
2416a481fa48SRick McNeal 				}
2417d618d68dSPriya Krishnan 				mutex_exit(&ict->ict_sess->ist_sn_mutex);
2418a6d42e7dSPeter Dunlap 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2419a6d42e7dSPeter Dunlap 				    SCSI_TCP_TM_RESP_COMPLETE);
2420a6d42e7dSPeter Dunlap 			} else {
2421a6d42e7dSPeter Dunlap 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2422a6d42e7dSPeter Dunlap 				    SCSI_TCP_TM_RESP_NO_TASK);
2423a6d42e7dSPeter Dunlap 			}
2424a6d42e7dSPeter Dunlap 		} else {
2425a6d42e7dSPeter Dunlap 
2426a6d42e7dSPeter Dunlap 			/*
2427a6d42e7dSPeter Dunlap 			 * Tell STMF to abort the task.  This will do no harm
2428a6d42e7dSPeter Dunlap 			 * if the task is already complete.
2429a6d42e7dSPeter Dunlap 			 */
2430a6d42e7dSPeter Dunlap 			stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
2431a6d42e7dSPeter Dunlap 			    STMF_ABORTED, NULL);
2432a6d42e7dSPeter Dunlap 
2433a6d42e7dSPeter Dunlap 			/*
2434a6d42e7dSPeter Dunlap 			 * Make sure the task hasn't already completed
2435a6d42e7dSPeter Dunlap 			 */
2436a6d42e7dSPeter Dunlap 			mutex_enter(&itask->it_idm_task->idt_mutex);
2437a6d42e7dSPeter Dunlap 			if ((itask->it_idm_task->idt_state == TASK_COMPLETE) ||
2438a6d42e7dSPeter Dunlap 			    (itask->it_idm_task->idt_state == TASK_IDLE)) {
2439a6d42e7dSPeter Dunlap 				/*
2440a6d42e7dSPeter Dunlap 				 * Task is complete, return "Task Does Not
2441a6d42e7dSPeter Dunlap 				 * Exist"
2442a6d42e7dSPeter Dunlap 				 */
2443a6d42e7dSPeter Dunlap 				mutex_exit(&itask->it_idm_task->idt_mutex);
2444a6d42e7dSPeter Dunlap 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2445a6d42e7dSPeter Dunlap 				    SCSI_TCP_TM_RESP_NO_TASK);
2446a6d42e7dSPeter Dunlap 			} else {
2447a6d42e7dSPeter Dunlap 				/*
2448a6d42e7dSPeter Dunlap 				 * STMF is now aborting the task, return
2449a6d42e7dSPeter Dunlap 				 * "Function Complete"
2450a6d42e7dSPeter Dunlap 				 */
2451a6d42e7dSPeter Dunlap 				mutex_exit(&itask->it_idm_task->idt_mutex);
2452a6d42e7dSPeter Dunlap 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2453a6d42e7dSPeter Dunlap 				    SCSI_TCP_TM_RESP_COMPLETE);
2454a6d42e7dSPeter Dunlap 			}
2455a6d42e7dSPeter Dunlap 			idm_task_rele(itask->it_idm_task);
2456a6d42e7dSPeter Dunlap 		}
2457a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2458a6d42e7dSPeter Dunlap 		return;
2459a6d42e7dSPeter Dunlap 
2460a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_ABORT_TASK_SET:
2461a6d42e7dSPeter Dunlap 		tm_func = TM_ABORT_TASK_SET;
2462a6d42e7dSPeter Dunlap 		break;
2463a6d42e7dSPeter Dunlap 
2464a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_CLEAR_ACA:
2465a6d42e7dSPeter Dunlap 		tm_func = TM_CLEAR_ACA;
2466a6d42e7dSPeter Dunlap 		break;
2467a6d42e7dSPeter Dunlap 
2468a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2469a6d42e7dSPeter Dunlap 		tm_func = TM_CLEAR_TASK_SET;
2470a6d42e7dSPeter Dunlap 		break;
2471a6d42e7dSPeter Dunlap 
2472a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2473a6d42e7dSPeter Dunlap 		tm_func = TM_LUN_RESET;
2474a6d42e7dSPeter Dunlap 		break;
2475a6d42e7dSPeter Dunlap 
2476a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2477a6d42e7dSPeter Dunlap 		tm_func = TM_TARGET_WARM_RESET;
2478a6d42e7dSPeter Dunlap 		break;
2479a6d42e7dSPeter Dunlap 
2480a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2481a6d42e7dSPeter Dunlap 		tm_func = TM_TARGET_COLD_RESET;
2482a6d42e7dSPeter Dunlap 		break;
2483a6d42e7dSPeter Dunlap 
2484a6d42e7dSPeter Dunlap 	case ISCSI_TM_FUNC_TASK_REASSIGN:
2485a6d42e7dSPeter Dunlap 		/*
2486a6d42e7dSPeter Dunlap 		 * We do not currently support allegiance reassignment.  When
2487a6d42e7dSPeter Dunlap 		 * we start supporting ERL1+, we will need to.
2488a6d42e7dSPeter Dunlap 		 */
2489a6d42e7dSPeter Dunlap 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2490a6d42e7dSPeter Dunlap 		    SCSI_TCP_TM_RESP_NO_ALLG_REASSN);
2491a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2492a6d42e7dSPeter Dunlap 		return;
2493a6d42e7dSPeter Dunlap 
2494a6d42e7dSPeter Dunlap 	default:
2495a6d42e7dSPeter Dunlap 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2496a6d42e7dSPeter Dunlap 		    SCSI_TCP_TM_RESP_REJECTED);
2497a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2498a6d42e7dSPeter Dunlap 		return;
2499a6d42e7dSPeter Dunlap 	}
2500a6d42e7dSPeter Dunlap 
2501a6d42e7dSPeter Dunlap 	tm_itask = iscsit_tm_task_alloc(ict);
2502a6d42e7dSPeter Dunlap 	if (tm_itask == NULL) {
2503a6d42e7dSPeter Dunlap 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2504a6d42e7dSPeter Dunlap 		    SCSI_TCP_TM_RESP_REJECTED);
2505a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2506a6d42e7dSPeter Dunlap 		return;
2507a6d42e7dSPeter Dunlap 	}
2508a6d42e7dSPeter Dunlap 
2509a6d42e7dSPeter Dunlap 
2510a6d42e7dSPeter Dunlap 	task = stmf_task_alloc(ict->ict_sess->ist_lport,
2511a6d42e7dSPeter Dunlap 	    ict->ict_sess->ist_stmf_sess, iscsi_tm->lun,
2512a6d42e7dSPeter Dunlap 	    0, STMF_TASK_EXT_NONE);
2513a6d42e7dSPeter Dunlap 	if (task == NULL) {
2514a6d42e7dSPeter Dunlap 		/*
2515a6d42e7dSPeter Dunlap 		 * If this happens, either the LU is in reset, couldn't
2516a6d42e7dSPeter Dunlap 		 * get memory, or some other condition in which we simply
2517a6d42e7dSPeter Dunlap 		 * can't complete this request.  It would be nice to return
2518a6d42e7dSPeter Dunlap 		 * an error code like "busy" but the closest we have is
2519a6d42e7dSPeter Dunlap 		 * "rejected".
2520a6d42e7dSPeter Dunlap 		 */
2521a6d42e7dSPeter Dunlap 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2522a6d42e7dSPeter Dunlap 		    SCSI_TCP_TM_RESP_REJECTED);
2523a6d42e7dSPeter Dunlap 		iscsit_tm_task_free(tm_itask);
2524a6d42e7dSPeter Dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2525a6d42e7dSPeter Dunlap 		return;
2526a6d42e7dSPeter Dunlap 	}
2527a6d42e7dSPeter Dunlap 
2528a6d42e7dSPeter Dunlap 	tm_itask->it_tm_pdu = tm_resp_pdu;
2529a6d42e7dSPeter Dunlap 	tm_itask->it_stmf_task = task;
2530a6d42e7dSPeter Dunlap 	task->task_port_private = tm_itask;
2531a6d42e7dSPeter Dunlap 	task->task_mgmt_function = tm_func;
2532a6d42e7dSPeter Dunlap 	task->task_additional_flags = TASK_AF_NO_EXPECTED_XFER_LENGTH;
2533a6d42e7dSPeter Dunlap 	task->task_priority = 0;
2534a6d42e7dSPeter Dunlap 	task->task_max_nbufs = STMF_BUFS_MAX;
2535a6d42e7dSPeter Dunlap 	task->task_cmd_seq_no = iscsi_tm->itt;
2536a6d42e7dSPeter Dunlap 	task->task_expected_xfer_length = 0;
2537a6d42e7dSPeter Dunlap 
2538a6d42e7dSPeter Dunlap 	stmf_post_task(task, NULL);
2539a6d42e7dSPeter Dunlap 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2540a6d42e7dSPeter Dunlap }
2541a6d42e7dSPeter Dunlap 
2542a6d42e7dSPeter Dunlap static void
2543a6d42e7dSPeter Dunlap iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2544a6d42e7dSPeter Dunlap {
2545a6d42e7dSPeter Dunlap 	iscsi_nop_out_hdr_t *out = (iscsi_nop_out_hdr_t *)rx_pdu->isp_hdr;
2546a6d42e7dSPeter Dunlap 	iscsi_nop_in_hdr_t *in;
2547a6d42e7dSPeter Dunlap 	int resp_datalen;
2548a6d42e7dSPeter Dunlap 	idm_pdu_t *resp;
2549a6d42e7dSPeter Dunlap 
2550a6d42e7dSPeter Dunlap 	/* Ignore the response from initiator */
255172cf3143Speter dunlap 	if ((out->itt == ISCSI_RSVD_TASK_TAG) ||
255272cf3143Speter dunlap 	    (out->ttt != ISCSI_RSVD_TASK_TAG)) {
255372cf3143Speter dunlap 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2554a6d42e7dSPeter Dunlap 		return;
255572cf3143Speter dunlap 	}
2556a6d42e7dSPeter Dunlap 
2557a6d42e7dSPeter Dunlap 	/* Allocate a PDU to respond */
2558a6d42e7dSPeter Dunlap 	resp_datalen = ntoh24(out->dlength);
2559a6d42e7dSPeter Dunlap 	resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
2560a6d42e7dSPeter Dunlap 	idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
2561a6d42e7dSPeter Dunlap 	if (resp_datalen > 0) {
2562a6d42e7dSPeter Dunlap 		bcopy(rx_pdu->isp_data, resp->isp_data, resp_datalen);
2563a6d42e7dSPeter Dunlap 	}
2564a6d42e7dSPeter Dunlap 
256560220f10SPriya Krishnan 	/*
256660220f10SPriya Krishnan 	 * When sending a NOP-In as a response to a NOP-Out from the initiator,
256760220f10SPriya Krishnan 	 * the target must respond with the same initiator task tag that was
256860220f10SPriya Krishnan 	 * provided in the NOP-Out request, the target transfer tag must be
256960220f10SPriya Krishnan 	 * ISCSI_RSVD_TASK_TAG (0xffffffff) and StatSN will contain the next
257060220f10SPriya Krishnan 	 * status sequence number. The StatSN for the connection is advanced
257160220f10SPriya Krishnan 	 * after this PDU is sent.
257260220f10SPriya Krishnan 	 */
2573a6d42e7dSPeter Dunlap 	in = (iscsi_nop_in_hdr_t *)resp->isp_hdr;
2574a6d42e7dSPeter Dunlap 	bzero(in, sizeof (*in));
2575a6d42e7dSPeter Dunlap 	in->opcode = ISCSI_OP_NOOP_IN;
2576a6d42e7dSPeter Dunlap 	in->flags = ISCSI_FLAG_FINAL;
2577a6d42e7dSPeter Dunlap 	bcopy(out->lun, in->lun, 8);
2578a6d42e7dSPeter Dunlap 	in->itt		= out->itt;
2579a6d42e7dSPeter Dunlap 	in->ttt		= ISCSI_RSVD_TASK_TAG;
2580a6d42e7dSPeter Dunlap 	hton24(in->dlength, resp_datalen);
258160220f10SPriya Krishnan 	resp->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2582a6d42e7dSPeter Dunlap 	/* Any other field in resp to be set? */
2583a6d42e7dSPeter Dunlap 	iscsit_pdu_tx(resp);
2584a6d42e7dSPeter Dunlap 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2585a6d42e7dSPeter Dunlap }
2586a6d42e7dSPeter Dunlap 
2587a6d42e7dSPeter Dunlap static void
2588a6d42e7dSPeter Dunlap iscsit_pdu_op_login_cmd(iscsit_conn_t	*ict, idm_pdu_t *rx_pdu)
2589a6d42e7dSPeter Dunlap {
2590a6d42e7dSPeter Dunlap 
2591a6d42e7dSPeter Dunlap 	/*
2592a6d42e7dSPeter Dunlap 	 * Submit PDU to login state machine.  State machine will free the
2593a6d42e7dSPeter Dunlap 	 * PDU.
2594a6d42e7dSPeter Dunlap 	 */
2595a6d42e7dSPeter Dunlap 	iscsit_login_sm_event(ict, ILE_LOGIN_RCV, rx_pdu);
2596a6d42e7dSPeter Dunlap }
2597a6d42e7dSPeter Dunlap 
2598a6d42e7dSPeter Dunlap void
2599a6d42e7dSPeter Dunlap iscsit_pdu_op_logout_cmd(iscsit_conn_t	*ict, idm_pdu_t *rx_pdu)
2600a6d42e7dSPeter Dunlap {
2601*1f99504aSToomas Soome 	iscsi_logout_hdr_t	*logout_req =
2602a6d42e7dSPeter Dunlap 	    (iscsi_logout_hdr_t *)rx_pdu->isp_hdr;
2603a6d42e7dSPeter Dunlap 	iscsi_logout_rsp_hdr_t	*logout_rsp;
2604a6d42e7dSPeter Dunlap 	idm_pdu_t *resp;
2605a6d42e7dSPeter Dunlap 
2606a6d42e7dSPeter Dunlap 	/* Allocate a PDU to respond */
2607a6d42e7dSPeter Dunlap 	resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2608a6d42e7dSPeter Dunlap 	idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
260960220f10SPriya Krishnan 	/*
261060220f10SPriya Krishnan 	 * The StatSN is to be sent to the initiator,
261160220f10SPriya Krishnan 	 * it is not required to increment the number
261260220f10SPriya Krishnan 	 * as the connection is terminating.
261360220f10SPriya Krishnan 	 */
261460220f10SPriya Krishnan 	resp->isp_flags |= IDM_PDU_SET_STATSN;
2615a6d42e7dSPeter Dunlap 	/*
2616a6d42e7dSPeter Dunlap 	 * Logout results in the immediate termination of all tasks except
2617a6d42e7dSPeter Dunlap 	 * if the logout reason is ISCSI_LOGOUT_REASON_RECOVERY.  The
2618a6d42e7dSPeter Dunlap 	 * connection state machine will drive this task cleanup automatically
2619a6d42e7dSPeter Dunlap 	 * so we don't need to handle that here.
2620a6d42e7dSPeter Dunlap 	 */
2621a6d42e7dSPeter Dunlap 	logout_rsp = (iscsi_logout_rsp_hdr_t *)resp->isp_hdr;
2622a6d42e7dSPeter Dunlap 	bzero(logout_rsp, sizeof (*logout_rsp));
2623a6d42e7dSPeter Dunlap 	logout_rsp->opcode = ISCSI_OP_LOGOUT_RSP;
2624a6d42e7dSPeter Dunlap 	logout_rsp->flags = ISCSI_FLAG_FINAL;
2625a6d42e7dSPeter Dunlap 	logout_rsp->itt = logout_req->itt;
2626a6d42e7dSPeter Dunlap 	if ((logout_req->flags & ISCSI_FLAG_LOGOUT_REASON_MASK) >
2627a6d42e7dSPeter Dunlap 	    ISCSI_LOGOUT_REASON_RECOVERY) {
2628a6d42e7dSPeter Dunlap 		logout_rsp->response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2629a6d42e7dSPeter Dunlap 	} else {
2630a6d42e7dSPeter Dunlap 		logout_rsp->response = ISCSI_LOGOUT_SUCCESS;
2631a6d42e7dSPeter Dunlap 	}
2632a6d42e7dSPeter Dunlap 
2633a6d42e7dSPeter Dunlap 	iscsit_pdu_tx(resp);
2634a6d42e7dSPeter Dunlap 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2635a6d42e7dSPeter Dunlap }
2636a6d42e7dSPeter Dunlap 
2637a6d42e7dSPeter Dunlap /*
2638a6d42e7dSPeter Dunlap  * Calculate the number of outstanding commands we can process
2639a6d42e7dSPeter Dunlap  */
2640a6d42e7dSPeter Dunlap int
2641a6d42e7dSPeter Dunlap iscsit_cmd_window()
2642a6d42e7dSPeter Dunlap {
2643d618d68dSPriya Krishnan 	/*
2644d618d68dSPriya Krishnan 	 * Instead of using a pre-defined constant for the command window,
2645d618d68dSPriya Krishnan 	 * it should be made confiurable and dynamic. With MC/S, sequence
2646d618d68dSPriya Krishnan 	 * numbers will be used up at a much faster rate than with SC/S.
2647d618d68dSPriya Krishnan 	 */
2648d618d68dSPriya Krishnan 	return	(ISCSIT_MAX_WINDOW);
2649a6d42e7dSPeter Dunlap }
2650a6d42e7dSPeter Dunlap 
2651a6d42e7dSPeter Dunlap /*
2652a6d42e7dSPeter Dunlap  * Set local registers based on incoming PDU
2653a6d42e7dSPeter Dunlap  */
2654a6d42e7dSPeter Dunlap void
2655a6d42e7dSPeter Dunlap iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2656a6d42e7dSPeter Dunlap {
2657a6d42e7dSPeter Dunlap 	iscsit_sess_t *ist;
2658a6d42e7dSPeter Dunlap 	iscsi_scsi_cmd_hdr_t *req;
2659a6d42e7dSPeter Dunlap 
2660a6d42e7dSPeter Dunlap 	ist = ict->ict_sess;
2661a6d42e7dSPeter Dunlap 
2662a6d42e7dSPeter Dunlap 	req = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2663d618d68dSPriya Krishnan 	if (req->opcode & ISCSI_OP_IMMEDIATE) {
2664d618d68dSPriya Krishnan 		/* no cmdsn increment for immediate PDUs */
2665d618d68dSPriya Krishnan 		return;
2666d618d68dSPriya Krishnan 	}
2667a6d42e7dSPeter Dunlap 
2668d618d68dSPriya Krishnan 	/* Ensure that the ExpCmdSN advances in an orderly manner */
2669d618d68dSPriya Krishnan 	mutex_enter(&ist->ist_sn_mutex);
2670a6d42e7dSPeter Dunlap 	ist->ist_expcmdsn = ntohl(req->cmdsn) + 1;
2671a6d42e7dSPeter Dunlap 	ist->ist_maxcmdsn = ntohl(req->cmdsn) + iscsit_cmd_window();
2672d618d68dSPriya Krishnan 	mutex_exit(&ist->ist_sn_mutex);
2673a6d42e7dSPeter Dunlap }
2674a6d42e7dSPeter Dunlap 
2675a6d42e7dSPeter Dunlap /*
2676a6d42e7dSPeter Dunlap  * Wrapper funtion, calls iscsi_calc_rspsn and idm_pdu_tx
2677a6d42e7dSPeter Dunlap  */
2678a6d42e7dSPeter Dunlap void
2679a6d42e7dSPeter Dunlap iscsit_pdu_tx(idm_pdu_t *pdu)
2680a6d42e7dSPeter Dunlap {
2681a6d42e7dSPeter Dunlap 	iscsit_conn_t *ict = pdu->isp_ic->ic_handle;
268260220f10SPriya Krishnan 	iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
268360220f10SPriya Krishnan 	iscsit_sess_t *ist = ict->ict_sess;
2684a6d42e7dSPeter Dunlap 
2685a6d42e7dSPeter Dunlap 	/*
268660220f10SPriya Krishnan 	 * The command sequence numbers are session-wide and must stay
268760220f10SPriya Krishnan 	 * consistent across the transfer, so protect the cmdsn with a
2688d618d68dSPriya Krishnan 	 * mutex lock on the session. The status sequence number will
268960220f10SPriya Krishnan 	 * be updated just before the transport layer transmits the PDU.
2690a6d42e7dSPeter Dunlap 	 */
269160220f10SPriya Krishnan 
2692d618d68dSPriya Krishnan 	mutex_enter(&ict->ict_sess->ist_sn_mutex);
269360220f10SPriya Krishnan 	/* Set ExpCmdSN and MaxCmdSN */
269460220f10SPriya Krishnan 	rsp->maxcmdsn = htonl(ist->ist_maxcmdsn);
269560220f10SPriya Krishnan 	rsp->expcmdsn = htonl(ist->ist_expcmdsn);
2696a6d42e7dSPeter Dunlap 	idm_pdu_tx(pdu);
2697d618d68dSPriya Krishnan 	mutex_exit(&ict->ict_sess->ist_sn_mutex);
2698a6d42e7dSPeter Dunlap }
2699a6d42e7dSPeter Dunlap 
2700a6d42e7dSPeter Dunlap /*
2701a6d42e7dSPeter Dunlap  * Internal functions
2702a6d42e7dSPeter Dunlap  */
2703a6d42e7dSPeter Dunlap 
2704a6d42e7dSPeter Dunlap void
2705a6d42e7dSPeter Dunlap iscsit_send_async_event(iscsit_conn_t *ict, uint8_t event)
2706a6d42e7dSPeter Dunlap {
2707a6d42e7dSPeter Dunlap 	idm_pdu_t		*abt;
2708a6d42e7dSPeter Dunlap 	iscsi_async_evt_hdr_t	*async_abt;
2709a6d42e7dSPeter Dunlap 
2710a6d42e7dSPeter Dunlap 	/*
2711a6d42e7dSPeter Dunlap 	 * Get a PDU to build the abort request.
2712a6d42e7dSPeter Dunlap 	 */
2713a6d42e7dSPeter Dunlap 	abt = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2714a6d42e7dSPeter Dunlap 	if (abt == NULL) {
2715*1f99504aSToomas Soome 		idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, 0);
2716a6d42e7dSPeter Dunlap 		return;
2717a6d42e7dSPeter Dunlap 	}
2718a6d42e7dSPeter Dunlap 
271960220f10SPriya Krishnan 	/*
272060220f10SPriya Krishnan 	 * A asynchronous message is sent by the target to request a logout.
272160220f10SPriya Krishnan 	 * The StatSN for the connection is advanced after the PDU is sent
272260220f10SPriya Krishnan 	 * to allow for initiator and target state synchronization.
272360220f10SPriya Krishnan 	 */
272472cf3143Speter dunlap 	idm_pdu_init(abt, ict->ict_ic, NULL, NULL);
2725a6d42e7dSPeter Dunlap 	abt->isp_datalen = 0;
272660220f10SPriya Krishnan 	abt->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2727a6d42e7dSPeter Dunlap 
2728a6d42e7dSPeter Dunlap 	async_abt = (iscsi_async_evt_hdr_t *)abt->isp_hdr;
2729a6d42e7dSPeter Dunlap 	bzero(async_abt, sizeof (*async_abt));
2730a6d42e7dSPeter Dunlap 	async_abt->opcode = ISCSI_OP_ASYNC_EVENT;
2731a6d42e7dSPeter Dunlap 	async_abt->async_event = event;
2732a6d42e7dSPeter Dunlap 	async_abt->flags = ISCSI_FLAG_FINAL;
2733a6d42e7dSPeter Dunlap 	async_abt->rsvd4[0] = 0xff;
2734a6d42e7dSPeter Dunlap 	async_abt->rsvd4[1] = 0xff;
2735a6d42e7dSPeter Dunlap 	async_abt->rsvd4[2] = 0xff;
2736a6d42e7dSPeter Dunlap 	async_abt->rsvd4[3] = 0xff;
2737a6d42e7dSPeter Dunlap 
2738a6d42e7dSPeter Dunlap 	switch (event) {
2739a6d42e7dSPeter Dunlap 	case ISCSI_ASYNC_EVENT_REQUEST_LOGOUT:
2740a6d42e7dSPeter Dunlap 		async_abt->param3 = htons(IDM_LOGOUT_SECONDS);
2741a6d42e7dSPeter Dunlap 		break;
2742a6d42e7dSPeter Dunlap 	case ISCSI_ASYNC_EVENT_SCSI_EVENT:
2743a6d42e7dSPeter Dunlap 	case ISCSI_ASYNC_EVENT_DROPPING_CONNECTION:
2744a6d42e7dSPeter Dunlap 	case ISCSI_ASYNC_EVENT_DROPPING_ALL_CONNECTIONS:
2745a6d42e7dSPeter Dunlap 	case ISCSI_ASYNC_EVENT_PARAM_NEGOTIATION:
2746a6d42e7dSPeter Dunlap 	default:
2747a6d42e7dSPeter Dunlap 		ASSERT(0);
2748a6d42e7dSPeter Dunlap 	}
2749a6d42e7dSPeter Dunlap 
2750a6d42e7dSPeter Dunlap 	iscsit_pdu_tx(abt);
2751a6d42e7dSPeter Dunlap }
2752a6d42e7dSPeter Dunlap 
275372cf3143Speter dunlap void
275472cf3143Speter dunlap iscsit_send_reject(iscsit_conn_t *ict, idm_pdu_t *rejected_pdu, uint8_t reason)
275572cf3143Speter dunlap {
275672cf3143Speter dunlap 	idm_pdu_t		*reject_pdu;
275772cf3143Speter dunlap 	iscsi_reject_rsp_hdr_t	*reject;
275872cf3143Speter dunlap 
275972cf3143Speter dunlap 	/*
276072cf3143Speter dunlap 	 * Get a PDU to build the abort request.
276172cf3143Speter dunlap 	 */
276272cf3143Speter dunlap 	reject_pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t),
276372cf3143Speter dunlap 	    rejected_pdu->isp_hdrlen);
276472cf3143Speter dunlap 	if (reject_pdu == NULL) {
2765*1f99504aSToomas Soome 		idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, 0);
276672cf3143Speter dunlap 		return;
276772cf3143Speter dunlap 	}
276872cf3143Speter dunlap 	idm_pdu_init(reject_pdu, ict->ict_ic, NULL, NULL);
276960220f10SPriya Krishnan 	/* StatSN is advanced after a Reject PDU */
277060220f10SPriya Krishnan 	reject_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
277172cf3143Speter dunlap 	reject_pdu->isp_datalen = rejected_pdu->isp_hdrlen;
277272cf3143Speter dunlap 	bcopy(rejected_pdu->isp_hdr, reject_pdu->isp_data,
277372cf3143Speter dunlap 	    rejected_pdu->isp_hdrlen);
277472cf3143Speter dunlap 
277572cf3143Speter dunlap 	reject = (iscsi_reject_rsp_hdr_t *)reject_pdu->isp_hdr;
277672cf3143Speter dunlap 	bzero(reject, sizeof (*reject));
277772cf3143Speter dunlap 	reject->opcode = ISCSI_OP_REJECT_MSG;
277872cf3143Speter dunlap 	reject->reason = reason;
277972cf3143Speter dunlap 	reject->flags = ISCSI_FLAG_FINAL;
278072cf3143Speter dunlap 	hton24(reject->dlength, rejected_pdu->isp_hdrlen);
278172cf3143Speter dunlap 	reject->must_be_ff[0] = 0xff;
278272cf3143Speter dunlap 	reject->must_be_ff[1] = 0xff;
278372cf3143Speter dunlap 	reject->must_be_ff[2] = 0xff;
278472cf3143Speter dunlap 	reject->must_be_ff[3] = 0xff;
278572cf3143Speter dunlap 
278672cf3143Speter dunlap 	iscsit_pdu_tx(reject_pdu);
278772cf3143Speter dunlap }
278872cf3143Speter dunlap 
2789a6d42e7dSPeter Dunlap 
2790a6d42e7dSPeter Dunlap static iscsit_task_t *
2791a6d42e7dSPeter Dunlap iscsit_task_alloc(iscsit_conn_t *ict)
2792a6d42e7dSPeter Dunlap {
2793a6d42e7dSPeter Dunlap 	iscsit_task_t *itask;
2794a6d42e7dSPeter Dunlap 	iscsit_buf_t *immed_ibuf;
2795a6d42e7dSPeter Dunlap 
2796a6d42e7dSPeter Dunlap 	/*
2797a6d42e7dSPeter Dunlap 	 * Possible items to pre-alloc if we cache iscsit_task_t's:
2798a6d42e7dSPeter Dunlap 	 *
2799a6d42e7dSPeter Dunlap 	 * Status PDU w/ sense buffer
2800a6d42e7dSPeter Dunlap 	 * stmf_data_buf_t for immediate data
2801a6d42e7dSPeter Dunlap 	 */
2802a6d42e7dSPeter Dunlap 	itask = kmem_alloc(sizeof (iscsit_task_t) + sizeof (iscsit_buf_t) +
2803a6d42e7dSPeter Dunlap 	    sizeof (stmf_data_buf_t), KM_NOSLEEP);
2804a6d42e7dSPeter Dunlap 	if (itask != NULL) {
2805a6d42e7dSPeter Dunlap 		mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2806a6d42e7dSPeter Dunlap 		itask->it_aborted = itask->it_stmf_abort =
2807a6d42e7dSPeter Dunlap 		    itask->it_tm_task = 0;
2808a6d42e7dSPeter Dunlap 
2809a6d42e7dSPeter Dunlap 		immed_ibuf = (iscsit_buf_t *)(itask + 1);
2810a6d42e7dSPeter Dunlap 		bzero(immed_ibuf, sizeof (*immed_ibuf));
2811a6d42e7dSPeter Dunlap 		immed_ibuf->ibuf_is_immed = B_TRUE;
2812a6d42e7dSPeter Dunlap 		immed_ibuf->ibuf_stmf_buf = (stmf_data_buf_t *)(immed_ibuf + 1);
2813a6d42e7dSPeter Dunlap 
2814a6d42e7dSPeter Dunlap 		bzero(immed_ibuf->ibuf_stmf_buf, sizeof (stmf_data_buf_t));
2815a6d42e7dSPeter Dunlap 		immed_ibuf->ibuf_stmf_buf->db_port_private = immed_ibuf;
2816a6d42e7dSPeter Dunlap 		immed_ibuf->ibuf_stmf_buf->db_sglist_length = 1;
2817a6d42e7dSPeter Dunlap 		immed_ibuf->ibuf_stmf_buf->db_flags = DB_DIRECTION_FROM_RPORT |
2818a6d42e7dSPeter Dunlap 		    DB_DONT_CACHE;
2819a6d42e7dSPeter Dunlap 		itask->it_immed_data = immed_ibuf;
2820a6d42e7dSPeter Dunlap 		itask->it_idm_task = idm_task_alloc(ict->ict_ic);
2821a6d42e7dSPeter Dunlap 		if (itask->it_idm_task != NULL) {
2822a6d42e7dSPeter Dunlap 			itask->it_idm_task->idt_private = itask;
2823a6d42e7dSPeter Dunlap 			itask->it_ict = ict;
2824a6d42e7dSPeter Dunlap 			itask->it_ttt = itask->it_idm_task->idt_tt;
2825a6d42e7dSPeter Dunlap 			return (itask);
2826a6d42e7dSPeter Dunlap 		} else {
2827a6d42e7dSPeter Dunlap 			kmem_free(itask, sizeof (iscsit_task_t) +
2828a6d42e7dSPeter Dunlap 			    sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2829a6d42e7dSPeter Dunlap 		}
2830a6d42e7dSPeter Dunlap 	}
2831a6d42e7dSPeter Dunlap 
2832a6d42e7dSPeter Dunlap 	return (NULL);
2833a6d42e7dSPeter Dunlap }
2834a6d42e7dSPeter Dunlap 
2835a6d42e7dSPeter Dunlap static void
2836a6d42e7dSPeter Dunlap iscsit_task_free(iscsit_task_t *itask)
2837a6d42e7dSPeter Dunlap {
2838a6d42e7dSPeter Dunlap 	idm_task_free(itask->it_idm_task);
2839a6d42e7dSPeter Dunlap 	mutex_destroy(&itask->it_mutex);
2840a6d42e7dSPeter Dunlap 	kmem_free(itask, sizeof (iscsit_task_t) +
2841a6d42e7dSPeter Dunlap 	    sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2842a6d42e7dSPeter Dunlap }
2843a6d42e7dSPeter Dunlap 
2844a6d42e7dSPeter Dunlap static iscsit_task_t *
2845a6d42e7dSPeter Dunlap iscsit_tm_task_alloc(iscsit_conn_t *ict)
2846a6d42e7dSPeter Dunlap {
2847a6d42e7dSPeter Dunlap 	iscsit_task_t *itask;
2848a6d42e7dSPeter Dunlap 
2849a6d42e7dSPeter Dunlap 	itask = kmem_zalloc(sizeof (iscsit_task_t), KM_NOSLEEP);
2850a6d42e7dSPeter Dunlap 	if (itask != NULL) {
2851a6d42e7dSPeter Dunlap 		idm_conn_hold(ict->ict_ic);
2852a6d42e7dSPeter Dunlap 		mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2853a6d42e7dSPeter Dunlap 		itask->it_aborted = itask->it_stmf_abort =
2854a6d42e7dSPeter Dunlap 		    itask->it_tm_responded = 0;
2855a6d42e7dSPeter Dunlap 		itask->it_tm_pdu = NULL;
2856a6d42e7dSPeter Dunlap 		itask->it_tm_task = 1;
2857a6d42e7dSPeter Dunlap 		itask->it_ict = ict;
2858a6d42e7dSPeter Dunlap 	}
2859a6d42e7dSPeter Dunlap 
2860a6d42e7dSPeter Dunlap 	return (itask);
2861a6d42e7dSPeter Dunlap }
2862a6d42e7dSPeter Dunlap 
2863a6d42e7dSPeter Dunlap static void
2864a6d42e7dSPeter Dunlap iscsit_tm_task_free(iscsit_task_t *itask)
2865a6d42e7dSPeter Dunlap {
2866a6d42e7dSPeter Dunlap 	/*
2867a6d42e7dSPeter Dunlap 	 * If we responded then the call to idm_pdu_complete will free the
2868a6d42e7dSPeter Dunlap 	 * PDU.  Otherwise we got aborted before the TM function could
2869a6d42e7dSPeter Dunlap 	 * complete and we need to free the PDU explicitly.
2870a6d42e7dSPeter Dunlap 	 */
2871a6d42e7dSPeter Dunlap 	if (itask->it_tm_pdu != NULL && !itask->it_tm_responded)
2872a6d42e7dSPeter Dunlap 		idm_pdu_free(itask->it_tm_pdu);
2873a6d42e7dSPeter Dunlap 	idm_conn_rele(itask->it_ict->ict_ic);
2874a6d42e7dSPeter Dunlap 	mutex_destroy(&itask->it_mutex);
2875a6d42e7dSPeter Dunlap 	kmem_free(itask, sizeof (iscsit_task_t));
2876a6d42e7dSPeter Dunlap }
2877a6d42e7dSPeter Dunlap 
287872cf3143Speter dunlap static idm_status_t
287972cf3143Speter dunlap iscsit_task_start(iscsit_task_t *itask)
288072cf3143Speter dunlap {
288172cf3143Speter dunlap 	iscsit_sess_t *ist = itask->it_ict->ict_sess;
288272cf3143Speter dunlap 	avl_index_t		where;
288372cf3143Speter dunlap 
288472cf3143Speter dunlap 	/*
288572cf3143Speter dunlap 	 * Sanity check the ITT and ensure that this task does not already
288672cf3143Speter dunlap 	 * exist.  If not then add the task to the session task list.
288772cf3143Speter dunlap 	 */
288872cf3143Speter dunlap 	mutex_enter(&ist->ist_mutex);
288972cf3143Speter dunlap 	mutex_enter(&itask->it_mutex);
289072cf3143Speter dunlap 	itask->it_active = 1;
289172cf3143Speter dunlap 	if (avl_find(&ist->ist_task_list, itask, &where) == NULL) {
289272cf3143Speter dunlap 		/* New task, add to AVL */
289372cf3143Speter dunlap 		avl_insert(&ist->ist_task_list, itask, where);
289472cf3143Speter dunlap 		mutex_exit(&itask->it_mutex);
289572cf3143Speter dunlap 		mutex_exit(&ist->ist_mutex);
289672cf3143Speter dunlap 		return (IDM_STATUS_SUCCESS);
289772cf3143Speter dunlap 	}
289872cf3143Speter dunlap 	mutex_exit(&itask->it_mutex);
289972cf3143Speter dunlap 	mutex_exit(&ist->ist_mutex);
290072cf3143Speter dunlap 
290172cf3143Speter dunlap 	return (IDM_STATUS_REJECT);
290272cf3143Speter dunlap }
290372cf3143Speter dunlap 
290472cf3143Speter dunlap static void
290572cf3143Speter dunlap iscsit_task_done(iscsit_task_t *itask)
290672cf3143Speter dunlap {
290772cf3143Speter dunlap 	iscsit_sess_t *ist = itask->it_ict->ict_sess;
290872cf3143Speter dunlap 
290972cf3143Speter dunlap 	mutex_enter(&ist->ist_mutex);
291072cf3143Speter dunlap 	mutex_enter(&itask->it_mutex);
291172cf3143Speter dunlap 	if (itask->it_active) {
291272cf3143Speter dunlap 		avl_remove(&ist->ist_task_list, itask);
291372cf3143Speter dunlap 		itask->it_active = 0;
291472cf3143Speter dunlap 	}
291572cf3143Speter dunlap 	mutex_exit(&itask->it_mutex);
291672cf3143Speter dunlap 	mutex_exit(&ist->ist_mutex);
291772cf3143Speter dunlap }
291872cf3143Speter dunlap 
2919a6d42e7dSPeter Dunlap /*
2920a6d42e7dSPeter Dunlap  * iscsit status PDU cache
2921a6d42e7dSPeter Dunlap  */
2922a6d42e7dSPeter Dunlap 
2923a6d42e7dSPeter Dunlap /*ARGSUSED*/
2924a6d42e7dSPeter Dunlap static int
2925a6d42e7dSPeter Dunlap iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags)
2926a6d42e7dSPeter Dunlap {
2927a6d42e7dSPeter Dunlap 	idm_pdu_t *pdu = pdu_void;
2928a6d42e7dSPeter Dunlap 	iscsi_scsi_rsp_hdr_t *rsp;
2929a6d42e7dSPeter Dunlap 
2930a6d42e7dSPeter Dunlap 	bzero(pdu, sizeof (idm_pdu_t));
2931a6d42e7dSPeter Dunlap 	pdu->isp_callback = iscsit_send_good_status_done;
2932a6d42e7dSPeter Dunlap 	pdu->isp_magic = IDM_PDU_MAGIC;
2933a6d42e7dSPeter Dunlap 	pdu->isp_hdr = (iscsi_hdr_t *)(pdu + 1); /* Ptr arithmetic */
2934a6d42e7dSPeter Dunlap 	pdu->isp_hdrlen = sizeof (iscsi_hdr_t);
2935a6d42e7dSPeter Dunlap 
2936a6d42e7dSPeter Dunlap 	/* Setup status response */
2937a6d42e7dSPeter Dunlap 	rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
2938a6d42e7dSPeter Dunlap 	bzero(rsp, sizeof (*rsp));
2939a6d42e7dSPeter Dunlap 	rsp->opcode = ISCSI_OP_SCSI_RSP;
2940a6d42e7dSPeter Dunlap 	rsp->flags = ISCSI_FLAG_FINAL;
2941a6d42e7dSPeter Dunlap 	rsp->response = ISCSI_STATUS_CMD_COMPLETED;
2942a6d42e7dSPeter Dunlap 
2943a6d42e7dSPeter Dunlap 	return (0);
2944a6d42e7dSPeter Dunlap }
2945a6d42e7dSPeter Dunlap 
2946a6d42e7dSPeter Dunlap /*
2947a6d42e7dSPeter Dunlap  * iscsit private data handler
2948a6d42e7dSPeter Dunlap  */
2949a6d42e7dSPeter Dunlap 
2950a6d42e7dSPeter Dunlap /*ARGSUSED*/
2951a6d42e7dSPeter Dunlap static void
2952a6d42e7dSPeter Dunlap iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags)
2953a6d42e7dSPeter Dunlap {
2954a6d42e7dSPeter Dunlap 	it_config_t		*cfg;
2955a6d42e7dSPeter Dunlap 	nvlist_t		*nvl;
29568c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	iscsit_service_enabled_t	old_state;
2957a6d42e7dSPeter Dunlap 
2958a6d42e7dSPeter Dunlap 	if ((cmd != STMF_PROVIDER_DATA_UPDATED) || (arg == NULL)) {
2959a6d42e7dSPeter Dunlap 		return;
2960a6d42e7dSPeter Dunlap 	}
2961a6d42e7dSPeter Dunlap 
2962a6d42e7dSPeter Dunlap 	nvl = (nvlist_t *)arg;
2963a6d42e7dSPeter Dunlap 
2964a6d42e7dSPeter Dunlap 	/* Translate nvlist */
2965a6d42e7dSPeter Dunlap 	if (it_nv_to_config(nvl, &cfg) != 0) {
2966a6d42e7dSPeter Dunlap 		cmn_err(CE_WARN, "Configuration is invalid");
2967a6d42e7dSPeter Dunlap 		return;
2968a6d42e7dSPeter Dunlap 	}
2969a6d42e7dSPeter Dunlap 
29708c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	/* Check that no iSCSI ioctl is currently running */
29718c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_enter(&iscsit_global.global_state_mutex);
29728c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	old_state = iscsit_global.global_svc_state;
29738c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	switch (iscsit_global.global_svc_state) {
29748c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	case ISE_ENABLED:
29758c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	case ISE_DISABLED:
29768c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		iscsit_global.global_svc_state = ISE_BUSY;
29778c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		break;
29788c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	case ISE_ENABLING:
29798c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		/*
29808c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		 * It is OK for the iscsit_pp_cb to be called from inside of
29818c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		 * an iSCSI ioctl only if we are currently executing inside
29828c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		 * of stmf_register_port_provider.
29838c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		 */
29848c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		ASSERT((flags & STMF_PCB_PREG_COMPLETE) != 0);
29858c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		break;
29868c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	default:
29878c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		cmn_err(CE_WARN, "iscsit_pp_cb called when global_svc_state"
29888c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		    " is not ENABLED(0x%x) -- ignoring",
29898c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		    iscsit_global.global_svc_state);
29908c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		mutex_exit(&iscsit_global.global_state_mutex);
29918c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		it_config_free_cmn(cfg);
29928c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 		return;
29938c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	}
29948c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_exit(&iscsit_global.global_state_mutex);
29958c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
2996a6d42e7dSPeter Dunlap 	/* Update config */
2997a6d42e7dSPeter Dunlap 	(void) iscsit_config_merge(cfg);
2998a6d42e7dSPeter Dunlap 
2999a6d42e7dSPeter Dunlap 	it_config_free_cmn(cfg);
30008c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 
30018c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	/* Restore old iSCSI driver global state */
30028c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_enter(&iscsit_global.global_state_mutex);
30038c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	ASSERT(iscsit_global.global_svc_state == ISE_BUSY ||
30048c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	    iscsit_global.global_svc_state == ISE_ENABLING);
30058c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	iscsit_global.global_svc_state = old_state;
30068c629652SPeter Cudhea - Sun Microsystems - Burlington, MA United States 	mutex_exit(&iscsit_global.global_state_mutex);
3007a6d42e7dSPeter Dunlap }
3008a6d42e7dSPeter Dunlap 
3009a6d42e7dSPeter Dunlap 
3010a6d42e7dSPeter Dunlap static it_cfg_status_t
3011a6d42e7dSPeter Dunlap iscsit_config_merge(it_config_t *in_cfg)
3012a6d42e7dSPeter Dunlap {
3013a6d42e7dSPeter Dunlap 	it_cfg_status_t	status;
3014a6d42e7dSPeter Dunlap 	it_config_t	*cfg;
3015a6d42e7dSPeter Dunlap 	it_config_t	tmp_cfg;
3016a6d42e7dSPeter Dunlap 	list_t		tpg_del_list;
3017a6d42e7dSPeter Dunlap 
3018a6d42e7dSPeter Dunlap 	if (in_cfg) {
3019a6d42e7dSPeter Dunlap 		cfg = in_cfg;
3020a6d42e7dSPeter Dunlap 	} else {
3021a6d42e7dSPeter Dunlap 		/* Make empty config */
3022a6d42e7dSPeter Dunlap 		bzero(&tmp_cfg, sizeof (tmp_cfg));
3023a6d42e7dSPeter Dunlap 		cfg = &tmp_cfg;
3024a6d42e7dSPeter Dunlap 	}
3025a6d42e7dSPeter Dunlap 
3026a6d42e7dSPeter Dunlap 	list_create(&tpg_del_list,  sizeof (iscsit_tpg_t),
3027a6d42e7dSPeter Dunlap 	    offsetof(iscsit_tpg_t, tpg_delete_ln));
3028a6d42e7dSPeter Dunlap 
3029a6d42e7dSPeter Dunlap 	/*
3030a6d42e7dSPeter Dunlap 	 * Update targets, initiator contexts, target portal groups,
3031a6d42e7dSPeter Dunlap 	 * and iSNS client
3032a6d42e7dSPeter Dunlap 	 */
3033a6d42e7dSPeter Dunlap 	ISCSIT_GLOBAL_LOCK(RW_WRITER);
3034a6d42e7dSPeter Dunlap 	if (((status = iscsit_config_merge_tpg(cfg, &tpg_del_list))
3035a6d42e7dSPeter Dunlap 	    != 0) ||
3036a6d42e7dSPeter Dunlap 	    ((status = iscsit_config_merge_tgt(cfg)) != 0) ||
3037a6d42e7dSPeter Dunlap 	    ((status = iscsit_config_merge_ini(cfg)) != 0) ||
3038a6d42e7dSPeter Dunlap 	    ((status = isnst_config_merge(cfg)) != 0)) {
3039a6d42e7dSPeter Dunlap 		ISCSIT_GLOBAL_UNLOCK();
3040a6d42e7dSPeter Dunlap 		return (status);
3041a6d42e7dSPeter Dunlap 	}
3042a6d42e7dSPeter Dunlap 
3043a6d42e7dSPeter Dunlap 	/* Update other global config parameters */
3044a6d42e7dSPeter Dunlap 	if (iscsit_global.global_props) {
3045a6d42e7dSPeter Dunlap 		nvlist_free(iscsit_global.global_props);
3046a6d42e7dSPeter Dunlap 		iscsit_global.global_props = NULL;
3047a6d42e7dSPeter Dunlap 	}
3048a6d42e7dSPeter Dunlap 	if (in_cfg) {
3049a6d42e7dSPeter Dunlap 		(void) nvlist_dup(cfg->config_global_properties,
3050a6d42e7dSPeter Dunlap 		    &iscsit_global.global_props, KM_SLEEP);
3051a6d42e7dSPeter Dunlap 	}
3052a6d42e7dSPeter Dunlap 	ISCSIT_GLOBAL_UNLOCK();
3053a6d42e7dSPeter Dunlap 
3054a6d42e7dSPeter Dunlap 	iscsit_config_destroy_tpgs(&tpg_del_list);
3055a6d42e7dSPeter Dunlap 
3056a6d42e7dSPeter Dunlap 	list_destroy(&tpg_del_list);
3057a6d42e7dSPeter Dunlap 
3058a6d42e7dSPeter Dunlap 	return (ITCFG_SUCCESS);
3059a6d42e7dSPeter Dunlap }
3060a6d42e7dSPeter Dunlap 
3061a6d42e7dSPeter Dunlap /*
3062a6d42e7dSPeter Dunlap  * iscsit_sna_lt[e]
3063a6d42e7dSPeter Dunlap  *
3064a6d42e7dSPeter Dunlap  * Compare serial numbers using serial number arithmetic as defined in
3065a6d42e7dSPeter Dunlap  * RFC 1982.
3066a6d42e7dSPeter Dunlap  *
3067ab003da8SJim Dunham  * NOTE: This code is duplicated in the isns server. It ought to be common.
3068a6d42e7dSPeter Dunlap  */
3069a6d42e7dSPeter Dunlap 
3070a6d42e7dSPeter Dunlap static int
3071a6d42e7dSPeter Dunlap iscsit_sna_lt(uint32_t sn1, uint32_t sn2)
3072a6d42e7dSPeter Dunlap {
3073a6d42e7dSPeter Dunlap 	return ((sn1 != sn2) &&
3074a6d42e7dSPeter Dunlap 	    (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
3075a6d42e7dSPeter Dunlap 	    ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
3076a6d42e7dSPeter Dunlap }
3077a6d42e7dSPeter Dunlap 
3078a6d42e7dSPeter Dunlap static int
3079a6d42e7dSPeter Dunlap iscsit_sna_lte(uint32_t sn1, uint32_t sn2)
3080a6d42e7dSPeter Dunlap {
3081a6d42e7dSPeter Dunlap 	return ((sn1 == sn2) ||
3082a6d42e7dSPeter Dunlap 	    (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
3083a6d42e7dSPeter Dunlap 	    ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
3084a6d42e7dSPeter Dunlap }
3085a6d42e7dSPeter Dunlap 
3086a6d42e7dSPeter Dunlap 
3087a6d42e7dSPeter Dunlap static boolean_t
3088a6d42e7dSPeter Dunlap iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn)
3089a6d42e7dSPeter Dunlap {
3090a6d42e7dSPeter Dunlap 	iscsit_sess_t	*ist = ict->ict_sess;
3091a6d42e7dSPeter Dunlap 	int		rval = B_TRUE;
3092a6d42e7dSPeter Dunlap 
3093a6d42e7dSPeter Dunlap 	ist = ict->ict_sess;
3094a6d42e7dSPeter Dunlap 
3095d618d68dSPriya Krishnan 	mutex_enter(&ist->ist_sn_mutex);
3096a6d42e7dSPeter Dunlap 
3097a6d42e7dSPeter Dunlap 	/*
3098a6d42e7dSPeter Dunlap 	 * If cmdsn is less than ist_expcmdsn - iscsit_cmd_window() or
3099a6d42e7dSPeter Dunlap 	 * greater than ist_expcmdsn, it's not in the window.
3100a6d42e7dSPeter Dunlap 	 */
3101a6d42e7dSPeter Dunlap 
3102a6d42e7dSPeter Dunlap 	if (iscsit_sna_lt(cmdsn, (ist->ist_expcmdsn - iscsit_cmd_window())) ||
3103a6d42e7dSPeter Dunlap 	    !iscsit_sna_lte(cmdsn, ist->ist_expcmdsn)) {
3104a6d42e7dSPeter Dunlap 		rval = B_FALSE;
3105a6d42e7dSPeter Dunlap 	}
3106a6d42e7dSPeter Dunlap 
3107d618d68dSPriya Krishnan 	mutex_exit(&ist->ist_sn_mutex);
3108a6d42e7dSPeter Dunlap 
3109a6d42e7dSPeter Dunlap 	return (rval);
3110a6d42e7dSPeter Dunlap }
3111d618d68dSPriya Krishnan 
3112d618d68dSPriya Krishnan /*
3113d618d68dSPriya Krishnan  * iscsit_check_cmdsn_and_queue
3114d618d68dSPriya Krishnan  *
3115d618d68dSPriya Krishnan  * Independent of the order in which the iSCSI target receives non-immediate
3116d618d68dSPriya Krishnan  * command PDU across the entire session and any multiple connections within
3117d618d68dSPriya Krishnan  * the session, the target must deliver the commands to the SCSI layer in
3118d618d68dSPriya Krishnan  * CmdSN order. So out-of-order non-immediate commands are queued up on a
3119d618d68dSPriya Krishnan  * session-wide wait queue. Duplicate commands are ignored.
3120d618d68dSPriya Krishnan  *
31212727bb05STim Kordas  * returns B_TRUE for commands which can be executed immediately (are
31222727bb05STim Kordas  * non-deferred), B_FALSE for cases where a command was deferred or invalid.
3123d618d68dSPriya Krishnan  */
31242727bb05STim Kordas static boolean_t
3125d618d68dSPriya Krishnan iscsit_check_cmdsn_and_queue(idm_pdu_t *rx_pdu)
3126d618d68dSPriya Krishnan {
3127d618d68dSPriya Krishnan 	idm_conn_t		*ic = rx_pdu->isp_ic;
3128d618d68dSPriya Krishnan 	iscsit_conn_t		*ict = ic->ic_handle;
3129d618d68dSPriya Krishnan 	iscsit_sess_t		*ist = ict->ict_sess;
3130d618d68dSPriya Krishnan 	iscsi_scsi_cmd_hdr_t	*hdr = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
3131d618d68dSPriya Krishnan 
3132d618d68dSPriya Krishnan 	mutex_enter(&ist->ist_sn_mutex);
3133d618d68dSPriya Krishnan 	if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
3134d618d68dSPriya Krishnan 		/* do not queue, handle it immediately */
3135d618d68dSPriya Krishnan 		DTRACE_PROBE2(immediate__cmd, iscsit_sess_t *, ist,
3136d618d68dSPriya Krishnan 		    idm_pdu_t *, rx_pdu);
3137d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
31382727bb05STim Kordas 		return (B_TRUE);
3139d618d68dSPriya Krishnan 	}
31402727bb05STim Kordas 	/*
31412727bb05STim Kordas 	 * See RFC3270 3.1.1.2: non-immediate commands outside of the
31422727bb05STim Kordas 	 * expected window (from expcmdsn to maxcmdsn, inclusive)
31432727bb05STim Kordas 	 * should be silently ignored.
31442727bb05STim Kordas 	 */
31452727bb05STim Kordas 	if (iscsit_sna_lt(ist->ist_expcmdsn, ntohl(hdr->cmdsn)) &&
31462727bb05STim Kordas 	    iscsit_sna_lt(ntohl(hdr->cmdsn), ist->ist_maxcmdsn)) {
3147d618d68dSPriya Krishnan 		/*
3148d618d68dSPriya Krishnan 		 * Out-of-order commands (cmdSN higher than ExpCmdSN)
3149d618d68dSPriya Krishnan 		 * are staged on a fixed-size circular buffer until
3150d618d68dSPriya Krishnan 		 * the missing command is delivered to the SCSI layer.
3151d618d68dSPriya Krishnan 		 * Irrespective of the order of insertion into the
3152d618d68dSPriya Krishnan 		 * staging queue, the commands are processed out of the
3153d618d68dSPriya Krishnan 		 * queue in cmdSN order only.
3154d618d68dSPriya Krishnan 		 */
315555a3a0efSJosef 'Jeff' Sipek 		rx_pdu->isp_queue_time = gethrtime();
3156d618d68dSPriya Krishnan 		iscsit_add_pdu_to_queue(ist, rx_pdu);
3157d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
31582727bb05STim Kordas 		return (B_FALSE);
31592727bb05STim Kordas 	} else if (iscsit_sna_lt(ntohl(hdr->cmdsn), ist->ist_expcmdsn) ||
31602727bb05STim Kordas 	    iscsit_sna_lt(ist->ist_maxcmdsn, ntohl(hdr->cmdsn))) {
31612727bb05STim Kordas 		/*
31622727bb05STim Kordas 		 * See above, this command is outside of our acceptable
31632727bb05STim Kordas 		 * window, we need to discard/complete.
31642727bb05STim Kordas 		 */
3165d618d68dSPriya Krishnan 		DTRACE_PROBE3(cmdsn__lt__expcmdsn, iscsit_sess_t *, ist,
3166d618d68dSPriya Krishnan 		    iscsit_conn_t *, ict, idm_pdu_t *, rx_pdu);
3167d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
31682727bb05STim Kordas 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
31692727bb05STim Kordas 		/*
31702727bb05STim Kordas 		 * tell our callers that the PDU "finished."
31712727bb05STim Kordas 		 */
31722727bb05STim Kordas 		return (B_FALSE);
3173d618d68dSPriya Krishnan 	} else {
3174d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
31752727bb05STim Kordas 		return (B_TRUE);
3176d618d68dSPriya Krishnan 	}
3177d618d68dSPriya Krishnan }
3178d618d68dSPriya Krishnan 
3179d618d68dSPriya Krishnan /*
3180d618d68dSPriya Krishnan  * iscsit_add_pdu_to_queue() adds PDUs into the array indexed by
3181d618d68dSPriya Krishnan  * their cmdsn value. The length of the array is kept above the
3182d618d68dSPriya Krishnan  * maximum window size. The window keeps the cmdsn within a range
3183d618d68dSPriya Krishnan  * such that there are no collisons. e.g. the assumption is that
3184d618d68dSPriya Krishnan  * the windowing checks make it impossible to receive PDUs that
3185d618d68dSPriya Krishnan  * index into the same location in the array.
3186d618d68dSPriya Krishnan  */
3187d618d68dSPriya Krishnan static void
3188d618d68dSPriya Krishnan iscsit_add_pdu_to_queue(iscsit_sess_t *ist, idm_pdu_t *rx_pdu)
3189d618d68dSPriya Krishnan {
3190d618d68dSPriya Krishnan 	iscsit_cbuf_t	*cbuf	= ist->ist_rxpdu_queue;
3191*1f99504aSToomas Soome 	iscsit_conn_t	*ict	= rx_pdu->isp_ic->ic_handle;
3192d618d68dSPriya Krishnan 	uint32_t	cmdsn	=
3193d618d68dSPriya Krishnan 	    ((iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr)->cmdsn;
3194d618d68dSPriya Krishnan 	uint32_t	index;
3195d618d68dSPriya Krishnan 
3196d618d68dSPriya Krishnan 	ASSERT(MUTEX_HELD(&ist->ist_sn_mutex));
3197d618d68dSPriya Krishnan 	/*
3198d618d68dSPriya Krishnan 	 * If the connection is being torn down, then
3199d618d68dSPriya Krishnan 	 * don't add the PDU to the staging queue
3200d618d68dSPriya Krishnan 	 */
3201d618d68dSPriya Krishnan 	mutex_enter(&ict->ict_mutex);
3202d618d68dSPriya Krishnan 	if (ict->ict_lost) {
3203d618d68dSPriya Krishnan 		mutex_exit(&ict->ict_mutex);
3204d618d68dSPriya Krishnan 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
3205d618d68dSPriya Krishnan 		return;
3206d618d68dSPriya Krishnan 	}
3207d618d68dSPriya Krishnan 	iscsit_conn_dispatch_hold(ict);
3208d618d68dSPriya Krishnan 	mutex_exit(&ict->ict_mutex);
3209d618d68dSPriya Krishnan 
3210d618d68dSPriya Krishnan 	index = ntohl(cmdsn) % ISCSIT_RXPDU_QUEUE_LEN;
32113e2514dcSDan McDonald 	/*
32123e2514dcSDan McDonald 	 * In the normal case, assuming that the Initiator is not
32133e2514dcSDan McDonald 	 * buggy and that we don't have packet duplication occuring,
32143e2514dcSDan McDonald 	 * the entry in the array will be NULL.  However, we may have
32153e2514dcSDan McDonald 	 * received a duplicate PDU with cmdsn > expsn , and in that
32163e2514dcSDan McDonald 	 * case we just ignore this PDU -- the previously received one
32173e2514dcSDan McDonald 	 * remains queued for processing.  We need to be careful not
32183e2514dcSDan McDonald 	 * to leak this one however.
32193e2514dcSDan McDonald 	 */
32203e2514dcSDan McDonald 	if (cbuf->cb_buffer[index] != NULL) {
32213e2514dcSDan McDonald 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
32223e2514dcSDan McDonald 	} else {
32233e2514dcSDan McDonald 		cbuf->cb_buffer[index] = rx_pdu;
32243e2514dcSDan McDonald 		cbuf->cb_num_elems++;
32253e2514dcSDan McDonald 	}
3226d618d68dSPriya Krishnan }
3227d618d68dSPriya Krishnan 
3228d618d68dSPriya Krishnan static idm_pdu_t *
3229d618d68dSPriya Krishnan iscsit_remove_pdu_from_queue(iscsit_sess_t *ist, uint32_t cmdsn)
3230d618d68dSPriya Krishnan {
3231d618d68dSPriya Krishnan 	iscsit_cbuf_t	*cbuf	= ist->ist_rxpdu_queue;
3232d618d68dSPriya Krishnan 	idm_pdu_t	*pdu	= NULL;
3233d618d68dSPriya Krishnan 	uint32_t	index;
3234d618d68dSPriya Krishnan 
3235d618d68dSPriya Krishnan 	ASSERT(MUTEX_HELD(&ist->ist_sn_mutex));
3236d618d68dSPriya Krishnan 	index = cmdsn % ISCSIT_RXPDU_QUEUE_LEN;
3237d618d68dSPriya Krishnan 	if ((pdu = cbuf->cb_buffer[index]) != NULL) {
3238d618d68dSPriya Krishnan 		ASSERT(cmdsn ==
3239d618d68dSPriya Krishnan 		    ntohl(((iscsi_scsi_cmd_hdr_t *)pdu->isp_hdr)->cmdsn));
3240d618d68dSPriya Krishnan 		cbuf->cb_buffer[index] = NULL;
3241d618d68dSPriya Krishnan 		cbuf->cb_num_elems--;
3242d618d68dSPriya Krishnan 		return (pdu);
3243d618d68dSPriya Krishnan 	}
3244d618d68dSPriya Krishnan 	return (NULL);
3245d618d68dSPriya Krishnan }
3246d618d68dSPriya Krishnan 
3247d618d68dSPriya Krishnan /*
3248d618d68dSPriya Krishnan  * iscsit_process_pdu_in_queue() finds the next pdu in sequence
3249d618d68dSPriya Krishnan  * and posts it to the SCSI layer
3250d618d68dSPriya Krishnan  */
3251d618d68dSPriya Krishnan static void
3252d618d68dSPriya Krishnan iscsit_process_pdu_in_queue(iscsit_sess_t *ist)
3253d618d68dSPriya Krishnan {
3254d618d68dSPriya Krishnan 	iscsit_cbuf_t	*cbuf	= ist->ist_rxpdu_queue;
3255d618d68dSPriya Krishnan 	idm_pdu_t	*pdu = NULL;
3256d618d68dSPriya Krishnan 	uint32_t	expcmdsn;
3257d618d68dSPriya Krishnan 
3258d618d68dSPriya Krishnan 	for (;;) {
3259d618d68dSPriya Krishnan 		mutex_enter(&ist->ist_sn_mutex);
3260d618d68dSPriya Krishnan 		if (cbuf->cb_num_elems == 0) {
3261d618d68dSPriya Krishnan 			mutex_exit(&ist->ist_sn_mutex);
3262d618d68dSPriya Krishnan 			break;
3263d618d68dSPriya Krishnan 		}
3264d618d68dSPriya Krishnan 		expcmdsn = ist->ist_expcmdsn;
3265d618d68dSPriya Krishnan 		if ((pdu = iscsit_remove_pdu_from_queue(ist, expcmdsn))
3266d618d68dSPriya Krishnan 		    == NULL) {
3267d618d68dSPriya Krishnan 			mutex_exit(&ist->ist_sn_mutex);
3268d618d68dSPriya Krishnan 			break;
3269d618d68dSPriya Krishnan 		}
3270d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
3271d618d68dSPriya Krishnan 		iscsit_post_staged_pdu(pdu);
3272d618d68dSPriya Krishnan 	}
3273d618d68dSPriya Krishnan }
3274d618d68dSPriya Krishnan 
3275d618d68dSPriya Krishnan static void
3276d618d68dSPriya Krishnan iscsit_post_staged_pdu(idm_pdu_t *rx_pdu)
3277d618d68dSPriya Krishnan {
3278d618d68dSPriya Krishnan 	iscsit_conn_t	*ict	= rx_pdu->isp_ic->ic_handle;
3279d618d68dSPriya Krishnan 
3280d618d68dSPriya Krishnan 	/* Post the PDU to the SCSI layer */
3281d618d68dSPriya Krishnan 	switch (IDM_PDU_OPCODE(rx_pdu)) {
3282d618d68dSPriya Krishnan 	case ISCSI_OP_NOOP_OUT:
3283d618d68dSPriya Krishnan 		iscsit_set_cmdsn(ict, rx_pdu);
3284d618d68dSPriya Krishnan 		iscsit_pdu_op_noop(ict, rx_pdu);
3285d618d68dSPriya Krishnan 		break;
3286d618d68dSPriya Krishnan 	case ISCSI_OP_TEXT_CMD:
3287d618d68dSPriya Krishnan 		iscsit_set_cmdsn(ict, rx_pdu);
3288d618d68dSPriya Krishnan 		iscsit_pdu_op_text_cmd(ict, rx_pdu);
3289d618d68dSPriya Krishnan 		break;
3290d618d68dSPriya Krishnan 	case ISCSI_OP_SCSI_TASK_MGT_MSG:
3291d618d68dSPriya Krishnan 		iscsit_set_cmdsn(ict, rx_pdu);
3292d618d68dSPriya Krishnan 		iscsit_op_scsi_task_mgmt(ict, rx_pdu);
3293d618d68dSPriya Krishnan 		break;
3294d618d68dSPriya Krishnan 	case ISCSI_OP_SCSI_CMD:
3295d618d68dSPriya Krishnan 		/* cmdSN will be incremented after creating itask */
3296d618d68dSPriya Krishnan 		iscsit_post_scsi_cmd(rx_pdu->isp_ic, rx_pdu);
3297d618d68dSPriya Krishnan 		break;
3298d618d68dSPriya Krishnan 	case ISCSI_OP_LOGOUT_CMD:
3299d618d68dSPriya Krishnan 		iscsit_set_cmdsn(ict, rx_pdu);
3300d618d68dSPriya Krishnan 		iscsit_pdu_op_logout_cmd(ict, rx_pdu);
3301d618d68dSPriya Krishnan 		break;
3302d618d68dSPriya Krishnan 	default:
3303d618d68dSPriya Krishnan 		/* No other PDUs should be placed on the queue */
3304d618d68dSPriya Krishnan 		ASSERT(0);
3305d618d68dSPriya Krishnan 	}
3306d618d68dSPriya Krishnan 	iscsit_conn_dispatch_rele(ict); /* release hold on the conn */
3307d618d68dSPriya Krishnan }
3308d618d68dSPriya Krishnan 
3309d618d68dSPriya Krishnan /* ARGSUSED */
3310d618d68dSPriya Krishnan void
3311d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor_start(void)
3312d618d68dSPriya Krishnan {
3313d618d68dSPriya Krishnan 	mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3314d618d68dSPriya Krishnan 	if (iscsit_rxpdu_queue_monitor_thr_running) {
3315d618d68dSPriya Krishnan 		mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3316d618d68dSPriya Krishnan 		return;
3317d618d68dSPriya Krishnan 	}
3318d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_thr_id =
3319d618d68dSPriya Krishnan 	    thread_create(NULL, 0, iscsit_rxpdu_queue_monitor, NULL,
3320d618d68dSPriya Krishnan 	    0, &p0, TS_RUN, minclsyspri);
3321d618d68dSPriya Krishnan 	while (!iscsit_rxpdu_queue_monitor_thr_running) {
3322d618d68dSPriya Krishnan 		cv_wait(&iscsit_rxpdu_queue_monitor_cv,
3323d618d68dSPriya Krishnan 		    &iscsit_rxpdu_queue_monitor_mutex);
3324d618d68dSPriya Krishnan 	}
3325d618d68dSPriya Krishnan 	mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3326d618d68dSPriya Krishnan 
3327d618d68dSPriya Krishnan }
3328d618d68dSPriya Krishnan 
3329d618d68dSPriya Krishnan /* ARGSUSED */
3330d618d68dSPriya Krishnan void
3331d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor_stop(void)
3332d618d68dSPriya Krishnan {
3333d618d68dSPriya Krishnan 	mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3334d618d68dSPriya Krishnan 	if (iscsit_rxpdu_queue_monitor_thr_running) {
3335d618d68dSPriya Krishnan 		iscsit_rxpdu_queue_monitor_thr_running = B_FALSE;
3336d618d68dSPriya Krishnan 		cv_signal(&iscsit_rxpdu_queue_monitor_cv);
3337d618d68dSPriya Krishnan 		mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3338d618d68dSPriya Krishnan 
3339d618d68dSPriya Krishnan 		thread_join(iscsit_rxpdu_queue_monitor_thr_did);
3340d618d68dSPriya Krishnan 		return;
3341d618d68dSPriya Krishnan 	}
3342d618d68dSPriya Krishnan 	mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3343d618d68dSPriya Krishnan }
3344d618d68dSPriya Krishnan 
3345d618d68dSPriya Krishnan /*
3346d618d68dSPriya Krishnan  * A separate thread is used to scan the staging queue on all the
3347d618d68dSPriya Krishnan  * sessions, If a delayed PDU does not arrive within a timeout, the
3348d618d68dSPriya Krishnan  * target will advance to the staged PDU that is next in sequence
3349d618d68dSPriya Krishnan  * and exceeded the threshold wait time. It is up to the initiator
3350d618d68dSPriya Krishnan  * to note that the target has not acknowledged a particular cmdsn
3351d618d68dSPriya Krishnan  * and take appropriate action.
3352d618d68dSPriya Krishnan  */
3353d618d68dSPriya Krishnan /* ARGSUSED */
3354d618d68dSPriya Krishnan static void
3355d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor(void *arg)
3356d618d68dSPriya Krishnan {
3357d618d68dSPriya Krishnan 	iscsit_tgt_t	*tgt;
3358d618d68dSPriya Krishnan 	iscsit_sess_t	*ist;
3359d618d68dSPriya Krishnan 
3360d618d68dSPriya Krishnan 	mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3361d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_thr_did = curthread->t_did;
3362d618d68dSPriya Krishnan 	iscsit_rxpdu_queue_monitor_thr_running = B_TRUE;
3363d618d68dSPriya Krishnan 	cv_signal(&iscsit_rxpdu_queue_monitor_cv);
3364d618d68dSPriya Krishnan 
3365d618d68dSPriya Krishnan 	while (iscsit_rxpdu_queue_monitor_thr_running) {
3366d618d68dSPriya Krishnan 		ISCSIT_GLOBAL_LOCK(RW_READER);
3367d618d68dSPriya Krishnan 		for (tgt = avl_first(&iscsit_global.global_target_list);
3368d618d68dSPriya Krishnan 		    tgt != NULL;
3369d618d68dSPriya Krishnan 		    tgt = AVL_NEXT(&iscsit_global.global_target_list, tgt)) {
3370d618d68dSPriya Krishnan 			mutex_enter(&tgt->target_mutex);
3371d618d68dSPriya Krishnan 			for (ist = avl_first(&tgt->target_sess_list);
3372d618d68dSPriya Krishnan 			    ist != NULL;
3373d618d68dSPriya Krishnan 			    ist = AVL_NEXT(&tgt->target_sess_list, ist)) {
3374d618d68dSPriya Krishnan 
3375d618d68dSPriya Krishnan 				iscsit_rxpdu_queue_monitor_session(ist);
3376d618d68dSPriya Krishnan 			}
3377d618d68dSPriya Krishnan 			mutex_exit(&tgt->target_mutex);
3378d618d68dSPriya Krishnan 		}
3379d618d68dSPriya Krishnan 		ISCSIT_GLOBAL_UNLOCK();
3380d618d68dSPriya Krishnan 		if (iscsit_rxpdu_queue_monitor_thr_running == B_FALSE) {
3381d618d68dSPriya Krishnan 			break;
3382d618d68dSPriya Krishnan 		}
3383d618d68dSPriya Krishnan 		(void) cv_reltimedwait(&iscsit_rxpdu_queue_monitor_cv,
3384d618d68dSPriya Krishnan 		    &iscsit_rxpdu_queue_monitor_mutex,
3385d618d68dSPriya Krishnan 		    ISCSIT_RXPDU_QUEUE_MONITOR_INTERVAL * drv_usectohz(1000000),
3386d618d68dSPriya Krishnan 		    TR_CLOCK_TICK);
3387d618d68dSPriya Krishnan 	}
3388d618d68dSPriya Krishnan 	mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3389d618d68dSPriya Krishnan 	thread_exit();
3390d618d68dSPriya Krishnan }
3391d618d68dSPriya Krishnan 
3392d618d68dSPriya Krishnan static void
3393d618d68dSPriya Krishnan iscsit_rxpdu_queue_monitor_session(iscsit_sess_t *ist)
3394d618d68dSPriya Krishnan {
3395d618d68dSPriya Krishnan 	iscsit_cbuf_t	*cbuf	= ist->ist_rxpdu_queue;
3396d618d68dSPriya Krishnan 	idm_pdu_t	*next_pdu = NULL;
3397d618d68dSPriya Krishnan 	uint32_t	index, next_cmdsn, i;
3398d618d68dSPriya Krishnan 
3399d618d68dSPriya Krishnan 	/*
3400d618d68dSPriya Krishnan 	 * Assume that all PDUs in the staging queue have a cmdsn >= expcmdsn.
3401d618d68dSPriya Krishnan 	 * Starting with the expcmdsn, iterate over the staged PDUs to find
3402d618d68dSPriya Krishnan 	 * the next PDU with a wait time greater than the threshold. If found
3403d618d68dSPriya Krishnan 	 * advance the staged PDU to the SCSI layer, skipping over the missing
3404d618d68dSPriya Krishnan 	 * PDU(s) to get past the hole in the command sequence. It is up to
3405d618d68dSPriya Krishnan 	 * the initiator to note that the target has not acknowledged a cmdsn
3406d618d68dSPriya Krishnan 	 * and take appropriate action.
3407d618d68dSPriya Krishnan 	 *
3408d618d68dSPriya Krishnan 	 * Since the PDU(s) arrive in any random order, it is possible that
3409d618d68dSPriya Krishnan 	 * that the actual wait time for a particular PDU is much longer than
3410d618d68dSPriya Krishnan 	 * the defined threshold. e.g. Consider a case where commands are sent
3411d618d68dSPriya Krishnan 	 * over 4 different connections, and cmdsn = 1004 arrives first, then
3412d618d68dSPriya Krishnan 	 * 1003, and 1002 and 1001 are lost due to a connection failure.
3413d618d68dSPriya Krishnan 	 * So now 1003 is waiting for 1002 to be delivered, and although the
3414d618d68dSPriya Krishnan 	 * wait time of 1004 > wait time of 1003, only 1003 will be considered
3415d618d68dSPriya Krishnan 	 * by the monitor thread. 1004 will be automatically processed by
3416d618d68dSPriya Krishnan 	 * iscsit_process_pdu_in_queue() once the scan is complete and the
3417d618d68dSPriya Krishnan 	 * expcmdsn becomes current.
3418d618d68dSPriya Krishnan 	 */
3419d618d68dSPriya Krishnan 	mutex_enter(&ist->ist_sn_mutex);
3420d618d68dSPriya Krishnan 	cbuf = ist->ist_rxpdu_queue;
3421d618d68dSPriya Krishnan 	if (cbuf->cb_num_elems == 0) {
3422d618d68dSPriya Krishnan 		mutex_exit(&ist->ist_sn_mutex);
3423d618d68dSPriya Krishnan 		return;
3424d618d68dSPriya Krishnan 	}
3425d618d68dSPriya Krishnan 	for (next_pdu = NULL, i = 0; ; i++) {
3426d618d68dSPriya Krishnan 		next_cmdsn = ist->ist_expcmdsn + i; /* start at expcmdsn */
3427d618d68dSPriya Krishnan 		index = next_cmdsn % ISCSIT_RXPDU_QUEUE_LEN;
3428d618d68dSPriya Krishnan 		if ((next_pdu = cbuf->cb_buffer[index]) != NULL) {
3429d618d68dSPriya Krishnan 			/*
3430d618d68dSPriya Krishnan 			 * If the PDU wait time has not exceeded threshold
3431d618d68dSPriya Krishnan 			 * stop scanning the staging queue until the timer
3432d618d68dSPriya Krishnan 			 * fires again
3433d618d68dSPriya Krishnan 			 */
343455a3a0efSJosef 'Jeff' Sipek 			if ((gethrtime() - next_pdu->isp_queue_time)
343555a3a0efSJosef 'Jeff' Sipek 			    < (rxpdu_queue_threshold * NANOSEC)) {
3436d618d68dSPriya Krishnan 				mutex_exit(&ist->ist_sn_mutex);
3437d618d68dSPriya Krishnan 				return;
3438d618d68dSPriya Krishnan 			}
3439d618d68dSPriya Krishnan 			/*
3440d618d68dSPriya Krishnan 			 * Remove the next PDU from the queue and post it
3441d618d68dSPriya Krishnan 			 * to the SCSI layer, skipping over the missing
3442d618d68dSPriya Krishnan 			 * PDU. Stop scanning the staging queue until
3443d618d68dSPriya Krishnan 			 * the monitor timer fires again
3444d618d68dSPriya Krishnan 			 */
3445d618d68dSPriya Krishnan 			(void) iscsit_remove_pdu_from_queue(ist, next_cmdsn);
3446d618d68dSPriya Krishnan 			mutex_exit(&ist->ist_sn_mutex);
3447d618d68dSPriya Krishnan 			DTRACE_PROBE3(advanced__to__blocked__cmdsn,
3448d618d68dSPriya Krishnan 			    iscsit_sess_t *, ist, idm_pdu_t *, next_pdu,
3449d618d68dSPriya Krishnan 			    uint32_t, next_cmdsn);
3450d618d68dSPriya Krishnan 			iscsit_post_staged_pdu(next_pdu);
3451d618d68dSPriya Krishnan 			/* Deliver any subsequent PDUs immediately */
3452d618d68dSPriya Krishnan 			iscsit_process_pdu_in_queue(ist);
3453d618d68dSPriya Krishnan 			return;
3454d618d68dSPriya Krishnan 		}
3455d618d68dSPriya Krishnan 		/*
3456d618d68dSPriya Krishnan 		 * Skipping over i PDUs, e.g. a case where commands 1001 and
3457d618d68dSPriya Krishnan 		 * 1002 are lost in the network, skip over both and post 1003
3458d618d68dSPriya Krishnan 		 * expcmdsn then becomes 1004 at the end of the scan.
3459d618d68dSPriya Krishnan 		 */
3460d618d68dSPriya Krishnan 		DTRACE_PROBE2(skipping__over__cmdsn, iscsit_sess_t *, ist,
3461d618d68dSPriya Krishnan 		    uint32_t, next_cmdsn);
3462d618d68dSPriya Krishnan 	}
3463d618d68dSPriya Krishnan 	/*
3464d618d68dSPriya Krishnan 	 * following the assumption, staged cmdsn >= expcmdsn, this statement
3465d618d68dSPriya Krishnan 	 * is never reached.
3466d618d68dSPriya Krishnan 	 */
3467d618d68dSPriya Krishnan }
3468