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