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