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