1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/cpuvar.h>
27 #include <sys/types.h>
28 #include <sys/conf.h>
29 #include <sys/stat.h>
30 #include <sys/file.h>
31 #include <sys/ddi.h>
32 #include <sys/sunddi.h>
33 #include <sys/modctl.h>
34 #include <sys/sysmacros.h>
35 #include <sys/socket.h>
36 #include <sys/strsubr.h>
37 #include <sys/nvpair.h>
38 
39 #include <sys/stmf.h>
40 #include <sys/stmf_ioctl.h>
41 #include <sys/portif.h>
42 #include <sys/idm/idm.h>
43 #include <sys/idm/idm_conn_sm.h>
44 #include <iscsit_isns.h>
45 #include <iscsit.h>
46 
47 #define	ISCSIT_VERSION		BUILD_DATE "-1.18dev"
48 #define	ISCSIT_NAME_VERSION	"COMSTAR ISCSIT v" ISCSIT_VERSION
49 
50 /*
51  * DDI entry points.
52  */
53 static int iscsit_drv_attach(dev_info_t *, ddi_attach_cmd_t);
54 static int iscsit_drv_detach(dev_info_t *, ddi_detach_cmd_t);
55 static int iscsit_drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
56 static int iscsit_drv_open(dev_t *, int, int, cred_t *);
57 static int iscsit_drv_close(dev_t, int, int, cred_t *);
58 static boolean_t iscsit_drv_busy(void);
59 static int iscsit_drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
60 
61 extern struct mod_ops mod_miscops;
62 
63 
64 static struct cb_ops iscsit_cb_ops = {
65 	iscsit_drv_open,	/* cb_open */
66 	iscsit_drv_close,	/* cb_close */
67 	nodev,			/* cb_strategy */
68 	nodev,			/* cb_print */
69 	nodev,			/* cb_dump */
70 	nodev,			/* cb_read */
71 	nodev,			/* cb_write */
72 	iscsit_drv_ioctl,	/* cb_ioctl */
73 	nodev,			/* cb_devmap */
74 	nodev,			/* cb_mmap */
75 	nodev,			/* cb_segmap */
76 	nochpoll,		/* cb_chpoll */
77 	ddi_prop_op,		/* cb_prop_op */
78 	NULL,			/* cb_streamtab */
79 	D_MP,			/* cb_flag */
80 	CB_REV,			/* cb_rev */
81 	nodev,			/* cb_aread */
82 	nodev,			/* cb_awrite */
83 };
84 
85 static struct dev_ops iscsit_dev_ops = {
86 	DEVO_REV,		/* devo_rev */
87 	0,			/* devo_refcnt */
88 	iscsit_drv_getinfo,	/* devo_getinfo */
89 	nulldev,		/* devo_identify */
90 	nulldev,		/* devo_probe */
91 	iscsit_drv_attach,	/* devo_attach */
92 	iscsit_drv_detach,	/* devo_detach */
93 	nodev,			/* devo_reset */
94 	&iscsit_cb_ops,		/* devo_cb_ops */
95 	NULL,			/* devo_bus_ops */
96 	NULL,			/* devo_power */
97 	ddi_quiesce_not_needed,	/* quiesce */
98 };
99 
100 static struct modldrv modldrv = {
101 	&mod_driverops,
102 	"iSCSI Target",
103 	&iscsit_dev_ops,
104 };
105 
106 static struct modlinkage modlinkage = {
107 	MODREV_1,
108 	&modldrv,
109 	NULL,
110 };
111 
112 
113 iscsit_global_t iscsit_global;
114 
115 kmem_cache_t	*iscsit_status_pdu_cache;
116 
117 boolean_t	iscsit_sm_logging = B_FALSE;
118 
119 static idm_status_t iscsit_init(dev_info_t *dip);
120 static idm_status_t iscsit_enable_svc(iscsit_hostinfo_t *hostinfo);
121 static void iscsit_disable_svc(void);
122 
123 static void
124 iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
125 
126 static void
127 iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
128 
129 static void
130 iscsit_pdu_op_login_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
131 
132 void
133 iscsit_pdu_op_text_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
134 
135 static void
136 iscsit_pdu_op_logout_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
137 
138 int iscsit_cmd_window();
139 
140 void
141 iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
142 
143 static void
144 iscsit_deferred_dispatch(idm_pdu_t *rx_pdu);
145 
146 static void
147 iscsit_deferred(void *rx_pdu_void);
148 
149 static idm_status_t
150 iscsit_conn_accept(idm_conn_t *ic);
151 
152 static idm_status_t
153 iscsit_ffp_enabled(idm_conn_t *ic);
154 
155 static idm_status_t
156 iscsit_ffp_disabled(idm_conn_t *ic, idm_ffp_disable_t disable_class);
157 
158 static idm_status_t
159 iscsit_conn_lost(idm_conn_t *ic);
160 
161 static idm_status_t
162 iscsit_conn_destroy(idm_conn_t *ic);
163 
164 static stmf_data_buf_t *
165 iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
166     uint32_t flags);
167 
168 static void
169 iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf);
170 
171 static void
172 iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status);
173 
174 static void
175 iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status);
176 
177 static void
178 iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status);
179 
180 static stmf_status_t
181 iscsit_idm_to_stmf(idm_status_t idmrc);
182 
183 static iscsit_task_t *
184 iscsit_task_alloc(iscsit_conn_t *ict);
185 
186 static void
187 iscsit_task_free(iscsit_task_t *itask);
188 
189 static iscsit_task_t *
190 iscsit_tm_task_alloc(iscsit_conn_t *ict);
191 
192 static void
193 iscsit_tm_task_free(iscsit_task_t *itask);
194 
195 static idm_status_t
196 iscsit_task_start(iscsit_task_t *itask);
197 
198 static void
199 iscsit_task_done(iscsit_task_t *itask);
200 
201 static int
202 iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags);
203 
204 static void
205 iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags);
206 
207 static it_cfg_status_t
208 iscsit_config_merge(it_config_t *cfg);
209 
210 static idm_status_t
211 iscsit_login_fail(idm_conn_t *ic);
212 
213 static boolean_t iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn);
214 static void iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu,
215     uint8_t response, uint8_t cmd_status);
216 static void iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu,
217     uint8_t tm_status);
218 
219 int
220 _init(void)
221 {
222 	int rc;
223 
224 	rw_init(&iscsit_global.global_rwlock, NULL, RW_DRIVER, NULL);
225 	iscsit_global.global_svc_state = ISE_DETACHED;
226 
227 	if ((rc = mod_install(&modlinkage)) != 0) {
228 		rw_destroy(&iscsit_global.global_rwlock);
229 		return (rc);
230 	}
231 
232 	return (rc);
233 }
234 
235 int
236 _info(struct modinfo *modinfop)
237 {
238 	return (mod_info(&modlinkage, modinfop));
239 }
240 
241 int
242 _fini(void)
243 {
244 	int rc;
245 
246 	rc = mod_remove(&modlinkage);
247 
248 	if (rc == 0) {
249 		rw_destroy(&iscsit_global.global_rwlock);
250 	}
251 
252 	return (rc);
253 }
254 
255 /*
256  * DDI entry points.
257  */
258 
259 /* ARGSUSED */
260 static int
261 iscsit_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,
262     void **result)
263 {
264 	ulong_t instance = getminor((dev_t)arg);
265 
266 	switch (cmd) {
267 	case DDI_INFO_DEVT2DEVINFO:
268 		*result = iscsit_global.global_dip;
269 		return (DDI_SUCCESS);
270 
271 	case DDI_INFO_DEVT2INSTANCE:
272 		*result = (void *)instance;
273 		return (DDI_SUCCESS);
274 
275 	default:
276 		break;
277 	}
278 
279 	return (DDI_FAILURE);
280 }
281 
282 static int
283 iscsit_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
284 {
285 	if (cmd != DDI_ATTACH) {
286 		return (DDI_FAILURE);
287 	}
288 
289 	if (ddi_get_instance(dip) != 0) {
290 		/* we only allow instance 0 to attach */
291 		return (DDI_FAILURE);
292 	}
293 
294 	/* create the minor node */
295 	if (ddi_create_minor_node(dip, ISCSIT_MODNAME, S_IFCHR, 0,
296 	    DDI_PSEUDO, 0) != DDI_SUCCESS) {
297 		cmn_err(CE_WARN, "iscsit_drv_attach: "
298 		    "failed creating minor node");
299 		return (DDI_FAILURE);
300 	}
301 
302 	if (iscsit_init(dip) != IDM_STATUS_SUCCESS) {
303 		cmn_err(CE_WARN, "iscsit_drv_attach: "
304 		    "failed to initialize");
305 		ddi_remove_minor_node(dip, NULL);
306 		return (DDI_FAILURE);
307 	}
308 
309 	iscsit_global.global_svc_state = ISE_DISABLED;
310 	iscsit_global.global_dip = dip;
311 
312 	return (DDI_SUCCESS);
313 }
314 
315 /*ARGSUSED*/
316 static int
317 iscsit_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
318 {
319 	if (cmd != DDI_DETACH)
320 		return (DDI_FAILURE);
321 
322 	ISCSIT_GLOBAL_LOCK(RW_WRITER);
323 	if (iscsit_drv_busy()) {
324 		ISCSIT_GLOBAL_UNLOCK();
325 		return (EBUSY);
326 	}
327 
328 	iscsit_global.global_dip = NULL;
329 	ddi_remove_minor_node(dip, NULL);
330 
331 	ldi_ident_release(iscsit_global.global_li);
332 	iscsit_global.global_svc_state = ISE_DETACHED;
333 
334 	ISCSIT_GLOBAL_UNLOCK();
335 
336 	return (DDI_SUCCESS);
337 }
338 
339 /*ARGSUSED*/
340 static int
341 iscsit_drv_open(dev_t *devp, int flag, int otyp, cred_t *credp)
342 {
343 	return (0);
344 }
345 
346 /* ARGSUSED */
347 static int
348 iscsit_drv_close(dev_t dev, int flag, int otyp, cred_t *credp)
349 {
350 	return (0);
351 }
352 
353 static boolean_t
354 iscsit_drv_busy(void)
355 {
356 	switch (iscsit_global.global_svc_state) {
357 	case ISE_DISABLED:
358 	case ISE_DETACHED:
359 		return (B_FALSE);
360 	default:
361 		return (B_TRUE);
362 	}
363 	/* NOTREACHED */
364 }
365 
366 /* ARGSUSED */
367 static int
368 iscsit_drv_ioctl(dev_t drv, int cmd, intptr_t argp, int flag, cred_t *cred,
369     int *retval)
370 {
371 	iscsit_ioc_set_config_t		setcfg;
372 	iscsit_ioc_set_config32_t	setcfg32;
373 	char				*cfg_pnvlist = NULL;
374 	nvlist_t			*cfg_nvlist = NULL;
375 	it_config_t			*cfg = NULL;
376 	idm_status_t			idmrc;
377 	int				rc = 0;
378 
379 	if (drv_priv(cred) != 0) {
380 		return (EPERM);
381 	}
382 
383 	ISCSIT_GLOBAL_LOCK(RW_WRITER);
384 
385 	/*
386 	 * Validate ioctl requests against global service state
387 	 */
388 	switch (iscsit_global.global_svc_state) {
389 	case ISE_ENABLED:
390 		if (cmd == ISCSIT_IOC_DISABLE_SVC) {
391 			iscsit_global.global_svc_state = ISE_DISABLING;
392 		} else if (cmd == ISCSIT_IOC_ENABLE_SVC) {
393 			/* Already enabled */
394 			ISCSIT_GLOBAL_UNLOCK();
395 			return (0);
396 		} else {
397 			iscsit_global.global_svc_state = ISE_BUSY;
398 		}
399 		break;
400 	case ISE_DISABLED:
401 		if (cmd == ISCSIT_IOC_ENABLE_SVC) {
402 			iscsit_global.global_svc_state = ISE_ENABLING;
403 		} else if (cmd == ISCSIT_IOC_DISABLE_SVC) {
404 			/* Already disabled */
405 			ISCSIT_GLOBAL_UNLOCK();
406 			return (0);
407 		} else {
408 			rc = EFAULT;
409 		}
410 		break;
411 	case ISE_ENABLING:
412 	case ISE_DISABLING:
413 		rc = EAGAIN;
414 		break;
415 	case ISE_DETACHED:
416 	default:
417 		rc = EFAULT;
418 		break;
419 	}
420 
421 	ISCSIT_GLOBAL_UNLOCK();
422 	if (rc != 0)
423 		return (rc);
424 
425 	/* Handle ioctl request (enable/disable have already been handled) */
426 	switch (cmd) {
427 	case ISCSIT_IOC_SET_CONFIG:
428 		/* Any errors must set state back to ISE_ENABLED */
429 		switch (ddi_model_convert_from(flag & FMODELS)) {
430 		case DDI_MODEL_ILP32:
431 			if (ddi_copyin((void *)argp, &setcfg32,
432 			    sizeof (iscsit_ioc_set_config32_t), flag) != 0) {
433 				rc = EFAULT;
434 				goto cleanup;
435 			}
436 
437 			setcfg.set_cfg_pnvlist =
438 			    (char *)((uintptr_t)setcfg32.set_cfg_pnvlist);
439 			setcfg.set_cfg_vers = setcfg32.set_cfg_vers;
440 			setcfg.set_cfg_pnvlist_len =
441 			    setcfg32.set_cfg_pnvlist_len;
442 			break;
443 		case DDI_MODEL_NONE:
444 			if (ddi_copyin((void *)argp, &setcfg,
445 			    sizeof (iscsit_ioc_set_config_t), flag) != 0) {
446 				rc = EFAULT;
447 				goto cleanup;
448 			}
449 			break;
450 		default:
451 			rc = EFAULT;
452 			goto cleanup;
453 		}
454 
455 		/* Check API version */
456 		if (setcfg.set_cfg_vers != ISCSIT_API_VERS0) {
457 			rc = EINVAL;
458 			goto cleanup;
459 		}
460 
461 		/* Config is in packed nvlist format so unpack it */
462 		cfg_pnvlist = kmem_alloc(setcfg.set_cfg_pnvlist_len,
463 		    KM_SLEEP);
464 		ASSERT(cfg_pnvlist != NULL);
465 
466 		if (ddi_copyin(setcfg.set_cfg_pnvlist, cfg_pnvlist,
467 		    setcfg.set_cfg_pnvlist_len, flag) != 0) {
468 			rc = EFAULT;
469 			goto cleanup;
470 		}
471 
472 		rc = nvlist_unpack(cfg_pnvlist, setcfg.set_cfg_pnvlist_len,
473 		    &cfg_nvlist, KM_SLEEP);
474 		if (rc != 0) {
475 			goto cleanup;
476 		}
477 
478 		/* Translate nvlist */
479 		rc = it_nv_to_config(cfg_nvlist, &cfg);
480 		if (rc != 0) {
481 			cmn_err(CE_WARN, "Configuration is invalid");
482 			goto cleanup;
483 		}
484 
485 		/* Update config */
486 		rc = iscsit_config_merge(cfg);
487 		/* FALLTHROUGH */
488 
489 cleanup:
490 		if (cfg)
491 			it_config_free_cmn(cfg);
492 		if (cfg_pnvlist)
493 			kmem_free(cfg_pnvlist, setcfg.set_cfg_pnvlist_len);
494 		if (cfg_nvlist)
495 			nvlist_free(cfg_nvlist);
496 
497 		/*
498 		 * Now that the reconfig is complete set our state back to
499 		 * enabled.
500 		 */
501 		ISCSIT_GLOBAL_LOCK(RW_WRITER);
502 		iscsit_global.global_svc_state = ISE_ENABLED;
503 		ISCSIT_GLOBAL_UNLOCK();
504 		break;
505 	case ISCSIT_IOC_ENABLE_SVC: {
506 		iscsit_hostinfo_t hostinfo;
507 
508 		if (ddi_copyin((void *)argp, &hostinfo.length,
509 		    sizeof (hostinfo.length), flag) != 0) {
510 			iscsit_global.global_svc_state = ISE_DISABLED;
511 			return (EFAULT);
512 		}
513 
514 		if (hostinfo.length > sizeof (hostinfo.fqhn))
515 			hostinfo.length = sizeof (hostinfo.fqhn);
516 
517 		if (ddi_copyin((void *)((caddr_t)argp +
518 		    sizeof (hostinfo.length)), &hostinfo.fqhn,
519 		    hostinfo.length, flag) != 0) {
520 			iscsit_global.global_svc_state = ISE_DISABLED;
521 			return (EFAULT);
522 		}
523 
524 		idmrc = iscsit_enable_svc(&hostinfo);
525 		ISCSIT_GLOBAL_LOCK(RW_WRITER);
526 		if (idmrc == IDM_STATUS_SUCCESS) {
527 			iscsit_global.global_svc_state = ISE_ENABLED;
528 		} else {
529 			rc = EIO;
530 			iscsit_global.global_svc_state = ISE_DISABLED;
531 		}
532 		ISCSIT_GLOBAL_UNLOCK();
533 		break;
534 	}
535 	case ISCSIT_IOC_DISABLE_SVC:
536 		iscsit_disable_svc();
537 		ISCSIT_GLOBAL_LOCK(RW_WRITER);
538 		iscsit_global.global_svc_state = ISE_DISABLED;
539 		ISCSIT_GLOBAL_UNLOCK();
540 		break;
541 
542 	default:
543 		rc = EINVAL;
544 		ISCSIT_GLOBAL_LOCK(RW_WRITER);
545 		iscsit_global.global_svc_state = ISE_ENABLED;
546 		ISCSIT_GLOBAL_UNLOCK();
547 	}
548 
549 	return (rc);
550 }
551 
552 static idm_status_t
553 iscsit_init(dev_info_t *dip)
554 {
555 	int			rc;
556 
557 	rc = ldi_ident_from_dip(dip, &iscsit_global.global_li);
558 	ASSERT(rc == 0);  /* Failure indicates invalid argument */
559 
560 	iscsit_global.global_svc_state = ISE_DISABLED;
561 
562 	return (IDM_STATUS_SUCCESS);
563 }
564 
565 /*
566  * iscsit_enable_svc
567  *
568  * registers all the configured targets and target portals with STMF
569  */
570 static idm_status_t
571 iscsit_enable_svc(iscsit_hostinfo_t *hostinfo)
572 {
573 	stmf_port_provider_t	*pp;
574 	stmf_dbuf_store_t	*dbuf_store;
575 	boolean_t		did_iscsit_isns_init;
576 	idm_status_t		retval = IDM_STATUS_SUCCESS;
577 
578 	ASSERT(iscsit_global.global_svc_state == ISE_ENABLING);
579 
580 	/*
581 	 * Make sure that can tell if we have partially allocated
582 	 * in case we need to exit and tear down anything allocated.
583 	 */
584 	iscsit_global.global_tsih_pool = NULL;
585 	iscsit_global.global_dbuf_store = NULL;
586 	iscsit_status_pdu_cache = NULL;
587 	pp = NULL;
588 	iscsit_global.global_pp = NULL;
589 	iscsit_global.global_default_tpg = NULL;
590 	did_iscsit_isns_init = B_FALSE;
591 	iscsit_global.global_dispatch_taskq = NULL;
592 
593 	/* Setup remaining fields in iscsit_global_t */
594 	idm_refcnt_init(&iscsit_global.global_refcnt,
595 	    &iscsit_global);
596 
597 	avl_create(&iscsit_global.global_discovery_sessions,
598 	    iscsit_sess_avl_compare, sizeof (iscsit_sess_t),
599 	    offsetof(iscsit_sess_t, ist_tgt_ln));
600 
601 	avl_create(&iscsit_global.global_target_list,
602 	    iscsit_tgt_avl_compare, sizeof (iscsit_tgt_t),
603 	    offsetof(iscsit_tgt_t, target_global_ln));
604 
605 	list_create(&iscsit_global.global_deleted_target_list,
606 	    sizeof (iscsit_tgt_t),
607 	    offsetof(iscsit_tgt_t, target_global_deleted_ln));
608 
609 	avl_create(&iscsit_global.global_tpg_list,
610 	    iscsit_tpg_avl_compare, sizeof (iscsit_tpg_t),
611 	    offsetof(iscsit_tpg_t, tpg_global_ln));
612 
613 	avl_create(&iscsit_global.global_ini_list,
614 	    iscsit_ini_avl_compare, sizeof (iscsit_ini_t),
615 	    offsetof(iscsit_ini_t, ini_global_ln));
616 
617 	iscsit_global.global_tsih_pool = vmem_create("iscsit_tsih_pool",
618 	    (void *)1, ISCSI_MAX_TSIH, 1, NULL, NULL, NULL, 0,
619 	    VM_SLEEP | VMC_IDENTIFIER);
620 
621 	/*
622 	 * Setup STMF dbuf store.  Our buffers are bound to a specific
623 	 * connection so we really can't let STMF cache buffers for us.
624 	 * Consequently we'll just allocate one global buffer store.
625 	 */
626 	dbuf_store = stmf_alloc(STMF_STRUCT_DBUF_STORE, 0, 0);
627 	if (dbuf_store == NULL) {
628 		retval = IDM_STATUS_FAIL;
629 		goto tear_down_and_return;
630 	}
631 	dbuf_store->ds_alloc_data_buf = iscsit_dbuf_alloc;
632 	dbuf_store->ds_free_data_buf = iscsit_dbuf_free;
633 	dbuf_store->ds_port_private = NULL;
634 	iscsit_global.global_dbuf_store = dbuf_store;
635 
636 	/* Status PDU cache */
637 	iscsit_status_pdu_cache = kmem_cache_create("iscsit_status_pdu_cache",
638 	    sizeof (idm_pdu_t) + sizeof (iscsi_scsi_rsp_hdr_t), 8,
639 	    &iscsit_status_pdu_constructor,
640 	    NULL, NULL, NULL, NULL, KM_SLEEP);
641 
642 	/* Default TPG and portal */
643 	iscsit_global.global_default_tpg = iscsit_tpg_createdefault();
644 	if (iscsit_global.global_default_tpg == NULL) {
645 		retval = IDM_STATUS_FAIL;
646 		goto tear_down_and_return;
647 	}
648 
649 	/* initialize isns client */
650 	(void) iscsit_isns_init(hostinfo);
651 	did_iscsit_isns_init = B_TRUE;
652 
653 	/* Register port provider */
654 	pp = stmf_alloc(STMF_STRUCT_PORT_PROVIDER, 0, 0);
655 	if (pp == NULL) {
656 		retval = IDM_STATUS_FAIL;
657 		goto tear_down_and_return;
658 	}
659 
660 	pp->pp_portif_rev = PORTIF_REV_1;
661 	pp->pp_instance = 0;
662 	pp->pp_name = ISCSIT_MODNAME;
663 	pp->pp_cb = iscsit_pp_cb;
664 
665 	iscsit_global.global_pp = pp;
666 
667 
668 	if (stmf_register_port_provider(pp) != STMF_SUCCESS) {
669 		retval = IDM_STATUS_FAIL;
670 		goto tear_down_and_return;
671 	}
672 
673 	iscsit_global.global_dispatch_taskq = taskq_create("iscsit_dispatch",
674 	    1, minclsyspri, 16, 16, TASKQ_PREPOPULATE);
675 
676 	return (IDM_STATUS_SUCCESS);
677 
678 tear_down_and_return:
679 
680 	if (iscsit_global.global_dispatch_taskq) {
681 		taskq_destroy(iscsit_global.global_dispatch_taskq);
682 		iscsit_global.global_dispatch_taskq = NULL;
683 	}
684 
685 	if (did_iscsit_isns_init)
686 		iscsit_isns_fini();
687 
688 	if (iscsit_global.global_default_tpg) {
689 		iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
690 		iscsit_global.global_default_tpg = NULL;
691 	}
692 
693 	if (iscsit_global.global_pp)
694 		iscsit_global.global_pp = NULL;
695 
696 	if (pp)
697 		stmf_free(pp);
698 
699 	if (iscsit_status_pdu_cache) {
700 		kmem_cache_destroy(iscsit_status_pdu_cache);
701 		iscsit_status_pdu_cache = NULL;
702 	}
703 
704 	if (iscsit_global.global_dbuf_store) {
705 		stmf_free(iscsit_global.global_dbuf_store);
706 		iscsit_global.global_dbuf_store = NULL;
707 	}
708 
709 	if (iscsit_global.global_tsih_pool) {
710 		vmem_destroy(iscsit_global.global_tsih_pool);
711 		iscsit_global.global_tsih_pool = NULL;
712 	}
713 
714 	avl_destroy(&iscsit_global.global_ini_list);
715 	avl_destroy(&iscsit_global.global_tpg_list);
716 	list_destroy(&iscsit_global.global_deleted_target_list);
717 	avl_destroy(&iscsit_global.global_target_list);
718 	avl_destroy(&iscsit_global.global_discovery_sessions);
719 
720 	idm_refcnt_destroy(&iscsit_global.global_refcnt);
721 
722 	return (retval);
723 }
724 
725 /*
726  * iscsit_disable_svc
727  *
728  * clean up all existing connections and deregister targets from STMF
729  */
730 static void
731 iscsit_disable_svc(void)
732 {
733 	iscsit_sess_t	*sess;
734 
735 	ASSERT(iscsit_global.global_svc_state == ISE_DISABLING);
736 
737 	/* tear down discovery sessions */
738 	for (sess = avl_first(&iscsit_global.global_discovery_sessions);
739 	    sess != NULL;
740 	    sess = AVL_NEXT(&iscsit_global.global_discovery_sessions, sess))
741 		iscsit_sess_close(sess);
742 
743 	/*
744 	 * Passing NULL to iscsit_config_merge tells it to go to an empty
745 	 * config.
746 	 */
747 	(void) iscsit_config_merge(NULL);
748 
749 	/*
750 	 * Wait until there are no more global references
751 	 */
752 	idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
753 	idm_refcnt_destroy(&iscsit_global.global_refcnt);
754 
755 	/*
756 	 * Default TPG must be destroyed after global_refcnt is 0.
757 	 */
758 	iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
759 
760 	avl_destroy(&iscsit_global.global_discovery_sessions);
761 	list_destroy(&iscsit_global.global_deleted_target_list);
762 	avl_destroy(&iscsit_global.global_target_list);
763 	avl_destroy(&iscsit_global.global_tpg_list);
764 	avl_destroy(&iscsit_global.global_ini_list);
765 
766 	taskq_destroy(iscsit_global.global_dispatch_taskq);
767 
768 	iscsit_isns_fini();
769 
770 	stmf_free(iscsit_global.global_dbuf_store);
771 	iscsit_global.global_dbuf_store = NULL;
772 
773 	(void) stmf_deregister_port_provider(iscsit_global.global_pp);
774 	stmf_free(iscsit_global.global_pp);
775 	iscsit_global.global_pp = NULL;
776 
777 	kmem_cache_destroy(iscsit_status_pdu_cache);
778 	iscsit_status_pdu_cache = NULL;
779 
780 	vmem_destroy(iscsit_global.global_tsih_pool);
781 	iscsit_global.global_tsih_pool = NULL;
782 }
783 
784 void
785 iscsit_global_hold()
786 {
787 	idm_refcnt_hold(&iscsit_global.global_refcnt);
788 }
789 
790 void
791 iscsit_global_rele()
792 {
793 	idm_refcnt_rele(&iscsit_global.global_refcnt);
794 }
795 
796 void
797 iscsit_global_wait_ref()
798 {
799 	idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
800 }
801 
802 /*
803  * IDM callbacks
804  */
805 
806 /*ARGSUSED*/
807 void
808 iscsit_rx_pdu(idm_conn_t *ic, idm_pdu_t *rx_pdu)
809 {
810 	iscsit_conn_t *ict = ic->ic_handle;
811 	switch (IDM_PDU_OPCODE(rx_pdu)) {
812 	case ISCSI_OP_SCSI_CMD:
813 		ASSERT(0); /* Shouldn't happen */
814 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
815 		break;
816 	case ISCSI_OP_SNACK_CMD:
817 		/*
818 		 * We'll need to handle this when we support ERL1/2.  For
819 		 * now we treat it as a protocol error.
820 		 */
821 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
822 		idm_conn_event(ic, CE_TRANSPORT_FAIL, NULL);
823 		break;
824 	case ISCSI_OP_SCSI_TASK_MGT_MSG:
825 		iscsit_set_cmdsn(ict, rx_pdu);
826 		iscsit_op_scsi_task_mgmt(ict, rx_pdu);
827 		break;
828 	case ISCSI_OP_NOOP_OUT:
829 	case ISCSI_OP_LOGIN_CMD:
830 	case ISCSI_OP_TEXT_CMD:
831 	case ISCSI_OP_LOGOUT_CMD:
832 		/*
833 		 * If/when we switch to userland processing these PDU's
834 		 * will be handled by iscsitd.
835 		 */
836 		iscsit_deferred_dispatch(rx_pdu);
837 		break;
838 	default:
839 		/* Protocol error */
840 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
841 		idm_conn_event(ic, CE_TRANSPORT_FAIL, NULL);
842 		break;
843 	}
844 }
845 
846 /*ARGSUSED*/
847 void
848 iscsit_rx_pdu_error(idm_conn_t *ic, idm_pdu_t *rx_pdu, idm_status_t status)
849 {
850 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
851 }
852 
853 void
854 iscsit_task_aborted(idm_task_t *idt, idm_status_t status)
855 {
856 	iscsit_task_t *itask = idt->idt_private;
857 
858 	switch (status) {
859 	case IDM_STATUS_SUSPENDED:
860 		break;
861 	case IDM_STATUS_ABORTED:
862 		mutex_enter(&itask->it_mutex);
863 		itask->it_aborted = B_TRUE;
864 		/*
865 		 * We rely on the fact that STMF tracks outstanding
866 		 * buffer transfers and will free all of our buffers
867 		 * before freeing the task so we don't need to
868 		 * explicitly free the buffers from iscsit/idm
869 		 */
870 		if (itask->it_stmf_abort) {
871 			mutex_exit(&itask->it_mutex);
872 			/*
873 			 * Task is no longer active
874 			 */
875 			iscsit_task_done(itask);
876 
877 			/*
878 			 * STMF has already asked for this task to be aborted
879 			 *
880 			 * STMF specification is wrong... says to return
881 			 * STMF_ABORTED, the code actually looks for
882 			 * STMF_ABORT_SUCCESS.
883 			 */
884 			stmf_task_lport_aborted(itask->it_stmf_task,
885 			    STMF_ABORT_SUCCESS, STMF_IOF_LPORT_DONE);
886 			return;
887 		} else {
888 			mutex_exit(&itask->it_mutex);
889 			/*
890 			 * Tell STMF to stop processing the task.
891 			 */
892 			stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
893 			    STMF_ABORTED, NULL);
894 			return;
895 		}
896 		/*NOTREACHED*/
897 	default:
898 		ASSERT(0);
899 	}
900 }
901 
902 /*ARGSUSED*/
903 idm_status_t
904 iscsit_client_notify(idm_conn_t *ic, idm_client_notify_t icn,
905     uintptr_t data)
906 {
907 	idm_status_t rc = IDM_STATUS_SUCCESS;
908 
909 	/*
910 	 * IDM client notifications will never occur at interrupt level
911 	 * since they are generated from the connection state machine which
912 	 * running on taskq threads.
913 	 *
914 	 */
915 	switch (icn) {
916 	case CN_CONNECT_ACCEPT:
917 		rc = iscsit_conn_accept(ic); /* No data */
918 		break;
919 	case CN_FFP_ENABLED:
920 		rc = iscsit_ffp_enabled(ic); /* No data */
921 		break;
922 	case CN_FFP_DISABLED:
923 		/*
924 		 * Data indicates whether this was the result of an
925 		 * explicit logout request.
926 		 */
927 		rc = iscsit_ffp_disabled(ic, (idm_ffp_disable_t)data);
928 		break;
929 	case CN_CONNECT_LOST:
930 		rc = iscsit_conn_lost(ic);
931 		break;
932 	case CN_CONNECT_DESTROY:
933 		rc = iscsit_conn_destroy(ic);
934 		break;
935 	case CN_LOGIN_FAIL:
936 		/*
937 		 * Force the login state machine to completion
938 		 */
939 		rc = iscsit_login_fail(ic);
940 		break;
941 	default:
942 		rc = IDM_STATUS_REJECT;
943 		break;
944 	}
945 
946 	return (rc);
947 }
948 
949 /*
950  * iscsit_update_statsn is invoked for all the PDUs which have the StatSN
951  * field in the header. The StatSN is incremented if the IDM_PDU_ADVANCE_STATSN
952  * flag is set in the pdu flags field. The StatSN is connection-wide and is
953  * protected by the mutex ict_statsn_mutex. For Data-In PDUs, if the flag
954  * IDM_TASK_PHASECOLLAPSE_REQ is set, the status (phase-collapse) is also filled
955  */
956 void
957 iscsit_update_statsn(idm_task_t *idm_task, idm_pdu_t *pdu)
958 {
959 	iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
960 	iscsit_conn_t *ict = (iscsit_conn_t *)pdu->isp_ic->ic_handle;
961 	iscsit_task_t *itask = NULL;
962 	scsi_task_t *task = NULL;
963 
964 	mutex_enter(&ict->ict_statsn_mutex);
965 	rsp->statsn = htonl(ict->ict_statsn);
966 	if (pdu->isp_flags & IDM_PDU_ADVANCE_STATSN)
967 		ict->ict_statsn++;
968 	mutex_exit(&ict->ict_statsn_mutex);
969 
970 	/*
971 	 * The last SCSI Data PDU passed for a command may also contain the
972 	 * status if the status indicates termination with no expections, i.e.
973 	 * no sense data or response involved. If the command completes with
974 	 * an error, then the response and sense data will be sent in a
975 	 * separate iSCSI Response PDU.
976 	 */
977 	if ((idm_task) && (idm_task->idt_flags & IDM_TASK_PHASECOLLAPSE_REQ)) {
978 		itask = idm_task->idt_private;
979 		task = itask->it_stmf_task;
980 
981 		rsp->cmd_status = task->task_scsi_status;
982 		rsp->flags	|= ISCSI_FLAG_DATA_STATUS;
983 		if (task->task_status_ctrl & TASK_SCTRL_OVER) {
984 			rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
985 		} else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
986 			rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
987 		}
988 		rsp->residual_count = htonl(task->task_resid);
989 	}
990 }
991 
992 void
993 iscsit_build_hdr(idm_task_t *idm_task, idm_pdu_t *pdu, uint8_t opcode)
994 {
995 	iscsit_task_t *itask = idm_task->idt_private;
996 	iscsi_data_rsp_hdr_t *dh = (iscsi_data_rsp_hdr_t *)pdu->isp_hdr;
997 
998 	/*
999 	 * We acquired iscsit_sess_t.ist_sn_rwlock in iscsit_xfer_scsi_data
1000 	 * in reader mode so we expect to be locked here
1001 	 */
1002 
1003 	/*
1004 	 * Lun is only required if the opcode == ISCSI_OP_SCSI_DATA_RSP
1005 	 * and the 'A' bit is to be set
1006 	 */
1007 	dh->opcode = opcode;
1008 	dh->itt = itask->it_itt;
1009 	dh->ttt = itask->it_ttt;
1010 	/* Maintain current statsn for RTT responses */
1011 	dh->statsn = (opcode == ISCSI_OP_RTT_RSP) ?
1012 	    htonl(itask->it_ict->ict_statsn) : 0;
1013 
1014 	dh->expcmdsn = htonl(itask->it_ict->ict_sess->ist_expcmdsn);
1015 	dh->maxcmdsn = htonl(itask->it_ict->ict_sess->ist_maxcmdsn);
1016 
1017 	/*
1018 	 * IDM must set:
1019 	 *
1020 	 * data.flags and rtt.flags
1021 	 * data.dlength
1022 	 * data.datasn
1023 	 * data.offset
1024 	 * statsn, residual_count and cmd_status (for phase collapse)
1025 	 * rtt.rttsn
1026 	 * rtt.data_offset
1027 	 * rtt.data_length
1028 	 */
1029 }
1030 
1031 void
1032 iscsit_keepalive(idm_conn_t *ic)
1033 {
1034 	idm_pdu_t		*nop_in_pdu;
1035 	iscsi_nop_in_hdr_t	*nop_in;
1036 	iscsit_conn_t		*ict = ic->ic_handle;
1037 
1038 	/*
1039 	 * IDM noticed the connection has been idle for too long so it's
1040 	 * time to provoke some activity.  Build and transmit an iSCSI
1041 	 * nop-in PDU -- when the initiator responds it will be counted
1042 	 * as "activity" and keep the connection alive.
1043 	 *
1044 	 * We don't actually care about the response here at the iscsit level
1045 	 * so we will just throw it away without looking at it when it arrives.
1046 	 */
1047 	nop_in_pdu = idm_pdu_alloc(sizeof (*nop_in), 0);
1048 	idm_pdu_init(nop_in_pdu, ic, NULL, NULL);
1049 	nop_in = (iscsi_nop_in_hdr_t *)nop_in_pdu->isp_hdr;
1050 	bzero(nop_in, sizeof (*nop_in));
1051 	nop_in->opcode = ISCSI_OP_NOOP_IN;
1052 	nop_in->flags = ISCSI_FLAG_FINAL;
1053 	nop_in->itt = ISCSI_RSVD_TASK_TAG;
1054 	/*
1055 	 * When the target sends a NOP-In as a Ping, the target transfer tag
1056 	 * is set to a valid (not reserved) value and the initiator task tag
1057 	 * is set to ISCSI_RSVD_TASK_TAG (0xffffffff). In this case the StatSN
1058 	 * will always contain the next sequence number but the StatSN for the
1059 	 * connection is not advanced after this PDU is sent.
1060 	 */
1061 	nop_in_pdu->isp_flags |= IDM_PDU_SET_STATSN;
1062 	/*
1063 	 * This works because we don't currently allocate ttt's anywhere else
1064 	 * in iscsit so as long as we stay out of IDM's range we are safe.
1065 	 * If we need to allocate ttt's for other PDU's in the future this will
1066 	 * need to be improved.
1067 	 */
1068 	mutex_enter(&ict->ict_mutex);
1069 	nop_in->ttt = ict->ict_keepalive_ttt;
1070 	ict->ict_keepalive_ttt++;
1071 	if (ict->ict_keepalive_ttt == ISCSI_RSVD_TASK_TAG)
1072 		ict->ict_keepalive_ttt = IDM_TASKIDS_MAX;
1073 	mutex_exit(&ict->ict_mutex);
1074 
1075 	iscsit_pdu_tx(nop_in_pdu);
1076 }
1077 
1078 static idm_status_t
1079 iscsit_conn_accept(idm_conn_t *ic)
1080 {
1081 	iscsit_conn_t *ict;
1082 
1083 	/*
1084 	 * We need to get a global hold here to ensure that the service
1085 	 * doesn't get shutdown prior to establishing a session. This
1086 	 * gets released in iscsit_conn_destroy().
1087 	 */
1088 	ISCSIT_GLOBAL_LOCK(RW_READER);
1089 	if (iscsit_global.global_svc_state != ISE_ENABLED) {
1090 		ISCSIT_GLOBAL_UNLOCK();
1091 		return (IDM_STATUS_FAIL);
1092 	}
1093 	iscsit_global_hold();
1094 	ISCSIT_GLOBAL_UNLOCK();
1095 
1096 	/*
1097 	 * Allocate an associated iscsit structure to represent this
1098 	 * connection.  We shouldn't really create a session until we
1099 	 * get the first login PDU.
1100 	 */
1101 	ict = kmem_zalloc(sizeof (*ict), KM_SLEEP);
1102 
1103 	ict->ict_ic = ic;
1104 	ict->ict_statsn = 1;
1105 	ict->ict_keepalive_ttt = IDM_TASKIDS_MAX; /* Avoid IDM TT range */
1106 	ic->ic_handle = ict;
1107 	mutex_init(&ict->ict_mutex, NULL, MUTEX_DRIVER, NULL);
1108 	mutex_init(&ict->ict_statsn_mutex, NULL, MUTEX_DRIVER, NULL);
1109 	idm_refcnt_init(&ict->ict_refcnt, ict);
1110 
1111 	/*
1112 	 * Initialize login state machine
1113 	 */
1114 	if (iscsit_login_sm_init(ict) != IDM_STATUS_SUCCESS) {
1115 		iscsit_global_rele();
1116 		/*
1117 		 * Cleanup the ict after idm notifies us about this failure
1118 		 */
1119 		return (IDM_STATUS_FAIL);
1120 	}
1121 
1122 	return (IDM_STATUS_SUCCESS);
1123 }
1124 
1125 idm_status_t
1126 iscsit_conn_reinstate(iscsit_conn_t *reinstate_ict, iscsit_conn_t *new_ict)
1127 {
1128 	idm_status_t	result;
1129 
1130 	/*
1131 	 * Note in new connection state that this connection is
1132 	 * reinstating an existing connection.
1133 	 */
1134 	new_ict->ict_reinstating = B_TRUE;
1135 	new_ict->ict_reinstate_conn = reinstate_ict;
1136 	new_ict->ict_statsn = reinstate_ict->ict_statsn;
1137 
1138 	/*
1139 	 * Now generate connection state machine event to existing connection
1140 	 * so that it starts the cleanup process.
1141 	 */
1142 	result = idm_conn_reinstate_event(reinstate_ict->ict_ic,
1143 	    new_ict->ict_ic);
1144 
1145 	return (result);
1146 }
1147 
1148 void
1149 iscsit_conn_hold(iscsit_conn_t *ict)
1150 {
1151 	idm_refcnt_hold(&ict->ict_refcnt);
1152 }
1153 
1154 void
1155 iscsit_conn_rele(iscsit_conn_t *ict)
1156 {
1157 	idm_refcnt_rele(&ict->ict_refcnt);
1158 }
1159 
1160 void
1161 iscsit_conn_dispatch_hold(iscsit_conn_t *ict)
1162 {
1163 	idm_refcnt_hold(&ict->ict_dispatch_refcnt);
1164 }
1165 
1166 void
1167 iscsit_conn_dispatch_rele(iscsit_conn_t *ict)
1168 {
1169 	idm_refcnt_rele(&ict->ict_dispatch_refcnt);
1170 }
1171 
1172 static idm_status_t
1173 iscsit_login_fail(idm_conn_t *ic)
1174 {
1175 	iscsit_conn_t *ict = ic->ic_handle;
1176 
1177 	/* Generate login state machine event */
1178 	iscsit_login_sm_event(ict, ILE_LOGIN_CONN_ERROR, NULL);
1179 
1180 	return (IDM_STATUS_SUCCESS);
1181 }
1182 
1183 static idm_status_t
1184 iscsit_ffp_enabled(idm_conn_t *ic)
1185 {
1186 	iscsit_conn_t *ict = ic->ic_handle;
1187 
1188 	/* Generate session state machine event */
1189 	iscsit_sess_sm_event(ict->ict_sess, SE_CONN_LOGGED_IN, ict);
1190 
1191 	return (IDM_STATUS_SUCCESS);
1192 }
1193 
1194 static idm_status_t
1195 iscsit_ffp_disabled(idm_conn_t *ic, idm_ffp_disable_t disable_class)
1196 {
1197 	iscsit_conn_t *ict = ic->ic_handle;
1198 
1199 	/* Generate session state machine event */
1200 	switch (disable_class) {
1201 	case FD_CONN_FAIL:
1202 		iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_FAIL, ict);
1203 		break;
1204 	case FD_CONN_LOGOUT:
1205 		iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_DISABLE, ict);
1206 		break;
1207 	case FD_SESS_LOGOUT:
1208 		iscsit_sess_sm_event(ict->ict_sess, SE_SESSION_CLOSE, ict);
1209 		break;
1210 	default:
1211 		ASSERT(0);
1212 	}
1213 
1214 	return (IDM_STATUS_SUCCESS);
1215 }
1216 
1217 static idm_status_t
1218 iscsit_conn_lost(idm_conn_t *ic)
1219 {
1220 	iscsit_conn_t *ict = ic->ic_handle;
1221 
1222 	mutex_enter(&ict->ict_mutex);
1223 	ict->ict_lost = B_TRUE;
1224 	mutex_exit(&ict->ict_mutex);
1225 
1226 	/*
1227 	 * Make sure there aren't any PDU's transitioning from the receive
1228 	 * handler to the dispatch taskq.
1229 	 */
1230 	idm_refcnt_wait_ref(&ict->ict_dispatch_refcnt);
1231 
1232 	return (IDM_STATUS_SUCCESS);
1233 }
1234 
1235 static idm_status_t
1236 iscsit_conn_destroy(idm_conn_t *ic)
1237 {
1238 	iscsit_conn_t *ict = ic->ic_handle;
1239 
1240 	mutex_enter(&ict->ict_mutex);
1241 	ict->ict_destroyed = B_TRUE;
1242 	mutex_exit(&ict->ict_mutex);
1243 
1244 	/* Generate session state machine event */
1245 	if (ict->ict_sess != NULL) {
1246 		/*
1247 		 * Session state machine will call iscsit_conn_destroy_done()
1248 		 * when it has removed references to this connection.
1249 		 */
1250 		iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FAIL, ict);
1251 	}
1252 
1253 	ict->ict_ic = NULL;
1254 
1255 	idm_refcnt_wait_ref(&ict->ict_refcnt);
1256 
1257 	/* Reap the login state machine */
1258 	iscsit_login_sm_fini(ict);
1259 
1260 	/* Clean up any text command remnants */
1261 	iscsit_text_cmd_fini(ict);
1262 
1263 	mutex_destroy(&ict->ict_mutex);
1264 	idm_refcnt_destroy(&ict->ict_refcnt);
1265 	kmem_free(ict, sizeof (*ict));
1266 
1267 	iscsit_global_rele();
1268 
1269 	return (IDM_STATUS_SUCCESS);
1270 }
1271 
1272 /*
1273  * STMF-related functions
1274  *
1275  * iSCSI to STMF mapping
1276  *
1277  * Session == ?
1278  * Connection == bound to local port but not itself a local port
1279  * Target
1280  * Target portal (group?) == local port (really but we're not going to do this)
1281  *	iscsit needs to map connections to local ports (whatever we decide
1282  * 	they are)
1283  * Target == ?
1284  */
1285 
1286 /*ARGSUSED*/
1287 static stmf_data_buf_t *
1288 iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
1289     uint32_t flags)
1290 {
1291 	iscsit_task_t *itask = task->task_port_private;
1292 	idm_buf_t *idm_buffer;
1293 	iscsit_buf_t	*ibuf;
1294 	stmf_data_buf_t *result;
1295 	uint32_t	bsize;
1296 
1297 	/*
1298 	 * If the requested size is larger than MaxBurstLength and the
1299 	 * given pminsize is also larger than MaxBurstLength, then the
1300 	 * allocation fails (dbuf = NULL) and pminsize is modified to
1301 	 * be equal to MaxBurstLength. stmf/sbd then should re-invoke
1302 	 * this function with the corrected values for transfer.
1303 	 */
1304 	ASSERT(pminsize);
1305 	if (size <= itask->it_ict->ict_op.op_max_burst_length) {
1306 		bsize = size;
1307 	} else if (*pminsize <= itask->it_ict->ict_op.op_max_burst_length) {
1308 		bsize = itask->it_ict->ict_op.op_max_burst_length;
1309 	} else {
1310 		*pminsize = itask->it_ict->ict_op.op_max_burst_length;
1311 		return (NULL);
1312 	}
1313 
1314 	/* Alloc buffer */
1315 	idm_buffer = idm_buf_alloc(itask->it_ict->ict_ic, NULL, bsize);
1316 	if (idm_buffer != NULL) {
1317 		result = stmf_alloc(STMF_STRUCT_DATA_BUF,
1318 		    sizeof (iscsit_buf_t), 0);
1319 		if (result != NULL) {
1320 			/* Fill in stmf_data_buf_t */
1321 			ibuf = result->db_port_private;
1322 			ibuf->ibuf_idm_buf = idm_buffer;
1323 			ibuf->ibuf_stmf_buf = result;
1324 			ibuf->ibuf_is_immed = B_FALSE;
1325 			result->db_flags = DB_DONT_CACHE;
1326 			result->db_buf_size = bsize;
1327 			result->db_data_size = bsize;
1328 			result->db_sglist_length = 1;
1329 			result->db_sglist[0].seg_addr = idm_buffer->idb_buf;
1330 			result->db_sglist[0].seg_length =
1331 			    idm_buffer->idb_buflen;
1332 			return (result);
1333 		}
1334 
1335 		/* Couldn't get the stmf_data_buf_t so free the buffer */
1336 		idm_buf_free(idm_buffer);
1337 	}
1338 
1339 	return (NULL);
1340 }
1341 
1342 /*ARGSUSED*/
1343 static void
1344 iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf)
1345 {
1346 	iscsit_buf_t *ibuf = dbuf->db_port_private;
1347 
1348 	if (ibuf->ibuf_is_immed) {
1349 		/*
1350 		 * The iscsit_buf_t structure itself will be freed with its
1351 		 * associated task.  Here we just need to free the PDU that
1352 		 * held the immediate data.
1353 		 */
1354 		idm_pdu_complete(ibuf->ibuf_immed_data_pdu, IDM_STATUS_SUCCESS);
1355 		ibuf->ibuf_immed_data_pdu = 0;
1356 	} else {
1357 		idm_buf_free(ibuf->ibuf_idm_buf);
1358 		stmf_free(dbuf);
1359 	}
1360 }
1361 
1362 /*ARGSUSED*/
1363 stmf_status_t
1364 iscsit_xfer_scsi_data(scsi_task_t *task, stmf_data_buf_t *dbuf,
1365     uint32_t ioflags)
1366 {
1367 	iscsit_task_t *iscsit_task = task->task_port_private;
1368 	iscsit_sess_t *ict_sess = iscsit_task->it_ict->ict_sess;
1369 	iscsit_buf_t *ibuf = dbuf->db_port_private;
1370 	int idm_rc;
1371 
1372 	/*
1373 	 * If we are aborting then we can ignore this request
1374 	 */
1375 	if (iscsit_task->it_stmf_abort) {
1376 		return (STMF_SUCCESS);
1377 	}
1378 
1379 	/*
1380 	 * If it's not immediate data then start the transfer
1381 	 */
1382 	ASSERT(ibuf->ibuf_is_immed == B_FALSE);
1383 	if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
1384 		/*
1385 		 * The DB_SEND_STATUS_GOOD flag in the STMF data buffer allows
1386 		 * the port provider to phase-collapse, i.e. send the status
1387 		 * along with the final data PDU for the command. The port
1388 		 * provider passes this request to the transport layer by
1389 		 * setting a flag IDM_TASK_PHASECOLLAPSE_REQ in the task.
1390 		 */
1391 		if (dbuf->db_flags & DB_SEND_STATUS_GOOD)
1392 			iscsit_task->it_idm_task->idt_flags |=
1393 			    IDM_TASK_PHASECOLLAPSE_REQ;
1394 		/*
1395 		 * IDM will call iscsit_build_hdr so lock now to serialize
1396 		 * access to the SN values.  We need to lock here to enforce
1397 		 * lock ordering
1398 		 */
1399 		rw_enter(&ict_sess->ist_sn_rwlock, RW_READER);
1400 		idm_rc = idm_buf_tx_to_ini(iscsit_task->it_idm_task,
1401 		    ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1402 		    dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1403 		rw_exit(&ict_sess->ist_sn_rwlock);
1404 
1405 		return (iscsit_idm_to_stmf(idm_rc));
1406 	} else if (dbuf->db_flags & DB_DIRECTION_FROM_RPORT) {
1407 		/* Grab the SN lock (see comment above) */
1408 		rw_enter(&ict_sess->ist_sn_rwlock, RW_READER);
1409 		idm_rc = idm_buf_rx_from_ini(iscsit_task->it_idm_task,
1410 		    ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1411 		    dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1412 		rw_exit(&ict_sess->ist_sn_rwlock);
1413 
1414 		return (iscsit_idm_to_stmf(idm_rc));
1415 	}
1416 
1417 	/* What are we supposed to do if there is no direction? */
1418 	return (STMF_INVALID_ARG);
1419 }
1420 
1421 static void
1422 iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status)
1423 {
1424 	iscsit_task_t *itask = idb->idb_task_binding->idt_private;
1425 	stmf_data_buf_t *dbuf = idb->idb_cb_arg;
1426 
1427 	dbuf->db_xfer_status = iscsit_idm_to_stmf(status);
1428 
1429 	/*
1430 	 * If the task has been aborted then we don't need to call STMF
1431 	 */
1432 	if (itask->it_stmf_abort) {
1433 		return;
1434 	}
1435 
1436 	/*
1437 	 * For ISCSI over TCP (not iSER), the last SCSI Data PDU passed
1438 	 * for a successful command contains the status as requested by
1439 	 * by COMSTAR (via the DB_SEND_STATUS_GOOD flag). But the iSER
1440 	 * transport does not support phase-collapse. So pretend we are
1441 	 * COMSTAR and send the status in a separate PDU now.
1442 	 */
1443 	if (idb->idb_task_binding->idt_flags & IDM_TASK_PHASECOLLAPSE_SUCCESS) {
1444 		/*
1445 		 * Mark task complete, remove task from the session task
1446 		 * list and notify COMSTAR that the status has been sent.
1447 		 */
1448 		iscsit_task_done(itask);
1449 		itask->it_idm_task->idt_state = TASK_COMPLETE;
1450 		stmf_send_status_done(itask->it_stmf_task,
1451 		    iscsit_idm_to_stmf(status), STMF_IOF_LPORT_DONE);
1452 	} else if ((dbuf->db_flags & DB_SEND_STATUS_GOOD) &&
1453 	    status == IDM_STATUS_SUCCESS) {
1454 
1455 		/*
1456 		 * The iscsi target port provider - for iSER, emulates the
1457 		 * DB_SEND_STATUS_GOOD optimization if requested by STMF;
1458 		 * it sends the status in a separate PDU after the data
1459 		 * transfer. In this case the port provider should first
1460 		 * call stmf_data_xfer_done() to mark the transfer complete
1461 		 * and then send the status. Although STMF will free the
1462 		 * buffer at the time the task is freed, even if the transfer
1463 		 * is not marked complete, this behavior makes statistics
1464 		 * gathering and task state tracking more difficult than it
1465 		 * needs to be.
1466 		 */
1467 		stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
1468 		if (iscsit_send_scsi_status(itask->it_stmf_task, 0)
1469 		    != STMF_SUCCESS) {
1470 			stmf_send_status_done(itask->it_stmf_task,
1471 			    STMF_FAILURE, STMF_IOF_LPORT_DONE);
1472 		}
1473 	} else {
1474 		stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
1475 		/* don't touch dbuf after stmf_data_xfer_done */
1476 	}
1477 }
1478 
1479 
1480 /*ARGSUSED*/
1481 stmf_status_t
1482 iscsit_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
1483 {
1484 	iscsit_task_t *itask = task->task_port_private;
1485 	iscsi_scsi_rsp_hdr_t *rsp;
1486 	idm_pdu_t *pdu;
1487 	int resp_datalen;
1488 
1489 	/*
1490 	 * If this task is aborted then we don't need to respond.
1491 	 */
1492 	if (itask->it_stmf_abort) {
1493 		return (STMF_SUCCESS);
1494 	}
1495 
1496 	/*
1497 	 * If this is a task management status, handle it elsewhere.
1498 	 */
1499 	if (task->task_mgmt_function != TM_NONE) {
1500 		/*
1501 		 * Don't wait for the PDU completion to tell STMF
1502 		 * the task is done -- it doesn't really matter and
1503 		 * it makes life complicated if STMF later asks us to
1504 		 * abort the request and we don't know whether the
1505 		 * status has been sent or not.
1506 		 */
1507 		itask->it_tm_responded = B_TRUE;
1508 		iscsit_send_task_mgmt_resp(itask->it_tm_pdu,
1509 		    (task->task_completion_status == STMF_SUCCESS) ?
1510 		    SCSI_TCP_TM_RESP_COMPLETE : SCSI_TCP_TM_RESP_FUNC_NOT_SUPP);
1511 		stmf_send_status_done(task, STMF_SUCCESS,
1512 		    STMF_IOF_LPORT_DONE);
1513 		return (STMF_SUCCESS);
1514 	}
1515 
1516 	/*
1517 	 * Remove the task from the session task list
1518 	 */
1519 	iscsit_task_done(itask);
1520 
1521 	/*
1522 	 * Send status
1523 	 */
1524 	mutex_enter(&itask->it_idm_task->idt_mutex);
1525 	if ((itask->it_idm_task->idt_state == TASK_ACTIVE) &&
1526 	    (task->task_completion_status == STMF_SUCCESS) &&
1527 	    (task->task_sense_length == 0) &&
1528 	    (task->task_resid == 0)) {
1529 		itask->it_idm_task->idt_state = TASK_COMPLETE;
1530 		/* PDU callback releases task hold */
1531 		idm_task_hold(itask->it_idm_task);
1532 		mutex_exit(&itask->it_idm_task->idt_mutex);
1533 		/*
1534 		 * Fast path.  Cached status PDU's are already
1535 		 * initialized.  We just need to fill in
1536 		 * connection and task information. StatSN is
1537 		 * incremented by 1 for every status sent a
1538 		 * connection.
1539 		 */
1540 		pdu = kmem_cache_alloc(iscsit_status_pdu_cache, KM_SLEEP);
1541 		pdu->isp_ic = itask->it_ict->ict_ic;
1542 		pdu->isp_private = itask;
1543 		pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1544 
1545 		rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1546 		rsp->itt = itask->it_itt;
1547 		/*
1548 		 * ExpDataSN is the number of R2T and Data-In (read)
1549 		 * PDUs the target has sent for the SCSI command.
1550 		 *
1551 		 * Since there is no support for bidirectional transfer
1552 		 * yet, either idt_exp_datasn or idt_exp_rttsn, but not
1553 		 * both is valid at any time
1554 		 */
1555 		rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
1556 		    htonl(itask->it_idm_task->idt_exp_datasn):
1557 		    htonl(itask->it_idm_task->idt_exp_rttsn);
1558 		rsp->cmd_status = task->task_scsi_status;
1559 		iscsit_pdu_tx(pdu);
1560 		return (STMF_SUCCESS);
1561 	} else {
1562 		if (itask->it_idm_task->idt_state != TASK_ACTIVE) {
1563 			mutex_exit(&itask->it_idm_task->idt_mutex);
1564 			return (STMF_FAILURE);
1565 		}
1566 		itask->it_idm_task->idt_state = TASK_COMPLETE;
1567 		/* PDU callback releases task hold */
1568 		idm_task_hold(itask->it_idm_task);
1569 		mutex_exit(&itask->it_idm_task->idt_mutex);
1570 
1571 		resp_datalen = (task->task_sense_length == 0) ? 0 :
1572 		    (task->task_sense_length + sizeof (uint16_t));
1573 
1574 		pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
1575 		idm_pdu_init(pdu, itask->it_ict->ict_ic, itask,
1576 		    iscsit_send_status_done);
1577 		pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1578 
1579 		rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1580 		bzero(rsp, sizeof (*rsp));
1581 		rsp->opcode = ISCSI_OP_SCSI_RSP;
1582 
1583 		rsp->flags = ISCSI_FLAG_FINAL;
1584 		if (task->task_status_ctrl & TASK_SCTRL_OVER) {
1585 			rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
1586 		} else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
1587 			rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
1588 		}
1589 
1590 		rsp->bi_residual_count = 0;
1591 		rsp->residual_count = htonl(task->task_resid);
1592 		rsp->itt = itask->it_itt;
1593 		rsp->response = ISCSI_STATUS_CMD_COMPLETED;
1594 		rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
1595 		    htonl(itask->it_idm_task->idt_exp_datasn):
1596 		    htonl(itask->it_idm_task->idt_exp_rttsn);
1597 		rsp->cmd_status = task->task_scsi_status;
1598 		if (task->task_sense_length != 0) {
1599 			/*
1600 			 * Add a byte to provide the sense length in
1601 			 * the response
1602 			 */
1603 			*(uint16_t *)((void *)pdu->isp_data) =
1604 			    htons(task->task_sense_length);
1605 			bcopy(task->task_sense_data,
1606 			    (uint8_t *)pdu->isp_data +
1607 			    sizeof (uint16_t),
1608 			    task->task_sense_length);
1609 			hton24(rsp->dlength, resp_datalen);
1610 		}
1611 
1612 		DTRACE_PROBE5(iscsi__scsi__response,
1613 		    iscsit_conn_t *, itask->it_ict,
1614 		    uint8_t, rsp->response,
1615 		    uint8_t, rsp->cmd_status,
1616 		    idm_pdu_t *, pdu,
1617 		    scsi_task_t *, task);
1618 
1619 		iscsit_pdu_tx(pdu);
1620 
1621 		return (STMF_SUCCESS);
1622 	}
1623 }
1624 
1625 /*ARGSUSED*/
1626 static void
1627 iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status)
1628 {
1629 	iscsit_task_t	*itask;
1630 	boolean_t	aborted;
1631 
1632 	itask = pdu->isp_private;
1633 	aborted = itask->it_stmf_abort;
1634 
1635 	/*
1636 	 * After releasing the hold the task may be freed at any time so
1637 	 * don't touch it.
1638 	 */
1639 	idm_task_rele(itask->it_idm_task);
1640 	if (!aborted) {
1641 		stmf_send_status_done(itask->it_stmf_task,
1642 		    iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1643 	}
1644 	kmem_cache_free(iscsit_status_pdu_cache, pdu);
1645 }
1646 
1647 /*ARGSUSED*/
1648 static void
1649 iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status)
1650 {
1651 	iscsit_task_t	 *itask;
1652 	boolean_t	aborted;
1653 
1654 	itask = pdu->isp_private;
1655 	aborted = itask->it_stmf_abort;
1656 
1657 	/*
1658 	 * After releasing the hold the task may be freed at any time so
1659 	 * don't touch it.
1660 	 */
1661 	idm_task_rele(itask->it_idm_task);
1662 	if (!aborted) {
1663 		stmf_send_status_done(itask->it_stmf_task,
1664 		    iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1665 	}
1666 	idm_pdu_free(pdu);
1667 }
1668 
1669 
1670 void
1671 iscsit_lport_task_free(scsi_task_t *task)
1672 {
1673 	iscsit_task_t *itask = task->task_port_private;
1674 
1675 	/* We only call idm_task_start for regular tasks, not task management */
1676 	if (task->task_mgmt_function == TM_NONE) {
1677 		idm_task_done(itask->it_idm_task);
1678 		iscsit_task_free(itask);
1679 		return;
1680 	} else {
1681 		iscsit_tm_task_free(itask);
1682 	}
1683 }
1684 
1685 /*ARGSUSED*/
1686 stmf_status_t
1687 iscsit_abort(stmf_local_port_t *lport, int abort_cmd, void *arg, uint32_t flags)
1688 {
1689 	scsi_task_t	*st = (scsi_task_t *)arg;
1690 	iscsit_task_t	*iscsit_task;
1691 	idm_task_t	*idt;
1692 
1693 	/*
1694 	 * If this is a task management request then there's really not much to
1695 	 * do.
1696 	 */
1697 	if (st->task_mgmt_function != TM_NONE) {
1698 		return (STMF_ABORT_SUCCESS);
1699 	}
1700 
1701 	/*
1702 	 * Regular task, start cleaning up
1703 	 */
1704 	iscsit_task = st->task_port_private;
1705 	idt = iscsit_task->it_idm_task;
1706 	mutex_enter(&iscsit_task->it_mutex);
1707 	iscsit_task->it_stmf_abort = B_TRUE;
1708 	if (iscsit_task->it_aborted) {
1709 		mutex_exit(&iscsit_task->it_mutex);
1710 		/*
1711 		 * Task is no longer active
1712 		 */
1713 		iscsit_task_done(iscsit_task);
1714 
1715 		/*
1716 		 * STMF specification is wrong... says to return
1717 		 * STMF_ABORTED, the code actually looks for
1718 		 * STMF_ABORT_SUCCESS.
1719 		 */
1720 		return (STMF_ABORT_SUCCESS);
1721 	} else {
1722 		mutex_exit(&iscsit_task->it_mutex);
1723 		/*
1724 		 * Call IDM to abort the task.  Due to a variety of
1725 		 * circumstances the task may already be in the process of
1726 		 * aborting.
1727 		 * We'll let IDM worry about rationalizing all that except
1728 		 * for one particular instance.  If the state of the task
1729 		 * is TASK_COMPLETE, we need to indicate to the framework
1730 		 * that we are in fact done.  This typically happens with
1731 		 * framework-initiated task management type requests
1732 		 * (e.g. abort task).
1733 		 */
1734 		if (idt->idt_state == TASK_COMPLETE) {
1735 			idm_refcnt_wait_ref(&idt->idt_refcnt);
1736 			return (STMF_ABORT_SUCCESS);
1737 		} else {
1738 			idm_task_abort(idt->idt_ic, idt, AT_TASK_MGMT_ABORT);
1739 			return (STMF_SUCCESS);
1740 		}
1741 	}
1742 
1743 	/*NOTREACHED*/
1744 }
1745 
1746 /*ARGSUSED*/
1747 void
1748 iscsit_ctl(stmf_local_port_t *lport, int cmd, void *arg)
1749 {
1750 	iscsit_tgt_t		*iscsit_tgt;
1751 
1752 	ASSERT((cmd == STMF_CMD_LPORT_ONLINE) ||
1753 	    (cmd == STMF_ACK_LPORT_ONLINE_COMPLETE) ||
1754 	    (cmd == STMF_CMD_LPORT_OFFLINE) ||
1755 	    (cmd == STMF_ACK_LPORT_OFFLINE_COMPLETE));
1756 
1757 	iscsit_tgt = (iscsit_tgt_t *)lport->lport_port_private;
1758 
1759 	switch (cmd) {
1760 	case STMF_CMD_LPORT_ONLINE:
1761 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_REQ);
1762 		break;
1763 	case STMF_CMD_LPORT_OFFLINE:
1764 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_REQ);
1765 		break;
1766 	case STMF_ACK_LPORT_ONLINE_COMPLETE:
1767 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_COMPLETE_ACK);
1768 		break;
1769 	case STMF_ACK_LPORT_OFFLINE_COMPLETE:
1770 		iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_COMPLETE_ACK);
1771 		break;
1772 
1773 	default:
1774 		break;
1775 	}
1776 }
1777 
1778 static stmf_status_t
1779 iscsit_idm_to_stmf(idm_status_t idmrc)
1780 {
1781 	switch (idmrc) {
1782 	case IDM_STATUS_SUCCESS:
1783 		return (STMF_SUCCESS);
1784 	default:
1785 		return (STMF_FAILURE);
1786 	}
1787 	/*NOTREACHED*/
1788 }
1789 
1790 
1791 /*
1792  * ISCSI protocol
1793  */
1794 
1795 void
1796 iscsit_op_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu)
1797 {
1798 	iscsit_conn_t		*ict;
1799 	iscsit_task_t		*itask;
1800 	scsi_task_t		*task;
1801 	iscsit_buf_t		*ibuf;
1802 	iscsi_scsi_cmd_hdr_t	*iscsi_scsi =
1803 	    (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
1804 	iscsi_addl_hdr_t	*ahs_hdr;
1805 	uint16_t		addl_cdb_len = 0;
1806 
1807 	ict = ic->ic_handle;
1808 
1809 	itask = iscsit_task_alloc(ict);
1810 	if (itask == NULL) {
1811 		/* Finish processing request */
1812 		iscsit_set_cmdsn(ict, rx_pdu);
1813 
1814 		iscsit_send_direct_scsi_resp(ict, rx_pdu,
1815 		    ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
1816 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
1817 		return;
1818 	}
1819 
1820 
1821 	/*
1822 	 * Note CmdSN and ITT in task.  IDM will have already validated this
1823 	 * request against the connection state so we don't need to check
1824 	 * that (the connection may have changed state in the meantime but
1825 	 * we will catch that when we try to send a response)
1826 	 */
1827 	itask->it_cmdsn = ntohl(iscsi_scsi->cmdsn);
1828 	itask->it_itt = iscsi_scsi->itt;
1829 
1830 	/*
1831 	 * Check for extended CDB AHS
1832 	 */
1833 	if (iscsi_scsi->hlength > 0) {
1834 		ahs_hdr = (iscsi_addl_hdr_t *)iscsi_scsi;
1835 		addl_cdb_len = ((ahs_hdr->ahs_hlen_hi << 8) |
1836 		    ahs_hdr->ahs_hlen_lo) - 1; /* Adjust for reserved byte */
1837 		if (((addl_cdb_len + 4) / sizeof (uint32_t)) >
1838 		    iscsi_scsi->hlength) {
1839 			/* Mangled header info, drop it */
1840 			idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
1841 			return;
1842 		}
1843 	}
1844 
1845 	ict = rx_pdu->isp_ic->ic_handle; /* IDM client private */
1846 
1847 	/*
1848 	 * Add task to session list.  This function will also check to
1849 	 * ensure that the task does not already exist.
1850 	 */
1851 	if (iscsit_task_start(itask) != IDM_STATUS_SUCCESS) {
1852 		/*
1853 		 * Task exists, free all resources and reject.  Don't
1854 		 * update expcmdsn in this case because RFC 3720 says
1855 		 * "The CmdSN of the rejected command PDU (if it is a
1856 		 * non-immediate command) MUST NOT be considered received
1857 		 * by the target (i.e., a command sequence gap must be
1858 		 * assumed for the CmdSN), even though the CmdSN of the
1859 		 * rejected command PDU may be reliably ascertained.  Upon
1860 		 * receiving the Reject, the initiator MUST plug the CmdSN
1861 		 * gap in order to continue to use the session.  The gap
1862 		 * may be plugged either by transmitting a command PDU
1863 		 * with the same CmdSN, or by aborting the task (see section
1864 		 * 6.9 on how an abort may plug a CmdSN gap)." (Section 6.3)
1865 		 */
1866 		iscsit_task_free(itask);
1867 		iscsit_send_reject(ict, rx_pdu, ISCSI_REJECT_TASK_IN_PROGRESS);
1868 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
1869 		return;
1870 	}
1871 
1872 	/* Update sequence numbers */
1873 	iscsit_set_cmdsn(ict, rx_pdu);
1874 
1875 	/*
1876 	 * Allocate STMF task
1877 	 */
1878 	itask->it_stmf_task = stmf_task_alloc(
1879 	    itask->it_ict->ict_sess->ist_lport,
1880 	    itask->it_ict->ict_sess->ist_stmf_sess, iscsi_scsi->lun,
1881 	    16 + addl_cdb_len, 0);
1882 	if (itask->it_stmf_task == NULL) {
1883 		/*
1884 		 * Either stmf really couldn't get memory for a task or,
1885 		 * more likely, the LU is currently in reset.  Either way
1886 		 * we have no choice but to fail the request.
1887 		 */
1888 		iscsit_task_done(itask);
1889 		iscsit_task_free(itask);
1890 		iscsit_send_direct_scsi_resp(ict, rx_pdu,
1891 		    ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
1892 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
1893 		return;
1894 	}
1895 
1896 	task = itask->it_stmf_task;
1897 	task->task_port_private = itask;
1898 
1899 	bcopy(iscsi_scsi->lun, task->task_lun_no, sizeof (task->task_lun_no));
1900 
1901 	/*
1902 	 * iSCSI and Comstar use the same values.  Should we rely on this
1903 	 * or translate them bit-wise?
1904 	 */
1905 
1906 	task->task_flags =
1907 	    (((iscsi_scsi->flags & ISCSI_FLAG_CMD_READ) ? TF_READ_DATA : 0) |
1908 	    ((iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ? TF_WRITE_DATA : 0) |
1909 	    ((rx_pdu->isp_datalen == 0) ? 0 : TF_INITIAL_BURST));
1910 
1911 	switch (iscsi_scsi->flags & ISCSI_FLAG_CMD_ATTR_MASK) {
1912 	case ISCSI_ATTR_UNTAGGED:
1913 		break;
1914 	case ISCSI_ATTR_SIMPLE:
1915 		task->task_additional_flags |= TF_ATTR_SIMPLE_QUEUE;
1916 		break;
1917 	case ISCSI_ATTR_ORDERED:
1918 		task->task_additional_flags |= TF_ATTR_ORDERED_QUEUE;
1919 		break;
1920 	case ISCSI_ATTR_HEAD_OF_QUEUE:
1921 		task->task_additional_flags |= TF_ATTR_HEAD_OF_QUEUE;
1922 		break;
1923 	case ISCSI_ATTR_ACA:
1924 		task->task_additional_flags |= TF_ATTR_ACA;
1925 		break;
1926 	default:
1927 		/* Protocol error but just take it, treat as untagged */
1928 		break;
1929 	}
1930 
1931 
1932 	task->task_additional_flags = 0;
1933 	task->task_priority = 0;
1934 	task->task_mgmt_function = TM_NONE;
1935 
1936 	/*
1937 	 * This "task_max_nbufs" doesn't map well to BIDI.  We probably need
1938 	 * parameter for each direction.  "MaxOutstandingR2T" may very well
1939 	 * be set to one which could prevent us from doing simultaneous
1940 	 * transfers in each direction.
1941 	 */
1942 	task->task_max_nbufs = (iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ?
1943 	    ict->ict_op.op_max_outstanding_r2t : STMF_BUFS_MAX;
1944 	task->task_cmd_seq_no = ntohl(iscsi_scsi->itt);
1945 	task->task_expected_xfer_length = ntohl(iscsi_scsi->data_length);
1946 
1947 	/* Copy CDB */
1948 	bcopy(iscsi_scsi->scb, task->task_cdb, 16);
1949 	if (addl_cdb_len > 0) {
1950 		bcopy(ahs_hdr->ahs_extscb, task->task_cdb + 16, addl_cdb_len);
1951 	}
1952 
1953 	DTRACE_ISCSI_3(scsi__command, idm_conn_t *, ic,
1954 	    iscsi_scsi_cmd_hdr_t *, (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr,
1955 	    scsi_task_t *, task);
1956 
1957 	/*
1958 	 * Copy the transport header into the task handle from the PDU
1959 	 * handle. The transport header describes this task's remote tagged
1960 	 * buffer.
1961 	 */
1962 	if (rx_pdu->isp_transport_hdrlen != 0) {
1963 		bcopy(rx_pdu->isp_transport_hdr,
1964 		    itask->it_idm_task->idt_transport_hdr,
1965 		    rx_pdu->isp_transport_hdrlen);
1966 	}
1967 
1968 	/*
1969 	 * Tell IDM about our new active task
1970 	 */
1971 	idm_task_start(itask->it_idm_task, (uintptr_t)itask->it_itt);
1972 
1973 	/*
1974 	 * If we have any immediate data then setup the immediate buffer
1975 	 * context that comes with the task
1976 	 */
1977 	if (rx_pdu->isp_datalen) {
1978 		ibuf = itask->it_immed_data;
1979 		ibuf->ibuf_immed_data_pdu = rx_pdu;
1980 		ibuf->ibuf_stmf_buf->db_data_size = rx_pdu->isp_datalen;
1981 		ibuf->ibuf_stmf_buf->db_buf_size = rx_pdu->isp_datalen;
1982 		ibuf->ibuf_stmf_buf->db_relative_offset = 0;
1983 		ibuf->ibuf_stmf_buf->db_sglist[0].seg_length =
1984 		    rx_pdu->isp_datalen;
1985 		ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr = rx_pdu->isp_data;
1986 
1987 		DTRACE_ISCSI_8(xfer__start, idm_conn_t *, ic,
1988 		    uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
1989 		    uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
1990 		    uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
1991 		    uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
1992 
1993 		/*
1994 		 * For immediate data transfer, there is no callback from
1995 		 * stmf to indicate that the initial burst of data is
1996 		 * transferred successfully. In some cases, the task can
1997 		 * get freed before execution returns from stmf_post_task.
1998 		 * Although this xfer-start/done probe accurately tracks
1999 		 * the size of the transfer, it does only provide a best
2000 		 * effort on the timing of the transfer.
2001 		 */
2002 		DTRACE_ISCSI_8(xfer__done, idm_conn_t *, ic,
2003 		    uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
2004 		    uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
2005 		    uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
2006 		    uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
2007 
2008 		stmf_post_task(task, ibuf->ibuf_stmf_buf);
2009 	} else {
2010 
2011 		stmf_post_task(task, NULL);
2012 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2013 	}
2014 }
2015 
2016 /*ARGSUSED*/
2017 void
2018 iscsit_deferred_dispatch(idm_pdu_t *rx_pdu)
2019 {
2020 	iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
2021 
2022 	/*
2023 	 * If the connection has been lost then ignore new PDU's
2024 	 */
2025 	mutex_enter(&ict->ict_mutex);
2026 	if (ict->ict_lost) {
2027 		mutex_exit(&ict->ict_mutex);
2028 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2029 		return;
2030 	}
2031 
2032 	/*
2033 	 * Grab a hold on the connection to prevent it from going away
2034 	 * between now and when the taskq function is called.
2035 	 */
2036 	iscsit_conn_dispatch_hold(ict);
2037 	mutex_exit(&ict->ict_mutex);
2038 
2039 	if (taskq_dispatch(iscsit_global.global_dispatch_taskq,
2040 	    iscsit_deferred, rx_pdu, DDI_NOSLEEP) == NULL) {
2041 		/*
2042 		 * In the unlikely scenario that we couldn't get the resources
2043 		 * to dispatch the PDU then just drop it.
2044 		 */
2045 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2046 		idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL);
2047 		iscsit_conn_dispatch_rele(ict);
2048 	}
2049 }
2050 
2051 static void
2052 iscsit_deferred(void *rx_pdu_void)
2053 {
2054 	idm_pdu_t *rx_pdu = rx_pdu_void;
2055 	idm_conn_t *ic = rx_pdu->isp_ic;
2056 	iscsit_conn_t *ict = ic->ic_handle;
2057 
2058 	switch (IDM_PDU_OPCODE(rx_pdu)) {
2059 	case ISCSI_OP_NOOP_OUT:
2060 		iscsit_set_cmdsn(ict, rx_pdu);
2061 		iscsit_pdu_op_noop(ict, rx_pdu);
2062 		break;
2063 	case ISCSI_OP_LOGIN_CMD:
2064 		iscsit_pdu_op_login_cmd(ict, rx_pdu);
2065 		break;
2066 	case ISCSI_OP_TEXT_CMD:
2067 		iscsit_set_cmdsn(ict, rx_pdu);
2068 		iscsit_pdu_op_text_cmd(ict, rx_pdu);
2069 		break;
2070 	case ISCSI_OP_LOGOUT_CMD:
2071 		iscsit_set_cmdsn(ict, rx_pdu);
2072 		iscsit_pdu_op_logout_cmd(ict, rx_pdu);
2073 		break;
2074 	default:
2075 		/* Protocol error.  IDM should have caught this */
2076 		idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2077 		ASSERT(0);
2078 		break;
2079 	}
2080 
2081 	iscsit_conn_dispatch_rele(ict);
2082 }
2083 
2084 static void
2085 iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu,
2086     uint8_t response, uint8_t cmd_status)
2087 {
2088 	idm_pdu_t			*rsp_pdu;
2089 	idm_conn_t			*ic;
2090 	iscsi_scsi_rsp_hdr_t		*resp;
2091 	iscsi_scsi_cmd_hdr_t		*req =
2092 	    (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2093 
2094 	ic = ict->ict_ic;
2095 
2096 	rsp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_rsp_hdr_t), 0);
2097 	idm_pdu_init(rsp_pdu, ic, NULL, NULL);
2098 	/*
2099 	 * StatSN is incremented by 1 for every response sent on
2100 	 * a connection except for responses sent as a result of
2101 	 * a retry or SNACK
2102 	 */
2103 	rsp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2104 
2105 	resp = (iscsi_scsi_rsp_hdr_t *)rsp_pdu->isp_hdr;
2106 
2107 	resp->opcode = ISCSI_OP_SCSI_RSP;
2108 	resp->flags = ISCSI_FLAG_FINAL;
2109 	resp->response = response;
2110 	resp->cmd_status = cmd_status;
2111 	resp->itt = req->itt;
2112 	if ((response == ISCSI_STATUS_CMD_COMPLETED) &&
2113 	    (req->data_length != 0) &&
2114 	    ((req->flags & ISCSI_FLAG_CMD_READ) ||
2115 	    (req->flags & ISCSI_FLAG_CMD_WRITE))) {
2116 		resp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
2117 		resp->residual_count = req->data_length;
2118 	}
2119 
2120 	DTRACE_PROBE4(iscsi__scsi__direct__response,
2121 	    iscsit_conn_t *, ict,
2122 	    uint8_t, resp->response,
2123 	    uint8_t, resp->cmd_status,
2124 	    idm_pdu_t *, rsp_pdu);
2125 
2126 	iscsit_pdu_tx(rsp_pdu);
2127 }
2128 
2129 void
2130 iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu, uint8_t tm_status)
2131 {
2132 	iscsi_scsi_task_mgt_rsp_hdr_t	*tm_resp;
2133 
2134 	/*
2135 	 * The target must take note of the last-sent StatSN.
2136 	 * The StatSN is to be incremented after sending a
2137 	 * task management response. Digest recovery can only
2138 	 * work if StatSN is incremented.
2139 	 */
2140 	tm_resp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2141 	tm_resp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2142 	tm_resp->response = tm_status;
2143 
2144 	DTRACE_PROBE3(iscsi__scsi__tm__response,
2145 	    iscsit_conn_t *, tm_resp_pdu->isp_ic->ic_handle,
2146 	    uint8_t, tm_resp->response,
2147 	    idm_pdu_t *, tm_resp_pdu);
2148 	iscsit_pdu_tx(tm_resp_pdu);
2149 }
2150 
2151 void
2152 iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2153 {
2154 	idm_pdu_t			*tm_resp_pdu;
2155 	iscsit_task_t			*itask;
2156 	iscsit_task_t			*tm_itask;
2157 	scsi_task_t			*task;
2158 	iscsi_scsi_task_mgt_hdr_t 	*iscsi_tm =
2159 	    (iscsi_scsi_task_mgt_hdr_t *)rx_pdu->isp_hdr;
2160 	iscsi_scsi_task_mgt_rsp_hdr_t 	*iscsi_tm_rsp =
2161 	    (iscsi_scsi_task_mgt_rsp_hdr_t *)rx_pdu->isp_hdr;
2162 	uint32_t			rtt, cmdsn, refcmdsn;
2163 	uint8_t				tm_func;
2164 
2165 	/*
2166 	 * Setup response PDU (response field will get filled in later)
2167 	 */
2168 	tm_resp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_task_mgt_rsp_hdr_t), 0);
2169 	if (tm_resp_pdu == NULL) {
2170 		/* Can't respond, just drop it */
2171 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2172 		return;
2173 	}
2174 	idm_pdu_init(tm_resp_pdu, ict->ict_ic, NULL, NULL);
2175 	iscsi_tm_rsp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2176 	bzero(iscsi_tm_rsp, sizeof (iscsi_scsi_task_mgt_rsp_hdr_t));
2177 	iscsi_tm_rsp->opcode = ISCSI_OP_SCSI_TASK_MGT_RSP;
2178 	iscsi_tm_rsp->flags = ISCSI_FLAG_FINAL;
2179 	iscsi_tm_rsp->itt = rx_pdu->isp_hdr->itt;
2180 
2181 	/*
2182 	 * Figure out what we're being asked to do.
2183 	 */
2184 	DTRACE_PROBE4(iscsi__scsi__tm__request,
2185 	    iscsit_conn_t *, ict,
2186 	    uint8_t, (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK),
2187 	    uint32_t, iscsi_tm->rtt,
2188 	    idm_pdu_t *, rx_pdu);
2189 	switch (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK) {
2190 	case ISCSI_TM_FUNC_ABORT_TASK:
2191 		/*
2192 		 * STMF doesn't currently support the "abort task" task
2193 		 * management command although it does support aborting
2194 		 * an individual task.  We'll get STMF to abort the task
2195 		 * for us but handle the details of the task management
2196 		 * command ourselves.
2197 		 *
2198 		 * Find the task associated with the referenced task tag.
2199 		 */
2200 		rtt = iscsi_tm->rtt;
2201 		itask = (iscsit_task_t *)idm_task_find_by_handle(ict->ict_ic,
2202 		    (uintptr_t)rtt);
2203 
2204 		if (itask == NULL) {
2205 			cmdsn = ntohl(iscsi_tm->cmdsn);
2206 			refcmdsn = ntohl(iscsi_tm->refcmdsn);
2207 
2208 			/*
2209 			 * Task was not found.  If RefCmdSN is within the CmdSN
2210 			 * window and less than CmdSN of the TM function, return
2211 			 * "Function Complete".  Otherwise, return
2212 			 * "Task Does Not Exist".
2213 			 */
2214 
2215 			if (iscsit_cmdsn_in_window(ict, refcmdsn) &&
2216 			    (refcmdsn < cmdsn)) {
2217 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2218 				    SCSI_TCP_TM_RESP_COMPLETE);
2219 			} else {
2220 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2221 				    SCSI_TCP_TM_RESP_NO_TASK);
2222 			}
2223 		} else {
2224 
2225 			/*
2226 			 * Tell STMF to abort the task.  This will do no harm
2227 			 * if the task is already complete.
2228 			 */
2229 			stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
2230 			    STMF_ABORTED, NULL);
2231 
2232 			/*
2233 			 * Make sure the task hasn't already completed
2234 			 */
2235 			mutex_enter(&itask->it_idm_task->idt_mutex);
2236 			if ((itask->it_idm_task->idt_state == TASK_COMPLETE) ||
2237 			    (itask->it_idm_task->idt_state == TASK_IDLE)) {
2238 				/*
2239 				 * Task is complete, return "Task Does Not
2240 				 * Exist"
2241 				 */
2242 				mutex_exit(&itask->it_idm_task->idt_mutex);
2243 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2244 				    SCSI_TCP_TM_RESP_NO_TASK);
2245 			} else {
2246 				/*
2247 				 * STMF is now aborting the task, return
2248 				 * "Function Complete"
2249 				 */
2250 				mutex_exit(&itask->it_idm_task->idt_mutex);
2251 				iscsit_send_task_mgmt_resp(tm_resp_pdu,
2252 				    SCSI_TCP_TM_RESP_COMPLETE);
2253 			}
2254 			idm_task_rele(itask->it_idm_task);
2255 		}
2256 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2257 		return;
2258 
2259 	case ISCSI_TM_FUNC_ABORT_TASK_SET:
2260 		tm_func = TM_ABORT_TASK_SET;
2261 		break;
2262 
2263 	case ISCSI_TM_FUNC_CLEAR_ACA:
2264 		tm_func = TM_CLEAR_ACA;
2265 		break;
2266 
2267 	case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2268 		tm_func = TM_CLEAR_TASK_SET;
2269 		break;
2270 
2271 	case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2272 		tm_func = TM_LUN_RESET;
2273 		break;
2274 
2275 	case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2276 		tm_func = TM_TARGET_WARM_RESET;
2277 		break;
2278 
2279 	case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2280 		tm_func = TM_TARGET_COLD_RESET;
2281 		break;
2282 
2283 	case ISCSI_TM_FUNC_TASK_REASSIGN:
2284 		/*
2285 		 * We do not currently support allegiance reassignment.  When
2286 		 * we start supporting ERL1+, we will need to.
2287 		 */
2288 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2289 		    SCSI_TCP_TM_RESP_NO_ALLG_REASSN);
2290 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2291 		return;
2292 
2293 	default:
2294 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2295 		    SCSI_TCP_TM_RESP_REJECTED);
2296 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2297 		return;
2298 	}
2299 
2300 	tm_itask = iscsit_tm_task_alloc(ict);
2301 	if (tm_itask == NULL) {
2302 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2303 		    SCSI_TCP_TM_RESP_REJECTED);
2304 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2305 		return;
2306 	}
2307 
2308 
2309 	task = stmf_task_alloc(ict->ict_sess->ist_lport,
2310 	    ict->ict_sess->ist_stmf_sess, iscsi_tm->lun,
2311 	    0, STMF_TASK_EXT_NONE);
2312 	if (task == NULL) {
2313 		/*
2314 		 * If this happens, either the LU is in reset, couldn't
2315 		 * get memory, or some other condition in which we simply
2316 		 * can't complete this request.  It would be nice to return
2317 		 * an error code like "busy" but the closest we have is
2318 		 * "rejected".
2319 		 */
2320 		iscsit_send_task_mgmt_resp(tm_resp_pdu,
2321 		    SCSI_TCP_TM_RESP_REJECTED);
2322 		iscsit_tm_task_free(tm_itask);
2323 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2324 		return;
2325 	}
2326 
2327 	tm_itask->it_tm_pdu = tm_resp_pdu;
2328 	tm_itask->it_stmf_task = task;
2329 	task->task_port_private = tm_itask;
2330 	task->task_mgmt_function = tm_func;
2331 	task->task_additional_flags = TASK_AF_NO_EXPECTED_XFER_LENGTH;
2332 	task->task_priority = 0;
2333 	task->task_max_nbufs = STMF_BUFS_MAX;
2334 	task->task_cmd_seq_no = iscsi_tm->itt;
2335 	task->task_expected_xfer_length = 0;
2336 
2337 	stmf_post_task(task, NULL);
2338 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2339 }
2340 
2341 static void
2342 iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2343 {
2344 	iscsi_nop_out_hdr_t *out = (iscsi_nop_out_hdr_t *)rx_pdu->isp_hdr;
2345 	iscsi_nop_in_hdr_t *in;
2346 	int resp_datalen;
2347 	idm_pdu_t *resp;
2348 
2349 	/* Ignore the response from initiator */
2350 	if ((out->itt == ISCSI_RSVD_TASK_TAG) ||
2351 	    (out->ttt != ISCSI_RSVD_TASK_TAG)) {
2352 		idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2353 		return;
2354 	}
2355 
2356 	/* Allocate a PDU to respond */
2357 	resp_datalen = ntoh24(out->dlength);
2358 	resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
2359 	idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
2360 	if (resp_datalen > 0) {
2361 		bcopy(rx_pdu->isp_data, resp->isp_data, resp_datalen);
2362 	}
2363 
2364 	/*
2365 	 * When sending a NOP-In as a response to a NOP-Out from the initiator,
2366 	 * the target must respond with the same initiator task tag that was
2367 	 * provided in the NOP-Out request, the target transfer tag must be
2368 	 * ISCSI_RSVD_TASK_TAG (0xffffffff) and StatSN will contain the next
2369 	 * status sequence number. The StatSN for the connection is advanced
2370 	 * after this PDU is sent.
2371 	 */
2372 	in = (iscsi_nop_in_hdr_t *)resp->isp_hdr;
2373 	bzero(in, sizeof (*in));
2374 	in->opcode = ISCSI_OP_NOOP_IN;
2375 	in->flags = ISCSI_FLAG_FINAL;
2376 	bcopy(out->lun, in->lun, 8);
2377 	in->itt		= out->itt;
2378 	in->ttt		= ISCSI_RSVD_TASK_TAG;
2379 	hton24(in->dlength, resp_datalen);
2380 	resp->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2381 	/* Any other field in resp to be set? */
2382 	iscsit_pdu_tx(resp);
2383 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2384 }
2385 
2386 static void
2387 iscsit_pdu_op_login_cmd(iscsit_conn_t	*ict, idm_pdu_t *rx_pdu)
2388 {
2389 
2390 	/*
2391 	 * Submit PDU to login state machine.  State machine will free the
2392 	 * PDU.
2393 	 */
2394 	iscsit_login_sm_event(ict, ILE_LOGIN_RCV, rx_pdu);
2395 }
2396 
2397 void
2398 iscsit_pdu_op_logout_cmd(iscsit_conn_t	*ict, idm_pdu_t *rx_pdu)
2399 {
2400 	iscsi_logout_hdr_t 	*logout_req =
2401 	    (iscsi_logout_hdr_t *)rx_pdu->isp_hdr;
2402 	iscsi_logout_rsp_hdr_t	*logout_rsp;
2403 	idm_pdu_t *resp;
2404 
2405 	/* Allocate a PDU to respond */
2406 	resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2407 	idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
2408 	/*
2409 	 * The StatSN is to be sent to the initiator,
2410 	 * it is not required to increment the number
2411 	 * as the connection is terminating.
2412 	 */
2413 	resp->isp_flags |= IDM_PDU_SET_STATSN;
2414 	/*
2415 	 * Logout results in the immediate termination of all tasks except
2416 	 * if the logout reason is ISCSI_LOGOUT_REASON_RECOVERY.  The
2417 	 * connection state machine will drive this task cleanup automatically
2418 	 * so we don't need to handle that here.
2419 	 */
2420 	logout_rsp = (iscsi_logout_rsp_hdr_t *)resp->isp_hdr;
2421 	bzero(logout_rsp, sizeof (*logout_rsp));
2422 	logout_rsp->opcode = ISCSI_OP_LOGOUT_RSP;
2423 	logout_rsp->flags = ISCSI_FLAG_FINAL;
2424 	logout_rsp->itt = logout_req->itt;
2425 	if ((logout_req->flags & ISCSI_FLAG_LOGOUT_REASON_MASK) >
2426 	    ISCSI_LOGOUT_REASON_RECOVERY) {
2427 		logout_rsp->response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2428 	} else {
2429 		logout_rsp->response = ISCSI_LOGOUT_SUCCESS;
2430 	}
2431 
2432 	iscsit_pdu_tx(resp);
2433 	idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2434 }
2435 
2436 /*
2437  * Calculate the number of outstanding commands we can process
2438  */
2439 int
2440 iscsit_cmd_window()
2441 {
2442 	/* Will be better later */
2443 	return	(1024);
2444 }
2445 
2446 /*
2447  * Set local registers based on incoming PDU
2448  */
2449 void
2450 iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2451 {
2452 	iscsit_sess_t *ist;
2453 	iscsi_scsi_cmd_hdr_t *req;
2454 
2455 	ist = ict->ict_sess;
2456 
2457 	req = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2458 
2459 	rw_enter(&ist->ist_sn_rwlock, RW_WRITER);
2460 	ist->ist_expcmdsn = ntohl(req->cmdsn) + 1;
2461 	ist->ist_maxcmdsn = ntohl(req->cmdsn) + iscsit_cmd_window();
2462 	rw_exit(&ist->ist_sn_rwlock);
2463 }
2464 
2465 /*
2466  * Wrapper funtion, calls iscsi_calc_rspsn and idm_pdu_tx
2467  */
2468 void
2469 iscsit_pdu_tx(idm_pdu_t *pdu)
2470 {
2471 	iscsit_conn_t *ict = pdu->isp_ic->ic_handle;
2472 	iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
2473 	iscsit_sess_t *ist = ict->ict_sess;
2474 
2475 	/*
2476 	 * The command sequence numbers are session-wide and must stay
2477 	 * consistent across the transfer, so protect the cmdsn with a
2478 	 * reader lock on the session. The status sequence number will
2479 	 * be updated just before the transport layer transmits the PDU.
2480 	 */
2481 
2482 	rw_enter(&ict->ict_sess->ist_sn_rwlock, RW_READER);
2483 	/* Set ExpCmdSN and MaxCmdSN */
2484 	rsp->maxcmdsn = htonl(ist->ist_maxcmdsn);
2485 	rsp->expcmdsn = htonl(ist->ist_expcmdsn);
2486 	idm_pdu_tx(pdu);
2487 	rw_exit(&ict->ict_sess->ist_sn_rwlock);
2488 }
2489 
2490 /*
2491  * Internal functions
2492  */
2493 
2494 void
2495 iscsit_send_async_event(iscsit_conn_t *ict, uint8_t event)
2496 {
2497 	idm_pdu_t		*abt;
2498 	iscsi_async_evt_hdr_t	*async_abt;
2499 
2500 	/*
2501 	 * Get a PDU to build the abort request.
2502 	 */
2503 	abt = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2504 	if (abt == NULL) {
2505 		idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL);
2506 		return;
2507 	}
2508 
2509 	/*
2510 	 * A asynchronous message is sent by the target to request a logout.
2511 	 * The StatSN for the connection is advanced after the PDU is sent
2512 	 * to allow for initiator and target state synchronization.
2513 	 */
2514 	idm_pdu_init(abt, ict->ict_ic, NULL, NULL);
2515 	abt->isp_datalen = 0;
2516 	abt->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2517 
2518 	async_abt = (iscsi_async_evt_hdr_t *)abt->isp_hdr;
2519 	bzero(async_abt, sizeof (*async_abt));
2520 	async_abt->opcode = ISCSI_OP_ASYNC_EVENT;
2521 	async_abt->async_event = event;
2522 	async_abt->flags = ISCSI_FLAG_FINAL;
2523 	async_abt->rsvd4[0] = 0xff;
2524 	async_abt->rsvd4[1] = 0xff;
2525 	async_abt->rsvd4[2] = 0xff;
2526 	async_abt->rsvd4[3] = 0xff;
2527 
2528 	switch (event) {
2529 	case ISCSI_ASYNC_EVENT_REQUEST_LOGOUT:
2530 		async_abt->param3 = htons(IDM_LOGOUT_SECONDS);
2531 		break;
2532 	case ISCSI_ASYNC_EVENT_SCSI_EVENT:
2533 	case ISCSI_ASYNC_EVENT_DROPPING_CONNECTION:
2534 	case ISCSI_ASYNC_EVENT_DROPPING_ALL_CONNECTIONS:
2535 	case ISCSI_ASYNC_EVENT_PARAM_NEGOTIATION:
2536 	default:
2537 		ASSERT(0);
2538 	}
2539 
2540 	iscsit_pdu_tx(abt);
2541 }
2542 
2543 void
2544 iscsit_send_reject(iscsit_conn_t *ict, idm_pdu_t *rejected_pdu, uint8_t reason)
2545 {
2546 	idm_pdu_t		*reject_pdu;
2547 	iscsi_reject_rsp_hdr_t	*reject;
2548 
2549 	/*
2550 	 * Get a PDU to build the abort request.
2551 	 */
2552 	reject_pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t),
2553 	    rejected_pdu->isp_hdrlen);
2554 	if (reject_pdu == NULL) {
2555 		idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL);
2556 		return;
2557 	}
2558 	idm_pdu_init(reject_pdu, ict->ict_ic, NULL, NULL);
2559 	/* StatSN is advanced after a Reject PDU */
2560 	reject_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2561 	reject_pdu->isp_datalen = rejected_pdu->isp_hdrlen;
2562 	bcopy(rejected_pdu->isp_hdr, reject_pdu->isp_data,
2563 	    rejected_pdu->isp_hdrlen);
2564 
2565 	reject = (iscsi_reject_rsp_hdr_t *)reject_pdu->isp_hdr;
2566 	bzero(reject, sizeof (*reject));
2567 	reject->opcode = ISCSI_OP_REJECT_MSG;
2568 	reject->reason = reason;
2569 	reject->flags = ISCSI_FLAG_FINAL;
2570 	hton24(reject->dlength, rejected_pdu->isp_hdrlen);
2571 	reject->must_be_ff[0] = 0xff;
2572 	reject->must_be_ff[1] = 0xff;
2573 	reject->must_be_ff[2] = 0xff;
2574 	reject->must_be_ff[3] = 0xff;
2575 
2576 	iscsit_pdu_tx(reject_pdu);
2577 }
2578 
2579 
2580 static iscsit_task_t *
2581 iscsit_task_alloc(iscsit_conn_t *ict)
2582 {
2583 	iscsit_task_t *itask;
2584 	iscsit_buf_t *immed_ibuf;
2585 
2586 	/*
2587 	 * Possible items to pre-alloc if we cache iscsit_task_t's:
2588 	 *
2589 	 * Status PDU w/ sense buffer
2590 	 * stmf_data_buf_t for immediate data
2591 	 */
2592 	itask = kmem_alloc(sizeof (iscsit_task_t) + sizeof (iscsit_buf_t) +
2593 	    sizeof (stmf_data_buf_t), KM_NOSLEEP);
2594 	if (itask != NULL) {
2595 		mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2596 		itask->it_aborted = itask->it_stmf_abort =
2597 		    itask->it_tm_task = 0;
2598 
2599 		immed_ibuf = (iscsit_buf_t *)(itask + 1);
2600 		bzero(immed_ibuf, sizeof (*immed_ibuf));
2601 		immed_ibuf->ibuf_is_immed = B_TRUE;
2602 		immed_ibuf->ibuf_stmf_buf = (stmf_data_buf_t *)(immed_ibuf + 1);
2603 
2604 		bzero(immed_ibuf->ibuf_stmf_buf, sizeof (stmf_data_buf_t));
2605 		immed_ibuf->ibuf_stmf_buf->db_port_private = immed_ibuf;
2606 		immed_ibuf->ibuf_stmf_buf->db_sglist_length = 1;
2607 		immed_ibuf->ibuf_stmf_buf->db_flags = DB_DIRECTION_FROM_RPORT |
2608 		    DB_DONT_CACHE;
2609 		itask->it_immed_data = immed_ibuf;
2610 		itask->it_idm_task = idm_task_alloc(ict->ict_ic);
2611 		if (itask->it_idm_task != NULL) {
2612 			itask->it_idm_task->idt_private = itask;
2613 			itask->it_ict = ict;
2614 			itask->it_ttt = itask->it_idm_task->idt_tt;
2615 			return (itask);
2616 		} else {
2617 			kmem_free(itask, sizeof (iscsit_task_t) +
2618 			    sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2619 		}
2620 	}
2621 
2622 	return (NULL);
2623 }
2624 
2625 static void
2626 iscsit_task_free(iscsit_task_t *itask)
2627 {
2628 	idm_task_free(itask->it_idm_task);
2629 	mutex_destroy(&itask->it_mutex);
2630 	kmem_free(itask, sizeof (iscsit_task_t) +
2631 	    sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2632 }
2633 
2634 static iscsit_task_t *
2635 iscsit_tm_task_alloc(iscsit_conn_t *ict)
2636 {
2637 	iscsit_task_t *itask;
2638 
2639 	itask = kmem_zalloc(sizeof (iscsit_task_t), KM_NOSLEEP);
2640 	if (itask != NULL) {
2641 		idm_conn_hold(ict->ict_ic);
2642 		mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2643 		itask->it_aborted = itask->it_stmf_abort =
2644 		    itask->it_tm_responded = 0;
2645 		itask->it_tm_pdu = NULL;
2646 		itask->it_tm_task = 1;
2647 		itask->it_ict = ict;
2648 	}
2649 
2650 	return (itask);
2651 }
2652 
2653 static void
2654 iscsit_tm_task_free(iscsit_task_t *itask)
2655 {
2656 	/*
2657 	 * If we responded then the call to idm_pdu_complete will free the
2658 	 * PDU.  Otherwise we got aborted before the TM function could
2659 	 * complete and we need to free the PDU explicitly.
2660 	 */
2661 	if (itask->it_tm_pdu != NULL && !itask->it_tm_responded)
2662 		idm_pdu_free(itask->it_tm_pdu);
2663 	idm_conn_rele(itask->it_ict->ict_ic);
2664 	mutex_destroy(&itask->it_mutex);
2665 	kmem_free(itask, sizeof (iscsit_task_t));
2666 }
2667 
2668 static idm_status_t
2669 iscsit_task_start(iscsit_task_t *itask)
2670 {
2671 	iscsit_sess_t *ist = itask->it_ict->ict_sess;
2672 	avl_index_t		where;
2673 
2674 	/*
2675 	 * Sanity check the ITT and ensure that this task does not already
2676 	 * exist.  If not then add the task to the session task list.
2677 	 */
2678 	mutex_enter(&ist->ist_mutex);
2679 	mutex_enter(&itask->it_mutex);
2680 	itask->it_active = 1;
2681 	if (avl_find(&ist->ist_task_list, itask, &where) == NULL) {
2682 		/* New task, add to AVL */
2683 		avl_insert(&ist->ist_task_list, itask, where);
2684 		mutex_exit(&itask->it_mutex);
2685 		mutex_exit(&ist->ist_mutex);
2686 		return (IDM_STATUS_SUCCESS);
2687 	}
2688 	mutex_exit(&itask->it_mutex);
2689 	mutex_exit(&ist->ist_mutex);
2690 
2691 	return (IDM_STATUS_REJECT);
2692 }
2693 
2694 static void
2695 iscsit_task_done(iscsit_task_t *itask)
2696 {
2697 	iscsit_sess_t *ist = itask->it_ict->ict_sess;
2698 
2699 	mutex_enter(&ist->ist_mutex);
2700 	mutex_enter(&itask->it_mutex);
2701 	if (itask->it_active) {
2702 		avl_remove(&ist->ist_task_list, itask);
2703 		itask->it_active = 0;
2704 	}
2705 	mutex_exit(&itask->it_mutex);
2706 	mutex_exit(&ist->ist_mutex);
2707 }
2708 
2709 /*
2710  * iscsit status PDU cache
2711  */
2712 
2713 /*ARGSUSED*/
2714 static int
2715 iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags)
2716 {
2717 	idm_pdu_t *pdu = pdu_void;
2718 	iscsi_scsi_rsp_hdr_t *rsp;
2719 
2720 	bzero(pdu, sizeof (idm_pdu_t));
2721 	pdu->isp_callback = iscsit_send_good_status_done;
2722 	pdu->isp_magic = IDM_PDU_MAGIC;
2723 	pdu->isp_hdr = (iscsi_hdr_t *)(pdu + 1); /* Ptr arithmetic */
2724 	pdu->isp_hdrlen = sizeof (iscsi_hdr_t);
2725 
2726 	/* Setup status response */
2727 	rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
2728 	bzero(rsp, sizeof (*rsp));
2729 	rsp->opcode = ISCSI_OP_SCSI_RSP;
2730 	rsp->flags = ISCSI_FLAG_FINAL;
2731 	rsp->response = ISCSI_STATUS_CMD_COMPLETED;
2732 
2733 	return (0);
2734 }
2735 
2736 /*
2737  * iscsit private data handler
2738  */
2739 
2740 /*ARGSUSED*/
2741 static void
2742 iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags)
2743 {
2744 	it_config_t		*cfg;
2745 	nvlist_t		*nvl;
2746 
2747 	if ((cmd != STMF_PROVIDER_DATA_UPDATED) || (arg == NULL)) {
2748 		return;
2749 	}
2750 
2751 	nvl = (nvlist_t *)arg;
2752 
2753 	/* Translate nvlist */
2754 	if (it_nv_to_config(nvl, &cfg) != 0) {
2755 		cmn_err(CE_WARN, "Configuration is invalid");
2756 		return;
2757 	}
2758 
2759 	/* Update config */
2760 	(void) iscsit_config_merge(cfg);
2761 
2762 	it_config_free_cmn(cfg);
2763 }
2764 
2765 
2766 static it_cfg_status_t
2767 iscsit_config_merge(it_config_t *in_cfg)
2768 {
2769 	it_cfg_status_t	status;
2770 	it_config_t	*cfg;
2771 	it_config_t	tmp_cfg;
2772 	list_t		tpg_del_list;
2773 
2774 	if (in_cfg) {
2775 		cfg = in_cfg;
2776 	} else {
2777 		/* Make empty config */
2778 		bzero(&tmp_cfg, sizeof (tmp_cfg));
2779 		cfg = &tmp_cfg;
2780 	}
2781 
2782 	list_create(&tpg_del_list,  sizeof (iscsit_tpg_t),
2783 	    offsetof(iscsit_tpg_t, tpg_delete_ln));
2784 
2785 	/*
2786 	 * Update targets, initiator contexts, target portal groups,
2787 	 * and iSNS client
2788 	 */
2789 	ISCSIT_GLOBAL_LOCK(RW_WRITER);
2790 	if (((status = iscsit_config_merge_tpg(cfg, &tpg_del_list))
2791 	    != 0) ||
2792 	    ((status = iscsit_config_merge_tgt(cfg)) != 0) ||
2793 	    ((status = iscsit_config_merge_ini(cfg)) != 0) ||
2794 	    ((status = isnst_config_merge(cfg)) != 0)) {
2795 		ISCSIT_GLOBAL_UNLOCK();
2796 		return (status);
2797 	}
2798 
2799 	/* Update other global config parameters */
2800 	if (iscsit_global.global_props) {
2801 		nvlist_free(iscsit_global.global_props);
2802 		iscsit_global.global_props = NULL;
2803 	}
2804 	if (in_cfg) {
2805 		(void) nvlist_dup(cfg->config_global_properties,
2806 		    &iscsit_global.global_props, KM_SLEEP);
2807 	}
2808 	ISCSIT_GLOBAL_UNLOCK();
2809 
2810 	iscsit_config_destroy_tpgs(&tpg_del_list);
2811 
2812 	list_destroy(&tpg_del_list);
2813 
2814 	return (ITCFG_SUCCESS);
2815 }
2816 
2817 /*
2818  * iscsit_sna_lt[e]
2819  *
2820  * Compare serial numbers using serial number arithmetic as defined in
2821  * RFC 1982.
2822  *
2823  * NOTE: This code is duplicated in the isns server as well as iscsitgtd.  It
2824  * ought to be common.
2825  */
2826 
2827 static int
2828 iscsit_sna_lt(uint32_t sn1, uint32_t sn2)
2829 {
2830 	return ((sn1 != sn2) &&
2831 	    (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
2832 	    ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
2833 }
2834 
2835 static int
2836 iscsit_sna_lte(uint32_t sn1, uint32_t sn2)
2837 {
2838 	return ((sn1 == sn2) ||
2839 	    (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
2840 	    ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
2841 }
2842 
2843 
2844 static boolean_t
2845 iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn)
2846 {
2847 	iscsit_sess_t	*ist = ict->ict_sess;
2848 	int		rval = B_TRUE;
2849 
2850 	ist = ict->ict_sess;
2851 
2852 	rw_enter(&ist->ist_sn_rwlock, RW_READER);
2853 
2854 	/*
2855 	 * If cmdsn is less than ist_expcmdsn - iscsit_cmd_window() or
2856 	 * greater than ist_expcmdsn, it's not in the window.
2857 	 */
2858 
2859 	if (iscsit_sna_lt(cmdsn, (ist->ist_expcmdsn - iscsit_cmd_window())) ||
2860 	    !iscsit_sna_lte(cmdsn, ist->ist_expcmdsn)) {
2861 		rval = B_FALSE;
2862 	}
2863 
2864 	rw_exit(&ist->ist_sn_rwlock);
2865 
2866 	return (rval);
2867 }
2868