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 iscsit is requested to allocate a buffer larger than 1242 * MaxBurstLength, then the allocation fails (dbuf = NULL) 1243 * and pminsize is modified to be equal to MaxBurstLength. 1244 * stmf/sbd then should re-invoke this function with the 1245 * corrected values for transfer. iscsit allocates a buffer 1246 * whose size is a minimum of the requested size and the 1247 * configured MaxBurstLength. 1248 */ 1249 ASSERT(pminsize); 1250 bsize = min(size, *pminsize); 1251 if (bsize > itask->it_ict->ict_op.op_max_burst_length) { 1252 *pminsize = itask->it_ict->ict_op.op_max_burst_length; 1253 return (NULL); 1254 } 1255 1256 /* Alloc buffer */ 1257 idm_buffer = idm_buf_alloc(itask->it_ict->ict_ic, NULL, bsize); 1258 if (idm_buffer != NULL) { 1259 result = stmf_alloc(STMF_STRUCT_DATA_BUF, 1260 sizeof (iscsit_buf_t), 0); 1261 if (result != NULL) { 1262 /* Fill in stmf_data_buf_t */ 1263 ibuf = result->db_port_private; 1264 ibuf->ibuf_idm_buf = idm_buffer; 1265 ibuf->ibuf_stmf_buf = result; 1266 ibuf->ibuf_is_immed = B_FALSE; 1267 result->db_flags = DB_DONT_CACHE; 1268 result->db_buf_size = bsize; 1269 result->db_data_size = bsize; 1270 result->db_sglist_length = 1; 1271 result->db_sglist[0].seg_addr = idm_buffer->idb_buf; 1272 result->db_sglist[0].seg_length = 1273 idm_buffer->idb_buflen; 1274 return (result); 1275 } 1276 1277 /* Couldn't get the stmf_data_buf_t so free the buffer */ 1278 idm_buf_free(idm_buffer); 1279 } 1280 1281 return (NULL); 1282 } 1283 1284 /*ARGSUSED*/ 1285 static void 1286 iscsit_dbuf_free(stmf_dbuf_store_t *ds, stmf_data_buf_t *dbuf) 1287 { 1288 iscsit_buf_t *ibuf = dbuf->db_port_private; 1289 1290 if (ibuf->ibuf_is_immed) { 1291 /* 1292 * The iscsit_buf_t structure itself will be freed with its 1293 * associated task. Here we just need to free the PDU that 1294 * held the immediate data. 1295 */ 1296 idm_pdu_complete(ibuf->ibuf_immed_data_pdu, IDM_STATUS_SUCCESS); 1297 ibuf->ibuf_immed_data_pdu = 0; 1298 } else { 1299 idm_buf_free(ibuf->ibuf_idm_buf); 1300 stmf_free(dbuf); 1301 } 1302 } 1303 1304 /*ARGSUSED*/ 1305 stmf_status_t 1306 iscsit_xfer_scsi_data(scsi_task_t *task, stmf_data_buf_t *dbuf, 1307 uint32_t ioflags) 1308 { 1309 iscsit_task_t *iscsit_task = task->task_port_private; 1310 iscsit_buf_t *ibuf = dbuf->db_port_private; 1311 int idm_rc; 1312 1313 /* 1314 * If we are aborting then we can ignore this request 1315 */ 1316 if (iscsit_task->it_stmf_abort) { 1317 return (STMF_SUCCESS); 1318 } 1319 1320 /* 1321 * If it's not immediate data then start the transfer 1322 */ 1323 ASSERT(ibuf->ibuf_is_immed == B_FALSE); 1324 if (dbuf->db_flags & DB_DIRECTION_TO_RPORT) { 1325 /* 1326 * IDM will call iscsit_build_hdr so lock now to serialize 1327 * access to the SN values. We need to lock here to enforce 1328 * lock ordering 1329 */ 1330 rw_enter(&iscsit_task->it_ict->ict_sess->ist_sn_rwlock, 1331 RW_READER); 1332 idm_rc = idm_buf_tx_to_ini(iscsit_task->it_idm_task, 1333 ibuf->ibuf_idm_buf, dbuf->db_relative_offset, 1334 dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf); 1335 rw_exit(&iscsit_task->it_ict->ict_sess->ist_sn_rwlock); 1336 1337 return (iscsit_idm_to_stmf(idm_rc)); 1338 } else if (dbuf->db_flags & DB_DIRECTION_FROM_RPORT) { 1339 /* Grab the SN lock (see comment above) */ 1340 rw_enter(&iscsit_task->it_ict->ict_sess->ist_sn_rwlock, 1341 RW_READER); 1342 idm_rc = idm_buf_rx_from_ini(iscsit_task->it_idm_task, 1343 ibuf->ibuf_idm_buf, dbuf->db_relative_offset, 1344 dbuf->db_data_size, &iscsit_buf_xfer_cb, dbuf); 1345 rw_exit(&iscsit_task->it_ict->ict_sess->ist_sn_rwlock); 1346 1347 return (iscsit_idm_to_stmf(idm_rc)); 1348 } 1349 1350 /* What are we supposed to do if there is no direction? */ 1351 return (STMF_INVALID_ARG); 1352 } 1353 1354 static void 1355 iscsit_buf_xfer_cb(idm_buf_t *idb, idm_status_t status) 1356 { 1357 iscsit_task_t *itask = idb->idb_task_binding->idt_private; 1358 stmf_data_buf_t *dbuf = idb->idb_cb_arg; 1359 1360 dbuf->db_xfer_status = iscsit_idm_to_stmf(status); 1361 1362 /* 1363 * If the task has been aborted then we don't need to call STMF 1364 */ 1365 if (itask->it_stmf_abort) { 1366 return; 1367 } 1368 1369 /* 1370 * COMSTAR currently requires port providers to support 1371 * the DB_SEND_STATUS_GOOD flag even if phase collapse is 1372 * not supported. So we will roll our own... pretend we are 1373 * COMSTAR and ask for a status PDU. 1374 */ 1375 if ((dbuf->db_flags & DB_SEND_STATUS_GOOD) && 1376 status == IDM_STATUS_SUCCESS) { 1377 /* 1378 * If iscsit_send_scsi_status succeeds then the TX PDU 1379 * callback will call stmf_send_status_done and set 1380 * STMF_IOF_LPORT_DONE. Consequently we don't need 1381 * to call stmf_data_xfer_done in that case. We 1382 * still need to call it if we get a failure. 1383 * 1384 * To elaborate on this some more, upon successful 1385 * return from iscsit_send_scsi_status it's possible 1386 * that itask and idb have been freed and are no 1387 * longer valid. 1388 */ 1389 if (iscsit_send_scsi_status(itask->it_stmf_task, 0) 1390 != STMF_SUCCESS) { 1391 /* Failed to send status */ 1392 dbuf->db_xfer_status = STMF_FAILURE; 1393 stmf_data_xfer_done(itask->it_stmf_task, dbuf, 1394 STMF_IOF_LPORT_DONE); 1395 } 1396 } else { 1397 stmf_data_xfer_done(itask->it_stmf_task, dbuf, 0); 1398 } 1399 } 1400 1401 1402 /*ARGSUSED*/ 1403 stmf_status_t 1404 iscsit_send_scsi_status(scsi_task_t *task, uint32_t ioflags) 1405 { 1406 iscsit_task_t *itask = task->task_port_private; 1407 iscsi_scsi_rsp_hdr_t *rsp; 1408 idm_pdu_t *pdu; 1409 int resp_datalen; 1410 1411 /* 1412 * If this task is aborted then we don't need to respond. 1413 */ 1414 if (itask->it_stmf_abort) { 1415 return (STMF_SUCCESS); 1416 } 1417 1418 /* 1419 * If this is a task management status, handle it elsewhere. 1420 */ 1421 if (task->task_mgmt_function != TM_NONE) { 1422 /* 1423 * Don't wait for the PDU completion to tell STMF 1424 * the task is done -- it doesn't really matter and 1425 * it makes life complicated if STMF later asks us to 1426 * abort the request and we don't know whether the 1427 * status has been sent or not. 1428 */ 1429 itask->it_tm_responded = B_TRUE; 1430 iscsit_send_task_mgmt_resp(itask->it_tm_pdu, 1431 (task->task_completion_status == STMF_SUCCESS) ? 1432 SCSI_TCP_TM_RESP_COMPLETE : SCSI_TCP_TM_RESP_FUNC_NOT_SUPP); 1433 stmf_send_status_done(task, STMF_SUCCESS, 1434 STMF_IOF_LPORT_DONE); 1435 return (STMF_SUCCESS); 1436 } 1437 1438 /* 1439 * Remove the task from the session task list 1440 */ 1441 iscsit_task_done(itask); 1442 1443 /* 1444 * Send status 1445 */ 1446 mutex_enter(&itask->it_idm_task->idt_mutex); 1447 if ((itask->it_idm_task->idt_state == TASK_ACTIVE) && 1448 (task->task_completion_status == STMF_SUCCESS) && 1449 (task->task_sense_length == 0) && 1450 (task->task_resid == 0)) { 1451 itask->it_idm_task->idt_state = TASK_COMPLETE; 1452 /* PDU callback releases task hold */ 1453 idm_task_hold(itask->it_idm_task); 1454 mutex_exit(&itask->it_idm_task->idt_mutex); 1455 /* 1456 * Fast path. Cached status PDU's are already 1457 * initialized. We just need to fill in 1458 * connection and task information. 1459 */ 1460 pdu = kmem_cache_alloc(iscsit_status_pdu_cache, KM_SLEEP); 1461 pdu->isp_ic = itask->it_ict->ict_ic; 1462 pdu->isp_private = itask; 1463 1464 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr; 1465 rsp->itt = itask->it_itt; 1466 rsp->cmd_status = task->task_scsi_status; 1467 iscsit_pdu_tx(pdu); 1468 return (STMF_SUCCESS); 1469 } else { 1470 if (itask->it_idm_task->idt_state != TASK_ACTIVE) { 1471 mutex_exit(&itask->it_idm_task->idt_mutex); 1472 return (STMF_FAILURE); 1473 } 1474 itask->it_idm_task->idt_state = TASK_COMPLETE; 1475 /* PDU callback releases task hold */ 1476 idm_task_hold(itask->it_idm_task); 1477 mutex_exit(&itask->it_idm_task->idt_mutex); 1478 1479 resp_datalen = (task->task_sense_length == 0) ? 0 : 1480 (task->task_sense_length + sizeof (uint16_t)); 1481 1482 pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen); 1483 idm_pdu_init(pdu, itask->it_ict->ict_ic, itask, 1484 iscsit_send_status_done); 1485 1486 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr; 1487 bzero(rsp, sizeof (*rsp)); 1488 rsp->opcode = ISCSI_OP_SCSI_RSP; 1489 1490 rsp->flags = ISCSI_FLAG_FINAL; 1491 if (task->task_status_ctrl & TASK_SCTRL_OVER) { 1492 rsp->flags |= ISCSI_FLAG_CMD_OVERFLOW; 1493 } else if (task->task_status_ctrl & TASK_SCTRL_UNDER) { 1494 rsp->flags |= ISCSI_FLAG_CMD_UNDERFLOW; 1495 } 1496 1497 rsp->bi_residual_count = 0; 1498 rsp->residual_count = htonl(task->task_resid); 1499 rsp->itt = itask->it_itt; 1500 rsp->response = ISCSI_STATUS_CMD_COMPLETED; 1501 rsp->cmd_status = task->task_scsi_status; 1502 if (task->task_sense_length != 0) { 1503 /* 1504 * Add a byte to provide the sense length in 1505 * the response 1506 */ 1507 *(uint16_t *)((void *)pdu->isp_data) = 1508 htons(task->task_sense_length); 1509 bcopy(task->task_sense_data, 1510 (uint8_t *)pdu->isp_data + 1511 sizeof (uint16_t), 1512 task->task_sense_length); 1513 hton24(rsp->dlength, resp_datalen); 1514 } 1515 1516 DTRACE_PROBE5(iscsi__scsi__response, 1517 iscsit_conn_t *, itask->it_ict, 1518 uint8_t, rsp->response, 1519 uint8_t, rsp->cmd_status, 1520 idm_pdu_t *, pdu, 1521 scsi_task_t *, task); 1522 1523 iscsit_pdu_tx(pdu); 1524 1525 return (STMF_SUCCESS); 1526 } 1527 } 1528 1529 /*ARGSUSED*/ 1530 static void 1531 iscsit_send_good_status_done(idm_pdu_t *pdu, idm_status_t status) 1532 { 1533 iscsit_task_t *itask; 1534 boolean_t aborted; 1535 1536 itask = pdu->isp_private; 1537 aborted = itask->it_stmf_abort; 1538 1539 /* 1540 * After releasing the hold the task may be freed at any time so 1541 * don't touch it. 1542 */ 1543 idm_task_rele(itask->it_idm_task); 1544 if (!aborted) { 1545 stmf_send_status_done(itask->it_stmf_task, 1546 iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE); 1547 } 1548 kmem_cache_free(iscsit_status_pdu_cache, pdu); 1549 } 1550 1551 /*ARGSUSED*/ 1552 static void 1553 iscsit_send_status_done(idm_pdu_t *pdu, idm_status_t status) 1554 { 1555 iscsit_task_t *itask; 1556 boolean_t aborted; 1557 1558 itask = pdu->isp_private; 1559 aborted = itask->it_stmf_abort; 1560 1561 /* 1562 * After releasing the hold the task may be freed at any time so 1563 * don't touch it. 1564 */ 1565 idm_task_rele(itask->it_idm_task); 1566 if (!aborted) { 1567 stmf_send_status_done(itask->it_stmf_task, 1568 iscsit_idm_to_stmf(pdu->isp_status), STMF_IOF_LPORT_DONE); 1569 } 1570 idm_pdu_free(pdu); 1571 } 1572 1573 1574 void 1575 iscsit_lport_task_free(scsi_task_t *task) 1576 { 1577 iscsit_task_t *itask = task->task_port_private; 1578 1579 /* We only call idm_task_start for regular tasks, not task management */ 1580 if (task->task_mgmt_function == TM_NONE) { 1581 idm_task_done(itask->it_idm_task); 1582 iscsit_task_free(itask); 1583 return; 1584 } else { 1585 iscsit_tm_task_free(itask); 1586 } 1587 } 1588 1589 /*ARGSUSED*/ 1590 stmf_status_t 1591 iscsit_abort(stmf_local_port_t *lport, int abort_cmd, void *arg, uint32_t flags) 1592 { 1593 scsi_task_t *st = (scsi_task_t *)arg; 1594 iscsit_task_t *iscsit_task; 1595 idm_task_t *idt; 1596 1597 /* 1598 * If this is a task management request then there's really not much to 1599 * do. 1600 */ 1601 if (st->task_mgmt_function != TM_NONE) { 1602 return (STMF_ABORT_SUCCESS); 1603 } 1604 1605 /* 1606 * Regular task, start cleaning up 1607 */ 1608 iscsit_task = st->task_port_private; 1609 idt = iscsit_task->it_idm_task; 1610 mutex_enter(&iscsit_task->it_mutex); 1611 iscsit_task->it_stmf_abort = B_TRUE; 1612 if (iscsit_task->it_aborted) { 1613 mutex_exit(&iscsit_task->it_mutex); 1614 /* 1615 * Task is no longer active 1616 */ 1617 iscsit_task_done(iscsit_task); 1618 1619 /* 1620 * STMF specification is wrong... says to return 1621 * STMF_ABORTED, the code actually looks for 1622 * STMF_ABORT_SUCCESS. 1623 */ 1624 return (STMF_ABORT_SUCCESS); 1625 } else { 1626 mutex_exit(&iscsit_task->it_mutex); 1627 /* 1628 * Call IDM to abort the task. Due to a variety of 1629 * circumstances the task may already be in the process of 1630 * aborting. 1631 * We'll let IDM worry about rationalizing all that except 1632 * for one particular instance. If the state of the task 1633 * is TASK_COMPLETE, we need to indicate to the framework 1634 * that we are in fact done. This typically happens with 1635 * framework-initiated task management type requests 1636 * (e.g. abort task). 1637 */ 1638 if (idt->idt_state == TASK_COMPLETE) { 1639 idm_refcnt_wait_ref(&idt->idt_refcnt); 1640 return (STMF_ABORT_SUCCESS); 1641 } else { 1642 idm_task_abort(idt->idt_ic, idt, AT_TASK_MGMT_ABORT); 1643 return (STMF_SUCCESS); 1644 } 1645 } 1646 1647 /*NOTREACHED*/ 1648 } 1649 1650 /*ARGSUSED*/ 1651 void 1652 iscsit_ctl(stmf_local_port_t *lport, int cmd, void *arg) 1653 { 1654 iscsit_tgt_t *iscsit_tgt; 1655 1656 ASSERT((cmd == STMF_CMD_LPORT_ONLINE) || 1657 (cmd == STMF_ACK_LPORT_ONLINE_COMPLETE) || 1658 (cmd == STMF_CMD_LPORT_OFFLINE) || 1659 (cmd == STMF_ACK_LPORT_OFFLINE_COMPLETE)); 1660 1661 iscsit_tgt = (iscsit_tgt_t *)lport->lport_port_private; 1662 1663 switch (cmd) { 1664 case STMF_CMD_LPORT_ONLINE: 1665 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_REQ); 1666 break; 1667 case STMF_CMD_LPORT_OFFLINE: 1668 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_REQ); 1669 break; 1670 case STMF_ACK_LPORT_ONLINE_COMPLETE: 1671 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_ONLINE_COMPLETE_ACK); 1672 break; 1673 case STMF_ACK_LPORT_OFFLINE_COMPLETE: 1674 iscsit_tgt_sm_event(iscsit_tgt, TE_STMF_OFFLINE_COMPLETE_ACK); 1675 break; 1676 1677 default: 1678 break; 1679 } 1680 } 1681 1682 static stmf_status_t 1683 iscsit_idm_to_stmf(idm_status_t idmrc) 1684 { 1685 switch (idmrc) { 1686 case IDM_STATUS_SUCCESS: 1687 return (STMF_SUCCESS); 1688 default: 1689 return (STMF_FAILURE); 1690 } 1691 /*NOTREACHED*/ 1692 } 1693 1694 1695 /* 1696 * ISCSI protocol 1697 */ 1698 1699 void 1700 iscsit_op_scsi_cmd(idm_conn_t *ic, idm_pdu_t *rx_pdu) 1701 { 1702 iscsit_conn_t *ict; 1703 iscsit_task_t *itask; 1704 scsi_task_t *task; 1705 iscsit_buf_t *ibuf; 1706 iscsi_scsi_cmd_hdr_t *iscsi_scsi = 1707 (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr; 1708 iscsi_addl_hdr_t *ahs_hdr; 1709 uint16_t addl_cdb_len = 0; 1710 1711 ict = ic->ic_handle; 1712 1713 itask = iscsit_task_alloc(ict); 1714 if (itask == NULL) { 1715 /* Finish processing request */ 1716 iscsit_set_cmdsn(ict, rx_pdu); 1717 1718 iscsit_send_direct_scsi_resp(ict, rx_pdu, 1719 ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY); 1720 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 1721 return; 1722 } 1723 1724 1725 /* 1726 * Note CmdSN and ITT in task. IDM will have already validated this 1727 * request against the connection state so we don't need to check 1728 * that (the connection may have changed state in the meantime but 1729 * we will catch that when we try to send a response) 1730 */ 1731 itask->it_cmdsn = ntohl(iscsi_scsi->cmdsn); 1732 itask->it_itt = iscsi_scsi->itt; 1733 1734 /* 1735 * Check for extended CDB AHS 1736 */ 1737 if (iscsi_scsi->hlength > 0) { 1738 ahs_hdr = (iscsi_addl_hdr_t *)iscsi_scsi; 1739 addl_cdb_len = ((ahs_hdr->ahs_hlen_hi << 8) | 1740 ahs_hdr->ahs_hlen_lo) - 1; /* Adjust for reserved byte */ 1741 if (((addl_cdb_len + 4) / sizeof (uint32_t)) > 1742 iscsi_scsi->hlength) { 1743 /* Mangled header info, drop it */ 1744 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 1745 return; 1746 } 1747 } 1748 1749 ict = rx_pdu->isp_ic->ic_handle; /* IDM client private */ 1750 1751 /* 1752 * Add task to session list. This function will also check to 1753 * ensure that the task does not already exist. 1754 */ 1755 if (iscsit_task_start(itask) != IDM_STATUS_SUCCESS) { 1756 /* 1757 * Task exists, free all resources and reject. Don't 1758 * update expcmdsn in this case because RFC 3720 says 1759 * "The CmdSN of the rejected command PDU (if it is a 1760 * non-immediate command) MUST NOT be considered received 1761 * by the target (i.e., a command sequence gap must be 1762 * assumed for the CmdSN), even though the CmdSN of the 1763 * rejected command PDU may be reliably ascertained. Upon 1764 * receiving the Reject, the initiator MUST plug the CmdSN 1765 * gap in order to continue to use the session. The gap 1766 * may be plugged either by transmitting a command PDU 1767 * with the same CmdSN, or by aborting the task (see section 1768 * 6.9 on how an abort may plug a CmdSN gap)." (Section 6.3) 1769 */ 1770 iscsit_task_free(itask); 1771 iscsit_send_reject(ict, rx_pdu, ISCSI_REJECT_TASK_IN_PROGRESS); 1772 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 1773 return; 1774 } 1775 1776 /* Update sequence numbers */ 1777 iscsit_set_cmdsn(ict, rx_pdu); 1778 1779 /* 1780 * Allocate STMF task 1781 */ 1782 itask->it_stmf_task = stmf_task_alloc( 1783 itask->it_ict->ict_sess->ist_lport, 1784 itask->it_ict->ict_sess->ist_stmf_sess, iscsi_scsi->lun, 1785 16 + addl_cdb_len, 0); 1786 if (itask->it_stmf_task == NULL) { 1787 /* 1788 * Either stmf really couldn't get memory for a task or, 1789 * more likely, the LU is currently in reset. Either way 1790 * we have no choice but to fail the request. 1791 */ 1792 iscsit_task_done(itask); 1793 iscsit_task_free(itask); 1794 iscsit_send_direct_scsi_resp(ict, rx_pdu, 1795 ISCSI_STATUS_CMD_COMPLETED, STATUS_BUSY); 1796 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 1797 return; 1798 } 1799 1800 task = itask->it_stmf_task; 1801 task->task_port_private = itask; 1802 1803 bcopy(iscsi_scsi->lun, task->task_lun_no, sizeof (task->task_lun_no)); 1804 1805 /* 1806 * iSCSI and Comstar use the same values. Should we rely on this 1807 * or translate them bit-wise? 1808 */ 1809 1810 task->task_flags = 1811 (((iscsi_scsi->flags & ISCSI_FLAG_CMD_READ) ? TF_READ_DATA : 0) | 1812 ((iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ? TF_WRITE_DATA : 0) | 1813 ((rx_pdu->isp_datalen == 0) ? 0 : TF_INITIAL_BURST)); 1814 1815 switch (iscsi_scsi->flags & ISCSI_FLAG_CMD_ATTR_MASK) { 1816 case ISCSI_ATTR_UNTAGGED: 1817 break; 1818 case ISCSI_ATTR_SIMPLE: 1819 task->task_additional_flags |= TF_ATTR_SIMPLE_QUEUE; 1820 break; 1821 case ISCSI_ATTR_ORDERED: 1822 task->task_additional_flags |= TF_ATTR_ORDERED_QUEUE; 1823 break; 1824 case ISCSI_ATTR_HEAD_OF_QUEUE: 1825 task->task_additional_flags |= TF_ATTR_HEAD_OF_QUEUE; 1826 break; 1827 case ISCSI_ATTR_ACA: 1828 task->task_additional_flags |= TF_ATTR_ACA; 1829 break; 1830 default: 1831 /* Protocol error but just take it, treat as untagged */ 1832 break; 1833 } 1834 1835 1836 task->task_additional_flags = 0; 1837 task->task_priority = 0; 1838 task->task_mgmt_function = TM_NONE; 1839 1840 /* 1841 * This "task_max_nbufs" doesn't map well to BIDI. We probably need 1842 * parameter for each direction. "MaxOutstandingR2T" may very well 1843 * be set to one which could prevent us from doing simultaneous 1844 * transfers in each direction. 1845 */ 1846 task->task_max_nbufs = (iscsi_scsi->flags & ISCSI_FLAG_CMD_WRITE) ? 1847 ict->ict_op.op_max_outstanding_r2t : STMF_BUFS_MAX; 1848 task->task_cmd_seq_no = ntohl(iscsi_scsi->itt); 1849 task->task_expected_xfer_length = ntohl(iscsi_scsi->data_length); 1850 1851 /* Copy CDB */ 1852 bcopy(iscsi_scsi->scb, task->task_cdb, 16); 1853 if (addl_cdb_len > 0) { 1854 bcopy(ahs_hdr->ahs_extscb, task->task_cdb + 16, addl_cdb_len); 1855 } 1856 1857 DTRACE_ISCSI_3(scsi__command, idm_conn_t *, ic, 1858 iscsi_scsi_cmd_hdr_t *, (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr, 1859 scsi_task_t *, task); 1860 1861 /* 1862 * Copy the transport header into the task handle from the PDU 1863 * handle. The transport header describes this task's remote tagged 1864 * buffer. 1865 */ 1866 if (rx_pdu->isp_transport_hdrlen != 0) { 1867 bcopy(rx_pdu->isp_transport_hdr, 1868 itask->it_idm_task->idt_transport_hdr, 1869 rx_pdu->isp_transport_hdrlen); 1870 } 1871 1872 /* 1873 * Tell IDM about our new active task 1874 */ 1875 idm_task_start(itask->it_idm_task, (uintptr_t)itask->it_itt); 1876 1877 /* 1878 * If we have any immediate data then setup the immediate buffer 1879 * context that comes with the task 1880 */ 1881 if (rx_pdu->isp_datalen) { 1882 ibuf = itask->it_immed_data; 1883 ibuf->ibuf_immed_data_pdu = rx_pdu; 1884 ibuf->ibuf_stmf_buf->db_data_size = rx_pdu->isp_datalen; 1885 ibuf->ibuf_stmf_buf->db_buf_size = rx_pdu->isp_datalen; 1886 ibuf->ibuf_stmf_buf->db_relative_offset = 0; 1887 ibuf->ibuf_stmf_buf->db_sglist[0].seg_length = 1888 rx_pdu->isp_datalen; 1889 ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr = rx_pdu->isp_data; 1890 1891 DTRACE_ISCSI_8(xfer__start, idm_conn_t *, ic, 1892 uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr, 1893 uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset, 1894 uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */ 1895 uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI); 1896 1897 /* 1898 * For immediate data transfer, there is no callback from 1899 * stmf to indicate that the initial burst of data is 1900 * transferred successfully. In some cases, the task can 1901 * get freed before execution returns from stmf_post_task. 1902 * Although this xfer-start/done probe accurately tracks 1903 * the size of the transfer, it does only provide a best 1904 * effort on the timing of the transfer. 1905 */ 1906 DTRACE_ISCSI_8(xfer__done, idm_conn_t *, ic, 1907 uintptr_t, ibuf->ibuf_stmf_buf->db_sglist[0].seg_addr, 1908 uint32_t, ibuf->ibuf_stmf_buf->db_relative_offset, 1909 uint64_t, 0, uint32_t, 0, uint32_t, 0, /* no raddr */ 1910 uint32_t, rx_pdu->isp_datalen, int, XFER_BUF_TX_TO_INI); 1911 1912 stmf_post_task(task, ibuf->ibuf_stmf_buf); 1913 } else { 1914 1915 stmf_post_task(task, NULL); 1916 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 1917 } 1918 } 1919 1920 /*ARGSUSED*/ 1921 void 1922 iscsit_deferred_dispatch(idm_pdu_t *rx_pdu) 1923 { 1924 iscsit_conn_t *ict = rx_pdu->isp_ic->ic_handle; 1925 1926 /* 1927 * If the connection has been lost then ignore new PDU's 1928 */ 1929 mutex_enter(&ict->ict_mutex); 1930 if (ict->ict_lost) { 1931 mutex_exit(&ict->ict_mutex); 1932 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL); 1933 return; 1934 } 1935 1936 /* 1937 * Grab a hold on the connection to prevent it from going away 1938 * between now and when the taskq function is called. 1939 */ 1940 iscsit_conn_dispatch_hold(ict); 1941 mutex_exit(&ict->ict_mutex); 1942 1943 if (taskq_dispatch(iscsit_global.global_dispatch_taskq, 1944 iscsit_deferred, rx_pdu, DDI_NOSLEEP) == NULL) { 1945 /* 1946 * In the unlikely scenario that we couldn't get the resources 1947 * to dispatch the PDU then just drop it. 1948 */ 1949 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL); 1950 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL); 1951 iscsit_conn_dispatch_rele(ict); 1952 } 1953 } 1954 1955 static void 1956 iscsit_deferred(void *rx_pdu_void) 1957 { 1958 idm_pdu_t *rx_pdu = rx_pdu_void; 1959 idm_conn_t *ic = rx_pdu->isp_ic; 1960 iscsit_conn_t *ict = ic->ic_handle; 1961 1962 switch (IDM_PDU_OPCODE(rx_pdu)) { 1963 case ISCSI_OP_NOOP_OUT: 1964 iscsit_set_cmdsn(ict, rx_pdu); 1965 iscsit_pdu_op_noop(ict, rx_pdu); 1966 break; 1967 case ISCSI_OP_LOGIN_CMD: 1968 iscsit_pdu_op_login_cmd(ict, rx_pdu); 1969 break; 1970 case ISCSI_OP_TEXT_CMD: 1971 iscsit_set_cmdsn(ict, rx_pdu); 1972 iscsit_pdu_op_text_cmd(ict, rx_pdu); 1973 break; 1974 case ISCSI_OP_LOGOUT_CMD: 1975 iscsit_set_cmdsn(ict, rx_pdu); 1976 iscsit_pdu_op_logout_cmd(ict, rx_pdu); 1977 break; 1978 default: 1979 /* Protocol error. IDM should have caught this */ 1980 idm_pdu_complete(rx_pdu, IDM_STATUS_FAIL); 1981 ASSERT(0); 1982 break; 1983 } 1984 1985 iscsit_conn_dispatch_rele(ict); 1986 } 1987 1988 static void 1989 iscsit_send_direct_scsi_resp(iscsit_conn_t *ict, idm_pdu_t *rx_pdu, 1990 uint8_t response, uint8_t cmd_status) 1991 { 1992 idm_pdu_t *rsp_pdu; 1993 idm_conn_t *ic; 1994 iscsi_scsi_rsp_hdr_t *resp; 1995 iscsi_scsi_cmd_hdr_t *req = 1996 (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr; 1997 1998 ic = ict->ict_ic; 1999 2000 rsp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_rsp_hdr_t), 0); 2001 idm_pdu_init(rsp_pdu, ic, NULL, NULL); 2002 resp = (iscsi_scsi_rsp_hdr_t *)rsp_pdu->isp_hdr; 2003 2004 resp->opcode = ISCSI_OP_SCSI_RSP; 2005 resp->flags = ISCSI_FLAG_FINAL; 2006 resp->response = response; 2007 resp->cmd_status = cmd_status; 2008 resp->itt = req->itt; 2009 if ((response == ISCSI_STATUS_CMD_COMPLETED) && 2010 (req->data_length != 0) && 2011 ((req->flags & ISCSI_FLAG_CMD_READ) || 2012 (req->flags & ISCSI_FLAG_CMD_WRITE))) { 2013 resp->flags |= ISCSI_FLAG_CMD_UNDERFLOW; 2014 resp->residual_count = req->data_length; 2015 } 2016 2017 DTRACE_PROBE4(iscsi__scsi__direct__response, 2018 iscsit_conn_t *, ict, 2019 uint8_t, resp->response, 2020 uint8_t, resp->cmd_status, 2021 idm_pdu_t *, rsp_pdu); 2022 2023 iscsit_pdu_tx(rsp_pdu); 2024 } 2025 2026 void 2027 iscsit_send_task_mgmt_resp(idm_pdu_t *tm_resp_pdu, uint8_t tm_status) 2028 { 2029 iscsi_scsi_task_mgt_rsp_hdr_t *tm_resp; 2030 2031 tm_resp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr; 2032 tm_resp->response = tm_status; 2033 2034 DTRACE_PROBE3(iscsi__scsi__tm__response, 2035 iscsit_conn_t *, tm_resp_pdu->isp_ic->ic_handle, 2036 uint8_t, tm_resp->response, 2037 idm_pdu_t *, tm_resp_pdu); 2038 iscsit_pdu_tx(tm_resp_pdu); 2039 } 2040 2041 void 2042 iscsit_op_scsi_task_mgmt(iscsit_conn_t *ict, idm_pdu_t *rx_pdu) 2043 { 2044 idm_pdu_t *tm_resp_pdu; 2045 iscsit_task_t *itask; 2046 iscsit_task_t *tm_itask; 2047 scsi_task_t *task; 2048 iscsi_scsi_task_mgt_hdr_t *iscsi_tm = 2049 (iscsi_scsi_task_mgt_hdr_t *)rx_pdu->isp_hdr; 2050 iscsi_scsi_task_mgt_rsp_hdr_t *iscsi_tm_rsp = 2051 (iscsi_scsi_task_mgt_rsp_hdr_t *)rx_pdu->isp_hdr; 2052 uint32_t rtt, cmdsn, refcmdsn; 2053 uint8_t tm_func; 2054 2055 /* 2056 * Setup response PDU (response field will get filled in later) 2057 */ 2058 tm_resp_pdu = idm_pdu_alloc(sizeof (iscsi_scsi_task_mgt_rsp_hdr_t), 0); 2059 if (tm_resp_pdu == NULL) { 2060 /* Can't respond, just drop it */ 2061 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2062 return; 2063 } 2064 idm_pdu_init(tm_resp_pdu, ict->ict_ic, NULL, NULL); 2065 iscsi_tm_rsp = (iscsi_scsi_task_mgt_rsp_hdr_t *)tm_resp_pdu->isp_hdr; 2066 bzero(iscsi_tm_rsp, sizeof (iscsi_scsi_task_mgt_rsp_hdr_t)); 2067 iscsi_tm_rsp->opcode = ISCSI_OP_SCSI_TASK_MGT_RSP; 2068 iscsi_tm_rsp->flags = ISCSI_FLAG_FINAL; 2069 iscsi_tm_rsp->itt = rx_pdu->isp_hdr->itt; 2070 2071 /* 2072 * Figure out what we're being asked to do. 2073 */ 2074 DTRACE_PROBE4(iscsi__scsi__tm__request, 2075 iscsit_conn_t *, ict, 2076 uint8_t, (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK), 2077 uint32_t, iscsi_tm->rtt, 2078 idm_pdu_t *, rx_pdu); 2079 switch (iscsi_tm->function & ISCSI_FLAG_TASK_MGMT_FUNCTION_MASK) { 2080 case ISCSI_TM_FUNC_ABORT_TASK: 2081 /* 2082 * STMF doesn't currently support the "abort task" task 2083 * management command although it does support aborting 2084 * an individual task. We'll get STMF to abort the task 2085 * for us but handle the details of the task management 2086 * command ourselves. 2087 * 2088 * Find the task associated with the referenced task tag. 2089 */ 2090 rtt = iscsi_tm->rtt; 2091 itask = (iscsit_task_t *)idm_task_find_by_handle(ict->ict_ic, 2092 (uintptr_t)rtt); 2093 2094 if (itask == NULL) { 2095 cmdsn = ntohl(iscsi_tm->cmdsn); 2096 refcmdsn = ntohl(iscsi_tm->refcmdsn); 2097 2098 /* 2099 * Task was not found. If RefCmdSN is within the CmdSN 2100 * window and less than CmdSN of the TM function, return 2101 * "Function Complete". Otherwise, return 2102 * "Task Does Not Exist". 2103 */ 2104 2105 if (iscsit_cmdsn_in_window(ict, refcmdsn) && 2106 (refcmdsn < cmdsn)) { 2107 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2108 SCSI_TCP_TM_RESP_COMPLETE); 2109 } else { 2110 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2111 SCSI_TCP_TM_RESP_NO_TASK); 2112 } 2113 } else { 2114 2115 /* 2116 * Tell STMF to abort the task. This will do no harm 2117 * if the task is already complete. 2118 */ 2119 stmf_abort(STMF_QUEUE_TASK_ABORT, itask->it_stmf_task, 2120 STMF_ABORTED, NULL); 2121 2122 /* 2123 * Make sure the task hasn't already completed 2124 */ 2125 mutex_enter(&itask->it_idm_task->idt_mutex); 2126 if ((itask->it_idm_task->idt_state == TASK_COMPLETE) || 2127 (itask->it_idm_task->idt_state == TASK_IDLE)) { 2128 /* 2129 * Task is complete, return "Task Does Not 2130 * Exist" 2131 */ 2132 mutex_exit(&itask->it_idm_task->idt_mutex); 2133 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2134 SCSI_TCP_TM_RESP_NO_TASK); 2135 } else { 2136 /* 2137 * STMF is now aborting the task, return 2138 * "Function Complete" 2139 */ 2140 mutex_exit(&itask->it_idm_task->idt_mutex); 2141 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2142 SCSI_TCP_TM_RESP_COMPLETE); 2143 } 2144 idm_task_rele(itask->it_idm_task); 2145 } 2146 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2147 return; 2148 2149 case ISCSI_TM_FUNC_ABORT_TASK_SET: 2150 tm_func = TM_ABORT_TASK_SET; 2151 break; 2152 2153 case ISCSI_TM_FUNC_CLEAR_ACA: 2154 tm_func = TM_CLEAR_ACA; 2155 break; 2156 2157 case ISCSI_TM_FUNC_CLEAR_TASK_SET: 2158 tm_func = TM_CLEAR_TASK_SET; 2159 break; 2160 2161 case ISCSI_TM_FUNC_LOGICAL_UNIT_RESET: 2162 tm_func = TM_LUN_RESET; 2163 break; 2164 2165 case ISCSI_TM_FUNC_TARGET_WARM_RESET: 2166 tm_func = TM_TARGET_WARM_RESET; 2167 break; 2168 2169 case ISCSI_TM_FUNC_TARGET_COLD_RESET: 2170 tm_func = TM_TARGET_COLD_RESET; 2171 break; 2172 2173 case ISCSI_TM_FUNC_TASK_REASSIGN: 2174 /* 2175 * We do not currently support allegiance reassignment. When 2176 * we start supporting ERL1+, we will need to. 2177 */ 2178 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2179 SCSI_TCP_TM_RESP_NO_ALLG_REASSN); 2180 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2181 return; 2182 2183 default: 2184 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2185 SCSI_TCP_TM_RESP_REJECTED); 2186 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2187 return; 2188 } 2189 2190 tm_itask = iscsit_tm_task_alloc(ict); 2191 if (tm_itask == NULL) { 2192 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2193 SCSI_TCP_TM_RESP_REJECTED); 2194 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2195 return; 2196 } 2197 2198 2199 task = stmf_task_alloc(ict->ict_sess->ist_lport, 2200 ict->ict_sess->ist_stmf_sess, iscsi_tm->lun, 2201 0, STMF_TASK_EXT_NONE); 2202 if (task == NULL) { 2203 /* 2204 * If this happens, either the LU is in reset, couldn't 2205 * get memory, or some other condition in which we simply 2206 * can't complete this request. It would be nice to return 2207 * an error code like "busy" but the closest we have is 2208 * "rejected". 2209 */ 2210 iscsit_send_task_mgmt_resp(tm_resp_pdu, 2211 SCSI_TCP_TM_RESP_REJECTED); 2212 iscsit_tm_task_free(tm_itask); 2213 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2214 return; 2215 } 2216 2217 tm_itask->it_tm_pdu = tm_resp_pdu; 2218 tm_itask->it_stmf_task = task; 2219 task->task_port_private = tm_itask; 2220 task->task_mgmt_function = tm_func; 2221 task->task_additional_flags = TASK_AF_NO_EXPECTED_XFER_LENGTH; 2222 task->task_priority = 0; 2223 task->task_max_nbufs = STMF_BUFS_MAX; 2224 task->task_cmd_seq_no = iscsi_tm->itt; 2225 task->task_expected_xfer_length = 0; 2226 2227 stmf_post_task(task, NULL); 2228 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2229 } 2230 2231 static void 2232 iscsit_pdu_op_noop(iscsit_conn_t *ict, idm_pdu_t *rx_pdu) 2233 { 2234 iscsi_nop_out_hdr_t *out = (iscsi_nop_out_hdr_t *)rx_pdu->isp_hdr; 2235 iscsi_nop_in_hdr_t *in; 2236 int resp_datalen; 2237 idm_pdu_t *resp; 2238 2239 /* Ignore the response from initiator */ 2240 if ((out->itt == ISCSI_RSVD_TASK_TAG) || 2241 (out->ttt != ISCSI_RSVD_TASK_TAG)) { 2242 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2243 return; 2244 } 2245 2246 /* Allocate a PDU to respond */ 2247 resp_datalen = ntoh24(out->dlength); 2248 resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), resp_datalen); 2249 idm_pdu_init(resp, ict->ict_ic, NULL, NULL); 2250 if (resp_datalen > 0) { 2251 bcopy(rx_pdu->isp_data, resp->isp_data, resp_datalen); 2252 } 2253 2254 in = (iscsi_nop_in_hdr_t *)resp->isp_hdr; 2255 bzero(in, sizeof (*in)); 2256 in->opcode = ISCSI_OP_NOOP_IN; 2257 in->flags = ISCSI_FLAG_FINAL; 2258 bcopy(out->lun, in->lun, 8); 2259 in->itt = out->itt; 2260 in->ttt = ISCSI_RSVD_TASK_TAG; 2261 hton24(in->dlength, resp_datalen); 2262 2263 /* Any other field in resp to be set? */ 2264 iscsit_pdu_tx(resp); 2265 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2266 } 2267 2268 static void 2269 iscsit_pdu_op_login_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu) 2270 { 2271 2272 /* 2273 * Submit PDU to login state machine. State machine will free the 2274 * PDU. 2275 */ 2276 iscsit_login_sm_event(ict, ILE_LOGIN_RCV, rx_pdu); 2277 } 2278 2279 void 2280 iscsit_pdu_op_logout_cmd(iscsit_conn_t *ict, idm_pdu_t *rx_pdu) 2281 { 2282 iscsi_logout_hdr_t *logout_req = 2283 (iscsi_logout_hdr_t *)rx_pdu->isp_hdr; 2284 iscsi_logout_rsp_hdr_t *logout_rsp; 2285 idm_pdu_t *resp; 2286 2287 /* Allocate a PDU to respond */ 2288 resp = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0); 2289 idm_pdu_init(resp, ict->ict_ic, NULL, NULL); 2290 2291 /* 2292 * Logout results in the immediate termination of all tasks except 2293 * if the logout reason is ISCSI_LOGOUT_REASON_RECOVERY. The 2294 * connection state machine will drive this task cleanup automatically 2295 * so we don't need to handle that here. 2296 */ 2297 logout_rsp = (iscsi_logout_rsp_hdr_t *)resp->isp_hdr; 2298 bzero(logout_rsp, sizeof (*logout_rsp)); 2299 logout_rsp->opcode = ISCSI_OP_LOGOUT_RSP; 2300 logout_rsp->flags = ISCSI_FLAG_FINAL; 2301 logout_rsp->itt = logout_req->itt; 2302 if ((logout_req->flags & ISCSI_FLAG_LOGOUT_REASON_MASK) > 2303 ISCSI_LOGOUT_REASON_RECOVERY) { 2304 logout_rsp->response = ISCSI_LOGOUT_RECOVERY_UNSUPPORTED; 2305 } else { 2306 logout_rsp->response = ISCSI_LOGOUT_SUCCESS; 2307 } 2308 2309 iscsit_pdu_tx(resp); 2310 idm_pdu_complete(rx_pdu, IDM_STATUS_SUCCESS); 2311 } 2312 2313 /* 2314 * Calculate the number of outstanding commands we can process 2315 */ 2316 int 2317 iscsit_cmd_window() 2318 { 2319 /* Will be better later */ 2320 return (1024); 2321 } 2322 2323 /* 2324 * Set local registers based on incoming PDU 2325 */ 2326 void 2327 iscsit_set_cmdsn(iscsit_conn_t *ict, idm_pdu_t *rx_pdu) 2328 { 2329 iscsit_sess_t *ist; 2330 iscsi_scsi_cmd_hdr_t *req; 2331 2332 ist = ict->ict_sess; 2333 2334 req = (iscsi_scsi_cmd_hdr_t *)rx_pdu->isp_hdr; 2335 2336 rw_enter(&ist->ist_sn_rwlock, RW_WRITER); 2337 ist->ist_expcmdsn = ntohl(req->cmdsn) + 1; 2338 ist->ist_maxcmdsn = ntohl(req->cmdsn) + iscsit_cmd_window(); 2339 rw_exit(&ist->ist_sn_rwlock); 2340 } 2341 2342 /* 2343 * Update local StatSN and set SNs in response 2344 */ 2345 static void 2346 iscsit_calc_rspsn(iscsit_conn_t *ict, idm_pdu_t *resp) 2347 { 2348 iscsit_sess_t *ist; 2349 iscsi_scsi_rsp_hdr_t *rsp; 2350 2351 /* Get iSCSI session handle */ 2352 ist = ict->ict_sess; 2353 2354 rsp = (iscsi_scsi_rsp_hdr_t *)resp->isp_hdr; 2355 2356 /* Update StatSN */ 2357 rsp->statsn = htonl(ict->ict_statsn); 2358 switch (IDM_PDU_OPCODE(resp)) { 2359 case ISCSI_OP_RTT_RSP: 2360 /* Do nothing */ 2361 break; 2362 case ISCSI_OP_NOOP_IN: 2363 /* 2364 * Refer to section 10.19.1, RFC3720. 2365 * Advance only if target is responding initiator 2366 */ 2367 if (((iscsi_nop_in_hdr_t *)rsp)->ttt == ISCSI_RSVD_TASK_TAG) 2368 ict->ict_statsn++; 2369 break; 2370 case ISCSI_OP_SCSI_DATA_RSP: 2371 if (rsp->flags & ISCSI_FLAG_DATA_STATUS) 2372 ict->ict_statsn++; 2373 else 2374 rsp->statsn = 0; 2375 break; 2376 default: 2377 ict->ict_statsn++; 2378 break; 2379 } 2380 2381 /* Set ExpCmdSN and MaxCmdSN */ 2382 rsp->maxcmdsn = htonl(ist->ist_maxcmdsn); 2383 rsp->expcmdsn = htonl(ist->ist_expcmdsn); 2384 } 2385 2386 /* 2387 * Wrapper funtion, calls iscsi_calc_rspsn and idm_pdu_tx 2388 */ 2389 void 2390 iscsit_pdu_tx(idm_pdu_t *pdu) 2391 { 2392 iscsit_conn_t *ict = pdu->isp_ic->ic_handle; 2393 2394 /* 2395 * Protect ict->ict_statsn, ist->ist_maxcmdsn, and ist->ist_expcmdsn 2396 * (which are used by iscsit_calc_rspsn) with the session mutex 2397 * (ist->ist_sn_mutex). 2398 */ 2399 rw_enter(&ict->ict_sess->ist_sn_rwlock, RW_WRITER); 2400 iscsit_calc_rspsn(ict, pdu); 2401 idm_pdu_tx(pdu); 2402 rw_exit(&ict->ict_sess->ist_sn_rwlock); 2403 } 2404 2405 /* 2406 * Internal functions 2407 */ 2408 2409 void 2410 iscsit_send_async_event(iscsit_conn_t *ict, uint8_t event) 2411 { 2412 idm_pdu_t *abt; 2413 iscsi_async_evt_hdr_t *async_abt; 2414 2415 /* 2416 * Get a PDU to build the abort request. 2417 */ 2418 abt = idm_pdu_alloc(sizeof (iscsi_hdr_t), 0); 2419 if (abt == NULL) { 2420 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL); 2421 return; 2422 } 2423 2424 idm_pdu_init(abt, ict->ict_ic, NULL, NULL); 2425 abt->isp_datalen = 0; 2426 2427 async_abt = (iscsi_async_evt_hdr_t *)abt->isp_hdr; 2428 bzero(async_abt, sizeof (*async_abt)); 2429 async_abt->opcode = ISCSI_OP_ASYNC_EVENT; 2430 async_abt->async_event = event; 2431 async_abt->flags = ISCSI_FLAG_FINAL; 2432 async_abt->rsvd4[0] = 0xff; 2433 async_abt->rsvd4[1] = 0xff; 2434 async_abt->rsvd4[2] = 0xff; 2435 async_abt->rsvd4[3] = 0xff; 2436 2437 switch (event) { 2438 case ISCSI_ASYNC_EVENT_REQUEST_LOGOUT: 2439 async_abt->param3 = htons(IDM_LOGOUT_SECONDS); 2440 break; 2441 case ISCSI_ASYNC_EVENT_SCSI_EVENT: 2442 case ISCSI_ASYNC_EVENT_DROPPING_CONNECTION: 2443 case ISCSI_ASYNC_EVENT_DROPPING_ALL_CONNECTIONS: 2444 case ISCSI_ASYNC_EVENT_PARAM_NEGOTIATION: 2445 default: 2446 ASSERT(0); 2447 } 2448 2449 iscsit_pdu_tx(abt); 2450 } 2451 2452 void 2453 iscsit_send_reject(iscsit_conn_t *ict, idm_pdu_t *rejected_pdu, uint8_t reason) 2454 { 2455 idm_pdu_t *reject_pdu; 2456 iscsi_reject_rsp_hdr_t *reject; 2457 2458 /* 2459 * Get a PDU to build the abort request. 2460 */ 2461 reject_pdu = idm_pdu_alloc(sizeof (iscsi_hdr_t), 2462 rejected_pdu->isp_hdrlen); 2463 if (reject_pdu == NULL) { 2464 idm_conn_event(ict->ict_ic, CE_TRANSPORT_FAIL, NULL); 2465 return; 2466 } 2467 idm_pdu_init(reject_pdu, ict->ict_ic, NULL, NULL); 2468 2469 reject_pdu->isp_datalen = rejected_pdu->isp_hdrlen; 2470 bcopy(rejected_pdu->isp_hdr, reject_pdu->isp_data, 2471 rejected_pdu->isp_hdrlen); 2472 2473 reject = (iscsi_reject_rsp_hdr_t *)reject_pdu->isp_hdr; 2474 bzero(reject, sizeof (*reject)); 2475 reject->opcode = ISCSI_OP_REJECT_MSG; 2476 reject->reason = reason; 2477 reject->flags = ISCSI_FLAG_FINAL; 2478 hton24(reject->dlength, rejected_pdu->isp_hdrlen); 2479 reject->must_be_ff[0] = 0xff; 2480 reject->must_be_ff[1] = 0xff; 2481 reject->must_be_ff[2] = 0xff; 2482 reject->must_be_ff[3] = 0xff; 2483 2484 iscsit_pdu_tx(reject_pdu); 2485 } 2486 2487 2488 static iscsit_task_t * 2489 iscsit_task_alloc(iscsit_conn_t *ict) 2490 { 2491 iscsit_task_t *itask; 2492 iscsit_buf_t *immed_ibuf; 2493 2494 /* 2495 * Possible items to pre-alloc if we cache iscsit_task_t's: 2496 * 2497 * Status PDU w/ sense buffer 2498 * stmf_data_buf_t for immediate data 2499 */ 2500 itask = kmem_alloc(sizeof (iscsit_task_t) + sizeof (iscsit_buf_t) + 2501 sizeof (stmf_data_buf_t), KM_NOSLEEP); 2502 if (itask != NULL) { 2503 mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL); 2504 itask->it_aborted = itask->it_stmf_abort = 2505 itask->it_tm_task = 0; 2506 2507 immed_ibuf = (iscsit_buf_t *)(itask + 1); 2508 bzero(immed_ibuf, sizeof (*immed_ibuf)); 2509 immed_ibuf->ibuf_is_immed = B_TRUE; 2510 immed_ibuf->ibuf_stmf_buf = (stmf_data_buf_t *)(immed_ibuf + 1); 2511 2512 bzero(immed_ibuf->ibuf_stmf_buf, sizeof (stmf_data_buf_t)); 2513 immed_ibuf->ibuf_stmf_buf->db_port_private = immed_ibuf; 2514 immed_ibuf->ibuf_stmf_buf->db_sglist_length = 1; 2515 immed_ibuf->ibuf_stmf_buf->db_flags = DB_DIRECTION_FROM_RPORT | 2516 DB_DONT_CACHE; 2517 itask->it_immed_data = immed_ibuf; 2518 itask->it_idm_task = idm_task_alloc(ict->ict_ic); 2519 if (itask->it_idm_task != NULL) { 2520 itask->it_idm_task->idt_private = itask; 2521 itask->it_ict = ict; 2522 itask->it_ttt = itask->it_idm_task->idt_tt; 2523 return (itask); 2524 } else { 2525 kmem_free(itask, sizeof (iscsit_task_t) + 2526 sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t)); 2527 } 2528 } 2529 2530 return (NULL); 2531 } 2532 2533 static void 2534 iscsit_task_free(iscsit_task_t *itask) 2535 { 2536 idm_task_free(itask->it_idm_task); 2537 mutex_destroy(&itask->it_mutex); 2538 kmem_free(itask, sizeof (iscsit_task_t) + 2539 sizeof (iscsit_buf_t) + sizeof (stmf_data_buf_t)); 2540 } 2541 2542 static iscsit_task_t * 2543 iscsit_tm_task_alloc(iscsit_conn_t *ict) 2544 { 2545 iscsit_task_t *itask; 2546 2547 itask = kmem_zalloc(sizeof (iscsit_task_t), KM_NOSLEEP); 2548 if (itask != NULL) { 2549 idm_conn_hold(ict->ict_ic); 2550 mutex_init(&itask->it_mutex, NULL, MUTEX_DRIVER, NULL); 2551 itask->it_aborted = itask->it_stmf_abort = 2552 itask->it_tm_responded = 0; 2553 itask->it_tm_pdu = NULL; 2554 itask->it_tm_task = 1; 2555 itask->it_ict = ict; 2556 } 2557 2558 return (itask); 2559 } 2560 2561 static void 2562 iscsit_tm_task_free(iscsit_task_t *itask) 2563 { 2564 /* 2565 * If we responded then the call to idm_pdu_complete will free the 2566 * PDU. Otherwise we got aborted before the TM function could 2567 * complete and we need to free the PDU explicitly. 2568 */ 2569 if (itask->it_tm_pdu != NULL && !itask->it_tm_responded) 2570 idm_pdu_free(itask->it_tm_pdu); 2571 idm_conn_rele(itask->it_ict->ict_ic); 2572 mutex_destroy(&itask->it_mutex); 2573 kmem_free(itask, sizeof (iscsit_task_t)); 2574 } 2575 2576 static idm_status_t 2577 iscsit_task_start(iscsit_task_t *itask) 2578 { 2579 iscsit_sess_t *ist = itask->it_ict->ict_sess; 2580 avl_index_t where; 2581 2582 /* 2583 * Sanity check the ITT and ensure that this task does not already 2584 * exist. If not then add the task to the session task list. 2585 */ 2586 mutex_enter(&ist->ist_mutex); 2587 mutex_enter(&itask->it_mutex); 2588 itask->it_active = 1; 2589 if (avl_find(&ist->ist_task_list, itask, &where) == NULL) { 2590 /* New task, add to AVL */ 2591 avl_insert(&ist->ist_task_list, itask, where); 2592 mutex_exit(&itask->it_mutex); 2593 mutex_exit(&ist->ist_mutex); 2594 return (IDM_STATUS_SUCCESS); 2595 } 2596 mutex_exit(&itask->it_mutex); 2597 mutex_exit(&ist->ist_mutex); 2598 2599 return (IDM_STATUS_REJECT); 2600 } 2601 2602 static void 2603 iscsit_task_done(iscsit_task_t *itask) 2604 { 2605 iscsit_sess_t *ist = itask->it_ict->ict_sess; 2606 2607 mutex_enter(&ist->ist_mutex); 2608 mutex_enter(&itask->it_mutex); 2609 if (itask->it_active) { 2610 avl_remove(&ist->ist_task_list, itask); 2611 itask->it_active = 0; 2612 } 2613 mutex_exit(&itask->it_mutex); 2614 mutex_exit(&ist->ist_mutex); 2615 } 2616 2617 /* 2618 * iscsit status PDU cache 2619 */ 2620 2621 /*ARGSUSED*/ 2622 static int 2623 iscsit_status_pdu_constructor(void *pdu_void, void *arg, int flags) 2624 { 2625 idm_pdu_t *pdu = pdu_void; 2626 iscsi_scsi_rsp_hdr_t *rsp; 2627 2628 bzero(pdu, sizeof (idm_pdu_t)); 2629 pdu->isp_callback = iscsit_send_good_status_done; 2630 pdu->isp_magic = IDM_PDU_MAGIC; 2631 pdu->isp_hdr = (iscsi_hdr_t *)(pdu + 1); /* Ptr arithmetic */ 2632 pdu->isp_hdrlen = sizeof (iscsi_hdr_t); 2633 2634 /* Setup status response */ 2635 rsp = (iscsi_scsi_rsp_hdr_t *)pdu->isp_hdr; 2636 bzero(rsp, sizeof (*rsp)); 2637 rsp->opcode = ISCSI_OP_SCSI_RSP; 2638 rsp->flags = ISCSI_FLAG_FINAL; 2639 rsp->response = ISCSI_STATUS_CMD_COMPLETED; 2640 2641 return (0); 2642 } 2643 2644 /* 2645 * iscsit private data handler 2646 */ 2647 2648 /*ARGSUSED*/ 2649 static void 2650 iscsit_pp_cb(struct stmf_port_provider *pp, int cmd, void *arg, uint32_t flags) 2651 { 2652 it_config_t *cfg; 2653 nvlist_t *nvl; 2654 2655 if ((cmd != STMF_PROVIDER_DATA_UPDATED) || (arg == NULL)) { 2656 return; 2657 } 2658 2659 nvl = (nvlist_t *)arg; 2660 2661 /* Translate nvlist */ 2662 if (it_nv_to_config(nvl, &cfg) != 0) { 2663 cmn_err(CE_WARN, "Configuration is invalid"); 2664 return; 2665 } 2666 2667 /* Update config */ 2668 (void) iscsit_config_merge(cfg); 2669 2670 it_config_free_cmn(cfg); 2671 } 2672 2673 2674 static it_cfg_status_t 2675 iscsit_config_merge(it_config_t *in_cfg) 2676 { 2677 it_cfg_status_t status; 2678 it_config_t *cfg; 2679 it_config_t tmp_cfg; 2680 list_t tpg_del_list; 2681 2682 if (in_cfg) { 2683 cfg = in_cfg; 2684 } else { 2685 /* Make empty config */ 2686 bzero(&tmp_cfg, sizeof (tmp_cfg)); 2687 cfg = &tmp_cfg; 2688 } 2689 2690 list_create(&tpg_del_list, sizeof (iscsit_tpg_t), 2691 offsetof(iscsit_tpg_t, tpg_delete_ln)); 2692 2693 /* 2694 * Update targets, initiator contexts, target portal groups, 2695 * and iSNS client 2696 */ 2697 ISCSIT_GLOBAL_LOCK(RW_WRITER); 2698 if (((status = iscsit_config_merge_tpg(cfg, &tpg_del_list)) 2699 != 0) || 2700 ((status = iscsit_config_merge_tgt(cfg)) != 0) || 2701 ((status = iscsit_config_merge_ini(cfg)) != 0) || 2702 ((status = isnst_config_merge(cfg)) != 0)) { 2703 ISCSIT_GLOBAL_UNLOCK(); 2704 return (status); 2705 } 2706 2707 /* Update other global config parameters */ 2708 if (iscsit_global.global_props) { 2709 nvlist_free(iscsit_global.global_props); 2710 iscsit_global.global_props = NULL; 2711 } 2712 if (in_cfg) { 2713 (void) nvlist_dup(cfg->config_global_properties, 2714 &iscsit_global.global_props, KM_SLEEP); 2715 } 2716 ISCSIT_GLOBAL_UNLOCK(); 2717 2718 iscsit_config_destroy_tpgs(&tpg_del_list); 2719 2720 list_destroy(&tpg_del_list); 2721 2722 return (ITCFG_SUCCESS); 2723 } 2724 2725 /* 2726 * iscsit_sna_lt[e] 2727 * 2728 * Compare serial numbers using serial number arithmetic as defined in 2729 * RFC 1982. 2730 * 2731 * NOTE: This code is duplicated in the isns server as well as iscsitgtd. It 2732 * ought to be common. 2733 */ 2734 2735 static int 2736 iscsit_sna_lt(uint32_t sn1, uint32_t sn2) 2737 { 2738 return ((sn1 != sn2) && 2739 (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) || 2740 ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK)))); 2741 } 2742 2743 static int 2744 iscsit_sna_lte(uint32_t sn1, uint32_t sn2) 2745 { 2746 return ((sn1 == sn2) || 2747 (((sn1 < sn2) && ((sn2 - sn1) < ISCSIT_SNA32_CHECK)) || 2748 ((sn1 > sn2) && ((sn1 - sn2) > ISCSIT_SNA32_CHECK)))); 2749 } 2750 2751 2752 static boolean_t 2753 iscsit_cmdsn_in_window(iscsit_conn_t *ict, uint32_t cmdsn) 2754 { 2755 iscsit_sess_t *ist = ict->ict_sess; 2756 int rval = B_TRUE; 2757 2758 ist = ict->ict_sess; 2759 2760 rw_enter(&ist->ist_sn_rwlock, RW_READER); 2761 2762 /* 2763 * If cmdsn is less than ist_expcmdsn - iscsit_cmd_window() or 2764 * greater than ist_expcmdsn, it's not in the window. 2765 */ 2766 2767 if (iscsit_sna_lt(cmdsn, (ist->ist_expcmdsn - iscsit_cmd_window())) || 2768 !iscsit_sna_lte(cmdsn, ist->ist_expcmdsn)) { 2769 rval = B_FALSE; 2770 } 2771 2772 rw_exit(&ist->ist_sn_rwlock); 2773 2774 return (rval); 2775 } 2776