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