Merge commit '720b16875295d57e0e6a4e0ec32db4d47412f896'
[unleashed.git] / kernel / comstar / port / iscsit / iscsit.c
blobd13ef341794490ac7fbfcee89539ad40495e78ee
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 2014, 2015 Nexenta Systems, Inc. All rights reserved.
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 nvlist_free(cfg_nvlist);
570 * Now that the reconfig is complete set our state back to
571 * enabled.
573 mutex_enter(&iscsit_global.global_state_mutex);
574 iscsit_global.global_svc_state = ISE_ENABLED;
575 mutex_exit(&iscsit_global.global_state_mutex);
576 break;
577 case ISCSIT_IOC_ENABLE_SVC: {
578 iscsit_hostinfo_t hostinfo;
580 if (ddi_copyin((void *)argp, &hostinfo.length,
581 sizeof (hostinfo.length), flag) != 0) {
582 mutex_enter(&iscsit_global.global_state_mutex);
583 iscsit_global.global_svc_state = ISE_DISABLED;
584 mutex_exit(&iscsit_global.global_state_mutex);
585 return (EFAULT);
588 if (hostinfo.length > sizeof (hostinfo.fqhn))
589 hostinfo.length = sizeof (hostinfo.fqhn);
591 if (ddi_copyin((void *)((caddr_t)argp +
592 sizeof (hostinfo.length)), &hostinfo.fqhn,
593 hostinfo.length, flag) != 0) {
594 mutex_enter(&iscsit_global.global_state_mutex);
595 iscsit_global.global_svc_state = ISE_DISABLED;
596 mutex_exit(&iscsit_global.global_state_mutex);
597 return (EFAULT);
600 idmrc = iscsit_enable_svc(&hostinfo);
601 mutex_enter(&iscsit_global.global_state_mutex);
602 if (idmrc == IDM_STATUS_SUCCESS) {
603 iscsit_global.global_svc_state = ISE_ENABLED;
604 } else {
605 rc = EIO;
606 iscsit_global.global_svc_state = ISE_DISABLED;
608 mutex_exit(&iscsit_global.global_state_mutex);
609 break;
611 case ISCSIT_IOC_DISABLE_SVC:
612 iscsit_disable_svc();
613 mutex_enter(&iscsit_global.global_state_mutex);
614 iscsit_global.global_svc_state = ISE_DISABLED;
615 mutex_exit(&iscsit_global.global_state_mutex);
616 break;
618 default:
619 rc = EINVAL;
620 mutex_enter(&iscsit_global.global_state_mutex);
621 iscsit_global.global_svc_state = ISE_ENABLED;
622 mutex_exit(&iscsit_global.global_state_mutex);
625 return (rc);
628 static idm_status_t
629 iscsit_init(dev_info_t *dip)
631 int rc;
633 rc = ldi_ident_from_dip(dip, &iscsit_global.global_li);
634 ASSERT(rc == 0); /* Failure indicates invalid argument */
636 iscsit_global.global_svc_state = ISE_DISABLED;
638 return (IDM_STATUS_SUCCESS);
642 * iscsit_enable_svc
644 * registers all the configured targets and target portals with STMF
646 static idm_status_t
647 iscsit_enable_svc(iscsit_hostinfo_t *hostinfo)
649 stmf_port_provider_t *pp;
650 stmf_dbuf_store_t *dbuf_store;
651 boolean_t did_iscsit_isns_init;
652 idm_status_t retval = IDM_STATUS_SUCCESS;
654 ASSERT(iscsit_global.global_svc_state == ISE_ENABLING);
657 * Make sure that can tell if we have partially allocated
658 * in case we need to exit and tear down anything allocated.
660 iscsit_global.global_tsih_pool = NULL;
661 iscsit_global.global_dbuf_store = NULL;
662 iscsit_status_pdu_cache = NULL;
663 pp = NULL;
664 iscsit_global.global_pp = NULL;
665 iscsit_global.global_default_tpg = NULL;
666 did_iscsit_isns_init = B_FALSE;
667 iscsit_global.global_dispatch_taskq = NULL;
669 /* Setup remaining fields in iscsit_global_t */
670 idm_refcnt_init(&iscsit_global.global_refcnt,
671 &iscsit_global);
673 avl_create(&iscsit_global.global_discovery_sessions,
674 iscsit_sess_avl_compare, sizeof (iscsit_sess_t),
675 offsetof(iscsit_sess_t, ist_tgt_ln));
677 avl_create(&iscsit_global.global_target_list,
678 iscsit_tgt_avl_compare, sizeof (iscsit_tgt_t),
679 offsetof(iscsit_tgt_t, target_global_ln));
681 list_create(&iscsit_global.global_deleted_target_list,
682 sizeof (iscsit_tgt_t),
683 offsetof(iscsit_tgt_t, target_global_deleted_ln));
685 avl_create(&iscsit_global.global_tpg_list,
686 iscsit_tpg_avl_compare, sizeof (iscsit_tpg_t),
687 offsetof(iscsit_tpg_t, tpg_global_ln));
689 avl_create(&iscsit_global.global_ini_list,
690 iscsit_ini_avl_compare, sizeof (iscsit_ini_t),
691 offsetof(iscsit_ini_t, ini_global_ln));
693 iscsit_global.global_tsih_pool = vmem_create("iscsit_tsih_pool",
694 (void *)1, ISCSI_MAX_TSIH, 1, NULL, NULL, NULL, 0,
695 VM_SLEEP | VMC_IDENTIFIER);
698 * Setup STMF dbuf store. Our buffers are bound to a specific
699 * connection so we really can't let STMF cache buffers for us.
700 * Consequently we'll just allocate one global buffer store.
702 dbuf_store = stmf_alloc(STMF_STRUCT_DBUF_STORE, 0, 0);
703 if (dbuf_store == NULL) {
704 retval = IDM_STATUS_FAIL;
705 goto tear_down_and_return;
707 dbuf_store->ds_alloc_data_buf = iscsit_dbuf_alloc;
708 dbuf_store->ds_free_data_buf = iscsit_dbuf_free;
709 dbuf_store->ds_port_private = NULL;
710 iscsit_global.global_dbuf_store = dbuf_store;
712 /* Status PDU cache */
713 iscsit_status_pdu_cache = kmem_cache_create("iscsit_status_pdu_cache",
714 sizeof (idm_pdu_t) + sizeof (iscsi_scsi_rsp_hdr_t), 8,
715 &iscsit_status_pdu_constructor,
716 NULL, NULL, NULL, NULL, KM_SLEEP);
718 /* Default TPG and portal */
719 iscsit_global.global_default_tpg = iscsit_tpg_createdefault();
720 if (iscsit_global.global_default_tpg == NULL) {
721 retval = IDM_STATUS_FAIL;
722 goto tear_down_and_return;
725 /* initialize isns client */
726 (void) iscsit_isns_init(hostinfo);
727 did_iscsit_isns_init = B_TRUE;
729 /* Register port provider */
730 pp = stmf_alloc(STMF_STRUCT_PORT_PROVIDER, 0, 0);
731 if (pp == NULL) {
732 retval = IDM_STATUS_FAIL;
733 goto tear_down_and_return;
736 pp->pp_portif_rev = PORTIF_REV_1;
737 pp->pp_instance = 0;
738 pp->pp_name = ISCSIT_MODNAME;
739 pp->pp_cb = iscsit_pp_cb;
741 iscsit_global.global_pp = pp;
744 if (stmf_register_port_provider(pp) != STMF_SUCCESS) {
745 retval = IDM_STATUS_FAIL;
746 goto tear_down_and_return;
749 iscsit_global.global_dispatch_taskq = taskq_create("iscsit_dispatch",
750 1, minclsyspri, 16, 16, TASKQ_PREPOPULATE);
752 /* Scan staged PDUs, meaningful in MC/S situations */
753 iscsit_rxpdu_queue_monitor_start();
755 return (IDM_STATUS_SUCCESS);
757 tear_down_and_return:
759 if (iscsit_global.global_dispatch_taskq) {
760 taskq_destroy(iscsit_global.global_dispatch_taskq);
761 iscsit_global.global_dispatch_taskq = NULL;
764 if (did_iscsit_isns_init)
765 iscsit_isns_fini();
767 if (iscsit_global.global_default_tpg) {
768 iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
769 iscsit_global.global_default_tpg = NULL;
772 if (iscsit_global.global_pp)
773 iscsit_global.global_pp = NULL;
775 if (pp)
776 stmf_free(pp);
778 if (iscsit_status_pdu_cache) {
779 kmem_cache_destroy(iscsit_status_pdu_cache);
780 iscsit_status_pdu_cache = NULL;
783 if (iscsit_global.global_dbuf_store) {
784 stmf_free(iscsit_global.global_dbuf_store);
785 iscsit_global.global_dbuf_store = NULL;
788 if (iscsit_global.global_tsih_pool) {
789 vmem_destroy(iscsit_global.global_tsih_pool);
790 iscsit_global.global_tsih_pool = NULL;
793 avl_destroy(&iscsit_global.global_ini_list);
794 avl_destroy(&iscsit_global.global_tpg_list);
795 list_destroy(&iscsit_global.global_deleted_target_list);
796 avl_destroy(&iscsit_global.global_target_list);
797 avl_destroy(&iscsit_global.global_discovery_sessions);
799 idm_refcnt_destroy(&iscsit_global.global_refcnt);
801 return (retval);
805 * iscsit_disable_svc
807 * clean up all existing connections and deregister targets from STMF
809 static void
810 iscsit_disable_svc(void)
812 iscsit_sess_t *sess;
814 ASSERT(iscsit_global.global_svc_state == ISE_DISABLING);
816 iscsit_rxpdu_queue_monitor_stop();
818 /* tear down discovery sessions */
819 for (sess = avl_first(&iscsit_global.global_discovery_sessions);
820 sess != NULL;
821 sess = AVL_NEXT(&iscsit_global.global_discovery_sessions, sess))
822 iscsit_sess_close(sess);
825 * Passing NULL to iscsit_config_merge tells it to go to an empty
826 * config.
828 (void) iscsit_config_merge(NULL);
831 * Wait until there are no more global references
833 idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
834 idm_refcnt_destroy(&iscsit_global.global_refcnt);
837 * Default TPG must be destroyed after global_refcnt is 0.
839 iscsit_tpg_destroydefault(iscsit_global.global_default_tpg);
841 avl_destroy(&iscsit_global.global_discovery_sessions);
842 list_destroy(&iscsit_global.global_deleted_target_list);
843 avl_destroy(&iscsit_global.global_target_list);
844 avl_destroy(&iscsit_global.global_tpg_list);
845 avl_destroy(&iscsit_global.global_ini_list);
847 taskq_destroy(iscsit_global.global_dispatch_taskq);
849 iscsit_isns_fini();
851 stmf_free(iscsit_global.global_dbuf_store);
852 iscsit_global.global_dbuf_store = NULL;
854 (void) stmf_deregister_port_provider(iscsit_global.global_pp);
855 stmf_free(iscsit_global.global_pp);
856 iscsit_global.global_pp = NULL;
858 kmem_cache_destroy(iscsit_status_pdu_cache);
859 iscsit_status_pdu_cache = NULL;
861 vmem_destroy(iscsit_global.global_tsih_pool);
862 iscsit_global.global_tsih_pool = NULL;
865 void
866 iscsit_global_hold()
869 * To take out a global hold, we must either own the global
870 * state mutex or we must be running inside of an ioctl that
871 * has set the global state to ISE_BUSY, ISE_DISABLING, or
872 * ISE_ENABLING. We don't track the "owner" for these flags,
873 * so just checking if they are set is enough for now.
875 ASSERT((iscsit_global.global_svc_state == ISE_ENABLING) ||
876 (iscsit_global.global_svc_state == ISE_DISABLING) ||
877 (iscsit_global.global_svc_state == ISE_BUSY) ||
878 MUTEX_HELD(&iscsit_global.global_state_mutex));
880 idm_refcnt_hold(&iscsit_global.global_refcnt);
883 void
884 iscsit_global_rele()
886 idm_refcnt_rele(&iscsit_global.global_refcnt);
889 void
890 iscsit_global_wait_ref()
892 idm_refcnt_wait_ref(&iscsit_global.global_refcnt);
896 * IDM callbacks
899 /*ARGSUSED*/
900 void
901 iscsit_rx_pdu(idm_conn_t *ic, idm_pdu_t *rx_pdu)
903 iscsit_conn_t *ict = ic->ic_handle;
904 switch (IDM_PDU_OPCODE(rx_pdu)) {
905 case ISCSI_OP_SCSI_CMD:
906 ASSERT(0); /* Shouldn't happen */
907 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
908 break;
909 case ISCSI_OP_SNACK_CMD:
911 * We'll need to handle this when we support ERL1/2. For
912 * now we treat it as a protocol error.
914 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
915 idm_conn_event(ic, CE_TRANSPORT_FAIL, (uintptr_t)NULL);
916 break;
917 case ISCSI_OP_SCSI_TASK_MGT_MSG:
918 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
919 iscsit_set_cmdsn(ict, rx_pdu);
920 iscsit_op_scsi_task_mgmt(ict, rx_pdu);
922 break;
923 case ISCSI_OP_NOOP_OUT:
924 case ISCSI_OP_LOGIN_CMD:
925 case ISCSI_OP_TEXT_CMD:
926 case ISCSI_OP_LOGOUT_CMD:
928 * If/when we switch to userland processing these PDU's
929 * will be handled by iscsitd.
931 iscsit_deferred_dispatch(rx_pdu);
932 break;
933 default:
934 /* Protocol error */
935 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
936 idm_conn_event(ic, CE_TRANSPORT_FAIL, (uintptr_t)NULL);
937 break;
941 /*ARGSUSED*/
942 void
943 iscsit_rx_pdu_error(idm_conn_t *ic, idm_pdu_t *rx_pdu, idm_status_t status)
945 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
949 * iscsit_rx_scsi_rsp -- cause the connection to be closed if response rx'd
951 * A target sends an SCSI Response PDU, it should never receive one.
952 * This has been seen when running the Codemonicon suite of tests which
953 * does negative testing of the protocol. If such a condition occurs using
954 * a normal initiator it most likely means there's data corruption in the
955 * header and that's grounds for dropping the connection as well.
957 void
958 iscsit_rx_scsi_rsp(idm_conn_t *ic, idm_pdu_t *rx_pdu)
960 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
961 idm_conn_event(ic, CE_TRANSPORT_FAIL, 0);
964 void
965 iscsit_task_aborted(idm_task_t *idt, idm_status_t status)
967 iscsit_task_t *itask = idt->idt_private;
969 switch (status) {
970 case IDM_STATUS_SUSPENDED:
971 break;
972 case IDM_STATUS_ABORTED:
973 mutex_enter(&itask->it_mutex);
974 itask->it_aborted = B_TRUE;
976 * We rely on the fact that STMF tracks outstanding
977 * buffer transfers and will free all of our buffers
978 * before freeing the task so we don't need to
979 * explicitly free the buffers from iscsit/idm
981 if (itask->it_stmf_abort) {
982 mutex_exit(&itask->it_mutex);
984 * Task is no longer active
986 iscsit_task_done(itask);
989 * STMF has already asked for this task to be aborted
991 * STMF specification is wrong... says to return
992 * STMF_ABORTED, the code actually looks for
993 * STMF_ABORT_SUCCESS.
995 stmf_task_lport_aborted(itask->it_stmf_task,
996 STMF_ABORT_SUCCESS, STMF_IOF_LPORT_DONE);
997 return;
998 } else {
999 mutex_exit(&itask->it_mutex);
1001 * Tell STMF to stop processing the task.
1003 stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
1004 STMF_ABORTED, NULL);
1005 return;
1007 /*NOTREACHED*/
1008 default:
1009 ASSERT(0);
1013 /*ARGSUSED*/
1014 idm_status_t
1015 iscsit_client_notify(idm_conn_t *ic, idm_client_notify_t icn,
1016 uintptr_t data)
1018 idm_status_t rc = IDM_STATUS_SUCCESS;
1021 * IDM client notifications will never occur at interrupt level
1022 * since they are generated from the connection state machine which
1023 * running on taskq threads.
1026 switch (icn) {
1027 case CN_CONNECT_ACCEPT:
1028 rc = iscsit_conn_accept(ic); /* No data */
1029 break;
1030 case CN_FFP_ENABLED:
1031 rc = iscsit_ffp_enabled(ic); /* No data */
1032 break;
1033 case CN_FFP_DISABLED:
1035 * Data indicates whether this was the result of an
1036 * explicit logout request.
1038 rc = iscsit_ffp_disabled(ic, (idm_ffp_disable_t)data);
1039 break;
1040 case CN_CONNECT_LOST:
1041 rc = iscsit_conn_lost(ic);
1042 break;
1043 case CN_CONNECT_DESTROY:
1044 rc = iscsit_conn_destroy(ic);
1045 break;
1046 case CN_LOGIN_FAIL:
1048 * Force the login state machine to completion
1050 rc = iscsit_login_fail(ic);
1051 break;
1052 default:
1053 rc = IDM_STATUS_REJECT;
1054 break;
1057 return (rc);
1061 * iscsit_update_statsn is invoked for all the PDUs which have the StatSN
1062 * field in the header. The StatSN is incremented if the IDM_PDU_ADVANCE_STATSN
1063 * flag is set in the pdu flags field. The StatSN is connection-wide and is
1064 * protected by the mutex ict_statsn_mutex. For Data-In PDUs, if the flag
1065 * IDM_TASK_PHASECOLLAPSE_REQ is set, the status (phase-collapse) is also filled
1067 void
1068 iscsit_update_statsn(idm_task_t *idm_task, idm_pdu_t *pdu)
1070 iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1071 iscsit_conn_t *ict = (iscsit_conn_t *)pdu->isp_ic->ic_handle;
1072 iscsit_task_t *itask = NULL;
1073 scsi_task_t *task = NULL;
1075 mutex_enter(&ict->ict_statsn_mutex);
1076 rsp->statsn = htonl(ict->ict_statsn);
1077 if (pdu->isp_flags & IDM_PDU_ADVANCE_STATSN)
1078 ict->ict_statsn++;
1079 mutex_exit(&ict->ict_statsn_mutex);
1082 * The last SCSI Data PDU passed for a command may also contain the
1083 * status if the status indicates termination with no expections, i.e.
1084 * no sense data or response involved. If the command completes with
1085 * an error, then the response and sense data will be sent in a
1086 * separate iSCSI Response PDU.
1088 if ((idm_task) && (idm_task->idt_flags & IDM_TASK_PHASECOLLAPSE_REQ)) {
1089 itask = idm_task->idt_private;
1090 task = itask->it_stmf_task;
1092 rsp->cmd_status = task->task_scsi_status;
1093 rsp->flags |= ISCSI_FLAG_DATA_STATUS;
1094 if (task->task_status_ctrl & TASK_SCTRL_OVER) {
1095 rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
1096 } else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
1097 rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
1099 rsp->residual_count = htonl(task->task_resid);
1102 * Removing the task from the session task list
1103 * just before the status is sent in the last
1104 * Data PDU transfer
1106 iscsit_task_done(itask);
1110 void
1111 iscsit_build_hdr(idm_task_t *idm_task, idm_pdu_t *pdu, uint8_t opcode)
1113 iscsit_task_t *itask = idm_task->idt_private;
1114 iscsi_data_rsp_hdr_t *dh = (iscsi_data_rsp_hdr_t *)pdu->isp_hdr;
1117 * We acquired iscsit_sess_t.ist_sn_mutex in iscsit_xfer_scsi_data
1119 ASSERT(MUTEX_HELD(&itask->it_ict->ict_sess->ist_sn_mutex));
1121 * On incoming data, the target transfer tag and Lun is only
1122 * provided by the target if the A bit is set, Since the target
1123 * does not currently support Error Recovery Level 1, the A
1124 * bit is never set.
1126 dh->opcode = opcode;
1127 dh->itt = itask->it_itt;
1128 dh->ttt = ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_SCSI_DATA_RSP) ?
1129 ISCSI_RSVD_TASK_TAG : itask->it_ttt;
1131 dh->expcmdsn = htonl(itask->it_ict->ict_sess->ist_expcmdsn);
1132 dh->maxcmdsn = htonl(itask->it_ict->ict_sess->ist_maxcmdsn);
1135 * IDM must set:
1137 * data.flags and rtt.flags
1138 * data.dlength
1139 * data.datasn
1140 * data.offset
1141 * statsn, residual_count and cmd_status (for phase collapse)
1142 * rtt.rttsn
1143 * rtt.data_offset
1144 * rtt.data_length
1148 void
1149 iscsit_keepalive(idm_conn_t *ic)
1151 idm_pdu_t *nop_in_pdu;
1152 iscsi_nop_in_hdr_t *nop_in;
1153 iscsit_conn_t *ict = ic->ic_handle;
1156 * IDM noticed the connection has been idle for too long so it's
1157 * time to provoke some activity. Build and transmit an iSCSI
1158 * nop-in PDU -- when the initiator responds it will be counted
1159 * as "activity" and keep the connection alive.
1161 * We don't actually care about the response here at the iscsit level
1162 * so we will just throw it away without looking at it when it arrives.
1164 nop_in_pdu = idm_pdu_alloc(sizeof (*nop_in), 0);
1165 idm_pdu_init(nop_in_pdu, ic, NULL, NULL);
1166 nop_in = (iscsi_nop_in_hdr_t *)nop_in_pdu->isp_hdr;
1167 bzero(nop_in, sizeof (*nop_in));
1168 nop_in->opcode = ISCSI_OP_NOOP_IN;
1169 nop_in->flags = ISCSI_FLAG_FINAL;
1170 nop_in->itt = ISCSI_RSVD_TASK_TAG;
1172 * When the target sends a NOP-In as a Ping, the target transfer tag
1173 * is set to a valid (not reserved) value and the initiator task tag
1174 * is set to ISCSI_RSVD_TASK_TAG (0xffffffff). In this case the StatSN
1175 * will always contain the next sequence number but the StatSN for the
1176 * connection is not advanced after this PDU is sent.
1178 nop_in_pdu->isp_flags |= IDM_PDU_SET_STATSN;
1180 * This works because we don't currently allocate ttt's anywhere else
1181 * in iscsit so as long as we stay out of IDM's range we are safe.
1182 * If we need to allocate ttt's for other PDU's in the future this will
1183 * need to be improved.
1185 mutex_enter(&ict->ict_mutex);
1186 nop_in->ttt = ict->ict_keepalive_ttt;
1187 ict->ict_keepalive_ttt++;
1188 if (ict->ict_keepalive_ttt == ISCSI_RSVD_TASK_TAG)
1189 ict->ict_keepalive_ttt = IDM_TASKIDS_MAX;
1190 mutex_exit(&ict->ict_mutex);
1192 iscsit_pdu_tx(nop_in_pdu);
1195 static idm_status_t
1196 iscsit_conn_accept(idm_conn_t *ic)
1198 iscsit_conn_t *ict;
1201 * We need to get a global hold here to ensure that the service
1202 * doesn't get shutdown prior to establishing a session. This
1203 * gets released in iscsit_conn_destroy().
1205 mutex_enter(&iscsit_global.global_state_mutex);
1206 if (iscsit_global.global_svc_state != ISE_ENABLED) {
1207 mutex_exit(&iscsit_global.global_state_mutex);
1208 return (IDM_STATUS_FAIL);
1210 iscsit_global_hold();
1211 mutex_exit(&iscsit_global.global_state_mutex);
1214 * Allocate an associated iscsit structure to represent this
1215 * connection. We shouldn't really create a session until we
1216 * get the first login PDU.
1218 ict = kmem_zalloc(sizeof (*ict), KM_SLEEP);
1220 ict->ict_ic = ic;
1221 ict->ict_statsn = 1;
1222 ict->ict_keepalive_ttt = IDM_TASKIDS_MAX; /* Avoid IDM TT range */
1223 ic->ic_handle = ict;
1224 mutex_init(&ict->ict_mutex, NULL, MUTEX_DRIVER, NULL);
1225 mutex_init(&ict->ict_statsn_mutex, NULL, MUTEX_DRIVER, NULL);
1226 idm_refcnt_init(&ict->ict_refcnt, ict);
1229 * Initialize login state machine
1231 if (iscsit_login_sm_init(ict) != IDM_STATUS_SUCCESS) {
1232 iscsit_global_rele();
1234 * Cleanup the ict after idm notifies us about this failure
1236 return (IDM_STATUS_FAIL);
1239 return (IDM_STATUS_SUCCESS);
1242 idm_status_t
1243 iscsit_conn_reinstate(iscsit_conn_t *reinstate_ict, iscsit_conn_t *new_ict)
1245 idm_status_t result;
1248 * Note in new connection state that this connection is
1249 * reinstating an existing connection.
1251 new_ict->ict_reinstating = B_TRUE;
1252 new_ict->ict_reinstate_conn = reinstate_ict;
1253 new_ict->ict_statsn = reinstate_ict->ict_statsn;
1256 * Now generate connection state machine event to existing connection
1257 * so that it starts the cleanup process.
1259 result = idm_conn_reinstate_event(reinstate_ict->ict_ic,
1260 new_ict->ict_ic);
1262 return (result);
1265 void
1266 iscsit_conn_hold(iscsit_conn_t *ict)
1268 idm_refcnt_hold(&ict->ict_refcnt);
1271 void
1272 iscsit_conn_rele(iscsit_conn_t *ict)
1274 idm_refcnt_rele(&ict->ict_refcnt);
1277 void
1278 iscsit_conn_dispatch_hold(iscsit_conn_t *ict)
1280 idm_refcnt_hold(&ict->ict_dispatch_refcnt);
1283 void
1284 iscsit_conn_dispatch_rele(iscsit_conn_t *ict)
1286 idm_refcnt_rele(&ict->ict_dispatch_refcnt);
1289 static idm_status_t
1290 iscsit_login_fail(idm_conn_t *ic)
1292 iscsit_conn_t *ict = ic->ic_handle;
1294 /* Generate login state machine event */
1295 iscsit_login_sm_event(ict, ILE_LOGIN_CONN_ERROR, NULL);
1297 return (IDM_STATUS_SUCCESS);
1300 static idm_status_t
1301 iscsit_ffp_enabled(idm_conn_t *ic)
1303 iscsit_conn_t *ict = ic->ic_handle;
1305 /* Generate session state machine event */
1306 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_LOGGED_IN, ict);
1308 return (IDM_STATUS_SUCCESS);
1311 static idm_status_t
1312 iscsit_ffp_disabled(idm_conn_t *ic, idm_ffp_disable_t disable_class)
1314 iscsit_conn_t *ict = ic->ic_handle;
1316 /* Generate session state machine event */
1317 switch (disable_class) {
1318 case FD_CONN_FAIL:
1319 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_FAIL, ict);
1320 break;
1321 case FD_CONN_LOGOUT:
1322 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FFP_DISABLE, ict);
1323 break;
1324 case FD_SESS_LOGOUT:
1325 iscsit_sess_sm_event(ict->ict_sess, SE_SESSION_CLOSE, ict);
1326 break;
1327 default:
1328 ASSERT(0);
1331 return (IDM_STATUS_SUCCESS);
1334 static idm_status_t
1335 iscsit_conn_lost(idm_conn_t *ic)
1337 iscsit_conn_t *ict = ic->ic_handle;
1338 iscsit_sess_t *ist = ict->ict_sess;
1339 iscsit_cbuf_t *cbuf;
1340 idm_pdu_t *rx_pdu;
1341 int i;
1343 mutex_enter(&ict->ict_mutex);
1344 ict->ict_lost = B_TRUE;
1345 mutex_exit(&ict->ict_mutex);
1347 * scrub the staging queue for all PDUs on this connection
1349 if (ist != NULL) {
1350 mutex_enter(&ist->ist_sn_mutex);
1351 for (cbuf = ist->ist_rxpdu_queue, i = 0;
1352 ((cbuf->cb_num_elems > 0) && (i < ISCSIT_RXPDU_QUEUE_LEN));
1353 i++) {
1354 if (((rx_pdu = cbuf->cb_buffer[i]) != NULL) &&
1355 (rx_pdu->isp_ic == ic)) {
1356 /* conn is lost, drop the pdu */
1357 DTRACE_PROBE3(scrubbing__staging__queue,
1358 iscsit_sess_t *, ist, idm_conn_t *, ic,
1359 idm_pdu_t *, rx_pdu);
1360 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
1361 cbuf->cb_buffer[i] = NULL;
1362 cbuf->cb_num_elems--;
1363 iscsit_conn_dispatch_rele(ict);
1366 mutex_exit(&ist->ist_sn_mutex);
1369 * Make sure there aren't any PDU's transitioning from the receive
1370 * handler to the dispatch taskq.
1372 idm_refcnt_wait_ref(&ict->ict_dispatch_refcnt);
1374 return (IDM_STATUS_SUCCESS);
1377 static idm_status_t
1378 iscsit_conn_destroy(idm_conn_t *ic)
1380 iscsit_conn_t *ict = ic->ic_handle;
1382 mutex_enter(&ict->ict_mutex);
1383 ict->ict_destroyed = B_TRUE;
1384 mutex_exit(&ict->ict_mutex);
1386 /* Generate session state machine event */
1387 if (ict->ict_sess != NULL) {
1389 * Session state machine will call iscsit_conn_destroy_done()
1390 * when it has removed references to this connection.
1392 iscsit_sess_sm_event(ict->ict_sess, SE_CONN_FAIL, ict);
1395 idm_refcnt_wait_ref(&ict->ict_refcnt);
1397 * The session state machine does not need to post
1398 * events to IDM any longer, so it is safe to set
1399 * the idm connection reference to NULL
1401 ict->ict_ic = NULL;
1403 /* Reap the login state machine */
1404 iscsit_login_sm_fini(ict);
1406 /* Clean up any text command remnants */
1407 iscsit_text_cmd_fini(ict);
1409 mutex_destroy(&ict->ict_mutex);
1410 idm_refcnt_destroy(&ict->ict_refcnt);
1411 kmem_free(ict, sizeof (*ict));
1413 iscsit_global_rele();
1415 return (IDM_STATUS_SUCCESS);
1418 void
1419 iscsit_conn_logout(iscsit_conn_t *ict)
1422 * If the iscsi connection is active, then
1423 * logout the IDM connection by sending a
1424 * CE_LOGOUT_SESSION_SUCCESS, else, no action
1425 * needs to be taken because the connection
1426 * is already in the teardown process.
1428 mutex_enter(&ict->ict_mutex);
1429 if (ict->ict_lost == B_FALSE && ict->ict_destroyed == B_FALSE) {
1430 idm_conn_event(ict->ict_ic, CE_LOGOUT_SESSION_SUCCESS,
1431 (uintptr_t)NULL);
1433 mutex_exit(&ict->ict_mutex);
1437 * STMF-related functions
1439 * iSCSI to STMF mapping
1441 * Session == ?
1442 * Connection == bound to local port but not itself a local port
1443 * Target
1444 * Target portal (group?) == local port (really but we're not going to do this)
1445 * iscsit needs to map connections to local ports (whatever we decide
1446 * they are)
1447 * Target == ?
1450 /*ARGSUSED*/
1451 static stmf_data_buf_t *
1452 iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize,
1453 uint32_t flags)
1455 iscsit_task_t *itask = task->task_port_private;
1456 idm_buf_t *idm_buffer;
1457 iscsit_buf_t *ibuf;
1458 stmf_data_buf_t *result;
1459 uint32_t bsize;
1462 * If the requested size is larger than MaxBurstLength and the
1463 * given pminsize is also larger than MaxBurstLength, then the
1464 * allocation fails (dbuf = NULL) and pminsize is modified to
1465 * be equal to MaxBurstLength. stmf/sbd then should re-invoke
1466 * this function with the corrected values for transfer.
1468 ASSERT(pminsize);
1469 if (size <= itask->it_ict->ict_op.op_max_burst_length) {
1470 bsize = size;
1471 } else if (*pminsize <= itask->it_ict->ict_op.op_max_burst_length) {
1472 bsize = itask->it_ict->ict_op.op_max_burst_length;
1473 } else {
1474 *pminsize = itask->it_ict->ict_op.op_max_burst_length;
1475 return (NULL);
1478 /* Alloc buffer */
1479 idm_buffer = idm_buf_alloc(itask->it_ict->ict_ic, NULL, bsize);
1480 if (idm_buffer != NULL) {
1481 result = stmf_alloc(STMF_STRUCT_DATA_BUF,
1482 sizeof (iscsit_buf_t), 0);
1483 if (result != NULL) {
1484 /* Fill in stmf_data_buf_t */
1485 ibuf = result->db_port_private;
1486 ibuf->ibuf_idm_buf = idm_buffer;
1487 ibuf->ibuf_stmf_buf = result;
1488 ibuf->ibuf_is_immed = B_FALSE;
1489 result->db_flags = DB_DONT_CACHE;
1490 result->db_buf_size = bsize;
1491 result->db_data_size = bsize;
1492 result->db_sglist_length = 1;
1493 result->db_sglist[0].seg_addr = idm_buffer->idb_buf;
1494 result->db_sglist[0].seg_length =
1495 idm_buffer->idb_buflen;
1496 return (result);
1499 /* Couldn't get the stmf_data_buf_t so free the buffer */
1500 idm_buf_free(idm_buffer);
1503 return (NULL);
1506 /*ARGSUSED*/
1507 static void
1508 iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf)
1510 iscsit_buf_t *ibuf = dbuf->db_port_private;
1512 if (ibuf->ibuf_is_immed) {
1514 * The iscsit_buf_t structure itself will be freed with its
1515 * associated task. Here we just need to free the PDU that
1516 * held the immediate data.
1518 idm_pdu_complete(ibuf->ibuf_immed_data_pdu, IDM_STATUS_SUCCESS);
1519 ibuf->ibuf_immed_data_pdu = 0;
1520 } else {
1521 idm_buf_free(ibuf->ibuf_idm_buf);
1522 stmf_free(dbuf);
1526 /*ARGSUSED*/
1527 stmf_status_t
1528 iscsit_xfer_scsi_data(scsi_task_t *task, stmf_data_buf_t *dbuf,
1529 uint32_t ioflags)
1531 iscsit_task_t *iscsit_task = task->task_port_private;
1532 iscsit_sess_t *ict_sess = iscsit_task->it_ict->ict_sess;
1533 iscsit_buf_t *ibuf = dbuf->db_port_private;
1534 int idm_rc;
1537 * If we are aborting then we can ignore this request
1539 if (iscsit_task->it_stmf_abort) {
1540 return (STMF_SUCCESS);
1544 * If it's not immediate data then start the transfer
1546 if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) {
1547 if (ibuf->ibuf_is_immed)
1548 return (iscsit_idm_to_stmf(IDM_STATUS_SUCCESS));
1550 * The DB_SEND_STATUS_GOOD flag in the STMF data buffer allows
1551 * the port provider to phase-collapse, i.e. send the status
1552 * along with the final data PDU for the command. The port
1553 * provider passes this request to the transport layer by
1554 * setting a flag IDM_TASK_PHASECOLLAPSE_REQ in the task.
1556 if (dbuf->db_flags & DB_SEND_STATUS_GOOD)
1557 iscsit_task->it_idm_task->idt_flags |=
1558 IDM_TASK_PHASECOLLAPSE_REQ;
1560 * IDM will call iscsit_build_hdr so lock now to serialize
1561 * access to the SN values. We need to lock here to enforce
1562 * lock ordering
1564 mutex_enter(&ict_sess->ist_sn_mutex);
1565 idm_rc = idm_buf_tx_to_ini(iscsit_task->it_idm_task,
1566 ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1567 dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1568 mutex_exit(&ict_sess->ist_sn_mutex);
1570 return (iscsit_idm_to_stmf(idm_rc));
1571 } else if (dbuf->db_flags & DB_DIRECTION_FROM_RPORT) {
1572 ASSERT(ibuf->ibuf_is_immed == B_FALSE);
1573 /* Grab the SN lock (see comment above) */
1574 mutex_enter(&ict_sess->ist_sn_mutex);
1575 idm_rc = idm_buf_rx_from_ini(iscsit_task->it_idm_task,
1576 ibuf->ibuf_idm_buf, dbuf->db_relative_offset,
1577 dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf);
1578 mutex_exit(&ict_sess->ist_sn_mutex);
1580 return (iscsit_idm_to_stmf(idm_rc));
1583 /* What are we supposed to do if there is no direction? */
1584 return (STMF_INVALID_ARG);
1587 static void
1588 iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status)
1590 iscsit_task_t *itask = idb->idb_task_binding->idt_private;
1591 stmf_data_buf_t *dbuf = idb->idb_cb_arg;
1593 dbuf->db_xfer_status = iscsit_idm_to_stmf(status);
1596 * If the task has been aborted then we don't need to call STMF
1598 if (itask->it_stmf_abort) {
1599 return;
1603 * For ISCSI over TCP (not iSER), the last SCSI Data PDU passed
1604 * for a successful command contains the status as requested by
1605 * by COMSTAR (via the DB_SEND_STATUS_GOOD flag). But the iSER
1606 * transport does not support phase-collapse. So pretend we are
1607 * COMSTAR and send the status in a separate PDU now.
1609 if (idb->idb_task_binding->idt_flags & IDM_TASK_PHASECOLLAPSE_SUCCESS) {
1611 * Mark task complete and notify COMSTAR
1612 * that the status has been sent.
1614 itask->it_idm_task->idt_state = TASK_COMPLETE;
1615 stmf_send_status_done(itask->it_stmf_task,
1616 iscsit_idm_to_stmf(status), STMF_IOF_LPORT_DONE);
1617 } else if ((dbuf->db_flags & DB_SEND_STATUS_GOOD) &&
1618 status == IDM_STATUS_SUCCESS) {
1621 * The iscsi target port provider - for iSER, emulates the
1622 * DB_SEND_STATUS_GOOD optimization if requested by STMF;
1623 * it sends the status in a separate PDU after the data
1624 * transfer. In this case the port provider should first
1625 * call stmf_data_xfer_done() to mark the transfer complete
1626 * and then send the status. Although STMF will free the
1627 * buffer at the time the task is freed, even if the transfer
1628 * is not marked complete, this behavior makes statistics
1629 * gathering and task state tracking more difficult than it
1630 * needs to be.
1632 stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
1633 if (iscsit_send_scsi_status(itask->it_stmf_task, 0)
1634 != STMF_SUCCESS) {
1635 stmf_send_status_done(itask->it_stmf_task,
1636 STMF_FAILURE, STMF_IOF_LPORT_DONE);
1638 } else {
1639 stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0);
1640 /* don't touch dbuf after stmf_data_xfer_done */
1645 /*ARGSUSED*/
1646 stmf_status_t
1647 iscsit_send_scsi_status(scsi_task_t *task, uint32_t ioflags)
1649 iscsit_task_t *itask = task->task_port_private;
1650 iscsi_scsi_rsp_hdr_t *rsp;
1651 idm_pdu_t *pdu;
1652 int resp_datalen;
1655 * If this task is aborted then we don't need to respond.
1657 if (itask->it_stmf_abort) {
1658 return (STMF_SUCCESS);
1662 * If this is a task management status, handle it elsewhere.
1664 if (task->task_mgmt_function != TM_NONE) {
1666 * Don't wait for the PDU completion to tell STMF
1667 * the task is done -- it doesn't really matter and
1668 * it makes life complicated if STMF later asks us to
1669 * abort the request and we don't know whether the
1670 * status has been sent or not.
1672 itask->it_tm_responded = B_TRUE;
1673 iscsit_send_task_mgmt_resp(itask->it_tm_pdu,
1674 (task->task_completion_status == STMF_SUCCESS) ?
1675 SCSI_TCP_TM_RESP_COMPLETE : SCSI_TCP_TM_RESP_FUNC_NOT_SUPP);
1676 stmf_send_status_done(task, STMF_SUCCESS,
1677 STMF_IOF_LPORT_DONE);
1678 return (STMF_SUCCESS);
1682 * Remove the task from the session task list
1684 iscsit_task_done(itask);
1687 * Send status
1689 mutex_enter(&itask->it_idm_task->idt_mutex);
1690 if ((itask->it_idm_task->idt_state == TASK_ACTIVE) &&
1691 (task->task_completion_status == STMF_SUCCESS) &&
1692 (task->task_sense_length == 0) &&
1693 (task->task_resid == 0)) {
1694 itask->it_idm_task->idt_state = TASK_COMPLETE;
1695 /* PDU callback releases task hold */
1696 idm_task_hold(itask->it_idm_task);
1697 mutex_exit(&itask->it_idm_task->idt_mutex);
1699 * Fast path. Cached status PDU's are already
1700 * initialized. We just need to fill in
1701 * connection and task information. StatSN is
1702 * incremented by 1 for every status sent a
1703 * connection.
1705 pdu = kmem_cache_alloc(iscsit_status_pdu_cache, KM_SLEEP);
1706 pdu->isp_ic = itask->it_ict->ict_ic;
1707 pdu->isp_private = itask;
1708 pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1710 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1711 rsp->itt = itask->it_itt;
1713 * ExpDataSN is the number of R2T and Data-In (read)
1714 * PDUs the target has sent for the SCSI command.
1716 * Since there is no support for bidirectional transfer
1717 * yet, either idt_exp_datasn or idt_exp_rttsn, but not
1718 * both is valid at any time
1720 rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
1721 htonl(itask->it_idm_task->idt_exp_datasn):
1722 htonl(itask->it_idm_task->idt_exp_rttsn);
1723 rsp->cmd_status = task->task_scsi_status;
1724 iscsit_pdu_tx(pdu);
1725 return (STMF_SUCCESS);
1726 } else {
1727 if (itask->it_idm_task->idt_state != TASK_ACTIVE) {
1728 mutex_exit(&itask->it_idm_task->idt_mutex);
1729 return (STMF_FAILURE);
1731 itask->it_idm_task->idt_state = TASK_COMPLETE;
1732 /* PDU callback releases task hold */
1733 idm_task_hold(itask->it_idm_task);
1734 mutex_exit(&itask->it_idm_task->idt_mutex);
1736 resp_datalen = (task->task_sense_length == 0) ? 0 :
1737 (task->task_sense_length + sizeof (uint16_t));
1739 pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
1740 idm_pdu_init(pdu, itask->it_ict->ict_ic, itask,
1741 iscsit_send_status_done);
1742 pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
1744 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
1745 bzero(rsp, sizeof (*rsp));
1746 rsp->opcode = ISCSI_OP_SCSI_RSP;
1748 rsp->flags = ISCSI_FLAG_FINAL;
1749 if (task->task_status_ctrl & TASK_SCTRL_OVER) {
1750 rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW;
1751 } else if (task->task_status_ctrl & TASK_SCTRL_UNDER) {
1752 rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
1755 rsp->bi_residual_count = 0;
1756 rsp->residual_count = htonl(task->task_resid);
1757 rsp->itt = itask->it_itt;
1758 rsp->response = ISCSI_STATUS_CMD_COMPLETED;
1759 rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ?
1760 htonl(itask->it_idm_task->idt_exp_datasn):
1761 htonl(itask->it_idm_task->idt_exp_rttsn);
1762 rsp->cmd_status = task->task_scsi_status;
1763 if (task->task_sense_length != 0) {
1765 * Add a byte to provide the sense length in
1766 * the response
1768 *(uint16_t *)((void *)pdu->isp_data) =
1769 htons(task->task_sense_length);
1770 bcopy(task->task_sense_data,
1771 (uint8_t *)pdu->isp_data +
1772 sizeof (uint16_t),
1773 task->task_sense_length);
1774 hton24(rsp->dlength, resp_datalen);
1777 DTRACE_PROBE5(iscsi__scsi__response,
1778 iscsit_conn_t *, itask->it_ict,
1779 uint8_t, rsp->response,
1780 uint8_t, rsp->cmd_status,
1781 idm_pdu_t *, pdu,
1782 scsi_task_t *, task);
1784 iscsit_pdu_tx(pdu);
1786 return (STMF_SUCCESS);
1790 /*ARGSUSED*/
1791 static void
1792 iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status)
1794 iscsit_task_t *itask;
1795 boolean_t aborted;
1797 itask = pdu->isp_private;
1798 aborted = itask->it_stmf_abort;
1801 * After releasing the hold the task may be freed at any time so
1802 * don't touch it.
1804 idm_task_rele(itask->it_idm_task);
1805 if (!aborted) {
1806 stmf_send_status_done(itask->it_stmf_task,
1807 iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1809 kmem_cache_free(iscsit_status_pdu_cache, pdu);
1812 /*ARGSUSED*/
1813 static void
1814 iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status)
1816 iscsit_task_t *itask;
1817 boolean_t aborted;
1819 itask = pdu->isp_private;
1820 aborted = itask->it_stmf_abort;
1823 * After releasing the hold the task may be freed at any time so
1824 * don't touch it.
1826 idm_task_rele(itask->it_idm_task);
1827 if (!aborted) {
1828 stmf_send_status_done(itask->it_stmf_task,
1829 iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE);
1831 idm_pdu_free(pdu);
1835 void
1836 iscsit_lport_task_free(scsi_task_t *task)
1838 iscsit_task_t *itask = task->task_port_private;
1840 /* We only call idm_task_start for regular tasks, not task management */
1841 if (task->task_mgmt_function == TM_NONE) {
1842 idm_task_done(itask->it_idm_task);
1843 iscsit_task_free(itask);
1844 return;
1845 } else {
1846 iscsit_tm_task_free(itask);
1850 /*ARGSUSED*/
1851 stmf_status_t
1852 iscsit_abort(stmf_local_port_t *lport, int abort_cmd, void *arg, uint32_t flags)
1854 scsi_task_t *st = (scsi_task_t *)arg;
1855 iscsit_task_t *iscsit_task;
1856 idm_task_t *idt;
1859 * If this is a task management request then there's really not much to
1860 * do.
1862 if (st->task_mgmt_function != TM_NONE) {
1863 return (STMF_ABORT_SUCCESS);
1867 * Regular task, start cleaning up
1869 iscsit_task = st->task_port_private;
1870 idt = iscsit_task->it_idm_task;
1871 mutex_enter(&iscsit_task->it_mutex);
1872 iscsit_task->it_stmf_abort = B_TRUE;
1873 if (iscsit_task->it_aborted) {
1874 mutex_exit(&iscsit_task->it_mutex);
1876 * Task is no longer active
1878 iscsit_task_done(iscsit_task);
1881 * STMF specification is wrong... says to return
1882 * STMF_ABORTED, the code actually looks for
1883 * STMF_ABORT_SUCCESS.
1885 return (STMF_ABORT_SUCCESS);
1886 } else {
1887 mutex_exit(&iscsit_task->it_mutex);
1889 * Call IDM to abort the task. Due to a variety of
1890 * circumstances the task may already be in the process of
1891 * aborting.
1892 * We'll let IDM worry about rationalizing all that except
1893 * for one particular instance. If the state of the task
1894 * is TASK_COMPLETE, we need to indicate to the framework
1895 * that we are in fact done. This typically happens with
1896 * framework-initiated task management type requests
1897 * (e.g. abort task).
1899 if (idt->idt_state == TASK_COMPLETE) {
1900 idm_refcnt_wait_ref(&idt->idt_refcnt);
1901 return (STMF_ABORT_SUCCESS);
1902 } else {
1903 idm_task_abort(idt->idt_ic, idt, AT_TASK_MGMT_ABORT);
1904 return (STMF_SUCCESS);
1908 /*NOTREACHED*/
1911 /*ARGSUSED*/
1912 void
1913 iscsit_ctl(stmf_local_port_t *lport, int cmd, void *arg)
1915 iscsit_tgt_t *iscsit_tgt;
1917 ASSERT((cmd == STMF_CMD_LPORT_ONLINE) ||
1918 (cmd == STMF_ACK_LPORT_ONLINE_COMPLETE) ||
1919 (cmd == STMF_CMD_LPORT_OFFLINE) ||
1920 (cmd == STMF_ACK_LPORT_OFFLINE_COMPLETE));
1922 iscsit_tgt = (iscsit_tgt_t *)lport->lport_port_private;
1924 switch (cmd) {
1925 case STMF_CMD_LPORT_ONLINE:
1926 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_REQ);
1927 break;
1928 case STMF_CMD_LPORT_OFFLINE:
1929 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_REQ);
1930 break;
1931 case STMF_ACK_LPORT_ONLINE_COMPLETE:
1932 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_COMPLETE_ACK);
1933 break;
1934 case STMF_ACK_LPORT_OFFLINE_COMPLETE:
1935 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_COMPLETE_ACK);
1936 break;
1938 default:
1939 break;
1943 static stmf_status_t
1944 iscsit_idm_to_stmf(idm_status_t idmrc)
1946 switch (idmrc) {
1947 case IDM_STATUS_SUCCESS:
1948 return (STMF_SUCCESS);
1949 default:
1950 return (STMF_FAILURE);
1952 /*NOTREACHED*/
1955 void
1956 iscsit_op_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu)
1958 iscsit_conn_t *ict = ic->ic_handle;
1960 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
1961 iscsit_post_scsi_cmd(ic, rx_pdu);
1963 iscsit_process_pdu_in_queue(ict->ict_sess);
1967 * ISCSI protocol
1970 void
1971 iscsit_post_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu)
1973 iscsit_conn_t *ict;
1974 iscsit_task_t *itask;
1975 scsi_task_t *task;
1976 iscsit_buf_t *ibuf;
1977 iscsi_scsi_cmd_hdr_t *iscsi_scsi =
1978 (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
1979 iscsi_addl_hdr_t *ahs_hdr;
1980 uint16_t addl_cdb_len = 0;
1982 ict = ic->ic_handle;
1984 itask = iscsit_task_alloc(ict);
1985 if (itask == NULL) {
1986 /* Finish processing request */
1987 iscsit_set_cmdsn(ict, rx_pdu);
1989 iscsit_send_direct_scsi_resp(ict, rx_pdu,
1990 ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
1991 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
1992 return;
1996 * Note CmdSN and ITT in task. IDM will have already validated this
1997 * request against the connection state so we don't need to check
1998 * that (the connection may have changed state in the meantime but
1999 * we will catch that when we try to send a response)
2001 itask->it_cmdsn = ntohl(iscsi_scsi->cmdsn);
2002 itask->it_itt = iscsi_scsi->itt;
2005 * Check for extended CDB AHS
2007 if (iscsi_scsi->hlength > 0) {
2008 ahs_hdr = (iscsi_addl_hdr_t *)iscsi_scsi;
2009 addl_cdb_len = ((ahs_hdr->ahs_hlen_hi << 8) |
2010 ahs_hdr->ahs_hlen_lo) - 1; /* Adjust for reserved byte */
2011 if (((addl_cdb_len + 4) / sizeof (uint32_t)) >
2012 iscsi_scsi->hlength) {
2013 /* Mangled header info, drop it */
2014 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2015 return;
2019 ict = rx_pdu->isp_ic->ic_handle; /* IDM client private */
2022 * Add task to session list. This function will also check to
2023 * ensure that the task does not already exist.
2025 if (iscsit_task_start(itask) != IDM_STATUS_SUCCESS) {
2027 * Task exists, free all resources and reject. Don't
2028 * update expcmdsn in this case because RFC 3720 says
2029 * "The CmdSN of the rejected command PDU (if it is a
2030 * non-immediate command) MUST NOT be considered received
2031 * by the target (i.e., a command sequence gap must be
2032 * assumed for the CmdSN), even though the CmdSN of the
2033 * rejected command PDU may be reliably ascertained. Upon
2034 * receiving the Reject, the initiator MUST plug the CmdSN
2035 * gap in order to continue to use the session. The gap
2036 * may be plugged either by transmitting a command PDU
2037 * with the same CmdSN, or by aborting the task (see section
2038 * 6.9 on how an abort may plug a CmdSN gap)." (Section 6.3)
2040 iscsit_task_free(itask);
2041 iscsit_send_reject(ict, rx_pdu, ISCSI_REJECT_TASK_IN_PROGRESS);
2042 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2043 return;
2046 /* Update sequence numbers */
2047 iscsit_set_cmdsn(ict, rx_pdu);
2050 * Allocate STMF task
2052 itask->it_stmf_task = stmf_task_alloc(
2053 itask->it_ict->ict_sess->ist_lport,
2054 itask->it_ict->ict_sess->ist_stmf_sess, iscsi_scsi->lun,
2055 16 + addl_cdb_len, 0);
2056 if (itask->it_stmf_task == NULL) {
2058 * Either stmf really couldn't get memory for a task or,
2059 * more likely, the LU is currently in reset. Either way
2060 * we have no choice but to fail the request.
2062 iscsit_task_done(itask);
2063 iscsit_task_free(itask);
2064 iscsit_send_direct_scsi_resp(ict, rx_pdu,
2065 ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY);
2066 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2067 return;
2070 task = itask->it_stmf_task;
2071 task->task_port_private = itask;
2073 bcopy(iscsi_scsi->lun, task->task_lun_no, sizeof (task->task_lun_no));
2076 * iSCSI and Comstar use the same values. Should we rely on this
2077 * or translate them bit-wise?
2080 task->task_flags =
2081 (((iscsi_scsi->flags & ISCSI_FLAG_CMD_READ) ? TF_READ_DATA : 0) |
2082 ((iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ? TF_WRITE_DATA : 0) |
2083 ((rx_pdu->isp_datalen == 0) ? 0 : TF_INITIAL_BURST));
2085 switch (iscsi_scsi->flags & ISCSI_FLAG_CMD_ATTR_MASK) {
2086 case ISCSI_ATTR_UNTAGGED:
2087 break;
2088 case ISCSI_ATTR_SIMPLE:
2089 task->task_additional_flags |= TF_ATTR_SIMPLE_QUEUE;
2090 break;
2091 case ISCSI_ATTR_ORDERED:
2092 task->task_additional_flags |= TF_ATTR_ORDERED_QUEUE;
2093 break;
2094 case ISCSI_ATTR_HEAD_OF_QUEUE:
2095 task->task_additional_flags |= TF_ATTR_HEAD_OF_QUEUE;
2096 break;
2097 case ISCSI_ATTR_ACA:
2098 task->task_additional_flags |= TF_ATTR_ACA;
2099 break;
2100 default:
2101 /* Protocol error but just take it, treat as untagged */
2102 break;
2106 task->task_additional_flags = 0;
2107 task->task_priority = 0;
2108 task->task_mgmt_function = TM_NONE;
2111 * This "task_max_nbufs" doesn't map well to BIDI. We probably need
2112 * parameter for each direction. "MaxOutstandingR2T" may very well
2113 * be set to one which could prevent us from doing simultaneous
2114 * transfers in each direction.
2116 task->task_max_nbufs = (iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ?
2117 ict->ict_op.op_max_outstanding_r2t : STMF_BUFS_MAX;
2118 task->task_cmd_seq_no = ntohl(iscsi_scsi->itt);
2119 task->task_expected_xfer_length = ntohl(iscsi_scsi->data_length);
2121 /* Copy CDB */
2122 bcopy(iscsi_scsi->scb, task->task_cdb, 16);
2123 if (addl_cdb_len > 0) {
2124 bcopy(ahs_hdr->ahs_extscb, task->task_cdb + 16, addl_cdb_len);
2127 DTRACE_ISCSI_3(scsi__command, idm_conn_t *, ic,
2128 iscsi_scsi_cmd_hdr_t *, (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr,
2129 scsi_task_t *, task);
2132 * Copy the transport header into the task handle from the PDU
2133 * handle. The transport header describes this task's remote tagged
2134 * buffer.
2136 if (rx_pdu->isp_transport_hdrlen != 0) {
2137 bcopy(rx_pdu->isp_transport_hdr,
2138 itask->it_idm_task->idt_transport_hdr,
2139 rx_pdu->isp_transport_hdrlen);
2143 * Tell IDM about our new active task
2145 idm_task_start(itask->it_idm_task, (uintptr_t)itask->it_itt);
2148 * If we have any immediate data then setup the immediate buffer
2149 * context that comes with the task
2151 if (rx_pdu->isp_datalen) {
2152 ibuf = itask->it_immed_data;
2153 ibuf->ibuf_immed_data_pdu = rx_pdu;
2154 ibuf->ibuf_stmf_buf->db_data_size = rx_pdu->isp_datalen;
2155 ibuf->ibuf_stmf_buf->db_buf_size = rx_pdu->isp_datalen;
2156 ibuf->ibuf_stmf_buf->db_relative_offset = 0;
2157 ibuf->ibuf_stmf_buf->db_sglist[0].seg_length =
2158 rx_pdu->isp_datalen;
2159 ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr = rx_pdu->isp_data;
2161 DTRACE_ISCSI_8(xfer__start, idm_conn_t *, ic,
2162 uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
2163 uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
2164 uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
2165 uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
2168 * For immediate data transfer, there is no callback from
2169 * stmf to indicate that the initial burst of data is
2170 * transferred successfully. In some cases, the task can
2171 * get freed before execution returns from stmf_post_task.
2172 * Although this xfer-start/done probe accurately tracks
2173 * the size of the transfer, it does only provide a best
2174 * effort on the timing of the transfer.
2176 DTRACE_ISCSI_8(xfer__done, idm_conn_t *, ic,
2177 uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr,
2178 uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset,
2179 uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */
2180 uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI);
2181 stmf_post_task(task, ibuf->ibuf_stmf_buf);
2182 } else {
2184 stmf_post_task(task, NULL);
2185 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2189 void
2190 iscsit_deferred_dispatch(idm_pdu_t *rx_pdu)
2192 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
2195 * If this isn't a login packet, we need a session. Otherwise
2196 * this is a protocol error (perhaps one IDM should've caught?).
2198 if (IDM_PDU_OPCODE(rx_pdu) != ISCSI_OP_LOGIN_CMD &&
2199 ict->ict_sess == NULL) {
2200 DTRACE_PROBE2(iscsi__idm__deferred__no__session,
2201 iscsit_conn_t *, ict, idm_pdu_t *, rx_pdu);
2202 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2203 return;
2207 * If the connection has been lost then ignore new PDU's
2209 mutex_enter(&ict->ict_mutex);
2210 if (ict->ict_lost) {
2211 mutex_exit(&ict->ict_mutex);
2212 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2213 return;
2217 * Grab a hold on the connection to prevent it from going away
2218 * between now and when the taskq function is called.
2220 iscsit_conn_dispatch_hold(ict);
2221 mutex_exit(&ict->ict_mutex);
2223 taskq_dispatch_ent(iscsit_global.global_dispatch_taskq,
2224 iscsit_deferred, rx_pdu, 0, &rx_pdu->isp_tqent);
2227 static void
2228 iscsit_deferred(void *rx_pdu_void)
2230 idm_pdu_t *rx_pdu = rx_pdu_void;
2231 idm_conn_t *ic = rx_pdu->isp_ic;
2232 iscsit_conn_t *ict = ic->ic_handle;
2235 * NOP and Task Management Commands can be marked for immediate
2236 * delivery. Commands marked as 'Immediate' are to be considered
2237 * for execution as soon as they arrive on the target. So these
2238 * should not be checked for sequence order and put in a queue.
2239 * The CmdSN is not advanced for Immediate Commands.
2241 switch (IDM_PDU_OPCODE(rx_pdu)) {
2242 case ISCSI_OP_NOOP_OUT:
2243 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2244 iscsit_set_cmdsn(ict, rx_pdu);
2245 iscsit_pdu_op_noop(ict, rx_pdu);
2247 break;
2248 case ISCSI_OP_LOGIN_CMD:
2249 iscsit_pdu_op_login_cmd(ict, rx_pdu);
2250 iscsit_conn_dispatch_rele(ict);
2251 return;
2252 case ISCSI_OP_TEXT_CMD:
2253 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2254 iscsit_set_cmdsn(ict, rx_pdu);
2255 iscsit_pdu_op_text_cmd(ict, rx_pdu);
2257 break;
2258 case ISCSI_OP_LOGOUT_CMD:
2259 if (iscsit_check_cmdsn_and_queue(rx_pdu)) {
2260 iscsit_set_cmdsn(ict, rx_pdu);
2261 iscsit_pdu_op_logout_cmd(ict, rx_pdu);
2263 break;
2264 default:
2265 /* Protocol error. IDM should have caught this */
2266 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
2267 ASSERT(0);
2268 break;
2271 * Check if there are other PDUs in the session staging queue
2272 * waiting to be posted to SCSI layer.
2274 iscsit_process_pdu_in_queue(ict->ict_sess);
2276 iscsit_conn_dispatch_rele(ict);
2279 static void
2280 iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu,
2281 uint8_t response, uint8_t cmd_status)
2283 idm_pdu_t *rsp_pdu;
2284 idm_conn_t *ic;
2285 iscsi_scsi_rsp_hdr_t *resp;
2286 iscsi_scsi_cmd_hdr_t *req =
2287 (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2289 ic = ict->ict_ic;
2291 rsp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_rsp_hdr_t), 0);
2292 idm_pdu_init(rsp_pdu, ic, NULL, NULL);
2294 * StatSN is incremented by 1 for every response sent on
2295 * a connection except for responses sent as a result of
2296 * a retry or SNACK
2298 rsp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2300 resp = (iscsi_scsi_rsp_hdr_t *)rsp_pdu->isp_hdr;
2302 resp->opcode = ISCSI_OP_SCSI_RSP;
2303 resp->flags = ISCSI_FLAG_FINAL;
2304 resp->response = response;
2305 resp->cmd_status = cmd_status;
2306 resp->itt = req->itt;
2307 if ((response == ISCSI_STATUS_CMD_COMPLETED) &&
2308 (req->data_length != 0) &&
2309 ((req->flags & ISCSI_FLAG_CMD_READ) ||
2310 (req->flags & ISCSI_FLAG_CMD_WRITE))) {
2311 resp->flags |= ISCSI_FLAG_CMD_UNDERFLOW;
2312 resp->residual_count = req->data_length;
2315 DTRACE_PROBE4(iscsi__scsi__direct__response,
2316 iscsit_conn_t *, ict,
2317 uint8_t, resp->response,
2318 uint8_t, resp->cmd_status,
2319 idm_pdu_t *, rsp_pdu);
2321 iscsit_pdu_tx(rsp_pdu);
2324 void
2325 iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu, uint8_t tm_status)
2327 iscsi_scsi_task_mgt_rsp_hdr_t *tm_resp;
2330 * The target must take note of the last-sent StatSN.
2331 * The StatSN is to be incremented after sending a
2332 * task management response. Digest recovery can only
2333 * work if StatSN is incremented.
2335 tm_resp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2336 tm_resp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2337 tm_resp->response = tm_status;
2339 DTRACE_PROBE3(iscsi__scsi__tm__response,
2340 iscsit_conn_t *, tm_resp_pdu->isp_ic->ic_handle,
2341 uint8_t, tm_resp->response,
2342 idm_pdu_t *, tm_resp_pdu);
2343 iscsit_pdu_tx(tm_resp_pdu);
2346 void
2347 iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2349 idm_pdu_t *tm_resp_pdu;
2350 iscsit_task_t *itask;
2351 iscsit_task_t *tm_itask;
2352 scsi_task_t *task;
2353 iscsi_scsi_task_mgt_hdr_t *iscsi_tm =
2354 (iscsi_scsi_task_mgt_hdr_t *)rx_pdu->isp_hdr;
2355 iscsi_scsi_task_mgt_rsp_hdr_t *iscsi_tm_rsp =
2356 (iscsi_scsi_task_mgt_rsp_hdr_t *)rx_pdu->isp_hdr;
2357 uint32_t rtt, cmdsn, refcmdsn;
2358 uint8_t tm_func;
2361 * Setup response PDU (response field will get filled in later)
2363 tm_resp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_task_mgt_rsp_hdr_t), 0);
2364 if (tm_resp_pdu == NULL) {
2365 /* Can't respond, just drop it */
2366 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2367 return;
2369 idm_pdu_init(tm_resp_pdu, ict->ict_ic, NULL, NULL);
2370 iscsi_tm_rsp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr;
2371 bzero(iscsi_tm_rsp, sizeof (iscsi_scsi_task_mgt_rsp_hdr_t));
2372 iscsi_tm_rsp->opcode = ISCSI_OP_SCSI_TASK_MGT_RSP;
2373 iscsi_tm_rsp->flags = ISCSI_FLAG_FINAL;
2374 iscsi_tm_rsp->itt = rx_pdu->isp_hdr->itt;
2377 * Figure out what we're being asked to do.
2379 DTRACE_PROBE4(iscsi__scsi__tm__request,
2380 iscsit_conn_t *, ict,
2381 uint8_t, (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK),
2382 uint32_t, iscsi_tm->rtt,
2383 idm_pdu_t *, rx_pdu);
2384 switch (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK) {
2385 case ISCSI_TM_FUNC_ABORT_TASK:
2387 * STMF doesn't currently support the "abort task" task
2388 * management command although it does support aborting
2389 * an individual task. We'll get STMF to abort the task
2390 * for us but handle the details of the task management
2391 * command ourselves.
2393 * Find the task associated with the referenced task tag.
2395 rtt = iscsi_tm->rtt;
2396 itask = (iscsit_task_t *)idm_task_find_by_handle(ict->ict_ic,
2397 (uintptr_t)rtt);
2399 if (itask == NULL) {
2400 cmdsn = ntohl(iscsi_tm->cmdsn);
2401 refcmdsn = ntohl(iscsi_tm->refcmdsn);
2404 * Task was not found. But the SCSI command could be
2405 * on the rxpdu wait queue. If RefCmdSN is within
2406 * the CmdSN window and less than CmdSN of the TM
2407 * function, return "Function Complete". Otherwise,
2408 * return "Task Does Not Exist".
2411 if (iscsit_cmdsn_in_window(ict, refcmdsn) &&
2412 iscsit_sna_lt(refcmdsn, cmdsn)) {
2413 mutex_enter(&ict->ict_sess->ist_sn_mutex);
2414 if (iscsit_remove_pdu_from_queue(
2415 ict->ict_sess, refcmdsn)) {
2416 iscsit_conn_dispatch_rele(ict);
2418 mutex_exit(&ict->ict_sess->ist_sn_mutex);
2419 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2420 SCSI_TCP_TM_RESP_COMPLETE);
2421 } else {
2422 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2423 SCSI_TCP_TM_RESP_NO_TASK);
2425 } else {
2428 * Tell STMF to abort the task. This will do no harm
2429 * if the task is already complete.
2431 stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task,
2432 STMF_ABORTED, NULL);
2435 * Make sure the task hasn't already completed
2437 mutex_enter(&itask->it_idm_task->idt_mutex);
2438 if ((itask->it_idm_task->idt_state == TASK_COMPLETE) ||
2439 (itask->it_idm_task->idt_state == TASK_IDLE)) {
2441 * Task is complete, return "Task Does Not
2442 * Exist"
2444 mutex_exit(&itask->it_idm_task->idt_mutex);
2445 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2446 SCSI_TCP_TM_RESP_NO_TASK);
2447 } else {
2449 * STMF is now aborting the task, return
2450 * "Function Complete"
2452 mutex_exit(&itask->it_idm_task->idt_mutex);
2453 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2454 SCSI_TCP_TM_RESP_COMPLETE);
2456 idm_task_rele(itask->it_idm_task);
2458 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2459 return;
2461 case ISCSI_TM_FUNC_ABORT_TASK_SET:
2462 tm_func = TM_ABORT_TASK_SET;
2463 break;
2465 case ISCSI_TM_FUNC_CLEAR_ACA:
2466 tm_func = TM_CLEAR_ACA;
2467 break;
2469 case ISCSI_TM_FUNC_CLEAR_TASK_SET:
2470 tm_func = TM_CLEAR_TASK_SET;
2471 break;
2473 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET:
2474 tm_func = TM_LUN_RESET;
2475 break;
2477 case ISCSI_TM_FUNC_TARGET_WARM_RESET:
2478 tm_func = TM_TARGET_WARM_RESET;
2479 break;
2481 case ISCSI_TM_FUNC_TARGET_COLD_RESET:
2482 tm_func = TM_TARGET_COLD_RESET;
2483 break;
2485 case ISCSI_TM_FUNC_TASK_REASSIGN:
2487 * We do not currently support allegiance reassignment. When
2488 * we start supporting ERL1+, we will need to.
2490 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2491 SCSI_TCP_TM_RESP_NO_ALLG_REASSN);
2492 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2493 return;
2495 default:
2496 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2497 SCSI_TCP_TM_RESP_REJECTED);
2498 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2499 return;
2502 tm_itask = iscsit_tm_task_alloc(ict);
2503 if (tm_itask == NULL) {
2504 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2505 SCSI_TCP_TM_RESP_REJECTED);
2506 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2507 return;
2511 task = stmf_task_alloc(ict->ict_sess->ist_lport,
2512 ict->ict_sess->ist_stmf_sess, iscsi_tm->lun,
2513 0, STMF_TASK_EXT_NONE);
2514 if (task == NULL) {
2516 * If this happens, either the LU is in reset, couldn't
2517 * get memory, or some other condition in which we simply
2518 * can't complete this request. It would be nice to return
2519 * an error code like "busy" but the closest we have is
2520 * "rejected".
2522 iscsit_send_task_mgmt_resp(tm_resp_pdu,
2523 SCSI_TCP_TM_RESP_REJECTED);
2524 iscsit_tm_task_free(tm_itask);
2525 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2526 return;
2529 tm_itask->it_tm_pdu = tm_resp_pdu;
2530 tm_itask->it_stmf_task = task;
2531 task->task_port_private = tm_itask;
2532 task->task_mgmt_function = tm_func;
2533 task->task_additional_flags = TASK_AF_NO_EXPECTED_XFER_LENGTH;
2534 task->task_priority = 0;
2535 task->task_max_nbufs = STMF_BUFS_MAX;
2536 task->task_cmd_seq_no = iscsi_tm->itt;
2537 task->task_expected_xfer_length = 0;
2539 stmf_post_task(task, NULL);
2540 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2543 static void
2544 iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2546 iscsi_nop_out_hdr_t *out = (iscsi_nop_out_hdr_t *)rx_pdu->isp_hdr;
2547 iscsi_nop_in_hdr_t *in;
2548 int resp_datalen;
2549 idm_pdu_t *resp;
2551 /* Ignore the response from initiator */
2552 if ((out->itt == ISCSI_RSVD_TASK_TAG) ||
2553 (out->ttt != ISCSI_RSVD_TASK_TAG)) {
2554 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2555 return;
2558 /* Allocate a PDU to respond */
2559 resp_datalen = ntoh24(out->dlength);
2560 resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen);
2561 idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
2562 if (resp_datalen > 0) {
2563 bcopy(rx_pdu->isp_data, resp->isp_data, resp_datalen);
2567 * When sending a NOP-In as a response to a NOP-Out from the initiator,
2568 * the target must respond with the same initiator task tag that was
2569 * provided in the NOP-Out request, the target transfer tag must be
2570 * ISCSI_RSVD_TASK_TAG (0xffffffff) and StatSN will contain the next
2571 * status sequence number. The StatSN for the connection is advanced
2572 * after this PDU is sent.
2574 in = (iscsi_nop_in_hdr_t *)resp->isp_hdr;
2575 bzero(in, sizeof (*in));
2576 in->opcode = ISCSI_OP_NOOP_IN;
2577 in->flags = ISCSI_FLAG_FINAL;
2578 bcopy(out->lun, in->lun, 8);
2579 in->itt = out->itt;
2580 in->ttt = ISCSI_RSVD_TASK_TAG;
2581 hton24(in->dlength, resp_datalen);
2582 resp->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2583 /* Any other field in resp to be set? */
2584 iscsit_pdu_tx(resp);
2585 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2588 static void
2589 iscsit_pdu_op_login_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2593 * Submit PDU to login state machine. State machine will free the
2594 * PDU.
2596 iscsit_login_sm_event(ict, ILE_LOGIN_RCV, rx_pdu);
2599 void
2600 iscsit_pdu_op_logout_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2602 iscsi_logout_hdr_t *logout_req =
2603 (iscsi_logout_hdr_t *)rx_pdu->isp_hdr;
2604 iscsi_logout_rsp_hdr_t *logout_rsp;
2605 idm_pdu_t *resp;
2607 /* Allocate a PDU to respond */
2608 resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2609 idm_pdu_init(resp, ict->ict_ic, NULL, NULL);
2611 * The StatSN is to be sent to the initiator,
2612 * it is not required to increment the number
2613 * as the connection is terminating.
2615 resp->isp_flags |= IDM_PDU_SET_STATSN;
2617 * Logout results in the immediate termination of all tasks except
2618 * if the logout reason is ISCSI_LOGOUT_REASON_RECOVERY. The
2619 * connection state machine will drive this task cleanup automatically
2620 * so we don't need to handle that here.
2622 logout_rsp = (iscsi_logout_rsp_hdr_t *)resp->isp_hdr;
2623 bzero(logout_rsp, sizeof (*logout_rsp));
2624 logout_rsp->opcode = ISCSI_OP_LOGOUT_RSP;
2625 logout_rsp->flags = ISCSI_FLAG_FINAL;
2626 logout_rsp->itt = logout_req->itt;
2627 if ((logout_req->flags & ISCSI_FLAG_LOGOUT_REASON_MASK) >
2628 ISCSI_LOGOUT_REASON_RECOVERY) {
2629 logout_rsp->response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED;
2630 } else {
2631 logout_rsp->response = ISCSI_LOGOUT_SUCCESS;
2634 iscsit_pdu_tx(resp);
2635 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
2639 * Calculate the number of outstanding commands we can process
2642 iscsit_cmd_window()
2645 * Instead of using a pre-defined constant for the command window,
2646 * it should be made confiurable and dynamic. With MC/S, sequence
2647 * numbers will be used up at a much faster rate than with SC/S.
2649 return (ISCSIT_MAX_WINDOW);
2653 * Set local registers based on incoming PDU
2655 void
2656 iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu)
2658 iscsit_sess_t *ist;
2659 iscsi_scsi_cmd_hdr_t *req;
2661 ist = ict->ict_sess;
2663 req = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
2664 if (req->opcode & ISCSI_OP_IMMEDIATE) {
2665 /* no cmdsn increment for immediate PDUs */
2666 return;
2669 /* Ensure that the ExpCmdSN advances in an orderly manner */
2670 mutex_enter(&ist->ist_sn_mutex);
2671 ist->ist_expcmdsn = ntohl(req->cmdsn) + 1;
2672 ist->ist_maxcmdsn = ntohl(req->cmdsn) + iscsit_cmd_window();
2673 mutex_exit(&ist->ist_sn_mutex);
2677 * Wrapper funtion, calls iscsi_calc_rspsn and idm_pdu_tx
2679 void
2680 iscsit_pdu_tx(idm_pdu_t *pdu)
2682 iscsit_conn_t *ict = pdu->isp_ic->ic_handle;
2683 iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
2684 iscsit_sess_t *ist = ict->ict_sess;
2687 * The command sequence numbers are session-wide and must stay
2688 * consistent across the transfer, so protect the cmdsn with a
2689 * mutex lock on the session. The status sequence number will
2690 * be updated just before the transport layer transmits the PDU.
2693 mutex_enter(&ict->ict_sess->ist_sn_mutex);
2694 /* Set ExpCmdSN and MaxCmdSN */
2695 rsp->maxcmdsn = htonl(ist->ist_maxcmdsn);
2696 rsp->expcmdsn = htonl(ist->ist_expcmdsn);
2697 idm_pdu_tx(pdu);
2698 mutex_exit(&ict->ict_sess->ist_sn_mutex);
2702 * Internal functions
2705 void
2706 iscsit_send_async_event(iscsit_conn_t *ict, uint8_t event)
2708 idm_pdu_t *abt;
2709 iscsi_async_evt_hdr_t *async_abt;
2712 * Get a PDU to build the abort request.
2714 abt = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0);
2715 if (abt == NULL) {
2716 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, (uintptr_t)NULL);
2717 return;
2721 * A asynchronous message is sent by the target to request a logout.
2722 * The StatSN for the connection is advanced after the PDU is sent
2723 * to allow for initiator and target state synchronization.
2725 idm_pdu_init(abt, ict->ict_ic, NULL, NULL);
2726 abt->isp_datalen = 0;
2727 abt->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2729 async_abt = (iscsi_async_evt_hdr_t *)abt->isp_hdr;
2730 bzero(async_abt, sizeof (*async_abt));
2731 async_abt->opcode = ISCSI_OP_ASYNC_EVENT;
2732 async_abt->async_event = event;
2733 async_abt->flags = ISCSI_FLAG_FINAL;
2734 async_abt->rsvd4[0] = 0xff;
2735 async_abt->rsvd4[1] = 0xff;
2736 async_abt->rsvd4[2] = 0xff;
2737 async_abt->rsvd4[3] = 0xff;
2739 switch (event) {
2740 case ISCSI_ASYNC_EVENT_REQUEST_LOGOUT:
2741 async_abt->param3 = htons(IDM_LOGOUT_SECONDS);
2742 break;
2743 case ISCSI_ASYNC_EVENT_SCSI_EVENT:
2744 case ISCSI_ASYNC_EVENT_DROPPING_CONNECTION:
2745 case ISCSI_ASYNC_EVENT_DROPPING_ALL_CONNECTIONS:
2746 case ISCSI_ASYNC_EVENT_PARAM_NEGOTIATION:
2747 default:
2748 ASSERT(0);
2751 iscsit_pdu_tx(abt);
2754 void
2755 iscsit_send_reject(iscsit_conn_t *ict, idm_pdu_t *rejected_pdu, uint8_t reason)
2757 idm_pdu_t *reject_pdu;
2758 iscsi_reject_rsp_hdr_t *reject;
2761 * Get a PDU to build the abort request.
2763 reject_pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t),
2764 rejected_pdu->isp_hdrlen);
2765 if (reject_pdu == NULL) {
2766 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, (uintptr_t)NULL);
2767 return;
2769 idm_pdu_init(reject_pdu, ict->ict_ic, NULL, NULL);
2770 /* StatSN is advanced after a Reject PDU */
2771 reject_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN;
2772 reject_pdu->isp_datalen = rejected_pdu->isp_hdrlen;
2773 bcopy(rejected_pdu->isp_hdr, reject_pdu->isp_data,
2774 rejected_pdu->isp_hdrlen);
2776 reject = (iscsi_reject_rsp_hdr_t *)reject_pdu->isp_hdr;
2777 bzero(reject, sizeof (*reject));
2778 reject->opcode = ISCSI_OP_REJECT_MSG;
2779 reject->reason = reason;
2780 reject->flags = ISCSI_FLAG_FINAL;
2781 hton24(reject->dlength, rejected_pdu->isp_hdrlen);
2782 reject->must_be_ff[0] = 0xff;
2783 reject->must_be_ff[1] = 0xff;
2784 reject->must_be_ff[2] = 0xff;
2785 reject->must_be_ff[3] = 0xff;
2787 iscsit_pdu_tx(reject_pdu);
2791 static iscsit_task_t *
2792 iscsit_task_alloc(iscsit_conn_t *ict)
2794 iscsit_task_t *itask;
2795 iscsit_buf_t *immed_ibuf;
2798 * Possible items to pre-alloc if we cache iscsit_task_t's:
2800 * Status PDU w/ sense buffer
2801 * stmf_data_buf_t for immediate data
2803 itask = kmem_alloc(sizeof (iscsit_task_t) + sizeof (iscsit_buf_t) +
2804 sizeof (stmf_data_buf_t), KM_NOSLEEP);
2805 if (itask != NULL) {
2806 mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2807 itask->it_aborted = itask->it_stmf_abort =
2808 itask->it_tm_task = 0;
2810 immed_ibuf = (iscsit_buf_t *)(itask + 1);
2811 bzero(immed_ibuf, sizeof (*immed_ibuf));
2812 immed_ibuf->ibuf_is_immed = B_TRUE;
2813 immed_ibuf->ibuf_stmf_buf = (stmf_data_buf_t *)(immed_ibuf + 1);
2815 bzero(immed_ibuf->ibuf_stmf_buf, sizeof (stmf_data_buf_t));
2816 immed_ibuf->ibuf_stmf_buf->db_port_private = immed_ibuf;
2817 immed_ibuf->ibuf_stmf_buf->db_sglist_length = 1;
2818 immed_ibuf->ibuf_stmf_buf->db_flags = DB_DIRECTION_FROM_RPORT |
2819 DB_DONT_CACHE;
2820 itask->it_immed_data = immed_ibuf;
2821 itask->it_idm_task = idm_task_alloc(ict->ict_ic);
2822 if (itask->it_idm_task != NULL) {
2823 itask->it_idm_task->idt_private = itask;
2824 itask->it_ict = ict;
2825 itask->it_ttt = itask->it_idm_task->idt_tt;
2826 return (itask);
2827 } else {
2828 kmem_free(itask, sizeof (iscsit_task_t) +
2829 sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2833 return (NULL);
2836 static void
2837 iscsit_task_free(iscsit_task_t *itask)
2839 idm_task_free(itask->it_idm_task);
2840 mutex_destroy(&itask->it_mutex);
2841 kmem_free(itask, sizeof (iscsit_task_t) +
2842 sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t));
2845 static iscsit_task_t *
2846 iscsit_tm_task_alloc(iscsit_conn_t *ict)
2848 iscsit_task_t *itask;
2850 itask = kmem_zalloc(sizeof (iscsit_task_t), KM_NOSLEEP);
2851 if (itask != NULL) {
2852 idm_conn_hold(ict->ict_ic);
2853 mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL);
2854 itask->it_aborted = itask->it_stmf_abort =
2855 itask->it_tm_responded = 0;
2856 itask->it_tm_pdu = NULL;
2857 itask->it_tm_task = 1;
2858 itask->it_ict = ict;
2861 return (itask);
2864 static void
2865 iscsit_tm_task_free(iscsit_task_t *itask)
2868 * If we responded then the call to idm_pdu_complete will free the
2869 * PDU. Otherwise we got aborted before the TM function could
2870 * complete and we need to free the PDU explicitly.
2872 if (itask->it_tm_pdu != NULL && !itask->it_tm_responded)
2873 idm_pdu_free(itask->it_tm_pdu);
2874 idm_conn_rele(itask->it_ict->ict_ic);
2875 mutex_destroy(&itask->it_mutex);
2876 kmem_free(itask, sizeof (iscsit_task_t));
2879 static idm_status_t
2880 iscsit_task_start(iscsit_task_t *itask)
2882 iscsit_sess_t *ist = itask->it_ict->ict_sess;
2883 avl_index_t where;
2886 * Sanity check the ITT and ensure that this task does not already
2887 * exist. If not then add the task to the session task list.
2889 mutex_enter(&ist->ist_mutex);
2890 mutex_enter(&itask->it_mutex);
2891 itask->it_active = 1;
2892 if (avl_find(&ist->ist_task_list, itask, &where) == NULL) {
2893 /* New task, add to AVL */
2894 avl_insert(&ist->ist_task_list, itask, where);
2895 mutex_exit(&itask->it_mutex);
2896 mutex_exit(&ist->ist_mutex);
2897 return (IDM_STATUS_SUCCESS);
2899 mutex_exit(&itask->it_mutex);
2900 mutex_exit(&ist->ist_mutex);
2902 return (IDM_STATUS_REJECT);
2905 static void
2906 iscsit_task_done(iscsit_task_t *itask)
2908 iscsit_sess_t *ist = itask->it_ict->ict_sess;
2910 mutex_enter(&ist->ist_mutex);
2911 mutex_enter(&itask->it_mutex);
2912 if (itask->it_active) {
2913 avl_remove(&ist->ist_task_list, itask);
2914 itask->it_active = 0;
2916 mutex_exit(&itask->it_mutex);
2917 mutex_exit(&ist->ist_mutex);
2921 * iscsit status PDU cache
2924 /*ARGSUSED*/
2925 static int
2926 iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags)
2928 idm_pdu_t *pdu = pdu_void;
2929 iscsi_scsi_rsp_hdr_t *rsp;
2931 bzero(pdu, sizeof (idm_pdu_t));
2932 pdu->isp_callback = iscsit_send_good_status_done;
2933 pdu->isp_magic = IDM_PDU_MAGIC;
2934 pdu->isp_hdr = (iscsi_hdr_t *)(pdu + 1); /* Ptr arithmetic */
2935 pdu->isp_hdrlen = sizeof (iscsi_hdr_t);
2937 /* Setup status response */
2938 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr;
2939 bzero(rsp, sizeof (*rsp));
2940 rsp->opcode = ISCSI_OP_SCSI_RSP;
2941 rsp->flags = ISCSI_FLAG_FINAL;
2942 rsp->response = ISCSI_STATUS_CMD_COMPLETED;
2944 return (0);
2948 * iscsit private data handler
2951 /*ARGSUSED*/
2952 static void
2953 iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags)
2955 it_config_t *cfg;
2956 nvlist_t *nvl;
2957 iscsit_service_enabled_t old_state;
2959 if ((cmd != STMF_PROVIDER_DATA_UPDATED) || (arg == NULL)) {
2960 return;
2963 nvl = (nvlist_t *)arg;
2965 /* Translate nvlist */
2966 if (it_nv_to_config(nvl, &cfg) != 0) {
2967 cmn_err(CE_WARN, "Configuration is invalid");
2968 return;
2971 /* Check that no iSCSI ioctl is currently running */
2972 mutex_enter(&iscsit_global.global_state_mutex);
2973 old_state = iscsit_global.global_svc_state;
2974 switch (iscsit_global.global_svc_state) {
2975 case ISE_ENABLED:
2976 case ISE_DISABLED:
2977 iscsit_global.global_svc_state = ISE_BUSY;
2978 break;
2979 case ISE_ENABLING:
2981 * It is OK for the iscsit_pp_cb to be called from inside of
2982 * an iSCSI ioctl only if we are currently executing inside
2983 * of stmf_register_port_provider.
2985 ASSERT((flags & STMF_PCB_PREG_COMPLETE) != 0);
2986 break;
2987 default:
2988 cmn_err(CE_WARN, "iscsit_pp_cb called when global_svc_state"
2989 " is not ENABLED(0x%x) -- ignoring",
2990 iscsit_global.global_svc_state);
2991 mutex_exit(&iscsit_global.global_state_mutex);
2992 it_config_free_cmn(cfg);
2993 return;
2995 mutex_exit(&iscsit_global.global_state_mutex);
2997 /* Update config */
2998 (void) iscsit_config_merge(cfg);
3000 it_config_free_cmn(cfg);
3002 /* Restore old iSCSI driver global state */
3003 mutex_enter(&iscsit_global.global_state_mutex);
3004 ASSERT(iscsit_global.global_svc_state == ISE_BUSY ||
3005 iscsit_global.global_svc_state == ISE_ENABLING);
3006 iscsit_global.global_svc_state = old_state;
3007 mutex_exit(&iscsit_global.global_state_mutex);
3011 static it_cfg_status_t
3012 iscsit_config_merge(it_config_t *in_cfg)
3014 it_cfg_status_t status;
3015 it_config_t *cfg;
3016 it_config_t tmp_cfg;
3017 list_t tpg_del_list;
3019 if (in_cfg) {
3020 cfg = in_cfg;
3021 } else {
3022 /* Make empty config */
3023 bzero(&tmp_cfg, sizeof (tmp_cfg));
3024 cfg = &tmp_cfg;
3027 list_create(&tpg_del_list, sizeof (iscsit_tpg_t),
3028 offsetof(iscsit_tpg_t, tpg_delete_ln));
3031 * Update targets, initiator contexts, target portal groups,
3032 * and iSNS client
3034 ISCSIT_GLOBAL_LOCK(RW_WRITER);
3035 if (((status = iscsit_config_merge_tpg(cfg, &tpg_del_list))
3036 != 0) ||
3037 ((status = iscsit_config_merge_tgt(cfg)) != 0) ||
3038 ((status = iscsit_config_merge_ini(cfg)) != 0) ||
3039 ((status = isnst_config_merge(cfg)) != 0)) {
3040 ISCSIT_GLOBAL_UNLOCK();
3041 return (status);
3044 /* Update other global config parameters */
3045 if (iscsit_global.global_props) {
3046 nvlist_free(iscsit_global.global_props);
3047 iscsit_global.global_props = NULL;
3049 if (in_cfg) {
3050 (void) nvlist_dup(cfg->config_global_properties,
3051 &iscsit_global.global_props, KM_SLEEP);
3053 ISCSIT_GLOBAL_UNLOCK();
3055 iscsit_config_destroy_tpgs(&tpg_del_list);
3057 list_destroy(&tpg_del_list);
3059 return (ITCFG_SUCCESS);
3063 * iscsit_sna_lt[e]
3065 * Compare serial numbers using serial number arithmetic as defined in
3066 * RFC 1982.
3068 * NOTE: This code is duplicated in the isns server. It ought to be common.
3071 static int
3072 iscsit_sna_lt(uint32_t sn1, uint32_t sn2)
3074 return ((sn1 != sn2) &&
3075 (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
3076 ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
3079 static int
3080 iscsit_sna_lte(uint32_t sn1, uint32_t sn2)
3082 return ((sn1 == sn2) ||
3083 (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) ||
3084 ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK))));
3088 static boolean_t
3089 iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn)
3091 iscsit_sess_t *ist = ict->ict_sess;
3092 int rval = B_TRUE;
3094 ist = ict->ict_sess;
3096 mutex_enter(&ist->ist_sn_mutex);
3099 * If cmdsn is less than ist_expcmdsn - iscsit_cmd_window() or
3100 * greater than ist_expcmdsn, it's not in the window.
3103 if (iscsit_sna_lt(cmdsn, (ist->ist_expcmdsn - iscsit_cmd_window())) ||
3104 !iscsit_sna_lte(cmdsn, ist->ist_expcmdsn)) {
3105 rval = B_FALSE;
3108 mutex_exit(&ist->ist_sn_mutex);
3110 return (rval);
3114 * iscsit_check_cmdsn_and_queue
3116 * Independent of the order in which the iSCSI target receives non-immediate
3117 * command PDU across the entire session and any multiple connections within
3118 * the session, the target must deliver the commands to the SCSI layer in
3119 * CmdSN order. So out-of-order non-immediate commands are queued up on a
3120 * session-wide wait queue. Duplicate commands are ignored.
3122 * returns B_TRUE for commands which can be executed immediately (are
3123 * non-deferred), B_FALSE for cases where a command was deferred or invalid.
3125 static boolean_t
3126 iscsit_check_cmdsn_and_queue(idm_pdu_t *rx_pdu)
3128 idm_conn_t *ic = rx_pdu->isp_ic;
3129 iscsit_conn_t *ict = ic->ic_handle;
3130 iscsit_sess_t *ist = ict->ict_sess;
3131 iscsi_scsi_cmd_hdr_t *hdr = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr;
3133 mutex_enter(&ist->ist_sn_mutex);
3134 if (hdr->opcode & ISCSI_OP_IMMEDIATE) {
3135 /* do not queue, handle it immediately */
3136 DTRACE_PROBE2(immediate__cmd, iscsit_sess_t *, ist,
3137 idm_pdu_t *, rx_pdu);
3138 mutex_exit(&ist->ist_sn_mutex);
3139 return (B_TRUE);
3142 * See RFC3270 3.1.1.2: non-immediate commands outside of the
3143 * expected window (from expcmdsn to maxcmdsn, inclusive)
3144 * should be silently ignored.
3146 if (iscsit_sna_lt(ist->ist_expcmdsn, ntohl(hdr->cmdsn)) &&
3147 iscsit_sna_lt(ntohl(hdr->cmdsn), ist->ist_maxcmdsn)) {
3149 * Out-of-order commands (cmdSN higher than ExpCmdSN)
3150 * are staged on a fixed-size circular buffer until
3151 * the missing command is delivered to the SCSI layer.
3152 * Irrespective of the order of insertion into the
3153 * staging queue, the commands are processed out of the
3154 * queue in cmdSN order only.
3156 rx_pdu->isp_queue_time = gethrtime();
3157 iscsit_add_pdu_to_queue(ist, rx_pdu);
3158 mutex_exit(&ist->ist_sn_mutex);
3159 return (B_FALSE);
3160 } else if (iscsit_sna_lt(ntohl(hdr->cmdsn), ist->ist_expcmdsn) ||
3161 iscsit_sna_lt(ist->ist_maxcmdsn, ntohl(hdr->cmdsn))) {
3163 * See above, this command is outside of our acceptable
3164 * window, we need to discard/complete.
3166 DTRACE_PROBE3(cmdsn__lt__expcmdsn, iscsit_sess_t *, ist,
3167 iscsit_conn_t *, ict, idm_pdu_t *, rx_pdu);
3168 mutex_exit(&ist->ist_sn_mutex);
3169 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS);
3171 * tell our callers that the PDU "finished."
3173 return (B_FALSE);
3174 } else {
3175 mutex_exit(&ist->ist_sn_mutex);
3176 return (B_TRUE);
3181 * iscsit_add_pdu_to_queue() adds PDUs into the array indexed by
3182 * their cmdsn value. The length of the array is kept above the
3183 * maximum window size. The window keeps the cmdsn within a range
3184 * such that there are no collisons. e.g. the assumption is that
3185 * the windowing checks make it impossible to receive PDUs that
3186 * index into the same location in the array.
3188 static void
3189 iscsit_add_pdu_to_queue(iscsit_sess_t *ist, idm_pdu_t *rx_pdu)
3191 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3192 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
3193 uint32_t cmdsn =
3194 ((iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr)->cmdsn;
3195 uint32_t index;
3197 ASSERT(MUTEX_HELD(&ist->ist_sn_mutex));
3199 * If the connection is being torn down, then
3200 * don't add the PDU to the staging queue
3202 mutex_enter(&ict->ict_mutex);
3203 if (ict->ict_lost) {
3204 mutex_exit(&ict->ict_mutex);
3205 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
3206 return;
3208 iscsit_conn_dispatch_hold(ict);
3209 mutex_exit(&ict->ict_mutex);
3211 index = ntohl(cmdsn) % ISCSIT_RXPDU_QUEUE_LEN;
3213 * In the normal case, assuming that the Initiator is not
3214 * buggy and that we don't have packet duplication occuring,
3215 * the entry in the array will be NULL. However, we may have
3216 * received a duplicate PDU with cmdsn > expsn , and in that
3217 * case we just ignore this PDU -- the previously received one
3218 * remains queued for processing. We need to be careful not
3219 * to leak this one however.
3221 if (cbuf->cb_buffer[index] != NULL) {
3222 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL);
3223 } else {
3224 cbuf->cb_buffer[index] = rx_pdu;
3225 cbuf->cb_num_elems++;
3229 static idm_pdu_t *
3230 iscsit_remove_pdu_from_queue(iscsit_sess_t *ist, uint32_t cmdsn)
3232 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3233 idm_pdu_t *pdu = NULL;
3234 uint32_t index;
3236 ASSERT(MUTEX_HELD(&ist->ist_sn_mutex));
3237 index = cmdsn % ISCSIT_RXPDU_QUEUE_LEN;
3238 if ((pdu = cbuf->cb_buffer[index]) != NULL) {
3239 ASSERT(cmdsn ==
3240 ntohl(((iscsi_scsi_cmd_hdr_t *)pdu->isp_hdr)->cmdsn));
3241 cbuf->cb_buffer[index] = NULL;
3242 cbuf->cb_num_elems--;
3243 return (pdu);
3245 return (NULL);
3249 * iscsit_process_pdu_in_queue() finds the next pdu in sequence
3250 * and posts it to the SCSI layer
3252 static void
3253 iscsit_process_pdu_in_queue(iscsit_sess_t *ist)
3255 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3256 idm_pdu_t *pdu = NULL;
3257 uint32_t expcmdsn;
3259 for (;;) {
3260 mutex_enter(&ist->ist_sn_mutex);
3261 if (cbuf->cb_num_elems == 0) {
3262 mutex_exit(&ist->ist_sn_mutex);
3263 break;
3265 expcmdsn = ist->ist_expcmdsn;
3266 if ((pdu = iscsit_remove_pdu_from_queue(ist, expcmdsn))
3267 == NULL) {
3268 mutex_exit(&ist->ist_sn_mutex);
3269 break;
3271 mutex_exit(&ist->ist_sn_mutex);
3272 iscsit_post_staged_pdu(pdu);
3276 static void
3277 iscsit_post_staged_pdu(idm_pdu_t *rx_pdu)
3279 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle;
3281 /* Post the PDU to the SCSI layer */
3282 switch (IDM_PDU_OPCODE(rx_pdu)) {
3283 case ISCSI_OP_NOOP_OUT:
3284 iscsit_set_cmdsn(ict, rx_pdu);
3285 iscsit_pdu_op_noop(ict, rx_pdu);
3286 break;
3287 case ISCSI_OP_TEXT_CMD:
3288 iscsit_set_cmdsn(ict, rx_pdu);
3289 iscsit_pdu_op_text_cmd(ict, rx_pdu);
3290 break;
3291 case ISCSI_OP_SCSI_TASK_MGT_MSG:
3292 iscsit_set_cmdsn(ict, rx_pdu);
3293 iscsit_op_scsi_task_mgmt(ict, rx_pdu);
3294 break;
3295 case ISCSI_OP_SCSI_CMD:
3296 /* cmdSN will be incremented after creating itask */
3297 iscsit_post_scsi_cmd(rx_pdu->isp_ic, rx_pdu);
3298 break;
3299 case ISCSI_OP_LOGOUT_CMD:
3300 iscsit_set_cmdsn(ict, rx_pdu);
3301 iscsit_pdu_op_logout_cmd(ict, rx_pdu);
3302 break;
3303 default:
3304 /* No other PDUs should be placed on the queue */
3305 ASSERT(0);
3307 iscsit_conn_dispatch_rele(ict); /* release hold on the conn */
3310 /* ARGSUSED */
3311 void
3312 iscsit_rxpdu_queue_monitor_start(void)
3314 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3315 if (iscsit_rxpdu_queue_monitor_thr_running) {
3316 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3317 return;
3319 iscsit_rxpdu_queue_monitor_thr_id =
3320 thread_create(NULL, 0, iscsit_rxpdu_queue_monitor, NULL,
3321 0, &p0, TS_RUN, minclsyspri);
3322 while (!iscsit_rxpdu_queue_monitor_thr_running) {
3323 cv_wait(&iscsit_rxpdu_queue_monitor_cv,
3324 &iscsit_rxpdu_queue_monitor_mutex);
3326 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3330 /* ARGSUSED */
3331 void
3332 iscsit_rxpdu_queue_monitor_stop(void)
3334 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3335 if (iscsit_rxpdu_queue_monitor_thr_running) {
3336 iscsit_rxpdu_queue_monitor_thr_running = B_FALSE;
3337 cv_signal(&iscsit_rxpdu_queue_monitor_cv);
3338 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3340 thread_join(iscsit_rxpdu_queue_monitor_thr_did);
3341 return;
3343 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3347 * A separate thread is used to scan the staging queue on all the
3348 * sessions, If a delayed PDU does not arrive within a timeout, the
3349 * target will advance to the staged PDU that is next in sequence
3350 * and exceeded the threshold wait time. It is up to the initiator
3351 * to note that the target has not acknowledged a particular cmdsn
3352 * and take appropriate action.
3354 /* ARGSUSED */
3355 static void
3356 iscsit_rxpdu_queue_monitor(void *arg)
3358 iscsit_tgt_t *tgt;
3359 iscsit_sess_t *ist;
3361 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex);
3362 iscsit_rxpdu_queue_monitor_thr_did = curthread->t_did;
3363 iscsit_rxpdu_queue_monitor_thr_running = B_TRUE;
3364 cv_signal(&iscsit_rxpdu_queue_monitor_cv);
3366 while (iscsit_rxpdu_queue_monitor_thr_running) {
3367 ISCSIT_GLOBAL_LOCK(RW_READER);
3368 for (tgt = avl_first(&iscsit_global.global_target_list);
3369 tgt != NULL;
3370 tgt = AVL_NEXT(&iscsit_global.global_target_list, tgt)) {
3371 mutex_enter(&tgt->target_mutex);
3372 for (ist = avl_first(&tgt->target_sess_list);
3373 ist != NULL;
3374 ist = AVL_NEXT(&tgt->target_sess_list, ist)) {
3376 iscsit_rxpdu_queue_monitor_session(ist);
3378 mutex_exit(&tgt->target_mutex);
3380 ISCSIT_GLOBAL_UNLOCK();
3381 if (iscsit_rxpdu_queue_monitor_thr_running == B_FALSE) {
3382 break;
3384 (void) cv_reltimedwait(&iscsit_rxpdu_queue_monitor_cv,
3385 &iscsit_rxpdu_queue_monitor_mutex,
3386 ISCSIT_RXPDU_QUEUE_MONITOR_INTERVAL * drv_usectohz(1000000),
3387 TR_CLOCK_TICK);
3389 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex);
3390 thread_exit();
3393 static void
3394 iscsit_rxpdu_queue_monitor_session(iscsit_sess_t *ist)
3396 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue;
3397 idm_pdu_t *next_pdu = NULL;
3398 uint32_t index, next_cmdsn, i;
3401 * Assume that all PDUs in the staging queue have a cmdsn >= expcmdsn.
3402 * Starting with the expcmdsn, iterate over the staged PDUs to find
3403 * the next PDU with a wait time greater than the threshold. If found
3404 * advance the staged PDU to the SCSI layer, skipping over the missing
3405 * PDU(s) to get past the hole in the command sequence. It is up to
3406 * the initiator to note that the target has not acknowledged a cmdsn
3407 * and take appropriate action.
3409 * Since the PDU(s) arrive in any random order, it is possible that
3410 * that the actual wait time for a particular PDU is much longer than
3411 * the defined threshold. e.g. Consider a case where commands are sent
3412 * over 4 different connections, and cmdsn = 1004 arrives first, then
3413 * 1003, and 1002 and 1001 are lost due to a connection failure.
3414 * So now 1003 is waiting for 1002 to be delivered, and although the
3415 * wait time of 1004 > wait time of 1003, only 1003 will be considered
3416 * by the monitor thread. 1004 will be automatically processed by
3417 * iscsit_process_pdu_in_queue() once the scan is complete and the
3418 * expcmdsn becomes current.
3420 mutex_enter(&ist->ist_sn_mutex);
3421 cbuf = ist->ist_rxpdu_queue;
3422 if (cbuf->cb_num_elems == 0) {
3423 mutex_exit(&ist->ist_sn_mutex);
3424 return;
3426 for (next_pdu = NULL, i = 0; ; i++) {
3427 next_cmdsn = ist->ist_expcmdsn + i; /* start at expcmdsn */
3428 index = next_cmdsn % ISCSIT_RXPDU_QUEUE_LEN;
3429 if ((next_pdu = cbuf->cb_buffer[index]) != NULL) {
3431 * If the PDU wait time has not exceeded threshold
3432 * stop scanning the staging queue until the timer
3433 * fires again
3435 if ((gethrtime() - next_pdu->isp_queue_time)
3436 < (rxpdu_queue_threshold * NANOSEC)) {
3437 mutex_exit(&ist->ist_sn_mutex);
3438 return;
3441 * Remove the next PDU from the queue and post it
3442 * to the SCSI layer, skipping over the missing
3443 * PDU. Stop scanning the staging queue until
3444 * the monitor timer fires again
3446 (void) iscsit_remove_pdu_from_queue(ist, next_cmdsn);
3447 mutex_exit(&ist->ist_sn_mutex);
3448 DTRACE_PROBE3(advanced__to__blocked__cmdsn,
3449 iscsit_sess_t *, ist, idm_pdu_t *, next_pdu,
3450 uint32_t, next_cmdsn);
3451 iscsit_post_staged_pdu(next_pdu);
3452 /* Deliver any subsequent PDUs immediately */
3453 iscsit_process_pdu_in_queue(ist);
3454 return;
3457 * Skipping over i PDUs, e.g. a case where commands 1001 and
3458 * 1002 are lost in the network, skip over both and post 1003
3459 * expcmdsn then becomes 1004 at the end of the scan.
3461 DTRACE_PROBE2(skipping__over__cmdsn, iscsit_sess_t *, ist,
3462 uint32_t, next_cmdsn);
3465 * following the assumption, staged cmdsn >= expcmdsn, this statement
3466 * is never reached.