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 2000 by Cisco Systems, Inc. All rights reserved. 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 * iSCSI Software Initiator 27 */ 28 29 /* 30 * Framework interface routines for iSCSI 31 */ 32 #include "iscsi.h" /* main header */ 33 #include <sys/scsi/adapters/iscsi_if.h> /* ioctl interfaces */ 34 #include <sys/scsi/adapters/iscsi_protocol.h> 35 /* protocol structs and defines */ 36 37 #include "iscsi_targetparam.h" 38 #include "persistent.h" 39 #include <sys/scsi/adapters/iscsi_door.h> 40 #include <sys/dlpi.h> 41 #include <sys/utsname.h> 42 #include "isns_client.h" 43 #include "isns_protocol.h" 44 #include <sys/bootprops.h> 45 46 #define ISCSI_NAME_VERSION "iSCSI Initiator v-1.55" 47 48 #define MAX_GET_NAME_SIZE 1024 49 #define MAX_NAME_PROP_SIZE 256 50 #define UNDEFINED -1 51 52 /* 53 * +--------------------------------------------------------------------+ 54 * | iscsi globals | 55 * +--------------------------------------------------------------------+ 56 */ 57 void *iscsi_state; 58 kmutex_t iscsi_oid_mutex; 59 uint32_t iscsi_oid; 60 int iscsi_nop_delay = ISCSI_DEFAULT_NOP_DELAY; 61 int iscsi_rx_window = ISCSI_DEFAULT_RX_WINDOW; 62 int iscsi_rx_max_window = ISCSI_DEFAULT_RX_MAX_WINDOW; 63 64 extern ib_boot_prop_t *iscsiboot_prop; 65 66 /* 67 * +--------------------------------------------------------------------+ 68 * | iscsi.c prototypes | 69 * +--------------------------------------------------------------------+ 70 */ 71 static int iscsi_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, 72 void *arg, void **result); 73 static int iscsi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 74 static int iscsi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 75 76 /* scsi_tran prototypes */ 77 static int iscsi_tran_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 78 scsi_hba_tran_t *hba_tran, struct scsi_device *sd); 79 static int iscsi_tran_lun_probe(struct scsi_device *sd, int (*callback) ()); 80 static struct scsi_pkt *iscsi_tran_init_pkt(struct scsi_address *ap, 81 struct scsi_pkt *pkt, struct buf *bp, int cmdlen, int statuslen, 82 int tgtlen, int flags, int (*callback) (), caddr_t arg); 83 static void iscsi_tran_lun_free(dev_info_t *hba_dip, dev_info_t *lun_dip, 84 scsi_hba_tran_t *hba_tran, struct scsi_device *sd); 85 static int iscsi_tran_start(struct scsi_address *ap, struct scsi_pkt *pkt); 86 static int iscsi_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt); 87 static int iscsi_tran_reset(struct scsi_address *ap, int level); 88 static int iscsi_tran_getcap(struct scsi_address *ap, char *cap, int whom); 89 static int iscsi_tran_setcap(struct scsi_address *ap, char *cap, 90 int value, int whom); 91 static void iscsi_tran_destroy_pkt(struct scsi_address *ap, 92 struct scsi_pkt *pkt); 93 static void iscsi_tran_dmafree(struct scsi_address *ap, 94 struct scsi_pkt *pkt); 95 static void iscsi_tran_sync_pkt(struct scsi_address *ap, 96 struct scsi_pkt *pkt); 97 static void iscsi_tran_sync_pkt(struct scsi_address *ap, 98 struct scsi_pkt *pkt); 99 static int iscsi_tran_reset_notify(struct scsi_address *ap, int flag, 100 void (*callback) (caddr_t), caddr_t arg); 101 static int iscsi_tran_bus_config(dev_info_t *parent, uint_t flags, 102 ddi_bus_config_op_t op, void *arg, dev_info_t **childp); 103 static int iscsi_tran_bus_unconfig(dev_info_t *parent, uint_t flags, 104 ddi_bus_config_op_t op, void *arg); 105 static int iscsi_tran_get_name(struct scsi_device *sd, char *name, int len); 106 static int iscsi_tran_get_bus_addr(struct scsi_device *sd, char *name, int len); 107 108 /* bus_ops prototypes */ 109 /* LINTED E_STATIC_UNUSED */ 110 static ddi_intrspec_t iscsi_get_intrspec(dev_info_t *dip, dev_info_t *rdip, 111 uint_t inumber); 112 /* LINTED E_STATIC_UNUSED */ 113 static int iscsi_add_intrspec(dev_info_t *dip, dev_info_t *rdip, 114 ddi_intrspec_t intrspec, ddi_iblock_cookie_t *iblock_cookiep, 115 ddi_idevice_cookie_t *idevice_cookiep, uint_t (*int_handler)(caddr_t 116 int_handler_arg), caddr_t int_handler_arg, int kind); 117 /* LINTED E_STATIC_UNUSED */ 118 static void iscsi_remove_intrspec(dev_info_t *dip, dev_info_t *rdip, 119 ddi_intrspec_t intrspec, ddi_iblock_cookie_t iblock_cookie); 120 /* LINTED E_STATIC_UNUSED */ 121 static int iscsi_ctl(dev_info_t *dip, dev_info_t *rdip, ddi_ctl_enum_t ctlop, 122 void *arg, void *result); 123 124 /* cb_ops prototypes */ 125 static int iscsi_open(dev_t *devp, int flags, int otyp, cred_t *credp); 126 static int iscsi_close(dev_t dev, int flag, int otyp, cred_t *credp); 127 /* --- iscsi_ioctl is called by the discovery code so needs to be global --- */ 128 int iscsi_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 129 cred_t *credp, int *rvalp); 130 131 int iscsi_get_persisted_param(uchar_t *name, 132 iscsi_param_get_t *ipgp, 133 iscsi_login_params_t *params); 134 static void iscsi_override_target_default(iscsi_hba_t *ihp, 135 iscsi_param_get_t *ipg); 136 137 /* scsi_tran helpers */ 138 static int iscsi_virt_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 139 scsi_hba_tran_t *hba_tran, struct scsi_device *sd); 140 static int iscsi_phys_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 141 scsi_hba_tran_t *hba_tran, struct scsi_device *sd); 142 static int iscsi_i_commoncap(struct scsi_address *ap, char *cap, 143 int val, int lunonly, int doset); 144 static void iscsi_get_name_to_iqn(char *name, int name_max_len); 145 static void iscsi_get_name_from_iqn(char *name, int name_max_len); 146 static boolean_t iscsi_cmp_boot_sess_oid(iscsi_hba_t *ihp, uint32_t oid); 147 148 /* struct helpers prototypes */ 149 150 /* 151 * At this point this driver doesn't need this structure because nothing 152 * is done during the open, close or ioctl. Code put in place because 153 * some admin related work might be done in the ioctl routine. 154 */ 155 static struct cb_ops iscsi_cb_ops = { 156 iscsi_open, /* open */ 157 iscsi_close, /* close */ 158 nodev, /* strategy */ 159 nodev, /* print */ 160 nodev, /* dump */ 161 nodev, /* read */ 162 nodev, /* write */ 163 iscsi_ioctl, /* ioctl */ 164 nodev, /* devmap */ 165 nodev, /* mmap */ 166 nodev, /* segmap */ 167 nochpoll, /* poll */ 168 ddi_prop_op, /* prop_op */ 169 NULL, /* streamtab */ 170 D_NEW | D_MP | D_HOTPLUG, /* flags */ 171 CB_REV, /* cb_rev */ 172 nodev, /* aread */ 173 nodev, /* awrite */ 174 }; 175 176 static struct dev_ops iscsi_dev_ops = { 177 DEVO_REV, /* devo_rev */ 178 0, /* refcnt */ 179 iscsi_getinfo, /* getinfo */ 180 nulldev, /* identify */ 181 nulldev, /* probe */ 182 iscsi_attach, /* attach */ 183 iscsi_detach, /* detach */ 184 nodev, /* reset */ 185 &iscsi_cb_ops, /* driver operations */ 186 NULL, /* bus ops */ 187 NULL, /* power management */ 188 }; 189 190 static struct modldrv modldrv = { 191 &mod_driverops, /* drv_modops */ 192 ISCSI_NAME_VERSION, /* drv_linkinfo */ 193 &iscsi_dev_ops /* drv_dev_ops */ 194 }; 195 196 static struct modlinkage modlinkage = { 197 MODREV_1, /* ml_rev */ 198 &modldrv, /* ml_linkage[] */ 199 NULL /* NULL termination */ 200 }; 201 202 /* 203 * This structure is bogus. scsi_hba_attach_setup() requires, as in the kernel 204 * will panic if you don't pass this in to the routine, this information. 205 * Need to determine what the actual impact to the system is by providing 206 * this information if any. Since dma allocation is done in pkt_init it may 207 * not have any impact. These values are straight from the Writing Device 208 * Driver manual. 209 */ 210 static ddi_dma_attr_t iscsi_dma_attr = { 211 DMA_ATTR_V0, /* ddi_dma_attr version */ 212 0, /* low address */ 213 0xffffffff, /* high address */ 214 0x00ffffff, /* counter upper bound */ 215 1, /* alignment requirements */ 216 0x3f, /* burst sizes */ 217 1, /* minimum DMA access */ 218 0xffffffff, /* maximum DMA access */ 219 (1 << 24) - 1, /* segment boundary restrictions */ 220 1, /* scater/gather list length */ 221 512, /* device granularity */ 222 0 /* DMA flags */ 223 }; 224 225 /* 226 * _init - General driver init entry 227 */ 228 int 229 _init(void) 230 { 231 int rval = 0; 232 233 iscsi_net_init(); 234 235 mutex_init(&iscsi_oid_mutex, NULL, MUTEX_DRIVER, NULL); 236 iscsi_oid = ISCSI_INITIATOR_OID; 237 238 /* 239 * Set up the soft state structures. If this driver is actually 240 * being attached to the system then we'll have at least one 241 * HBA/NIC used. 242 */ 243 rval = ddi_soft_state_init(&iscsi_state, 244 sizeof (iscsi_hba_t), 1); 245 if (rval != 0) { 246 iscsi_net_fini(); 247 goto init_done; 248 } 249 250 rval = scsi_hba_init(&modlinkage); 251 if (rval != 0) { 252 ddi_soft_state_fini(&iscsi_state); 253 iscsi_net_fini(); 254 goto init_done; 255 } 256 257 rval = mod_install(&modlinkage); 258 if (rval != 0) { 259 ddi_soft_state_fini(&iscsi_state); 260 scsi_hba_fini(&modlinkage); 261 iscsi_net_fini(); 262 goto init_done; 263 } 264 (void) iscsi_door_ini(); 265 266 init_done: 267 return (rval); 268 } 269 270 /* 271 * _fini - General driver destructor entry 272 */ 273 int 274 _fini(void) 275 { 276 int rval = 0; 277 278 rval = mod_remove(&modlinkage); 279 if (rval == 0) { 280 scsi_hba_fini(&modlinkage); 281 ddi_soft_state_fini(&iscsi_state); 282 mutex_destroy(&iscsi_oid_mutex); 283 (void) iscsi_door_term(); 284 iscsi_net_fini(); 285 } 286 return (rval); 287 } 288 289 /* 290 * _info - General driver info entry 291 */ 292 int 293 _info(struct modinfo *mp) 294 { 295 int rval = 0; 296 297 rval = mod_info(&modlinkage, mp); 298 299 return (rval); 300 } 301 302 303 /* 304 * +--------------------------------------------------------------------+ 305 * | Start of dev_ops routines | 306 * +--------------------------------------------------------------------+ 307 */ 308 309 /* 310 * iscsi_getinfo - returns general driver information 311 */ 312 /* ARGSUSED */ 313 static int 314 iscsi_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, 315 void *arg, void **result) 316 { 317 int rval = DDI_SUCCESS; 318 int instance = getminor((dev_t)arg); 319 iscsi_hba_t *ip; 320 321 switch (infocmd) { 322 case DDI_INFO_DEVT2DEVINFO: 323 if ((ip = ddi_get_soft_state(iscsi_state, instance)) == NULL) { 324 return (DDI_FAILURE); 325 } 326 *result = ip->hba_dip; 327 if (ip->hba_dip == NULL) 328 rval = DDI_FAILURE; 329 else 330 rval = DDI_SUCCESS; 331 break; 332 333 case DDI_INFO_DEVT2INSTANCE: 334 *result = (void *)(uintptr_t)instance; 335 rval = DDI_SUCCESS; 336 break; 337 338 default: 339 rval = DDI_FAILURE; 340 break; 341 } 342 return (rval); 343 } 344 345 346 /* 347 * iscsi_attach -- Attach instance of an iSCSI HBA. We 348 * will attempt to create our HBA and register it with 349 * scsi_vhci. If it's not possible to create the HBA 350 * or register with vhci we will fail the attach. 351 */ 352 static int 353 iscsi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 354 { 355 int rval = DDI_SUCCESS; 356 int instance = ddi_get_instance(dip); 357 iscsi_hba_t *ihp = NULL; 358 scsi_hba_tran_t *tran = NULL; 359 char init_port_name[MAX_NAME_PROP_SIZE]; 360 361 switch (cmd) { 362 case DDI_ATTACH: 363 /* create iSCSH HBA devctl device node */ 364 if (ddi_create_minor_node(dip, ISCSI_DEVCTL, S_IFCHR, 0, 365 DDI_PSEUDO, 0) == DDI_SUCCESS) { 366 367 /* allocate HBA soft state */ 368 if (ddi_soft_state_zalloc(iscsi_state, instance) != 369 DDI_SUCCESS) { 370 ddi_remove_minor_node(dip, NULL); 371 rval = DDI_FAILURE; 372 break; 373 } 374 375 /* get reference to soft state */ 376 if ((ihp = (iscsi_hba_t *)ddi_get_soft_state( 377 iscsi_state, instance)) == NULL) { 378 ddi_remove_minor_node(dip, NULL); 379 ddi_soft_state_free(iscsi_state, instance); 380 rval = DDI_FAILURE; 381 break; 382 } 383 384 /* init HBA mutex used to protect discovery events */ 385 mutex_init(&ihp->hba_discovery_events_mutex, NULL, 386 MUTEX_DRIVER, NULL); 387 388 /* 389 * init SendTargets semaphore that is used to allow 390 * only one operation at a time 391 */ 392 sema_init(&ihp->hba_sendtgts_semaphore, 1, NULL, 393 SEMA_DRIVER, NULL); 394 395 ihp->hba_sess_list = NULL; 396 rw_init(&ihp->hba_sess_list_rwlock, NULL, 397 RW_DRIVER, NULL); 398 399 /* allocate scsi_hba_tran */ 400 if ((tran = scsi_hba_tran_alloc(dip, SCSI_HBA_CANSLEEP)) 401 == NULL) { 402 ddi_remove_minor_node(dip, NULL); 403 goto iscsi_attach_failed2; 404 } 405 406 /* soft state setup */ 407 ihp->hba_sig = ISCSI_SIG_HBA; 408 ihp->hba_tran = tran; 409 ihp->hba_dip = dip; 410 411 mutex_enter(&iscsi_oid_mutex); 412 ihp->hba_oid = iscsi_oid++; 413 mutex_exit(&iscsi_oid_mutex); 414 415 ihp->hba_name[0] = '\0'; 416 ihp->hba_name_length = 0; 417 ihp->hba_alias_length = 0; 418 ihp->hba_alias[0] = '\0'; 419 420 iscsi_net->tweaks.rcvbuf = ddi_prop_get_int( 421 DDI_DEV_T_ANY, ihp->hba_dip, 0, "so-rcvbuf", 422 ISCSI_SOCKET_RCVBUF_SIZE); 423 424 iscsi_net->tweaks.sndbuf = ddi_prop_get_int( 425 DDI_DEV_T_ANY, ihp->hba_dip, 0, "so-sndbuf", 426 ISCSI_SOCKET_SNDBUF_SIZE); 427 428 iscsi_net->tweaks.nodelay = ddi_prop_get_int( 429 DDI_DEV_T_ANY, ihp->hba_dip, 0, "tcp-nodelay", 430 ISCSI_TCP_NODELAY_DEFAULT); 431 432 iscsi_net->tweaks.conn_notify_threshold = 433 ddi_prop_get_int(DDI_DEV_T_ANY, 434 ihp->hba_dip, 0, "tcp-conn-notify-threshold", 435 ISCSI_TCP_CNOTIFY_THRESHOLD_DEFAULT); 436 437 iscsi_net->tweaks.conn_abort_threshold = 438 ddi_prop_get_int(DDI_DEV_T_ANY, ihp->hba_dip, 439 0, "tcp-conn-abort-threshold", 440 ISCSI_TCP_CABORT_THRESHOLD_DEFAULT); 441 442 iscsi_net->tweaks.abort_threshold = ddi_prop_get_int( 443 DDI_DEV_T_ANY, ihp->hba_dip, 0, 444 "tcp-abort-threshold", 445 ISCSI_TCP_ABORT_THRESHOLD_DEFAULT); 446 447 ihp->hba_config_storm_delay = ddi_prop_get_int( 448 DDI_DEV_T_ANY, ihp->hba_dip, 0, 449 "config-storm-delay", 450 ISCSI_CONFIG_STORM_DELAY_DEFAULT); 451 452 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 453 "so-rcvbuf", iscsi_net->tweaks.rcvbuf); 454 455 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 456 "so-sndbuf", iscsi_net->tweaks.sndbuf); 457 458 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 459 "tcp-nodelay", iscsi_net->tweaks.nodelay); 460 461 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 462 "tcp-conn-notify-threshold", 463 iscsi_net->tweaks.conn_notify_threshold); 464 465 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 466 "tcp-conn-abort-threshold", 467 iscsi_net->tweaks.conn_abort_threshold); 468 469 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 470 "tcp-abort-threshold", 471 iscsi_net->tweaks.abort_threshold); 472 473 (void) ddi_prop_update_int(DDI_DEV_T_NONE, ihp->hba_dip, 474 "config-storm-delay", 475 ihp->hba_config_storm_delay); 476 477 /* setup hba defaults */ 478 iscsi_set_default_login_params(&ihp->hba_params); 479 480 /* hba set up */ 481 tran->tran_hba_private = ihp; 482 tran->tran_tgt_private = NULL; 483 tran->tran_tgt_init = iscsi_tran_lun_init; 484 tran->tran_tgt_probe = iscsi_tran_lun_probe; 485 tran->tran_tgt_free = iscsi_tran_lun_free; 486 tran->tran_start = iscsi_tran_start; 487 tran->tran_abort = iscsi_tran_abort; 488 tran->tran_reset = iscsi_tran_reset; 489 tran->tran_getcap = iscsi_tran_getcap; 490 tran->tran_setcap = iscsi_tran_setcap; 491 tran->tran_init_pkt = iscsi_tran_init_pkt; 492 tran->tran_destroy_pkt = iscsi_tran_destroy_pkt; 493 tran->tran_dmafree = iscsi_tran_dmafree; 494 tran->tran_sync_pkt = iscsi_tran_sync_pkt; 495 tran->tran_reset_notify = iscsi_tran_reset_notify; 496 tran->tran_bus_config = iscsi_tran_bus_config; 497 tran->tran_bus_unconfig = iscsi_tran_bus_unconfig; 498 499 tran->tran_get_name = iscsi_tran_get_name; 500 tran->tran_get_bus_addr = iscsi_tran_get_bus_addr; 501 tran->tran_interconnect_type = INTERCONNECT_ISCSI; 502 503 /* register scsi hba with scsa */ 504 if (scsi_hba_attach_setup(dip, &iscsi_dma_attr, 505 tran, SCSI_HBA_TRAN_CLONE) != DDI_SUCCESS) { 506 goto iscsi_attach_failed1; 507 } 508 509 /* register scsi hba with mdi (MPxIO/vhci) */ 510 if (mdi_phci_register(MDI_HCI_CLASS_SCSI, dip, 0) != 511 MDI_SUCCESS) { 512 ihp->hba_mpxio_enabled = B_FALSE; 513 } else { 514 ihp->hba_mpxio_enabled = B_TRUE; 515 } 516 517 (void) iscsi_hba_kstat_init(ihp); 518 519 /* Initialize targetparam list */ 520 iscsi_targetparam_init(); 521 522 /* Initialize ISID */ 523 ihp->hba_isid[0] = ISCSI_SUN_ISID_0; 524 ihp->hba_isid[1] = ISCSI_SUN_ISID_1; 525 ihp->hba_isid[2] = ISCSI_SUN_ISID_2; 526 ihp->hba_isid[3] = ISCSI_SUN_ISID_3; 527 ihp->hba_isid[4] = ISCSI_SUN_ISID_4; 528 ihp->hba_isid[5] = ISCSI_SUN_ISID_5; 529 530 /* Setup iSNS transport services and client */ 531 isns_client_init(); 532 533 /* 534 * initialize the discovery processes and 535 * persistent store. 536 */ 537 ihp->persistent_loaded = B_FALSE; 538 if (iscsid_init(ihp, B_FALSE) == B_FALSE) { 539 goto iscsi_attach_failed0; 540 } 541 542 /* Setup init_port_name for MPAPI */ 543 (void) snprintf(init_port_name, MAX_NAME_PROP_SIZE, 544 "%s,%02x%02x%02x%02x%02x%02x", 545 (char *)ihp->hba_name, ihp->hba_isid[0], 546 ihp->hba_isid[1], ihp->hba_isid[2], 547 ihp->hba_isid[3], ihp->hba_isid[4], 548 ihp->hba_isid[5]); 549 550 if (ddi_prop_update_string(DDI_DEV_T_NONE, dip, 551 "initiator-port", init_port_name) != 552 DDI_PROP_SUCCESS) { 553 cmn_err(CE_WARN, "iscsi_attach: Creating " 554 "initiator-port property on iSCSI " 555 "HBA(%s) with dip(%d) Failed", 556 (char *)ihp->hba_name, 557 ddi_get_instance(dip)); 558 } 559 560 ddi_report_dev(dip); 561 } else { 562 rval = DDI_FAILURE; 563 } 564 break; 565 566 iscsi_attach_failed0: 567 isns_client_cleanup(); 568 if (ihp->stats.ks) { 569 (void) iscsi_hba_kstat_term(ihp); 570 } 571 if (ihp->hba_mpxio_enabled == B_TRUE) { 572 (void) mdi_phci_unregister(dip, 0); 573 } 574 (void) scsi_hba_detach(ihp->hba_dip); 575 iscsi_attach_failed1: 576 ddi_remove_minor_node(dip, NULL); 577 ddi_prop_remove_all(ihp->hba_dip); 578 scsi_hba_tran_free(tran); 579 iscsi_attach_failed2: 580 mutex_destroy(&ihp->hba_discovery_events_mutex); 581 sema_destroy(&ihp->hba_sendtgts_semaphore); 582 rw_destroy(&ihp->hba_sess_list_rwlock); 583 ddi_soft_state_free(iscsi_state, instance); 584 rval = DDI_FAILURE; 585 break; 586 587 case DDI_RESUME: 588 break; 589 590 default: 591 rval = DDI_FAILURE; 592 } 593 594 if (rval != DDI_SUCCESS) { 595 cmn_err(CE_WARN, "iscsi driver unable to attach " 596 "hba instance %d", instance); 597 } 598 599 return (rval); 600 } 601 602 /* 603 * iscsi_detach - called on unload of hba instance 604 */ 605 static int 606 iscsi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 607 { 608 int rval = DDI_SUCCESS; 609 scsi_hba_tran_t *tran = NULL; 610 iscsi_hba_t *ihp = NULL; 611 iscsi_hba_t *ihp_check = NULL; 612 int instance; 613 char *init_node_name; 614 615 instance = ddi_get_instance(dip); 616 617 switch (cmd) { 618 case DDI_DETACH: 619 if (!(tran = (scsi_hba_tran_t *)ddi_get_driver_private(dip))) { 620 rval = DDI_SUCCESS; 621 break; 622 } 623 624 if ((ihp = (iscsi_hba_t *)tran->tran_hba_private) == NULL) { 625 rval = DDI_FAILURE; 626 break; 627 } 628 629 /* 630 * Validate that what is stored by the DDI framework is still 631 * the same state structure referenced by the SCSI framework 632 */ 633 ihp_check = ddi_get_soft_state(iscsi_state, instance); 634 if (ihp_check != ihp) { 635 rval = DDI_FAILURE; 636 break; 637 } 638 639 /* If a session exists we can't safely detach */ 640 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 641 if (ihp->hba_sess_list != NULL) { 642 rw_exit(&ihp->hba_sess_list_rwlock); 643 rval = DDI_FAILURE; 644 break; 645 } 646 rw_exit(&ihp->hba_sess_list_rwlock); 647 648 /* Disable all discovery services */ 649 if (iscsid_disable_discovery(ihp, 650 ISCSI_ALL_DISCOVERY_METHODS) == B_FALSE) { 651 /* Disable failed. Fail detach */ 652 rval = DDI_FAILURE; 653 break; 654 } 655 656 /* Deregister from iSNS server(s). */ 657 init_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 658 if (persistent_initiator_name_get(init_node_name, 659 ISCSI_MAX_NAME_LEN) == B_TRUE) { 660 if (strlen(init_node_name) > 0) { 661 (void) isns_dereg(ihp->hba_isid, 662 (uint8_t *)init_node_name); 663 } 664 } 665 kmem_free(init_node_name, ISCSI_MAX_NAME_LEN); 666 init_node_name = NULL; 667 668 /* Cleanup iSNS Client */ 669 isns_client_cleanup(); 670 671 iscsi_targetparam_cleanup(); 672 673 /* Cleanup iscsid resources */ 674 iscsid_fini(); 675 676 if (rval != DDI_SUCCESS) { 677 break; 678 } 679 /* kstat hba. destroy */ 680 KSTAT_DEC_HBA_CNTR_SESS(ihp); 681 682 if (ihp->hba_mpxio_enabled == B_TRUE) { 683 (void) mdi_phci_unregister(dip, 0); 684 } 685 ddi_remove_minor_node(dip, NULL); 686 687 ddi_prop_remove_all(ihp->hba_dip); 688 mutex_destroy(&ihp->hba_discovery_events_mutex); 689 rw_destroy(&ihp->hba_sess_list_rwlock); 690 (void) iscsi_hba_kstat_term(ihp); 691 692 (void) scsi_hba_detach(dip); 693 if (tran != NULL) { 694 scsi_hba_tran_free(tran); 695 } 696 ddi_soft_state_free(iscsi_state, instance); 697 break; 698 default: 699 break; 700 } 701 702 if (rval != DDI_SUCCESS) { 703 cmn_err(CE_WARN, "iscsi driver unable to " 704 "detach hba instance %d", instance); 705 } 706 707 return (rval); 708 } 709 710 /* 711 * +--------------------------------------------------------------------+ 712 * | End of dev_ops routines | 713 * +--------------------------------------------------------------------+ 714 */ 715 716 /* 717 * +--------------------------------------------------------------------+ 718 * | scsi_tran(9E) routines | 719 * +--------------------------------------------------------------------+ 720 */ 721 722 /* 723 * iscsi_tran_lun_init - Find target device based on SCSI device 724 * Based on the information given (SCSI device, target dev_info) find 725 * the target iSCSI device and put a pointer to that information in 726 * the scsi_hba_tran_t structure. 727 */ 728 static int 729 iscsi_tran_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 730 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 731 { 732 int rval = 0; 733 int type = 0; 734 735 ASSERT(hba_tran->tran_hba_private != NULL); 736 737 /* 738 * Child node is getting initialized. Look at the mpxio component 739 * type on the child device to see if this device is mpxio managed 740 * or not. 741 */ 742 type = mdi_get_component_type(lun_dip); 743 if (type != MDI_COMPONENT_CLIENT) { 744 rval = iscsi_phys_lun_init(hba_dip, lun_dip, hba_tran, sd); 745 } else { 746 rval = iscsi_virt_lun_init(hba_dip, lun_dip, hba_tran, sd); 747 } 748 749 return (rval); 750 } 751 752 /* 753 * iscsi_tran_lun_probe - This function didn't need to be implemented. 754 * We could have left NULL in the tran table. Since this isn't a 755 * performance path this seems safe. We are just wrappering the 756 * function so we can see the call go through if we have debugging 757 * enabled. 758 */ 759 static int 760 iscsi_tran_lun_probe(struct scsi_device *sd, int (*callback) ()) 761 { 762 int rval = 0; 763 764 rval = scsi_hba_probe(sd, callback); 765 766 return (rval); 767 } 768 769 /* 770 * iscsi_init_pkt - Allocate SCSI packet and fill in required info. 771 */ 772 /* ARGSUSED */ 773 static struct scsi_pkt * 774 iscsi_tran_init_pkt(struct scsi_address *ap, struct scsi_pkt *pkt, 775 struct buf *bp, int cmdlen, int statuslen, int tgtlen, int flags, 776 int (*callback) (), caddr_t arg) 777 { 778 iscsi_lun_t *ilp; 779 iscsi_cmd_t *icmdp; 780 781 ASSERT(ap != NULL); 782 ASSERT(callback == NULL_FUNC || callback == SLEEP_FUNC); 783 784 /* 785 * The software stack doesn't have DMA which means the iSCSI 786 * protocol layer will be doing a bcopy from bp to outgoing 787 * streams buffers. Make sure that the buffer is mapped in 788 * so that the copy won't panic the system. 789 */ 790 if (bp && (bp->b_bcount != 0) && 791 bp_mapin_common(bp, (callback == NULL_FUNC) ? 792 VM_NOSLEEP : VM_SLEEP) == NULL) { 793 return (NULL); 794 } 795 796 ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private; 797 ASSERT(ilp != NULL); 798 799 if (pkt == NULL) { 800 pkt = scsi_hba_pkt_alloc(ilp->lun_sess->sess_hba->hba_dip, 801 ap, cmdlen, statuslen, tgtlen, sizeof (iscsi_cmd_t), 802 callback, arg); 803 if (pkt == NULL) { 804 return (NULL); 805 } 806 icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private; 807 icmdp->cmd_sig = ISCSI_SIG_CMD; 808 icmdp->cmd_state = ISCSI_CMD_STATE_FREE; 809 icmdp->cmd_lun = ilp; 810 icmdp->cmd_type = ISCSI_CMD_TYPE_SCSI; 811 /* add the report lun addressing type on to the lun */ 812 icmdp->cmd_un.scsi.lun = ilp->lun_addr_type << 14; 813 icmdp->cmd_un.scsi.lun = icmdp->cmd_un.scsi.lun | 814 ilp->lun_num; 815 icmdp->cmd_un.scsi.pkt = pkt; 816 icmdp->cmd_un.scsi.bp = bp; 817 icmdp->cmd_un.scsi.cmdlen = cmdlen; 818 icmdp->cmd_un.scsi.statuslen = statuslen; 819 icmdp->cmd_crc_error_seen = B_FALSE; 820 icmdp->cmd_misc_flags = 0; 821 if (flags & PKT_XARQ) { 822 icmdp->cmd_misc_flags |= ISCSI_CMD_MISCFLAG_XARQ; 823 } 824 825 mutex_init(&icmdp->cmd_mutex, NULL, MUTEX_DRIVER, NULL); 826 cv_init(&icmdp->cmd_completion, NULL, CV_DRIVER, NULL); 827 828 pkt->pkt_address = *ap; 829 pkt->pkt_comp = (void (*)())NULL; 830 pkt->pkt_flags = 0; 831 pkt->pkt_time = 0; 832 pkt->pkt_resid = 0; 833 pkt->pkt_statistics = 0; 834 pkt->pkt_reason = 0; 835 } 836 return (pkt); 837 } 838 839 /* 840 * iscsi_tran_lun_free - Free a SCSI LUN 841 */ 842 static void 843 iscsi_tran_lun_free(dev_info_t *hba_dip, dev_info_t *lun_dip, 844 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 845 { 846 iscsi_lun_t *ilp = NULL; 847 848 ASSERT(hba_dip != NULL); 849 ASSERT(lun_dip != NULL); 850 ASSERT(hba_tran != NULL); 851 ASSERT(sd != NULL); 852 ilp = (iscsi_lun_t *)hba_tran->tran_tgt_private; 853 ASSERT(ilp != NULL); 854 855 (void) mdi_prop_remove(ilp->lun_pip, NULL); 856 } 857 858 /* 859 * iscsi_start -- Start a SCSI transaction based on the packet 860 * This will attempt to add the icmdp to the pending queue 861 * for the connection and kick the queue. If the enqueue 862 * fails that means the queue is full. 863 */ 864 static int 865 iscsi_tran_start(struct scsi_address *ap, struct scsi_pkt *pkt) 866 { 867 iscsi_lun_t *ilp = NULL; 868 iscsi_sess_t *isp = NULL; 869 iscsi_cmd_t *icmdp = NULL; 870 uint_t flags; 871 872 ASSERT(ap != NULL); 873 ASSERT(pkt != NULL); 874 ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private; 875 isp = (iscsi_sess_t *)ilp->lun_sess; 876 icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private; 877 flags = pkt->pkt_flags; 878 ASSERT(ilp != NULL); 879 ASSERT(isp != NULL); 880 ASSERT(icmdp != NULL); 881 882 /* 883 * If the session is in the FREE state then 884 * all connections are down and retries have 885 * been exhausted. Fail command with fatal error. 886 */ 887 mutex_enter(&isp->sess_state_mutex); 888 if (isp->sess_state == ISCSI_SESS_STATE_FREE) { 889 mutex_exit(&isp->sess_state_mutex); 890 return (TRAN_FATAL_ERROR); 891 } 892 893 /* 894 * If the session is not in LOGGED_IN then we have 895 * no connections LOGGED_IN, but we haven't exhuasted 896 * our retries. Fail the command with busy so the 897 * caller might try again later. Once retries are 898 * exhausted the state machine will move us to FREE. 899 */ 900 if (isp->sess_state != ISCSI_SESS_STATE_LOGGED_IN) { 901 mutex_exit(&isp->sess_state_mutex); 902 return (TRAN_BUSY); 903 } 904 905 /* 906 * If we haven't received data from the target in the 907 * max specified period something is wrong with the 908 * transport. Fail IO with FATAL_ERROR. 909 */ 910 if (isp->sess_rx_lbolt + SEC_TO_TICK(iscsi_rx_max_window) < 911 ddi_get_lbolt()) { 912 mutex_exit(&isp->sess_state_mutex); 913 return (TRAN_FATAL_ERROR); 914 } 915 916 /* 917 * If we haven't received data from the target in the 918 * specified period something is probably wrong with 919 * the transport. Just return back BUSY until either 920 * the problem is resolved of the transport fails. 921 */ 922 if (isp->sess_rx_lbolt + SEC_TO_TICK(iscsi_rx_window) < 923 ddi_get_lbolt()) { 924 mutex_exit(&isp->sess_state_mutex); 925 return (TRAN_BUSY); 926 } 927 928 929 /* reset cmd values in case upper level driver is retrying cmd */ 930 icmdp->cmd_prev = icmdp->cmd_next = NULL; 931 icmdp->cmd_crc_error_seen = B_FALSE; 932 icmdp->cmd_lbolt_pending = icmdp->cmd_lbolt_active = 933 icmdp->cmd_lbolt_aborting = icmdp->cmd_lbolt_timeout = 934 (clock_t)NULL; 935 icmdp->cmd_itt = icmdp->cmd_ttt = 0; 936 icmdp->cmd_un.scsi.abort_icmdp = NULL; 937 938 mutex_enter(&isp->sess_queue_pending.mutex); 939 iscsi_cmd_state_machine(icmdp, ISCSI_CMD_EVENT_E1, isp); 940 mutex_exit(&isp->sess_queue_pending.mutex); 941 mutex_exit(&isp->sess_state_mutex); 942 943 /* 944 * If this packet doesn't have FLAG_NOINTR set, it could have 945 * already run to completion (and the memory freed) at this 946 * point, so check our local copy of pkt_flags. Otherwise we 947 * have to wait for completion before returning to the caller. 948 */ 949 if (flags & FLAG_NOINTR) { 950 mutex_enter(&icmdp->cmd_mutex); 951 while ((icmdp->cmd_state != ISCSI_CMD_STATE_COMPLETED) || 952 (icmdp->cmd_un.scsi.r2t_icmdp != NULL) || 953 (icmdp->cmd_un.scsi.abort_icmdp != NULL) || 954 (icmdp->cmd_un.scsi.r2t_more == B_TRUE)) { 955 cv_wait(&icmdp->cmd_completion, &icmdp->cmd_mutex); 956 } 957 icmdp->cmd_state = ISCSI_CMD_STATE_FREE; 958 mutex_exit(&icmdp->cmd_mutex); 959 } 960 961 return (TRAN_ACCEPT); 962 } 963 964 /* 965 * iscsi_tran_abort - Called when an upper level application 966 * or driver wants to kill a scsi_pkt that was already sent to 967 * this driver. 968 */ 969 /* ARGSUSED */ 970 static int 971 iscsi_tran_abort(struct scsi_address *ap, struct scsi_pkt *pkt) 972 { 973 return (0); 974 } 975 976 /* 977 * iscsi_tran_reset - Reset target at either BUS, TARGET, or LUN 978 * level. This will require the issuing of a task management 979 * command down to the target/lun. 980 */ 981 static int 982 iscsi_tran_reset(struct scsi_address *ap, int level) 983 { 984 int rval = ISCSI_STATUS_INTERNAL_ERROR; 985 iscsi_sess_t *isp = NULL; 986 iscsi_lun_t *ilp = NULL; 987 988 ilp = (iscsi_lun_t *)ap->a_hba_tran->tran_tgt_private; 989 ASSERT(ilp != NULL); 990 isp = ilp->lun_sess; 991 ASSERT(isp != NULL); 992 993 switch (level) { 994 case RESET_LUN: 995 /* reset attempt will block until attempt is complete */ 996 rval = iscsi_handle_reset(isp, level, ilp); 997 break; 998 case RESET_BUS: 999 /* 1000 * What are we going to realy reset the ethernet 1001 * network!? Just fall through to a target reset. 1002 */ 1003 case RESET_TARGET: 1004 /* reset attempt will block until attempt is complete */ 1005 rval = iscsi_handle_reset(isp, level, NULL); 1006 break; 1007 case RESET_ALL: 1008 default: 1009 break; 1010 } 1011 1012 return (ISCSI_SUCCESS(rval) ? 1 : 0); 1013 } 1014 1015 /* 1016 * iscsi_tran_getcap - Get target/lun capabilities. 1017 */ 1018 static int 1019 iscsi_tran_getcap(struct scsi_address *ap, char *cap, int whom) 1020 { 1021 return (iscsi_i_commoncap(ap, cap, 0, whom, 0)); 1022 } 1023 1024 1025 /* 1026 * iscsi_tran_setcap - Set target/lun capabilities. 1027 */ 1028 /* ARGSUSED */ 1029 static int 1030 iscsi_tran_setcap(struct scsi_address *ap, char *cap, int value, int whom) 1031 { 1032 return (iscsi_i_commoncap(ap, cap, 0, whom, 1)); 1033 } 1034 1035 1036 /* 1037 * iscsi_tran_destroy_pkt - Clean up packet 1038 */ 1039 static void 1040 iscsi_tran_destroy_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 1041 { 1042 iscsi_cmd_t *icmdp; 1043 1044 icmdp = (iscsi_cmd_t *)pkt->pkt_ha_private; 1045 1046 ASSERT(icmdp != NULL); 1047 ASSERT(icmdp->cmd_sig == ISCSI_SIG_CMD); 1048 ASSERT(icmdp->cmd_state == ISCSI_CMD_STATE_FREE); 1049 1050 mutex_destroy(&icmdp->cmd_mutex); 1051 cv_destroy(&icmdp->cmd_completion); 1052 scsi_hba_pkt_free(ap, pkt); 1053 } 1054 1055 /* 1056 * iscsi_tran_dmafree - This is a software driver, NO DMA 1057 */ 1058 /* ARGSUSED */ 1059 static void 1060 iscsi_tran_dmafree(struct scsi_address *ap, struct scsi_pkt *pkt) 1061 { 1062 /* 1063 * The iSCSI interface doesn't deal with DMA 1064 */ 1065 } 1066 1067 /* 1068 * iscsi_tran_sync_pkt - This is a software driver, NO DMA 1069 */ 1070 /* ARGSUSED */ 1071 static void 1072 iscsi_tran_sync_pkt(struct scsi_address *ap, struct scsi_pkt *pkt) 1073 { 1074 /* 1075 * The iSCSI interface doesn't deal with DMA 1076 */ 1077 } 1078 1079 /* 1080 * iscsi_tran_reset_notify - We don't support BUS_RESET so there 1081 * is no point in support callback. 1082 */ 1083 /* ARGSUSED */ 1084 static int 1085 iscsi_tran_reset_notify(struct scsi_address *ap, int flag, 1086 void (*callback) (caddr_t), caddr_t arg) 1087 { 1088 1089 /* 1090 * We never do BUS_RESETS so allowing this call 1091 * back to register has no point? 1092 */ 1093 return (DDI_SUCCESS); 1094 } 1095 1096 1097 /* 1098 * iscsi_tran_bus_config - on demand device configuration 1099 * 1100 * iscsi_tran_bus_config is called by the NDI layer at the completion 1101 * of a dev_node creation. There are two primary cases defined in this 1102 * function. The first is BUS_CONFIG_ALL. In this case the NDI is trying 1103 * to identify that targets/luns are available configured at that point 1104 * in time. It is safe to just complete the process succcessfully. The 1105 * second case is a new case that was defined in S10 for devfs. BUS_CONFIG_ONE 1106 * this is to help driver the top down discovery instead of bottom up. If 1107 * we receive a BUS_CONFIG_ONE we should check to see if the <addr> exists 1108 * if so complete successfull processing. Otherwise we should call the 1109 * deamon and see if we can plumb the <addr>. If it is possible to plumb the 1110 * <addr> block until plumbing is complete. In both cases of being able to 1111 * plumb <addr> or not continue with successfull processing. 1112 */ 1113 static int 1114 iscsi_tran_bus_config(dev_info_t *parent, uint_t flags, 1115 ddi_bus_config_op_t op, void *arg, dev_info_t **childp) 1116 { 1117 int rval = NDI_SUCCESS; 1118 iscsi_hba_t *ihp = NULL; 1119 int iflags = flags; 1120 char *name = NULL; 1121 char *ptr = NULL; 1122 1123 /* get reference to soft state */ 1124 ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state, 1125 ddi_get_instance(parent)); 1126 if (ihp == NULL) { 1127 return (NDI_FAILURE); 1128 } 1129 1130 /* lock so only one config operation occrs */ 1131 sema_p(&iscsid_config_semaphore); 1132 1133 switch (op) { 1134 case BUS_CONFIG_ONE: 1135 /* parse target name out of name given */ 1136 if ((ptr = strchr((char *)arg, '@')) == NULL) { 1137 rval = NDI_FAILURE; 1138 break; 1139 } 1140 ptr++; /* move past '@' */ 1141 name = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP); 1142 (void) strncpy(name, ptr, MAX_GET_NAME_SIZE); 1143 /* We need to strip the LUN */ 1144 if ((ptr = strchr(name, ',')) == NULL) { 1145 rval = NDI_FAILURE; 1146 kmem_free(name, MAX_GET_NAME_SIZE); 1147 name = NULL; 1148 break; 1149 } 1150 /* We also need to strip the 4 bytes of hex TPGT */ 1151 ptr -= 4; 1152 if (ptr <= name) { 1153 rval = NDI_FAILURE; 1154 kmem_free(name, MAX_GET_NAME_SIZE); 1155 name = NULL; 1156 break; 1157 } 1158 *ptr = '\0'; /* NULL terminate */ 1159 1160 /* translate name back to original iSCSI name */ 1161 iscsi_get_name_to_iqn(name, MAX_GET_NAME_SIZE); 1162 1163 /* configure target, skip 4 byte ISID */ 1164 iscsid_config_one(ihp, (name+4), B_TRUE); 1165 1166 kmem_free(name, MAX_GET_NAME_SIZE); 1167 name = NULL; 1168 1169 /* 1170 * DDI group instructed us to use this flag. 1171 */ 1172 iflags |= NDI_MDI_FALLBACK; 1173 break; 1174 case BUS_CONFIG_DRIVER: 1175 /* FALLTHRU */ 1176 case BUS_CONFIG_ALL: 1177 iscsid_config_all(ihp, B_TRUE); 1178 break; 1179 default: 1180 rval = NDI_FAILURE; 1181 break; 1182 } 1183 1184 if (rval == NDI_SUCCESS) { 1185 rval = ndi_busop_bus_config(parent, iflags, 1186 op, arg, childp, 0); 1187 } 1188 sema_v(&iscsid_config_semaphore); 1189 1190 return (rval); 1191 } 1192 1193 /* 1194 * iscsi_tran_bus_unconfig - on demand device unconfiguration 1195 * 1196 * Called by the os framework under low resource situations. 1197 * It will attempt to unload our minor nodes (logical units 1198 * ndi/mdi nodes). 1199 */ 1200 static int 1201 iscsi_tran_bus_unconfig(dev_info_t *parent, uint_t flag, 1202 ddi_bus_config_op_t op, void *arg) 1203 { 1204 return (ndi_busop_bus_unconfig(parent, flag, op, arg)); 1205 } 1206 1207 1208 /* 1209 * iscsi_tran_get_name - create private /devices name for LUN 1210 * 1211 * This creates the <addr> in /devices/iscsi/<driver>@<addr> 1212 * path. For this <addr> we return the <session/target_name>,<lun num> 1213 * Where <target_name> is an <iqn/eui/...> as defined by the iSCSI 1214 * specification. We do modify the name slightly so that it still 1215 * complies with the IEEE <addr> naming scheme. This means that we 1216 * will substitute out the ':', '@', ... and other reserved characters 1217 * defined in the IEEE definition with '%<hex value of special char>' 1218 * This routine is indirectly called by iscsi_lun_create_xxx. These 1219 * calling routines must prevent the session and lun lists from changing 1220 * during this routine. 1221 */ 1222 static int 1223 iscsi_tran_get_name(struct scsi_device *sd, char *name, int len) 1224 { 1225 int target = 0; 1226 int lun = 0; 1227 iscsi_hba_t *ihp = NULL; 1228 iscsi_sess_t *isp = NULL; 1229 iscsi_lun_t *ilp = NULL; 1230 dev_info_t *lun_dip = NULL; 1231 1232 ASSERT(sd != NULL); 1233 ASSERT(name != NULL); 1234 lun_dip = sd->sd_dev; 1235 ASSERT(lun_dip != NULL); 1236 1237 /* get reference to soft state */ 1238 ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state, 1239 ddi_get_instance(ddi_get_parent(lun_dip))); 1240 if (ihp == NULL) { 1241 name[0] = '\0'; 1242 return (0); 1243 } 1244 1245 /* Get the target num */ 1246 target = ddi_prop_get_int(DDI_DEV_T_ANY, sd->sd_dev, 1247 DDI_PROP_DONTPASS, TARGET_PROP, 0); 1248 1249 /* Get the target num */ 1250 lun = ddi_prop_get_int(DDI_DEV_T_ANY, sd->sd_dev, 1251 DDI_PROP_DONTPASS, LUN_PROP, 0); 1252 1253 /* 1254 * Now we need to find our ilp by walking the lists 1255 * off the ihp and isp. 1256 */ 1257 /* See if we already created this session */ 1258 1259 /* Walk the HBA's session list */ 1260 for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) { 1261 /* compare target name as the unique identifier */ 1262 if (target == isp->sess_oid) { 1263 /* found match */ 1264 break; 1265 } 1266 } 1267 1268 /* If we found matching session continue searching for tgt */ 1269 if (isp == NULL) { 1270 /* sess not found */ 1271 name[0] = '\0'; 1272 return (0); 1273 } 1274 1275 /* 1276 * Search for the matching iscsi lun structure. We don't 1277 * need to hold the READER for the lun list at this point. 1278 * because the tran_get_name is being called from the online 1279 * function which is already holding a reader on the lun 1280 * list. 1281 */ 1282 for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) { 1283 if (lun == ilp->lun_num) { 1284 /* found match */ 1285 break; 1286 } 1287 } 1288 1289 if (ilp == NULL) { 1290 /* tgt not found */ 1291 name[0] = '\0'; 1292 return (0); 1293 } 1294 1295 /* Ensure enough space for lun_addr is available */ 1296 ASSERT(ilp->lun_addr != NULL); 1297 if ((strlen(ilp->lun_addr) + 1) > len) { 1298 return (0); 1299 } 1300 1301 /* copy lun_addr name */ 1302 (void) strcpy(name, ilp->lun_addr); 1303 1304 /* 1305 * Based on IEEE-1275 we can't have any ':', ' ', '@', or '/' 1306 * characters in our naming. So replace all those characters 1307 * with '-' 1308 */ 1309 iscsi_get_name_from_iqn(name, len); 1310 1311 return (1); 1312 } 1313 1314 /* 1315 * iscsi_tran_get_bus_addr - This returns a human readable string 1316 * for the bus address. Examining most other drivers fcp, etc. They 1317 * all just return the same string as tran_get_name. In our case 1318 * our tran get name is already some what usable so leave alone. 1319 */ 1320 static int 1321 iscsi_tran_get_bus_addr(struct scsi_device *sd, char *name, int len) 1322 { 1323 return (iscsi_tran_get_name(sd, name, len)); 1324 } 1325 1326 1327 /* 1328 * +--------------------------------------------------------------------+ 1329 * | End of scsi_tran routines | 1330 * +--------------------------------------------------------------------+ 1331 */ 1332 1333 /* 1334 * +--------------------------------------------------------------------+ 1335 * | Start of cb_ops routines | 1336 * +--------------------------------------------------------------------+ 1337 */ 1338 1339 /* 1340 * iscsi_open - Driver should be made IOCTL MT safe. Otherwise 1341 * this function needs updated. 1342 */ 1343 /* ARGSUSED */ 1344 static int 1345 iscsi_open(dev_t *devp, int flags, int otyp, cred_t *credp) 1346 { 1347 return (0); 1348 } 1349 1350 /* 1351 * iscsi_close - 1352 */ 1353 /* ARGSUSED */ 1354 static int 1355 iscsi_close(dev_t dev, int flags, int otyp, cred_t *credp) 1356 { 1357 return (0); 1358 } 1359 1360 /* 1361 * iscsi_ioctl - 1362 */ 1363 /* ARGSUSED */ 1364 int 1365 iscsi_ioctl(dev_t dev, int cmd, intptr_t arg, int mode, 1366 cred_t *credp, int *rvalp) 1367 { 1368 int rtn = 0; 1369 int instance = 0; 1370 int list_space = 0; 1371 int lun_sz = 0; 1372 int did; 1373 int retry; 1374 iscsi_hba_t *ihp = NULL; 1375 iscsi_sess_t *isp = NULL; 1376 iscsi_conn_t *icp = NULL; 1377 iscsi_login_params_t *params = NULL; 1378 iscsi_login_params_t *tmpParams = NULL; 1379 uchar_t *name = NULL; 1380 dev_info_t *lun_dip = NULL; 1381 1382 entry_t e; 1383 iscsi_oid_t oid; 1384 iscsi_property_t *ipp; 1385 iscsi_static_property_t *ispp; 1386 iscsi_param_get_t *ilg; 1387 iscsi_param_set_t *ils; 1388 iscsi_target_list_t idl, *idlp = NULL; 1389 iscsi_addr_list_t ial, *ialp = NULL; 1390 iscsi_chap_props_t *chap = NULL; 1391 iscsi_radius_props_t *radius = NULL; 1392 iscsi_auth_props_t *auth = NULL; 1393 iscsi_lun_list_t *ll, *llp = NULL; 1394 iscsi_lun_props_t *lun = NULL; 1395 iscsi_lun_t *ilp = NULL; 1396 iSCSIDiscoveryMethod_t method; 1397 iSCSIDiscoveryProperties_t discovery_props; 1398 iscsi_uscsi_t iu; 1399 iscsi_uscsi_t iu_caller; 1400 #ifdef _MULTI_DATAMODEL 1401 /* For use when a 32 bit app makes a call into a 64 bit ioctl */ 1402 iscsi_uscsi32_t iu32_caller; 1403 model_t model; 1404 #endif /* _MULTI_DATAMODEL */ 1405 void *void_p; 1406 iscsi_sendtgts_list_t *stl_hdr; 1407 iscsi_sendtgts_list_t *istl; 1408 int stl_sz; 1409 iscsi_target_entry_t *target; 1410 uint32_t old_oid; 1411 uint32_t target_oid; 1412 iscsi_targetparam_entry_t *curr_entry; 1413 char *initiator_node_name; 1414 char *initiator_node_alias; 1415 isns_portal_group_list_t *pg_list = NULL; 1416 isns_server_portal_group_list_t *server_pg_list_hdr = NULL; 1417 isns_server_portal_group_list_t *server_pg_list = NULL; 1418 int pg_list_sz, pg_sz_copy_out, server_pg_list_sz; 1419 iscsi_config_sess_t *ics; 1420 int size; 1421 boolean_t rval; 1422 char init_port_name[MAX_NAME_PROP_SIZE]; 1423 iscsi_sockaddr_t addr_dsc; 1424 iscsi_boot_property_t *bootProp; 1425 boolean_t discovered = B_TRUE; 1426 1427 instance = getminor(dev); 1428 ihp = (iscsi_hba_t *)ddi_get_soft_state(iscsi_state, instance); 1429 if (ihp == NULL) 1430 return (EFAULT); 1431 1432 switch (cmd) { 1433 /* 1434 * ISCSI_CREATE_OID - Create a Object IDentifier for a TargetName 1435 */ 1436 case ISCSI_CREATE_OID: 1437 if (ddi_copyin((caddr_t)arg, &oid, sizeof (oid), mode)) { 1438 rtn = EFAULT; 1439 break; 1440 } 1441 if (oid.o_vers != ISCSI_INTERFACE_VERSION) { 1442 rtn = EINVAL; 1443 break; 1444 } 1445 1446 /* Set the target that this session is associated with */ 1447 oid.o_oid = iscsi_targetparam_get_oid(oid.o_name); 1448 1449 if (ddi_copyout(&oid, (caddr_t)arg, sizeof (oid), mode)) { 1450 rtn = EFAULT; 1451 break; 1452 } 1453 break; 1454 /* 1455 * ISCSI_PARAM_GET - Get param for specified 1456 * connection/session. 1457 */ 1458 case ISCSI_PARAM_GET: 1459 /* copyin user args */ 1460 ilg = (iscsi_param_get_t *)kmem_alloc(sizeof (*ilg), KM_SLEEP); 1461 if (ddi_copyin((caddr_t)arg, ilg, sizeof (*ilg), mode)) { 1462 rtn = EFAULT; 1463 kmem_free(ilg, sizeof (*ilg)); 1464 break; 1465 } 1466 1467 if (ilg->g_vers != ISCSI_INTERFACE_VERSION) { 1468 rtn = EINVAL; 1469 kmem_free(ilg, sizeof (*ilg)); 1470 break; 1471 } 1472 1473 /* handle special case for Initiator name */ 1474 if (ilg->g_param == ISCSI_LOGIN_PARAM_INITIATOR_NAME) { 1475 (void) strlcpy((char *)ilg->g_value.v_name, 1476 (char *)ihp->hba_name, ISCSI_MAX_NAME_LEN); 1477 } else if (ilg->g_param == ISCSI_LOGIN_PARAM_INITIATOR_ALIAS) { 1478 if (ihp->hba_alias_length == 0) { 1479 rtn = EINVAL; 1480 } else { 1481 (void) strlcpy((char *)ilg->g_value.v_name, 1482 (char *)ihp->hba_alias, ISCSI_MAX_NAME_LEN); 1483 } 1484 } else { 1485 /* To describe the validity of the requested param */ 1486 boolean_t valid_flag = B_TRUE; 1487 1488 name = NULL; 1489 1490 /* 1491 * switch login based if looking for initiator 1492 * params 1493 */ 1494 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 1495 if (ilg->g_oid == ihp->hba_oid) { 1496 /* initiator */ 1497 params = &ihp->hba_params; 1498 name = ihp->hba_name; 1499 if (iscsi_get_persisted_param(name, 1500 ilg, params) != 0) { 1501 valid_flag = B_FALSE; 1502 } 1503 } else { 1504 /* 1505 * If the oid does represent a session check 1506 * to see if it is a target oid. If so, 1507 * return the target's associated session. 1508 */ 1509 rtn = iscsi_sess_get(ilg->g_oid, ihp, &isp); 1510 if (rtn != 0) { 1511 rtn = iscsi_sess_get_by_target( 1512 ilg->g_oid, ihp, &isp); 1513 } 1514 1515 /* 1516 * If rtn is zero then we have found an 1517 * existing session. Use the session name to 1518 * do param lookup. If rtn is non-zero then 1519 * create a targetparam object and use its name 1520 * for param lookup. 1521 */ 1522 if (rtn == 0) { 1523 name = isp->sess_name; 1524 params = &isp->sess_params; 1525 } else { 1526 name = 1527 iscsi_targetparam_get_name( 1528 ilg->g_oid); 1529 if (ilg->g_param_type == 1530 ISCSI_SESS_PARAM) { 1531 tmpParams = 1532 (iscsi_login_params_t *) 1533 kmem_alloc( 1534 sizeof (*tmpParams), 1535 KM_SLEEP); 1536 params = tmpParams; 1537 } 1538 rtn = 0; 1539 } 1540 1541 if (name == NULL) { 1542 rw_exit( 1543 &ihp->hba_sess_list_rwlock); 1544 rtn = EFAULT; 1545 kmem_free(ilg, sizeof (*ilg)); 1546 if (tmpParams != NULL) 1547 kmem_free(tmpParams, 1548 sizeof (*tmpParams)); 1549 1550 break; 1551 } 1552 1553 if (ilg->g_param_type == ISCSI_SESS_PARAM) { 1554 /* session */ 1555 /* 1556 * Update sess_params with the 1557 * latest params from the 1558 * persistent store. 1559 */ 1560 if (iscsi_get_persisted_param(name, 1561 ilg, params) != 0) { 1562 /* 1563 * If the parameter in 1564 * question is not 1565 * overriden, no effect 1566 * on existing session 1567 * parameters. However, 1568 * the parameter is 1569 * marked invalid 1570 * (from the standpoint 1571 * of whether it is 1572 * overriden). 1573 */ 1574 valid_flag = B_FALSE; 1575 } 1576 } else if (ilg->g_param_type == 1577 ISCSI_CONN_PARAM && isp != NULL) { 1578 /* connection */ 1579 rw_enter(&isp->sess_conn_list_rwlock, 1580 RW_READER); 1581 /* Assuming 1 conn per sess. */ 1582 /* 1583 * MC/S - Need to be modified to 1584 * take g_conn_cid into account when 1585 * we go multi-connection. 1586 */ 1587 if ((isp->sess_conn_act != NULL) && 1588 (isp->sess_conn_act->conn_state == 1589 ISCSI_CONN_STATE_LOGGED_IN)) { 1590 params = &(isp-> 1591 sess_conn_act-> 1592 conn_params); 1593 } else { 1594 valid_flag = B_FALSE; 1595 } 1596 rw_exit(&isp->sess_conn_list_rwlock); 1597 } 1598 } 1599 1600 /* make sure we have params to get info from */ 1601 if (params) { 1602 rtn = iscsi_get_param(params, valid_flag, ilg); 1603 1604 /* 1605 * for target parameters, check if any 1606 * parameters were overridden at the initiator 1607 * level. If so, then change the default value 1608 * to the initiator's overridden value 1609 */ 1610 if ((rtn == 0) && 1611 (ilg->g_oid != ihp->hba_oid)) { 1612 iscsi_override_target_default(ihp, 1613 ilg); 1614 } 1615 } 1616 rw_exit(&ihp->hba_sess_list_rwlock); 1617 } 1618 1619 if (rtn == 0) { 1620 rtn = ddi_copyout(ilg, (caddr_t)arg, 1621 sizeof (iscsi_param_get_t), mode); 1622 } 1623 kmem_free(ilg, sizeof (*ilg)); 1624 if (tmpParams != NULL) 1625 kmem_free(tmpParams, sizeof (*tmpParams)); 1626 break; 1627 1628 /* 1629 * ISCSI_INIT_NODE_NAME_SET - Change the initiator-node name for 1630 * the specified connection/session. 1631 */ 1632 case ISCSI_INIT_NODE_NAME_SET: 1633 /* copyin user args */ 1634 ils = (iscsi_param_set_t *)kmem_alloc(sizeof (*ils), KM_SLEEP); 1635 if (ddi_copyin((caddr_t)arg, ils, sizeof (*ils), mode)) { 1636 rtn = EFAULT; 1637 kmem_free(ils, sizeof (*ils)); 1638 break; 1639 } 1640 1641 if (ils->s_vers != ISCSI_INTERFACE_VERSION) { 1642 rtn = EINVAL; 1643 kmem_free(ils, sizeof (*ils)); 1644 break; 1645 } 1646 1647 /* saving off the old initiator-node name */ 1648 initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 1649 rval = persistent_initiator_name_get(initiator_node_name, 1650 ISCSI_MAX_NAME_LEN); 1651 1652 rtn = iscsi_set_params(ils, ihp, B_TRUE); 1653 kmem_free(ils, sizeof (*ils)); 1654 if (rtn != 0) { 1655 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 1656 return (rtn); 1657 } 1658 1659 (void) snprintf(init_port_name, MAX_NAME_PROP_SIZE, 1660 "%s,%02x%02x%02x%02x%02x%02x", 1661 (char *)ihp->hba_name, ihp->hba_isid[0], 1662 ihp->hba_isid[1], ihp->hba_isid[2], 1663 ihp->hba_isid[3], ihp->hba_isid[4], 1664 ihp->hba_isid[5]); 1665 1666 if (ddi_prop_update_string(DDI_DEV_T_NONE, 1667 ihp->hba_dip, "initiator-port", 1668 init_port_name) != DDI_PROP_SUCCESS) { 1669 cmn_err(CE_WARN, "iscsi_ioctl: Updating " 1670 "initiator-port property on iSCSI " 1671 "HBA(%s) with dip(%d) Failed", 1672 (char *)ihp->hba_name, 1673 ddi_get_instance(ihp->hba_dip)); 1674 } 1675 1676 /* 1677 * Deregister the old initiator-node name from the iSNS 1678 * server 1679 * Register the new initiator-node name with the iSNS server 1680 */ 1681 method = persistent_disc_meth_get(); 1682 if (method & iSCSIDiscoveryMethodISNS) { 1683 if (rval == B_TRUE) { 1684 if (strlen(initiator_node_name) > 0) { 1685 /* 1686 * we will attempt to offline the targets. 1687 * if logouts fail, we will still continue 1688 */ 1689 #define STRING_INNO "initiator-node name - Offline " 1690 #define STRING_FFOMD "failed for one or more devices" 1691 if ((iscsid_del( 1692 ihp, NULL, method, NULL)) 1693 != B_TRUE) { 1694 cmn_err(CE_NOTE, 1695 "Attempting to change " 1696 STRING_INNO 1697 STRING_FFOMD); 1698 } 1699 (void) isns_dereg(ihp->hba_isid, 1700 (uint8_t *)initiator_node_name); 1701 #undef STRING_INNO 1702 #undef STRING_FFOMD 1703 } 1704 } 1705 if (persistent_initiator_name_get(initiator_node_name, 1706 ISCSI_MAX_NAME_LEN) != B_TRUE) { 1707 kmem_free(initiator_node_name, 1708 ISCSI_MAX_NAME_LEN); 1709 initiator_node_name = NULL; 1710 rtn = EIO; 1711 break; 1712 } 1713 if (strlen(initiator_node_name) == 0) { 1714 kmem_free(initiator_node_name, 1715 ISCSI_MAX_NAME_LEN); 1716 initiator_node_name = NULL; 1717 rtn = EIO; 1718 break; 1719 } 1720 1721 initiator_node_alias = kmem_zalloc(ISCSI_MAX_NAME_LEN, 1722 KM_SLEEP); 1723 if (persistent_alias_name_get(initiator_node_alias, 1724 ISCSI_MAX_NAME_LEN) != B_TRUE) { 1725 initiator_node_alias[0] = '\0'; 1726 } 1727 1728 (void) isns_reg(ihp->hba_isid, 1729 (uint8_t *)initiator_node_name, 1730 ISCSI_MAX_NAME_LEN, 1731 (uint8_t *)initiator_node_alias, 1732 ISCSI_MAX_NAME_LEN, 1733 ISNS_INITIATOR_NODE_TYPE, 1734 isns_scn_callback); 1735 iscsid_do_isns_query(ihp); 1736 1737 /* Done using the name and alias - free them. */ 1738 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 1739 initiator_node_name = NULL; 1740 kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN); 1741 initiator_node_alias = NULL; 1742 } 1743 break; 1744 1745 /* 1746 * ISCSI_PARAM_SET - Set param for specified connection/session. 1747 */ 1748 case ISCSI_PARAM_SET: 1749 /* copyin user args */ 1750 ils = (iscsi_param_set_t *)kmem_alloc(sizeof (*ils), KM_SLEEP); 1751 if (ddi_copyin((caddr_t)arg, ils, sizeof (*ils), mode)) { 1752 rtn = EFAULT; 1753 kmem_free(ils, sizeof (*ils)); 1754 break; 1755 } 1756 1757 if (ils->s_vers != ISCSI_INTERFACE_VERSION) { 1758 rtn = EINVAL; 1759 kmem_free(ils, sizeof (*ils)); 1760 break; 1761 } 1762 rtn = iscsi_set_params(ils, ihp, B_TRUE); 1763 if (iscsiboot_prop) { 1764 if (iscsi_cmp_boot_sess_oid(ihp, ils->s_oid)) { 1765 /* 1766 * found active session for this object 1767 * or this is initiator's object 1768 * with mpxio enabled 1769 */ 1770 if (!iscsi_reconfig_boot_sess(ihp)) { 1771 rtn = EINVAL; 1772 kmem_free(ils, sizeof (*ils)); 1773 break; 1774 } 1775 } 1776 } 1777 kmem_free(ils, sizeof (*ils)); 1778 break; 1779 1780 /* 1781 * ISCSI_TARGET_PARAM_CLEAR 1782 * - remove custom parameter settings for a target. 1783 */ 1784 case ISCSI_TARGET_PARAM_CLEAR: 1785 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 1786 rtn = EFAULT; 1787 break; 1788 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 1789 rtn = EINVAL; 1790 break; 1791 } 1792 1793 if ((e.e_oid != ihp->hba_oid) && 1794 (e.e_oid != ISCSI_OID_NOTSET)) { 1795 uchar_t *t_name; 1796 iscsi_sess_t *t_isp; 1797 1798 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 1799 /* 1800 * If the oid does represent a session check to see 1801 * if it is a target oid. If so, return the target's 1802 * associated session. 1803 */ 1804 rtn = iscsi_sess_get(e.e_oid, ihp, &isp); 1805 if (rtn != 0) { 1806 rtn = iscsi_sess_get_by_target(e.e_oid, ihp, 1807 &isp); 1808 } 1809 1810 /* 1811 * If rtn is zero then we have found an 1812 * existing session. Use the session name to 1813 * do param lookup. If rtn is non-zero then 1814 * create a targetparam object and use its name 1815 * for param lookup. 1816 */ 1817 if (rtn == 0) { 1818 t_name = isp->sess_name; 1819 } else { 1820 t_name = iscsi_targetparam_get_name(e.e_oid); 1821 rtn = 0; 1822 } 1823 1824 if (t_name == NULL) { 1825 rw_exit(&ihp->hba_sess_list_rwlock); 1826 rtn = EFAULT; 1827 break; 1828 } 1829 1830 name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 1831 (void) strncpy((char *)name, (char *)t_name, 1832 ISCSI_MAX_NAME_LEN); 1833 1834 if (persistent_param_clear((char *)name) == B_FALSE) { 1835 kmem_free(name, ISCSI_MAX_NAME_LEN); 1836 rw_exit(&ihp->hba_sess_list_rwlock); 1837 rtn = EIO; 1838 break; 1839 } 1840 1841 ics = kmem_zalloc(sizeof (*ics), KM_SLEEP); 1842 ics->ics_ver = ISCSI_INTERFACE_VERSION; 1843 ics->ics_oid = ISCSI_INITIATOR_OID; 1844 ics->ics_in = 1; 1845 1846 /* 1847 * We may have multiple sessions with different 1848 * tpgt values. So we need to loop through 1849 * the sessions and update all sessions. 1850 */ 1851 for (isp = ihp->hba_sess_list; isp; 1852 isp = t_isp) { 1853 t_isp = isp->sess_next; 1854 1855 if (strncmp((char *)isp->sess_name, 1856 (char *)name, ISCSI_MAX_NAME_LEN) == 0) { 1857 /* 1858 * When removing target-params we need 1859 * slightly different actions depending 1860 * on if the session should still exist. 1861 * Get the initiator-node value for 1862 * MS/T. If there is no initiator 1863 * value then assume the default value 1864 * of 1. If the initiator value is 1865 * less than this ISID then we need to 1866 * destroy the session. Otherwise 1867 * update the session information and 1868 * resync (N7 event). 1869 */ 1870 rtn = iscsi_ioctl_get_config_sess( 1871 ihp, ics); 1872 if (((rtn != 0) && 1873 (isp->sess_isid[5] > 0)) || 1874 ((rtn == 0) && 1875 (ics->ics_out <= 1876 isp->sess_isid[5]))) { 1877 1878 /* 1879 * This session should no 1880 * longer exist. Remove 1881 * session. 1882 */ 1883 if (!ISCSI_SUCCESS( 1884 iscsi_sess_destroy(isp))) { 1885 kmem_free(ics, 1886 sizeof (*ics)); 1887 kmem_free(name, 1888 ISCSI_MAX_NAME_LEN); 1889 rw_exit(&ihp-> 1890 hba_sess_list_rwlock); 1891 rtn = EBUSY; 1892 break; 1893 } 1894 isp = ihp->hba_sess_list; 1895 } else { 1896 /* 1897 * Reset the session 1898 * parameters. 1899 */ 1900 bcopy(&(isp->sess_hba-> 1901 hba_params), 1902 &(isp->sess_params), 1903 sizeof (isp->sess_params)); 1904 if (iscsiboot_prop && 1905 isp->sess_boot) { 1906 /* 1907 * reconfig boot 1908 * session later 1909 */ 1910 continue; 1911 } 1912 /* 1913 * Notify the session that the 1914 * login parameters have 1915 * changed. 1916 */ 1917 mutex_enter(&isp-> 1918 sess_state_mutex); 1919 iscsi_sess_state_machine(isp, 1920 ISCSI_SESS_EVENT_N7); 1921 mutex_exit(&isp-> 1922 sess_state_mutex); 1923 } 1924 } 1925 } 1926 kmem_free(ics, sizeof (*ics)); 1927 kmem_free(name, ISCSI_MAX_NAME_LEN); 1928 rw_exit(&ihp->hba_sess_list_rwlock); 1929 if (iscsiboot_prop) { 1930 if (iscsi_cmp_boot_sess_oid(ihp, e.e_oid)) { 1931 /* 1932 * found active session for this object 1933 * or this is initiator object 1934 * with mpxio enabled 1935 */ 1936 if (!iscsi_reconfig_boot_sess(ihp)) { 1937 rtn = EINVAL; 1938 break; 1939 } 1940 } 1941 } 1942 } 1943 break; 1944 1945 /* 1946 * ISCSI_TARGET_OID_LIST_GET - 1947 */ 1948 case ISCSI_TARGET_OID_LIST_GET: 1949 /* copyin user args */ 1950 if (ddi_copyin((caddr_t)arg, &idl, 1951 sizeof (idl), mode)) { 1952 rtn = EFAULT; 1953 break; 1954 } 1955 1956 if (idl.tl_vers != ISCSI_INTERFACE_VERSION) { 1957 rtn = EINVAL; 1958 break; 1959 } 1960 1961 list_space = sizeof (iscsi_target_list_t); 1962 if (idl.tl_in_cnt != 0) 1963 list_space += (sizeof (uint32_t) * 1964 (idl.tl_in_cnt - 1)); 1965 1966 idlp = kmem_zalloc(list_space, KM_SLEEP); 1967 bcopy(&idl, idlp, sizeof (idl)); 1968 idlp->tl_out_cnt = 0; 1969 1970 /* 1971 * If target list type is ISCSI_TGT_OID_LIST and discovery 1972 * has not been completed or in progress, poke the discovery 1973 * methods so target information is returned 1974 */ 1975 mutex_enter(&ihp->hba_discovery_events_mutex); 1976 method = ihp->hba_discovery_events; 1977 if ((idl.tl_tgt_list_type == ISCSI_TGT_OID_LIST) && 1978 (method != ISCSI_ALL_DISCOVERY_METHODS) && 1979 (ihp->hba_discovery_in_progress == B_FALSE)) { 1980 ihp->hba_discovery_in_progress = B_TRUE; 1981 mutex_exit(&ihp->hba_discovery_events_mutex); 1982 iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodUnknown); 1983 mutex_enter(&ihp->hba_discovery_events_mutex); 1984 ihp->hba_discovery_in_progress = B_FALSE; 1985 } 1986 mutex_exit(&ihp->hba_discovery_events_mutex); 1987 1988 /* 1989 * Return the correct list information based on the type 1990 */ 1991 switch (idl.tl_tgt_list_type) { 1992 /* ISCSI_TGT_PARAM_OID_LIST - iscsiadm list target-params */ 1993 case ISCSI_TGT_PARAM_OID_LIST: 1994 /* get params from persistent store */ 1995 iscsi_targetparam_lock_list(RW_READER); 1996 curr_entry = iscsi_targetparam_get_next_entry(NULL); 1997 while (curr_entry != NULL) { 1998 if (idlp->tl_out_cnt < idlp->tl_in_cnt) { 1999 idlp->tl_oid_list[idlp->tl_out_cnt] = 2000 curr_entry->target_oid; 2001 } 2002 idlp->tl_out_cnt++; 2003 curr_entry = iscsi_targetparam_get_next_entry( 2004 curr_entry); 2005 } 2006 iscsi_targetparam_unlock_list(); 2007 break; 2008 2009 /* ISCSI_STATIC_TGT_OID_LIST - iscsiadm list static-config */ 2010 case ISCSI_STATIC_TGT_OID_LIST: 2011 { 2012 char *target_name = NULL; 2013 void *v = NULL; 2014 2015 /* get static-config from persistent store */ 2016 target_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 2017 persistent_static_addr_lock(); 2018 while (persistent_static_addr_next(&v, 2019 (char *)target_name, &e) == B_TRUE) { 2020 2021 if (idlp->tl_out_cnt < idlp->tl_in_cnt) { 2022 idlp->tl_oid_list[idlp->tl_out_cnt] = 2023 e.e_oid; 2024 } 2025 idlp->tl_out_cnt++; 2026 2027 } 2028 2029 persistent_static_addr_unlock(); 2030 kmem_free(target_name, ISCSI_MAX_NAME_LEN); 2031 break; 2032 } 2033 2034 /* ISCSI_TGT_OID_LIST - iscsiadm list target */ 2035 case ISCSI_TGT_OID_LIST: 2036 2037 /* get sessions from hba's session list */ 2038 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2039 for (isp = ihp->hba_sess_list; isp; 2040 isp = isp->sess_next) { 2041 2042 if (((isp->sess_state != 2043 ISCSI_SESS_STATE_FREE) || 2044 (isp->sess_discovered_by != 2045 iSCSIDiscoveryMethodUnknown)) && 2046 (isp->sess_type == 2047 ISCSI_SESS_TYPE_NORMAL)) { 2048 if (idlp->tl_out_cnt < 2049 idlp->tl_in_cnt) { 2050 idlp->tl_oid_list[ 2051 idlp->tl_out_cnt] = 2052 isp->sess_oid; 2053 } 2054 idlp->tl_out_cnt++; 2055 } 2056 2057 } 2058 rw_exit(&ihp->hba_sess_list_rwlock); 2059 break; 2060 2061 default: 2062 ASSERT(FALSE); 2063 } 2064 2065 rtn = ddi_copyout(idlp, (caddr_t)arg, list_space, mode); 2066 kmem_free(idlp, list_space); 2067 break; 2068 2069 /* 2070 * ISCSI_TARGET_PROPS_GET - 2071 */ 2072 case ISCSI_TARGET_PROPS_GET: 2073 /* ---- fall through sense the code is almost the same ---- */ 2074 2075 /* 2076 * ISCSI_TARGET_PROPS_SET - 2077 */ 2078 case ISCSI_TARGET_PROPS_SET: 2079 /* copyin user args */ 2080 ipp = (iscsi_property_t *)kmem_alloc(sizeof (*ipp), 2081 KM_SLEEP); 2082 if (ddi_copyin((caddr_t)arg, ipp, sizeof (*ipp), mode)) { 2083 rtn = EFAULT; 2084 kmem_free(ipp, sizeof (*ipp)); 2085 break; 2086 } 2087 2088 if (ipp->p_vers != ISCSI_INTERFACE_VERSION) { 2089 rtn = EINVAL; 2090 kmem_free(ipp, sizeof (*ipp)); 2091 break; 2092 } 2093 2094 rtn = iscsi_target_prop_mod(ihp, ipp, cmd); 2095 if (rtn == 0) 2096 rtn = ddi_copyout(ipp, (caddr_t)arg, 2097 sizeof (*ipp), mode); 2098 kmem_free(ipp, sizeof (*ipp)); 2099 break; 2100 2101 /* 2102 * ISCSI_TARGET_ADDRESS_GET - 2103 */ 2104 case ISCSI_TARGET_ADDRESS_GET: 2105 if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) { 2106 rtn = EFAULT; 2107 break; 2108 } 2109 2110 if (ial.al_vers != ISCSI_INTERFACE_VERSION) { 2111 rtn = EINVAL; 2112 break; 2113 } 2114 2115 /* 2116 * Find out how much space we need to malloc for the users 2117 * request. 2118 */ 2119 list_space = sizeof (iscsi_addr_list_t); 2120 if (ial.al_in_cnt != 0) { 2121 list_space += (sizeof (iscsi_addr_t) * 2122 (ial.al_in_cnt - 1)); 2123 } 2124 ialp = (iscsi_addr_list_t *)kmem_zalloc(list_space, KM_SLEEP); 2125 2126 /* Copy in the header portion */ 2127 bcopy(&ial, ialp, sizeof (ial)); 2128 2129 /* session */ 2130 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2131 rtn = iscsi_sess_get(ialp->al_oid, ihp, &isp); 2132 if (rtn != 0) { 2133 rw_exit(&ihp->hba_sess_list_rwlock); 2134 rtn = EFAULT; 2135 break; 2136 } 2137 2138 ialp->al_out_cnt = 0; 2139 ialp->al_tpgt = isp->sess_tpgt_conf; 2140 rw_enter(&isp->sess_conn_list_rwlock, RW_READER); 2141 for (icp = isp->sess_conn_list; icp; icp = icp->conn_next) { 2142 if (icp->conn_state != ISCSI_CONN_STATE_LOGGED_IN) { 2143 continue; 2144 } 2145 if (ialp->al_out_cnt < ialp->al_in_cnt) { 2146 iscsi_addr_t *ap; 2147 2148 ap = &ialp->al_addrs[ialp->al_out_cnt]; 2149 if (icp->conn_base_addr.sin.sa_family 2150 == AF_INET) { 2151 2152 struct sockaddr_in *addr_in = 2153 (struct sockaddr_in *)&icp-> 2154 conn_base_addr.sin4; 2155 ap->a_addr.i_insize = 2156 sizeof (struct in_addr); 2157 bcopy(&addr_in->sin_addr.s_addr, 2158 &ap->a_addr.i_addr.in4.s_addr, 2159 sizeof (struct in_addr)); 2160 ap->a_port = addr_in->sin_port; 2161 2162 } else { 2163 2164 struct sockaddr_in6 *addr_in6 = 2165 (struct sockaddr_in6 *)&icp-> 2166 conn_base_addr.sin6; 2167 ap->a_addr.i_insize = 2168 sizeof (struct in6_addr); 2169 bcopy(&addr_in6->sin6_addr.s6_addr, 2170 &ap->a_addr.i_addr.in6.s6_addr, 2171 sizeof (struct in6_addr)); 2172 ap->a_port = addr_in6->sin6_port; 2173 2174 } 2175 } 2176 ialp->al_out_cnt++; 2177 } 2178 rw_exit(&isp->sess_conn_list_rwlock); 2179 rw_exit(&ihp->hba_sess_list_rwlock); 2180 2181 rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode); 2182 kmem_free(ialp, list_space); 2183 break; 2184 2185 /* 2186 * ISCSI_CHAP_SET - 2187 */ 2188 case ISCSI_CHAP_SET: 2189 chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap), 2190 KM_SLEEP); 2191 if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) { 2192 rtn = EFAULT; 2193 kmem_free(chap, sizeof (*chap)); 2194 break; 2195 } else if (chap->c_vers != ISCSI_INTERFACE_VERSION) { 2196 rtn = EINVAL; 2197 kmem_free(chap, sizeof (*chap)); 2198 break; 2199 } 2200 2201 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2202 if (chap->c_oid == ihp->hba_oid) 2203 name = ihp->hba_name; 2204 else { 2205 rtn = iscsi_sess_get(chap->c_oid, ihp, &isp); 2206 if (rtn != 0) { 2207 rtn = iscsi_sess_get_by_target( 2208 chap->c_oid, ihp, &isp); 2209 } 2210 2211 /* 2212 * If rtn is zero then we have found an 2213 * existing session. Use the session name to 2214 * do param lookup. If rtn is non-zero then 2215 * create a targetparam object and use its name 2216 * for param lookup. 2217 */ 2218 if (rtn == 0) { 2219 name = isp->sess_name; 2220 } else { 2221 name = 2222 iscsi_targetparam_get_name(chap->c_oid); 2223 rtn = 0; 2224 } 2225 } 2226 2227 if (name == NULL) { 2228 rw_exit( 2229 &ihp->hba_sess_list_rwlock); 2230 rtn = EFAULT; 2231 kmem_free(chap, sizeof (*chap)); 2232 break; 2233 } 2234 2235 if (persistent_chap_set((char *)name, chap) == 2236 B_FALSE) { 2237 rtn = EIO; 2238 } 2239 rw_exit(&ihp->hba_sess_list_rwlock); 2240 kmem_free(chap, sizeof (*chap)); 2241 break; 2242 2243 /* 2244 * ISCSI_CHAP_GET - 2245 */ 2246 case ISCSI_CHAP_GET: 2247 chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap), 2248 KM_SLEEP); 2249 if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) { 2250 kmem_free(chap, sizeof (*chap)); 2251 rtn = EFAULT; 2252 break; 2253 } else if (chap->c_vers != ISCSI_INTERFACE_VERSION) { 2254 kmem_free(chap, sizeof (*chap)); 2255 rtn = EINVAL; 2256 break; 2257 } 2258 2259 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2260 if (chap->c_oid == ihp->hba_oid) 2261 name = ihp->hba_name; 2262 else { 2263 rtn = iscsi_sess_get(chap->c_oid, ihp, &isp); 2264 if (rtn != 0) { 2265 rtn = iscsi_sess_get_by_target( 2266 chap->c_oid, ihp, &isp); 2267 } 2268 2269 /* 2270 * If rtn is zero then we have found an 2271 * existing session. Use the session name to 2272 * do param lookup. If rtn is non-zero then 2273 * create a targetparam object and use its name 2274 * for param lookup. 2275 */ 2276 if (rtn == 0) { 2277 name = isp->sess_name; 2278 } else { 2279 rtn = 0; 2280 name = 2281 iscsi_targetparam_get_name(chap->c_oid); 2282 } 2283 2284 if (name == NULL) { 2285 rw_exit(&ihp->hba_sess_list_rwlock); 2286 rtn = EFAULT; 2287 break; 2288 } 2289 /* 2290 * Initialize the target-side chap name to the 2291 * session name if no chap settings have been 2292 * saved for the current session. 2293 */ 2294 if (persistent_chap_get((char *)name, 2295 chap) == B_FALSE) { 2296 int name_len = strlen((char *)name); 2297 iscsi_chap_props_t *chap = NULL; 2298 chap = (iscsi_chap_props_t *)kmem_zalloc 2299 (sizeof (iscsi_chap_props_t), KM_SLEEP); 2300 bcopy((char *)name, chap->c_user, name_len); 2301 chap->c_user_len = name_len; 2302 (void) (persistent_chap_set((char *)name, 2303 chap)); 2304 kmem_free(chap, sizeof (*chap)); 2305 } 2306 } 2307 2308 if (name == NULL) { 2309 rw_exit( 2310 &ihp->hba_sess_list_rwlock); 2311 rtn = EFAULT; 2312 break; 2313 } 2314 2315 if (persistent_chap_get((char *)name, chap) == B_FALSE) { 2316 rw_exit(&ihp->hba_sess_list_rwlock); 2317 rtn = EIO; 2318 break; 2319 } 2320 rw_exit(&ihp->hba_sess_list_rwlock); 2321 2322 rtn = ddi_copyout(chap, (caddr_t)arg, sizeof (*chap), mode); 2323 kmem_free(chap, sizeof (*chap)); 2324 break; 2325 2326 /* 2327 * ISCSI_CHAP_CLEAR - 2328 */ 2329 case ISCSI_CHAP_CLEAR: 2330 chap = (iscsi_chap_props_t *)kmem_zalloc(sizeof (*chap), 2331 KM_SLEEP); 2332 if (ddi_copyin((caddr_t)arg, chap, sizeof (*chap), mode)) { 2333 rtn = EFAULT; 2334 kmem_free(chap, sizeof (*chap)); 2335 break; 2336 } else if (chap->c_vers != ISCSI_INTERFACE_VERSION) { 2337 rtn = EINVAL; 2338 kmem_free(chap, sizeof (*chap)); 2339 break; 2340 } 2341 2342 if (chap->c_oid == ihp->hba_oid) { 2343 iscsi_sess_t *sessp; 2344 2345 name = ihp->hba_name; 2346 2347 if (persistent_chap_clear( 2348 (char *)name) == B_FALSE) { 2349 rtn = EIO; 2350 } 2351 2352 /* 2353 * Loop through all sessions and memset their 2354 * (initiator's) passwords 2355 */ 2356 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2357 for (sessp = ihp->hba_sess_list; sessp; 2358 sessp = sessp->sess_next) { 2359 (void) memset(sessp->sess_auth.password, 2360 0, iscsiAuthStringMaxLength); 2361 sessp->sess_auth.password_length = 0; 2362 } 2363 rw_exit(&ihp->hba_sess_list_rwlock); 2364 2365 } else { 2366 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 2367 /* 2368 * If the oid does represent a session check to see 2369 * if it is a target oid. If so, return the target's 2370 * associated session. 2371 */ 2372 rtn = iscsi_sess_get(chap->c_oid, ihp, &isp); 2373 if (rtn != 0) { 2374 rtn = iscsi_sess_get_by_target(chap->c_oid, 2375 ihp, &isp); 2376 } 2377 2378 rw_exit(&ihp->hba_sess_list_rwlock); 2379 2380 /* 2381 * If rtn is zero then we have found an 2382 * existing session. Use the session name to 2383 * do param lookup. If rtn is non-zero then 2384 * create a targetparam object and use its name 2385 * for param lookup. 2386 */ 2387 if (rtn == 0) { 2388 name = isp->sess_name; 2389 } else { 2390 name = 2391 iscsi_targetparam_get_name(chap->c_oid); 2392 rtn = 0; 2393 } 2394 2395 if (name == NULL) { 2396 rw_exit( 2397 &ihp->hba_sess_list_rwlock); 2398 rtn = EFAULT; 2399 break; 2400 } 2401 2402 if (persistent_chap_clear( 2403 (char *)name) == B_FALSE) { 2404 rtn = EIO; 2405 } 2406 2407 /* 2408 * Clear out session chap password if we found a 2409 * session above. 2410 */ 2411 if (isp != NULL) { 2412 (void) memset(isp->sess_auth.password_in, 2413 0, iscsiAuthStringMaxLength); 2414 isp->sess_auth.password_length_in = 0; 2415 } 2416 2417 } 2418 2419 kmem_free(chap, sizeof (*chap)); 2420 break; 2421 2422 /* 2423 * ISCSI_STATIC_GET - 2424 */ 2425 case ISCSI_STATIC_GET: 2426 ispp = (iscsi_static_property_t *)kmem_alloc( 2427 sizeof (*ispp), KM_SLEEP); 2428 2429 if (ddi_copyin((caddr_t)arg, ispp, sizeof (*ispp), mode)) { 2430 rtn = EFAULT; 2431 kmem_free(ispp, sizeof (*ispp)); 2432 break; 2433 } 2434 2435 if (ispp->p_vers != ISCSI_INTERFACE_VERSION) { 2436 rtn = EINVAL; 2437 kmem_free(ispp, sizeof (*ispp)); 2438 break; 2439 } 2440 2441 { 2442 void *v = NULL; 2443 boolean_t found = B_FALSE; 2444 2445 persistent_static_addr_lock(); 2446 while (persistent_static_addr_next(&v, 2447 (char *)ispp->p_name, &e) == B_TRUE) { 2448 2449 if (ispp->p_oid == e.e_oid) { 2450 /* 2451 * In case there are multiple 2452 * addresses associated with the 2453 * given target OID, pick the first 2454 * one. 2455 */ 2456 iscsi_addr_t *ap; 2457 2458 ap = &(ispp->p_addr_list.al_addrs[0]); 2459 ap->a_port = e.e_port; 2460 ap->a_addr.i_insize = e.e_insize; 2461 bcopy(e.e_u.u_in6.s6_addr, 2462 ap->a_addr.i_addr.in6.s6_addr, 2463 e.e_insize); 2464 ispp->p_name_len = 2465 strlen((char *)ispp->p_name); 2466 ispp->p_addr_list.al_tpgt = e.e_tpgt; 2467 ispp->p_addr_list.al_out_cnt = 1; 2468 2469 found = B_TRUE; 2470 break; 2471 } 2472 } 2473 persistent_static_addr_unlock(); 2474 2475 if (found == B_TRUE) { 2476 rtn = ddi_copyout(ispp, (caddr_t)arg, 2477 sizeof (*ispp), mode); 2478 } else { 2479 rtn = ENOENT; 2480 } 2481 } 2482 kmem_free(ispp, sizeof (*ispp)); 2483 2484 break; 2485 2486 /* 2487 * ISCSI_STATIC_SET - 2488 */ 2489 case ISCSI_STATIC_SET: 2490 target = iscsi_ioctl_copyin((caddr_t)arg, mode, 2491 sizeof (*target)); 2492 if (target == NULL) { 2493 rtn = EFAULT; 2494 break; 2495 } 2496 2497 if (target->te_entry.e_vers != ISCSI_INTERFACE_VERSION) { 2498 kmem_free(target, sizeof (*target)); 2499 rtn = EINVAL; 2500 break; 2501 } 2502 2503 /* Check if the target's already been added */ 2504 { 2505 boolean_t static_target_found = B_FALSE; 2506 void *v = NULL; 2507 2508 name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 2509 persistent_static_addr_lock(); 2510 while (persistent_static_addr_next(&v, (char *)name, 2511 &e) == B_TRUE) { 2512 /* 2513 * MC/S - Need to check IP address and port 2514 * number as well when we support MC/S. 2515 */ 2516 if ((strncmp((char *)name, 2517 (char *)target->te_name, 2518 ISCSI_MAX_NAME_LEN) == 0) && 2519 (target->te_entry.e_tpgt == e.e_tpgt) && 2520 (target->te_entry.e_insize == e.e_insize) && 2521 (bcmp(&target->te_entry.e_u, &e.e_u, 2522 e.e_insize) == 0)) { 2523 /* 2524 * We don't allow MC/S for now but 2525 * we do allow adding the same target 2526 * with different TPGTs (hence, 2527 * different sessions). 2528 */ 2529 static_target_found = B_TRUE; 2530 break; 2531 } 2532 } 2533 persistent_static_addr_unlock(); 2534 kmem_free(name, ISCSI_MAX_NAME_LEN); 2535 2536 if (static_target_found == B_TRUE) { 2537 /* Duplicate entry */ 2538 kmem_free(target, sizeof (*target)); 2539 rtn = EEXIST; 2540 break; 2541 } 2542 } 2543 2544 if (target->te_entry.e_oid == ISCSI_OID_NOTSET) { 2545 mutex_enter(&iscsi_oid_mutex); 2546 target->te_entry.e_oid = iscsi_oid++; 2547 mutex_exit(&iscsi_oid_mutex); 2548 } 2549 2550 persistent_static_addr_lock(); 2551 if (persistent_static_addr_set((char *)target->te_name, 2552 &target->te_entry) == B_FALSE) { 2553 persistent_static_addr_unlock(); 2554 kmem_free(target, sizeof (*target)); 2555 rtn = EIO; 2556 break; 2557 } 2558 persistent_static_addr_unlock(); 2559 2560 /* 2561 * If Static Targets discovery is enabled, then add 2562 * target to discovery queue. Otherwise, just create 2563 * the session for potential future use. 2564 */ 2565 method = persistent_disc_meth_get(); 2566 if (method & iSCSIDiscoveryMethodStatic) { 2567 iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodStatic); 2568 (void) iscsid_login_tgt(ihp, (char *)target->te_name, 2569 iSCSIDiscoveryMethodStatic, NULL); 2570 } 2571 2572 rtn = iscsi_ioctl_copyout(target, sizeof (*target), 2573 (caddr_t)arg, mode); 2574 break; 2575 2576 /* 2577 * ISCSI_STATIC_CLEAR - 2578 */ 2579 case ISCSI_STATIC_CLEAR: 2580 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 2581 rtn = EFAULT; 2582 break; 2583 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 2584 rtn = EINVAL; 2585 break; 2586 } 2587 2588 { 2589 boolean_t found = B_FALSE; 2590 void *v = NULL; 2591 entry_t tmp_e; 2592 char *name = NULL; 2593 2594 name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 2595 2596 /* Find name for matching static_tgt oid */ 2597 persistent_static_addr_lock(); 2598 while (persistent_static_addr_next(&v, 2599 (char *)name, &tmp_e) == B_TRUE) { 2600 if (e.e_oid == tmp_e.e_oid) { 2601 found = B_TRUE; 2602 break; 2603 } 2604 } 2605 2606 /* If static_tgt found logout and remove it */ 2607 if (found == B_TRUE) { 2608 2609 iscsid_addr_to_sockaddr(tmp_e.e_insize, 2610 &tmp_e.e_u, tmp_e.e_port, &addr_dsc.sin); 2611 2612 /* Attempt to logout of target */ 2613 if (iscsid_del(ihp, (char *)name, 2614 iSCSIDiscoveryMethodStatic, &addr_dsc.sin) 2615 == B_TRUE) { 2616 persistent_static_addr_unlock(); 2617 2618 /* remove from persistent store */ 2619 if (persistent_static_addr_clear( 2620 e.e_oid) == B_FALSE) { 2621 rtn = EIO; 2622 } 2623 2624 iscsid_poke_discovery(ihp, 2625 iSCSIDiscoveryMethodStatic); 2626 (void) iscsid_login_tgt(ihp, 2627 (char *)name, 2628 iSCSIDiscoveryMethodStatic, 2629 NULL); 2630 2631 } else { 2632 persistent_static_addr_unlock(); 2633 rtn = EBUSY; 2634 } 2635 } else { 2636 persistent_static_addr_unlock(); 2637 rtn = EIO; 2638 } 2639 kmem_free(name, ISCSI_MAX_NAME_LEN); 2640 } 2641 break; 2642 2643 /* 2644 * ISCSI_ISNS_SERVER_ADDR_SET: 2645 */ 2646 case ISCSI_ISNS_SERVER_ADDR_SET: 2647 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 2648 rtn = EFAULT; 2649 break; 2650 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 2651 rtn = EINVAL; 2652 break; 2653 } 2654 2655 if (persistent_isns_addr_set(&e) == B_FALSE) { 2656 rtn = EIO; 2657 break; 2658 } 2659 2660 /* 2661 * If iSNS server discovery is enabled, then kickoff 2662 * discovery of the targets advertised by the recently 2663 * added iSNS server address. 2664 */ 2665 method = persistent_disc_meth_get(); 2666 if (method & iSCSIDiscoveryMethodISNS) { 2667 initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, 2668 KM_SLEEP); 2669 if (persistent_initiator_name_get(initiator_node_name, 2670 ISCSI_MAX_NAME_LEN) != B_TRUE) { 2671 kmem_free(initiator_node_name, 2672 ISCSI_MAX_NAME_LEN); 2673 initiator_node_name = NULL; 2674 rtn = EIO; 2675 break; 2676 } 2677 if (strlen(initiator_node_name) == 0) { 2678 kmem_free(initiator_node_name, 2679 ISCSI_MAX_NAME_LEN); 2680 initiator_node_name = NULL; 2681 rtn = EIO; 2682 break; 2683 } 2684 2685 initiator_node_alias = kmem_zalloc(ISCSI_MAX_NAME_LEN, 2686 KM_SLEEP); 2687 if (persistent_alias_name_get(initiator_node_alias, 2688 ISCSI_MAX_NAME_LEN) != B_TRUE) { 2689 initiator_node_alias[0] = '\0'; 2690 } 2691 2692 /* 2693 * Register this initiator node against this iSNS 2694 * server. 2695 */ 2696 (void) isns_reg_one_server(&e, ihp->hba_isid, 2697 (uint8_t *)initiator_node_name, 2698 ISCSI_MAX_NAME_LEN, 2699 (uint8_t *)initiator_node_alias, 2700 ISCSI_MAX_NAME_LEN, 2701 ISNS_INITIATOR_NODE_TYPE, 2702 isns_scn_callback); 2703 2704 iscsid_do_isns_query_one_server(ihp, &e); 2705 2706 iscsid_addr_to_sockaddr(e.e_insize, 2707 &e.e_u, e.e_port, &addr_dsc.sin); 2708 2709 (void) iscsid_login_tgt(ihp, NULL, 2710 iSCSIDiscoveryMethodISNS, 2711 &addr_dsc.sin); 2712 2713 /* Done using the name and alias - free them. */ 2714 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 2715 initiator_node_name = NULL; 2716 kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN); 2717 initiator_node_alias = NULL; 2718 } 2719 break; 2720 2721 /* 2722 * ISCSI_DISCOVERY_ADDR_SET: 2723 */ 2724 case ISCSI_DISCOVERY_ADDR_SET: 2725 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 2726 rtn = EFAULT; 2727 break; 2728 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 2729 rtn = EINVAL; 2730 break; 2731 } 2732 2733 if (e.e_oid == ISCSI_OID_NOTSET) { 2734 mutex_enter(&iscsi_oid_mutex); 2735 e.e_oid = iscsi_oid++; 2736 mutex_exit(&iscsi_oid_mutex); 2737 } 2738 2739 if (persistent_disc_addr_set(&e) == B_FALSE) { 2740 rtn = EIO; 2741 break; 2742 } 2743 2744 /* 2745 * If Send Targets discovery is enabled, then kickoff 2746 * discovery of the targets advertised by the recently 2747 * added discovery address. 2748 */ 2749 method = persistent_disc_meth_get(); 2750 if (method & iSCSIDiscoveryMethodSendTargets) { 2751 2752 iscsid_addr_to_sockaddr(e.e_insize, 2753 &e.e_u, e.e_port, &addr_dsc.sin); 2754 iscsid_do_sendtgts(&e); 2755 (void) iscsid_login_tgt(ihp, NULL, 2756 iSCSIDiscoveryMethodSendTargets, 2757 &addr_dsc.sin); 2758 2759 } 2760 break; 2761 2762 /* 2763 * ISCSI_DISCOVERY_ADDR_LIST_GET 2764 */ 2765 case ISCSI_DISCOVERY_ADDR_LIST_GET: 2766 /* copyin user args */ 2767 if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) { 2768 rtn = EFAULT; 2769 break; 2770 } 2771 2772 if (ial.al_vers != ISCSI_INTERFACE_VERSION) { 2773 rtn = EINVAL; 2774 break; 2775 } 2776 2777 list_space = sizeof (iscsi_addr_list_t); 2778 if (ial.al_in_cnt != 0) { 2779 list_space += (sizeof (iscsi_addr_t) * 2780 (ial.al_in_cnt - 1)); 2781 } 2782 2783 ialp = kmem_zalloc(list_space, KM_SLEEP); 2784 bcopy(&ial, ialp, sizeof (iscsi_addr_list_t)); 2785 2786 void_p = NULL; 2787 ialp->al_out_cnt = 0; 2788 persistent_disc_addr_lock(); 2789 while (persistent_disc_addr_next(&void_p, &e) == B_TRUE) { 2790 if (ialp->al_out_cnt < ialp->al_in_cnt) { 2791 int i = ialp->al_out_cnt; 2792 iscsi_addr_t *addr = &ialp->al_addrs[i]; 2793 2794 addr->a_port = e.e_port; 2795 addr->a_addr.i_insize = e.e_insize; 2796 addr->a_oid = e.e_oid; 2797 2798 if (e.e_insize == sizeof (struct in_addr)) { 2799 /* IPv4 */ 2800 addr->a_addr.i_addr.in4.s_addr = 2801 e.e_u.u_in4.s_addr; 2802 } else if (e.e_insize == 2803 sizeof (struct in6_addr)) { 2804 /* IPv6 */ 2805 bcopy(e.e_u.u_in6.s6_addr, 2806 addr->a_addr.i_addr.in6.s6_addr, 2807 16); 2808 } 2809 } 2810 ialp->al_out_cnt++; 2811 } 2812 persistent_disc_addr_unlock(); 2813 2814 rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode); 2815 kmem_free(ialp, list_space); 2816 break; 2817 2818 /* 2819 * ISCSI_ISNS_SERVER_ADDR_LIST_GET 2820 */ 2821 case ISCSI_ISNS_SERVER_ADDR_LIST_GET: 2822 /* copyin user args */ 2823 if (ddi_copyin((caddr_t)arg, &ial, sizeof (ial), mode)) { 2824 rtn = EFAULT; 2825 break; 2826 } 2827 2828 if (ial.al_vers != ISCSI_INTERFACE_VERSION) { 2829 rtn = EINVAL; 2830 break; 2831 } 2832 2833 list_space = sizeof (iscsi_addr_list_t); 2834 if (ial.al_in_cnt != 0) { 2835 list_space += (sizeof (iscsi_addr_t) * 2836 (ial.al_in_cnt - 1)); 2837 } 2838 2839 ialp = kmem_zalloc(list_space, KM_SLEEP); 2840 bcopy(&ial, ialp, sizeof (iscsi_addr_list_t)); 2841 2842 void_p = NULL; 2843 ialp->al_out_cnt = 0; 2844 persistent_isns_addr_lock(); 2845 while (persistent_isns_addr_next(&void_p, &e) == B_TRUE) { 2846 if (ialp->al_out_cnt < ialp->al_in_cnt) { 2847 int i = ialp->al_out_cnt; 2848 iscsi_addr_t *addr = &ialp->al_addrs[i]; 2849 2850 addr->a_port = e.e_port; 2851 addr->a_addr.i_insize = e.e_insize; 2852 if (e.e_insize == sizeof (struct in_addr)) { 2853 /* IPv4 */ 2854 addr->a_addr.i_addr.in4.s_addr = 2855 e.e_u.u_in4.s_addr; 2856 } else if (e.e_insize == 2857 sizeof (struct in6_addr)) { 2858 /* IPv6 */ 2859 bcopy(e.e_u.u_in6.s6_addr, 2860 addr->a_addr.i_addr.in6.s6_addr, 2861 16); 2862 } 2863 } 2864 ialp->al_out_cnt++; 2865 } 2866 persistent_isns_addr_unlock(); 2867 2868 rtn = ddi_copyout(ialp, (caddr_t)arg, list_space, mode); 2869 kmem_free(ialp, list_space); 2870 break; 2871 2872 /* 2873 * ISCSI_DISCOVERY_ADDR_CLEAR: 2874 */ 2875 case ISCSI_DISCOVERY_ADDR_CLEAR: 2876 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 2877 rtn = EFAULT; 2878 break; 2879 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 2880 rtn = EINVAL; 2881 break; 2882 } 2883 2884 iscsid_addr_to_sockaddr(e.e_insize, 2885 &e.e_u, e.e_port, &addr_dsc.sin); 2886 2887 /* Attempt to logout of associated targets */ 2888 if (iscsid_del(ihp, NULL, 2889 iSCSIDiscoveryMethodSendTargets, &addr_dsc.sin) == 2890 B_TRUE) { 2891 /* Logout successful remove disc. addr. */ 2892 if (persistent_disc_addr_clear(&e) == B_FALSE) { 2893 rtn = EIO; 2894 } 2895 } else { 2896 rtn = EBUSY; 2897 } 2898 break; 2899 2900 /* 2901 * ISCSI_ISNS_SERVER_CLEAR: 2902 */ 2903 case ISCSI_ISNS_SERVER_ADDR_CLEAR: 2904 if (ddi_copyin((caddr_t)arg, &e, sizeof (e), mode)) { 2905 rtn = EFAULT; 2906 break; 2907 } else if (e.e_vers != ISCSI_INTERFACE_VERSION) { 2908 rtn = EINVAL; 2909 break; 2910 } 2911 2912 iscsid_addr_to_sockaddr(e.e_insize, 2913 &e.e_u, e.e_port, &addr_dsc.sin); 2914 2915 /* Attempt logout of associated targets */ 2916 if (iscsid_del(ihp, NULL, iSCSIDiscoveryMethodISNS, 2917 &addr_dsc.sin) == B_TRUE) { 2918 /* Logout successful */ 2919 2920 if (persistent_isns_addr_clear(&e) == B_FALSE) { 2921 rtn = EIO; 2922 break; 2923 } 2924 2925 method = persistent_disc_meth_get(); 2926 if (method & iSCSIDiscoveryMethodISNS) { 2927 boolean_t is_last_isns_server_b = 2928 B_FALSE; 2929 int isns_server_count = 0; 2930 void *void_p = NULL; 2931 2932 /* 2933 * Check if the last iSNS server's been 2934 * removed. 2935 */ 2936 { 2937 entry_t tmp_e; 2938 persistent_isns_addr_lock(); 2939 while (persistent_isns_addr_next( 2940 &void_p, &tmp_e) == B_TRUE) { 2941 isns_server_count++; 2942 } 2943 } 2944 persistent_isns_addr_unlock(); 2945 if (isns_server_count == 0) { 2946 is_last_isns_server_b = B_TRUE; 2947 } 2948 2949 /* 2950 * Deregister this node from this iSNS 2951 * server. 2952 */ 2953 initiator_node_name = kmem_zalloc( 2954 ISCSI_MAX_NAME_LEN, KM_SLEEP); 2955 if (persistent_initiator_name_get( 2956 initiator_node_name, 2957 ISCSI_MAX_NAME_LEN) == B_TRUE) { 2958 2959 if (strlen(initiator_node_name) > 0) { 2960 (void) isns_dereg_one_server( 2961 &e, (uint8_t *) 2962 initiator_node_name, 2963 is_last_isns_server_b); 2964 } 2965 } 2966 kmem_free(initiator_node_name, 2967 ISCSI_MAX_NAME_LEN); 2968 initiator_node_name = NULL; 2969 } 2970 } else { 2971 rtn = EBUSY; 2972 } 2973 break; 2974 2975 /* 2976 * ISCSI_DISCOVERY_SET - 2977 */ 2978 case ISCSI_DISCOVERY_SET: 2979 if (ddi_copyin((caddr_t)arg, &method, sizeof (method), mode)) { 2980 rtn = EFAULT; 2981 break; 2982 } 2983 2984 if (persistent_disc_meth_set(method) == B_FALSE) { 2985 rtn = EIO; 2986 } else { 2987 (void) iscsid_enable_discovery(ihp, method, B_FALSE); 2988 iscsid_poke_discovery(ihp, method); 2989 (void) iscsid_login_tgt(ihp, NULL, method, NULL); 2990 } 2991 break; 2992 2993 /* 2994 * ISCSI_DISCOVERY_GET - 2995 */ 2996 case ISCSI_DISCOVERY_GET: 2997 method = persistent_disc_meth_get(); 2998 rtn = ddi_copyout(&method, (caddr_t)arg, 2999 sizeof (method), mode); 3000 break; 3001 3002 /* 3003 * ISCSI_DISCOVERY_CLEAR - 3004 */ 3005 #define ISCSI_DISCOVERY_DELAY 2 /* seconds */ 3006 case ISCSI_DISCOVERY_CLEAR: 3007 if (ddi_copyin((caddr_t)arg, &method, sizeof (method), mode)) { 3008 rtn = EFAULT; 3009 break; 3010 } 3011 3012 /* If discovery in progress, try few times before return busy */ 3013 retry = 0; 3014 mutex_enter(&ihp->hba_discovery_events_mutex); 3015 while (ihp->hba_discovery_in_progress == B_TRUE) { 3016 if (++retry == 5) { 3017 rtn = EBUSY; 3018 break; 3019 } 3020 mutex_exit(&ihp->hba_discovery_events_mutex); 3021 delay(SEC_TO_TICK(ISCSI_DISCOVERY_DELAY)); 3022 mutex_enter(&ihp->hba_discovery_events_mutex); 3023 } 3024 #undef ISCSI_DISCOVERY_DELAY 3025 3026 /* 3027 * Clear discovery first, so that any bus config or 3028 * discovery requests will ignore this discovery method 3029 */ 3030 if (rtn == 0 && persistent_disc_meth_clear(method) == B_FALSE) { 3031 rtn = EIO; 3032 } 3033 mutex_exit(&ihp->hba_discovery_events_mutex); 3034 3035 if (rtn != 0) { 3036 break; 3037 } 3038 3039 /* Attempt to logout from all associated targets */ 3040 if (iscsid_disable_discovery(ihp, method) == B_FALSE) { 3041 /* Failure!, reset the discovery */ 3042 if (persistent_disc_meth_set(method) == B_FALSE) { 3043 cmn_err(CE_WARN, "Failed to reset discovery " 3044 "method after discovery disable failure."); 3045 } 3046 rtn = EBUSY; 3047 } 3048 break; 3049 3050 /* 3051 * ISCSI_DISCOVERY_PROPS - 3052 */ 3053 case ISCSI_DISCOVERY_PROPS: 3054 iscsid_props(&discovery_props); 3055 if (ddi_copyout(&discovery_props, (caddr_t)arg, 3056 sizeof (discovery_props), mode)) 3057 rtn = EFAULT; 3058 break; 3059 3060 /* 3061 * ISCSI_LUN_OID_LIST -- 3062 */ 3063 case ISCSI_LUN_OID_LIST_GET: 3064 ll = (iscsi_lun_list_t *)kmem_alloc(sizeof (*ll), KM_SLEEP); 3065 if (ddi_copyin((caddr_t)arg, ll, sizeof (*ll), mode)) { 3066 rtn = EFAULT; 3067 kmem_free(ll, sizeof (*ll)); 3068 break; 3069 } 3070 3071 if (ll->ll_vers != ISCSI_INTERFACE_VERSION) { 3072 rtn = EINVAL; 3073 kmem_free(ll, sizeof (*ll)); 3074 break; 3075 } 3076 3077 /* 3078 * Find out how much space the user has allocated in their 3079 * structure. Match the same space for our structure. 3080 */ 3081 lun_sz = sizeof (iscsi_lun_list_t); 3082 if (ll->ll_in_cnt > 0) { 3083 lun_sz += (ll->ll_in_cnt - 1) * sizeof (iscsi_if_lun_t); 3084 } 3085 3086 llp = kmem_zalloc(lun_sz, KM_SLEEP); 3087 bcopy(ll, llp, sizeof (*ll)); 3088 kmem_free(ll, sizeof (*ll)); 3089 3090 /* 3091 * Check to see if oid references a target-param oid. If so, 3092 * find the associated session oid before getting lu list. 3093 */ 3094 if (iscsi_targetparam_get_name(llp->ll_tgt_oid) != NULL) { 3095 for (isp = ihp->hba_sess_list; isp; 3096 isp = isp->sess_next) { 3097 if (isp->sess_target_oid == llp->ll_tgt_oid) { 3098 target_oid = isp->sess_oid; 3099 break; 3100 } 3101 } 3102 } else { 3103 target_oid = llp->ll_tgt_oid; 3104 } 3105 3106 3107 /* 3108 * Look at the LUNs attached to the specified target. If there 3109 * is space in the user structure save that information locally. 3110 * Always add up the count to the total. By always adding 3111 * the count this code can be used if ll_in_cnt == 0 and 3112 * the user just wishes to know the appropriate size to 3113 * allocate. 3114 */ 3115 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3116 for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) { 3117 if ((llp->ll_all_tgts == B_FALSE) && 3118 (isp->sess_oid != target_oid)) { 3119 continue; 3120 } 3121 rw_enter(&isp->sess_lun_list_rwlock, RW_READER); 3122 for (ilp = isp->sess_lun_list; ilp; 3123 ilp = ilp->lun_next) { 3124 if (ilp->lun_state == 3125 ISCSI_LUN_STATE_ONLINE) { 3126 if (llp->ll_out_cnt < 3127 llp->ll_in_cnt) { 3128 iscsi_if_lun_t *lp; 3129 lp = &llp->ll_luns[ 3130 llp->ll_out_cnt]; 3131 3132 lp->l_tgt_oid = 3133 isp->sess_oid; 3134 lp->l_oid = ilp->lun_oid; 3135 lp->l_num = ilp->lun_num; 3136 } 3137 llp->ll_out_cnt++; 3138 } 3139 } 3140 rw_exit(&isp->sess_lun_list_rwlock); 3141 } 3142 rw_exit(&ihp->hba_sess_list_rwlock); 3143 3144 if (ddi_copyout(llp, (caddr_t)arg, lun_sz, mode)) { 3145 rtn = EFAULT; 3146 } 3147 3148 kmem_free(llp, lun_sz); 3149 break; 3150 3151 /* 3152 * ISCSI_LUN_PROPS_GET -- 3153 */ 3154 case ISCSI_LUN_PROPS_GET: 3155 lun = (iscsi_lun_props_t *)kmem_zalloc(sizeof (*lun), KM_SLEEP); 3156 if (ddi_copyin((caddr_t)arg, lun, sizeof (*lun), mode)) { 3157 rtn = EFAULT; 3158 kmem_free(lun, sizeof (*lun)); 3159 break; 3160 } 3161 3162 if (lun->lp_vers != ISCSI_INTERFACE_VERSION) { 3163 rtn = EINVAL; 3164 kmem_free(lun, sizeof (*lun)); 3165 break; 3166 } 3167 3168 /* 3169 * For the target specified, find the LUN specified and 3170 * return its properties 3171 */ 3172 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3173 rtn = iscsi_sess_get(lun->lp_tgt_oid, ihp, &isp); 3174 if (rtn != 0) { 3175 rw_exit(&ihp->hba_sess_list_rwlock); 3176 rtn = EFAULT; 3177 kmem_free(lun, sizeof (*lun)); 3178 break; 3179 } 3180 rtn = EINVAL; /* Set bad rtn, correct only if found */ 3181 rw_enter(&isp->sess_lun_list_rwlock, RW_READER); 3182 for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) { 3183 if (ilp->lun_oid == lun->lp_oid) { 3184 lun->lp_num = ilp->lun_num; 3185 lun->lp_status = LunValid; 3186 lun->lp_time_online = ilp->lun_time_online; 3187 3188 if (ilp->lun_pip != NULL) { 3189 lun_dip = mdi_pi_get_client( 3190 ilp->lun_pip); 3191 } else { 3192 lun_dip = ilp->lun_dip; 3193 } 3194 3195 if (lun_dip != NULL && 3196 ((i_ddi_devi_attached(lun_dip)) || 3197 (ddi_get_devstate(lun_dip) == 3198 DDI_DEVSTATE_UP))) { 3199 (void) ddi_pathname(lun_dip, 3200 lun->lp_pathname); 3201 } else { 3202 /* 3203 * The LUN is not exported to the 3204 * OS yet. It is in the process 3205 * of being added. 3206 */ 3207 lun->lp_status = LunDoesNotExist; 3208 } 3209 bcopy(ilp->lun_vid, lun->lp_vid, 3210 sizeof (lun->lp_vid)); 3211 bcopy(ilp->lun_pid, lun->lp_pid, 3212 sizeof (lun->lp_pid)); 3213 rtn = ddi_copyout(lun, (caddr_t)arg, 3214 sizeof (*lun), mode); 3215 if (rtn == -1) { 3216 rtn = EFAULT; 3217 } 3218 break; 3219 } 3220 } 3221 rw_exit(&isp->sess_lun_list_rwlock); 3222 rw_exit(&ihp->hba_sess_list_rwlock); 3223 3224 kmem_free(lun, sizeof (*lun)); 3225 break; 3226 3227 /* 3228 * ISCSI_CONN_OID_LIST_GET -- 3229 */ 3230 #define ISCSIIOCOLGC iscsi_ioctl_conn_oid_list_get_copyout 3231 case ISCSI_CONN_OID_LIST_GET: 3232 { 3233 iscsi_conn_list_t *cl; 3234 3235 /* Asuume the worst */ 3236 rtn = EFAULT; 3237 3238 /* Copy the input argument into kernel world. */ 3239 cl = iscsi_ioctl_conn_oid_list_get_copyin( 3240 (caddr_t)arg, 3241 mode); 3242 if (cl != NULL) { 3243 if (iscsi_ioctl_conn_oid_list_get(ihp, cl) == 3244 B_TRUE) { 3245 rtn = 3246 ISCSIIOCOLGC( 3247 cl, (caddr_t)arg, mode); 3248 } 3249 } 3250 break; 3251 } 3252 #undef ISCSIIOCOLGC 3253 /* 3254 * ISCSI_CONN_OID_LIST_GET -- 3255 */ 3256 case ISCSI_CONN_PROPS_GET: 3257 { 3258 iscsi_conn_props_t *cp; 3259 3260 /* Asuume the worst */ 3261 rtn = EFAULT; 3262 3263 /* Copy the input argument into kernel world. */ 3264 cp = iscsi_ioctl_copyin( 3265 (caddr_t)arg, 3266 mode, 3267 sizeof (iscsi_conn_props_t)); 3268 3269 if (cp != NULL) { 3270 /* Get the propereties. */ 3271 if (iscsi_ioctl_conn_props_get(ihp, cp) == 3272 B_TRUE) { 3273 rtn = 3274 iscsi_ioctl_copyout( 3275 cp, 3276 sizeof (*cp), 3277 (caddr_t)arg, 3278 mode); 3279 } else { 3280 kmem_free(cp, sizeof (*cp)); 3281 cp = NULL; 3282 } 3283 } 3284 break; 3285 } 3286 3287 /* 3288 * ISCSI_RADIUS_GET - 3289 */ 3290 case ISCSI_RADIUS_GET: 3291 { 3292 iscsi_nvfile_status_t status; 3293 3294 radius = (iscsi_radius_props_t *)kmem_zalloc(sizeof (*radius), 3295 KM_SLEEP); 3296 if (ddi_copyin((caddr_t)arg, radius, sizeof (*radius), mode)) { 3297 kmem_free(radius, sizeof (*radius)); 3298 rtn = EFAULT; 3299 break; 3300 } else if (radius->r_vers != ISCSI_INTERFACE_VERSION) { 3301 kmem_free(radius, sizeof (*radius)); 3302 rtn = EINVAL; 3303 break; 3304 } 3305 3306 old_oid = radius->r_oid; 3307 3308 if (radius->r_oid == ihp->hba_oid) { 3309 name = ihp->hba_name; 3310 } else { 3311 /* 3312 * RADIUS configuration should be done on a per 3313 * initiator basis. 3314 */ 3315 kmem_free(radius, sizeof (*radius)); 3316 rtn = EINVAL; 3317 break; 3318 } 3319 3320 status = persistent_radius_get(radius); 3321 if (status == ISCSI_NVFILE_SUCCESS) { 3322 /* 3323 * Restore the value for overridden (and bogus) oid. 3324 */ 3325 radius->r_oid = old_oid; 3326 rtn = ddi_copyout(radius, (caddr_t)arg, 3327 sizeof (*radius), mode); 3328 } else if (status == ISCSI_NVFILE_NAMEVAL_NOT_FOUND) { 3329 rtn = ENOENT; 3330 } else { 3331 rtn = EIO; 3332 } 3333 kmem_free(radius, sizeof (*radius)); 3334 break; 3335 } 3336 3337 /* 3338 * ISCSI_RADIUS_SET - 3339 */ 3340 case ISCSI_RADIUS_SET: 3341 radius = (iscsi_radius_props_t *)kmem_zalloc(sizeof (*radius), 3342 KM_SLEEP); 3343 if (ddi_copyin((caddr_t)arg, radius, sizeof (*radius), mode)) { 3344 rtn = EFAULT; 3345 kmem_free(radius, sizeof (*radius)); 3346 break; 3347 } else if (radius->r_vers != ISCSI_INTERFACE_VERSION) { 3348 rtn = EINVAL; 3349 kmem_free(radius, sizeof (*radius)); 3350 break; 3351 } 3352 3353 if (radius->r_oid == ihp->hba_oid) { 3354 name = ihp->hba_name; 3355 } else { 3356 /* 3357 * RADIUS configuration should be done on a per 3358 * initiator basis. 3359 */ 3360 kmem_free(radius, sizeof (*radius)); 3361 rtn = EINVAL; 3362 break; 3363 } 3364 3365 if (persistent_radius_set(radius) == B_FALSE) { 3366 rtn = EIO; 3367 } 3368 3369 kmem_free(radius, sizeof (*radius)); 3370 break; 3371 3372 /* 3373 * ISCSI_AUTH_GET - 3374 */ 3375 case ISCSI_AUTH_GET: 3376 auth = (iscsi_auth_props_t *)kmem_zalloc(sizeof (*auth), 3377 KM_SLEEP); 3378 if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) { 3379 kmem_free(auth, sizeof (*auth)); 3380 rtn = EFAULT; 3381 break; 3382 } else if (auth->a_vers != ISCSI_INTERFACE_VERSION) { 3383 kmem_free(auth, sizeof (*auth)); 3384 rtn = EINVAL; 3385 break; 3386 } 3387 3388 old_oid = auth->a_oid; 3389 3390 if (auth->a_oid == ihp->hba_oid) { 3391 name = ihp->hba_name; 3392 } else { 3393 3394 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3395 /* 3396 * If the oid does represent a session check to see 3397 * if it is a target oid. If so, return the target's 3398 * associated session. 3399 */ 3400 rtn = iscsi_sess_get(auth->a_oid, ihp, &isp); 3401 if (rtn != 0) { 3402 rtn = iscsi_sess_get_by_target(auth->a_oid, 3403 ihp, &isp); 3404 } 3405 rw_exit(&ihp->hba_sess_list_rwlock); 3406 3407 /* 3408 * If rtn is zero then we have found an 3409 * existing session. Use the session name to 3410 * do param lookup. If rtn is non-zero then 3411 * create a targetparam object and use its name 3412 * for param lookup. 3413 */ 3414 if (rtn == 0) { 3415 name = isp->sess_name; 3416 } else { 3417 name = 3418 iscsi_targetparam_get_name(auth->a_oid); 3419 } 3420 } 3421 3422 if (name == NULL) { 3423 rw_exit( 3424 &ihp->hba_sess_list_rwlock); 3425 rtn = EFAULT; 3426 break; 3427 } 3428 3429 if (persistent_auth_get((char *)name, auth) == B_TRUE) { 3430 /* 3431 * Restore the value for overridden (and bogus) oid. 3432 */ 3433 auth->a_oid = old_oid; 3434 rtn = ddi_copyout(auth, (caddr_t)arg, 3435 sizeof (*auth), mode); 3436 } else { 3437 rtn = EIO; 3438 } 3439 3440 kmem_free(auth, sizeof (*auth)); 3441 break; 3442 3443 /* 3444 * ISCSI_AUTH_SET - 3445 */ 3446 case ISCSI_AUTH_SET: 3447 auth = (iscsi_auth_props_t *)kmem_zalloc(sizeof (*auth), 3448 KM_SLEEP); 3449 if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) { 3450 kmem_free(auth, sizeof (*auth)); 3451 rtn = EFAULT; 3452 break; 3453 } else if (auth->a_vers != ISCSI_INTERFACE_VERSION) { 3454 kmem_free(auth, sizeof (*auth)); 3455 rtn = EINVAL; 3456 break; 3457 } 3458 3459 if (auth->a_oid == ihp->hba_oid) { 3460 name = ihp->hba_name; 3461 } else { 3462 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3463 /* 3464 * If the oid does represent a session check to see 3465 * if it is a target oid. If so, return the target's 3466 * associated session. 3467 */ 3468 rtn = iscsi_sess_get(auth->a_oid, ihp, &isp); 3469 if (rtn != 0) { 3470 rtn = iscsi_sess_get_by_target(auth->a_oid, 3471 ihp, &isp); 3472 } 3473 rw_exit(&ihp->hba_sess_list_rwlock); 3474 3475 /* 3476 * If rtn is zero then we have found an 3477 * existing session. Use the session name to 3478 * do param lookup. If rtn is non-zero then 3479 * create a targetparam object and use its name 3480 * for param lookup. 3481 */ 3482 if (rtn == 0) { 3483 name = isp->sess_name; 3484 } else { 3485 name = 3486 iscsi_targetparam_get_name(auth->a_oid); 3487 rtn = 0; 3488 } 3489 } 3490 3491 if (name == NULL) { 3492 rtn = EFAULT; 3493 } else if (persistent_auth_set((char *)name, auth) 3494 == B_FALSE) { 3495 rtn = EIO; 3496 } 3497 3498 kmem_free(auth, sizeof (*auth)); 3499 break; 3500 3501 /* 3502 * ISCSI_AUTH_CLEAR - 3503 */ 3504 case ISCSI_AUTH_CLEAR: 3505 auth = (iscsi_auth_props_t *)kmem_alloc(sizeof (*auth), 3506 KM_SLEEP); 3507 if (ddi_copyin((caddr_t)arg, auth, sizeof (*auth), mode)) { 3508 kmem_free(auth, sizeof (*auth)); 3509 rtn = EFAULT; 3510 break; 3511 } else if (auth->a_vers != ISCSI_INTERFACE_VERSION) { 3512 kmem_free(auth, sizeof (*auth)); 3513 rtn = EINVAL; 3514 break; 3515 } 3516 3517 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3518 /* 3519 * If the oid does represent a session check to see 3520 * if it is a target oid. If so, return the target's 3521 * associated session. 3522 */ 3523 rtn = iscsi_sess_get(auth->a_oid, ihp, &isp); 3524 if (rtn != 0) { 3525 rtn = iscsi_sess_get_by_target(auth->a_oid, ihp, &isp); 3526 } 3527 rw_exit(&ihp->hba_sess_list_rwlock); 3528 3529 /* 3530 * If rtn is zero then we have found an 3531 * existing session. Use the session name to 3532 * do param lookup. If rtn is non-zero then 3533 * create a targetparam object and use its name 3534 * for param lookup. 3535 */ 3536 if (rtn == 0) { 3537 name = isp->sess_name; 3538 } else { 3539 name = 3540 iscsi_targetparam_get_name(auth->a_oid); 3541 rtn = 0; 3542 discovered = B_FALSE; 3543 } 3544 3545 if (name == NULL) { 3546 rw_exit( 3547 &ihp->hba_sess_list_rwlock); 3548 rtn = EFAULT; 3549 break; 3550 } 3551 3552 if (persistent_auth_clear((char *)name) == B_FALSE) { 3553 rtn = EIO; 3554 } 3555 3556 /* 3557 * ISCSI_TARGET_PARAM_CLEAR, ISCSI_CHAP_CLEAR and 3558 * ISCSI_AUTH_CLEAR ioctl are called sequentially to remove 3559 * target parameters. Here, the target that is not discovered 3560 * by initiator should be removed from the iscsi_targets list 3561 * residing in the memory. 3562 */ 3563 if (discovered == B_FALSE) { 3564 (void) iscsi_targetparam_remove_target(auth->a_oid); 3565 } 3566 3567 kmem_free(auth, sizeof (*auth)); 3568 break; 3569 3570 /* 3571 * ISCSI_DB_RELOAD - 3572 */ 3573 case ISCSI_DB_RELOAD: 3574 /* ---- database will be closed and reread ---- */ 3575 if (iscsid_init(ihp, B_TRUE) == B_FALSE) { 3576 rtn = EFAULT; 3577 } 3578 break; 3579 3580 /* 3581 * ISCSI_DB_DUMP - 3582 */ 3583 case ISCSI_DB_DUMP: 3584 persistent_dump_data(); 3585 break; 3586 3587 case ISCSI_USCSI: 3588 3589 #ifdef _MULTI_DATAMODEL 3590 model = ddi_model_convert_from(mode & FMODELS); 3591 switch (model) { 3592 case DDI_MODEL_ILP32: 3593 3594 if (ddi_copyin((caddr_t)arg, &iu32_caller, 3595 sizeof (iscsi_uscsi32_t), mode)) { 3596 rtn = EFAULT; 3597 break; 3598 } 3599 3600 /* perform conversion from 32 -> 64 */ 3601 iu_caller.iu_vers = iu32_caller.iu_vers; 3602 iu_caller.iu_oid = iu32_caller.iu_oid; 3603 iu_caller.iu_tpgt = iu32_caller.iu_tpgt; 3604 iu_caller.iu_len = iu32_caller.iu_len; 3605 iu_caller.iu_lun = iu32_caller.iu_lun; 3606 uscsi_cmd32touscsi_cmd((&iu32_caller.iu_ucmd), 3607 (&iu_caller.iu_ucmd)); 3608 3609 break; 3610 case DDI_MODEL_NONE: 3611 if (ddi_copyin((caddr_t)arg, &iu_caller, 3612 sizeof (iscsi_uscsi_t), mode)) { 3613 rtn = EFAULT; 3614 break; 3615 } 3616 break; 3617 default: 3618 ASSERT(FALSE); 3619 rtn = EINVAL; 3620 break; 3621 } 3622 #endif /* _MULTI_DATAMODEL */ 3623 3624 /* If failures earlier break */ 3625 if (rtn != 0) { 3626 break; 3627 } 3628 3629 /* copy from caller to internel cmd */ 3630 bcopy(&iu_caller, &iu, sizeof (iu)); 3631 3632 if (iu.iu_vers != ISCSI_INTERFACE_VERSION) { 3633 rtn = EINVAL; 3634 break; 3635 } 3636 /* 3637 * Check to see if oid references a target-param oid. If so, 3638 * find the associated session oid before getting lu list. 3639 */ 3640 if (iscsi_targetparam_get_name(iu.iu_oid) != NULL) { 3641 for (isp = ihp->hba_sess_list; isp; isp = 3642 isp->sess_next) { 3643 if (isp->sess_target_oid == iu.iu_oid) { 3644 target_oid = isp->sess_oid; 3645 break; 3646 } 3647 } 3648 } else { 3649 target_oid = iu.iu_oid; 3650 } 3651 3652 /* make sure we have a matching session for this command */ 3653 rw_enter(&ihp->hba_sess_list_rwlock, RW_READER); 3654 rtn = iscsi_sess_get(target_oid, ihp, &isp); 3655 if (rtn != 0) { 3656 rtn = iscsi_sess_get_by_target(target_oid, ihp, 3657 &isp); 3658 if (rtn != 0) { 3659 rw_exit(&ihp->hba_sess_list_rwlock); 3660 rtn = EFAULT; 3661 break; 3662 } 3663 } 3664 /* 3665 * If a caller buffer is present allocate duplicate 3666 * kernel space and copyin caller memory. 3667 */ 3668 if (iu.iu_ucmd.uscsi_buflen > 0) { 3669 iu.iu_ucmd.uscsi_bufaddr = (caddr_t)kmem_alloc( 3670 iu.iu_ucmd.uscsi_buflen, KM_SLEEP); 3671 if (ddi_copyin(iu_caller.iu_ucmd.uscsi_bufaddr, 3672 iu.iu_ucmd.uscsi_bufaddr, 3673 iu.iu_ucmd.uscsi_buflen, mode)) { 3674 rw_exit(&ihp->hba_sess_list_rwlock); 3675 rtn = EFAULT; 3676 break; 3677 } 3678 } 3679 3680 /* 3681 * If a caller cdb is present allocate duplicate 3682 * kernel space and copyin caller memory. 3683 */ 3684 if (iu.iu_ucmd.uscsi_cdblen > 0) { 3685 iu.iu_ucmd.uscsi_cdb = (caddr_t)kmem_alloc( 3686 iu_caller.iu_ucmd.uscsi_cdblen, KM_SLEEP); 3687 if (ddi_copyin(iu_caller.iu_ucmd.uscsi_cdb, 3688 iu.iu_ucmd.uscsi_cdb, 3689 iu.iu_ucmd.uscsi_cdblen, mode)) { 3690 if (iu.iu_ucmd.uscsi_buflen > 0) { 3691 kmem_free(iu.iu_ucmd.uscsi_bufaddr, 3692 iu_caller.iu_ucmd.uscsi_buflen); 3693 } 3694 rw_exit(&ihp->hba_sess_list_rwlock); 3695 rtn = EFAULT; 3696 break; 3697 } 3698 } 3699 3700 /* 3701 * If a caller request sense is present allocate 3702 * duplicate kernel space. No need to copyin. 3703 */ 3704 if (iu.iu_ucmd.uscsi_rqlen > 0) { 3705 iu.iu_ucmd.uscsi_rqbuf = (caddr_t)kmem_alloc( 3706 iu.iu_ucmd.uscsi_rqlen, KM_SLEEP); 3707 } 3708 3709 /* issue passthru to io path handler */ 3710 rtn = iscsi_handle_passthru(isp, iu.iu_lun, &iu.iu_ucmd); 3711 if (rtn != 0) { 3712 rtn = EFAULT; 3713 } 3714 3715 /* 3716 * If the caller had a buf we need to do a copyout 3717 * and free the kernel memory 3718 */ 3719 if (iu.iu_ucmd.uscsi_buflen > 0) { 3720 if (ddi_copyout(iu.iu_ucmd.uscsi_bufaddr, 3721 iu_caller.iu_ucmd.uscsi_bufaddr, 3722 iu.iu_ucmd.uscsi_buflen, mode) != 0) { 3723 rtn = EFAULT; 3724 } 3725 kmem_free(iu.iu_ucmd.uscsi_bufaddr, 3726 iu.iu_ucmd.uscsi_buflen); 3727 } 3728 3729 /* We need to free kernel cdb, no need to copyout */ 3730 if (iu.iu_ucmd.uscsi_cdblen > 0) { 3731 kmem_free(iu.iu_ucmd.uscsi_cdb, 3732 iu.iu_ucmd.uscsi_cdblen); 3733 } 3734 3735 /* 3736 * If the caller had a request sense we need to 3737 * do a copyout and free the kernel memory 3738 */ 3739 if (iu.iu_ucmd.uscsi_rqlen > 0) { 3740 if (ddi_copyout(iu.iu_ucmd.uscsi_rqbuf, 3741 iu_caller.iu_ucmd.uscsi_rqbuf, 3742 iu.iu_ucmd.uscsi_rqlen, mode) != 0) { 3743 rtn = EFAULT; 3744 } 3745 kmem_free(iu.iu_ucmd.uscsi_rqbuf, 3746 iu.iu_ucmd.uscsi_rqlen); 3747 } 3748 3749 #ifdef _MULTI_DATAMODEL 3750 if (iu.iu_ucmd.uscsi_status != 0) { 3751 switch (model = ddi_model_convert_from( 3752 mode & FMODELS)) { 3753 case DDI_MODEL_ILP32: 3754 iu32_caller.iu_ucmd.uscsi_status = 3755 iu.iu_ucmd.uscsi_status; 3756 if (ddi_copyout((void *)&iu32_caller, 3757 (caddr_t)arg, sizeof (iscsi_uscsi32_t), 3758 mode) != 0) { 3759 rtn = EFAULT; 3760 } 3761 break; 3762 case DDI_MODEL_NONE: 3763 iu_caller.iu_ucmd.uscsi_status = 3764 iu.iu_ucmd.uscsi_status; 3765 if (ddi_copyout((void *)&iu_caller, 3766 (caddr_t)arg, sizeof (iscsi_uscsi_t), 3767 mode) != 0) { 3768 rtn = EFAULT; 3769 } 3770 break; 3771 default: 3772 ASSERT(FALSE); 3773 } 3774 } 3775 #endif /* _MULTI_DATAMODEL */ 3776 rw_exit(&ihp->hba_sess_list_rwlock); 3777 break; 3778 3779 /* 3780 * ISCSI_DOOR_HANDLE_SET - 3781 */ 3782 case ISCSI_DOOR_HANDLE_SET: 3783 if (ddi_copyin((caddr_t)arg, &did, sizeof (int), mode) != 0) { 3784 rtn = EFAULT; 3785 } 3786 if (iscsi_door_bind(did) == B_FALSE) { 3787 rtn = EFAULT; 3788 } 3789 break; 3790 3791 case ISCSI_DISCOVERY_EVENTS: 3792 /* 3793 * If discovery has not been completed and not in progress, 3794 * poke the discovery methods 3795 */ 3796 mutex_enter(&ihp->hba_discovery_events_mutex); 3797 method = ihp->hba_discovery_events; 3798 if ((method != ISCSI_ALL_DISCOVERY_METHODS) && 3799 (ihp->hba_discovery_in_progress == B_FALSE)) { 3800 ihp->hba_discovery_in_progress = B_TRUE; 3801 mutex_exit(&ihp->hba_discovery_events_mutex); 3802 iscsid_poke_discovery(ihp, iSCSIDiscoveryMethodUnknown); 3803 mutex_enter(&ihp->hba_discovery_events_mutex); 3804 ihp->hba_discovery_in_progress = B_FALSE; 3805 method = ihp->hba_discovery_events; 3806 } 3807 mutex_exit(&ihp->hba_discovery_events_mutex); 3808 3809 if (ddi_copyout((void *)&method, (caddr_t)arg, 3810 sizeof (method), mode) != 0) 3811 rtn = EFAULT; 3812 break; 3813 3814 /* 3815 * ISCSI_SENDTGTS_GET -- 3816 */ 3817 case ISCSI_SENDTGTS_GET: 3818 stl_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode, 3819 sizeof (*stl_hdr)); 3820 if (stl_hdr == NULL) { 3821 rtn = EFAULT; 3822 break; 3823 } 3824 3825 if (stl_hdr->stl_entry.e_vers != ISCSI_INTERFACE_VERSION) { 3826 rtn = EINVAL; 3827 kmem_free(stl_hdr, sizeof (*stl_hdr)); 3828 break; 3829 } 3830 3831 /* calculate how much memory user allocated for SendTgts */ 3832 stl_sz = sizeof (*stl_hdr); 3833 if (stl_hdr->stl_in_cnt > 0) { 3834 stl_sz += ((stl_hdr->stl_in_cnt - 1) * 3835 sizeof (iscsi_sendtgts_entry_t)); 3836 } 3837 3838 /* allocate local SendTgts list of the same size */ 3839 istl = kmem_zalloc(stl_sz, KM_SLEEP); 3840 bcopy(stl_hdr, istl, sizeof (*stl_hdr)); 3841 kmem_free(stl_hdr, sizeof (*stl_hdr)); 3842 3843 /* lock interface so only one SendTargets operation occurs */ 3844 sema_p(&ihp->hba_sendtgts_semaphore); 3845 3846 rtn = iscsi_ioctl_sendtgts_get(ihp, istl); 3847 3848 if (rtn == 0) { 3849 rtn = iscsi_ioctl_copyout(istl, stl_sz, 3850 (caddr_t)arg, mode); 3851 } 3852 3853 /* release lock to allow another SendTargets discovery */ 3854 sema_v(&ihp->hba_sendtgts_semaphore); 3855 3856 break; 3857 3858 /* 3859 * ISCSI_ISNS_SERVER_GET -- 3860 */ 3861 case ISCSI_ISNS_SERVER_GET: 3862 server_pg_list_hdr = iscsi_ioctl_copyin((caddr_t)arg, mode, 3863 sizeof (*server_pg_list_hdr)); 3864 if (server_pg_list_hdr == NULL) { 3865 rtn = EFAULT; 3866 break; 3867 } 3868 3869 /* If iSNS discovery mode is not set, return with zero entry */ 3870 method = persistent_disc_meth_get(); 3871 if ((method & iSCSIDiscoveryMethodISNS) == 0) { 3872 kmem_free(server_pg_list_hdr, 3873 sizeof (*server_pg_list_hdr)); 3874 server_pg_list_hdr = NULL; 3875 rtn = EACCES; 3876 break; 3877 } 3878 3879 initiator_node_name = kmem_zalloc(ISCSI_MAX_NAME_LEN, KM_SLEEP); 3880 if (persistent_initiator_name_get(initiator_node_name, 3881 ISCSI_MAX_NAME_LEN) != B_TRUE) { 3882 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 3883 initiator_node_name = NULL; 3884 kmem_free(server_pg_list_hdr, 3885 sizeof (*server_pg_list_hdr)); 3886 server_pg_list_hdr = NULL; 3887 rtn = EIO; 3888 break; 3889 } 3890 if (strlen(initiator_node_name) == 0) { 3891 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 3892 initiator_node_name = NULL; 3893 kmem_free(server_pg_list_hdr, 3894 sizeof (*server_pg_list_hdr)); 3895 server_pg_list_hdr = NULL; 3896 rtn = EIO; 3897 break; 3898 } 3899 3900 initiator_node_alias = kmem_zalloc( 3901 ISCSI_MAX_NAME_LEN, KM_SLEEP); 3902 if (persistent_alias_name_get(initiator_node_alias, 3903 ISCSI_MAX_NAME_LEN) != B_TRUE) { 3904 initiator_node_alias[0] = '\0'; 3905 } 3906 rtn = isns_query_one_server(&(server_pg_list_hdr->addr), 3907 ihp->hba_isid, 3908 (uint8_t *)initiator_node_name, 3909 (uint8_t *)initiator_node_alias, 3910 ISNS_INITIATOR_NODE_TYPE, 3911 &pg_list); 3912 if (rtn != isns_ok || pg_list == NULL) { 3913 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 3914 initiator_node_name = NULL; 3915 kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN); 3916 initiator_node_alias = NULL; 3917 kmem_free(server_pg_list_hdr, 3918 sizeof (*server_pg_list_hdr)); 3919 server_pg_list_hdr = NULL; 3920 rtn = EIO; 3921 break; 3922 } 3923 3924 /* 3925 * pg_list_sz is the size of the pg_list returned from the 3926 * isns_query_all 3927 * 3928 * pg_sz_copy_out is the size of the pg_list we are going to 3929 * return back to the caller 3930 * 3931 * server_pg_list_sz is total amount of data we are returning 3932 * back to the caller 3933 */ 3934 pg_list->pg_in_cnt = 3935 server_pg_list_hdr->addr_port_list.pg_in_cnt; 3936 pg_list_sz = sizeof (isns_portal_group_list_t); 3937 if (pg_list->pg_out_cnt > 0) { 3938 pg_list_sz += (pg_list->pg_out_cnt - 1) * 3939 sizeof (isns_portal_group_t); 3940 } 3941 /* 3942 * check if caller passed in a buffer with enough space 3943 * if there isn't enough space, fill the caller's buffer with 3944 * as much information as possible. 3945 * 3946 * if pg_out_cnt > pg_in_cnt, pg_out_cnt will be returned with 3947 * the total number of targets found 3948 * 3949 * if pg_out_cnt < pg_in_cnt, pg_out_cnt will be the number 3950 * of targets returned 3951 */ 3952 if (pg_list->pg_in_cnt < pg_list->pg_out_cnt) { 3953 pg_sz_copy_out = sizeof (isns_portal_group_list_t); 3954 if (pg_list->pg_in_cnt > 0) { 3955 pg_sz_copy_out += (pg_list->pg_in_cnt - 1) * 3956 sizeof (isns_portal_group_t); 3957 } 3958 server_pg_list_sz = 3959 sizeof (isns_server_portal_group_list_t); 3960 if (pg_list->pg_in_cnt > 0) { 3961 server_pg_list_sz += (pg_list->pg_in_cnt - 1) * 3962 sizeof (isns_portal_group_t); 3963 } 3964 } else { 3965 pg_sz_copy_out = pg_list_sz; 3966 server_pg_list_sz = 3967 sizeof (isns_server_portal_group_list_t); 3968 if (pg_list->pg_out_cnt > 0) { 3969 server_pg_list_sz += (pg_list->pg_out_cnt - 1) * 3970 sizeof (isns_portal_group_t); 3971 } 3972 } 3973 3974 server_pg_list = (isns_server_portal_group_list_t *)kmem_zalloc( 3975 server_pg_list_sz, KM_SLEEP); 3976 3977 bcopy(&(server_pg_list_hdr->addr), &(server_pg_list->addr), 3978 sizeof (server_pg_list->addr)); 3979 bcopy(pg_list, &server_pg_list->addr_port_list, pg_sz_copy_out); 3980 3981 if (ddi_copyout(server_pg_list, (caddr_t)arg, server_pg_list_sz, 3982 mode) != 0) { 3983 rtn = EFAULT; 3984 } 3985 DTRACE_PROBE1(iscsi_ioctl_iscsi_isns_server_get_pg_sz, 3986 int, pg_list_sz); 3987 kmem_free(initiator_node_name, ISCSI_MAX_NAME_LEN); 3988 initiator_node_name = NULL; 3989 kmem_free(initiator_node_alias, ISCSI_MAX_NAME_LEN); 3990 initiator_node_alias = NULL; 3991 kmem_free(pg_list, pg_list_sz); 3992 pg_list = NULL; 3993 kmem_free(server_pg_list, server_pg_list_sz); 3994 server_pg_list = NULL; 3995 kmem_free(server_pg_list_hdr, sizeof (*server_pg_list_hdr)); 3996 server_pg_list_hdr = NULL; 3997 break; 3998 3999 /* 4000 * ISCSI_GET_CONFIG_SESSIONS -- 4001 */ 4002 case ISCSI_GET_CONFIG_SESSIONS: 4003 /* FALLTHRU */ 4004 4005 case ISCSI_SET_CONFIG_SESSIONS: 4006 size = sizeof (*ics); 4007 ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size); 4008 if (ics == NULL) { 4009 rtn = EFAULT; 4010 break; 4011 } 4012 4013 /* verify version infomration */ 4014 if (ics->ics_ver != ISCSI_INTERFACE_VERSION) { 4015 rtn = EINVAL; 4016 kmem_free(ics, size); 4017 ics = NULL; 4018 break; 4019 } 4020 4021 /* Check to see if we need to copy in more memory */ 4022 if (ics->ics_in > 1) { 4023 /* record correct size */ 4024 size = ISCSI_SESSION_CONFIG_SIZE(ics->ics_in); 4025 /* free old buffer */ 4026 kmem_free(ics, sizeof (*ics)); 4027 4028 /* copy in complete buffer size */ 4029 ics = iscsi_ioctl_copyin((caddr_t)arg, mode, size); 4030 if (ics == NULL) { 4031 rtn = EFAULT; 4032 break; 4033 } 4034 } 4035 4036 /* switch action based on get or set */ 4037 if (cmd == ISCSI_GET_CONFIG_SESSIONS) { 4038 /* get */ 4039 rtn = iscsi_ioctl_get_config_sess(ihp, ics); 4040 if (rtn == 0) { 4041 /* copyout data for gets */ 4042 rtn = iscsi_ioctl_copyout(ics, size, 4043 (caddr_t)arg, mode); 4044 } else { 4045 kmem_free(ics, size); 4046 ics = NULL; 4047 } 4048 } else { 4049 /* set */ 4050 rtn = iscsi_ioctl_set_config_sess(ihp, ics); 4051 if (iscsiboot_prop) { 4052 if (iscsi_cmp_boot_sess_oid(ihp, 4053 ics->ics_oid)) { 4054 /* 4055 * found active session for this object 4056 * or this is initiator object 4057 * with mpxio enabled 4058 */ 4059 if (!iscsi_reconfig_boot_sess(ihp)) { 4060 kmem_free(ics, size); 4061 ics = NULL; 4062 rtn = EINVAL; 4063 break; 4064 } 4065 } 4066 } 4067 kmem_free(ics, size); 4068 ics = NULL; 4069 } 4070 break; 4071 4072 case ISCSI_IS_ACTIVE: 4073 /* 4074 * dhcpagent calls here to check if there are 4075 * active iSCSI sessions 4076 */ 4077 instance = 0; 4078 if (iscsiboot_prop) { 4079 instance = 1; 4080 } 4081 if (!instance) { 4082 rw_enter(&ihp->hba_sess_list_rwlock, 4083 RW_READER); 4084 for (isp = ihp->hba_sess_list; isp; 4085 isp = isp->sess_next) { 4086 if ((isp->sess_state == 4087 ISCSI_SESS_STATE_LOGGED_IN) && 4088 (isp->sess_lun_list != 4089 NULL)) { 4090 instance = 1; 4091 break; 4092 } 4093 } 4094 rw_exit(&ihp->hba_sess_list_rwlock); 4095 } 4096 size = sizeof (instance); 4097 if (ddi_copyout(&instance, (caddr_t)arg, size, 4098 mode) != 0) { 4099 rtn = EFAULT; 4100 } 4101 break; 4102 4103 case ISCSI_BOOTPROP_GET: 4104 size = sizeof (*bootProp); 4105 bootProp = iscsi_ioctl_copyin((caddr_t)arg, mode, size); 4106 if (bootProp == NULL) { 4107 rtn = EFAULT; 4108 break; 4109 } 4110 bootProp->hba_mpxio_enabled = 4111 iscsi_chk_bootlun_mpxio(ihp); 4112 if (iscsiboot_prop == NULL) { 4113 bootProp->iscsiboot = 0; 4114 rtn = iscsi_ioctl_copyout(bootProp, size, 4115 (caddr_t)arg, mode); 4116 break; 4117 } else { 4118 bootProp->iscsiboot = 1; 4119 } 4120 4121 if (iscsiboot_prop->boot_init.ini_name != NULL) { 4122 (void) strncpy((char *)bootProp->ini_name.n_name, 4123 (char *)iscsiboot_prop->boot_init.ini_name, 4124 ISCSI_MAX_NAME_LEN); 4125 } 4126 if (iscsiboot_prop->boot_init.ini_chap_name != NULL) { 4127 bootProp->auth.a_auth_method = authMethodCHAP; 4128 (void) strncpy((char *)bootProp->ini_chap.c_user, 4129 (char *)iscsiboot_prop->boot_init.ini_chap_name, 4130 ISCSI_MAX_NAME_LEN); 4131 (void) strncpy((char *)bootProp->ini_chap.c_secret, 4132 (char *)iscsiboot_prop->boot_init.ini_chap_sec, 4133 ISCSI_CHAP_SECRET_LEN); 4134 if (iscsiboot_prop->boot_tgt.tgt_chap_name != 4135 NULL) { 4136 bootProp->auth.a_bi_auth = B_TRUE; 4137 } else { 4138 bootProp->auth.a_bi_auth = B_FALSE; 4139 } 4140 } 4141 if (iscsiboot_prop->boot_tgt.tgt_name != NULL) { 4142 (void) strncpy((char *)bootProp->tgt_name.n_name, 4143 (char *)iscsiboot_prop->boot_tgt.tgt_name, 4144 ISCSI_MAX_NAME_LEN); 4145 } 4146 if (iscsiboot_prop->boot_tgt.tgt_chap_name != NULL) { 4147 (void) strncpy((char *)bootProp->tgt_chap.c_user, 4148 (char *)iscsiboot_prop->boot_tgt.tgt_chap_name, 4149 ISCSI_MAX_NAME_LEN); 4150 (void) strncpy((char *)bootProp->tgt_chap.c_secret, 4151 (char *)iscsiboot_prop->boot_tgt.tgt_chap_sec, 4152 ISCSI_CHAP_SECRET_LEN); 4153 } 4154 4155 rtn = iscsi_ioctl_copyout(bootProp, size, (caddr_t)arg, mode); 4156 break; 4157 4158 default: 4159 rtn = ENOTTY; 4160 cmn_err(CE_NOTE, "unrecognized ioctl 0x%x", cmd); 4161 } /* end of ioctl type switch/cases */ 4162 4163 return (rtn); 4164 } 4165 4166 /* 4167 * +--------------------------------------------------------------------+ 4168 * | End of cb_ops routines | 4169 * +--------------------------------------------------------------------+ 4170 */ 4171 4172 4173 /* 4174 * +--------------------------------------------------------------------+ 4175 * | Common scsi_tran support routines | 4176 * +--------------------------------------------------------------------+ 4177 */ 4178 4179 /* 4180 * iscsi_i_commoncap -- SCSA host adapter get/set capability routines. 4181 * 4182 * Need to determine if any of these can be determined through the iSCSI 4183 * protocol. For now just return error on most. 4184 */ 4185 /* ARGSUSED */ 4186 static int 4187 iscsi_i_commoncap(struct scsi_address *ap, char *cap, int val, 4188 int tgtonly, int doset) 4189 { 4190 int rtn; 4191 int cidx; 4192 iscsi_lun_t *ilp; 4193 4194 ASSERT((ap)->a_hba_tran->tran_hba_private != NULL); 4195 ilp = (iscsi_lun_t *)((ap)->a_hba_tran->tran_tgt_private); 4196 ASSERT(ilp != NULL); 4197 4198 if (cap == (char *)0) { 4199 return (FALSE); 4200 } 4201 4202 cidx = scsi_hba_lookup_capstr(cap); 4203 if (cidx == -1) { 4204 return (cidx); 4205 } 4206 4207 /* 4208 * Process setcap request. 4209 */ 4210 if (doset) { 4211 /* 4212 * At present, we can only set binary (0/1) values 4213 */ 4214 switch (cidx) { 4215 case SCSI_CAP_LUN_RESET: 4216 if (val) { 4217 ilp->lun_cap |= ISCSI_LUN_CAP_RESET; 4218 } else { 4219 ilp->lun_cap &= ~ISCSI_LUN_CAP_RESET; 4220 } 4221 rtn = TRUE; 4222 break; 4223 default: 4224 /* 4225 * None of these are settable via 4226 * the capability interface. 4227 */ 4228 rtn = FALSE; 4229 break; 4230 } 4231 4232 /* 4233 * Process getcap request. 4234 */ 4235 } else { 4236 switch (cidx) { 4237 case SCSI_CAP_DMA_MAX: 4238 /* no DMA, Psuedo value */ 4239 rtn = INT32_MAX; 4240 break; 4241 case SCSI_CAP_INITIATOR_ID: 4242 rtn = 7; 4243 break; 4244 case SCSI_CAP_ARQ: 4245 case SCSI_CAP_RESET_NOTIFICATION: 4246 case SCSI_CAP_TAGGED_QING: 4247 rtn = TRUE; 4248 break; 4249 case SCSI_CAP_SCSI_VERSION: 4250 rtn = SCSI_VERSION_3; 4251 break; 4252 case SCSI_CAP_INTERCONNECT_TYPE: 4253 rtn = INTERCONNECT_FABRIC; 4254 break; 4255 case SCSI_CAP_LUN_RESET: 4256 rtn = ((ilp->lun_cap & ISCSI_LUN_CAP_RESET) != 0) ? 4257 TRUE : FALSE; 4258 break; 4259 case SCSI_CAP_CDB_LEN: 4260 /* 4261 * iSCSI RFC 3720 defines a default 16 byte 4262 * CDB as part of the Basic Header Segment 4263 * (BHS) (10.2.1) and allows for an Additional 4264 * Header Segment (AHS) Length of 255 * 4 4265 * (10.2.1.5). The AHS length can be used 4266 * for different purposes two of which are 4267 * Extended CDB ADS (10.2.2.3) and Bidirectional 4268 * Expected Read-Data Length AHS (10.2.2.4). 4269 * The largest header of these consumes is 4270 * 32 bytes. So the total Max CDB Length is 4271 * 16 + ((255 * 4 ) - 32) = 1004. 4272 */ 4273 rtn = 1004; 4274 break; 4275 default: 4276 rtn = UNDEFINED; 4277 break; 4278 } 4279 } 4280 return (rtn); 4281 } 4282 4283 /* 4284 * iscsi_virt_lun_init - attempts to complete a mdi/scsi_vhci binding 4285 * 4286 * This routine is used to associate the tran_tgt_private to our ilp 4287 * structure. This function is indirectly called from our 4288 * iscsi_lun_create_xxx routines. These routines must prevent 4289 * the session and lun lists from changing during this call. 4290 */ 4291 /* ARGSUSED */ 4292 static int 4293 iscsi_virt_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 4294 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 4295 { 4296 iscsi_lun_t *ilp = NULL; 4297 iscsi_lun_t *ilp_check = NULL; 4298 iscsi_sess_t *isp = NULL; 4299 char *lun_guid = NULL; 4300 mdi_pathinfo_t *pip = NULL; 4301 iscsi_hba_t *ihp = (iscsi_hba_t *)hba_tran->tran_hba_private; 4302 char target_port_name[MAX_NAME_PROP_SIZE]; 4303 4304 /* 4305 * Here's a nice little piece of undocumented stuff. 4306 */ 4307 if ((pip = (mdi_pathinfo_t *)sd->sd_private) == NULL) { 4308 /* 4309 * Very bad news if this occurs. Somehow SCSI_vhci has 4310 * lost the pathinfo node for this target. 4311 */ 4312 return (DDI_NOT_WELL_FORMED); 4313 } 4314 4315 ilp = (iscsi_lun_t *)mdi_pi_get_phci_private(pip); 4316 4317 /* 4318 * +----------------------------------------------------+ 4319 * | Looking to find the target device via the property | 4320 * | is not required since the driver can easily get | 4321 * | this information from the mdi_phci_get_private() | 4322 * | call above. This is just a consistency check | 4323 * | which can be removed. | 4324 */ 4325 if (mdi_prop_lookup_string(pip, MDI_GUID, &lun_guid) != 4326 DDI_PROP_SUCCESS) { 4327 return (DDI_NOT_WELL_FORMED); 4328 } 4329 4330 for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) { 4331 4332 /* If this isn't the matching session continue */ 4333 if (ilp->lun_sess != isp) { 4334 continue; 4335 } 4336 4337 /* 4338 * We are already holding the lun list rwlock 4339 * for this thread on the callers side of mdi_pi_online 4340 * or ndi_devi_online. Which lead to this functions 4341 * call. 4342 */ 4343 for (ilp_check = isp->sess_lun_list; ilp_check; 4344 ilp_check = ilp_check->lun_next) { 4345 4346 /* 4347 * If this is the matching LUN and contains 4348 * the same LUN GUID then break we found our 4349 * match. 4350 */ 4351 if ((ilp == ilp_check) && 4352 (strcmp(lun_guid, ilp_check->lun_guid) == 0)) { 4353 break; 4354 } 4355 } 4356 if (ilp_check != NULL) { 4357 break; 4358 } 4359 } 4360 4361 /* 4362 * Free resource that's no longer required. 4363 */ 4364 if (lun_guid != NULL) 4365 (void) mdi_prop_free(lun_guid); 4366 4367 if (ilp_check == NULL) { 4368 /* 4369 * Failed to find iSCSI LUN in HBA chain based 4370 * on the GUID that was stored as a property on 4371 * the pathinfo node. 4372 */ 4373 return (DDI_NOT_WELL_FORMED); 4374 } 4375 4376 if (ilp != ilp_check) { 4377 /* 4378 * The iSCSI target that we found on the HBA link is 4379 * different than the iSCSI target that was stored as 4380 * private data on the pathinfo node. 4381 */ 4382 return (DDI_NOT_WELL_FORMED); 4383 } 4384 /* 4385 * | End of consistency check | 4386 * +----------------------------------------------------+ 4387 */ 4388 4389 hba_tran->tran_tgt_private = ilp; 4390 4391 target_port_name[0] = '\0'; 4392 if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) { 4393 (void) snprintf(target_port_name, MAX_NAME_PROP_SIZE, 4394 "%02x%02x%02x%02x%02x%02x,%s", 4395 ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1], 4396 ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3], 4397 ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5], 4398 ilp->lun_sess->sess_name); 4399 } else { 4400 (void) snprintf(target_port_name, MAX_NAME_PROP_SIZE, 4401 "%02x%02x%02x%02x%02x%02x,%s,%d", 4402 ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1], 4403 ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3], 4404 ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5], 4405 ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf); 4406 } 4407 4408 if (mdi_prop_update_string(pip, "target-port", 4409 target_port_name) != DDI_PROP_SUCCESS) { 4410 cmn_err(CE_WARN, "iscsi_virt_lun_init: Creating 'target-port' " 4411 "property on Path(%p) for Target(%s), Lun(%d) Failed", 4412 (void *)pip, ilp->lun_sess->sess_name, ilp->lun_num); 4413 } 4414 4415 return (DDI_SUCCESS); 4416 } 4417 4418 /* 4419 * iscsi_phys_lun_init - attempts to complete a ndi binding 4420 * 4421 * This routine is used to associate the tran_tgt_private to our 4422 * ilp structure. This function is indirectly called from our 4423 * iscsi_lun_create_xxx routines. These routines must prevent 4424 * the session and lun lists from changing during this call. 4425 */ 4426 static int 4427 iscsi_phys_lun_init(dev_info_t *hba_dip, dev_info_t *lun_dip, 4428 scsi_hba_tran_t *hba_tran, struct scsi_device *sd) 4429 { 4430 int rtn = DDI_SUCCESS; 4431 iscsi_hba_t *ihp = NULL; 4432 iscsi_sess_t *isp = NULL; 4433 iscsi_lun_t *ilp = NULL; 4434 char target_port_name[MAX_NAME_PROP_SIZE]; 4435 int *words = NULL; 4436 uint_t nwords = 0; 4437 4438 ASSERT(hba_dip); 4439 ASSERT(lun_dip); 4440 ASSERT(hba_tran); 4441 ASSERT(sd); 4442 ihp = (iscsi_hba_t *)hba_tran->tran_hba_private; 4443 ASSERT(ihp); 4444 4445 if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, lun_dip, 4446 DDI_PROP_DONTPASS, LUN_PROP, &words, &nwords) != DDI_PROP_SUCCESS) { 4447 cmn_err(CE_WARN, "iscsi_phys_lun_init: Returning DDI_FAILURE:" 4448 "lun for %s (instance %d)", ddi_get_name(lun_dip), 4449 ddi_get_instance(lun_dip)); 4450 return (DDI_FAILURE); 4451 } 4452 4453 if (nwords == 0) { 4454 ddi_prop_free(words); 4455 return (DDI_FAILURE); 4456 } 4457 4458 ASSERT(words != NULL); 4459 4460 /* See if we already created this session */ 4461 4462 /* Walk the HBA's session list */ 4463 for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) { 4464 /* compare target name as the unique identifier */ 4465 if (sd->sd_address.a_target == isp->sess_oid) { 4466 /* found match */ 4467 break; 4468 } 4469 } 4470 4471 /* If we found matching session continue searching for tgt */ 4472 if (isp != NULL) { 4473 /* 4474 * Search for the matching iscsi lun structure. We don't 4475 * need to hold the READER for the lun list at this point. 4476 * because the tran_get_name is being called from the online 4477 * function which is already holding a reader on the lun 4478 * list. 4479 */ 4480 for (ilp = isp->sess_lun_list; ilp; ilp = ilp->lun_next) { 4481 if (*words == ilp->lun_num) { 4482 /* found match */ 4483 break; 4484 } 4485 } 4486 4487 if (ilp != NULL) { 4488 /* 4489 * tgt found path it to the tran_lun_private 4490 * this is used later for fast access on 4491 * init_pkt and start 4492 */ 4493 hba_tran->tran_tgt_private = ilp; 4494 } else { 4495 /* tgt not found */ 4496 ddi_prop_free(words); 4497 return (DDI_FAILURE); 4498 } 4499 } else { 4500 /* sess not found */ 4501 ddi_prop_free(words); 4502 return (DDI_FAILURE); 4503 } 4504 ddi_prop_free(words); 4505 4506 target_port_name[0] = '\0'; 4507 if (ilp->lun_sess->sess_tpgt_conf == ISCSI_DEFAULT_TPGT) { 4508 (void) snprintf(target_port_name, MAX_NAME_PROP_SIZE, 4509 "%02x%02x%02x%02x%02x%02x,%s", 4510 ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1], 4511 ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3], 4512 ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5], 4513 ilp->lun_sess->sess_name); 4514 } else { 4515 (void) snprintf(target_port_name, MAX_NAME_PROP_SIZE, 4516 "%02x%02x%02x%02x%02x%02x,%s,%d", 4517 ilp->lun_sess->sess_isid[0], ilp->lun_sess->sess_isid[1], 4518 ilp->lun_sess->sess_isid[2], ilp->lun_sess->sess_isid[3], 4519 ilp->lun_sess->sess_isid[4], ilp->lun_sess->sess_isid[5], 4520 ilp->lun_sess->sess_name, ilp->lun_sess->sess_tpgt_conf); 4521 } 4522 4523 if (ddi_prop_update_string(DDI_DEV_T_NONE, lun_dip, 4524 "target-port", target_port_name) != DDI_PROP_SUCCESS) { 4525 cmn_err(CE_WARN, "iscsi_phys_lun_init: Creating 'target-port' " 4526 "property on Target(%s), Lun(%d) Failed", 4527 ilp->lun_sess->sess_name, ilp->lun_num); 4528 } 4529 4530 return (rtn); 4531 } 4532 4533 /* 4534 * +--------------------------------------------------------------------+ 4535 * | End of scsi_tran support routines | 4536 * +--------------------------------------------------------------------+ 4537 */ 4538 4539 /* 4540 * +--------------------------------------------------------------------+ 4541 * | Begin of struct utility routines | 4542 * +--------------------------------------------------------------------+ 4543 */ 4544 4545 4546 /* 4547 * iscsi_set_default_login_params - This function sets the 4548 * driver default login params. This is using during the 4549 * creation of our iSCSI HBA structure initialization by 4550 * could be used at other times to reset back to the defaults. 4551 */ 4552 void 4553 iscsi_set_default_login_params(iscsi_login_params_t *params) 4554 { 4555 params->immediate_data = ISCSI_DEFAULT_IMMEDIATE_DATA; 4556 params->initial_r2t = ISCSI_DEFAULT_INITIALR2T; 4557 params->first_burst_length = ISCSI_DEFAULT_FIRST_BURST_LENGTH; 4558 params->max_burst_length = ISCSI_DEFAULT_MAX_BURST_LENGTH; 4559 params->data_pdu_in_order = ISCSI_DEFAULT_DATA_PDU_IN_ORDER; 4560 params->data_sequence_in_order = ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER; 4561 params->default_time_to_wait = ISCSI_DEFAULT_TIME_TO_WAIT; 4562 params->default_time_to_retain = ISCSI_DEFAULT_TIME_TO_RETAIN; 4563 params->header_digest = ISCSI_DEFAULT_HEADER_DIGEST; 4564 params->data_digest = ISCSI_DEFAULT_DATA_DIGEST; 4565 params->max_recv_data_seg_len = ISCSI_DEFAULT_MAX_RECV_SEG_LEN; 4566 params->max_xmit_data_seg_len = ISCSI_DEFAULT_MAX_XMIT_SEG_LEN; 4567 params->max_connections = ISCSI_DEFAULT_MAX_CONNECTIONS; 4568 params->max_outstanding_r2t = ISCSI_DEFAULT_MAX_OUT_R2T; 4569 params->error_recovery_level = ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL; 4570 params->ifmarker = ISCSI_DEFAULT_IFMARKER; 4571 params->ofmarker = ISCSI_DEFAULT_OFMARKER; 4572 } 4573 4574 4575 /* 4576 * +--------------------------------------------------------------------+ 4577 * | End of struct utility routines | 4578 * +--------------------------------------------------------------------+ 4579 */ 4580 4581 /* 4582 * +--------------------------------------------------------------------+ 4583 * | Begin of ioctl utility routines | 4584 * +--------------------------------------------------------------------+ 4585 */ 4586 4587 /* 4588 * iscsi_get_param - This function is a helper to ISCSI_GET_PARAM 4589 * IOCTL 4590 */ 4591 int 4592 iscsi_get_param(iscsi_login_params_t *params, boolean_t valid_flag, 4593 iscsi_param_get_t *ipgp) { 4594 int rtn = 0; 4595 4596 /* ---- Default to settable, possibly changed later ---- */ 4597 ipgp->g_value.v_valid = valid_flag; 4598 ipgp->g_value.v_settable = B_TRUE; 4599 4600 switch (ipgp->g_param) { 4601 /* 4602 * Boolean parameters 4603 */ 4604 case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER: 4605 ipgp->g_value.v_bool.b_current = 4606 params->data_sequence_in_order; 4607 ipgp->g_value.v_bool.b_default = 4608 ISCSI_DEFAULT_DATA_SEQUENCE_IN_ORDER; 4609 break; 4610 case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA: 4611 ipgp->g_value.v_bool.b_current = 4612 params->immediate_data; 4613 ipgp->g_value.v_bool.b_default = 4614 ISCSI_DEFAULT_IMMEDIATE_DATA; 4615 break; 4616 case ISCSI_LOGIN_PARAM_INITIAL_R2T: 4617 ipgp->g_value.v_bool.b_current = 4618 params->initial_r2t; 4619 ipgp->g_value.v_bool.b_default = 4620 ISCSI_DEFAULT_IMMEDIATE_DATA; 4621 break; 4622 case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER: 4623 ipgp->g_value.v_bool.b_current = 4624 params->data_pdu_in_order; 4625 ipgp->g_value.v_bool.b_default = 4626 ISCSI_DEFAULT_DATA_PDU_IN_ORDER; 4627 break; 4628 4629 /* 4630 * Integer parameters 4631 */ 4632 case ISCSI_LOGIN_PARAM_HEADER_DIGEST: 4633 ipgp->g_value.v_integer.i_current = params->header_digest; 4634 ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_HEADER_DIGEST; 4635 ipgp->g_value.v_integer.i_min = 0; 4636 ipgp->g_value.v_integer.i_max = ISCSI_MAX_HEADER_DIGEST; 4637 ipgp->g_value.v_integer.i_incr = 1; 4638 break; 4639 case ISCSI_LOGIN_PARAM_DATA_DIGEST: 4640 ipgp->g_value.v_integer.i_current = params->data_digest; 4641 ipgp->g_value.v_integer.i_default = ISCSI_DEFAULT_DATA_DIGEST; 4642 ipgp->g_value.v_integer.i_min = 0; 4643 ipgp->g_value.v_integer.i_max = ISCSI_MAX_DATA_DIGEST; 4644 ipgp->g_value.v_integer.i_incr = 1; 4645 break; 4646 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN: 4647 ipgp->g_value.v_integer.i_current = 4648 params->default_time_to_retain; 4649 ipgp->g_value.v_integer.i_default = 4650 ISCSI_DEFAULT_TIME_TO_RETAIN; 4651 ipgp->g_value.v_integer.i_min = 0; 4652 ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2RETAIN; 4653 ipgp->g_value.v_integer.i_incr = 1; 4654 break; 4655 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT: 4656 ipgp->g_value.v_integer.i_current = 4657 params->default_time_to_wait; 4658 ipgp->g_value.v_integer.i_default = 4659 ISCSI_DEFAULT_TIME_TO_WAIT; 4660 ipgp->g_value.v_integer.i_min = 0; 4661 ipgp->g_value.v_integer.i_max = ISCSI_MAX_TIME2WAIT; 4662 ipgp->g_value.v_integer.i_incr = 1; 4663 break; 4664 case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL: 4665 ipgp->g_value.v_integer.i_current = 4666 params->error_recovery_level; 4667 ipgp->g_value.v_integer.i_default = 4668 ISCSI_DEFAULT_ERROR_RECOVERY_LEVEL; 4669 ipgp->g_value.v_integer.i_min = 0; 4670 ipgp->g_value.v_integer.i_max = ISCSI_MAX_ERROR_RECOVERY_LEVEL; 4671 ipgp->g_value.v_integer.i_incr = 1; 4672 ipgp->g_value.v_settable = B_FALSE; 4673 break; 4674 case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH: 4675 ipgp->g_value.v_integer.i_current = 4676 params->first_burst_length; 4677 ipgp->g_value.v_integer.i_default = 4678 ISCSI_DEFAULT_FIRST_BURST_LENGTH; 4679 ipgp->g_value.v_integer.i_min = 512; 4680 ipgp->g_value.v_integer.i_max = ISCSI_MAX_FIRST_BURST_LENGTH; 4681 ipgp->g_value.v_integer.i_incr = 1; 4682 break; 4683 case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH: 4684 ipgp->g_value.v_integer.i_current = 4685 params->max_burst_length; 4686 ipgp->g_value.v_integer.i_default = 4687 ISCSI_DEFAULT_MAX_BURST_LENGTH; 4688 ipgp->g_value.v_integer.i_min = 512; 4689 ipgp->g_value.v_integer.i_max = ISCSI_MAX_BURST_LENGTH; 4690 ipgp->g_value.v_integer.i_incr = 1; 4691 break; 4692 case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS: 4693 ipgp->g_value.v_integer.i_current = 4694 params->max_connections; 4695 ipgp->g_value.v_settable = B_FALSE; 4696 ipgp->g_value.v_integer.i_default = 4697 ISCSI_DEFAULT_MAX_CONNECTIONS; 4698 ipgp->g_value.v_integer.i_min = 1; 4699 ipgp->g_value.v_integer.i_max = ISCSI_MAX_CONNECTIONS; 4700 ipgp->g_value.v_integer.i_incr = 1; 4701 break; 4702 case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T: 4703 ipgp->g_value.v_integer.i_current = 4704 params->max_outstanding_r2t; 4705 ipgp->g_value.v_settable = B_FALSE; 4706 ipgp->g_value.v_integer.i_default = 4707 ISCSI_DEFAULT_MAX_OUT_R2T; 4708 ipgp->g_value.v_integer.i_min = 1; 4709 ipgp->g_value.v_integer.i_max = ISCSI_MAX_OUTSTANDING_R2T; 4710 ipgp->g_value.v_integer.i_incr = 1; 4711 break; 4712 case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH: 4713 ipgp->g_value.v_integer.i_current = 4714 params->max_recv_data_seg_len; 4715 ipgp->g_value.v_integer.i_default = 4716 ISCSI_DEFAULT_MAX_RECV_SEG_LEN; 4717 ipgp->g_value.v_integer.i_min = 512; 4718 ipgp->g_value.v_integer.i_max = 4719 ISCSI_MAX_RECV_DATA_SEGMENT_LENGTH; 4720 ipgp->g_value.v_integer.i_incr = 1; 4721 break; 4722 default: 4723 rtn = EINVAL; 4724 } 4725 4726 return (rtn); 4727 } 4728 4729 /* 4730 * +--------------------------------------------------------------------+ 4731 * | End of ioctl utility routines | 4732 * +--------------------------------------------------------------------+ 4733 */ 4734 4735 /* 4736 * iscsi_get_name_from_iqn - Translates a normal iqn/eui into a 4737 * IEEE safe address. IEEE addresses have a number of characters 4738 * set aside as reserved. 4739 */ 4740 static void 4741 iscsi_get_name_from_iqn(char *name, int name_max_len) 4742 { 4743 char *tmp = NULL; 4744 char *oldch = NULL; 4745 char *newch = NULL; 4746 4747 tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP); 4748 4749 for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0'; 4750 oldch++, newch++) { 4751 switch (*oldch) { 4752 case ':': 4753 *newch++ = '%'; 4754 *newch++ = '3'; 4755 *newch = 'A'; 4756 break; 4757 case ' ': 4758 *newch++ = '%'; 4759 *newch++ = '2'; 4760 *newch = '0'; 4761 break; 4762 case '@': 4763 *newch++ = '%'; 4764 *newch++ = '4'; 4765 *newch = '0'; 4766 break; 4767 case '/': 4768 *newch++ = '%'; 4769 *newch++ = '2'; 4770 *newch = 'F'; 4771 break; 4772 default: 4773 *newch = *oldch; 4774 } 4775 } 4776 (void) strncpy(name, tmp, name_max_len); 4777 kmem_free(tmp, MAX_GET_NAME_SIZE); 4778 } 4779 4780 /* 4781 * iscsi_get_name_to_iqn - Converts IEEE safe address back 4782 * into a iscsi iqn/eui. 4783 */ 4784 static void 4785 iscsi_get_name_to_iqn(char *name, int name_max_len) 4786 { 4787 char *tmp = NULL; 4788 char *oldch = NULL; 4789 char *newch = NULL; 4790 4791 tmp = kmem_zalloc(MAX_GET_NAME_SIZE, KM_SLEEP); 4792 4793 for (oldch = &name[0], newch = &tmp[0]; *oldch != '\0'; 4794 oldch++, newch++) { 4795 if (*oldch == '%') { 4796 switch (*(oldch+1)) { 4797 case '2': 4798 if (*(oldch+2) == '0') { 4799 *newch = ' '; 4800 oldch += 2; 4801 } else if (*(oldch+2) == 'F') { 4802 *newch = '/'; 4803 oldch += 2; 4804 } else { 4805 *newch = *oldch; 4806 } 4807 break; 4808 case '3': 4809 if (*(oldch+2) == 'A') { 4810 *newch = ':'; 4811 oldch += 2; 4812 } else { 4813 *newch = *oldch; 4814 } 4815 break; 4816 case '4': 4817 if (*(oldch+2) == '0') { 4818 *newch = '@'; 4819 oldch += 2; 4820 } else { 4821 *newch = *oldch; 4822 } 4823 break; 4824 default: 4825 *newch = *oldch; 4826 } 4827 } else { 4828 *newch = *oldch; 4829 } 4830 } 4831 (void) strncpy(name, tmp, name_max_len); 4832 kmem_free(tmp, MAX_GET_NAME_SIZE); 4833 } 4834 4835 /* 4836 * iscsi_get_persisted_param * - a helper to ISCSI_GET_PARAM ioctl 4837 * 4838 * On return 0 means persisted parameter found 4839 */ 4840 int 4841 iscsi_get_persisted_param(uchar_t *name, iscsi_param_get_t *ipgp, 4842 iscsi_login_params_t *params) 4843 { 4844 int rtn = 1; 4845 persistent_param_t *pparam; 4846 4847 if (name == NULL || strlen((char *)name) == 0) { 4848 return (rtn); 4849 } 4850 4851 pparam = (persistent_param_t *)kmem_zalloc(sizeof (*pparam), KM_SLEEP); 4852 4853 if (persistent_param_get((char *)name, pparam) == B_TRUE) { 4854 if (pparam->p_bitmap & (1 << ipgp->g_param)) { 4855 /* Found configured parameter. */ 4856 bcopy(&pparam->p_params, params, sizeof (*params)); 4857 rtn = 0; 4858 } 4859 } 4860 4861 kmem_free(pparam, sizeof (*pparam)); 4862 4863 return (rtn); 4864 } 4865 4866 /* 4867 * iscsi_override_target_default - helper function set the target's default 4868 * login parameter if there is a configured initiator parameter. 4869 * 4870 */ 4871 static void 4872 iscsi_override_target_default(iscsi_hba_t *ihp, iscsi_param_get_t *ipg) 4873 { 4874 persistent_param_t *pp; 4875 iscsi_login_params_t *params; 4876 4877 pp = (persistent_param_t *)kmem_zalloc(sizeof (*pp), KM_SLEEP); 4878 if (persistent_param_get((char *)ihp->hba_name, pp) == B_TRUE) { 4879 if (pp->p_bitmap & (1 << ipg->g_param)) { 4880 params = &pp->p_params; 4881 switch (ipg->g_param) { 4882 case ISCSI_LOGIN_PARAM_DATA_SEQUENCE_IN_ORDER: 4883 ipg->g_value.v_bool.b_default = 4884 params->data_sequence_in_order; 4885 break; 4886 case ISCSI_LOGIN_PARAM_IMMEDIATE_DATA: 4887 ipg->g_value.v_bool.b_default = 4888 params->immediate_data; 4889 break; 4890 case ISCSI_LOGIN_PARAM_INITIAL_R2T: 4891 ipg->g_value.v_bool.b_default = 4892 params->initial_r2t; 4893 break; 4894 case ISCSI_LOGIN_PARAM_DATA_PDU_IN_ORDER: 4895 ipg->g_value.v_bool.b_default = 4896 params->data_pdu_in_order; 4897 break; 4898 case ISCSI_LOGIN_PARAM_HEADER_DIGEST: 4899 ipg->g_value.v_integer.i_default = 4900 params->header_digest; 4901 break; 4902 case ISCSI_LOGIN_PARAM_DATA_DIGEST: 4903 ipg->g_value.v_integer.i_default = 4904 params->data_digest; 4905 break; 4906 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_RETAIN: 4907 ipg->g_value.v_integer.i_default = 4908 params->default_time_to_retain; 4909 break; 4910 case ISCSI_LOGIN_PARAM_DEFAULT_TIME_2_WAIT: 4911 ipg->g_value.v_integer.i_default = 4912 params->default_time_to_wait; 4913 break; 4914 case ISCSI_LOGIN_PARAM_ERROR_RECOVERY_LEVEL: 4915 ipg->g_value.v_integer.i_default = 4916 params->error_recovery_level; 4917 break; 4918 case ISCSI_LOGIN_PARAM_FIRST_BURST_LENGTH: 4919 ipg->g_value.v_integer.i_default = 4920 params->first_burst_length; 4921 break; 4922 case ISCSI_LOGIN_PARAM_MAX_BURST_LENGTH: 4923 ipg->g_value.v_integer.i_default = 4924 params->max_burst_length; 4925 break; 4926 case ISCSI_LOGIN_PARAM_MAX_CONNECTIONS: 4927 ipg->g_value.v_integer.i_default = 4928 params->max_connections; 4929 break; 4930 case ISCSI_LOGIN_PARAM_OUTSTANDING_R2T: 4931 ipg->g_value.v_integer.i_default = 4932 params->max_outstanding_r2t; 4933 break; 4934 case ISCSI_LOGIN_PARAM_MAX_RECV_DATA_SEGMENT_LENGTH: 4935 ipg->g_value.v_integer.i_default = 4936 params->max_xmit_data_seg_len; 4937 break; 4938 default: 4939 break; 4940 } 4941 } 4942 } 4943 kmem_free(pp, sizeof (*pp)); 4944 } 4945 4946 static boolean_t 4947 iscsi_cmp_boot_sess_oid(iscsi_hba_t *ihp, uint32_t oid) 4948 { 4949 iscsi_sess_t *isp = NULL; 4950 4951 if (iscsi_chk_bootlun_mpxio(ihp)) { 4952 for (isp = ihp->hba_sess_list; isp; isp = isp->sess_next) { 4953 if ((isp->sess_oid == oid) && isp->sess_boot) { 4954 /* oid is session object */ 4955 break; 4956 } 4957 if ((isp->sess_target_oid == oid) && isp->sess_boot) { 4958 /* 4959 * oid is target object while 4960 * this session is boot session 4961 */ 4962 break; 4963 } 4964 } 4965 if (oid == ihp->hba_oid) { 4966 /* oid is initiator object id */ 4967 return (B_TRUE); 4968 } else if ((isp != NULL) && (isp->sess_boot)) { 4969 /* oid is boot session object id */ 4970 return (B_TRUE); 4971 } 4972 } 4973 return (B_FALSE); 4974 } 4975