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