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