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