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