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 idm_refcnt_wait_ref(&ict->ict_refcnt); 1371 /* 1372 * The session state machine does not need to post 1373 * events to IDM any longer, so it is safe to set 1374 * the idm connection reference to NULL 1375 */ 1376 ict->ict_ic = NULL; 1377 1378 /* Reap the login state machine */ 1379 iscsit_login_sm_fini(ict); 1380 1381 /* Clean up any text command remnants */ 1382 iscsit_text_cmd_fini(ict); 1383 1384 mutex_destroy(&ict->ict_mutex); 1385 idm_refcnt_destroy(&ict->ict_refcnt); 1386 kmem_free(ict, sizeof (*ict)); 1387 1388 iscsit_global_rele(); 1389 1390 return (IDM_STATUS_SUCCESS); 1391 } 1392 1393 void 1394 iscsit_conn_logout(iscsit_conn_t *ict) 1395 { 1396 /* 1397 * If the iscsi connection is active, then 1398 * logout the IDM connection by sending a 1399 * CE_LOGOUT_SESSION_SUCCESS, else, no action 1400 * needs to be taken because the connection 1401 * is already in the teardown process. 1402 */ 1403 mutex_enter(&ict->ict_mutex); 1404 if (ict->ict_lost == B_FALSE && ict->ict_destroyed == B_FALSE) { 1405 idm_conn_event(ict->ict_ic, CE_LOGOUT_SESSION_SUCCESS, NULL); 1406 } 1407 mutex_exit(&ict->ict_mutex); 1408 } 1409 1410 /* 1411 * STMF-related functions 1412 * 1413 * iSCSI to STMF mapping 1414 * 1415 * Session == ? 1416 * Connection == bound to local port but not itself a local port 1417 * Target 1418 * Target portal (group?) == local port (really but we're not going to do this) 1419 * iscsit needs to map connections to local ports (whatever we decide 1420 * they are) 1421 * Target == ? 1422 */ 1423 1424 /*ARGSUSED*/ 1425 static stmf_data_buf_t * 1426 iscsit_dbuf_alloc(scsi_task_t *task, uint32_t size, uint32_t *pminsize, 1427 uint32_t flags) 1428 { 1429 iscsit_task_t *itask = task->task_port_private; 1430 idm_buf_t *idm_buffer; 1431 iscsit_buf_t *ibuf; 1432 stmf_data_buf_t *result; 1433 uint32_t bsize; 1434 1435 /* 1436 * If the requested size is larger than MaxBurstLength and the 1437 * given pminsize is also larger than MaxBurstLength, then the 1438 * allocation fails (dbuf = NULL) and pminsize is modified to 1439 * be equal to MaxBurstLength. stmf/sbd then should re-invoke 1440 * this function with the corrected values for transfer. 1441 */ 1442 ASSERT(pminsize); 1443 if (size <= itask->it_ict->ict_op.op_max_burst_length) { 1444 bsize = size; 1445 } else if (*pminsize <= itask->it_ict->ict_op.op_max_burst_length) { 1446 bsize = itask->it_ict->ict_op.op_max_burst_length; 1447 } else { 1448 *pminsize = itask->it_ict->ict_op.op_max_burst_length; 1449 return (NULL); 1450 } 1451 1452 /* Alloc buffer */ 1453 idm_buffer = idm_buf_alloc(itask->it_ict->ict_ic, NULL, bsize); 1454 if (idm_buffer != NULL) { 1455 result = stmf_alloc(STMF_STRUCT_DATA_BUF, 1456 sizeof (iscsit_buf_t), 0); 1457 if (result != NULL) { 1458 /* Fill in stmf_data_buf_t */ 1459 ibuf = result->db_port_private; 1460 ibuf->ibuf_idm_buf = idm_buffer; 1461 ibuf->ibuf_stmf_buf = result; 1462 ibuf->ibuf_is_immed = B_FALSE; 1463 result->db_flags = DB_DONT_CACHE; 1464 result->db_buf_size = bsize; 1465 result->db_data_size = bsize; 1466 result->db_sglist_length = 1; 1467 result->db_sglist[0].seg_addr = idm_buffer->idb_buf; 1468 result->db_sglist[0].seg_length = 1469 idm_buffer->idb_buflen; 1470 return (result); 1471 } 1472 1473 /* Couldn't get the stmf_data_buf_t so free the buffer */ 1474 idm_buf_free(idm_buffer); 1475 } 1476 1477 return (NULL); 1478 } 1479 1480 /*ARGSUSED*/ 1481 static void 1482 iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf) 1483 { 1484 iscsit_buf_t *ibuf = dbuf->db_port_private; 1485 1486 if (ibuf->ibuf_is_immed) { 1487 /* 1488 * The iscsit_buf_t structure itself will be freed with its 1489 * associated task. Here we just need to free the PDU that 1490 * held the immediate data. 1491 */ 1492 idm_pdu_complete(ibuf->ibuf_immed_data_pdu, IDM_STATUS_SUCCESS); 1493 ibuf->ibuf_immed_data_pdu = 0; 1494 } else { 1495 idm_buf_free(ibuf->ibuf_idm_buf); 1496 stmf_free(dbuf); 1497 } 1498 } 1499 1500 /*ARGSUSED*/ 1501 stmf_status_t 1502 iscsit_xfer_scsi_data(scsi_task_t *task, stmf_data_buf_t *dbuf, 1503 uint32_t ioflags) 1504 { 1505 iscsit_task_t *iscsit_task = task->task_port_private; 1506 iscsit_sess_t *ict_sess = iscsit_task->it_ict->ict_sess; 1507 iscsit_buf_t *ibuf = dbuf->db_port_private; 1508 int idm_rc; 1509 1510 /* 1511 * If we are aborting then we can ignore this request 1512 */ 1513 if (iscsit_task->it_stmf_abort) { 1514 return (STMF_SUCCESS); 1515 } 1516 1517 /* 1518 * If it's not immediate data then start the transfer 1519 */ 1520 ASSERT(ibuf->ibuf_is_immed == B_FALSE); 1521 if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) { 1522 /* 1523 * The DB_SEND_STATUS_GOOD flag in the STMF data buffer allows 1524 * the port provider to phase-collapse, i.e. send the status 1525 * along with the final data PDU for the command. The port 1526 * provider passes this request to the transport layer by 1527 * setting a flag IDM_TASK_PHASECOLLAPSE_REQ in the task. 1528 */ 1529 if (dbuf->db_flags & DB_SEND_STATUS_GOOD) 1530 iscsit_task->it_idm_task->idt_flags |= 1531 IDM_TASK_PHASECOLLAPSE_REQ; 1532 /* 1533 * IDM will call iscsit_build_hdr so lock now to serialize 1534 * access to the SN values. We need to lock here to enforce 1535 * lock ordering 1536 */ 1537 mutex_enter(&ict_sess->ist_sn_mutex); 1538 idm_rc = idm_buf_tx_to_ini(iscsit_task->it_idm_task, 1539 ibuf->ibuf_idm_buf, dbuf->db_relative_offset, 1540 dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf); 1541 mutex_exit(&ict_sess->ist_sn_mutex); 1542 1543 return (iscsit_idm_to_stmf(idm_rc)); 1544 } else if (dbuf->db_flags & DB_DIRECTION_FROM_RPORT) { 1545 /* Grab the SN lock (see comment above) */ 1546 mutex_enter(&ict_sess->ist_sn_mutex); 1547 idm_rc = idm_buf_rx_from_ini(iscsit_task->it_idm_task, 1548 ibuf->ibuf_idm_buf, dbuf->db_relative_offset, 1549 dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf); 1550 mutex_exit(&ict_sess->ist_sn_mutex); 1551 1552 return (iscsit_idm_to_stmf(idm_rc)); 1553 } 1554 1555 /* What are we supposed to do if there is no direction? */ 1556 return (STMF_INVALID_ARG); 1557 } 1558 1559 static void 1560 iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status) 1561 { 1562 iscsit_task_t *itask = idb->idb_task_binding->idt_private; 1563 stmf_data_buf_t *dbuf = idb->idb_cb_arg; 1564 1565 dbuf->db_xfer_status = iscsit_idm_to_stmf(status); 1566 1567 /* 1568 * If the task has been aborted then we don't need to call STMF 1569 */ 1570 if (itask->it_stmf_abort) { 1571 return; 1572 } 1573 1574 /* 1575 * For ISCSI over TCP (not iSER), the last SCSI Data PDU passed 1576 * for a successful command contains the status as requested by 1577 * by COMSTAR (via the DB_SEND_STATUS_GOOD flag). But the iSER 1578 * transport does not support phase-collapse. So pretend we are 1579 * COMSTAR and send the status in a separate PDU now. 1580 */ 1581 if (idb->idb_task_binding->idt_flags & IDM_TASK_PHASECOLLAPSE_SUCCESS) { 1582 /* 1583 * Mark task complete and notify COMSTAR 1584 * that the status has been sent. 1585 */ 1586 itask->it_idm_task->idt_state = TASK_COMPLETE; 1587 stmf_send_status_done(itask->it_stmf_task, 1588 iscsit_idm_to_stmf(status), STMF_IOF_LPORT_DONE); 1589 } else if ((dbuf->db_flags & DB_SEND_STATUS_GOOD) && 1590 status == IDM_STATUS_SUCCESS) { 1591 1592 /* 1593 * The iscsi target port provider - for iSER, emulates the 1594 * DB_SEND_STATUS_GOOD optimization if requested by STMF; 1595 * it sends the status in a separate PDU after the data 1596 * transfer. In this case the port provider should first 1597 * call stmf_data_xfer_done() to mark the transfer complete 1598 * and then send the status. Although STMF will free the 1599 * buffer at the time the task is freed, even if the transfer 1600 * is not marked complete, this behavior makes statistics 1601 * gathering and task state tracking more difficult than it 1602 * needs to be. 1603 */ 1604 stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0); 1605 if (iscsit_send_scsi_status(itask->it_stmf_task, 0) 1606 != STMF_SUCCESS) { 1607 stmf_send_status_done(itask->it_stmf_task, 1608 STMF_FAILURE, STMF_IOF_LPORT_DONE); 1609 } 1610 } else { 1611 stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0); 1612 /* don't touch dbuf after stmf_data_xfer_done */ 1613 } 1614 } 1615 1616 1617 /*ARGSUSED*/ 1618 stmf_status_t 1619 iscsit_send_scsi_status(scsi_task_t *task, uint32_t ioflags) 1620 { 1621 iscsit_task_t *itask = task->task_port_private; 1622 iscsi_scsi_rsp_hdr_t *rsp; 1623 idm_pdu_t *pdu; 1624 int resp_datalen; 1625 1626 /* 1627 * If this task is aborted then we don't need to respond. 1628 */ 1629 if (itask->it_stmf_abort) { 1630 return (STMF_SUCCESS); 1631 } 1632 1633 /* 1634 * If this is a task management status, handle it elsewhere. 1635 */ 1636 if (task->task_mgmt_function != TM_NONE) { 1637 /* 1638 * Don't wait for the PDU completion to tell STMF 1639 * the task is done -- it doesn't really matter and 1640 * it makes life complicated if STMF later asks us to 1641 * abort the request and we don't know whether the 1642 * status has been sent or not. 1643 */ 1644 itask->it_tm_responded = B_TRUE; 1645 iscsit_send_task_mgmt_resp(itask->it_tm_pdu, 1646 (task->task_completion_status == STMF_SUCCESS) ? 1647 SCSI_TCP_TM_RESP_COMPLETE : SCSI_TCP_TM_RESP_FUNC_NOT_SUPP); 1648 stmf_send_status_done(task, STMF_SUCCESS, 1649 STMF_IOF_LPORT_DONE); 1650 return (STMF_SUCCESS); 1651 } 1652 1653 /* 1654 * Remove the task from the session task list 1655 */ 1656 iscsit_task_done(itask); 1657 1658 /* 1659 * Send status 1660 */ 1661 mutex_enter(&itask->it_idm_task->idt_mutex); 1662 if ((itask->it_idm_task->idt_state == TASK_ACTIVE) && 1663 (task->task_completion_status == STMF_SUCCESS) && 1664 (task->task_sense_length == 0) && 1665 (task->task_resid == 0)) { 1666 itask->it_idm_task->idt_state = TASK_COMPLETE; 1667 /* PDU callback releases task hold */ 1668 idm_task_hold(itask->it_idm_task); 1669 mutex_exit(&itask->it_idm_task->idt_mutex); 1670 /* 1671 * Fast path. Cached status PDU's are already 1672 * initialized. We just need to fill in 1673 * connection and task information. StatSN is 1674 * incremented by 1 for every status sent a 1675 * connection. 1676 */ 1677 pdu = kmem_cache_alloc(iscsit_status_pdu_cache, KM_SLEEP); 1678 pdu->isp_ic = itask->it_ict->ict_ic; 1679 pdu->isp_private = itask; 1680 pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN; 1681 1682 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr; 1683 rsp->itt = itask->it_itt; 1684 /* 1685 * ExpDataSN is the number of R2T and Data-In (read) 1686 * PDUs the target has sent for the SCSI command. 1687 * 1688 * Since there is no support for bidirectional transfer 1689 * yet, either idt_exp_datasn or idt_exp_rttsn, but not 1690 * both is valid at any time 1691 */ 1692 rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ? 1693 htonl(itask->it_idm_task->idt_exp_datasn): 1694 htonl(itask->it_idm_task->idt_exp_rttsn); 1695 rsp->cmd_status = task->task_scsi_status; 1696 iscsit_pdu_tx(pdu); 1697 return (STMF_SUCCESS); 1698 } else { 1699 if (itask->it_idm_task->idt_state != TASK_ACTIVE) { 1700 mutex_exit(&itask->it_idm_task->idt_mutex); 1701 return (STMF_FAILURE); 1702 } 1703 itask->it_idm_task->idt_state = TASK_COMPLETE; 1704 /* PDU callback releases task hold */ 1705 idm_task_hold(itask->it_idm_task); 1706 mutex_exit(&itask->it_idm_task->idt_mutex); 1707 1708 resp_datalen = (task->task_sense_length == 0) ? 0 : 1709 (task->task_sense_length + sizeof (uint16_t)); 1710 1711 pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen); 1712 idm_pdu_init(pdu, itask->it_ict->ict_ic, itask, 1713 iscsit_send_status_done); 1714 pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN; 1715 1716 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr; 1717 bzero(rsp, sizeof (*rsp)); 1718 rsp->opcode = ISCSI_OP_SCSI_RSP; 1719 1720 rsp->flags = ISCSI_FLAG_FINAL; 1721 if (task->task_status_ctrl & TASK_SCTRL_OVER) { 1722 rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW; 1723 } else if (task->task_status_ctrl & TASK_SCTRL_UNDER) { 1724 rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW; 1725 } 1726 1727 rsp->bi_residual_count = 0; 1728 rsp->residual_count = htonl(task->task_resid); 1729 rsp->itt = itask->it_itt; 1730 rsp->response = ISCSI_STATUS_CMD_COMPLETED; 1731 rsp->expdatasn = (itask->it_idm_task->idt_exp_datasn != 0) ? 1732 htonl(itask->it_idm_task->idt_exp_datasn): 1733 htonl(itask->it_idm_task->idt_exp_rttsn); 1734 rsp->cmd_status = task->task_scsi_status; 1735 if (task->task_sense_length != 0) { 1736 /* 1737 * Add a byte to provide the sense length in 1738 * the response 1739 */ 1740 *(uint16_t *)((void *)pdu->isp_data) = 1741 htons(task->task_sense_length); 1742 bcopy(task->task_sense_data, 1743 (uint8_t *)pdu->isp_data + 1744 sizeof (uint16_t), 1745 task->task_sense_length); 1746 hton24(rsp->dlength, resp_datalen); 1747 } 1748 1749 DTRACE_PROBE5(iscsi__scsi__response, 1750 iscsit_conn_t *, itask->it_ict, 1751 uint8_t, rsp->response, 1752 uint8_t, rsp->cmd_status, 1753 idm_pdu_t *, pdu, 1754 scsi_task_t *, task); 1755 1756 iscsit_pdu_tx(pdu); 1757 1758 return (STMF_SUCCESS); 1759 } 1760 } 1761 1762 /*ARGSUSED*/ 1763 static void 1764 iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status) 1765 { 1766 iscsit_task_t *itask; 1767 boolean_t aborted; 1768 1769 itask = pdu->isp_private; 1770 aborted = itask->it_stmf_abort; 1771 1772 /* 1773 * After releasing the hold the task may be freed at any time so 1774 * don't touch it. 1775 */ 1776 idm_task_rele(itask->it_idm_task); 1777 if (!aborted) { 1778 stmf_send_status_done(itask->it_stmf_task, 1779 iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE); 1780 } 1781 kmem_cache_free(iscsit_status_pdu_cache, pdu); 1782 } 1783 1784 /*ARGSUSED*/ 1785 static void 1786 iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status) 1787 { 1788 iscsit_task_t *itask; 1789 boolean_t aborted; 1790 1791 itask = pdu->isp_private; 1792 aborted = itask->it_stmf_abort; 1793 1794 /* 1795 * After releasing the hold the task may be freed at any time so 1796 * don't touch it. 1797 */ 1798 idm_task_rele(itask->it_idm_task); 1799 if (!aborted) { 1800 stmf_send_status_done(itask->it_stmf_task, 1801 iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE); 1802 } 1803 idm_pdu_free(pdu); 1804 } 1805 1806 1807 void 1808 iscsit_lport_task_free(scsi_task_t *task) 1809 { 1810 iscsit_task_t *itask = task->task_port_private; 1811 1812 /* We only call idm_task_start for regular tasks, not task management */ 1813 if (task->task_mgmt_function == TM_NONE) { 1814 idm_task_done(itask->it_idm_task); 1815 iscsit_task_free(itask); 1816 return; 1817 } else { 1818 iscsit_tm_task_free(itask); 1819 } 1820 } 1821 1822 /*ARGSUSED*/ 1823 stmf_status_t 1824 iscsit_abort(stmf_local_port_t *lport, int abort_cmd, void *arg, uint32_t flags) 1825 { 1826 scsi_task_t *st = (scsi_task_t *)arg; 1827 iscsit_task_t *iscsit_task; 1828 idm_task_t *idt; 1829 1830 /* 1831 * If this is a task management request then there's really not much to 1832 * do. 1833 */ 1834 if (st->task_mgmt_function != TM_NONE) { 1835 return (STMF_ABORT_SUCCESS); 1836 } 1837 1838 /* 1839 * Regular task, start cleaning up 1840 */ 1841 iscsit_task = st->task_port_private; 1842 idt = iscsit_task->it_idm_task; 1843 mutex_enter(&iscsit_task->it_mutex); 1844 iscsit_task->it_stmf_abort = B_TRUE; 1845 if (iscsit_task->it_aborted) { 1846 mutex_exit(&iscsit_task->it_mutex); 1847 /* 1848 * Task is no longer active 1849 */ 1850 iscsit_task_done(iscsit_task); 1851 1852 /* 1853 * STMF specification is wrong... says to return 1854 * STMF_ABORTED, the code actually looks for 1855 * STMF_ABORT_SUCCESS. 1856 */ 1857 return (STMF_ABORT_SUCCESS); 1858 } else { 1859 mutex_exit(&iscsit_task->it_mutex); 1860 /* 1861 * Call IDM to abort the task. Due to a variety of 1862 * circumstances the task may already be in the process of 1863 * aborting. 1864 * We'll let IDM worry about rationalizing all that except 1865 * for one particular instance. If the state of the task 1866 * is TASK_COMPLETE, we need to indicate to the framework 1867 * that we are in fact done. This typically happens with 1868 * framework-initiated task management type requests 1869 * (e.g. abort task). 1870 */ 1871 if (idt->idt_state == TASK_COMPLETE) { 1872 idm_refcnt_wait_ref(&idt->idt_refcnt); 1873 return (STMF_ABORT_SUCCESS); 1874 } else { 1875 idm_task_abort(idt->idt_ic, idt, AT_TASK_MGMT_ABORT); 1876 return (STMF_SUCCESS); 1877 } 1878 } 1879 1880 /*NOTREACHED*/ 1881 } 1882 1883 /*ARGSUSED*/ 1884 void 1885 iscsit_ctl(stmf_local_port_t *lport, int cmd, void *arg) 1886 { 1887 iscsit_tgt_t *iscsit_tgt; 1888 1889 ASSERT((cmd == STMF_CMD_LPORT_ONLINE) || 1890 (cmd == STMF_ACK_LPORT_ONLINE_COMPLETE) || 1891 (cmd == STMF_CMD_LPORT_OFFLINE) || 1892 (cmd == STMF_ACK_LPORT_OFFLINE_COMPLETE)); 1893 1894 iscsit_tgt = (iscsit_tgt_t *)lport->lport_port_private; 1895 1896 switch (cmd) { 1897 case STMF_CMD_LPORT_ONLINE: 1898 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_REQ); 1899 break; 1900 case STMF_CMD_LPORT_OFFLINE: 1901 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_REQ); 1902 break; 1903 case STMF_ACK_LPORT_ONLINE_COMPLETE: 1904 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_COMPLETE_ACK); 1905 break; 1906 case STMF_ACK_LPORT_OFFLINE_COMPLETE: 1907 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_COMPLETE_ACK); 1908 break; 1909 1910 default: 1911 break; 1912 } 1913 } 1914 1915 static stmf_status_t 1916 iscsit_idm_to_stmf(idm_status_t idmrc) 1917 { 1918 switch (idmrc) { 1919 case IDM_STATUS_SUCCESS: 1920 return (STMF_SUCCESS); 1921 default: 1922 return (STMF_FAILURE); 1923 } 1924 /*NOTREACHED*/ 1925 } 1926 1927 void 1928 iscsit_op_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu) 1929 { 1930 iscsit_conn_t *ict = ic->ic_handle; 1931 1932 if (iscsit_check_cmdsn_and_queue(rx_pdu)) { 1933 iscsit_post_scsi_cmd(ic, rx_pdu); 1934 } 1935 iscsit_process_pdu_in_queue(ict->ict_sess); 1936 } 1937 1938 /* 1939 * ISCSI protocol 1940 */ 1941 1942 void 1943 iscsit_post_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu) 1944 { 1945 iscsit_conn_t *ict; 1946 iscsit_task_t *itask; 1947 scsi_task_t *task; 1948 iscsit_buf_t *ibuf; 1949 iscsi_scsi_cmd_hdr_t *iscsi_scsi = 1950 (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr; 1951 iscsi_addl_hdr_t *ahs_hdr; 1952 uint16_t addl_cdb_len = 0; 1953 1954 ict = ic->ic_handle; 1955 1956 itask = iscsit_task_alloc(ict); 1957 if (itask == NULL) { 1958 /* Finish processing request */ 1959 iscsit_set_cmdsn(ict, rx_pdu); 1960 1961 iscsit_send_direct_scsi_resp(ict, rx_pdu, 1962 ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY); 1963 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 1964 return; 1965 } 1966 1967 /* 1968 * Note CmdSN and ITT in task. IDM will have already validated this 1969 * request against the connection state so we don't need to check 1970 * that (the connection may have changed state in the meantime but 1971 * we will catch that when we try to send a response) 1972 */ 1973 itask->it_cmdsn = ntohl(iscsi_scsi->cmdsn); 1974 itask->it_itt = iscsi_scsi->itt; 1975 1976 /* 1977 * Check for extended CDB AHS 1978 */ 1979 if (iscsi_scsi->hlength > 0) { 1980 ahs_hdr = (iscsi_addl_hdr_t *)iscsi_scsi; 1981 addl_cdb_len = ((ahs_hdr->ahs_hlen_hi << 8) | 1982 ahs_hdr->ahs_hlen_lo) - 1; /* Adjust for reserved byte */ 1983 if (((addl_cdb_len + 4) / sizeof (uint32_t)) > 1984 iscsi_scsi->hlength) { 1985 /* Mangled header info, drop it */ 1986 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 1987 return; 1988 } 1989 } 1990 1991 ict = rx_pdu->isp_ic->ic_handle; /* IDM client private */ 1992 1993 /* 1994 * Add task to session list. This function will also check to 1995 * ensure that the task does not already exist. 1996 */ 1997 if (iscsit_task_start(itask) != IDM_STATUS_SUCCESS) { 1998 /* 1999 * Task exists, free all resources and reject. Don't 2000 * update expcmdsn in this case because RFC 3720 says 2001 * "The CmdSN of the rejected command PDU (if it is a 2002 * non-immediate command) MUST NOT be considered received 2003 * by the target (i.e., a command sequence gap must be 2004 * assumed for the CmdSN), even though the CmdSN of the 2005 * rejected command PDU may be reliably ascertained. Upon 2006 * receiving the Reject, the initiator MUST plug the CmdSN 2007 * gap in order to continue to use the session. The gap 2008 * may be plugged either by transmitting a command PDU 2009 * with the same CmdSN, or by aborting the task (see section 2010 * 6.9 on how an abort may plug a CmdSN gap)." (Section 6.3) 2011 */ 2012 iscsit_task_free(itask); 2013 iscsit_send_reject(ict, rx_pdu, ISCSI_REJECT_TASK_IN_PROGRESS); 2014 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2015 return; 2016 } 2017 2018 /* Update sequence numbers */ 2019 iscsit_set_cmdsn(ict, rx_pdu); 2020 2021 /* 2022 * Allocate STMF task 2023 */ 2024 itask->it_stmf_task = stmf_task_alloc( 2025 itask->it_ict->ict_sess->ist_lport, 2026 itask->it_ict->ict_sess->ist_stmf_sess, iscsi_scsi->lun, 2027 16 + addl_cdb_len, 0); 2028 if (itask->it_stmf_task == NULL) { 2029 /* 2030 * Either stmf really couldn't get memory for a task or, 2031 * more likely, the LU is currently in reset. Either way 2032 * we have no choice but to fail the request. 2033 */ 2034 iscsit_task_done(itask); 2035 iscsit_task_free(itask); 2036 iscsit_send_direct_scsi_resp(ict, rx_pdu, 2037 ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY); 2038 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2039 return; 2040 } 2041 2042 task = itask->it_stmf_task; 2043 task->task_port_private = itask; 2044 2045 bcopy(iscsi_scsi->lun, task->task_lun_no, sizeof (task->task_lun_no)); 2046 2047 /* 2048 * iSCSI and Comstar use the same values. Should we rely on this 2049 * or translate them bit-wise? 2050 */ 2051 2052 task->task_flags = 2053 (((iscsi_scsi->flags & ISCSI_FLAG_CMD_READ) ? TF_READ_DATA : 0) | 2054 ((iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ? TF_WRITE_DATA : 0) | 2055 ((rx_pdu->isp_datalen == 0) ? 0 : TF_INITIAL_BURST)); 2056 2057 switch (iscsi_scsi->flags & ISCSI_FLAG_CMD_ATTR_MASK) { 2058 case ISCSI_ATTR_UNTAGGED: 2059 break; 2060 case ISCSI_ATTR_SIMPLE: 2061 task->task_additional_flags |= TF_ATTR_SIMPLE_QUEUE; 2062 break; 2063 case ISCSI_ATTR_ORDERED: 2064 task->task_additional_flags |= TF_ATTR_ORDERED_QUEUE; 2065 break; 2066 case ISCSI_ATTR_HEAD_OF_QUEUE: 2067 task->task_additional_flags |= TF_ATTR_HEAD_OF_QUEUE; 2068 break; 2069 case ISCSI_ATTR_ACA: 2070 task->task_additional_flags |= TF_ATTR_ACA; 2071 break; 2072 default: 2073 /* Protocol error but just take it, treat as untagged */ 2074 break; 2075 } 2076 2077 2078 task->task_additional_flags = 0; 2079 task->task_priority = 0; 2080 task->task_mgmt_function = TM_NONE; 2081 2082 /* 2083 * This "task_max_nbufs" doesn't map well to BIDI. We probably need 2084 * parameter for each direction. "MaxOutstandingR2T" may very well 2085 * be set to one which could prevent us from doing simultaneous 2086 * transfers in each direction. 2087 */ 2088 task->task_max_nbufs = (iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ? 2089 ict->ict_op.op_max_outstanding_r2t : STMF_BUFS_MAX; 2090 task->task_cmd_seq_no = ntohl(iscsi_scsi->itt); 2091 task->task_expected_xfer_length = ntohl(iscsi_scsi->data_length); 2092 2093 /* Copy CDB */ 2094 bcopy(iscsi_scsi->scb, task->task_cdb, 16); 2095 if (addl_cdb_len > 0) { 2096 bcopy(ahs_hdr->ahs_extscb, task->task_cdb + 16, addl_cdb_len); 2097 } 2098 2099 DTRACE_ISCSI_3(scsi__command, idm_conn_t *, ic, 2100 iscsi_scsi_cmd_hdr_t *, (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr, 2101 scsi_task_t *, task); 2102 2103 /* 2104 * Copy the transport header into the task handle from the PDU 2105 * handle. The transport header describes this task's remote tagged 2106 * buffer. 2107 */ 2108 if (rx_pdu->isp_transport_hdrlen != 0) { 2109 bcopy(rx_pdu->isp_transport_hdr, 2110 itask->it_idm_task->idt_transport_hdr, 2111 rx_pdu->isp_transport_hdrlen); 2112 } 2113 2114 /* 2115 * Tell IDM about our new active task 2116 */ 2117 idm_task_start(itask->it_idm_task, (uintptr_t)itask->it_itt); 2118 2119 /* 2120 * If we have any immediate data then setup the immediate buffer 2121 * context that comes with the task 2122 */ 2123 if (rx_pdu->isp_datalen) { 2124 ibuf = itask->it_immed_data; 2125 ibuf->ibuf_immed_data_pdu = rx_pdu; 2126 ibuf->ibuf_stmf_buf->db_data_size = rx_pdu->isp_datalen; 2127 ibuf->ibuf_stmf_buf->db_buf_size = rx_pdu->isp_datalen; 2128 ibuf->ibuf_stmf_buf->db_relative_offset = 0; 2129 ibuf->ibuf_stmf_buf->db_sglist[0].seg_length = 2130 rx_pdu->isp_datalen; 2131 ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr = rx_pdu->isp_data; 2132 2133 DTRACE_ISCSI_8(xfer__start, idm_conn_t *, ic, 2134 uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr, 2135 uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset, 2136 uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */ 2137 uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI); 2138 2139 /* 2140 * For immediate data transfer, there is no callback from 2141 * stmf to indicate that the initial burst of data is 2142 * transferred successfully. In some cases, the task can 2143 * get freed before execution returns from stmf_post_task. 2144 * Although this xfer-start/done probe accurately tracks 2145 * the size of the transfer, it does only provide a best 2146 * effort on the timing of the transfer. 2147 */ 2148 DTRACE_ISCSI_8(xfer__done, idm_conn_t *, ic, 2149 uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr, 2150 uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset, 2151 uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */ 2152 uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI); 2153 stmf_post_task(task, ibuf->ibuf_stmf_buf); 2154 } else { 2155 2156 stmf_post_task(task, NULL); 2157 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2158 } 2159 } 2160 2161 /*ARGSUSED*/ 2162 void 2163 iscsit_deferred_dispatch(idm_pdu_t *rx_pdu) 2164 { 2165 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle; 2166 2167 /* 2168 * If the connection has been lost then ignore new PDU's 2169 */ 2170 mutex_enter(&ict->ict_mutex); 2171 if (ict->ict_lost) { 2172 mutex_exit(&ict->ict_mutex); 2173 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL); 2174 return; 2175 } 2176 2177 /* 2178 * Grab a hold on the connection to prevent it from going away 2179 * between now and when the taskq function is called. 2180 */ 2181 iscsit_conn_dispatch_hold(ict); 2182 mutex_exit(&ict->ict_mutex); 2183 2184 if (taskq_dispatch(iscsit_global.global_dispatch_taskq, 2185 iscsit_deferred, rx_pdu, DDI_NOSLEEP) == NULL) { 2186 /* 2187 * In the unlikely scenario that we couldn't get the resources 2188 * to dispatch the PDU then just drop it. 2189 */ 2190 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL); 2191 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL); 2192 iscsit_conn_dispatch_rele(ict); 2193 } 2194 } 2195 2196 static void 2197 iscsit_deferred(void *rx_pdu_void) 2198 { 2199 idm_pdu_t *rx_pdu = rx_pdu_void; 2200 idm_conn_t *ic = rx_pdu->isp_ic; 2201 iscsit_conn_t *ict = ic->ic_handle; 2202 2203 /* 2204 * NOP and Task Management Commands can be marked for immediate 2205 * delivery. Commands marked as 'Immediate' are to be considered 2206 * for execution as soon as they arrive on the target. So these 2207 * should not be checked for sequence order and put in a queue. 2208 * The CmdSN is not advanced for Immediate Commands. 2209 */ 2210 switch (IDM_PDU_OPCODE(rx_pdu)) { 2211 case ISCSI_OP_NOOP_OUT: 2212 if (iscsit_check_cmdsn_and_queue(rx_pdu)) { 2213 iscsit_set_cmdsn(ict, rx_pdu); 2214 iscsit_pdu_op_noop(ict, rx_pdu); 2215 } 2216 break; 2217 case ISCSI_OP_LOGIN_CMD: 2218 iscsit_pdu_op_login_cmd(ict, rx_pdu); 2219 iscsit_conn_dispatch_rele(ict); 2220 return; 2221 case ISCSI_OP_TEXT_CMD: 2222 if (iscsit_check_cmdsn_and_queue(rx_pdu)) { 2223 iscsit_set_cmdsn(ict, rx_pdu); 2224 iscsit_pdu_op_text_cmd(ict, rx_pdu); 2225 } 2226 break; 2227 case ISCSI_OP_LOGOUT_CMD: 2228 if (iscsit_check_cmdsn_and_queue(rx_pdu)) { 2229 iscsit_set_cmdsn(ict, rx_pdu); 2230 iscsit_pdu_op_logout_cmd(ict, rx_pdu); 2231 } 2232 break; 2233 default: 2234 /* Protocol error. IDM should have caught this */ 2235 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL); 2236 ASSERT(0); 2237 break; 2238 } 2239 /* 2240 * Check if there are other PDUs in the session staging queue 2241 * waiting to be posted to SCSI layer. 2242 */ 2243 iscsit_process_pdu_in_queue(ict->ict_sess); 2244 2245 iscsit_conn_dispatch_rele(ict); 2246 } 2247 2248 static void 2249 iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu, 2250 uint8_t response, uint8_t cmd_status) 2251 { 2252 idm_pdu_t *rsp_pdu; 2253 idm_conn_t *ic; 2254 iscsi_scsi_rsp_hdr_t *resp; 2255 iscsi_scsi_cmd_hdr_t *req = 2256 (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr; 2257 2258 ic = ict->ict_ic; 2259 2260 rsp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_rsp_hdr_t), 0); 2261 idm_pdu_init(rsp_pdu, ic, NULL, NULL); 2262 /* 2263 * StatSN is incremented by 1 for every response sent on 2264 * a connection except for responses sent as a result of 2265 * a retry or SNACK 2266 */ 2267 rsp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN; 2268 2269 resp = (iscsi_scsi_rsp_hdr_t *)rsp_pdu->isp_hdr; 2270 2271 resp->opcode = ISCSI_OP_SCSI_RSP; 2272 resp->flags = ISCSI_FLAG_FINAL; 2273 resp->response = response; 2274 resp->cmd_status = cmd_status; 2275 resp->itt = req->itt; 2276 if ((response == ISCSI_STATUS_CMD_COMPLETED) && 2277 (req->data_length != 0) && 2278 ((req->flags & ISCSI_FLAG_CMD_READ) || 2279 (req->flags & ISCSI_FLAG_CMD_WRITE))) { 2280 resp->flags |= ISCSI_FLAG_CMD_UNDERFLOW; 2281 resp->residual_count = req->data_length; 2282 } 2283 2284 DTRACE_PROBE4(iscsi__scsi__direct__response, 2285 iscsit_conn_t *, ict, 2286 uint8_t, resp->response, 2287 uint8_t, resp->cmd_status, 2288 idm_pdu_t *, rsp_pdu); 2289 2290 iscsit_pdu_tx(rsp_pdu); 2291 } 2292 2293 void 2294 iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu, uint8_t tm_status) 2295 { 2296 iscsi_scsi_task_mgt_rsp_hdr_t *tm_resp; 2297 2298 /* 2299 * The target must take note of the last-sent StatSN. 2300 * The StatSN is to be incremented after sending a 2301 * task management response. Digest recovery can only 2302 * work if StatSN is incremented. 2303 */ 2304 tm_resp_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN; 2305 tm_resp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr; 2306 tm_resp->response = tm_status; 2307 2308 DTRACE_PROBE3(iscsi__scsi__tm__response, 2309 iscsit_conn_t *, tm_resp_pdu->isp_ic->ic_handle, 2310 uint8_t, tm_resp->response, 2311 idm_pdu_t *, tm_resp_pdu); 2312 iscsit_pdu_tx(tm_resp_pdu); 2313 } 2314 2315 void 2316 iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu) 2317 { 2318 idm_pdu_t *tm_resp_pdu; 2319 iscsit_task_t *itask; 2320 iscsit_task_t *tm_itask; 2321 scsi_task_t *task; 2322 iscsi_scsi_task_mgt_hdr_t *iscsi_tm = 2323 (iscsi_scsi_task_mgt_hdr_t *)rx_pdu->isp_hdr; 2324 iscsi_scsi_task_mgt_rsp_hdr_t *iscsi_tm_rsp = 2325 (iscsi_scsi_task_mgt_rsp_hdr_t *)rx_pdu->isp_hdr; 2326 uint32_t rtt, cmdsn, refcmdsn; 2327 uint8_t tm_func; 2328 2329 /* 2330 * Setup response PDU (response field will get filled in later) 2331 */ 2332 tm_resp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_task_mgt_rsp_hdr_t), 0); 2333 if (tm_resp_pdu == NULL) { 2334 /* Can't respond, just drop it */ 2335 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2336 return; 2337 } 2338 idm_pdu_init(tm_resp_pdu, ict->ict_ic, NULL, NULL); 2339 iscsi_tm_rsp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr; 2340 bzero(iscsi_tm_rsp, sizeof (iscsi_scsi_task_mgt_rsp_hdr_t)); 2341 iscsi_tm_rsp->opcode = ISCSI_OP_SCSI_TASK_MGT_RSP; 2342 iscsi_tm_rsp->flags = ISCSI_FLAG_FINAL; 2343 iscsi_tm_rsp->itt = rx_pdu->isp_hdr->itt; 2344 2345 /* 2346 * Figure out what we're being asked to do. 2347 */ 2348 DTRACE_PROBE4(iscsi__scsi__tm__request, 2349 iscsit_conn_t *, ict, 2350 uint8_t, (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK), 2351 uint32_t, iscsi_tm->rtt, 2352 idm_pdu_t *, rx_pdu); 2353 switch (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK) { 2354 case ISCSI_TM_FUNC_ABORT_TASK: 2355 /* 2356 * STMF doesn't currently support the "abort task" task 2357 * management command although it does support aborting 2358 * an individual task. We'll get STMF to abort the task 2359 * for us but handle the details of the task management 2360 * command ourselves. 2361 * 2362 * Find the task associated with the referenced task tag. 2363 */ 2364 rtt = iscsi_tm->rtt; 2365 itask = (iscsit_task_t *)idm_task_find_by_handle(ict->ict_ic, 2366 (uintptr_t)rtt); 2367 2368 if (itask == NULL) { 2369 cmdsn = ntohl(iscsi_tm->cmdsn); 2370 refcmdsn = ntohl(iscsi_tm->refcmdsn); 2371 2372 /* 2373 * Task was not found. But the SCSI command could be 2374 * on the rxpdu wait queue. If RefCmdSN is within 2375 * the CmdSN window and less than CmdSN of the TM 2376 * function, return "Function Complete". Otherwise, 2377 * return "Task Does Not Exist". 2378 */ 2379 2380 if (iscsit_cmdsn_in_window(ict, refcmdsn) && 2381 iscsit_sna_lt(refcmdsn, cmdsn)) { 2382 mutex_enter(&ict->ict_sess->ist_sn_mutex); 2383 (void) iscsit_remove_pdu_from_queue( 2384 ict->ict_sess, refcmdsn); 2385 iscsit_conn_dispatch_rele(ict); 2386 mutex_exit(&ict->ict_sess->ist_sn_mutex); 2387 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2388 SCSI_TCP_TM_RESP_COMPLETE); 2389 } else { 2390 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2391 SCSI_TCP_TM_RESP_NO_TASK); 2392 } 2393 } else { 2394 2395 /* 2396 * Tell STMF to abort the task. This will do no harm 2397 * if the task is already complete. 2398 */ 2399 stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task, 2400 STMF_ABORTED, NULL); 2401 2402 /* 2403 * Make sure the task hasn't already completed 2404 */ 2405 mutex_enter(&itask->it_idm_task->idt_mutex); 2406 if ((itask->it_idm_task->idt_state == TASK_COMPLETE) || 2407 (itask->it_idm_task->idt_state == TASK_IDLE)) { 2408 /* 2409 * Task is complete, return "Task Does Not 2410 * Exist" 2411 */ 2412 mutex_exit(&itask->it_idm_task->idt_mutex); 2413 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2414 SCSI_TCP_TM_RESP_NO_TASK); 2415 } else { 2416 /* 2417 * STMF is now aborting the task, return 2418 * "Function Complete" 2419 */ 2420 mutex_exit(&itask->it_idm_task->idt_mutex); 2421 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2422 SCSI_TCP_TM_RESP_COMPLETE); 2423 } 2424 idm_task_rele(itask->it_idm_task); 2425 } 2426 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2427 return; 2428 2429 case ISCSI_TM_FUNC_ABORT_TASK_SET: 2430 tm_func = TM_ABORT_TASK_SET; 2431 break; 2432 2433 case ISCSI_TM_FUNC_CLEAR_ACA: 2434 tm_func = TM_CLEAR_ACA; 2435 break; 2436 2437 case ISCSI_TM_FUNC_CLEAR_TASK_SET: 2438 tm_func = TM_CLEAR_TASK_SET; 2439 break; 2440 2441 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: 2442 tm_func = TM_LUN_RESET; 2443 break; 2444 2445 case ISCSI_TM_FUNC_TARGET_WARM_RESET: 2446 tm_func = TM_TARGET_WARM_RESET; 2447 break; 2448 2449 case ISCSI_TM_FUNC_TARGET_COLD_RESET: 2450 tm_func = TM_TARGET_COLD_RESET; 2451 break; 2452 2453 case ISCSI_TM_FUNC_TASK_REASSIGN: 2454 /* 2455 * We do not currently support allegiance reassignment. When 2456 * we start supporting ERL1+, we will need to. 2457 */ 2458 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2459 SCSI_TCP_TM_RESP_NO_ALLG_REASSN); 2460 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2461 return; 2462 2463 default: 2464 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2465 SCSI_TCP_TM_RESP_REJECTED); 2466 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2467 return; 2468 } 2469 2470 tm_itask = iscsit_tm_task_alloc(ict); 2471 if (tm_itask == NULL) { 2472 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2473 SCSI_TCP_TM_RESP_REJECTED); 2474 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2475 return; 2476 } 2477 2478 2479 task = stmf_task_alloc(ict->ict_sess->ist_lport, 2480 ict->ict_sess->ist_stmf_sess, iscsi_tm->lun, 2481 0, STMF_TASK_EXT_NONE); 2482 if (task == NULL) { 2483 /* 2484 * If this happens, either the LU is in reset, couldn't 2485 * get memory, or some other condition in which we simply 2486 * can't complete this request. It would be nice to return 2487 * an error code like "busy" but the closest we have is 2488 * "rejected". 2489 */ 2490 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2491 SCSI_TCP_TM_RESP_REJECTED); 2492 iscsit_tm_task_free(tm_itask); 2493 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2494 return; 2495 } 2496 2497 tm_itask->it_tm_pdu = tm_resp_pdu; 2498 tm_itask->it_stmf_task = task; 2499 task->task_port_private = tm_itask; 2500 task->task_mgmt_function = tm_func; 2501 task->task_additional_flags = TASK_AF_NO_EXPECTED_XFER_LENGTH; 2502 task->task_priority = 0; 2503 task->task_max_nbufs = STMF_BUFS_MAX; 2504 task->task_cmd_seq_no = iscsi_tm->itt; 2505 task->task_expected_xfer_length = 0; 2506 2507 stmf_post_task(task, NULL); 2508 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2509 } 2510 2511 static void 2512 iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu) 2513 { 2514 iscsi_nop_out_hdr_t *out = (iscsi_nop_out_hdr_t *)rx_pdu->isp_hdr; 2515 iscsi_nop_in_hdr_t *in; 2516 int resp_datalen; 2517 idm_pdu_t *resp; 2518 2519 /* Ignore the response from initiator */ 2520 if ((out->itt == ISCSI_RSVD_TASK_TAG) || 2521 (out->ttt != ISCSI_RSVD_TASK_TAG)) { 2522 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2523 return; 2524 } 2525 2526 /* Allocate a PDU to respond */ 2527 resp_datalen = ntoh24(out->dlength); 2528 resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen); 2529 idm_pdu_init(resp, ict->ict_ic, NULL, NULL); 2530 if (resp_datalen > 0) { 2531 bcopy(rx_pdu->isp_data, resp->isp_data, resp_datalen); 2532 } 2533 2534 /* 2535 * When sending a NOP-In as a response to a NOP-Out from the initiator, 2536 * the target must respond with the same initiator task tag that was 2537 * provided in the NOP-Out request, the target transfer tag must be 2538 * ISCSI_RSVD_TASK_TAG (0xffffffff) and StatSN will contain the next 2539 * status sequence number. The StatSN for the connection is advanced 2540 * after this PDU is sent. 2541 */ 2542 in = (iscsi_nop_in_hdr_t *)resp->isp_hdr; 2543 bzero(in, sizeof (*in)); 2544 in->opcode = ISCSI_OP_NOOP_IN; 2545 in->flags = ISCSI_FLAG_FINAL; 2546 bcopy(out->lun, in->lun, 8); 2547 in->itt = out->itt; 2548 in->ttt = ISCSI_RSVD_TASK_TAG; 2549 hton24(in->dlength, resp_datalen); 2550 resp->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN; 2551 /* Any other field in resp to be set? */ 2552 iscsit_pdu_tx(resp); 2553 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2554 } 2555 2556 static void 2557 iscsit_pdu_op_login_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu) 2558 { 2559 2560 /* 2561 * Submit PDU to login state machine. State machine will free the 2562 * PDU. 2563 */ 2564 iscsit_login_sm_event(ict, ILE_LOGIN_RCV, rx_pdu); 2565 } 2566 2567 void 2568 iscsit_pdu_op_logout_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu) 2569 { 2570 iscsi_logout_hdr_t *logout_req = 2571 (iscsi_logout_hdr_t *)rx_pdu->isp_hdr; 2572 iscsi_logout_rsp_hdr_t *logout_rsp; 2573 idm_pdu_t *resp; 2574 2575 /* Allocate a PDU to respond */ 2576 resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0); 2577 idm_pdu_init(resp, ict->ict_ic, NULL, NULL); 2578 /* 2579 * The StatSN is to be sent to the initiator, 2580 * it is not required to increment the number 2581 * as the connection is terminating. 2582 */ 2583 resp->isp_flags |= IDM_PDU_SET_STATSN; 2584 /* 2585 * Logout results in the immediate termination of all tasks except 2586 * if the logout reason is ISCSI_LOGOUT_REASON_RECOVERY. The 2587 * connection state machine will drive this task cleanup automatically 2588 * so we don't need to handle that here. 2589 */ 2590 logout_rsp = (iscsi_logout_rsp_hdr_t *)resp->isp_hdr; 2591 bzero(logout_rsp, sizeof (*logout_rsp)); 2592 logout_rsp->opcode = ISCSI_OP_LOGOUT_RSP; 2593 logout_rsp->flags = ISCSI_FLAG_FINAL; 2594 logout_rsp->itt = logout_req->itt; 2595 if ((logout_req->flags & ISCSI_FLAG_LOGOUT_REASON_MASK) > 2596 ISCSI_LOGOUT_REASON_RECOVERY) { 2597 logout_rsp->response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED; 2598 } else { 2599 logout_rsp->response = ISCSI_LOGOUT_SUCCESS; 2600 } 2601 2602 iscsit_pdu_tx(resp); 2603 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2604 } 2605 2606 /* 2607 * Calculate the number of outstanding commands we can process 2608 */ 2609 int 2610 iscsit_cmd_window() 2611 { 2612 /* 2613 * Instead of using a pre-defined constant for the command window, 2614 * it should be made confiurable and dynamic. With MC/S, sequence 2615 * numbers will be used up at a much faster rate than with SC/S. 2616 */ 2617 return (ISCSIT_MAX_WINDOW); 2618 } 2619 2620 /* 2621 * Set local registers based on incoming PDU 2622 */ 2623 void 2624 iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu) 2625 { 2626 iscsit_sess_t *ist; 2627 iscsi_scsi_cmd_hdr_t *req; 2628 2629 ist = ict->ict_sess; 2630 2631 req = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr; 2632 if (req->opcode & ISCSI_OP_IMMEDIATE) { 2633 /* no cmdsn increment for immediate PDUs */ 2634 return; 2635 } 2636 2637 /* Ensure that the ExpCmdSN advances in an orderly manner */ 2638 mutex_enter(&ist->ist_sn_mutex); 2639 ist->ist_expcmdsn = ntohl(req->cmdsn) + 1; 2640 ist->ist_maxcmdsn = ntohl(req->cmdsn) + iscsit_cmd_window(); 2641 mutex_exit(&ist->ist_sn_mutex); 2642 } 2643 2644 /* 2645 * Wrapper funtion, calls iscsi_calc_rspsn and idm_pdu_tx 2646 */ 2647 void 2648 iscsit_pdu_tx(idm_pdu_t *pdu) 2649 { 2650 iscsit_conn_t *ict = pdu->isp_ic->ic_handle; 2651 iscsi_scsi_rsp_hdr_t *rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr; 2652 iscsit_sess_t *ist = ict->ict_sess; 2653 2654 /* 2655 * The command sequence numbers are session-wide and must stay 2656 * consistent across the transfer, so protect the cmdsn with a 2657 * mutex lock on the session. The status sequence number will 2658 * be updated just before the transport layer transmits the PDU. 2659 */ 2660 2661 mutex_enter(&ict->ict_sess->ist_sn_mutex); 2662 /* Set ExpCmdSN and MaxCmdSN */ 2663 rsp->maxcmdsn = htonl(ist->ist_maxcmdsn); 2664 rsp->expcmdsn = htonl(ist->ist_expcmdsn); 2665 idm_pdu_tx(pdu); 2666 mutex_exit(&ict->ict_sess->ist_sn_mutex); 2667 } 2668 2669 /* 2670 * Internal functions 2671 */ 2672 2673 void 2674 iscsit_send_async_event(iscsit_conn_t *ict, uint8_t event) 2675 { 2676 idm_pdu_t *abt; 2677 iscsi_async_evt_hdr_t *async_abt; 2678 2679 /* 2680 * Get a PDU to build the abort request. 2681 */ 2682 abt = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0); 2683 if (abt == NULL) { 2684 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL); 2685 return; 2686 } 2687 2688 /* 2689 * A asynchronous message is sent by the target to request a logout. 2690 * The StatSN for the connection is advanced after the PDU is sent 2691 * to allow for initiator and target state synchronization. 2692 */ 2693 idm_pdu_init(abt, ict->ict_ic, NULL, NULL); 2694 abt->isp_datalen = 0; 2695 abt->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN; 2696 2697 async_abt = (iscsi_async_evt_hdr_t *)abt->isp_hdr; 2698 bzero(async_abt, sizeof (*async_abt)); 2699 async_abt->opcode = ISCSI_OP_ASYNC_EVENT; 2700 async_abt->async_event = event; 2701 async_abt->flags = ISCSI_FLAG_FINAL; 2702 async_abt->rsvd4[0] = 0xff; 2703 async_abt->rsvd4[1] = 0xff; 2704 async_abt->rsvd4[2] = 0xff; 2705 async_abt->rsvd4[3] = 0xff; 2706 2707 switch (event) { 2708 case ISCSI_ASYNC_EVENT_REQUEST_LOGOUT: 2709 async_abt->param3 = htons(IDM_LOGOUT_SECONDS); 2710 break; 2711 case ISCSI_ASYNC_EVENT_SCSI_EVENT: 2712 case ISCSI_ASYNC_EVENT_DROPPING_CONNECTION: 2713 case ISCSI_ASYNC_EVENT_DROPPING_ALL_CONNECTIONS: 2714 case ISCSI_ASYNC_EVENT_PARAM_NEGOTIATION: 2715 default: 2716 ASSERT(0); 2717 } 2718 2719 iscsit_pdu_tx(abt); 2720 } 2721 2722 void 2723 iscsit_send_reject(iscsit_conn_t *ict, idm_pdu_t *rejected_pdu, uint8_t reason) 2724 { 2725 idm_pdu_t *reject_pdu; 2726 iscsi_reject_rsp_hdr_t *reject; 2727 2728 /* 2729 * Get a PDU to build the abort request. 2730 */ 2731 reject_pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t), 2732 rejected_pdu->isp_hdrlen); 2733 if (reject_pdu == NULL) { 2734 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL); 2735 return; 2736 } 2737 idm_pdu_init(reject_pdu, ict->ict_ic, NULL, NULL); 2738 /* StatSN is advanced after a Reject PDU */ 2739 reject_pdu->isp_flags |= IDM_PDU_SET_STATSN | IDM_PDU_ADVANCE_STATSN; 2740 reject_pdu->isp_datalen = rejected_pdu->isp_hdrlen; 2741 bcopy(rejected_pdu->isp_hdr, reject_pdu->isp_data, 2742 rejected_pdu->isp_hdrlen); 2743 2744 reject = (iscsi_reject_rsp_hdr_t *)reject_pdu->isp_hdr; 2745 bzero(reject, sizeof (*reject)); 2746 reject->opcode = ISCSI_OP_REJECT_MSG; 2747 reject->reason = reason; 2748 reject->flags = ISCSI_FLAG_FINAL; 2749 hton24(reject->dlength, rejected_pdu->isp_hdrlen); 2750 reject->must_be_ff[0] = 0xff; 2751 reject->must_be_ff[1] = 0xff; 2752 reject->must_be_ff[2] = 0xff; 2753 reject->must_be_ff[3] = 0xff; 2754 2755 iscsit_pdu_tx(reject_pdu); 2756 } 2757 2758 2759 static iscsit_task_t * 2760 iscsit_task_alloc(iscsit_conn_t *ict) 2761 { 2762 iscsit_task_t *itask; 2763 iscsit_buf_t *immed_ibuf; 2764 2765 /* 2766 * Possible items to pre-alloc if we cache iscsit_task_t's: 2767 * 2768 * Status PDU w/ sense buffer 2769 * stmf_data_buf_t for immediate data 2770 */ 2771 itask = kmem_alloc(sizeof (iscsit_task_t) + sizeof (iscsit_buf_t) + 2772 sizeof (stmf_data_buf_t), KM_NOSLEEP); 2773 if (itask != NULL) { 2774 mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL); 2775 itask->it_aborted = itask->it_stmf_abort = 2776 itask->it_tm_task = 0; 2777 2778 immed_ibuf = (iscsit_buf_t *)(itask + 1); 2779 bzero(immed_ibuf, sizeof (*immed_ibuf)); 2780 immed_ibuf->ibuf_is_immed = B_TRUE; 2781 immed_ibuf->ibuf_stmf_buf = (stmf_data_buf_t *)(immed_ibuf + 1); 2782 2783 bzero(immed_ibuf->ibuf_stmf_buf, sizeof (stmf_data_buf_t)); 2784 immed_ibuf->ibuf_stmf_buf->db_port_private = immed_ibuf; 2785 immed_ibuf->ibuf_stmf_buf->db_sglist_length = 1; 2786 immed_ibuf->ibuf_stmf_buf->db_flags = DB_DIRECTION_FROM_RPORT | 2787 DB_DONT_CACHE; 2788 itask->it_immed_data = immed_ibuf; 2789 itask->it_idm_task = idm_task_alloc(ict->ict_ic); 2790 if (itask->it_idm_task != NULL) { 2791 itask->it_idm_task->idt_private = itask; 2792 itask->it_ict = ict; 2793 itask->it_ttt = itask->it_idm_task->idt_tt; 2794 return (itask); 2795 } else { 2796 kmem_free(itask, sizeof (iscsit_task_t) + 2797 sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t)); 2798 } 2799 } 2800 2801 return (NULL); 2802 } 2803 2804 static void 2805 iscsit_task_free(iscsit_task_t *itask) 2806 { 2807 idm_task_free(itask->it_idm_task); 2808 mutex_destroy(&itask->it_mutex); 2809 kmem_free(itask, sizeof (iscsit_task_t) + 2810 sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t)); 2811 } 2812 2813 static iscsit_task_t * 2814 iscsit_tm_task_alloc(iscsit_conn_t *ict) 2815 { 2816 iscsit_task_t *itask; 2817 2818 itask = kmem_zalloc(sizeof (iscsit_task_t), KM_NOSLEEP); 2819 if (itask != NULL) { 2820 idm_conn_hold(ict->ict_ic); 2821 mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL); 2822 itask->it_aborted = itask->it_stmf_abort = 2823 itask->it_tm_responded = 0; 2824 itask->it_tm_pdu = NULL; 2825 itask->it_tm_task = 1; 2826 itask->it_ict = ict; 2827 } 2828 2829 return (itask); 2830 } 2831 2832 static void 2833 iscsit_tm_task_free(iscsit_task_t *itask) 2834 { 2835 /* 2836 * If we responded then the call to idm_pdu_complete will free the 2837 * PDU. Otherwise we got aborted before the TM function could 2838 * complete and we need to free the PDU explicitly. 2839 */ 2840 if (itask->it_tm_pdu != NULL && !itask->it_tm_responded) 2841 idm_pdu_free(itask->it_tm_pdu); 2842 idm_conn_rele(itask->it_ict->ict_ic); 2843 mutex_destroy(&itask->it_mutex); 2844 kmem_free(itask, sizeof (iscsit_task_t)); 2845 } 2846 2847 static idm_status_t 2848 iscsit_task_start(iscsit_task_t *itask) 2849 { 2850 iscsit_sess_t *ist = itask->it_ict->ict_sess; 2851 avl_index_t where; 2852 2853 /* 2854 * Sanity check the ITT and ensure that this task does not already 2855 * exist. If not then add the task to the session task list. 2856 */ 2857 mutex_enter(&ist->ist_mutex); 2858 mutex_enter(&itask->it_mutex); 2859 itask->it_active = 1; 2860 if (avl_find(&ist->ist_task_list, itask, &where) == NULL) { 2861 /* New task, add to AVL */ 2862 avl_insert(&ist->ist_task_list, itask, where); 2863 mutex_exit(&itask->it_mutex); 2864 mutex_exit(&ist->ist_mutex); 2865 return (IDM_STATUS_SUCCESS); 2866 } 2867 mutex_exit(&itask->it_mutex); 2868 mutex_exit(&ist->ist_mutex); 2869 2870 return (IDM_STATUS_REJECT); 2871 } 2872 2873 static void 2874 iscsit_task_done(iscsit_task_t *itask) 2875 { 2876 iscsit_sess_t *ist = itask->it_ict->ict_sess; 2877 2878 mutex_enter(&ist->ist_mutex); 2879 mutex_enter(&itask->it_mutex); 2880 if (itask->it_active) { 2881 avl_remove(&ist->ist_task_list, itask); 2882 itask->it_active = 0; 2883 } 2884 mutex_exit(&itask->it_mutex); 2885 mutex_exit(&ist->ist_mutex); 2886 } 2887 2888 /* 2889 * iscsit status PDU cache 2890 */ 2891 2892 /*ARGSUSED*/ 2893 static int 2894 iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags) 2895 { 2896 idm_pdu_t *pdu = pdu_void; 2897 iscsi_scsi_rsp_hdr_t *rsp; 2898 2899 bzero(pdu, sizeof (idm_pdu_t)); 2900 pdu->isp_callback = iscsit_send_good_status_done; 2901 pdu->isp_magic = IDM_PDU_MAGIC; 2902 pdu->isp_hdr = (iscsi_hdr_t *)(pdu + 1); /* Ptr arithmetic */ 2903 pdu->isp_hdrlen = sizeof (iscsi_hdr_t); 2904 2905 /* Setup status response */ 2906 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr; 2907 bzero(rsp, sizeof (*rsp)); 2908 rsp->opcode = ISCSI_OP_SCSI_RSP; 2909 rsp->flags = ISCSI_FLAG_FINAL; 2910 rsp->response = ISCSI_STATUS_CMD_COMPLETED; 2911 2912 return (0); 2913 } 2914 2915 /* 2916 * iscsit private data handler 2917 */ 2918 2919 /*ARGSUSED*/ 2920 static void 2921 iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags) 2922 { 2923 it_config_t *cfg; 2924 nvlist_t *nvl; 2925 iscsit_service_enabled_t old_state; 2926 2927 if ((cmd != STMF_PROVIDER_DATA_UPDATED) || (arg == NULL)) { 2928 return; 2929 } 2930 2931 nvl = (nvlist_t *)arg; 2932 2933 /* Translate nvlist */ 2934 if (it_nv_to_config(nvl, &cfg) != 0) { 2935 cmn_err(CE_WARN, "Configuration is invalid"); 2936 return; 2937 } 2938 2939 /* Check that no iSCSI ioctl is currently running */ 2940 mutex_enter(&iscsit_global.global_state_mutex); 2941 old_state = iscsit_global.global_svc_state; 2942 switch (iscsit_global.global_svc_state) { 2943 case ISE_ENABLED: 2944 case ISE_DISABLED: 2945 iscsit_global.global_svc_state = ISE_BUSY; 2946 break; 2947 case ISE_ENABLING: 2948 /* 2949 * It is OK for the iscsit_pp_cb to be called from inside of 2950 * an iSCSI ioctl only if we are currently executing inside 2951 * of stmf_register_port_provider. 2952 */ 2953 ASSERT((flags & STMF_PCB_PREG_COMPLETE) != 0); 2954 break; 2955 default: 2956 cmn_err(CE_WARN, "iscsit_pp_cb called when global_svc_state" 2957 " is not ENABLED(0x%x) -- ignoring", 2958 iscsit_global.global_svc_state); 2959 mutex_exit(&iscsit_global.global_state_mutex); 2960 it_config_free_cmn(cfg); 2961 return; 2962 } 2963 mutex_exit(&iscsit_global.global_state_mutex); 2964 2965 /* Update config */ 2966 (void) iscsit_config_merge(cfg); 2967 2968 it_config_free_cmn(cfg); 2969 2970 /* Restore old iSCSI driver global state */ 2971 mutex_enter(&iscsit_global.global_state_mutex); 2972 ASSERT(iscsit_global.global_svc_state == ISE_BUSY || 2973 iscsit_global.global_svc_state == ISE_ENABLING); 2974 iscsit_global.global_svc_state = old_state; 2975 mutex_exit(&iscsit_global.global_state_mutex); 2976 } 2977 2978 2979 static it_cfg_status_t 2980 iscsit_config_merge(it_config_t *in_cfg) 2981 { 2982 it_cfg_status_t status; 2983 it_config_t *cfg; 2984 it_config_t tmp_cfg; 2985 list_t tpg_del_list; 2986 2987 if (in_cfg) { 2988 cfg = in_cfg; 2989 } else { 2990 /* Make empty config */ 2991 bzero(&tmp_cfg, sizeof (tmp_cfg)); 2992 cfg = &tmp_cfg; 2993 } 2994 2995 list_create(&tpg_del_list, sizeof (iscsit_tpg_t), 2996 offsetof(iscsit_tpg_t, tpg_delete_ln)); 2997 2998 /* 2999 * Update targets, initiator contexts, target portal groups, 3000 * and iSNS client 3001 */ 3002 ISCSIT_GLOBAL_LOCK(RW_WRITER); 3003 if (((status = iscsit_config_merge_tpg(cfg, &tpg_del_list)) 3004 != 0) || 3005 ((status = iscsit_config_merge_tgt(cfg)) != 0) || 3006 ((status = iscsit_config_merge_ini(cfg)) != 0) || 3007 ((status = isnst_config_merge(cfg)) != 0)) { 3008 ISCSIT_GLOBAL_UNLOCK(); 3009 return (status); 3010 } 3011 3012 /* Update other global config parameters */ 3013 if (iscsit_global.global_props) { 3014 nvlist_free(iscsit_global.global_props); 3015 iscsit_global.global_props = NULL; 3016 } 3017 if (in_cfg) { 3018 (void) nvlist_dup(cfg->config_global_properties, 3019 &iscsit_global.global_props, KM_SLEEP); 3020 } 3021 ISCSIT_GLOBAL_UNLOCK(); 3022 3023 iscsit_config_destroy_tpgs(&tpg_del_list); 3024 3025 list_destroy(&tpg_del_list); 3026 3027 return (ITCFG_SUCCESS); 3028 } 3029 3030 /* 3031 * iscsit_sna_lt[e] 3032 * 3033 * Compare serial numbers using serial number arithmetic as defined in 3034 * RFC 1982. 3035 * 3036 * NOTE: This code is duplicated in the isns server. It ought to be common. 3037 */ 3038 3039 static int 3040 iscsit_sna_lt(uint32_t sn1, uint32_t sn2) 3041 { 3042 return ((sn1 != sn2) && 3043 (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) || 3044 ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK)))); 3045 } 3046 3047 static int 3048 iscsit_sna_lte(uint32_t sn1, uint32_t sn2) 3049 { 3050 return ((sn1 == sn2) || 3051 (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) || 3052 ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK)))); 3053 } 3054 3055 3056 static boolean_t 3057 iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn) 3058 { 3059 iscsit_sess_t *ist = ict->ict_sess; 3060 int rval = B_TRUE; 3061 3062 ist = ict->ict_sess; 3063 3064 mutex_enter(&ist->ist_sn_mutex); 3065 3066 /* 3067 * If cmdsn is less than ist_expcmdsn - iscsit_cmd_window() or 3068 * greater than ist_expcmdsn, it's not in the window. 3069 */ 3070 3071 if (iscsit_sna_lt(cmdsn, (ist->ist_expcmdsn - iscsit_cmd_window())) || 3072 !iscsit_sna_lte(cmdsn, ist->ist_expcmdsn)) { 3073 rval = B_FALSE; 3074 } 3075 3076 mutex_exit(&ist->ist_sn_mutex); 3077 3078 return (rval); 3079 } 3080 3081 /* 3082 * iscsit_check_cmdsn_and_queue 3083 * 3084 * Independent of the order in which the iSCSI target receives non-immediate 3085 * command PDU across the entire session and any multiple connections within 3086 * the session, the target must deliver the commands to the SCSI layer in 3087 * CmdSN order. So out-of-order non-immediate commands are queued up on a 3088 * session-wide wait queue. Duplicate commands are ignored. 3089 * 3090 */ 3091 static int 3092 iscsit_check_cmdsn_and_queue(idm_pdu_t *rx_pdu) 3093 { 3094 idm_conn_t *ic = rx_pdu->isp_ic; 3095 iscsit_conn_t *ict = ic->ic_handle; 3096 iscsit_sess_t *ist = ict->ict_sess; 3097 iscsi_scsi_cmd_hdr_t *hdr = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr; 3098 3099 mutex_enter(&ist->ist_sn_mutex); 3100 if (hdr->opcode & ISCSI_OP_IMMEDIATE) { 3101 /* do not queue, handle it immediately */ 3102 DTRACE_PROBE2(immediate__cmd, iscsit_sess_t *, ist, 3103 idm_pdu_t *, rx_pdu); 3104 mutex_exit(&ist->ist_sn_mutex); 3105 return (ISCSIT_CMDSN_EQ_EXPCMDSN); 3106 } 3107 if (iscsit_sna_lt(ist->ist_expcmdsn, ntohl(hdr->cmdsn))) { 3108 /* 3109 * Out-of-order commands (cmdSN higher than ExpCmdSN) 3110 * are staged on a fixed-size circular buffer until 3111 * the missing command is delivered to the SCSI layer. 3112 * Irrespective of the order of insertion into the 3113 * staging queue, the commands are processed out of the 3114 * queue in cmdSN order only. 3115 */ 3116 rx_pdu->isp_queue_time = ddi_get_time(); 3117 iscsit_add_pdu_to_queue(ist, rx_pdu); 3118 mutex_exit(&ist->ist_sn_mutex); 3119 return (ISCSIT_CMDSN_GT_EXPCMDSN); 3120 } else if (iscsit_sna_lt(ntohl(hdr->cmdsn), ist->ist_expcmdsn)) { 3121 DTRACE_PROBE3(cmdsn__lt__expcmdsn, iscsit_sess_t *, ist, 3122 iscsit_conn_t *, ict, idm_pdu_t *, rx_pdu); 3123 mutex_exit(&ist->ist_sn_mutex); 3124 return (ISCSIT_CMDSN_LT_EXPCMDSN); 3125 } else { 3126 mutex_exit(&ist->ist_sn_mutex); 3127 return (ISCSIT_CMDSN_EQ_EXPCMDSN); 3128 } 3129 } 3130 3131 /* 3132 * iscsit_add_pdu_to_queue() adds PDUs into the array indexed by 3133 * their cmdsn value. The length of the array is kept above the 3134 * maximum window size. The window keeps the cmdsn within a range 3135 * such that there are no collisons. e.g. the assumption is that 3136 * the windowing checks make it impossible to receive PDUs that 3137 * index into the same location in the array. 3138 */ 3139 static void 3140 iscsit_add_pdu_to_queue(iscsit_sess_t *ist, idm_pdu_t *rx_pdu) 3141 { 3142 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue; 3143 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle; 3144 uint32_t cmdsn = 3145 ((iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr)->cmdsn; 3146 uint32_t index; 3147 3148 ASSERT(MUTEX_HELD(&ist->ist_sn_mutex)); 3149 /* 3150 * If the connection is being torn down, then 3151 * don't add the PDU to the staging queue 3152 */ 3153 mutex_enter(&ict->ict_mutex); 3154 if (ict->ict_lost) { 3155 mutex_exit(&ict->ict_mutex); 3156 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL); 3157 return; 3158 } 3159 iscsit_conn_dispatch_hold(ict); 3160 mutex_exit(&ict->ict_mutex); 3161 3162 index = ntohl(cmdsn) % ISCSIT_RXPDU_QUEUE_LEN; 3163 ASSERT(cbuf->cb_buffer[index] == NULL); 3164 cbuf->cb_buffer[index] = rx_pdu; 3165 cbuf->cb_num_elems++; 3166 } 3167 3168 static idm_pdu_t * 3169 iscsit_remove_pdu_from_queue(iscsit_sess_t *ist, uint32_t cmdsn) 3170 { 3171 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue; 3172 idm_pdu_t *pdu = NULL; 3173 uint32_t index; 3174 3175 ASSERT(MUTEX_HELD(&ist->ist_sn_mutex)); 3176 index = cmdsn % ISCSIT_RXPDU_QUEUE_LEN; 3177 if ((pdu = cbuf->cb_buffer[index]) != NULL) { 3178 ASSERT(cmdsn == 3179 ntohl(((iscsi_scsi_cmd_hdr_t *)pdu->isp_hdr)->cmdsn)); 3180 cbuf->cb_buffer[index] = NULL; 3181 cbuf->cb_num_elems--; 3182 return (pdu); 3183 } 3184 return (NULL); 3185 } 3186 3187 /* 3188 * iscsit_process_pdu_in_queue() finds the next pdu in sequence 3189 * and posts it to the SCSI layer 3190 */ 3191 static void 3192 iscsit_process_pdu_in_queue(iscsit_sess_t *ist) 3193 { 3194 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue; 3195 idm_pdu_t *pdu = NULL; 3196 uint32_t expcmdsn; 3197 3198 for (;;) { 3199 mutex_enter(&ist->ist_sn_mutex); 3200 if (cbuf->cb_num_elems == 0) { 3201 mutex_exit(&ist->ist_sn_mutex); 3202 break; 3203 } 3204 expcmdsn = ist->ist_expcmdsn; 3205 if ((pdu = iscsit_remove_pdu_from_queue(ist, expcmdsn)) 3206 == NULL) { 3207 mutex_exit(&ist->ist_sn_mutex); 3208 break; 3209 } 3210 mutex_exit(&ist->ist_sn_mutex); 3211 iscsit_post_staged_pdu(pdu); 3212 } 3213 } 3214 3215 static void 3216 iscsit_post_staged_pdu(idm_pdu_t *rx_pdu) 3217 { 3218 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle; 3219 3220 /* Post the PDU to the SCSI layer */ 3221 switch (IDM_PDU_OPCODE(rx_pdu)) { 3222 case ISCSI_OP_NOOP_OUT: 3223 iscsit_set_cmdsn(ict, rx_pdu); 3224 iscsit_pdu_op_noop(ict, rx_pdu); 3225 break; 3226 case ISCSI_OP_TEXT_CMD: 3227 iscsit_set_cmdsn(ict, rx_pdu); 3228 iscsit_pdu_op_text_cmd(ict, rx_pdu); 3229 break; 3230 case ISCSI_OP_SCSI_TASK_MGT_MSG: 3231 iscsit_set_cmdsn(ict, rx_pdu); 3232 iscsit_op_scsi_task_mgmt(ict, rx_pdu); 3233 break; 3234 case ISCSI_OP_SCSI_CMD: 3235 /* cmdSN will be incremented after creating itask */ 3236 iscsit_post_scsi_cmd(rx_pdu->isp_ic, rx_pdu); 3237 break; 3238 case ISCSI_OP_LOGOUT_CMD: 3239 iscsit_set_cmdsn(ict, rx_pdu); 3240 iscsit_pdu_op_logout_cmd(ict, rx_pdu); 3241 break; 3242 default: 3243 /* No other PDUs should be placed on the queue */ 3244 ASSERT(0); 3245 } 3246 iscsit_conn_dispatch_rele(ict); /* release hold on the conn */ 3247 } 3248 3249 /* ARGSUSED */ 3250 void 3251 iscsit_rxpdu_queue_monitor_start(void) 3252 { 3253 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex); 3254 if (iscsit_rxpdu_queue_monitor_thr_running) { 3255 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex); 3256 return; 3257 } 3258 iscsit_rxpdu_queue_monitor_thr_id = 3259 thread_create(NULL, 0, iscsit_rxpdu_queue_monitor, NULL, 3260 0, &p0, TS_RUN, minclsyspri); 3261 while (!iscsit_rxpdu_queue_monitor_thr_running) { 3262 cv_wait(&iscsit_rxpdu_queue_monitor_cv, 3263 &iscsit_rxpdu_queue_monitor_mutex); 3264 } 3265 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex); 3266 3267 } 3268 3269 /* ARGSUSED */ 3270 void 3271 iscsit_rxpdu_queue_monitor_stop(void) 3272 { 3273 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex); 3274 if (iscsit_rxpdu_queue_monitor_thr_running) { 3275 iscsit_rxpdu_queue_monitor_thr_running = B_FALSE; 3276 cv_signal(&iscsit_rxpdu_queue_monitor_cv); 3277 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex); 3278 3279 thread_join(iscsit_rxpdu_queue_monitor_thr_did); 3280 return; 3281 } 3282 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex); 3283 } 3284 3285 /* 3286 * A separate thread is used to scan the staging queue on all the 3287 * sessions, If a delayed PDU does not arrive within a timeout, the 3288 * target will advance to the staged PDU that is next in sequence 3289 * and exceeded the threshold wait time. It is up to the initiator 3290 * to note that the target has not acknowledged a particular cmdsn 3291 * and take appropriate action. 3292 */ 3293 /* ARGSUSED */ 3294 static void 3295 iscsit_rxpdu_queue_monitor(void *arg) 3296 { 3297 iscsit_tgt_t *tgt; 3298 iscsit_sess_t *ist; 3299 3300 mutex_enter(&iscsit_rxpdu_queue_monitor_mutex); 3301 iscsit_rxpdu_queue_monitor_thr_did = curthread->t_did; 3302 iscsit_rxpdu_queue_monitor_thr_running = B_TRUE; 3303 cv_signal(&iscsit_rxpdu_queue_monitor_cv); 3304 3305 while (iscsit_rxpdu_queue_monitor_thr_running) { 3306 ISCSIT_GLOBAL_LOCK(RW_READER); 3307 for (tgt = avl_first(&iscsit_global.global_target_list); 3308 tgt != NULL; 3309 tgt = AVL_NEXT(&iscsit_global.global_target_list, tgt)) { 3310 mutex_enter(&tgt->target_mutex); 3311 for (ist = avl_first(&tgt->target_sess_list); 3312 ist != NULL; 3313 ist = AVL_NEXT(&tgt->target_sess_list, ist)) { 3314 3315 iscsit_rxpdu_queue_monitor_session(ist); 3316 } 3317 mutex_exit(&tgt->target_mutex); 3318 } 3319 ISCSIT_GLOBAL_UNLOCK(); 3320 if (iscsit_rxpdu_queue_monitor_thr_running == B_FALSE) { 3321 break; 3322 } 3323 (void) cv_reltimedwait(&iscsit_rxpdu_queue_monitor_cv, 3324 &iscsit_rxpdu_queue_monitor_mutex, 3325 ISCSIT_RXPDU_QUEUE_MONITOR_INTERVAL * drv_usectohz(1000000), 3326 TR_CLOCK_TICK); 3327 } 3328 mutex_exit(&iscsit_rxpdu_queue_monitor_mutex); 3329 thread_exit(); 3330 } 3331 3332 static void 3333 iscsit_rxpdu_queue_monitor_session(iscsit_sess_t *ist) 3334 { 3335 iscsit_cbuf_t *cbuf = ist->ist_rxpdu_queue; 3336 idm_pdu_t *next_pdu = NULL; 3337 uint32_t index, next_cmdsn, i; 3338 3339 /* 3340 * Assume that all PDUs in the staging queue have a cmdsn >= expcmdsn. 3341 * Starting with the expcmdsn, iterate over the staged PDUs to find 3342 * the next PDU with a wait time greater than the threshold. If found 3343 * advance the staged PDU to the SCSI layer, skipping over the missing 3344 * PDU(s) to get past the hole in the command sequence. It is up to 3345 * the initiator to note that the target has not acknowledged a cmdsn 3346 * and take appropriate action. 3347 * 3348 * Since the PDU(s) arrive in any random order, it is possible that 3349 * that the actual wait time for a particular PDU is much longer than 3350 * the defined threshold. e.g. Consider a case where commands are sent 3351 * over 4 different connections, and cmdsn = 1004 arrives first, then 3352 * 1003, and 1002 and 1001 are lost due to a connection failure. 3353 * So now 1003 is waiting for 1002 to be delivered, and although the 3354 * wait time of 1004 > wait time of 1003, only 1003 will be considered 3355 * by the monitor thread. 1004 will be automatically processed by 3356 * iscsit_process_pdu_in_queue() once the scan is complete and the 3357 * expcmdsn becomes current. 3358 */ 3359 mutex_enter(&ist->ist_sn_mutex); 3360 cbuf = ist->ist_rxpdu_queue; 3361 if (cbuf->cb_num_elems == 0) { 3362 mutex_exit(&ist->ist_sn_mutex); 3363 return; 3364 } 3365 for (next_pdu = NULL, i = 0; ; i++) { 3366 next_cmdsn = ist->ist_expcmdsn + i; /* start at expcmdsn */ 3367 index = next_cmdsn % ISCSIT_RXPDU_QUEUE_LEN; 3368 if ((next_pdu = cbuf->cb_buffer[index]) != NULL) { 3369 /* 3370 * If the PDU wait time has not exceeded threshold 3371 * stop scanning the staging queue until the timer 3372 * fires again 3373 */ 3374 if ((ddi_get_time() - next_pdu->isp_queue_time) 3375 < rxpdu_queue_threshold) { 3376 mutex_exit(&ist->ist_sn_mutex); 3377 return; 3378 } 3379 /* 3380 * Remove the next PDU from the queue and post it 3381 * to the SCSI layer, skipping over the missing 3382 * PDU. Stop scanning the staging queue until 3383 * the monitor timer fires again 3384 */ 3385 (void) iscsit_remove_pdu_from_queue(ist, next_cmdsn); 3386 mutex_exit(&ist->ist_sn_mutex); 3387 DTRACE_PROBE3(advanced__to__blocked__cmdsn, 3388 iscsit_sess_t *, ist, idm_pdu_t *, next_pdu, 3389 uint32_t, next_cmdsn); 3390 iscsit_post_staged_pdu(next_pdu); 3391 /* Deliver any subsequent PDUs immediately */ 3392 iscsit_process_pdu_in_queue(ist); 3393 return; 3394 } 3395 /* 3396 * Skipping over i PDUs, e.g. a case where commands 1001 and 3397 * 1002 are lost in the network, skip over both and post 1003 3398 * expcmdsn then becomes 1004 at the end of the scan. 3399 */ 3400 DTRACE_PROBE2(skipping__over__cmdsn, iscsit_sess_t *, ist, 3401 uint32_t, next_cmdsn); 3402 } 3403 /* 3404 * following the assumption, staged cmdsn >= expcmdsn, this statement 3405 * is never reached. 3406 */ 3407 } 3408