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