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