16615 ena assertion failure in ena_tx_intr_work()
[illumos-gate.git] / usr / src / uts / common / io / comstar / port / iscsit / iscsit.c
blob1a3483f1897cac66216e89b3821c4d40e5eb1c60
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright 2017 Nexenta Systems, Inc.
25 * Copyright (c) 2017, Joyent, Inc. All rights reserved.
28 #include <sys/cpuvar.h>
29 #include <sys/types.h>
30 #include <sys/conf.h>
31 #include <sys/stat.h>
32 #include <sys/file.h>
33 #include <sys/ddi.h>
34 #include <sys/sunddi.h>
35 #include <sys/modctl.h>
36 #include <sys/sysmacros.h>
37 #include <sys/socket.h>
38 #include <sys/strsubr.h>
39 #include <sys/nvpair.h>
41 #include <sys/stmf.h>
42 #include <sys/stmf_ioctl.h>
43 #include <sys/portif.h>
44 #include <sys/idm/idm.h>
45 #include <sys/idm/idm_conn_sm.h>
47 #include "iscsit_isns.h"
48 #include "iscsit.h"
50 #define ISCSIT_VERSION BUILD_DATE "-1.18dev"
51 #define ISCSIT_NAME_VERSION "COMSTAR ISCSIT v" ISCSIT_VERSION
54 * DDI entry points.
56 static int iscsit_drv_attach(dev_info_t *, ddi_attach_cmd_t);
57 static int iscsit_drv_detach(dev_info_t *, ddi_detach_cmd_t);
58 static int iscsit_drv_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
59 static int iscsit_drv_open(dev_t *, int, int, cred_t *);
60 static int iscsit_drv_close(dev_t, int, int, cred_t *);
61 static boolean_t iscsit_drv_busy(void);
62 static int iscsit_drv_ioctl(dev_t, int, intptr_t, int, cred_t *, int *);
64 extern struct mod_ops mod_miscops;
67 static struct cb_ops iscsit_cb_ops = {
68 iscsit_drv_open, /* cb_open */
69 iscsit_drv_close, /* cb_close */
70 nodev, /* cb_strategy */
71 nodev, /* cb_print */
72 nodev, /* cb_dump */
73 nodev, /* cb_read */
74 nodev, /* cb_write */
75 iscsit_drv_ioctl, /* cb_ioctl */
76 nodev, /* cb_devmap */
77 nodev, /* cb_mmap */
78 nodev, /* cb_segmap */
79 nochpoll, /* cb_chpoll */
80 ddi_prop_op, /* cb_prop_op */
81 NULL, /* cb_streamtab */
82 D_MP, /* cb_flag */
83 CB_REV, /* cb_rev */
84 nodev, /* cb_aread */
85 nodev, /* cb_awrite */
88 static struct dev_ops iscsit_dev_ops = {
89 DEVO_REV, /* devo_rev */
90 0, /* devo_refcnt */
91 iscsit_drv_getinfo, /* devo_getinfo */
92 nulldev, /* devo_identify */
93 nulldev, /* devo_probe */
94 iscsit_drv_attach, /* devo_attach */
95 iscsit_drv_detach, /* devo_detach */
96 nodev, /* devo_reset */
97 &iscsit_cb_ops, /* devo_cb_ops */
98 NULL, /* devo_bus_ops */
99 NULL, /* devo_power */
100 ddi_quiesce_not_needed, /* quiesce */
103 static struct modldrv modldrv = {
104 &mod_driverops,
105 "iSCSI Target",
106 &iscsit_dev_ops,
109 static struct modlinkage modlinkage = {
110 MODREV_1,
111 &modldrv,
112 NULL,
116 iscsit_global_t iscsit_global;
118 kmem_cache_t *iscsit_status_pdu_cache;
120 boolean_t iscsit_sm_logging = B_FALSE;
122 kmutex_t login_sm_session_mutex;
124 static idm_status_t iscsit_init(dev_info_t *dip);
125 static idm_status_t iscsit_enable_svc(iscsit_hostinfo_t *hostinfo);
126 static void iscsit_disable_svc(void);
128 static boolean_t
129 iscsit_check_cmdsn_and_queue(idm_pdu_t *rx_pdu);
131 static void
132 iscsit_add_pdu_to_queue(iscsit_sess_t *ist, idm_pdu_t *rx_pdu);
134 static idm_pdu_t *
135 iscsit_remove_pdu_from_queue(iscsit_sess_t *ist, uint32_t cmdsn);
137 static void
138 iscsit_process_pdu_in_queue(iscsit_sess_t *ist);
140 static void
141 iscsit_rxpdu_queue_monitor_session(iscsit_sess_t *ist);
143 static void
144 iscsit_rxpdu_queue_monitor(void *arg);
146 static void
147 iscsit_post_staged_pdu(idm_pdu_t *rx_pdu);
149 static void
150 iscsit_post_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu);
152 static void
153 iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
155 static void
156 iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
158 static void
159 iscsit_pdu_op_login_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
161 void
162 iscsit_pdu_op_text_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
164 static void
165 iscsit_pdu_op_logout_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
167 int iscsit_cmd_window();
169 static int
170 iscsit_sna_lt(uint32_t sn1, uint32_t sn2);
172 void
173 iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu);
175 static void
176 iscsit_deferred_dispatch(idm_pdu_t *rx_pdu);
178 static void
179 iscsit_deferred(void *rx_pdu_void);
181 static idm_status_t
182 iscsit_conn_accept(idm_conn_t *ic);
184 static idm_status_t
185 iscsit_ffp_enabled(idm_conn_t *ic);
187 static idm_status_t
188 iscsit_ffp_disabled(idm_conn_t *ic, idm_ffp_disable_t disable_class);
190 static idm_status_t
191 iscsit_conn_lost(idm_conn_t *ic);
193 static idm_status_t
194 iscsit_conn_destroy(idm_conn_t *ic);
196 static stmf_data_buf_t *
197 iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
198 uint32_t flags);
200 static void
201 iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf);
203 static void
204 iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status);
206 static void
207 iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status);
209 static void
210 iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status);
212 static stmf_status_t
213 iscsit_idm_to_stmf(idm_status_t idmrc);
215 static iscsit_task_t *
216 iscsit_task_alloc(iscsit_conn_t *ict);
218 static void
219 iscsit_task_free(iscsit_task_t *itask);
221 static iscsit_task_t *
222 iscsit_tm_task_alloc(iscsit_conn_t *ict);
224 static void
225 iscsit_tm_task_free(iscsit_task_t *itask);
227 static idm_status_t
228 iscsit_task_start(iscsit_task_t *itask);
230 static void
231 iscsit_task_done(iscsit_task_t *itask);
233 static int
234 iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags);
236 static void
237 iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags);
239 static it_cfg_status_t
240 iscsit_config_merge(it_config_t *cfg);
242 static idm_status_t
243 iscsit_login_fail(idm_conn_t *ic);
245 static boolean_t iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn);
246 static void iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu,
247 uint8_t response, uint8_t cmd_status);
248 static void iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu,
249 uint8_t tm_status);
252 * MC/S: Out-of-order commands are staged on a session-wide wait
253 * queue until a system-tunable threshold is reached. A separate
254 * thread is used to scan the staging queue on all the session,
255 * If a delayed PDU does not arrive within a timeout, the target
256 * will advance to the staged PDU that is next in sequence, skipping
257 * over the missing PDU(s) to go past a hole in the sequence.
259 volatile int rxpdu_queue_threshold = ISCSIT_RXPDU_QUEUE_THRESHOLD;
261 static kmutex_t iscsit_rxpdu_queue_monitor_mutex;
262 kthread_t *iscsit_rxpdu_queue_monitor_thr_id;
263 static kt_did_t iscsit_rxpdu_queue_monitor_thr_did;
264 static boolean_t iscsit_rxpdu_queue_monitor_thr_running;
265 static kcondvar_t iscsit_rxpdu_queue_monitor_cv;
268 _init(void)
270 int rc;
272 rw_init(&iscsit_global.global_rwlock, NULL, RW_DRIVER, NULL);
273 mutex_init(&iscsit_global.global_state_mutex, NULL,
274 MUTEX_DRIVER, NULL);
275 iscsit_global.global_svc_state = ISE_DETACHED;
277 mutex_init(&iscsit_rxpdu_queue_monitor_mutex, NULL,
278 MUTEX_DRIVER, NULL);
279 mutex_init(&login_sm_session_mutex, NULL, MUTEX_DRIVER, NULL);
280 iscsit_rxpdu_queue_monitor_thr_id = NULL;
281 iscsit_rxpdu_queue_monitor_thr_running = B_FALSE;
282 cv_init(&iscsit_rxpdu_queue_monitor_cv, NULL, CV_DEFAULT, NULL);
284 if ((rc = mod_install(&modlinkage)) != 0) {
285 mutex_destroy(&iscsit_global.global_state_mutex);
286 rw_destroy(&iscsit_global.global_rwlock);
287 return (rc);
290 return (rc);
294 _info(struct modinfo *modinfop)
296 return (mod_info(&modlinkage, modinfop));
300 _fini(void)
302 int rc;
304 rc = mod_remove(&modlinkage);
306 if (rc == 0) {
307 mutex_destroy(&iscsit_rxpdu_queue_monitor_mutex);
308 mutex_destroy(&login_sm_session_mutex);
309 cv_destroy(&iscsit_rxpdu_queue_monitor_cv);
310 mutex_destroy(&iscsit_global.global_state_mutex);
311 rw_destroy(&iscsit_global.global_rwlock);
314 return (rc);
318 * DDI entry points.
321 /* ARGSUSED */
322 static int
323 iscsit_drv_getinfo(dev_info_t *dip, ddi_info_cmd_t cmd, void *arg,
324 void **result)
326 ulong_t instance = getminor((dev_t)arg);
328 switch (cmd) {
329 case DDI_INFO_DEVT2DEVINFO:
330 *result = iscsit_global.global_dip;
331 return (DDI_SUCCESS);
333 case DDI_INFO_DEVT2INSTANCE:
334 *result = (void *)instance;
335 return (DDI_SUCCESS);
337 default:
338 break;
341 return (DDI_FAILURE);
344 static int
345 iscsit_drv_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
347 if (cmd != DDI_ATTACH) {
348 return (DDI_FAILURE);
351 if (ddi_get_instance(dip) != 0) {
352 /* we only allow instance 0 to attach */
353 return (DDI_FAILURE);
356 /* create the minor node */
357 if (ddi_create_minor_node(dip, ISCSIT_MODNAME, S_IFCHR, 0,
358 DDI_PSEUDO, 0) != DDI_SUCCESS) {
359 cmn_err(CE_WARN, "iscsit_drv_attach: "
360 "failed creating minor node");
361 return (DDI_FAILURE);
364 if (iscsit_init(dip) != IDM_STATUS_SUCCESS) {
365 cmn_err(CE_WARN, "iscsit_drv_attach: "
366 "failed to initialize");
367 ddi_remove_minor_node(dip, NULL);
368 return (DDI_FAILURE);
371 iscsit_global.global_svc_state = ISE_DISABLED;
372 iscsit_global.global_dip = dip;
374 return (DDI_SUCCESS);
377 /*ARGSUSED*/
378 static int
379 iscsit_drv_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
381 if (cmd != DDI_DETACH)
382 return (DDI_FAILURE);
385 * drv_detach is called in a context that owns the
386 * device node for the /dev/pseudo device. If this thread blocks
387 * for any resource, other threads that need the /dev/pseudo device
388 * may end up in a deadlock with this thread.Hence, we use a
389 * separate lock just for the structures that drv_detach needs
390 * to access.
392 mutex_enter(&iscsit_global.global_state_mutex);
393 if (iscsit_drv_busy()) {
394 mutex_exit(&iscsit_global.global_state_mutex);
395 return (EBUSY);
398 iscsit_global.global_dip = NULL;
399 ddi_remove_minor_node(dip, NULL);
401 ldi_ident_release(iscsit_global.global_li);
402 iscsit_global.global_svc_state = ISE_DETACHED;
404 mutex_exit(&iscsit_global.global_state_mutex);
406 return (DDI_SUCCESS);
409 /*ARGSUSED*/
410 static int
411 iscsit_drv_open(dev_t *devp, int flag, int otyp, cred_t *credp)
413 return (0);
416 /* ARGSUSED */
417 static int
418 iscsit_drv_close(dev_t dev, int flag, int otyp, cred_t *credp)
420 return (0);
423 static boolean_t
424 iscsit_drv_busy(void)
426 ASSERT(MUTEX_HELD(&iscsit_global.global_state_mutex));
428 switch (iscsit_global.global_svc_state) {
429 case ISE_DISABLED:
430 case ISE_DETACHED:
431 return (B_FALSE);
432 default:
433 return (B_TRUE);
435 /* NOTREACHED */
438 /* ARGSUSED */
439 static int
440 iscsit_drv_ioctl(dev_t drv, int cmd, intptr_t argp, int flag, cred_t *cred,
441 int *retval)
443 iscsit_ioc_set_config_t setcfg;
444 iscsit_ioc_set_config32_t setcfg32;
445 char *cfg_pnvlist = NULL;
446 nvlist_t *cfg_nvlist = NULL;
447 it_config_t *cfg = NULL;
448 idm_status_t idmrc;
449 int rc = 0;
451 if (drv_priv(cred) != 0) {
452 return (EPERM);
455 mutex_enter(&iscsit_global.global_state_mutex);
458 * Validate ioctl requests against global service state
460 switch (iscsit_global.global_svc_state) {
461 case ISE_ENABLED:
462 if (cmd == ISCSIT_IOC_DISABLE_SVC) {
463 iscsit_global.global_svc_state = ISE_DISABLING;
464 } else if (cmd == ISCSIT_IOC_ENABLE_SVC) {
465 /* Already enabled */
466 mutex_exit(&iscsit_global.global_state_mutex);
467 return (0);
468 } else {
469 iscsit_global.global_svc_state = ISE_BUSY;
471 break;
472 case ISE_DISABLED:
473 if (cmd == ISCSIT_IOC_ENABLE_SVC) {
474 iscsit_global.global_svc_state = ISE_ENABLING;
475 } else if (cmd == ISCSIT_IOC_DISABLE_SVC) {
476 /* Already disabled */
477 mutex_exit(&iscsit_global.global_state_mutex);
478 return (0);
479 } else {
480 rc = EFAULT;
482 break;
483 case ISE_BUSY:
484 case ISE_ENABLING:
485 case ISE_DISABLING:
486 rc = EAGAIN;
487 break;
488 case ISE_DETACHED:
489 default:
490 rc = EFAULT;
491 break;
494 mutex_exit(&iscsit_global.global_state_mutex);
495 if (rc != 0)
496 return (rc);
498 /* Handle ioctl request (enable/disable have already been handled) */
499 switch (cmd) {
500 case ISCSIT_IOC_SET_CONFIG:
501 /* Any errors must set state back to ISE_ENABLED */
502 switch (ddi_model_convert_from(flag & FMODELS)) {
503 case DDI_MODEL_ILP32:
504 if (ddi_copyin((void *)argp, &setcfg32,
505 sizeof (iscsit_ioc_set_config32_t), flag) != 0) {
506 rc = EFAULT;
507 goto cleanup;
510 setcfg.set_cfg_pnvlist =
511 (char *)((uintptr_t)setcfg32.set_cfg_pnvlist);
512 setcfg.set_cfg_vers = setcfg32.set_cfg_vers;
513 setcfg.set_cfg_pnvlist_len =
514 setcfg32.set_cfg_pnvlist_len;
515 break;
516 case DDI_MODEL_NONE:
517 if (ddi_copyin((void *)argp, &setcfg,
518 sizeof (iscsit_ioc_set_config_t), flag) != 0) {
519 rc = EFAULT;
520 goto cleanup;
522 break;
523 default:
524 rc = EFAULT;
525 goto cleanup;
528 /* Check API version */
529 if (setcfg.set_cfg_vers != ISCSIT_API_VERS0) {
530 rc = EINVAL;
531 goto cleanup;
534 /* Config is in packed nvlist format so unpack it */
535 cfg_pnvlist = kmem_alloc(setcfg.set_cfg_pnvlist_len,
536 KM_SLEEP);
537 ASSERT(cfg_pnvlist != NULL);
539 if (ddi_copyin(setcfg.set_cfg_pnvlist, cfg_pnvlist,
540 setcfg.set_cfg_pnvlist_len, flag) != 0) {
541 rc = EFAULT;
542 goto cleanup;
545 rc = nvlist_unpack(cfg_pnvlist, setcfg.set_cfg_pnvlist_len,
546 &cfg_nvlist, KM_SLEEP);
547 if (rc != 0) {
548 goto cleanup;
551 /* Translate nvlist */
552 rc = it_nv_to_config(cfg_nvlist, &cfg);
553 if (rc != 0) {
554 cmn_err(CE_WARN, "Configuration is invalid");
555 goto cleanup;
558 /* Update config */
559 rc = iscsit_config_merge(cfg);
560 /* FALLTHROUGH */
562 cleanup:
563 if (cfg)
564 it_config_free_cmn(cfg);
565 if (cfg_pnvlist)
566 kmem_free(cfg_pnvlist, setcfg.set_cfg_pnvlist_len);
567 if (cfg_nvlist)
568 nvlist_free(cfg_nvlist);
571 * Now that the reconfig is complete set our state back to
572 * enabled.
574 mutex_enter(&iscsit_global.global_state_mutex);
575 iscsit_global.global_svc_state = ISE_ENABLED;
576 mutex_exit(&iscsit_global.global_state_mutex);
577 break;
578 case ISCSIT_IOC_ENABLE_SVC: {
579 iscsit_hostinfo_t hostinfo;
581 if (ddi_copyin((void *)argp, &hostinfo.length,
582 sizeof (hostinfo.length), flag) != 0) {
583 mutex_enter(&iscsit_global.global_state_mutex);
584 iscsit_global.global_svc_state = ISE_DISABLED;
585 mutex_exit(&iscsit_global.global_state_mutex);
586 return (EFAULT);
589 if (hostinfo.length > sizeof (hostinfo.fqhn))
590 hostinfo.length = sizeof (hostinfo.fqhn);
592 if (ddi_copyin((void *)((caddr_t)argp +
593 sizeof (hostinfo.length)), &hostinfo.fqhn,
594 hostinfo.length, flag) != 0) {
595 mutex_enter(&iscsit_global.global_state_mutex);
596 iscsit_global.global_svc_state = ISE_DISABLED;
597 mutex_exit(&iscsit_global.global_state_mutex);
598 return (EFAULT);
601 idmrc = iscsit_enable_svc(&hostinfo);
602 mutex_enter(&iscsit_global.global_state_mutex);
603 if (idmrc == IDM_STATUS_SUCCESS) {
604 iscsit_global.global_svc_state = ISE_ENABLED;
605 } else {
606 rc = EIO;
607 iscsit_global.global_svc_state = ISE_DISABLED;
609 mutex_exit(&iscsit_global.global_state_mutex);
610 break;
612 case ISCSIT_IOC_DISABLE_SVC:
613 iscsit_disable_svc();
614 mutex_enter(&iscsit_global.global_state_mutex);
615 iscsit_global.global_svc_state = ISE_DISABLED;
616 mutex_exit(&iscsit_global.global_state_mutex);
617 break;
619 default:
620 rc = EINVAL;
621 mutex_enter(&iscsit_global.global_state_mutex);
622 iscsit_global.global_svc_state = ISE_ENABLED;
623 mutex_exit(&iscsit_global.global_state_mutex);
626 return (rc);
629 static idm_status_t
630 iscsit_init(dev_info_t *dip)
632 int rc;
634 rc = ldi_ident_from_dip(dip, &iscsit_global.global_li);
635 ASSERT(rc == 0); /* Failure indicates invalid argument */
637 iscsit_global.global_svc_state = ISE_DISABLED;
639 return (IDM_STATUS_SUCCESS);
643 * iscsit_enable_svc
645 * registers all the configured targets and target portals with STMF
647 static idm_status_t
648 iscsit_enable_svc(iscsit_hostinfo_t *hostinfo)
650 stmf_port_provider_t *pp;
651 stmf_dbuf_store_t *dbuf_store;
652 boolean_t did_iscsit_isns_init;
653 idm_status_t retval = IDM_STATUS_SUCCESS;
655 ASSERT(iscsit_global.global_svc_state == ISE_ENABLING);
658 * Make sure that can tell if we have partially allocated
659 * in case we need to exit and tear down anything allocated.
661 iscsit_global.global_tsih_pool = NULL;
662 iscsit_global.global_dbuf_store = NULL;
663 iscsit_status_pdu_cache = NULL;
664 pp = NULL;
665 iscsit_global.global_pp = NULL;
666 iscsit_global.global_default_tpg = NULL;
667 did_iscsit_isns_init = B_FALSE;
668 iscsit_global.global_dispatch_taskq = NULL;
670 /* Setup remaining fields in iscsit_global_t */
671 idm_refcnt_init(&iscsit_global.global_refcnt,
672 &iscsit_global);
674 avl_create(&iscsit_global.global_discovery_sessions,
675 iscsit_sess_avl_compare, sizeof (iscsit_sess_t),
676 offsetof(iscsit_sess_t, ist_tgt_ln));
678 avl_create(&iscsit_global.global_target_list,
679 iscsit_tgt_avl_compare, sizeof (iscsit_tgt_t),
680 offsetof(iscsit_tgt_t, target_global_ln));
682 list_create(&iscsit_global.global_deleted_target_list,
683 sizeof (iscsit_tgt_t),
684 offsetof(iscsit_tgt_t, target_global_deleted_ln));
686 avl_create(&iscsit_global.global_tpg_list,
687 iscsit_tpg_avl_compare, sizeof (iscsit_tpg_t),
688 offsetof(iscsit_tpg_t, tpg_global_ln));
690 avl_create(&iscsit_global.global_ini_list,
691 iscsit_ini_avl_compare, sizeof (iscsit_ini_t),
692 offsetof(iscsit_ini_t, ini_global_ln));
694 iscsit_global.global_tsih_pool = vmem_create("iscsit_tsih_pool",
695 (void *)1, ISCSI_MAX_TSIH, 1, NULL, NULL, NULL, 0,
696 VM_SLEEP | VMC_IDENTIFIER);
699 * Setup STMF dbuf store. Our buffers are bound to a specific
700 * connection so we really can't let STMF cache buffers for us.
701 * Consequently we'll just allocate one global buffer store.
703 dbuf_store = stmf_alloc(STMF_STRUCT_DBUF_STORE, 0, 0);
704 if (dbuf_store == NULL) {
705 retval = IDM_STATUS_FAIL;
706 goto tear_down_and_return;
708 dbuf_store->ds_alloc_data_buf = iscsit_dbuf_alloc;
709 dbuf_store->ds_free_data_buf = iscsit_dbuf_free;
710 dbuf_store->ds_port_private = NULL;
711 iscsit_global.global_dbuf_store = dbuf_store;
713 /* Status PDU cache */
714 iscsit_status_pdu_cache = kmem_cache_create("iscsit_status_pdu_cache",
715 sizeof (idm_pdu_t) + sizeof (iscsi_scsi_rsp_hdr_t), 8,
716 &iscsit_status_pdu_constructor,
717 NULL, NULL, NULL, NULL, KM_SLEEP);
719 /* Default TPG and portal */
720 iscsit_global.global_default_tpg = iscsit_tpg_createdefault();
721 if (iscsit_global.global_default_tpg == NULL) {
722 retval = IDM_STATUS_FAIL;
723 goto tear_down_and_return;
726 /* initialize isns client */
727 (void) iscsit_isns_init(hostinfo);
728 did_iscsit_isns_init = B_TRUE;
730 /* Register port provider */
731 pp = stmf_alloc(STMF_STRUCT_PORT_PROVIDER, 0, 0);
732 if (pp == NULL) {
733 retval = IDM_STATUS_FAIL;
734 goto tear_down_and_return;
737 pp->pp_portif_rev = PORTIF_REV_1;
738 pp->pp_instance = 0;
739 pp->pp_name = ISCSIT_MODNAME;
740 pp->pp_cb = iscsit_pp_cb;
742 iscsit_global.global_pp = pp;
745 if (stmf_register_port_provider(pp) != STMF_SUCCESS) {
746 retval = IDM_STATUS_FAIL;
747 goto tear_down_and_return;
750 iscsit_global.global_dispatch_taskq = taskq_create("iscsit_dispatch",
751 1, minclsyspri, 16, 16, TASKQ_PREPOPULATE);
753 /* Scan staged PDUs, meaningful in MC/S situations */
754 iscsit_rxpdu_queue_monitor_start();
756 return (IDM_STATUS_SUCCESS);
758 tear_down_and_return:
760 if (iscsit_global.global_dispatch_taskq) {
761 taskq_destroy(iscsit_global.global_dispatch_taskq);
762 iscsit_global.global_dispatch_taskq = NULL;
765 if (did_iscsit_isns_init)
766 iscsit_isns_fini();
768 if (iscsit_global.global_default_tpg) {
769 iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
770 iscsit_global.global_default_tpg = NULL;
773 if (iscsit_global.global_pp)
774 iscsit_global.global_pp = NULL;
776 if (pp)
777 stmf_free(pp);
779 if (iscsit_status_pdu_cache) {
780 kmem_cache_destroy(iscsit_status_pdu_cache);
781 iscsit_status_pdu_cache = NULL;
784 if (iscsit_global.global_dbuf_store) {
785 stmf_free(iscsit_global.global_dbuf_store);
786 iscsit_global.global_dbuf_store = NULL;
789 if (iscsit_global.global_tsih_pool) {
790 vmem_destroy(iscsit_global.global_tsih_pool);
791 iscsit_global.global_tsih_pool = NULL;
794 avl_destroy(&iscsit_global.global_ini_list);
795 avl_destroy(&iscsit_global.global_tpg_list);
796 list_destroy(&iscsit_global.global_deleted_target_list);
797 avl_destroy(&iscsit_global.global_target_list);
798 avl_destroy(&iscsit_global.global_discovery_sessions);
800 idm_refcnt_destroy(&iscsit_global.global_refcnt);
802 return (retval);
806 * iscsit_disable_svc
808 * clean up all existing connections and deregister targets from STMF
810 static void
811 iscsit_disable_svc(void)
813 iscsit_sess_t *sess;
815 ASSERT(iscsit_global.global_svc_state == ISE_DISABLING);
817 iscsit_rxpdu_queue_monitor_stop();
819 /* tear down discovery sessions */
820 for (sess = avl_first(&iscsit_global.global_discovery_sessions);
821 sess != NULL;
822 sess = AVL_NEXT(&iscsit_global.global_discovery_sessions, sess))
823 iscsit_sess_close(sess);
826 * Passing NULL to iscsit_config_merge tells it to go to an empty
827 * config.
829 (void) iscsit_config_merge(NULL);
832 * Wait until there are no more global references
834 idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
835 idm_refcnt_destroy(&iscsit_global.global_refcnt);
838 * Default TPG must be destroyed after global_refcnt is 0.
840 iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
842 avl_destroy(&iscsit_global.global_discovery_sessions);
843 list_destroy(&iscsit_global.global_deleted_target_list);
844 avl_destroy(&iscsit_global.global_target_list);
845 avl_destroy(&iscsit_global.global_tpg_list);
846 avl_destroy(&iscsit_global.global_ini_list);
848 taskq_destroy(iscsit_global.global_dispatch_taskq);
850 iscsit_isns_fini();
852 stmf_free(iscsit_global.global_dbuf_store);
853 iscsit_global.global_dbuf_store = NULL;
855 (void) stmf_deregister_port_provider(iscsit_global.global_pp);
856 stmf_free(iscsit_global.global_pp);
857 iscsit_global.global_pp = NULL;
859 kmem_cache_destroy(iscsit_status_pdu_cache);
860 iscsit_status_pdu_cache = NULL;
862 vmem_destroy(iscsit_global.global_tsih_pool);
863 iscsit_global.global_tsih_pool = NULL;
866 void
867 iscsit_global_hold()
870 * To take out a global hold, we must either own the global
871 * state mutex or we must be running inside of an ioctl that
872 * has set the global state to ISE_BUSY, ISE_DISABLING, or
873 * ISE_ENABLING. We don't track the "owner" for these flags,
874 * so just checking if they are set is enough for now.
876 ASSERT((iscsit_global.global_svc_state == ISE_ENABLING) ||
877 (iscsit_global.global_svc_state == ISE_DISABLING) ||
878 (iscsit_global.global_svc_state == ISE_BUSY) ||
879 MUTEX_HELD(&iscsit_global.global_state_mutex));
881 idm_refcnt_hold(&iscsit_global.global_refcnt);
884 void
885 iscsit_global_rele()
887 idm_refcnt_rele(&iscsit_global.global_refcnt);
890 void
891 iscsit_global_wait_ref()
893 idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
897 * IDM callbacks
900 /*ARGSUSED*/
901 void
902 iscsit_rx_pdu(idm_conn_t *ic, idm_pdu_t *rx_pdu)
904 iscsit_conn_t *ict = ic->ic_handle;
905 switch (IDM_PDU_OPCODE(rx_pdu)) {
906 case ISCSI_OP_SCSI_CMD:
907 ASSERT(0); /* Shouldn't happen */
908 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
909 break;
910 case ISCSI_OP_SNACK_CMD:
912 * We'll need to handle this when we support ERL1/2. For
913 * now we treat it as a protocol error.
915 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
916 idm_conn_event(ic, CE_TRANSPORT_FAIL, 0);
917 break;
918 case ISCSI_OP_SCSI_TASK_MGT_MSG:
919 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
920 iscsit_set_cmdsn(ict, rx_pdu);
921 iscsit_op_scsi_task_mgmt(ict, rx_pdu);
923 break;
924 case ISCSI_OP_NOOP_OUT:
925 case ISCSI_OP_LOGIN_CMD:
926 case ISCSI_OP_TEXT_CMD:
927 case ISCSI_OP_LOGOUT_CMD:
929 * If/when we switch to userland processing these PDU's
930 * will be handled by iscsitd.
932 iscsit_deferred_dispatch(rx_pdu);
933 break;
934 default:
935 /* Protocol error */
936 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
937 idm_conn_event(ic, CE_TRANSPORT_FAIL, 0);
938 break;
942 /*ARGSUSED*/
943 void
944 iscsit_rx_pdu_error(idm_conn_t *ic, idm_pdu_t *rx_pdu, idm_status_t status)
946 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
950 * iscsit_rx_scsi_rsp -- cause the connection to be closed if response rx'd
952 * A target sends an SCSI Response PDU, it should never receive one.
953 * This has been seen when running the Codemonicon suite of tests which
954 * does negative testing of the protocol. If such a condition occurs using
955 * a normal initiator it most likely means there's data corruption in the
956 * header and that's grounds for dropping the connection as well.
958 void
959 iscsit_rx_scsi_rsp(idm_conn_t *ic, idm_pdu_t *rx_pdu)
961 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
962 idm_conn_event(ic, CE_TRANSPORT_FAIL, 0);
965 void
966 iscsit_task_aborted(idm_task_t *idt, idm_status_t status)
968 iscsit_task_t *itask = idt->idt_private;
970 switch (status) {
971 case IDM_STATUS_SUSPENDED:
972 break;
973 case IDM_STATUS_ABORTED:
974 mutex_enter(&itask->it_mutex);
975 itask->it_aborted = B_TRUE;
977 * We rely on the fact that STMF tracks outstanding
978 * buffer transfers and will free all of our buffers
979 * before freeing the task so we don't need to
980 * explicitly free the buffers from iscsit/idm
982 if (itask->it_stmf_abort) {
983 mutex_exit(&itask->it_mutex);
985 * Task is no longer active
987 iscsit_task_done(itask);
990 * STMF has already asked for this task to be aborted
992 * STMF specification is wrong... says to return
993 * STMF_ABORTED, the code actually looks for
994 * STMF_ABORT_SUCCESS.
996 stmf_task_lport_aborted_unlocked(itask->it_stmf_task,
997 STMF_ABORT_SUCCESS, STMF_IOF_LPORT_DONE);
998 return;
999 } else {
1000 mutex_exit(&itask->it_mutex);
1002 * Tell STMF to stop processing the task.
1004 stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
1005 STMF_ABORTED, NULL);
1006 return;
1008 /*NOTREACHED*/
1009 default:
1010 ASSERT(0);
1014 /*ARGSUSED*/
1015 idm_status_t
1016 iscsit_client_notify(idm_conn_t *ic, idm_client_notify_t icn,
1017 uintptr_t data)
1019 idm_status_t rc = IDM_STATUS_SUCCESS;
1022 * IDM client notifications will never occur at interrupt level
1023 * since they are generated from the connection state machine which
1024 * running on taskq threads.
1027 switch (icn) {
1028 case CN_CONNECT_ACCEPT:
1029 rc = iscsit_conn_accept(ic); /* No data */
1030 break;
1031 case CN_FFP_ENABLED:
1032 rc = iscsit_ffp_enabled(ic); /* No data */
1033 break;
1034 case CN_FFP_DISABLED:
1036 * Data indicates whether this was the result of an
1037 * explicit logout request.
1039 rc = iscsit_ffp_disabled(ic, (idm_ffp_disable_t)data);
1040 break;
1041 case CN_CONNECT_LOST:
1042 rc = iscsit_conn_lost(ic);
1043 break;
1044 case CN_CONNECT_DESTROY:
1045 rc = iscsit_conn_destroy(ic);
1046 break;
1047 case CN_LOGIN_FAIL:
1049 * Force the login state machine to completion
1051 rc = iscsit_login_fail(ic);
1052 break;
1053 default:
1054 rc = IDM_STATUS_REJECT;
1055 break;
1058 return (rc);
1062 * iscsit_update_statsn is invoked for all the PDUs which have the StatSN
1063 * field in the header. The StatSN is incremented if the IDM_PDU_ADVANCE_STATSN
1064 * flag is set in the pdu flags field. The StatSN is connection-wide and is
1065 * protected by the mutex ict_statsn_mutex. For Data-In PDUs, if the flag
1066 * IDM_TASK_PHASECOLLAPSE_REQ is set, the status (phase-collapse) is also filled
1068 void
1069 iscsit_update_statsn(idm_task_t *idm_task, idm_pdu_t *pdu)
1071 iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1072 iscsit_conn_t *ict = (iscsit_conn_t *)pdu->isp_ic->ic_handle;
1073 iscsit_task_t *itask = NULL;
1074 scsi_task_t *task = NULL;
1076 mutex_enter(&ict->ict_statsn_mutex);
1077 rsp->statsn = htonl(ict->ict_statsn);
1078 if (pdu->isp_flags & IDM_PDU_ADVANCE_STATSN)
1079 ict->ict_statsn++;
1080 mutex_exit(&ict->ict_statsn_mutex);
1083 * The last SCSI Data PDU passed for a command may also contain the
1084 * status if the status indicates termination with no expections, i.e.
1085 * no sense data or response involved. If the command completes with
1086 * an error, then the response and sense data will be sent in a
1087 * separate iSCSI Response PDU.
1089 if ((idm_task) && (idm_task->idt_flags & IDM_TASK_PHASECOLLAPSE_REQ)) {
1090 itask = idm_task->idt_private;
1091 task = itask->it_stmf_task;
1093 rsp->cmd_status = task->task_scsi_status;
1094 rsp->flags |= ISCSI_FLAG_DATA_STATUS;
1095 if (task->task_status_ctrl & TASK_SCTRL_OVER) {
1096 rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
1097 } else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
1098 rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
1100 rsp->residual_count = htonl(task->task_resid);
1103 * Removing the task from the session task list
1104 * just before the status is sent in the last
1105 * Data PDU transfer
1107 iscsit_task_done(itask);
1111 void
1112 iscsit_build_hdr(idm_task_t *idm_task, idm_pdu_t *pdu, uint8_t opcode)
1114 iscsit_task_t *itask = idm_task->idt_private;
1115 iscsi_data_rsp_hdr_t *dh = (iscsi_data_rsp_hdr_t *)pdu->isp_hdr;
1118 * We acquired iscsit_sess_t.ist_sn_mutex in iscsit_xfer_scsi_data
1120 ASSERT(MUTEX_HELD(&itask->it_ict->ict_sess->ist_sn_mutex));
1122 * On incoming data, the target transfer tag and Lun is only
1123 * provided by the target if the A bit is set, Since the target
1124 * does not currently support Error Recovery Level 1, the A
1125 * bit is never set.
1127 dh->opcode = opcode;
1128 dh->itt = itask->it_itt;
1129 dh->ttt = ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_SCSI_DATA_RSP) ?
1130 ISCSI_RSVD_TASK_TAG : itask->it_ttt;
1132 dh->expcmdsn = htonl(itask->it_ict->ict_sess->ist_expcmdsn);
1133 dh->maxcmdsn = htonl(itask->it_ict->ict_sess->ist_maxcmdsn);
1136 * IDM must set:
1138 * data.flags and rtt.flags
1139 * data.dlength
1140 * data.datasn
1141 * data.offset
1142 * statsn, residual_count and cmd_status (for phase collapse)
1143 * rtt.rttsn
1144 * rtt.data_offset
1145 * rtt.data_length
1149 void
1150 iscsit_keepalive(idm_conn_t *ic)
1152 idm_pdu_t *nop_in_pdu;
1153 iscsi_nop_in_hdr_t *nop_in;
1154 iscsit_conn_t *ict = ic->ic_handle;
1157 * IDM noticed the connection has been idle for too long so it's
1158 * time to provoke some activity. Build and transmit an iSCSI
1159 * nop-in PDU -- when the initiator responds it will be counted
1160 * as "activity" and keep the connection alive.
1162 * We don't actually care about the response here at the iscsit level
1163 * so we will just throw it away without looking at it when it arrives.
1165 nop_in_pdu = idm_pdu_alloc(sizeof (*nop_in), 0);
1166 idm_pdu_init(nop_in_pdu, ic, NULL, NULL);
1167 nop_in = (iscsi_nop_in_hdr_t *)nop_in_pdu->isp_hdr;
1168 bzero(nop_in, sizeof (*nop_in));
1169 nop_in->opcode = ISCSI_OP_NOOP_IN;
1170 nop_in->flags = ISCSI_FLAG_FINAL;
1171 nop_in->itt = ISCSI_RSVD_TASK_TAG;
1173 * When the target sends a NOP-In as a Ping, the target transfer tag
1174 * is set to a valid (not reserved) value and the initiator task tag
1175 * is set to ISCSI_RSVD_TASK_TAG (0xffffffff). In this case the StatSN
1176 * will always contain the next sequence number but the StatSN for the
1177 * connection is not advanced after this PDU is sent.
1179 nop_in_pdu->isp_flags |= IDM_PDU_SET_STATSN;
1181 * This works because we don't currently allocate ttt's anywhere else
1182 * in iscsit so as long as we stay out of IDM's range we are safe.
1183 * If we need to allocate ttt's for other PDU's in the future this will
1184 * need to be improved.
1186 mutex_enter(&ict->ict_mutex);
1187 nop_in->ttt = ict->ict_keepalive_ttt;
1188 ict->ict_keepalive_ttt++;
1189 if (ict->ict_keepalive_ttt == ISCSI_RSVD_TASK_TAG)
1190 ict->ict_keepalive_ttt = IDM_TASKIDS_MAX;
1191 mutex_exit(&ict->ict_mutex);
1193 iscsit_pdu_tx(nop_in_pdu);
1196 static idm_status_t
1197 iscsit_conn_accept(idm_conn_t *ic)
1199 iscsit_conn_t *ict;
1202 * We need to get a global hold here to ensure that the service
1203 * doesn't get shutdown prior to establishing a session. This
1204 * gets released in iscsit_conn_destroy().
1206 mutex_enter(&iscsit_global.global_state_mutex);
1207 if (iscsit_global.global_svc_state != ISE_ENABLED) {
1208 mutex_exit(&iscsit_global.global_state_mutex);
1209 return (IDM_STATUS_FAIL);
1211 iscsit_global_hold();
1212 mutex_exit(&iscsit_global.global_state_mutex);
1215 * Allocate an associated iscsit structure to represent this
1216 * connection. We shouldn't really create a session until we
1217 * get the first login PDU.
1219 ict = kmem_zalloc(sizeof (*ict), KM_SLEEP);
1221 ict->ict_ic = ic;
1222 ict->ict_statsn = 1;
1223 ict->ict_keepalive_ttt = IDM_TASKIDS_MAX; /* Avoid IDM TT range */
1224 ic->ic_handle = ict;
1225 mutex_init(&ict->ict_mutex, NULL, MUTEX_DRIVER, NULL);
1226 mutex_init(&ict->ict_statsn_mutex, NULL, MUTEX_DRIVER, NULL);
1227 idm_refcnt_init(&ict->ict_refcnt, ict);
1228 idm_refcnt_init(&ict->ict_dispatch_refcnt, ict);
1231 * Initialize login state machine
1233 if (iscsit_login_sm_init(ict) != IDM_STATUS_SUCCESS) {
1234 iscsit_global_rele();
1236 * Cleanup the ict after idm notifies us about this failure
1238 return (IDM_STATUS_FAIL);
1241 return (IDM_STATUS_SUCCESS);
1244 idm_status_t
1245 iscsit_conn_reinstate(iscsit_conn_t *reinstate_ict, iscsit_conn_t *new_ict)
1247 idm_status_t result;
1250 * Note in new connection state that this connection is
1251 * reinstating an existing connection.
1253 new_ict->ict_reinstating = B_TRUE;
1254 new_ict->ict_reinstate_conn = reinstate_ict;
1255 new_ict->ict_statsn = reinstate_ict->ict_statsn;
1258 * Now generate connection state machine event to existing connection
1259 * so that it starts the cleanup process.
1261 result = idm_conn_reinstate_event(reinstate_ict->ict_ic,
1262 new_ict->ict_ic);
1264 return (result);
1267 void
1268 iscsit_conn_hold(iscsit_conn_t *ict)
1270 idm_refcnt_hold(&ict->ict_refcnt);
1273 void
1274 iscsit_conn_rele(iscsit_conn_t *ict)
1276 idm_refcnt_rele(&ict->ict_refcnt);
1279 void
1280 iscsit_conn_dispatch_hold(iscsit_conn_t *ict)
1282 idm_refcnt_hold(&ict->ict_dispatch_refcnt);
1285 void
1286 iscsit_conn_dispatch_rele(iscsit_conn_t *ict)
1288 idm_refcnt_rele(&ict->ict_dispatch_refcnt);
1291 static idm_status_t
1292 iscsit_login_fail(idm_conn_t *ic)
1294 iscsit_conn_t *ict = ic->ic_handle;
1296 /* Generate login state machine event */
1297 iscsit_login_sm_event(ict, ILE_LOGIN_CONN_ERROR, NULL);
1299 return (IDM_STATUS_SUCCESS);
1302 static idm_status_t
1303 iscsit_ffp_enabled(idm_conn_t *ic)
1305 iscsit_conn_t *ict = ic->ic_handle;
1307 /* Generate session state machine event */
1308 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_LOGGED_IN, ict);
1310 return (IDM_STATUS_SUCCESS);
1313 static idm_status_t
1314 iscsit_ffp_disabled(idm_conn_t *ic, idm_ffp_disable_t disable_class)
1316 iscsit_conn_t *ict = ic->ic_handle;
1318 /* Generate session state machine event */
1319 switch (disable_class) {
1320 case FD_CONN_FAIL:
1321 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_FAIL, ict);
1322 break;
1323 case FD_CONN_LOGOUT:
1324 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_DISABLE, ict);
1325 break;
1326 case FD_SESS_LOGOUT:
1327 iscsit_sess_sm_event(ict->ict_sess, SE_SESSION_CLOSE, ict);
1328 break;
1329 default:
1330 ASSERT(0);
1333 return (IDM_STATUS_SUCCESS);
1336 static idm_status_t
1337 iscsit_conn_lost(idm_conn_t *ic)
1339 iscsit_conn_t *ict = ic->ic_handle;
1340 iscsit_sess_t *ist = ict->ict_sess;
1341 iscsit_cbuf_t *cbuf;
1342 idm_pdu_t *rx_pdu;
1343 int i;
1345 mutex_enter(&ict->ict_mutex);
1346 ict->ict_lost = B_TRUE;
1347 mutex_exit(&ict->ict_mutex);
1349 * scrub the staging queue for all PDUs on this connection
1351 if (ist != NULL) {
1352 mutex_enter(&ist->ist_sn_mutex);
1353 for (cbuf = ist->ist_rxpdu_queue, i = 0;
1354 ((cbuf->cb_num_elems > 0) && (i < ISCSIT_RXPDU_QUEUE_LEN));
1355 i++) {
1356 if (((rx_pdu = cbuf->cb_buffer[i]) != NULL) &&
1357 (rx_pdu->isp_ic == ic)) {
1358 /* conn is lost, drop the pdu */
1359 DTRACE_PROBE3(scrubbing__staging__queue,
1360 iscsit_sess_t *, ist, idm_conn_t *, ic,
1361 idm_pdu_t *, rx_pdu);
1362 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
1363 cbuf->cb_buffer[i] = NULL;
1364 cbuf->cb_num_elems--;
1365 iscsit_conn_dispatch_rele(ict);
1368 mutex_exit(&ist->ist_sn_mutex);
1371 * Make sure there aren't any PDU's transitioning from the receive
1372 * handler to the dispatch taskq.
1374 if (idm_refcnt_is_held(&ict->ict_dispatch_refcnt) < 0) {
1375 cmn_err(CE_WARN, "Possible hang in iscsit_conn_lost");
1377 idm_refcnt_wait_ref(&ict->ict_dispatch_refcnt);
1379 return (IDM_STATUS_SUCCESS);
1382 static idm_status_t
1383 iscsit_conn_destroy(idm_conn_t *ic)
1385 iscsit_conn_t *ict = ic->ic_handle;
1387 mutex_enter(&ict->ict_mutex);
1388 ict->ict_destroyed = B_TRUE;
1389 mutex_exit(&ict->ict_mutex);
1391 /* Generate session state machine event */
1392 if (ict->ict_sess != NULL) {
1393 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FAIL, ict);
1396 idm_refcnt_wait_ref(&ict->ict_dispatch_refcnt);
1397 idm_refcnt_wait_ref(&ict->ict_refcnt);
1399 * The session state machine does not need to post
1400 * events to IDM any longer, so it is safe to set
1401 * the idm connection reference to NULL
1403 ict->ict_ic = NULL;
1405 /* Reap the login state machine */
1406 iscsit_login_sm_fini(ict);
1408 /* Clean up any text command remnants */
1409 iscsit_text_cmd_fini(ict);
1411 mutex_destroy(&ict->ict_mutex);
1412 idm_refcnt_destroy(&ict->ict_dispatch_refcnt);
1413 idm_refcnt_destroy(&ict->ict_refcnt);
1414 kmem_free(ict, sizeof (*ict));
1416 iscsit_global_rele();
1418 return (IDM_STATUS_SUCCESS);
1421 void
1422 iscsit_conn_logout(iscsit_conn_t *ict)
1425 * If the iscsi connection is active, then
1426 * logout the IDM connection by sending a
1427 * CE_LOGOUT_SESSION_SUCCESS, else, no action
1428 * needs to be taken because the connection
1429 * is already in the teardown process.
1431 mutex_enter(&ict->ict_mutex);
1432 if (ict->ict_lost == B_FALSE && ict->ict_destroyed == B_FALSE) {
1433 idm_conn_event(ict->ict_ic, CE_LOGOUT_SESSION_SUCCESS, 0);
1435 mutex_exit(&ict->ict_mutex);
1439 * STMF-related functions
1441 * iSCSI to STMF mapping
1443 * Session == ?
1444 * Connection == bound to local port but not itself a local port
1445 * Target
1446 * Target portal (group?) == local port (really but we're not going to do this)
1447 * iscsit needs to map connections to local ports (whatever we decide
1448 * they are)
1449 * Target == ?
1452 /*ARGSUSED*/
1453 static stmf_data_buf_t *
1454 iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
1455 uint32_t flags)
1457 iscsit_task_t *itask = task->task_port_private;
1458 idm_buf_t *idm_buffer;
1459 iscsit_buf_t *ibuf;
1460 stmf_data_buf_t *result;
1461 uint32_t bsize;
1464 * If the requested size is larger than MaxBurstLength and the
1465 * given pminsize is also larger than MaxBurstLength, then the
1466 * allocation fails (dbuf = NULL) and pminsize is modified to
1467 * be equal to MaxBurstLength. stmf/sbd then should re-invoke
1468 * this function with the corrected values for transfer.
1470 ASSERT(pminsize);
1471 if (size <= itask->it_ict->ict_op.op_max_burst_length) {
1472 bsize = size;
1473 } else if (*pminsize <= itask->it_ict->ict_op.op_max_burst_length) {
1474 bsize = itask->it_ict->ict_op.op_max_burst_length;
1475 } else {
1476 *pminsize = itask->it_ict->ict_op.op_max_burst_length;
1477 return (NULL);
1480 /* Alloc buffer */
1481 idm_buffer = idm_buf_alloc(itask->it_ict->ict_ic, NULL, bsize);
1482 if (idm_buffer != NULL) {
1483 result = stmf_alloc(STMF_STRUCT_DATA_BUF,
1484 sizeof (iscsit_buf_t), 0);
1485 if (result != NULL) {
1486 /* Fill in stmf_data_buf_t */
1487 ibuf = result->db_port_private;
1488 ibuf->ibuf_idm_buf = idm_buffer;
1489 ibuf->ibuf_stmf_buf = result;
1490 ibuf->ibuf_is_immed = B_FALSE;
1491 result->db_flags = DB_DONT_CACHE;
1492 result->db_buf_size = bsize;
1493 result->db_data_size = bsize;
1494 result->db_sglist_length = 1;
1495 result->db_sglist[0].seg_addr = idm_buffer->idb_buf;
1496 result->db_sglist[0].seg_length =
1497 idm_buffer->idb_buflen;
1498 return (result);
1501 /* Couldn't get the stmf_data_buf_t so free the buffer */
1502 idm_buf_free(idm_buffer);
1505 return (NULL);
1508 /*ARGSUSED*/
1509 static void
1510 iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf)
1512 iscsit_buf_t *ibuf = dbuf->db_port_private;
1514 if (ibuf->ibuf_is_immed) {
1516 * The iscsit_buf_t structure itself will be freed with its
1517 * associated task. Here we just need to free the PDU that
1518 * held the immediate data.
1520 idm_pdu_complete(ibuf->ibuf_immed_data_pdu, IDM_STATUS_SUCCESS);
1521 ibuf->ibuf_immed_data_pdu = 0;
1522 } else {
1523 idm_buf_free(ibuf->ibuf_idm_buf);
1524 stmf_free(dbuf);
1528 /*ARGSUSED*/
1529 stmf_status_t
1530 iscsit_xfer_scsi_data(scsi_task_t *task, stmf_data_buf_t *dbuf,
1531 uint32_t ioflags)
1533 iscsit_task_t *iscsit_task = task->task_port_private;
1534 iscsit_sess_t *ict_sess = iscsit_task->it_ict->ict_sess;
1535 iscsit_buf_t *ibuf = dbuf->db_port_private;
1536 int idm_rc;
1539 * If we are aborting then we can ignore this request
1541 if (iscsit_task->it_stmf_abort) {
1542 return (STMF_SUCCESS);
1546 * If it's not immediate data then start the transfer
1548 if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
1549 if (ibuf->ibuf_is_immed)
1550 return (iscsit_idm_to_stmf(IDM_STATUS_SUCCESS));
1552 * The DB_SEND_STATUS_GOOD flag in the STMF data buffer allows
1553 * the port provider to phase-collapse, i.e. send the status
1554 * along with the final data PDU for the command. The port
1555 * provider passes this request to the transport layer by
1556 * setting a flag IDM_TASK_PHASECOLLAPSE_REQ in the task.
1558 if (dbuf->db_flags & DB_SEND_STATUS_GOOD)
1559 iscsit_task->it_idm_task->idt_flags |=
1560 IDM_TASK_PHASECOLLAPSE_REQ;
1562 * IDM will call iscsit_build_hdr so lock now to serialize
1563 * access to the SN values. We need to lock here to enforce
1564 * lock ordering
1566 mutex_enter(&ict_sess->ist_sn_mutex);
1567 idm_rc = idm_buf_tx_to_ini(iscsit_task->it_idm_task,
1568 ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1569 dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1570 mutex_exit(&ict_sess->ist_sn_mutex);
1572 return (iscsit_idm_to_stmf(idm_rc));
1573 } else if (dbuf->db_flags & DB_DIRECTION_FROM_RPORT) {
1574 ASSERT(ibuf->ibuf_is_immed == B_FALSE);
1575 /* Grab the SN lock (see comment above) */
1576 mutex_enter(&ict_sess->ist_sn_mutex);
1577 idm_rc = idm_buf_rx_from_ini(iscsit_task->it_idm_task,
1578 ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1579 dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1580 mutex_exit(&ict_sess->ist_sn_mutex);
1582 return (iscsit_idm_to_stmf(idm_rc));
1585 /* What are we supposed to do if there is no direction? */
1586 return (STMF_INVALID_ARG);
1589 static void
1590 iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status)
1592 iscsit_task_t *itask = idb->idb_task_binding->idt_private;
1593 stmf_data_buf_t *dbuf = idb->idb_cb_arg;
1595 dbuf->db_xfer_status = iscsit_idm_to_stmf(status);
1598 * If the task has been aborted then we don't need to call STMF
1600 if (itask->it_stmf_abort) {
1601 return;
1605 * For ISCSI over TCP (not iSER), the last SCSI Data PDU passed
1606 * for a successful command contains the status as requested by
1607 * by COMSTAR (via the DB_SEND_STATUS_GOOD flag). But the iSER
1608 * transport does not support phase-collapse. So pretend we are
1609 * COMSTAR and send the status in a separate PDU now.
1611 if (idb->idb_task_binding->idt_flags & IDM_TASK_PHASECOLLAPSE_SUCCESS) {
1613 * Mark task complete and notify COMSTAR
1614 * that the status has been sent.
1616 itask->it_idm_task->idt_state = TASK_COMPLETE;
1617 stmf_send_status_done(itask->it_stmf_task,
1618 iscsit_idm_to_stmf(status), STMF_IOF_LPORT_DONE);
1619 } else if ((dbuf->db_flags & DB_SEND_STATUS_GOOD) &&
1620 status == IDM_STATUS_SUCCESS) {
1623 * The iscsi target port provider - for iSER, emulates the
1624 * DB_SEND_STATUS_GOOD optimization if requested by STMF;
1625 * it sends the status in a separate PDU after the data
1626 * transfer. In this case the port provider should first
1627 * call stmf_data_xfer_done() to mark the transfer complete
1628 * and then send the status. Although STMF will free the
1629 * buffer at the time the task is freed, even if the transfer
1630 * is not marked complete, this behavior makes statistics
1631 * gathering and task state tracking more difficult than it
1632 * needs to be.
1634 stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
1635 if (iscsit_send_scsi_status(itask->it_stmf_task, 0)
1636 != STMF_SUCCESS) {
1637 stmf_send_status_done(itask->it_stmf_task,
1638 STMF_FAILURE, STMF_IOF_LPORT_DONE);
1640 } else {
1641 stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
1642 /* don't touch dbuf after stmf_data_xfer_done */
1647 /*ARGSUSED*/
1648 stmf_status_t
1649 iscsit_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
1651 iscsit_task_t *itask = task->task_port_private;
1652 iscsi_scsi_rsp_hdr_t *rsp;
1653 idm_pdu_t *pdu;
1654 int resp_datalen;
1657 * If this task is aborted then we don't need to respond.
1659 if (itask->it_stmf_abort) {
1660 return (STMF_SUCCESS);
1664 * If this is a task management status, handle it elsewhere.
1666 if (task->task_mgmt_function != TM_NONE) {
1668 * Don't wait for the PDU completion to tell STMF
1669 * the task is done -- it doesn't really matter and
1670 * it makes life complicated if STMF later asks us to
1671 * abort the request and we don't know whether the
1672 * status has been sent or not.
1674 itask->it_tm_responded = B_TRUE;
1675 iscsit_send_task_mgmt_resp(itask->it_tm_pdu,
1676 (task->task_completion_status == STMF_SUCCESS) ?
1677 SCSI_TCP_TM_RESP_COMPLETE : SCSI_TCP_TM_RESP_FUNC_NOT_SUPP);
1678 stmf_send_status_done(task, STMF_SUCCESS,
1679 STMF_IOF_LPORT_DONE);
1680 return (STMF_SUCCESS);
1684 * Remove the task from the session task list
1686 iscsit_task_done(itask);
1689 * Send status
1691 mutex_enter(&itask->it_idm_task->idt_mutex);
1692 if ((itask->it_idm_task->idt_state == TASK_ACTIVE) &&
1693 (task->task_completion_status == STMF_SUCCESS) &&
1694 (task->task_sense_length == 0) &&
1695 (task->task_resid == 0)) {
1696 itask->it_idm_task->idt_state = TASK_COMPLETE;
1697 /* PDU callback releases task hold */
1698 idm_task_hold(itask->it_idm_task);
1699 mutex_exit(&itask->it_idm_task->idt_mutex);
1701 * Fast path. Cached status PDU's are already
1702 * initialized. We just need to fill in
1703 * connection and task information. StatSN is
1704 * incremented by 1 for every status sent a
1705 * connection.
1707 pdu = kmem_cache_alloc(iscsit_status_pdu_cache, KM_SLEEP);
1708 pdu->isp_ic = itask->it_ict->ict_ic;
1709 pdu->isp_private = itask;
1710 pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1712 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1713 rsp->itt = itask->it_itt;
1715 * ExpDataSN is the number of R2T and Data-In (read)
1716 * PDUs the target has sent for the SCSI command.
1718 * Since there is no support for bidirectional transfer
1719 * yet, either idt_exp_datasn or idt_exp_rttsn, but not
1720 * both is valid at any time
1722 rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
1723 htonl(itask->it_idm_task->idt_exp_datasn):
1724 htonl(itask->it_idm_task->idt_exp_rttsn);
1725 rsp->cmd_status = task->task_scsi_status;
1726 iscsit_pdu_tx(pdu);
1727 return (STMF_SUCCESS);
1728 } else {
1729 if (itask->it_idm_task->idt_state != TASK_ACTIVE) {
1730 mutex_exit(&itask->it_idm_task->idt_mutex);
1731 return (STMF_FAILURE);
1733 itask->it_idm_task->idt_state = TASK_COMPLETE;
1734 /* PDU callback releases task hold */
1735 idm_task_hold(itask->it_idm_task);
1736 mutex_exit(&itask->it_idm_task->idt_mutex);
1738 resp_datalen = (task->task_sense_length == 0) ? 0 :
1739 (task->task_sense_length + sizeof (uint16_t));
1741 pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
1742 idm_pdu_init(pdu, itask->it_ict->ict_ic, itask,
1743 iscsit_send_status_done);
1744 pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1746 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1747 bzero(rsp, sizeof (*rsp));
1748 rsp->opcode = ISCSI_OP_SCSI_RSP;
1750 rsp->flags = ISCSI_FLAG_FINAL;
1751 if (task->task_status_ctrl & TASK_SCTRL_OVER) {
1752 rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
1753 } else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
1754 rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
1757 rsp->bi_residual_count = 0;
1758 rsp->residual_count = htonl(task->task_resid);
1759 rsp->itt = itask->it_itt;
1760 rsp->response = ISCSI_STATUS_CMD_COMPLETED;
1761 rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
1762 htonl(itask->it_idm_task->idt_exp_datasn):
1763 htonl(itask->it_idm_task->idt_exp_rttsn);
1764 rsp->cmd_status = task->task_scsi_status;
1765 if (task->task_sense_length != 0) {
1767 * Add a byte to provide the sense length in
1768 * the response
1770 *(uint16_t *)((void *)pdu->isp_data) =
1771 htons(task->task_sense_length);
1772 bcopy(task->task_sense_data,
1773 (uint8_t *)pdu->isp_data +
1774 sizeof (uint16_t),
1775 task->task_sense_length);
1776 hton24(rsp->dlength, resp_datalen);
1779 DTRACE_PROBE5(iscsi__scsi__response,
1780 iscsit_conn_t *, itask->it_ict,
1781 uint8_t, rsp->response,
1782 uint8_t, rsp->cmd_status,
1783 idm_pdu_t *, pdu,
1784 scsi_task_t *, task);
1786 iscsit_pdu_tx(pdu);
1788 return (STMF_SUCCESS);
1792 /*ARGSUSED*/
1793 static void
1794 iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status)
1796 iscsit_task_t *itask;
1797 boolean_t aborted;
1799 itask = pdu->isp_private;
1800 aborted = itask->it_stmf_abort;
1803 * After releasing the hold the task may be freed at any time so
1804 * don't touch it.
1806 idm_task_rele(itask->it_idm_task);
1807 if (!aborted) {
1808 stmf_send_status_done(itask->it_stmf_task,
1809 iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1811 kmem_cache_free(iscsit_status_pdu_cache, pdu);
1814 /*ARGSUSED*/
1815 static void
1816 iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status)
1818 iscsit_task_t *itask;
1819 boolean_t aborted;
1821 itask = pdu->isp_private;
1822 aborted = itask->it_stmf_abort;
1825 * After releasing the hold the task may be freed at any time so
1826 * don't touch it.
1828 idm_task_rele(itask->it_idm_task);
1829 if (!aborted) {
1830 stmf_send_status_done(itask->it_stmf_task,
1831 iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1833 idm_pdu_free(pdu);
1837 void
1838 iscsit_lport_task_free(scsi_task_t *task)
1840 iscsit_task_t *itask = task->task_port_private;
1842 /* We only call idm_task_start for regular tasks, not task management */
1843 if (task->task_mgmt_function == TM_NONE) {
1844 idm_task_done(itask->it_idm_task);
1845 iscsit_task_free(itask);
1846 return;
1847 } else {
1848 iscsit_tm_task_free(itask);
1852 /*ARGSUSED*/
1853 stmf_status_t
1854 iscsit_abort(stmf_local_port_t *lport, int abort_cmd, void *arg, uint32_t flags)
1856 scsi_task_t *st = (scsi_task_t *)arg;
1857 iscsit_task_t *iscsit_task;
1858 idm_task_t *idt;
1861 * If this is a task management request then there's really not much to
1862 * do.
1864 if (st->task_mgmt_function != TM_NONE) {
1865 return (STMF_ABORT_SUCCESS);
1869 * Regular task, start cleaning up
1871 iscsit_task = st->task_port_private;
1872 idt = iscsit_task->it_idm_task;
1873 mutex_enter(&iscsit_task->it_mutex);
1874 iscsit_task->it_stmf_abort = B_TRUE;
1875 if (iscsit_task->it_aborted) {
1876 mutex_exit(&iscsit_task->it_mutex);
1878 * Task is no longer active
1880 iscsit_task_done(iscsit_task);
1883 * STMF specification is wrong... says to return
1884 * STMF_ABORTED, the code actually looks for
1885 * STMF_ABORT_SUCCESS.
1887 return (STMF_ABORT_SUCCESS);
1888 } else {
1889 mutex_exit(&iscsit_task->it_mutex);
1891 * Call IDM to abort the task. Due to a variety of
1892 * circumstances the task may already be in the process of
1893 * aborting.
1895 return (idm_task_abort(idt->idt_ic, idt, AT_TASK_MGMT_ABORT));
1898 /*NOTREACHED*/
1901 /*ARGSUSED*/
1902 void
1903 iscsit_ctl(stmf_local_port_t *lport, int cmd, void *arg)
1905 iscsit_tgt_t *iscsit_tgt;
1907 ASSERT((cmd == STMF_CMD_LPORT_ONLINE) ||
1908 (cmd == STMF_ACK_LPORT_ONLINE_COMPLETE) ||
1909 (cmd == STMF_CMD_LPORT_OFFLINE) ||
1910 (cmd == STMF_ACK_LPORT_OFFLINE_COMPLETE));
1912 iscsit_tgt = (iscsit_tgt_t *)lport->lport_port_private;
1914 switch (cmd) {
1915 case STMF_CMD_LPORT_ONLINE:
1916 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_REQ);
1917 break;
1918 case STMF_CMD_LPORT_OFFLINE:
1919 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_REQ);
1920 break;
1921 case STMF_ACK_LPORT_ONLINE_COMPLETE:
1922 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_COMPLETE_ACK);
1923 break;
1924 case STMF_ACK_LPORT_OFFLINE_COMPLETE:
1925 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_COMPLETE_ACK);
1926 break;
1928 default:
1929 break;
1933 static stmf_status_t
1934 iscsit_idm_to_stmf(idm_status_t idmrc)
1936 switch (idmrc) {
1937 case IDM_STATUS_SUCCESS:
1938 return (STMF_SUCCESS);
1939 default:
1940 return (STMF_FAILURE);
1942 /*NOTREACHED*/
1945 void
1946 iscsit_op_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu)
1948 iscsit_conn_t *ict = ic->ic_handle;
1950 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
1951 iscsit_post_scsi_cmd(ic, rx_pdu);
1953 iscsit_process_pdu_in_queue(ict->ict_sess);
1956 static int
1957 iscsit_validate_idm_pdu(idm_pdu_t *rx_pdu)
1959 iscsi_scsi_cmd_hdr_t *iscsi_scsi =
1960 (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
1962 if ((iscsi_scsi->scb[0] == SCMD_READ) ||
1963 (iscsi_scsi->scb[0] == SCMD_READ_G1) ||
1964 (iscsi_scsi->scb[0] == SCMD_READ_G4)) {
1965 if (iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE)
1966 return (IDM_STATUS_FAIL);
1968 return (IDM_STATUS_SUCCESS);
1972 * ISCSI protocol
1975 void
1976 iscsit_post_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu)
1978 iscsit_conn_t *ict;
1979 iscsit_task_t *itask;
1980 scsi_task_t *task;
1981 iscsit_buf_t *ibuf;
1982 iscsi_scsi_cmd_hdr_t *iscsi_scsi =
1983 (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
1984 iscsi_addl_hdr_t *ahs_hdr;
1985 uint16_t addl_cdb_len = 0;
1987 ict = ic->ic_handle;
1988 if (iscsit_validate_idm_pdu(rx_pdu) != IDM_STATUS_SUCCESS) {
1989 /* Finish processing request */
1990 iscsit_set_cmdsn(ict, rx_pdu);
1992 iscsit_send_direct_scsi_resp(ict, rx_pdu,
1993 ISCSI_STATUS_CMD_COMPLETED, STATUS_CHECK);
1994 idm_pdu_complete(rx_pdu, IDM_STATUS_PROTOCOL_ERROR);
1995 return;
1998 itask = iscsit_task_alloc(ict);
1999 if (itask == NULL) {
2000 /* Finish processing request */
2001 iscsit_set_cmdsn(ict, rx_pdu);
2003 iscsit_send_direct_scsi_resp(ict, rx_pdu,
2004 ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
2005 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2006 return;
2010 * Note CmdSN and ITT in task. IDM will have already validated this
2011 * request against the connection state so we don't need to check
2012 * that (the connection may have changed state in the meantime but
2013 * we will catch that when we try to send a response)
2015 itask->it_cmdsn = ntohl(iscsi_scsi->cmdsn);
2016 itask->it_itt = iscsi_scsi->itt;
2019 * Check for extended CDB AHS
2021 if (iscsi_scsi->hlength > 0) {
2022 ahs_hdr = (iscsi_addl_hdr_t *)iscsi_scsi;
2023 addl_cdb_len = ((ahs_hdr->ahs_hlen_hi << 8) |
2024 ahs_hdr->ahs_hlen_lo) - 1; /* Adjust for reserved byte */
2025 if (((addl_cdb_len + 4) / sizeof (uint32_t)) >
2026 iscsi_scsi->hlength) {
2027 /* Mangled header info, drop it */
2028 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2029 return;
2033 ict = rx_pdu->isp_ic->ic_handle; /* IDM client private */
2036 * Add task to session list. This function will also check to
2037 * ensure that the task does not already exist.
2039 if (iscsit_task_start(itask) != IDM_STATUS_SUCCESS) {
2041 * Task exists, free all resources and reject. Don't
2042 * update expcmdsn in this case because RFC 3720 says
2043 * "The CmdSN of the rejected command PDU (if it is a
2044 * non-immediate command) MUST NOT be considered received
2045 * by the target (i.e., a command sequence gap must be
2046 * assumed for the CmdSN), even though the CmdSN of the
2047 * rejected command PDU may be reliably ascertained. Upon
2048 * receiving the Reject, the initiator MUST plug the CmdSN
2049 * gap in order to continue to use the session. The gap
2050 * may be plugged either by transmitting a command PDU
2051 * with the same CmdSN, or by aborting the task (see section
2052 * 6.9 on how an abort may plug a CmdSN gap)." (Section 6.3)
2054 iscsit_task_free(itask);
2055 iscsit_send_reject(ict, rx_pdu, ISCSI_REJECT_TASK_IN_PROGRESS);
2056 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2057 return;
2060 /* Update sequence numbers */
2061 iscsit_set_cmdsn(ict, rx_pdu);
2064 * Allocate STMF task
2066 itask->it_stmf_task = stmf_task_alloc(
2067 itask->it_ict->ict_sess->ist_lport,
2068 itask->it_ict->ict_sess->ist_stmf_sess, iscsi_scsi->lun,
2069 16 + addl_cdb_len, 0);
2070 if (itask->it_stmf_task == NULL) {
2072 * Either stmf really couldn't get memory for a task or,
2073 * more likely, the LU is currently in reset. Either way
2074 * we have no choice but to fail the request.
2076 iscsit_task_done(itask);
2077 iscsit_task_free(itask);
2078 iscsit_send_direct_scsi_resp(ict, rx_pdu,
2079 ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
2080 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2081 return;
2084 task = itask->it_stmf_task;
2085 task->task_port_private = itask;
2087 bcopy(iscsi_scsi->lun, task->task_lun_no, sizeof (task->task_lun_no));
2090 * iSCSI and Comstar use the same values. Should we rely on this
2091 * or translate them bit-wise?
2094 task->task_flags =
2095 (((iscsi_scsi->flags & ISCSI_FLAG_CMD_READ) ? TF_READ_DATA : 0) |
2096 ((iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ? TF_WRITE_DATA : 0) |
2097 ((rx_pdu->isp_datalen == 0) ? 0 : TF_INITIAL_BURST));
2099 switch (iscsi_scsi->flags & ISCSI_FLAG_CMD_ATTR_MASK) {
2100 case ISCSI_ATTR_UNTAGGED:
2101 break;
2102 case ISCSI_ATTR_SIMPLE:
2103 task->task_additional_flags |= TF_ATTR_SIMPLE_QUEUE;
2104 break;
2105 case ISCSI_ATTR_ORDERED:
2106 task->task_additional_flags |= TF_ATTR_ORDERED_QUEUE;
2107 break;
2108 case ISCSI_ATTR_HEAD_OF_QUEUE:
2109 task->task_additional_flags |= TF_ATTR_HEAD_OF_QUEUE;
2110 break;
2111 case ISCSI_ATTR_ACA:
2112 task->task_additional_flags |= TF_ATTR_ACA;
2113 break;
2114 default:
2115 /* Protocol error but just take it, treat as untagged */
2116 break;
2120 task->task_additional_flags = 0;
2121 task->task_priority = 0;
2122 task->task_mgmt_function = TM_NONE;
2125 * This "task_max_nbufs" doesn't map well to BIDI. We probably need
2126 * parameter for each direction. "MaxOutstandingR2T" may very well
2127 * be set to one which could prevent us from doing simultaneous
2128 * transfers in each direction.
2130 task->task_max_nbufs = (iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ?
2131 ict->ict_op.op_max_outstanding_r2t : STMF_BUFS_MAX;
2132 task->task_cmd_seq_no = ntohl(iscsi_scsi->itt);
2133 task->task_expected_xfer_length = ntohl(iscsi_scsi->data_length);
2135 /* Copy CDB */
2136 bcopy(iscsi_scsi->scb, task->task_cdb, 16);
2137 if (addl_cdb_len > 0) {
2138 bcopy(ahs_hdr->ahs_extscb, task->task_cdb + 16, addl_cdb_len);
2141 DTRACE_ISCSI_3(scsi__command, idm_conn_t *, ic,
2142 iscsi_scsi_cmd_hdr_t *, (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr,
2143 scsi_task_t *, task);
2146 * Copy the transport header into the task handle from the PDU
2147 * handle. The transport header describes this task's remote tagged
2148 * buffer.
2150 if (rx_pdu->isp_transport_hdrlen != 0) {
2151 bcopy(rx_pdu->isp_transport_hdr,
2152 itask->it_idm_task->idt_transport_hdr,
2153 rx_pdu->isp_transport_hdrlen);
2157 * Tell IDM about our new active task
2159 idm_task_start(itask->it_idm_task, (uintptr_t)itask->it_itt);
2162 * If we have any immediate data then setup the immediate buffer
2163 * context that comes with the task
2165 if (rx_pdu->isp_datalen) {
2166 ibuf = itask->it_immed_data;
2167 ibuf->ibuf_immed_data_pdu = rx_pdu;
2168 ibuf->ibuf_stmf_buf->db_data_size = rx_pdu->isp_datalen;
2169 ibuf->ibuf_stmf_buf->db_buf_size = rx_pdu->isp_datalen;
2170 ibuf->ibuf_stmf_buf->db_relative_offset = 0;
2171 ibuf->ibuf_stmf_buf->db_sglist[0].seg_length =
2172 rx_pdu->isp_datalen;
2173 ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr = rx_pdu->isp_data;
2175 DTRACE_ISCSI_8(xfer__start, idm_conn_t *, ic,
2176 uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
2177 uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
2178 uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
2179 uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
2182 * For immediate data transfer, there is no callback from
2183 * stmf to indicate that the initial burst of data is
2184 * transferred successfully. In some cases, the task can
2185 * get freed before execution returns from stmf_post_task.
2186 * Although this xfer-start/done probe accurately tracks
2187 * the size of the transfer, it does only provide a best
2188 * effort on the timing of the transfer.
2190 DTRACE_ISCSI_8(xfer__done, idm_conn_t *, ic,
2191 uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
2192 uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
2193 uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
2194 uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
2195 stmf_post_task(task, ibuf->ibuf_stmf_buf);
2196 } else {
2198 stmf_post_task(task, NULL);
2199 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2203 void
2204 iscsit_deferred_dispatch(idm_pdu_t *rx_pdu)
2206 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
2209 * If this isn't a login packet, we need a session. Otherwise
2210 * this is a protocol error (perhaps one IDM should've caught?).
2212 if (IDM_PDU_OPCODE(rx_pdu) != ISCSI_OP_LOGIN_CMD &&
2213 ict->ict_sess == NULL) {
2214 DTRACE_PROBE2(iscsi__idm__deferred__no__session,
2215 iscsit_conn_t *, ict, idm_pdu_t *, rx_pdu);
2216 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2217 return;
2221 * If the connection has been lost then ignore new PDU's
2223 mutex_enter(&ict->ict_mutex);
2224 if (ict->ict_lost) {
2225 mutex_exit(&ict->ict_mutex);
2226 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2227 return;
2231 * Grab a hold on the connection to prevent it from going away
2232 * between now and when the taskq function is called.
2234 iscsit_conn_dispatch_hold(ict);
2235 mutex_exit(&ict->ict_mutex);
2237 taskq_dispatch_ent(iscsit_global.global_dispatch_taskq,
2238 iscsit_deferred, rx_pdu, 0, &rx_pdu->isp_tqent);
2241 static void
2242 iscsit_deferred(void *rx_pdu_void)
2244 idm_pdu_t *rx_pdu = rx_pdu_void;
2245 idm_conn_t *ic = rx_pdu->isp_ic;
2246 iscsit_conn_t *ict = ic->ic_handle;
2249 * NOP and Task Management Commands can be marked for immediate
2250 * delivery. Commands marked as 'Immediate' are to be considered
2251 * for execution as soon as they arrive on the target. So these
2252 * should not be checked for sequence order and put in a queue.
2253 * The CmdSN is not advanced for Immediate Commands.
2255 switch (IDM_PDU_OPCODE(rx_pdu)) {
2256 case ISCSI_OP_NOOP_OUT:
2257 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2258 iscsit_set_cmdsn(ict, rx_pdu);
2259 iscsit_pdu_op_noop(ict, rx_pdu);
2261 break;
2262 case ISCSI_OP_LOGIN_CMD:
2263 iscsit_pdu_op_login_cmd(ict, rx_pdu);
2264 iscsit_conn_dispatch_rele(ict);
2265 return;
2266 case ISCSI_OP_TEXT_CMD:
2267 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2268 iscsit_set_cmdsn(ict, rx_pdu);
2269 iscsit_pdu_op_text_cmd(ict, rx_pdu);
2271 break;
2272 case ISCSI_OP_LOGOUT_CMD:
2273 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2274 iscsit_set_cmdsn(ict, rx_pdu);
2275 iscsit_pdu_op_logout_cmd(ict, rx_pdu);
2277 break;
2278 default:
2279 /* Protocol error. IDM should have caught this */
2280 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2281 ASSERT(0);
2282 break;
2285 * Check if there are other PDUs in the session staging queue
2286 * waiting to be posted to SCSI layer.
2288 iscsit_process_pdu_in_queue(ict->ict_sess);
2290 iscsit_conn_dispatch_rele(ict);
2293 static void
2294 iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu,
2295 uint8_t response, uint8_t cmd_status)
2297 idm_pdu_t *rsp_pdu;
2298 idm_conn_t *ic;
2299 iscsi_scsi_rsp_hdr_t *resp;
2300 iscsi_scsi_cmd_hdr_t *req =
2301 (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2303 ic = ict->ict_ic;
2305 rsp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_rsp_hdr_t), 0);
2306 idm_pdu_init(rsp_pdu, ic, NULL, NULL);
2308 * StatSN is incremented by 1 for every response sent on
2309 * a connection except for responses sent as a result of
2310 * a retry or SNACK
2312 rsp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2314 resp = (iscsi_scsi_rsp_hdr_t *)rsp_pdu->isp_hdr;
2316 resp->opcode = ISCSI_OP_SCSI_RSP;
2317 resp->flags = ISCSI_FLAG_FINAL;
2318 resp->response = response;
2319 resp->cmd_status = cmd_status;
2320 resp->itt = req->itt;
2321 if ((response == ISCSI_STATUS_CMD_COMPLETED) &&
2322 (req->data_length != 0) &&
2323 ((req->flags & ISCSI_FLAG_CMD_READ) ||
2324 (req->flags & ISCSI_FLAG_CMD_WRITE))) {
2325 resp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
2326 resp->residual_count = req->data_length;
2329 DTRACE_PROBE4(iscsi__scsi__direct__response,
2330 iscsit_conn_t *, ict,
2331 uint8_t, resp->response,
2332 uint8_t, resp->cmd_status,
2333 idm_pdu_t *, rsp_pdu);
2335 iscsit_pdu_tx(rsp_pdu);
2338 void
2339 iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu, uint8_t tm_status)
2341 iscsi_scsi_task_mgt_rsp_hdr_t *tm_resp;
2344 * The target must take note of the last-sent StatSN.
2345 * The StatSN is to be incremented after sending a
2346 * task management response. Digest recovery can only
2347 * work if StatSN is incremented.
2349 tm_resp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2350 tm_resp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2351 tm_resp->response = tm_status;
2353 DTRACE_PROBE3(iscsi__scsi__tm__response,
2354 iscsit_conn_t *, tm_resp_pdu->isp_ic->ic_handle,
2355 uint8_t, tm_resp->response,
2356 idm_pdu_t *, tm_resp_pdu);
2357 iscsit_pdu_tx(tm_resp_pdu);
2360 void
2361 iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2363 idm_pdu_t *tm_resp_pdu;
2364 iscsit_task_t *itask;
2365 iscsit_task_t *tm_itask;
2366 scsi_task_t *task;
2367 iscsi_scsi_task_mgt_hdr_t *iscsi_tm =
2368 (iscsi_scsi_task_mgt_hdr_t *)rx_pdu->isp_hdr;
2369 iscsi_scsi_task_mgt_rsp_hdr_t *iscsi_tm_rsp =
2370 (iscsi_scsi_task_mgt_rsp_hdr_t *)rx_pdu->isp_hdr;
2371 uint32_t rtt, cmdsn, refcmdsn;
2372 uint8_t tm_func;
2375 * Setup response PDU (response field will get filled in later)
2377 tm_resp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_task_mgt_rsp_hdr_t), 0);
2378 if (tm_resp_pdu == NULL) {
2379 /* Can't respond, just drop it */
2380 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2381 return;
2383 idm_pdu_init(tm_resp_pdu, ict->ict_ic, NULL, NULL);
2384 iscsi_tm_rsp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2385 bzero(iscsi_tm_rsp, sizeof (iscsi_scsi_task_mgt_rsp_hdr_t));
2386 iscsi_tm_rsp->opcode = ISCSI_OP_SCSI_TASK_MGT_RSP;
2387 iscsi_tm_rsp->flags = ISCSI_FLAG_FINAL;
2388 iscsi_tm_rsp->itt = rx_pdu->isp_hdr->itt;
2391 * Figure out what we're being asked to do.
2393 DTRACE_PROBE4(iscsi__scsi__tm__request,
2394 iscsit_conn_t *, ict,
2395 uint8_t, (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK),
2396 uint32_t, iscsi_tm->rtt,
2397 idm_pdu_t *, rx_pdu);
2398 switch (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK) {
2399 case ISCSI_TM_FUNC_ABORT_TASK:
2401 * STMF doesn't currently support the "abort task" task
2402 * management command although it does support aborting
2403 * an individual task. We'll get STMF to abort the task
2404 * for us but handle the details of the task management
2405 * command ourselves.
2407 * Find the task associated with the referenced task tag.
2409 rtt = iscsi_tm->rtt;
2410 itask = (iscsit_task_t *)idm_task_find_by_handle(ict->ict_ic,
2411 (uintptr_t)rtt);
2413 if (itask == NULL) {
2414 cmdsn = ntohl(iscsi_tm->cmdsn);
2415 refcmdsn = ntohl(iscsi_tm->refcmdsn);
2418 * Task was not found. But the SCSI command could be
2419 * on the rxpdu wait queue. If RefCmdSN is within
2420 * the CmdSN window and less than CmdSN of the TM
2421 * function, return "Function Complete". Otherwise,
2422 * return "Task Does Not Exist".
2425 if (iscsit_cmdsn_in_window(ict, refcmdsn) &&
2426 iscsit_sna_lt(refcmdsn, cmdsn)) {
2427 mutex_enter(&ict->ict_sess->ist_sn_mutex);
2428 if (iscsit_remove_pdu_from_queue(
2429 ict->ict_sess, refcmdsn)) {
2430 iscsit_conn_dispatch_rele(ict);
2432 mutex_exit(&ict->ict_sess->ist_sn_mutex);
2433 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2434 SCSI_TCP_TM_RESP_COMPLETE);
2435 } else {
2436 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2437 SCSI_TCP_TM_RESP_NO_TASK);
2439 } else {
2442 * Tell STMF to abort the task. This will do no harm
2443 * if the task is already complete.
2445 stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
2446 STMF_ABORTED, NULL);
2449 * Make sure the task hasn't already completed
2451 mutex_enter(&itask->it_idm_task->idt_mutex);
2452 if ((itask->it_idm_task->idt_state == TASK_COMPLETE) ||
2453 (itask->it_idm_task->idt_state == TASK_IDLE)) {
2455 * Task is complete, return "Task Does Not
2456 * Exist"
2458 mutex_exit(&itask->it_idm_task->idt_mutex);
2459 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2460 SCSI_TCP_TM_RESP_NO_TASK);
2461 } else {
2463 * STMF is now aborting the task, return
2464 * "Function Complete"
2466 mutex_exit(&itask->it_idm_task->idt_mutex);
2467 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2468 SCSI_TCP_TM_RESP_COMPLETE);
2470 idm_task_rele(itask->it_idm_task);
2472 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2473 return;
2475 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2476 tm_func = TM_ABORT_TASK_SET;
2477 break;
2479 case ISCSI_TM_FUNC_CLEAR_ACA:
2480 tm_func = TM_CLEAR_ACA;
2481 break;
2483 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2484 tm_func = TM_CLEAR_TASK_SET;
2485 break;
2487 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2488 tm_func = TM_LUN_RESET;
2489 break;
2491 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2492 tm_func = TM_TARGET_WARM_RESET;
2493 break;
2495 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2496 tm_func = TM_TARGET_COLD_RESET;
2497 break;
2499 case ISCSI_TM_FUNC_TASK_REASSIGN:
2501 * We do not currently support allegiance reassignment. When
2502 * we start supporting ERL1+, we will need to.
2504 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2505 SCSI_TCP_TM_RESP_NO_ALLG_REASSN);
2506 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2507 return;
2509 default:
2510 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2511 SCSI_TCP_TM_RESP_REJECTED);
2512 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2513 return;
2516 tm_itask = iscsit_tm_task_alloc(ict);
2517 if (tm_itask == NULL) {
2518 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2519 SCSI_TCP_TM_RESP_REJECTED);
2520 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2521 return;
2525 task = stmf_task_alloc(ict->ict_sess->ist_lport,
2526 ict->ict_sess->ist_stmf_sess, iscsi_tm->lun,
2527 0, STMF_TASK_EXT_NONE);
2528 if (task == NULL) {
2530 * If this happens, either the LU is in reset, couldn't
2531 * get memory, or some other condition in which we simply
2532 * can't complete this request. It would be nice to return
2533 * an error code like "busy" but the closest we have is
2534 * "rejected".
2536 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2537 SCSI_TCP_TM_RESP_REJECTED);
2538 iscsit_tm_task_free(tm_itask);
2539 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2540 return;
2543 tm_itask->it_tm_pdu = tm_resp_pdu;
2544 tm_itask->it_stmf_task = task;
2545 task->task_port_private = tm_itask;
2546 task->task_mgmt_function = tm_func;
2547 task->task_additional_flags = TASK_AF_NO_EXPECTED_XFER_LENGTH;
2548 task->task_priority = 0;
2549 task->task_max_nbufs = STMF_BUFS_MAX;
2550 task->task_cmd_seq_no = iscsi_tm->itt;
2551 task->task_expected_xfer_length = 0;
2553 stmf_post_task(task, NULL);
2554 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2557 static void
2558 iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2560 iscsi_nop_out_hdr_t *out = (iscsi_nop_out_hdr_t *)rx_pdu->isp_hdr;
2561 iscsi_nop_in_hdr_t *in;
2562 int resp_datalen;
2563 idm_pdu_t *resp;
2565 /* Ignore the response from initiator */
2566 if ((out->itt == ISCSI_RSVD_TASK_TAG) ||
2567 (out->ttt != ISCSI_RSVD_TASK_TAG)) {
2568 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2569 return;
2572 /* Allocate a PDU to respond */
2573 resp_datalen = ntoh24(out->dlength);
2574 resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
2575 idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
2576 if (resp_datalen > 0) {
2577 bcopy(rx_pdu->isp_data, resp->isp_data, resp_datalen);
2581 * When sending a NOP-In as a response to a NOP-Out from the initiator,
2582 * the target must respond with the same initiator task tag that was
2583 * provided in the NOP-Out request, the target transfer tag must be
2584 * ISCSI_RSVD_TASK_TAG (0xffffffff) and StatSN will contain the next
2585 * status sequence number. The StatSN for the connection is advanced
2586 * after this PDU is sent.
2588 in = (iscsi_nop_in_hdr_t *)resp->isp_hdr;
2589 bzero(in, sizeof (*in));
2590 in->opcode = ISCSI_OP_NOOP_IN;
2591 in->flags = ISCSI_FLAG_FINAL;
2592 bcopy(out->lun, in->lun, 8);
2593 in->itt = out->itt;
2594 in->ttt = ISCSI_RSVD_TASK_TAG;
2595 hton24(in->dlength, resp_datalen);
2596 resp->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2597 /* Any other field in resp to be set? */
2598 iscsit_pdu_tx(resp);
2599 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2602 static void
2603 iscsit_pdu_op_login_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2607 * Submit PDU to login state machine. State machine will free the
2608 * PDU.
2610 iscsit_login_sm_event(ict, ILE_LOGIN_RCV, rx_pdu);
2613 void
2614 iscsit_pdu_op_logout_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2616 iscsi_logout_hdr_t *logout_req =
2617 (iscsi_logout_hdr_t *)rx_pdu->isp_hdr;
2618 iscsi_logout_rsp_hdr_t *logout_rsp;
2619 idm_pdu_t *resp;
2621 /* Allocate a PDU to respond */
2622 resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2623 idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
2625 * The StatSN is to be sent to the initiator,
2626 * it is not required to increment the number
2627 * as the connection is terminating.
2629 resp->isp_flags |= IDM_PDU_SET_STATSN;
2631 * Logout results in the immediate termination of all tasks except
2632 * if the logout reason is ISCSI_LOGOUT_REASON_RECOVERY. The
2633 * connection state machine will drive this task cleanup automatically
2634 * so we don't need to handle that here.
2636 logout_rsp = (iscsi_logout_rsp_hdr_t *)resp->isp_hdr;
2637 bzero(logout_rsp, sizeof (*logout_rsp));
2638 logout_rsp->opcode = ISCSI_OP_LOGOUT_RSP;
2639 logout_rsp->flags = ISCSI_FLAG_FINAL;
2640 logout_rsp->itt = logout_req->itt;
2641 if ((logout_req->flags & ISCSI_FLAG_LOGOUT_REASON_MASK) >
2642 ISCSI_LOGOUT_REASON_RECOVERY) {
2643 logout_rsp->response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2644 } else {
2645 logout_rsp->response = ISCSI_LOGOUT_SUCCESS;
2648 iscsit_pdu_tx(resp);
2649 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2653 * Calculate the number of outstanding commands we can process
2656 iscsit_cmd_window()
2659 * Instead of using a pre-defined constant for the command window,
2660 * it should be made confiurable and dynamic. With MC/S, sequence
2661 * numbers will be used up at a much faster rate than with SC/S.
2663 return (ISCSIT_MAX_WINDOW);
2667 * Set local registers based on incoming PDU
2669 void
2670 iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2672 iscsit_sess_t *ist;
2673 iscsi_scsi_cmd_hdr_t *req;
2675 ist = ict->ict_sess;
2677 req = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2678 if (req->opcode & ISCSI_OP_IMMEDIATE) {
2679 /* no cmdsn increment for immediate PDUs */
2680 return;
2683 /* Ensure that the ExpCmdSN advances in an orderly manner */
2684 mutex_enter(&ist->ist_sn_mutex);
2685 ist->ist_expcmdsn = ntohl(req->cmdsn) + 1;
2686 ist->ist_maxcmdsn = ntohl(req->cmdsn) + iscsit_cmd_window();
2687 mutex_exit(&ist->ist_sn_mutex);
2691 * Wrapper funtion, calls iscsi_calc_rspsn and idm_pdu_tx
2693 void
2694 iscsit_pdu_tx(idm_pdu_t *pdu)
2696 iscsit_conn_t *ict = pdu->isp_ic->ic_handle;
2697 iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
2698 iscsit_sess_t *ist = ict->ict_sess;
2701 * The command sequence numbers are session-wide and must stay
2702 * consistent across the transfer, so protect the cmdsn with a
2703 * mutex lock on the session. The status sequence number will
2704 * be updated just before the transport layer transmits the PDU.
2707 mutex_enter(&ict->ict_sess->ist_sn_mutex);
2708 /* Set ExpCmdSN and MaxCmdSN */
2709 rsp->maxcmdsn = htonl(ist->ist_maxcmdsn);
2710 rsp->expcmdsn = htonl(ist->ist_expcmdsn);
2711 idm_pdu_tx(pdu);
2712 mutex_exit(&ict->ict_sess->ist_sn_mutex);
2716 * Internal functions
2719 void
2720 iscsit_send_async_event(iscsit_conn_t *ict, uint8_t event)
2722 idm_pdu_t *abt;
2723 iscsi_async_evt_hdr_t *async_abt;
2726 * Get a PDU to build the abort request.
2728 abt = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2729 if (abt == NULL) {
2730 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, 0);
2731 return;
2735 * A asynchronous message is sent by the target to request a logout.
2736 * The StatSN for the connection is advanced after the PDU is sent
2737 * to allow for initiator and target state synchronization.
2739 idm_pdu_init(abt, ict->ict_ic, NULL, NULL);
2740 abt->isp_datalen = 0;
2741 abt->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2743 async_abt = (iscsi_async_evt_hdr_t *)abt->isp_hdr;
2744 bzero(async_abt, sizeof (*async_abt));
2745 async_abt->opcode = ISCSI_OP_ASYNC_EVENT;
2746 async_abt->async_event = event;
2747 async_abt->flags = ISCSI_FLAG_FINAL;
2748 async_abt->rsvd4[0] = 0xff;
2749 async_abt->rsvd4[1] = 0xff;
2750 async_abt->rsvd4[2] = 0xff;
2751 async_abt->rsvd4[3] = 0xff;
2753 switch (event) {
2754 case ISCSI_ASYNC_EVENT_REQUEST_LOGOUT:
2755 async_abt->param3 = htons(IDM_LOGOUT_SECONDS);
2756 break;
2757 case ISCSI_ASYNC_EVENT_SCSI_EVENT:
2758 case ISCSI_ASYNC_EVENT_DROPPING_CONNECTION:
2759 case ISCSI_ASYNC_EVENT_DROPPING_ALL_CONNECTIONS:
2760 case ISCSI_ASYNC_EVENT_PARAM_NEGOTIATION:
2761 default:
2762 ASSERT(0);
2765 iscsit_pdu_tx(abt);
2768 void
2769 iscsit_send_reject(iscsit_conn_t *ict, idm_pdu_t *rejected_pdu, uint8_t reason)
2771 idm_pdu_t *reject_pdu;
2772 iscsi_reject_rsp_hdr_t *reject;
2775 * Get a PDU to build the abort request.
2777 reject_pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t),
2778 rejected_pdu->isp_hdrlen);
2779 if (reject_pdu == NULL) {
2780 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, 0);
2781 return;
2783 idm_pdu_init(reject_pdu, ict->ict_ic, NULL, NULL);
2784 /* StatSN is advanced after a Reject PDU */
2785 reject_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2786 reject_pdu->isp_datalen = rejected_pdu->isp_hdrlen;
2787 bcopy(rejected_pdu->isp_hdr, reject_pdu->isp_data,
2788 rejected_pdu->isp_hdrlen);
2790 reject = (iscsi_reject_rsp_hdr_t *)reject_pdu->isp_hdr;
2791 bzero(reject, sizeof (*reject));
2792 reject->opcode = ISCSI_OP_REJECT_MSG;
2793 reject->reason = reason;
2794 reject->flags = ISCSI_FLAG_FINAL;
2795 hton24(reject->dlength, rejected_pdu->isp_hdrlen);
2796 reject->must_be_ff[0] = 0xff;
2797 reject->must_be_ff[1] = 0xff;
2798 reject->must_be_ff[2] = 0xff;
2799 reject->must_be_ff[3] = 0xff;
2801 iscsit_pdu_tx(reject_pdu);
2805 static iscsit_task_t *
2806 iscsit_task_alloc(iscsit_conn_t *ict)
2808 iscsit_task_t *itask;
2809 iscsit_buf_t *immed_ibuf;
2812 * Possible items to pre-alloc if we cache iscsit_task_t's:
2814 * Status PDU w/ sense buffer
2815 * stmf_data_buf_t for immediate data
2817 itask = kmem_alloc(sizeof (iscsit_task_t) + sizeof (iscsit_buf_t) +
2818 sizeof (stmf_data_buf_t), KM_NOSLEEP);
2819 if (itask != NULL) {
2820 mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2821 itask->it_aborted = itask->it_stmf_abort =
2822 itask->it_tm_task = 0;
2824 immed_ibuf = (iscsit_buf_t *)(itask + 1);
2825 bzero(immed_ibuf, sizeof (*immed_ibuf));
2826 immed_ibuf->ibuf_is_immed = B_TRUE;
2827 immed_ibuf->ibuf_stmf_buf = (stmf_data_buf_t *)(immed_ibuf + 1);
2829 bzero(immed_ibuf->ibuf_stmf_buf, sizeof (stmf_data_buf_t));
2830 immed_ibuf->ibuf_stmf_buf->db_port_private = immed_ibuf;
2831 immed_ibuf->ibuf_stmf_buf->db_sglist_length = 1;
2832 immed_ibuf->ibuf_stmf_buf->db_flags = DB_DIRECTION_FROM_RPORT |
2833 DB_DONT_CACHE;
2834 itask->it_immed_data = immed_ibuf;
2835 itask->it_idm_task = idm_task_alloc(ict->ict_ic);
2836 if (itask->it_idm_task != NULL) {
2837 itask->it_idm_task->idt_private = itask;
2838 itask->it_ict = ict;
2839 itask->it_ttt = itask->it_idm_task->idt_tt;
2840 return (itask);
2841 } else {
2842 kmem_free(itask, sizeof (iscsit_task_t) +
2843 sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2847 return (NULL);
2850 static void
2851 iscsit_task_free(iscsit_task_t *itask)
2853 idm_task_free(itask->it_idm_task);
2854 mutex_destroy(&itask->it_mutex);
2855 kmem_free(itask, sizeof (iscsit_task_t) +
2856 sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2859 static iscsit_task_t *
2860 iscsit_tm_task_alloc(iscsit_conn_t *ict)
2862 iscsit_task_t *itask;
2864 itask = kmem_zalloc(sizeof (iscsit_task_t), KM_NOSLEEP);
2865 if (itask != NULL) {
2866 idm_conn_hold(ict->ict_ic);
2867 mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2868 itask->it_aborted = itask->it_stmf_abort =
2869 itask->it_tm_responded = 0;
2870 itask->it_tm_pdu = NULL;
2871 itask->it_tm_task = 1;
2872 itask->it_ict = ict;
2875 return (itask);
2878 static void
2879 iscsit_tm_task_free(iscsit_task_t *itask)
2882 * If we responded then the call to idm_pdu_complete will free the
2883 * PDU. Otherwise we got aborted before the TM function could
2884 * complete and we need to free the PDU explicitly.
2886 if (itask->it_tm_pdu != NULL && !itask->it_tm_responded)
2887 idm_pdu_free(itask->it_tm_pdu);
2888 idm_conn_rele(itask->it_ict->ict_ic);
2889 mutex_destroy(&itask->it_mutex);
2890 kmem_free(itask, sizeof (iscsit_task_t));
2893 static idm_status_t
2894 iscsit_task_start(iscsit_task_t *itask)
2896 iscsit_sess_t *ist = itask->it_ict->ict_sess;
2897 avl_index_t where;
2900 * Sanity check the ITT and ensure that this task does not already
2901 * exist. If not then add the task to the session task list.
2903 mutex_enter(&ist->ist_mutex);
2904 mutex_enter(&itask->it_mutex);
2905 itask->it_active = 1;
2906 if (avl_find(&ist->ist_task_list, itask, &where) == NULL) {
2907 /* New task, add to AVL */
2908 avl_insert(&ist->ist_task_list, itask, where);
2909 mutex_exit(&itask->it_mutex);
2910 mutex_exit(&ist->ist_mutex);
2911 return (IDM_STATUS_SUCCESS);
2913 mutex_exit(&itask->it_mutex);
2914 mutex_exit(&ist->ist_mutex);
2916 return (IDM_STATUS_REJECT);
2919 static void
2920 iscsit_task_done(iscsit_task_t *itask)
2922 iscsit_sess_t *ist = itask->it_ict->ict_sess;
2924 mutex_enter(&ist->ist_mutex);
2925 mutex_enter(&itask->it_mutex);
2926 if (itask->it_active) {
2927 avl_remove(&ist->ist_task_list, itask);
2928 itask->it_active = 0;
2930 mutex_exit(&itask->it_mutex);
2931 mutex_exit(&ist->ist_mutex);
2935 * iscsit status PDU cache
2938 /*ARGSUSED*/
2939 static int
2940 iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags)
2942 idm_pdu_t *pdu = pdu_void;
2943 iscsi_scsi_rsp_hdr_t *rsp;
2945 bzero(pdu, sizeof (idm_pdu_t));
2946 pdu->isp_callback = iscsit_send_good_status_done;
2947 pdu->isp_magic = IDM_PDU_MAGIC;
2948 pdu->isp_hdr = (iscsi_hdr_t *)(pdu + 1); /* Ptr arithmetic */
2949 pdu->isp_hdrlen = sizeof (iscsi_hdr_t);
2951 /* Setup status response */
2952 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
2953 bzero(rsp, sizeof (*rsp));
2954 rsp->opcode = ISCSI_OP_SCSI_RSP;
2955 rsp->flags = ISCSI_FLAG_FINAL;
2956 rsp->response = ISCSI_STATUS_CMD_COMPLETED;
2958 return (0);
2962 * iscsit private data handler
2965 /*ARGSUSED*/
2966 static void
2967 iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags)
2969 it_config_t *cfg;
2970 nvlist_t *nvl;
2971 iscsit_service_enabled_t old_state;
2973 if ((cmd != STMF_PROVIDER_DATA_UPDATED) || (arg == NULL)) {
2974 return;
2977 nvl = (nvlist_t *)arg;
2979 /* Translate nvlist */
2980 if (it_nv_to_config(nvl, &cfg) != 0) {
2981 cmn_err(CE_WARN, "Configuration is invalid");
2982 return;
2985 /* Check that no iSCSI ioctl is currently running */
2986 mutex_enter(&iscsit_global.global_state_mutex);
2987 old_state = iscsit_global.global_svc_state;
2988 switch (iscsit_global.global_svc_state) {
2989 case ISE_ENABLED:
2990 case ISE_DISABLED:
2991 iscsit_global.global_svc_state = ISE_BUSY;
2992 break;
2993 case ISE_ENABLING:
2995 * It is OK for the iscsit_pp_cb to be called from inside of
2996 * an iSCSI ioctl only if we are currently executing inside
2997 * of stmf_register_port_provider.
2999 ASSERT((flags & STMF_PCB_PREG_COMPLETE) != 0);
3000 break;
3001 default:
3002 cmn_err(CE_WARN, "iscsit_pp_cb called when global_svc_state"
3003 " is not ENABLED(0x%x) -- ignoring",
3004 iscsit_global.global_svc_state);
3005 mutex_exit(&iscsit_global.global_state_mutex);
3006 it_config_free_cmn(cfg);
3007 return;
3009 mutex_exit(&iscsit_global.global_state_mutex);
3011 /* Update config */
3012 (void) iscsit_config_merge(cfg);
3014 it_config_free_cmn(cfg);
3016 /* Restore old iSCSI driver global state */
3017 mutex_enter(&iscsit_global.global_state_mutex);
3018 ASSERT(iscsit_global.global_svc_state == ISE_BUSY ||
3019 iscsit_global.global_svc_state == ISE_ENABLING);
3020 iscsit_global.global_svc_state = old_state;
3021 mutex_exit(&iscsit_global.global_state_mutex);
3025 static it_cfg_status_t
3026 iscsit_config_merge(it_config_t *in_cfg)
3028 it_cfg_status_t status;
3029 it_config_t *cfg;
3030 it_config_t tmp_cfg;
3031 list_t tpg_del_list;
3033 if (in_cfg) {
3034 cfg = in_cfg;
3035 } else {
3036 /* Make empty config */
3037 bzero(&tmp_cfg, sizeof (tmp_cfg));
3038 cfg = &tmp_cfg;
3041 list_create(&tpg_del_list, sizeof (iscsit_tpg_t),
3042 offsetof(iscsit_tpg_t, tpg_delete_ln));
3045 * Update targets, initiator contexts, target portal groups,
3046 * and iSNS client
3048 ISCSIT_GLOBAL_LOCK(RW_WRITER);
3049 if (((status = iscsit_config_merge_tpg(cfg, &tpg_del_list))
3050 != 0) ||
3051 ((status = iscsit_config_merge_tgt(cfg)) != 0) ||
3052 ((status = iscsit_config_merge_ini(cfg)) != 0) ||
3053 ((status = isnst_config_merge(cfg)) != 0)) {
3054 ISCSIT_GLOBAL_UNLOCK();
3055 return (status);
3058 /* Update other global config parameters */
3059 if (iscsit_global.global_props) {
3060 nvlist_free(iscsit_global.global_props);
3061 iscsit_global.global_props = NULL;
3063 if (in_cfg) {
3064 (void) nvlist_dup(cfg->config_global_properties,
3065 &iscsit_global.global_props, KM_SLEEP);
3067 ISCSIT_GLOBAL_UNLOCK();
3069 iscsit_config_destroy_tpgs(&tpg_del_list);
3071 list_destroy(&tpg_del_list);
3073 return (ITCFG_SUCCESS);
3077 * iscsit_sna_lt[e]
3079 * Compare serial numbers using serial number arithmetic as defined in
3080 * RFC 1982.
3082 * NOTE: This code is duplicated in the isns server. It ought to be common.
3085 static int
3086 iscsit_sna_lt(uint32_t sn1, uint32_t sn2)
3088 return ((sn1 != sn2) &&
3089 (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
3090 ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
3093 static int
3094 iscsit_sna_lte(uint32_t sn1, uint32_t sn2)
3096 return ((sn1 == sn2) ||
3097 (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
3098 ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
3102 static boolean_t
3103 iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn)
3105 iscsit_sess_t *ist = ict->ict_sess;
3106 int rval = B_TRUE;
3108 ist = ict->ict_sess;
3110 mutex_enter(&ist->ist_sn_mutex);
3113 * If cmdsn is less than ist_expcmdsn - iscsit_cmd_window() or
3114 * greater than ist_expcmdsn, it's not in the window.
3117 if (iscsit_sna_lt(cmdsn, (ist->ist_expcmdsn - iscsit_cmd_window())) ||
3118 !iscsit_sna_lte(cmdsn, ist->ist_expcmdsn)) {
3119 rval = B_FALSE;
3122 mutex_exit(&ist->ist_sn_mutex);
3124 return (rval);
3128 * iscsit_check_cmdsn_and_queue
3130 * Independent of the order in which the iSCSI target receives non-immediate
3131 * command PDU across the entire session and any multiple connections within
3132 * the session, the target must deliver the commands to the SCSI layer in
3133 * CmdSN order. So out-of-order non-immediate commands are queued up on a
3134 * session-wide wait queue. Duplicate commands are ignored.
3136 * returns B_TRUE for commands which can be executed immediately (are
3137 * non-deferred), B_FALSE for cases where a command was deferred or invalid.
3139 static boolean_t
3140 iscsit_check_cmdsn_and_queue(idm_pdu_t *rx_pdu)
3142 idm_conn_t *ic = rx_pdu->isp_ic;
3143 iscsit_conn_t *ict = ic->ic_handle;
3144 iscsit_sess_t *ist = ict->ict_sess;
3145 iscsi_scsi_cmd_hdr_t *hdr = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
3147 mutex_enter(&ist->ist_sn_mutex);
3148 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
3149 /* do not queue, handle it immediately */
3150 DTRACE_PROBE2(immediate__cmd, iscsit_sess_t *, ist,
3151 idm_pdu_t *, rx_pdu);
3152 mutex_exit(&ist->ist_sn_mutex);
3153 return (B_TRUE);
3156 * See RFC3270 3.1.1.2: non-immediate commands outside of the
3157 * expected window (from expcmdsn to maxcmdsn, inclusive)
3158 * should be silently ignored.
3160 if (iscsit_sna_lt(ist->ist_expcmdsn, ntohl(hdr->cmdsn)) &&
3161 iscsit_sna_lt(ntohl(hdr->cmdsn), ist->ist_maxcmdsn)) {
3163 * Out-of-order commands (cmdSN higher than ExpCmdSN)
3164 * are staged on a fixed-size circular buffer until
3165 * the missing command is delivered to the SCSI layer.
3166 * Irrespective of the order of insertion into the
3167 * staging queue, the commands are processed out of the
3168 * queue in cmdSN order only.
3170 rx_pdu->isp_queue_time = gethrtime();
3171 iscsit_add_pdu_to_queue(ist, rx_pdu);
3172 mutex_exit(&ist->ist_sn_mutex);
3173 return (B_FALSE);
3174 } else if (iscsit_sna_lt(ntohl(hdr->cmdsn), ist->ist_expcmdsn) ||
3175 iscsit_sna_lt(ist->ist_maxcmdsn, ntohl(hdr->cmdsn))) {
3177 * See above, this command is outside of our acceptable
3178 * window, we need to discard/complete.
3180 DTRACE_PROBE3(cmdsn__lt__expcmdsn, iscsit_sess_t *, ist,
3181 iscsit_conn_t *, ict, idm_pdu_t *, rx_pdu);
3182 mutex_exit(&ist->ist_sn_mutex);
3183 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
3185 * tell our callers that the PDU "finished."
3187 return (B_FALSE);
3188 } else {
3189 mutex_exit(&ist->ist_sn_mutex);
3190 return (B_TRUE);
3195 * iscsit_add_pdu_to_queue() adds PDUs into the array indexed by
3196 * their cmdsn value. The length of the array is kept above the
3197 * maximum window size. The window keeps the cmdsn within a range
3198 * such that there are no collisons. e.g. the assumption is that
3199 * the windowing checks make it impossible to receive PDUs that
3200 * index into the same location in the array.
3202 static void
3203 iscsit_add_pdu_to_queue(iscsit_sess_t *ist, idm_pdu_t *rx_pdu)
3205 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3206 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
3207 uint32_t cmdsn =
3208 ((iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr)->cmdsn;
3209 uint32_t index;
3211 ASSERT(MUTEX_HELD(&ist->ist_sn_mutex));
3213 * If the connection is being torn down, then
3214 * don't add the PDU to the staging queue
3216 mutex_enter(&ict->ict_mutex);
3217 if (ict->ict_lost) {
3218 mutex_exit(&ict->ict_mutex);
3219 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
3220 return;
3222 iscsit_conn_dispatch_hold(ict);
3223 mutex_exit(&ict->ict_mutex);
3225 index = ntohl(cmdsn) % ISCSIT_RXPDU_QUEUE_LEN;
3227 * In the normal case, assuming that the Initiator is not
3228 * buggy and that we don't have packet duplication occuring,
3229 * the entry in the array will be NULL. However, we may have
3230 * received a duplicate PDU with cmdsn > expsn , and in that
3231 * case we just ignore this PDU -- the previously received one
3232 * remains queued for processing. We need to be careful not
3233 * to leak this one however.
3235 if (cbuf->cb_buffer[index] != NULL) {
3236 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
3237 } else {
3238 cbuf->cb_buffer[index] = rx_pdu;
3239 cbuf->cb_num_elems++;
3243 static idm_pdu_t *
3244 iscsit_remove_pdu_from_queue(iscsit_sess_t *ist, uint32_t cmdsn)
3246 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3247 idm_pdu_t *pdu = NULL;
3248 uint32_t index;
3250 ASSERT(MUTEX_HELD(&ist->ist_sn_mutex));
3251 index = cmdsn % ISCSIT_RXPDU_QUEUE_LEN;
3252 if ((pdu = cbuf->cb_buffer[index]) != NULL) {
3253 ASSERT(cmdsn ==
3254 ntohl(((iscsi_scsi_cmd_hdr_t *)pdu->isp_hdr)->cmdsn));
3255 cbuf->cb_buffer[index] = NULL;
3256 cbuf->cb_num_elems--;
3257 return (pdu);
3259 return (NULL);
3263 * iscsit_process_pdu_in_queue() finds the next pdu in sequence
3264 * and posts it to the SCSI layer
3266 static void
3267 iscsit_process_pdu_in_queue(iscsit_sess_t *ist)
3269 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3270 idm_pdu_t *pdu = NULL;
3271 uint32_t expcmdsn;
3273 for (;;) {
3274 mutex_enter(&ist->ist_sn_mutex);
3275 if (cbuf->cb_num_elems == 0) {
3276 mutex_exit(&ist->ist_sn_mutex);
3277 break;
3279 expcmdsn = ist->ist_expcmdsn;
3280 if ((pdu = iscsit_remove_pdu_from_queue(ist, expcmdsn))
3281 == NULL) {
3282 mutex_exit(&ist->ist_sn_mutex);
3283 break;
3285 mutex_exit(&ist->ist_sn_mutex);
3286 iscsit_post_staged_pdu(pdu);
3290 static void
3291 iscsit_post_staged_pdu(idm_pdu_t *rx_pdu)
3293 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
3295 /* Post the PDU to the SCSI layer */
3296 switch (IDM_PDU_OPCODE(rx_pdu)) {
3297 case ISCSI_OP_NOOP_OUT:
3298 iscsit_set_cmdsn(ict, rx_pdu);
3299 iscsit_pdu_op_noop(ict, rx_pdu);
3300 break;
3301 case ISCSI_OP_TEXT_CMD:
3302 iscsit_set_cmdsn(ict, rx_pdu);
3303 iscsit_pdu_op_text_cmd(ict, rx_pdu);
3304 break;
3305 case ISCSI_OP_SCSI_TASK_MGT_MSG:
3306 iscsit_set_cmdsn(ict, rx_pdu);
3307 iscsit_op_scsi_task_mgmt(ict, rx_pdu);
3308 break;
3309 case ISCSI_OP_SCSI_CMD:
3310 /* cmdSN will be incremented after creating itask */
3311 iscsit_post_scsi_cmd(rx_pdu->isp_ic, rx_pdu);
3312 break;
3313 case ISCSI_OP_LOGOUT_CMD:
3314 iscsit_set_cmdsn(ict, rx_pdu);
3315 iscsit_pdu_op_logout_cmd(ict, rx_pdu);
3316 break;
3317 default:
3318 /* No other PDUs should be placed on the queue */
3319 ASSERT(0);
3321 iscsit_conn_dispatch_rele(ict); /* release hold on the conn */
3324 /* ARGSUSED */
3325 void
3326 iscsit_rxpdu_queue_monitor_start(void)
3328 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3329 if (iscsit_rxpdu_queue_monitor_thr_running) {
3330 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3331 return;
3333 iscsit_rxpdu_queue_monitor_thr_id =
3334 thread_create(NULL, 0, iscsit_rxpdu_queue_monitor, NULL,
3335 0, &p0, TS_RUN, minclsyspri);
3336 while (!iscsit_rxpdu_queue_monitor_thr_running) {
3337 cv_wait(&iscsit_rxpdu_queue_monitor_cv,
3338 &iscsit_rxpdu_queue_monitor_mutex);
3340 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3344 /* ARGSUSED */
3345 void
3346 iscsit_rxpdu_queue_monitor_stop(void)
3348 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3349 if (iscsit_rxpdu_queue_monitor_thr_running) {
3350 iscsit_rxpdu_queue_monitor_thr_running = B_FALSE;
3351 cv_signal(&iscsit_rxpdu_queue_monitor_cv);
3352 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3354 thread_join(iscsit_rxpdu_queue_monitor_thr_did);
3355 return;
3357 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3361 * A separate thread is used to scan the staging queue on all the
3362 * sessions, If a delayed PDU does not arrive within a timeout, the
3363 * target will advance to the staged PDU that is next in sequence
3364 * and exceeded the threshold wait time. It is up to the initiator
3365 * to note that the target has not acknowledged a particular cmdsn
3366 * and take appropriate action.
3368 /* ARGSUSED */
3369 static void
3370 iscsit_rxpdu_queue_monitor(void *arg)
3372 iscsit_tgt_t *tgt;
3373 iscsit_sess_t *ist;
3375 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3376 iscsit_rxpdu_queue_monitor_thr_did = curthread->t_did;
3377 iscsit_rxpdu_queue_monitor_thr_running = B_TRUE;
3378 cv_signal(&iscsit_rxpdu_queue_monitor_cv);
3380 while (iscsit_rxpdu_queue_monitor_thr_running) {
3381 ISCSIT_GLOBAL_LOCK(RW_READER);
3382 for (tgt = avl_first(&iscsit_global.global_target_list);
3383 tgt != NULL;
3384 tgt = AVL_NEXT(&iscsit_global.global_target_list, tgt)) {
3385 mutex_enter(&tgt->target_mutex);
3386 for (ist = avl_first(&tgt->target_sess_list);
3387 ist != NULL;
3388 ist = AVL_NEXT(&tgt->target_sess_list, ist)) {
3390 iscsit_rxpdu_queue_monitor_session(ist);
3392 mutex_exit(&tgt->target_mutex);
3394 ISCSIT_GLOBAL_UNLOCK();
3395 if (iscsit_rxpdu_queue_monitor_thr_running == B_FALSE) {
3396 break;
3398 (void) cv_reltimedwait(&iscsit_rxpdu_queue_monitor_cv,
3399 &iscsit_rxpdu_queue_monitor_mutex,
3400 ISCSIT_RXPDU_QUEUE_MONITOR_INTERVAL * drv_usectohz(1000000),
3401 TR_CLOCK_TICK);
3403 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3404 thread_exit();
3407 static void
3408 iscsit_rxpdu_queue_monitor_session(iscsit_sess_t *ist)
3410 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3411 idm_pdu_t *next_pdu = NULL;
3412 uint32_t index, next_cmdsn, i;
3415 * Assume that all PDUs in the staging queue have a cmdsn >= expcmdsn.
3416 * Starting with the expcmdsn, iterate over the staged PDUs to find
3417 * the next PDU with a wait time greater than the threshold. If found
3418 * advance the staged PDU to the SCSI layer, skipping over the missing
3419 * PDU(s) to get past the hole in the command sequence. It is up to
3420 * the initiator to note that the target has not acknowledged a cmdsn
3421 * and take appropriate action.
3423 * Since the PDU(s) arrive in any random order, it is possible that
3424 * that the actual wait time for a particular PDU is much longer than
3425 * the defined threshold. e.g. Consider a case where commands are sent
3426 * over 4 different connections, and cmdsn = 1004 arrives first, then
3427 * 1003, and 1002 and 1001 are lost due to a connection failure.
3428 * So now 1003 is waiting for 1002 to be delivered, and although the
3429 * wait time of 1004 > wait time of 1003, only 1003 will be considered
3430 * by the monitor thread. 1004 will be automatically processed by
3431 * iscsit_process_pdu_in_queue() once the scan is complete and the
3432 * expcmdsn becomes current.
3434 mutex_enter(&ist->ist_sn_mutex);
3435 cbuf = ist->ist_rxpdu_queue;
3436 if (cbuf->cb_num_elems == 0) {
3437 mutex_exit(&ist->ist_sn_mutex);
3438 return;
3440 for (next_pdu = NULL, i = 0; ; i++) {
3441 next_cmdsn = ist->ist_expcmdsn + i; /* start at expcmdsn */
3442 index = next_cmdsn % ISCSIT_RXPDU_QUEUE_LEN;
3443 if ((next_pdu = cbuf->cb_buffer[index]) != NULL) {
3445 * If the PDU wait time has not exceeded threshold
3446 * stop scanning the staging queue until the timer
3447 * fires again
3449 if ((gethrtime() - next_pdu->isp_queue_time)
3450 < (rxpdu_queue_threshold * NANOSEC)) {
3451 mutex_exit(&ist->ist_sn_mutex);
3452 return;
3455 * Remove the next PDU from the queue and post it
3456 * to the SCSI layer, skipping over the missing
3457 * PDU. Stop scanning the staging queue until
3458 * the monitor timer fires again
3460 (void) iscsit_remove_pdu_from_queue(ist, next_cmdsn);
3461 mutex_exit(&ist->ist_sn_mutex);
3462 DTRACE_PROBE3(advanced__to__blocked__cmdsn,
3463 iscsit_sess_t *, ist, idm_pdu_t *, next_pdu,
3464 uint32_t, next_cmdsn);
3465 iscsit_post_staged_pdu(next_pdu);
3466 /* Deliver any subsequent PDUs immediately */
3467 iscsit_process_pdu_in_queue(ist);
3468 return;
3471 * Skipping over i PDUs, e.g. a case where commands 1001 and
3472 * 1002 are lost in the network, skip over both and post 1003
3473 * expcmdsn then becomes 1004 at the end of the scan.
3475 DTRACE_PROBE2(skipping__over__cmdsn, iscsit_sess_t *, ist,
3476 uint32_t, next_cmdsn);
3479 * following the assumption, staged cmdsn >= expcmdsn, this statement
3480 * is never reached.