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